DGtal  1.4.beta

Marching-cube like surface extracted using the combinatorial manifold structure of digital surfaces and the intermediate representation PolygonalSurface.

See also
Helpers to convert polygonal surfaces from/to mesh
# Commands
$ ./examples/shapes/viewMarchingCubes  ../examples/samples/lobster.vol 40 255 0 
New Block [Reading vol file into an image.]
EndBlock [Reading vol file into an image.] (510.243 ms)
New Block [Construct the Khalimsky space from the image domain.]
EndBlock [Construct the Khalimsky space from the image domain.] (0.016 ms)
New Block [Extracting boundary by scanning the space. ]
  Digital surface has 160660 surfels.
EndBlock [Extracting boundary by scanning the space. ] (2757.2 ms)
New Block [Making triangulated surface. ]
  Polygonal surface is [PolygonalSurface #V=160660 #E=321320 #F=160524 Chi=-136]
  Mesh has 160660 vertices and 160524 faces.
EndBlock [Making triangulated surface. ] (2380.92 ms)
Marching-cube surface of lobster.vol file.
#include <iostream>
#include <queue>
#include "DGtal/base/Common.h"
#include "DGtal/kernel/CanonicEmbedder.h"
#include "DGtal/io/readers/VolReader.h"
#include "DGtal/images/ImageSelector.h"
#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
#include "DGtal/images/ImageLinearCellEmbedder.h"
#include "DGtal/shapes/Shapes.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/topology/helpers/Surfaces.h"
#include "DGtal/topology/DigitalSurface.h"
#include "DGtal/topology/SetOfSurfels.h"
#include "DGtal/shapes/Mesh.h"
#include "DGtal/shapes/PolygonalSurface.h"
#include "DGtal/shapes/MeshHelpers.h"
#include "DGtal/io/viewers/Viewer3D.h"
using namespace std;
using namespace DGtal;
using namespace Z3i;
void usage( int, char** argv )
std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <Adj>" << std::endl;
std::cerr << "\t - displays the boundary of the shape stored in vol file <fileName.vol>" << std::endl;
std::cerr << "\t as a Marching-Cube triangulated surface (more precisely a dual" << std::endl;
std::cerr << "\t surface to the digital boundary)." << std::endl;
std::cerr << "\t - voxel v belongs to the shape iff its value I(v) follows minT <= I(v) <= maxT." << std::endl;
std::cerr << "\t - 0: interior adjacency, 1: exterior adjacency (rules used to connect surface elements unambiguously)." << std::endl;
int main( int argc, char** argv )
if ( argc < 5 )
usage( argc, argv );
return 1;
std::string inputFilename = argv[ 1 ];
unsigned int minThreshold = atoi( argv[ 2 ] );
unsigned int maxThreshold = atoi( argv[ 3 ] );
bool intAdjacency = atoi( argv[ 4 ] ) == 0;
trace.beginBlock( "Reading vol file into an image." );
Image image = VolReader<Image>::importVol(inputFilename);
DigitalSet set3d (image.domain());
minThreshold, maxThreshold);
trace.beginBlock( "Construct the Khalimsky space from the image domain." );
KSpace ks;
bool space_ok = ks.init( image.domain().lowerBound(),
image.domain().upperBound(), true );
if (!space_ok)
trace.error() << "Error in the Khamisky space construction."<<std::endl;
return 2;
typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
MySurfelAdjacency surfAdj( intAdjacency ); // interior in all directions.
trace.beginBlock( "Extracting boundary by scanning the space. " );
typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
MySetOfSurfels theSetOfSurfels( ks, surfAdj );
Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
ks, set3d,
image.domain().upperBound() );
MyDigitalSurface digSurf( theSetOfSurfels );
trace.info() << "Digital surface has " << digSurf.size() << " surfels."
<< std::endl;
trace.beginBlock( "Making triangulated surface. " );
typedef CanonicEmbedder< Space > TrivialEmbedder;
typedef PolygonalSurface< RealPoint > PolygonMesh;
typedef Mesh< RealPoint > ViewMesh;
typedef std::map< MyDigitalSurface::Vertex, PolygonMesh::Index > VertexMap;
PolygonMesh polymesh;
ViewMesh viewmesh;
TrivialEmbedder trivialEmbedder;
CellEmbedder cellEmbedder;
// The +0.5 is to avoid isosurface going exactly through a voxel
// center, especially for binary volumes.
cellEmbedder.init( ks, image, trivialEmbedder,
( (double) minThreshold ) + 0.5 );
VertexMap vmap; // stores the map Vertex -> Index
( digSurf, cellEmbedder, polymesh, vmap );
trace.info() << "Polygonal surface is " << polymesh << std::endl;
MeshHelpers::polygonalSurface2Mesh( polymesh, viewmesh );
trace.info() << "Mesh has " << viewmesh.nbVertex()
<< " vertices and " << viewmesh.nbFaces() << " faces." << std::endl;
QApplication application(argc,argv);
Viewer3D<> viewer;
viewer << viewmesh;
viewer << Viewer3D<>::updateDisplay;
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
Aim: Define utilities to convert a digital set into an image.
Definition: SetFromImage.h:63
double endBlock()
std::set< SCell > SurfelSet
Preferred type for defining a set of surfels (always signed cells).
Definition: KhalimskySpaceND.h:450
Definition: ImageContainerBySTLVector.h:126
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
Definition: DigitalSurface.h:139
DGtal::SurfelAdjacency< KSpace::dimension >
std::ostream & error()
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Trace trace
Definition: Common.h:154
void beginBlock(const std::string &keyword="")
std::ostream & info()
Definition: Viewer3D.h:131
Aim: This class is defined to represent a surface mesh through a set of vertices and faces....
Definition: Mesh.h:91
ImageContainerBySTLVector< Domain, Value > Image
Definition: testSimpleRandomAccessRangeFromPoint.cpp:45
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: A trivial embedder for digital points, which corresponds to the canonic injection of Zn into Rn.
Definition: CanonicEmbedder.h:64
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
DGtal::Display3D< SpaceND< 3 >, KhalimskySpaceND< 3 > >::setLineColor
virtual void setLineColor(DGtal::Color aColor)
void usage(int, char **argv)
Definition: approximation.cpp:64
Aim: implements methods to read a "Vol" file format.
Definition: VolReader.h:89
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as connected surfels....
Definition: SetOfSurfels.h:73
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
Aim: a cellular embedder for images. (default constructible, copy constructible, assignable)....
Definition: ImageLinearCellEmbedder.h:69
Aim: Represents a polygon mesh, i.e. a 2-dimensional combinatorial surface whose faces are (topologic...
Definition: PolygonalSurface.h:86
Z2i::RealPoint RealPoint
Definition: testAstroid2D.cpp:46
double Value
Definition: testSimpleRandomAccessRangeFromPoint.cpp:38
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
Definition: greedy-plane-segmentation-ex2.cpp:92
MyDigitalSurface::SurfelSet SurfelSet
Definition: greedy-plane-segmentation-ex2.cpp:95
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Definition: DigitalSetByAssociativeContainer.h:89
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:64