DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
exampleGridCurve3d-2.cpp
1 
31 
32 #include <iostream>
33 #include <QtGui/qapplication.h>
34 #include "DGtal/base/Common.h"
35 
36 #include "DGtal/helpers/StdDefs.h"
37 #include "DGtal/topology/KhalimskySpaceND.h"
38 #include "DGtal/topology/SurfelAdjacency.h"
39 #include "DGtal/topology/DigitalSurface.h"
40 #include "DGtal/topology/SetOfSurfels.h"
41 #include "DGtal/topology/DigitalSurface2DSlice.h"
42 #include "DGtal/topology/helpers/Surfaces.h"
43 
44 #include "DGtal/io/viewers/Viewer3D.h"
45 #include "DGtal/io/readers/VolReader.h"
46 #include "DGtal/io/DrawWithDisplay3DModifier.h"
47 #include "DGtal/images/ImageSelector.h"
48 #include "DGtal/images/ImageHelper.h"
49 #include "DGtal/kernel/sets/DigitalSetInserter.h"
50 #include "DGtal/io/Color.h"
51 
52 #include "DGtal/geometry/curves/GridCurve.h"
53 
54 #include "ConfigExamples.h"
55 
57 #include <boost/program_options/options_description.hpp>
58 #include <boost/program_options/parsers.hpp>
59 #include <boost/program_options/variables_map.hpp>
60 
61 namespace po = boost::program_options;
62 
64 using namespace std;
65 using namespace DGtal;
66 using namespace Z3i;
68 
69 int main( int argc, char** argv )
70 {
71 
73  // parse command line
74  po::options_description general_opt("Allowed options are");
75  general_opt.add_options()
76  ("help,h", "display this message")
77  ("range,r", po::value<string>()->default_value("gridcurve"),
78  " Either <gridcurve> (default), <inner>, <outer>, <incident> " );
79 
80  po::variables_map vm;
81  po::store(po::parse_command_line(argc, argv, general_opt), vm);
82  po::notify(vm);
83  if(vm.count("help"))
84  {
85  trace.info()<< "exampleGridCurve3d" << std::endl
86  << "Basic usage: "<<std::endl
87  << argv[0] << " " << std::endl
88  << general_opt << "\n";
89  return 0;
90  }
91 
92  //Parse options
93  string type = vm["range"].as<string>();
94 
95 
96  //vol reading and digital set construction
97  trace.beginBlock( "Reading vol file into an image." );
99  std::string inputFilename = examplesPath + "samples/cat10.vol";
100  Image image = VolReader<Image>::importVol(inputFilename);
101  DigitalSet set3d (image.domain());
102  SetPredicate<DigitalSet> set3dPredicate( set3d );
103  setFromImage( image, DigitalSetInserter<DigitalSet>(set3d), 1);
104  trace.info() << set3d.size() << " voxels." << std::endl;
105  trace.endBlock();
106 
107  //Khalimsky space construction
108  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
109  KSpace ks;
110 /* bool space_ok = ks.init( image.domain().lowerBound(),
111  image.domain().upperBound(), true );
112  if (!space_ok)
113  {
114  trace.error() << "Error in the Khamisky space construction."<<std::endl;
115  return 2;
116  }*/
117  trace.endBlock();
118 
119  //digital surface construction
120  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
121  MySurfelAdjacency surfAdj( true ); // interior in all directions.
122 
123  trace.beginBlock( "Extracting boundary by scanning the space. " );
124  typedef KSpace::Surfel Surfel;
125  typedef KSpace::SurfelSet SurfelSet;
126  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
128  MySetOfSurfels theSetOfSurfels( ks, surfAdj );
129  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
130  ks, set3dPredicate,
131  image.domain().lowerBound(),
132  image.domain().upperBound() );
133  MyDigitalSurface digSurf( theSetOfSurfels );
134  trace.info() << digSurf.size() << " surfels." << std::endl;
135  trace.endBlock();
136 
137  //slice retrieving
138  trace.beginBlock( "Extracting slice and constructing a grid curve. " );
139  typedef MyDigitalSurface::DigitalSurfaceTracker MyTracker;
140  typedef DigitalSurface2DSlice< MyTracker > My2DSlice;
141 
142  //Extract an initial boundary cell
143  Surfel surf = *digSurf.begin();
144  MyTracker* tracker = digSurf.container().newTracker( surf );
145 
146  // Extract the bondary contour associated to the initial surfel in
147  // its first direction
148  My2DSlice slice( tracker, *(ks.sDirs( surf )) );
149  delete tracker;
150 
152  GridCurve<KSpace> gc(ks);
153  gc.initFromSCellsRange( slice.begin(), slice.end() );
155 
156  trace.endBlock();
157 
158 
159  // for 3D display with Viewer3D
160  QApplication application(argc,argv);
161  trace.beginBlock( "Display all with QGLViewer." );
162  Viewer3D viewer;
163  viewer.show();
164  // Displaying all the surfels in transparent mode
165  viewer << SetMode3D( surf.className(), "Transparent");
166  for( MyDigitalSurface::ConstIterator it = theSetOfSurfels.begin(),
167  it_end = theSetOfSurfels.end(); it != it_end; ++it )
168  viewer<< *it;
169 
170 
171  // Displaying slice
172  viewer << Viewer3D::shiftSurfelVisu;
173  viewer << SetMode3D( surf.className(), "");
174  viewer.setFillColor( Color( 50, 50, 255 ) );
175 
176  if (type == "gridcurve")
177  {
178  viewer << gc;
179  }
180  else if (type == "inner")
181  {
182  viewer << gc.getInnerPointsRange();
183  }
184  else if (type == "outer")
185  {
186  viewer << gc.getOuterPointsRange();
187  }
188  else if (type == "incident")
189  {
190  viewer << gc.getIncidentPointsRange();
191  }
192  else
193  {
194  trace.info() << "Display type not known. Use option -h" << std::endl;
195  }
196 
197  viewer << Viewer3D::updateDisplay;
198  trace.endBlock();
199 
200  return application.exec();
201 }
202 // //