DGtal 1.4.0
Loading...
Searching...
No Matches
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
37using namespace std;
38using namespace DGtal;
39using namespace Z3i;
40
42
43void 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
53int 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);
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 }
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;
115 typedef CellEmbedder::Value RealPoint;
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
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:68
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.
static void polygonalSurface2Mesh(const PolygonalSurface< Point > &polysurf, Mesh< Point > &mesh)
static void digitalSurface2DualPolygonalSurface(const DigitalSurface< DigitalSurfaceContainer > &dsurf, const CellEmbedder &cembedder, PolygonalSurface< typename CellEmbedder::Value > &polysurf, VertexMap &vertexmap)
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....
static void sMakeBoundary(SCellSet &aBoundary, const KSpace &aKSpace, const PointPredicate &pp, const Point &aLowerBound, const Point &aUpperBound)
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
void beginBlock(const std::string &keyword="")
std::ostream & error()
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
Space::RealPoint RealPoint
Definition StdDefs.h:170
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition Common.h:153
STL namespace.
Aim: A trivial embedder for digital points, which corresponds to the canonic injection of Zn into Rn.
ImageContainerBySTLVector< Domain, Value > Type
static void append(Set &aSet, const ForegroundPredicate &isForeground, typename Image::Domain::ConstIterator itBegin, typename Image::Domain::ConstIterator itEnd)
static ImageContainer importVol(const std::string &filename, const Functor &aFunctor=Functor())
int main()
Definition testBits.cpp:56
ImageContainerBySTLVector< Domain, Value > Image