DGtal  0.9.3beta
digitalSurfaceSlice.cpp
1 
32 #include <iostream>
34 
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/readers/VolReader.h"
46 #include "DGtal/io/DrawWithDisplay3DModifier.h"
47 #include "DGtal/io/viewers/Viewer3D.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  SetFromImage<DigitalSet>::append<Image>(set3d, image,
73  0, 1 );
74  Viewer3D<> viewer;
75  viewer.show();
76  trace.endBlock();
78 
80  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
81  KSpace ks;
82  bool space_ok = ks.init( image.domain().lowerBound(),
83  image.domain().upperBound(), true );
84  if (!space_ok)
85  {
86  trace.error() << "Error in the Khamisky space construction."<<std::endl;
87  return 2;
88  }
89  trace.endBlock();
91 
93  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
94  MySurfelAdjacency surfAdj( true ); // interior in all directions.
96 
98  trace.beginBlock( "Extracting boundary by scanning the space. " );
99  typedef KSpace::Surfel Surfel;
100  typedef KSpace::SurfelSet SurfelSet;
101  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
103  MySetOfSurfels theSetOfSurfels( ks, surfAdj );
104  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
105  ks, set3d,
106  image.domain().lowerBound(),
107  image.domain().upperBound() );
108  MyDigitalSurface digSurf( theSetOfSurfels );
109  trace.info() << "Digital surface has " << digSurf.size() << " surfels."
110  << std::endl;
111  trace.endBlock();
113 
115  trace.beginBlock( "Extract slices." );
116  typedef MyDigitalSurface::DigitalSurfaceTracker MyTracker;
117  typedef DigitalSurface2DSlice< MyTracker > My2DSlice;
118  //Extract an initial boundary cell
119  Surfel surf = *digSurf.begin();
120  MyTracker* tracker1 = digSurf.container().newTracker( surf );
121  MyTracker* tracker2 = digSurf.container().newTracker( surf );
122  // Extract the bondary contour associated to the initial surfel in
123  // its first direction
124  My2DSlice slice1( tracker1, *(ks.sDirs( surf )) );
125  // Extract the bondary contour associated to the initial surfel in
126  // its second direction
127  My2DSlice slice2( tracker2, *++(ks.sDirs( surf )) );
128  delete tracker1;
129  delete tracker2;
130  trace.endBlock();
132 
133  ASSERT( slice1.start() == slice1.begin() );
134  ASSERT( slice1.cstart() == slice1.c() );
135  ASSERT( *slice1.begin() == surf );
136  ASSERT( *slice1.c() == surf );
137  ASSERT( *slice1.start() == surf );
138  ASSERT( *slice1.cstart() == surf );
139  ASSERT( *slice1.rcstart() == surf );
140  ASSERT( slice1.rcstart() == slice1.rc() );
141  ASSERT( *slice1.rc() == surf );
142  ASSERT( *(slice1.c()+1) == *(slice1.begin()+1) );
143  ASSERT( *(slice1.rc()+1) == *(slice1.rbegin()) );
144 
146  trace.beginBlock( "Display all with QGLViewer." );
147  // Displaying all the surfels in transparent mode
148  viewer << SetMode3D( surf.className(), "Transparent");
149  for( MyDigitalSurface::ConstIterator it = theSetOfSurfels.begin(),
150  it_end = theSetOfSurfels.end(); it != it_end; ++it )
151  viewer<< *it;
152 
153  // Displaying First surfels cut with gradient colors.;
154  GradientColorMap<int> cmap_grad( 0, slice1.size() );
155  cmap_grad.addColor( Color( 50, 50, 255 ) );
156  cmap_grad.addColor( Color( 255, 0, 0 ) );
157  cmap_grad.addColor( Color( 255, 255, 10 ) );
158 
159  // Need to avoid surfel superposition (the surfel size in increased)
160  viewer << Viewer3D<>::shiftSurfelVisu;
161  viewer << SetMode3D( surf.className(), "");
162  viewer.setFillColor(Color(180, 200, 25, 255));
163 
164  int d=0;
165  for ( My2DSlice::ConstIterator it = slice1.begin(),
166  it_end = slice1.end(); it != it_end; ++it )
167  {
168  Color col= cmap_grad(d);
169  viewer.setFillColor(Color(col.red(),col.green() ,col.blue(), 255));
170  viewer<< *it;
171  d++;
172  }
173 
174  GradientColorMap<int> cmap_grad2( 0, slice2.size() );
175  cmap_grad2.addColor( Color( 50, 50, 255 ) );
176  cmap_grad2.addColor( Color( 255, 0, 0 ) );
177  cmap_grad2.addColor( Color( 255, 255, 10 ) );
178 
179  d=0;
180  for ( My2DSlice::ConstIterator it = slice2.begin(),
181  it_end = slice2.end(); it != it_end; ++it )
182  {
183  Color col= cmap_grad2(d);
184  viewer.setFillColor(Color(col.red(),col.green() ,col.blue(), 255));
185  viewer<< *it;
186  d++;
187  }
188 
189  // One need once again to avoid superposition.
190  viewer << Viewer3D<>::shiftSurfelVisu;
191  viewer.setFillColor(Color(18, 200, 25, 255));
192  viewer << surf ;
193  viewer << Viewer3D<>::updateDisplay;
194  trace.endBlock();
195 
196  return application.exec();
198 }
199 // //
void beginBlock(const std::string &keyword="")
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition: StdDefs.h:100
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:137
DigitalSurfaceContainer::SurfelConstIterator ConstIterator
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
KhalimskySpaceND< 2, Integer > KSpace
Definition: StdDefs.h:77
void addColor(const Color &color)
STL namespace.
double endBlock()
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
DigitalSurfaceContainer::DigitalSurfaceTracker DigitalSurfaceTracker
ConstIterator begin() const
Aim: Represents a 2-dimensional slice in a DigitalSurface. In a sense, it is a 4-connected contour...
Aim: implements methods to read a "Vol" file format.
Definition: VolReader.h:88
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: Define utilities to convert a digital set into an image.
Definition: SetFromImage.h:63
std::ostream & info()
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as connected surfels...
Definition: SetOfSurfels.h:73
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
void green(const unsigned char aGreenValue)
const Domain & domain() const
Definition: Image.h:192
void red(const unsigned char aRedValue)
std::ostream & error()
Modifier class in a Display3D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithDisplay3D.
void blue(const unsigned char aBlueValue)