DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
volToOFF.cpp
1 
30 
31 
32 #include <iostream>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/io/readers/VolReader.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/topology/helpers/Surfaces.h"
37 #include "DGtal/topology/DigitalSurface.h"
38 #include "DGtal/topology/SetOfSurfels.h"
39 #include "DGtal/images/ImageSelector.h"
40 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
42 
44 
45 using namespace std;
46 using namespace DGtal;
47 using namespace Z3i;
48 
50 
51 
52 void usage( int, char** argv )
53 {
54  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <int=0|ext=1>" << std::endl;
55  std::cerr << "\t - displays the boundary of the shape stored in vol file <fileName.vol> as an OFF geomview surface file. It is a kind of marching-cube surface, defined by duality with respect to the digital surface." << std::endl;
56  std::cerr << "\t - voxel v belongs to the shape iff its value I(v) follows minT <= I(v) <= maxT." << std::endl;
57  std::cerr << "\t - 0: interior adjacency, 1: exterior adjacency." << std::endl;
58 }
59 
60 int main( int argc, char** argv )
61 {
62  if ( argc < 5 )
63  {
64  usage( argc, argv );
65  return 1;
66  }
67  std::string inputFilename = argv[ 1 ];
68  unsigned int minThreshold = atoi( argv[ 2 ] );
69  unsigned int maxThreshold = atoi( argv[ 3 ] );
70  bool intAdjacency = atoi( argv[ 4 ] ) == 0;
71 
73  trace.beginBlock( "Reading vol file into an image." );
75  Image image = VolReader<Image>::importVol(inputFilename);
76  DigitalSet set3d (image.domain());
77  SetPredicate<DigitalSet> set3dPredicate( set3d );
78  SetFromImage<DigitalSet>::append<Image>(set3d, image,
79  minThreshold, maxThreshold);
80  trace.endBlock();
82 
84  // Construct the Khalimsky space from the image domain
85  KSpace K;
86  bool space_ok = K.init( image.domain().lowerBound(),
87  image.domain().upperBound(), true );
88  if (!space_ok)
89  {
90  trace.error() << "Error in the Khamisky space construction."<<std::endl;
91  return 2;
92  }
94 
96  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
97  MySurfelAdjacency surfAdj( intAdjacency ); // interior in all directions.
99 
101  trace.beginBlock( "Extracting boundary by scanning the space. " );
102  typedef KSpace::Surfel Surfel;
103  typedef KSpace::SurfelSet SurfelSet;
104  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
106 
107  MySetOfSurfels theSetOfSurfels( K, surfAdj );
108  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
109  K, set3dPredicate,
110  image.domain().lowerBound(),
111  image.domain().upperBound() );
112  MyDigitalSurface digSurf( theSetOfSurfels );
113  trace.info() << "Digital surface has " << digSurf.size() << " surfels."
114  << std::endl;
115  trace.endBlock();
117 
119  trace.beginBlock( "Making OFF surface <marching-cube.off>. " );
120  ofstream out( "marching-cube.off" );
121  if ( out.good() )
122  digSurf.exportSurfaceAs3DOFF( out );
123  out.close();
124  trace.endBlock();
126 
127  return 0;
128 }