DGtal  0.9.4beta
digitalSurfaceSlice.cpp
1 
41 #include <iostream>
43 
44 #include "DGtal/base/Common.h"
45 
46 #include "DGtal/helpers/StdDefs.h"
47 #include "DGtal/topology/KhalimskySpaceND.h"
48 #include "DGtal/topology/SurfelAdjacency.h"
49 #include "DGtal/topology/DigitalSurface.h"
50 #include "DGtal/topology/SetOfSurfels.h"
51 #include "DGtal/topology/DigitalSurface2DSlice.h"
52 #include "DGtal/topology/helpers/Surfaces.h"
53 
54 #include "DGtal/io/readers/VolReader.h"
55 #include "DGtal/io/DrawWithDisplay3DModifier.h"
56 #include "DGtal/io/viewers/Viewer3D.h"
57 #include "DGtal/images/ImageSelector.h"
58 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
59 #include "DGtal/io/Color.h"
60 #include "DGtal/io/colormaps/GradientColorMap.h"
61 
62 #include "ConfigExamples.h"
63 
65 using namespace std;
66 using namespace DGtal;
67 using namespace Z3i;
69 
70 int main( int argc, char** argv )
71 {
72  // for 3D display with Viewer3D
73  QApplication application(argc,argv);
74 
76  trace.beginBlock( "Reading vol file into an image." );
78  std::string inputFilename = examplesPath + "samples/Al.100.vol";
79  Image image = VolReader<Image>::importVol(inputFilename);
80  DigitalSet set3d (image.domain());
81  SetFromImage<DigitalSet>::append<Image>(set3d, image,
82  0, 1 );
83  Viewer3D<> viewer;
84  viewer.show();
85  trace.endBlock();
87 
89  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
90  KSpace ks;
91  bool space_ok = ks.init( image.domain().lowerBound(),
92  image.domain().upperBound(), true );
93  if (!space_ok)
94  {
95  trace.error() << "Error in the Khamisky space construction."<<std::endl;
96  return 2;
97  }
98  trace.endBlock();
100 
102  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
103  MySurfelAdjacency surfAdj( true ); // interior in all directions.
105 
107  trace.beginBlock( "Extracting boundary by scanning the space. " );
108  typedef KSpace::Surfel Surfel;
109  typedef KSpace::SurfelSet SurfelSet;
110  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
112  MySetOfSurfels theSetOfSurfels( ks, surfAdj );
113  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
114  ks, set3d,
115  image.domain().lowerBound(),
116  image.domain().upperBound() );
117  MyDigitalSurface digSurf( theSetOfSurfels );
118  trace.info() << "Digital surface has " << digSurf.size() << " surfels."
119  << std::endl;
120  trace.endBlock();
122 
124  trace.beginBlock( "Extract slices." );
125  typedef MyDigitalSurface::DigitalSurfaceTracker MyTracker;
126  typedef DigitalSurface2DSlice< MyTracker > My2DSlice;
127  //Extract an initial boundary cell
128  Surfel surf = *digSurf.begin();
129  MyTracker* tracker1 = digSurf.container().newTracker( surf );
130  MyTracker* tracker2 = digSurf.container().newTracker( surf );
131  // Extract the bondary contour associated to the initial surfel in
132  // its first direction
133  My2DSlice slice1( tracker1, *(ks.sDirs( surf )) );
134  // Extract the bondary contour associated to the initial surfel in
135  // its second direction
136  My2DSlice slice2( tracker2, *++(ks.sDirs( surf )) );
137  delete tracker1;
138  delete tracker2;
139  trace.endBlock();
141 
142  ASSERT( slice1.start() == slice1.begin() );
143  ASSERT( slice1.cstart() == slice1.c() );
144  ASSERT( *slice1.begin() == surf );
145  ASSERT( *slice1.c() == surf );
146  ASSERT( *slice1.start() == surf );
147  ASSERT( *slice1.cstart() == surf );
148  ASSERT( *slice1.rcstart() == surf );
149  ASSERT( slice1.rcstart() == slice1.rc() );
150  ASSERT( *slice1.rc() == surf );
151  ASSERT( *(slice1.c()+1) == *(slice1.begin()+1) );
152  ASSERT( *(slice1.rc()+1) == *(slice1.rbegin()) );
153 
155  trace.beginBlock( "Display all with QGLViewer." );
156  // Displaying all the surfels in transparent mode
157  viewer << SetMode3D( surf.className(), "Transparent");
158  for( MyDigitalSurface::ConstIterator it = theSetOfSurfels.begin(),
159  it_end = theSetOfSurfels.end(); it != it_end; ++it )
160  viewer<< *it;
161 
162  // Displaying First surfels cut with gradient colors.;
163  GradientColorMap<int> cmap_grad( 0, slice1.size() );
164  cmap_grad.addColor( Color( 50, 50, 255 ) );
165  cmap_grad.addColor( Color( 255, 0, 0 ) );
166  cmap_grad.addColor( Color( 255, 255, 10 ) );
167 
168  // Need to avoid surfel superposition (the surfel size in increased)
169  viewer << Viewer3D<>::shiftSurfelVisu;
170  viewer << SetMode3D( surf.className(), "");
171  viewer.setFillColor(Color(180, 200, 25, 255));
172 
173  int d=0;
174  for ( My2DSlice::ConstIterator it = slice1.begin(),
175  it_end = slice1.end(); it != it_end; ++it )
176  {
177  Color col= cmap_grad(d);
178  viewer.setFillColor(Color(col.red(),col.green() ,col.blue(), 255));
179  viewer<< *it;
180  d++;
181  }
182 
183  GradientColorMap<int> cmap_grad2( 0, slice2.size() );
184  cmap_grad2.addColor( Color( 50, 50, 255 ) );
185  cmap_grad2.addColor( Color( 255, 0, 0 ) );
186  cmap_grad2.addColor( Color( 255, 255, 10 ) );
187 
188  d=0;
189  for ( My2DSlice::ConstIterator it = slice2.begin(),
190  it_end = slice2.end(); it != it_end; ++it )
191  {
192  Color col= cmap_grad2(d);
193  viewer.setFillColor(Color(col.red(),col.green() ,col.blue(), 255));
194  viewer<< *it;
195  d++;
196  }
197 
198  // One need once again to avoid superposition.
199  viewer << Viewer3D<>::shiftSurfelVisu;
200  viewer.setFillColor(Color(18, 200, 25, 255));
201  viewer << surf ;
202  viewer << Viewer3D<>::updateDisplay;
203  trace.endBlock();
204 
205  return application.exec();
207 }
208 // //
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
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:89
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
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
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)