DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
volIVViewer.cpp
1 
30 
31 #include <iostream>
32 #include <Qt/qapplication.h>
33 #include <Inventor/nodes/SoCube.h>
34 #include "DGtal/io/viewers/DGtalInventor.h"
35 #include "DGtal/io/viewers/IVViewer.h"
36 #include "DGtal/base/Common.h"
37 #include "DGtal/kernel/domains/HyperRectDomain.h"
38 #include "DGtal/images/ImageSelector.h"
39 #include "DGtal/io/readers/VolReader.h"
40 #include "DGtal/io/Color.h"
41 #include "DGtal/io/colormaps/GradientColorMap.h"
42 #include "DGtal/topology/SurfelAdjacency.h"
43 #include "DGtal/topology/SurfelNeighborhood.h"
44 #include "DGtal/topology/KhalimskySpaceND.h"
45 #include "DGtal/helpers/StdDefs.h"
46 #include "DGtal/shapes/Shapes.h"
47 #include "DGtal/topology/helpers/Surfaces.h"
48 #include "ConfigTest.h"
49 
51 
52 using namespace std;
53 using namespace DGtal;
54 
56 // Functions for testing class IVViewer.
58 using namespace Z3i;
59 
60 
61 void addAxis( DGtalInventor<Space> & inventor,
62  const Point & low,
63  const Point & up,
64  Dimension k,
65  const DGtal::Color & c1,
66  const DGtal::Color & c2 )
67 {
69  // @todo GradientColorMap seems to have a bug
70  GradientColorMap<int> cmap_grad( 0, up[ k ] - low[ k ] );
71  cmap_grad.clearColors();
72  cmap_grad.addColor( c1 );
73  cmap_grad.addColor( c2 );
74  Point p( low );
75  for ( Point::Component x = low[ k ]; x <= up[ k ]; ++x )
76  {
77  p[ k ] = x;
78  DGtal::Color c( cmap_grad( x - low[ k ] ) );
79  inventor.setDiffuseColor( Color( c.red(), c.green(), c.blue() ) );
80  inventor.drawPoint( p );
81  }
82 }
83 void addBounds( DGtalInventor<Space> & inventor,
84  const Point & low,
85  const Point & up )
86 {
87  typedef DGtalInventor<Space>::Color Color;
88  Point p( low );
89  for ( Dimension i = 0; i < 8; ++i )
90  {
91  Color c;
92  for ( Dimension j = 0; j < 3; ++j )
93  {
94  p[ j ] = ( i & ( 1 << j ) ) ? up[ j ] : low[ j ];
95  c[ j ] = ( i & ( 1 << j ) ) ? 1.0 : 0.1 ;
96  }
97  inventor.setDiffuseColor( c ); //Color( 0.1, 0.1, 0.1 ) );
98  inventor.drawPoint( p );
99  }
100  // addAxis( inventor, low, up, 0,
101  // LibBoard::Color::Black, LibBoard::Color::Blue );
102  // addAxis( inventor, low, up, 1,
103  // LibBoard::Color::Black, LibBoard::Color::Green );
104  // addAxis( inventor, low, up, 2,
105  // LibBoard::Color::Black, LibBoard::Color::Red );
106 }
107 
108 int main( int argc, char** argv )
109 {
110  QApplication app( argc, argv );
111  trace.beginBlock ( "Testing class IVViewer" );
112  IVViewer ivv( argc, argv );
113 
114  // Load volumetric image
116  std::string filename = testPath + "samples/cat10.vol";
117  Image image = VolReader<Image>::importVol( filename );
118  trace.info() << image <<endl;
119 
120  // make shape.
121  Domain domain = image.domain() ;
122  DigitalSet shape_set( domain );
123  SetPredicate<DigitalSet> shape_set_predicate( shape_set );
124  for ( Domain::ConstIterator it = domain.begin(), itend = domain.end();
125  it != itend;
126  ++it )
127  {
128  if ( image( *it ) != 0 )
129  shape_set.insert( *it );
130  }
131 
132  // find first surfel on the boundary (not nice).
133  Point first;
134  for ( Domain::ConstIterator it = domain.begin(), itend = domain.end();
135  it != itend;
136  ++it )
137  {
138  if ( image( *it ) != 0 )
139  {
140  first = *it;
141  break;
142  }
143  }
144 
145  // Builds Khalimsky space.
146  typedef KhalimskySpaceND<3> KSpace;
147  typedef KSpace::SCell SCell;
148  KSpace K3;
150  K3.init( image.domain().lowerBound(), image.domain().upperBound(), true );
151 
152  // Tracks the shape boundary.
153  SCell intvoxel = K3.sSpel( first );
154  SCell surfel = K3.sIncident( intvoxel, 0, false );
155  std::set<SCell> bdry;
157  K3, SAdj, shape_set_predicate, surfel );
158 
159  trace.info() << "tracking finished, size=" << bdry.size() << endl;
160 
161  // Display surface.
162  DGtalInventor<Space> inventor;
163  typedef DGtalInventor<Space>::Color Color;
164  addBounds( inventor, image.domain().lowerBound(), image.domain().upperBound() );
165  for ( std::set<SCell>::const_iterator it = bdry.begin(), itend = bdry.end();
166  it != itend;
167  ++it )
168  {
169  inventor.setDiffuseColor( Color( 0.7, 0.7, 1.0 ) );
170  inventor.drawCell( K3.sKCoords( *it ),
171  ! K3.sDirect( *it, K3.sOrthDir( *it ) ) );
172  }
173  // start surfel is red.
174  inventor.setDiffuseColor( Color( 1.0, 0.0, 0.0 ) );
175  inventor.drawCell( K3.sKCoords( surfel ),
176  ! K3.sDirect( surfel, K3.sOrthDir( surfel ) ) );
177  inventor.generate( ivv.root() );
178  // Qt will get the hand.
179  ivv.show();
180 
181  bool res = true;
182  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
183  trace.endBlock();
184  return res ? 0 : 1;
185 }