DGtal  1.5.beta
shapes/viewMarchingCubes.cpp

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

See also
Helpers to convert triangulated 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.] (509.444 ms)
New Block [Construct the Khalimsky space from the image domain.]
EndBlock [Construct the Khalimsky space from the image domain.] (0.015 ms)
New Block [Extracting boundary by scanning the space. ]
  Digital surface has 160660 surfels.
EndBlock [Extracting boundary by scanning the space. ] (2471.36 ms)
New Block [Making triangulated surface. ]
  Triangulated surface is [TriangulatedSurface #V=273182 #E=819954 #F=546636 Chi=-136]
  Mesh has 273182 vertices and 546636 faces.
EndBlock [Making triangulated surface. ] (4228.9 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/TriangulatedSurface.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>=255] [<Adj>=0]" << 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 - minT is the iso-surface level." << std::endl;
std::cerr << "\t - maxT should be equal to the maximum possible value in the image." << 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 = argc > 3 ? atoi( argv[ 3 ] ) : 255;
bool intAdjacency = argc > 4 ? (atoi( argv[ 4 ] ) == 0) : true;
typedef ImageSelector < Domain, int>::Type Image;
trace.beginBlock( "Reading vol file into an image." );
Image image = VolReader<Image>::importVol(inputFilename);
DigitalSet set3d (image.domain());
SetFromImage<DigitalSet>::append<Image>(set3d, image,
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;
typedef DigitalSurface< MySetOfSurfels > MyDigitalSurface;
MySetOfSurfels theSetOfSurfels( ks, surfAdj );
Surfaces<KSpace>::sMakeBoundary( theSetOfSurfels.surfelSet(),
ks, set3d,
image.domain().lowerBound(),
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 ImageLinearCellEmbedder< KSpace, Image, TrivialEmbedder > CellEmbedder;
typedef TriangulatedSurface< RealPoint > TriMesh;
typedef Mesh< RealPoint > ViewMesh;
typedef std::map< MyDigitalSurface::Vertex, TriMesh::Index > VertexMap;
TriMesh trimesh;
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
MeshHelpers::digitalSurface2DualTriangulatedSurface
( digSurf, cellEmbedder, trimesh, vmap );
trace.info() << "Triangulated surface is " << trimesh << std::endl;
MeshHelpers::triangulatedSurface2Mesh( trimesh, viewmesh );
trace.info() << "Mesh has " << viewmesh.nbVertex()
<< " vertices and " << viewmesh.nbFaces() << " faces." << std::endl;
QApplication application(argc,argv);
Viewer3D<> viewer;
viewer.show();
viewer.setLineColor(Color(150,0,0,254));
viewer << viewmesh;
viewer << Viewer3D<>::updateDisplay;
application.exec();
}
void usage(int, char **argv)
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:153
int main(int argc, char **argv)
ImageContainerBySTLVector< Domain, Value > Image
TriangulatedSurface< RealPoint > TriMesh
PointVector< 3, double > RealPoint
Z2i::DigitalSet DigitalSet