DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
volMarchingCubes.cpp
1 
14 
15 
16 #include <iostream>
17 #include <queue>
18 #include "DGtal/base/Common.h"
19 #include "DGtal/kernel/sets/SetPredicate.h"
20 #include "DGtal/kernel/CanonicEmbedder.h"
21 #include "DGtal/io/readers/VolReader.h"
22 #include "DGtal/images/ImageSelector.h"
23 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
24 #include "DGtal/images/ImageLinearCellEmbedder.h"
25 #include "DGtal/shapes/Shapes.h"
26 #include "DGtal/helpers/StdDefs.h"
27 #include "DGtal/topology/helpers/Surfaces.h"
28 #include "DGtal/topology/DigitalSurface.h"
29 #include "DGtal/topology/SetOfSurfels.h"
31 
33 
34 using namespace std;
35 using namespace DGtal;
36 using namespace Z3i;
37 
39 
40 void usage( int, char** argv )
41 {
42  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <Adj>" << std::endl;
43  std::cerr << "\t - displays the boundary of the shape stored in vol file <fileName.vol>." << std::endl;
44  std::cerr << "\t - voxel v belongs to the shape iff its value I(v) follows minT <= I(v) <= maxT." << std::endl;
45  std::cerr << "\t - 0: interior adjacency, 1: exterior adjacency." << std::endl;
46 
47 }
48 
49 int main( int argc, char** argv )
50 {
51  if ( argc < 5 )
52  {
53  usage( argc, argv );
54  return 1;
55  }
56  std::string inputFilename = argv[ 1 ];
57  unsigned int minThreshold = atoi( argv[ 2 ] );
58  unsigned int maxThreshold = atoi( argv[ 3 ] );
59  bool intAdjacency = atoi( argv[ 4 ] ) == 0;
60 
62  trace.beginBlock( "Reading vol file into an image." );
64  Image image = VolReader<Image>::importVol(inputFilename);
65  DigitalSet set3d (image.domain());
66  SetPredicate<DigitalSet> set3dPredicate( set3d );
67  SetFromImage<DigitalSet>::append<Image>(set3d, image,
68  minThreshold, maxThreshold);
69  trace.endBlock();
71 
72 
74  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
75  KSpace ks;
76  bool space_ok = ks.init( image.domain().lowerBound(),
77  image.domain().upperBound(), true );
78  if (!space_ok)
79  {
80  trace.error() << "Error in the Khamisky space construction."<<std::endl;
81  return 2;
82  }
83  trace.endBlock();
85 
87  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
88  MySurfelAdjacency surfAdj( intAdjacency ); // interior in all directions.
90 
92  trace.beginBlock( "Extracting boundary by scanning the space. " );
93  typedef KSpace::SurfelSet SurfelSet;
94  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
96  MySetOfSurfels theSetOfSurfels( ks, surfAdj );
97  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
98  ks, set3dPredicate,
99  image.domain().lowerBound(),
100  image.domain().upperBound() );
101  MyDigitalSurface digSurf( theSetOfSurfels );
102  trace.info() << "Digital surface has " << digSurf.size() << " surfels."
103  << std::endl;
104  trace.endBlock();
106 
108  trace.beginBlock( "Making OFF surface <marching-cube.off>. " );
109  typedef CanonicEmbedder< Space > MyEmbedder;
110  typedef
112  CellEmbedder cellEmbedder;
113  MyEmbedder trivialEmbedder;
114  cellEmbedder.init( ks, image, trivialEmbedder, minThreshold );
115  ofstream out( "marching-cube.off" );
116  if ( out.good() )
117  digSurf.exportEmbeddedSurfaceAs3DOFF( out, cellEmbedder );
118  out.close();
119  trace.endBlock();
121  return 0;
122 }
123