DGtal  1.2.0
volMarchingCubes.cpp
Go to the documentation of this file.
1 
34 #include <iostream>
35 #include <queue>
36 #include "DGtal/base/Common.h"
37 #include "DGtal/kernel/CanonicEmbedder.h"
38 #include "DGtal/io/readers/VolReader.h"
39 #include "DGtal/images/ImageSelector.h"
40 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
41 #include "DGtal/images/ImageLinearCellEmbedder.h"
42 #include "DGtal/shapes/Shapes.h"
43 #include "DGtal/helpers/StdDefs.h"
44 #include "DGtal/topology/helpers/Surfaces.h"
45 #include "DGtal/topology/DigitalSurface.h"
46 #include "DGtal/topology/SetOfSurfels.h"
48 
50 
51 using namespace std;
52 using namespace DGtal;
53 using namespace Z3i;
54 
56 
57 void usage( int, char** argv )
58 {
59  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <Adj>" << std::endl;
60  std::cerr << "\t - displays the boundary of the shape stored in vol file <fileName.vol>." << std::endl;
61  std::cerr << "\t - voxel v belongs to the shape iff its value I(v) follows minT <= I(v) <= maxT." << std::endl;
62  std::cerr << "\t - 0: interior adjacency, 1: exterior adjacency." << std::endl;
63 
64 }
65 
66 int main( int argc, char** argv )
67 {
68  if ( argc < 5 )
69  {
70  usage( argc, argv );
71  return 1;
72  }
73  std::string inputFilename = argv[ 1 ];
74  unsigned int minThreshold = atoi( argv[ 2 ] );
75  unsigned int maxThreshold = atoi( argv[ 3 ] );
76  bool intAdjacency = atoi( argv[ 4 ] ) == 0;
77 
79  trace.beginBlock( "Reading vol file into an image." );
81  Image image = VolReader<Image>::importVol(inputFilename);
82  DigitalSet set3d (image.domain());
84  minThreshold, maxThreshold);
85  trace.endBlock();
87 
88 
90  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
91  KSpace ks;
92  bool space_ok = ks.init( image.domain().lowerBound(),
93  image.domain().upperBound(), true );
94  if (!space_ok)
95  {
96  trace.error() << "Error in the Khamisky space construction."<<std::endl;
97  return 2;
98  }
99  trace.endBlock();
101 
103  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
104  MySurfelAdjacency surfAdj( intAdjacency ); // interior in all directions.
106 
108  trace.beginBlock( "Extracting boundary by scanning the space. " );
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( "Making OFF surface <marching-cube.off>. " );
125  typedef CanonicEmbedder< Space > MyEmbedder;
126  typedef
128  CellEmbedder cellEmbedder;
129  MyEmbedder trivialEmbedder;
130  // The +0.5 is to avoid isosurface going exactly through a voxel
131  // center, especially for binary volumes.
132  cellEmbedder.init( ks, image, trivialEmbedder,
133  ( (double) minThreshold ) + 0.5 );
134  ofstream out( "marching-cube.off" );
135  if ( out.good() )
136  digSurf.exportEmbeddedSurfaceAs3DOFF( out, cellEmbedder );
137  out.close();
138  trace.endBlock();
140  return 0;
141 }
142 
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
void exportEmbeddedSurfaceAs3DOFF(std::ostream &out, const CellEmbedder &cembedder) const
Aim: a cellular embedder for images. (default constructible, copy constructible, assignable)....
void init(ConstAlias< KSpace > K, ConstAlias< Image > f, ConstAlias< Embedder > e, double iso_value)
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
std::set< SCell > SurfelSet
Preferred type for defining a set of surfels (always signed cells).
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as connected surfels....
Definition: SetOfSurfels.h:74
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:79
std::ostream & error()
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
MyDigitalSurface::SurfelSet SurfelSet
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
Aim: A trivial embedder for digital points, which corresponds to the canonic injection of Zn into Rn.
Aim: Define utilities to convert a digital set into an image.
Definition: SetFromImage.h:64
Aim: implements methods to read a "Vol" file format.
Definition: VolReader.h:90
ImageContainerBySTLVector< Domain, Value > Image
void usage(int, char **argv)
int main(int argc, char **argv)