DGtal  0.9.2
volDistanceTraversal.cpp
1 
14 #include <iostream>
17 #include <queue>
18 #include <QImageReader>
19 
20 #include "DGtal/base/BasicFunctors.h"
21 #include "DGtal/topology/CanonicSCellEmbedder.h"
22 #include "DGtal/helpers/StdDefs.h"
23 #include "DGtal/graph/DistanceBreadthFirstVisitor.h"
24 #include "DGtal/topology/DigitalSurface.h"
25 #include "DGtal/topology/LightImplicitDigitalSurface.h"
26 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
27 #include "DGtal/io/readers/VolReader.h"
28 #include "DGtal/io/viewers/Viewer3D.h"
29 
30 #include "DGtal/io/Color.h"
31 #include "DGtal/io/colormaps/HueShadeColorMap.h"
32 #include "DGtal/images/ImageSelector.h"
33 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
35 
37 
38 using namespace std;
39 using namespace DGtal;
40 using namespace Z3i;
41 
43 
44 void usage( int, char** argv )
45 {
46  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <idxP>" << std::endl;
47  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;
48  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;
49  std::cerr << "\t - starts from the <idxP>-th surfel or first one if invalid." << std::endl;
50 }
51 
52 int main( int argc, char** argv )
53 {
54  if ( argc < 4 )
55  {
56  usage( argc, argv );
57  return 1;
58  }
59  std::string inputFilename = argv[ 1 ];
60  unsigned int minThreshold = atoi( argv[ 2 ] );
61  unsigned int maxThreshold = atoi( argv[ 3 ] );
62  unsigned int idxP = (argc <= 4) ? 0 : atoi( argv[ 4 ] );
63 
65  trace.beginBlock( "Reading vol file into an image." );
67  Image image = VolReader<Image>::importVol(inputFilename);
68  DigitalSet set3d (image.domain());
69  SetFromImage<DigitalSet>::append<Image>(set3d, image,
70  minThreshold, maxThreshold);
71  trace.endBlock();
73 
74 
76  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
77  KSpace ks;
78  bool space_ok = ks.init( image.domain().lowerBound(),
79  image.domain().upperBound(), true );
80  if (!space_ok)
81  {
82  trace.error() << "Error in the Khamisky space construction."<<std::endl;
83  return 2;
84  }
85  trace.endBlock();
87 
89  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
90  MySurfelAdjacency surfAdj( true ); // interior in all directions.
92 
94  trace.beginBlock( "Set up digital surface." );
98  SCell bel = Surfaces<KSpace>::findABel( ks, set3d, 100000 );
99  MyDigitalSurfaceContainer* ptrSurfContainer =
100  new MyDigitalSurfaceContainer( ks, set3d, surfAdj, bel );
101  MyDigitalSurface digSurf( ptrSurfContainer ); // acquired
102  trace.endBlock();
103  // Find first bel.
104  MyDigitalSurface::ConstIterator it = digSurf.begin();
105  for ( idxP = idxP % digSurf.size(); idxP != 0; --idxP ) ++it;
106  bel = *it;
108 
110  trace.beginBlock( "Extracting boundary by distance tracking from an initial bel." );
111  typedef CanonicSCellEmbedder<KSpace> SCellEmbedder;
112  typedef SCellEmbedder::Value RealPoint;
113  typedef RealPoint::Coordinate Scalar;
115 
116  typedef std::binder1st< Distance > DistanceToPoint;
119  MyDistanceVisitor;
120  typedef MyDistanceVisitor::Node MyNode;
121  typedef MyDistanceVisitor::Scalar MySize;
122 
123  SCellEmbedder embedder( ks );
124  Distance distance;
125  DistanceToPoint distanceToPoint = std::bind1st( distance, embedder( bel ) );
126  VertexFunctor vfunctor( embedder, distanceToPoint );
127  MyDistanceVisitor visitor( digSurf, vfunctor, bel );
128 
129  unsigned long nbSurfels = 0;
130  MyNode node;
131  while ( ! visitor.finished() )
132  {
133  node = visitor.current();
134  ++nbSurfels;
135  visitor.expand();
136  }
137  MySize maxDist = node.second;
138  trace.endBlock();
140 
142  trace.beginBlock( "Displaying surface in Viewer3D." );
143  QApplication application(argc,argv);
144  Viewer3D<> viewer( ks );
145  viewer.show();
146  HueShadeColorMap<MySize,1> hueShade( 0, maxDist );
147  MyDistanceVisitor visitor2( digSurf, vfunctor, bel );
148  viewer << SetMode3D( bel.className(), "Basic" );
149  viewer << CustomColors3D( Color::Black, Color::White )
150  << bel;
151  visitor2.expand();
152  std::vector< MyDistanceVisitor::Node > layer;
153  while ( ! visitor2.finished() )
154  {
155  MyNode n = visitor2.current();
156  Color c = hueShade( n.second );
157  viewer << CustomColors3D( Color::Red, c )
158  << n.first;
159  visitor2.expand();
160  }
161  viewer << Viewer3D<>::updateDisplay;
162  trace.info() << "nb surfels = " << nbSurfels << std::endl;
163  trace.endBlock();
164  return application.exec();
166 }
167 
Aim: This class is useful to perform an exploration of a graph given a starting point or set (called ...
void beginBlock(const std::string &keyword="")
std::string className() const
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:130
Component Coordinate
Type for Point elements.
Definition: PointVector.h:158
DigitalSurfaceContainer::SurfelConstIterator ConstIterator
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
STL namespace.
double endBlock()
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of a given...
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
Aim: implements separable l_p metrics with exact predicates.
Aim: Define a new Functor from the composition of two other functors.
bool init(const Point &lower, const Point &upper, bool isClosed)
Aim: implements methods to read a "Vol" file format.
Definition: VolReader.h:88
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Aim: Define utilities to convert a digital set into an image.
Definition: SetFromImage.h:63
std::ostream & info()
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value...
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
const Domain & domain() const
Definition: Image.h:192
std::ostream & error()
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
Modifier class in a Display3D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithDisplay3D.