DGtal  1.2.0
graph/volDistanceTraversal.cpp

Displays the Euclidean distance to a starting surfel on the boundary of a vol shape (traversal by mix distance/breadth-first, see DGtal::DistanceBreadthFirstVisitor).

Graph traversal with visitors
# Commands
\$ ./examples/graph/volDistanceTraversal ../examples/samples/cat10.vol 0 255 100

Coloring vertices of a vol boundary according to the Euclidean distance to a starting surfel (distance breadth-first traversal).
#include <iostream>
#include <queue>
#include "DGtal/base/BasicFunctors.h"
#include "DGtal/topology/CanonicSCellEmbedder.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/topology/DigitalSurface.h"
#include "DGtal/topology/LightImplicitDigitalSurface.h"
#include "DGtal/geometry/volumes/distance/LpMetric.h"
#include "DGtal/io/viewers/Viewer3D.h"
#include "DGtal/io/Color.h"
#include "DGtal/images/ImageSelector.h"
#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
using namespace std;
using namespace DGtal;
using namespace Z3i;
void usage( int, char** argv )
{
std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <idxP>" << std::endl;
std::cerr << "\t - displays the Euclidean distance to the specified surfel on the boundary of the shape stored in vol file <fileName.vol>." << std::endl;
std::cerr << "\t - the shape is defined implicitly: voxel v belongs to the shape iff its value I(v) follows minT < I(v) <= maxT." << std::endl;
std::cerr << "\t - starts from the <idxP>-th surfel or first one if invalid." << std::endl;
}
int main( int argc, char** argv )
{
if ( argc < 4 )
{
usage( argc, argv );
return 1;
}
std::string inputFilename = argv[ 1 ];
unsigned int minThreshold = atoi( argv[ 2 ] );
unsigned int maxThreshold = atoi( argv[ 3 ] );
unsigned int idxP = (argc <= 4) ? 0 : atoi( argv[ 4 ] );
trace.beginBlock( "Reading vol file into an image." );
typedef ImageSelector < Domain, int>::Type Image;
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;
}
trace.beginBlock( "Set up digital surface." );
typedef LightImplicitDigitalSurface<KSpace, DigitalSet >
typedef DigitalSurface<MyDigitalSurfaceContainer> MyDigitalSurface;
SCell bel = Surfaces<KSpace>::findABel( ks, set3d, 100000 );
MyDigitalSurfaceContainer* ptrSurfContainer =
new MyDigitalSurfaceContainer( ks, set3d, surfAdj, bel );
MyDigitalSurface digSurf( ptrSurfContainer ); // acquired
// Find first bel.
MyDigitalSurface::ConstIterator it = digSurf.begin();
for ( idxP = idxP % digSurf.size(); idxP != 0; --idxP ) ++it;
bel = *it;
trace.beginBlock( "Extracting boundary by distance tracking from an initial bel." );
typedef CanonicSCellEmbedder<KSpace> SCellEmbedder;
typedef RealPoint::Coordinate Scalar;
typedef LpMetric<Space> Distance;
using DistanceToPoint = std::function<double(const Space::RealPoint &)>;
typedef DistanceBreadthFirstVisitor< MyDigitalSurface, VertexFunctor, std::set<SCell> >
MyDistanceVisitor;
typedef MyDistanceVisitor::Node MyNode;
typedef MyDistanceVisitor::Scalar MySize;
SCellEmbedder embedder( ks );
Distance distance(2.0);
DistanceToPoint distanceToPoint = std::bind( distance, embedder( bel ), std::placeholders::_1 );
VertexFunctor vfunctor( embedder, distanceToPoint );
MyDistanceVisitor visitor( digSurf, vfunctor, bel );
unsigned long nbSurfels = 0;
MyNode node;
while ( ! visitor.finished() )
{
node = visitor.current();
++nbSurfels;
visitor.expand();
}
MySize maxDist = node.second;
trace.beginBlock( "Displaying surface in Viewer3D." );
QApplication application(argc,argv);
Viewer3D<> viewer( ks );
viewer.show();
MyDistanceVisitor visitor2( digSurf, vfunctor, bel );
viewer << SetMode3D( bel.className(), "Basic" );
viewer << CustomColors3D( Color::Black, Color::White )
<< bel;
visitor2.expand();
std::vector< MyDistanceVisitor::Node > layer;
while ( ! visitor2.finished() )
{
MyNode n = visitor2.current();
Color c = hueShade( n.second );
viewer << CustomColors3D( Color::Red, c )
<< n.first;
visitor2.expand();
}
viewer << Viewer3D<>::updateDisplay;
trace.info() << "nb surfels = " << nbSurfels << std::endl;
return application.exec();
}
void usage(int, char **argv)
std::ostream & error()
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Aim: Define a new Functor from the composition of two other functors.
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
MyDigitalSurface::ConstIterator ConstIterator
DigitalSetBoundary< KSpace, DigitalSet > MyDigitalSurfaceContainer
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition: StdDefs.h:100
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
int main(int argc, char **argv)
Z2i::RealPoint RealPoint
ImageContainerBySTLVector< Domain, Value > Image