DGtal  1.2.0
viewPolygonalMarchingCubes.cpp
Go to the documentation of this file.
1 
16 #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"
29 #include "DGtal/shapes/Mesh.h"
30 #include "DGtal/shapes/PolygonalSurface.h"
31 #include "DGtal/shapes/MeshHelpers.h"
32 #include "DGtal/io/viewers/Viewer3D.h"
34 
36 
37 using namespace std;
38 using namespace DGtal;
39 using namespace Z3i;
40 
42 
43 void usage( int, char** argv )
44 {
45  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <Adj>" << std::endl;
46  std::cerr << "\t - displays the boundary of the shape stored in vol file <fileName.vol>" << std::endl;
47  std::cerr << "\t as a Marching-Cube triangulated surface (more precisely a dual" << std::endl;
48  std::cerr << "\t surface to the digital boundary)." << std::endl;
49  std::cerr << "\t - voxel v belongs to the shape iff its value I(v) follows minT <= I(v) <= maxT." << std::endl;
50  std::cerr << "\t - 0: interior adjacency, 1: exterior adjacency (rules used to connect surface elements unambiguously)." << std::endl;
51 }
52 
53 int main( int argc, char** argv )
54 {
55  if ( argc < 5 )
56  {
57  usage( argc, argv );
58  return 1;
59  }
60  std::string inputFilename = argv[ 1 ];
61  unsigned int minThreshold = atoi( argv[ 2 ] );
62  unsigned int maxThreshold = atoi( argv[ 3 ] );
63  bool intAdjacency = atoi( argv[ 4 ] ) == 0;
64 
66 
68  trace.beginBlock( "Reading vol file into an image." );
69  Image image = VolReader<Image>::importVol(inputFilename);
70  DigitalSet set3d (image.domain());
72  minThreshold, maxThreshold);
73  trace.endBlock();
75 
76 
78  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
79  KSpace ks;
80  bool space_ok = ks.init( image.domain().lowerBound(),
81  image.domain().upperBound(), true );
82  if (!space_ok)
83  {
84  trace.error() << "Error in the Khamisky space construction."<<std::endl;
85  return 2;
86  }
87  trace.endBlock();
89 
91  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
92  MySurfelAdjacency surfAdj( intAdjacency ); // interior in all directions.
94 
96  trace.beginBlock( "Extracting boundary by scanning the space. " );
98  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
100  MySetOfSurfels theSetOfSurfels( ks, surfAdj );
101  Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
102  ks, set3d,
103  image.domain().lowerBound(),
104  image.domain().upperBound() );
105  MyDigitalSurface digSurf( theSetOfSurfels );
106  trace.info() << "Digital surface has " << digSurf.size() << " surfels."
107  << std::endl;
108  trace.endBlock();
110 
112  trace.beginBlock( "Making triangulated surface. " );
113  typedef CanonicEmbedder< Space > TrivialEmbedder;
116  typedef PolygonalSurface< RealPoint > PolygonMesh;
117  typedef Mesh< RealPoint > ViewMesh;
118  typedef std::map< MyDigitalSurface::Vertex, PolygonMesh::Index > VertexMap;
119  PolygonMesh polymesh;
120  ViewMesh viewmesh;
121  TrivialEmbedder trivialEmbedder;
122  CellEmbedder cellEmbedder;
123  // The +0.5 is to avoid isosurface going exactly through a voxel
124  // center, especially for binary volumes.
125  cellEmbedder.init( ks, image, trivialEmbedder,
126  ( (double) minThreshold ) + 0.5 );
127  VertexMap vmap; // stores the map Vertex -> Index
128  MeshHelpers::digitalSurface2DualPolygonalSurface
129  ( digSurf, cellEmbedder, polymesh, vmap );
130  trace.info() << "Polygonal surface is " << polymesh << std::endl;
131  MeshHelpers::polygonalSurface2Mesh( polymesh, viewmesh );
132  trace.info() << "Mesh has " << viewmesh.nbVertex()
133  << " vertices and " << viewmesh.nbFaces() << " faces." << std::endl;
134  trace.endBlock();
136 
137  QApplication application(argc,argv);
138  Viewer3D<> viewer;
139  viewer.show();
140  viewer.setLineColor(Color(150,0,0,254));
141  viewer << viewmesh;
142  viewer << Viewer3D<>::updateDisplay;
143  application.exec();
144 
145 }
146 
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
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...
virtual void setLineColor(DGtal::Color aColor)
Aim: a cellular embedder for images. (default constructible, copy constructible, assignable)....
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: This class is defined to represent a surface mesh through a set of vertices and faces....
Definition: Mesh.h:92
Aim: Represents a polygon mesh, i.e. a 2-dimensional combinatorial surface whose faces are (topologic...
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()
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
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
Z2i::RealPoint RealPoint
ImageContainerBySTLVector< Domain, Value > Image
void usage(int, char **argv)
int main(int argc, char **argv)