DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
digitalSurfaceSlice.cpp
1 
32 
33 #include <iostream>
34 #include <QtGui/qapplication.h>
35 #include "DGtal/base/Common.h"
36 
37 #include "DGtal/helpers/StdDefs.h"
38 #include "DGtal/topology/KhalimskySpaceND.h"
39 #include "DGtal/topology/SurfelAdjacency.h"
40 #include "DGtal/topology/DigitalSurface.h"
41 #include "DGtal/topology/SetOfSurfels.h"
42 #include "DGtal/topology/DigitalSurface2DSlice.h"
43 #include "DGtal/topology/helpers/Surfaces.h"
44 
45 #include "DGtal/io/viewers/Viewer3D.h"
46 #include "DGtal/io/readers/VolReader.h"
47 #include "DGtal/io/DrawWithDisplay3DModifier.h"
48 #include "DGtal/images/ImageSelector.h"
49 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
50 #include "DGtal/io/Color.h"
51 #include "DGtal/io/colormaps/GradientColorMap.h"
52 
53 #include "ConfigExamples.h"
54 
56 using namespace std;
57 using namespace DGtal;
58 using namespace Z3i;
60 
61 int main( int argc, char** argv )
62 {
63  // for 3D display with Viewer3D
64  QApplication application(argc,argv);
65 
67  trace.beginBlock( "Reading vol file into an image." );
69  std::string inputFilename = examplesPath + "samples/Al.100.vol";
70  Image image = VolReader<Image>::importVol(inputFilename);
71  DigitalSet set3d (image.domain());
72  SetPredicate<DigitalSet> set3dPredicate( set3d );
73  SetFromImage<DigitalSet>::append<Image>(set3d, image,
74  0, 1 );
75  Viewer3D viewer;
76  viewer.show();
77  trace.endBlock();
79 
81  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
82  KSpace ks;
83  bool space_ok = ks.init( image.domain().lowerBound(),
84  image.domain().upperBound(), true );
85  if (!space_ok)
86  {
87  trace.error() << "Error in the Khamisky space construction."<<std::endl;
88  return 2;
89  }
90  trace.endBlock();
92 
94  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
95  MySurfelAdjacency surfAdj( true ); // interior in all directions.
97 
99  trace.beginBlock( "Extracting boundary by scanning the space. " );
100  typedef KSpace::Surfel Surfel;
101  typedef KSpace::SurfelSet SurfelSet;
102  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
104  MySetOfSurfels theSetOfSurfels( ks, surfAdj );
105  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
106  ks, set3dPredicate,
107  image.domain().lowerBound(),
108  image.domain().upperBound() );
109  MyDigitalSurface digSurf( theSetOfSurfels );
110  trace.info() << "Digital surface has " << digSurf.size() << " surfels."
111  << std::endl;
112  trace.endBlock();
114 
116  trace.beginBlock( "Extract slices." );
117  typedef MyDigitalSurface::DigitalSurfaceTracker MyTracker;
118  typedef DigitalSurface2DSlice< MyTracker > My2DSlice;
119  //Extract an initial boundary cell
120  Surfel surf = *digSurf.begin();
121  MyTracker* tracker1 = digSurf.container().newTracker( surf );
122  MyTracker* tracker2 = digSurf.container().newTracker( surf );
123  // Extract the bondary contour associated to the initial surfel in
124  // its first direction
125  My2DSlice slice1( tracker1, *(ks.sDirs( surf )) );
126  // Extract the bondary contour associated to the initial surfel in
127  // its second direction
128  My2DSlice slice2( tracker2, *++(ks.sDirs( surf )) );
129  delete tracker1;
130  delete tracker2;
131  trace.endBlock();
133 
134  ASSERT( slice1.start() == slice1.begin() );
135  ASSERT( slice1.cstart() == slice1.c() );
136  ASSERT( *slice1.begin() == surf );
137  ASSERT( *slice1.c() == surf );
138  ASSERT( *slice1.start() == surf );
139  ASSERT( *slice1.cstart() == surf );
140  ASSERT( *slice1.rcstart() == surf );
141  ASSERT( slice1.rcstart() == slice1.rc() );
142  ASSERT( *slice1.rc() == surf );
143  ASSERT( *(slice1.c()+1) == *(slice1.begin()+1) );
144  ASSERT( *(slice1.rc()+1) == *(slice1.rbegin()) );
145 
147  trace.beginBlock( "Display all with QGLViewer." );
148  // Displaying all the surfels in transparent mode
149  viewer << SetMode3D( surf.className(), "Transparent");
150  for( MyDigitalSurface::ConstIterator it = theSetOfSurfels.begin(),
151  it_end = theSetOfSurfels.end(); it != it_end; ++it )
152  viewer<< *it;
153 
154  // Displaying First surfels cut with gradient colors.;
155  GradientColorMap<int> cmap_grad( 0, slice1.size() );
156  cmap_grad.addColor( Color( 50, 50, 255 ) );
157  cmap_grad.addColor( Color( 255, 0, 0 ) );
158  cmap_grad.addColor( Color( 255, 255, 10 ) );
159 
160  // Need to avoid surfel superposition (the surfel size in increased)
161  viewer << Viewer3D::shiftSurfelVisu;
162  viewer << SetMode3D( surf.className(), "");
163  viewer.setFillColor(Color(180, 200, 25, 255));
164 
165  int d=0;
166  for ( My2DSlice::ConstIterator it = slice1.begin(),
167  it_end = slice1.end(); it != it_end; ++it )
168  {
169  Color col= cmap_grad(d);
170  viewer.setFillColor(Color(col.red(),col.green() ,col.blue(), 255));
171  viewer<< *it;
172  d++;
173  }
174 
175  GradientColorMap<int> cmap_grad2( 0, slice2.size() );
176  cmap_grad2.addColor( Color( 50, 50, 255 ) );
177  cmap_grad2.addColor( Color( 255, 0, 0 ) );
178  cmap_grad2.addColor( Color( 255, 255, 10 ) );
179 
180  d=0;
181  for ( My2DSlice::ConstIterator it = slice2.begin(),
182  it_end = slice2.end(); it != it_end; ++it )
183  {
184  Color col= cmap_grad2(d);
185  viewer.setFillColor(Color(col.red(),col.green() ,col.blue(), 255));
186  viewer<< *it;
187  d++;
188  }
189 
190  // One need once again to avoid superposition.
191  viewer << Viewer3D::shiftSurfelVisu;
192  viewer.setFillColor(Color(18, 200, 25, 255));
193  viewer << surf ;
194  viewer << Viewer3D::updateDisplay;
195  trace.endBlock();
196 
197  return application.exec();
199 }
200 // //