DGtal  0.9.3beta
volMarchingCubes.cpp
1 
14 #include <iostream>
17 #include <queue>
18 #include "DGtal/base/Common.h"
19 #include "DGtal/kernel/CanonicEmbedder.h"
20 #include "DGtal/io/readers/VolReader.h"
21 #include "DGtal/images/ImageSelector.h"
22 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
23 #include "DGtal/images/ImageLinearCellEmbedder.h"
24 #include "DGtal/shapes/Shapes.h"
25 #include "DGtal/helpers/StdDefs.h"
26 #include "DGtal/topology/helpers/Surfaces.h"
27 #include "DGtal/topology/DigitalSurface.h"
28 #include "DGtal/topology/SetOfSurfels.h"
30 
32 
33 using namespace std;
34 using namespace DGtal;
35 using namespace Z3i;
36 
38 
39 void usage( int, char** argv )
40 {
41  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <Adj>" << std::endl;
42  std::cerr << "\t - displays the boundary of the shape stored in vol file <fileName.vol>." << std::endl;
43  std::cerr << "\t - voxel v belongs to the shape iff its value I(v) follows minT <= I(v) <= maxT." << std::endl;
44  std::cerr << "\t - 0: interior adjacency, 1: exterior adjacency." << std::endl;
45 
46 }
47 
48 int main( int argc, char** argv )
49 {
50  if ( argc < 5 )
51  {
52  usage( argc, argv );
53  return 1;
54  }
55  std::string inputFilename = argv[ 1 ];
56  unsigned int minThreshold = atoi( argv[ 2 ] );
57  unsigned int maxThreshold = atoi( argv[ 3 ] );
58  bool intAdjacency = atoi( argv[ 4 ] ) == 0;
59 
61  trace.beginBlock( "Reading vol file into an image." );
63  Image image = VolReader<Image>::importVol(inputFilename);
64  DigitalSet set3d (image.domain());
65  SetFromImage<DigitalSet>::append<Image>(set3d, image,
66  minThreshold, maxThreshold);
67  trace.endBlock();
69 
70 
72  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
73  KSpace ks;
74  bool space_ok = ks.init( image.domain().lowerBound(),
75  image.domain().upperBound(), true );
76  if (!space_ok)
77  {
78  trace.error() << "Error in the Khamisky space construction."<<std::endl;
79  return 2;
80  }
81  trace.endBlock();
83 
85  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
86  MySurfelAdjacency surfAdj( intAdjacency ); // interior in all directions.
88 
90  trace.beginBlock( "Extracting boundary by scanning the space. " );
91  typedef KSpace::SurfelSet SurfelSet;
92  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
94  MySetOfSurfels theSetOfSurfels( ks, surfAdj );
95  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
96  ks, set3d,
97  image.domain().lowerBound(),
98  image.domain().upperBound() );
99  MyDigitalSurface digSurf( theSetOfSurfels );
100  trace.info() << "Digital surface has " << digSurf.size() << " surfels."
101  << std::endl;
102  trace.endBlock();
104 
106  trace.beginBlock( "Making OFF surface <marching-cube.off>. " );
107  typedef CanonicEmbedder< Space > MyEmbedder;
108  typedef
110  CellEmbedder cellEmbedder;
111  MyEmbedder trivialEmbedder;
112  // The +0.5 is to avoid isosurface going exactly through a voxel
113  // center, especially for binary volumes.
114  cellEmbedder.init( ks, image, trivialEmbedder,
115  ( (double) minThreshold ) + 0.5 );
116  ofstream out( "marching-cube.off" );
117  if ( out.good() )
118  digSurf.exportEmbeddedSurfaceAs3DOFF( out, cellEmbedder );
119  out.close();
120  trace.endBlock();
122  return 0;
123 }
124 
void beginBlock(const std::string &keyword="")
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition: StdDefs.h:100
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:137
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
STL namespace.
double endBlock()
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
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
Aim: A trivial embedder for digital points, which corresponds to the canonic injection of Zn into Rn...
std::ostream & info()
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as connected surfels...
Definition: SetOfSurfels.h:73
const Domain & domain() const
Definition: Image.h:192
void init(ConstAlias< KSpace > K, ConstAlias< Image > f, ConstAlias< Embedder > e, double iso_value)
std::ostream & error()
Aim: a cellular embedder for images. (default constructible, copy constructible, assignable). Model of CCellEmbedder.