DGtal  0.9.3
volDistanceTraversal.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
34 #include <queue>
35 #include <QImageReader>
36 
37 #include "DGtal/base/BasicFunctors.h"
38 #include "DGtal/topology/CanonicSCellEmbedder.h"
39 #include "DGtal/helpers/StdDefs.h"
40 #include "DGtal/graph/DistanceBreadthFirstVisitor.h"
41 #include "DGtal/topology/DigitalSurface.h"
42 #include "DGtal/topology/LightImplicitDigitalSurface.h"
43 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
44 #include "DGtal/io/readers/VolReader.h"
45 #include "DGtal/io/viewers/Viewer3D.h"
46 
47 #include "DGtal/io/Color.h"
48 #include "DGtal/io/colormaps/HueShadeColorMap.h"
49 #include "DGtal/images/ImageSelector.h"
50 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
52 
54 
55 using namespace std;
56 using namespace DGtal;
57 using namespace Z3i;
58 
60 
61 void usage( int, char** argv )
62 {
63  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT> <idxP>" << std::endl;
64  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;
65  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;
66  std::cerr << "\t - starts from the <idxP>-th surfel or first one if invalid." << std::endl;
67 }
68 
69 int main( int argc, char** argv )
70 {
71  if ( argc < 4 )
72  {
73  usage( argc, argv );
74  return 1;
75  }
76  std::string inputFilename = argv[ 1 ];
77  unsigned int minThreshold = atoi( argv[ 2 ] );
78  unsigned int maxThreshold = atoi( argv[ 3 ] );
79  unsigned int idxP = (argc <= 4) ? 0 : atoi( argv[ 4 ] );
80 
82  trace.beginBlock( "Reading vol file into an image." );
84  Image image = VolReader<Image>::importVol(inputFilename);
85  DigitalSet set3d (image.domain());
87  minThreshold, maxThreshold);
88  trace.endBlock();
90 
91 
93  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
94  KSpace ks;
95  bool space_ok = ks.init( image.domain().lowerBound(),
96  image.domain().upperBound(), true );
97  if (!space_ok)
98  {
99  trace.error() << "Error in the Khamisky space construction."<<std::endl;
100  return 2;
101  }
102  trace.endBlock();
104 
106  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
107  MySurfelAdjacency surfAdj( true ); // interior in all directions.
109 
111  trace.beginBlock( "Set up digital surface." );
115  SCell bel = Surfaces<KSpace>::findABel( ks, set3d, 100000 );
116  MyDigitalSurfaceContainer* ptrSurfContainer =
117  new MyDigitalSurfaceContainer( ks, set3d, surfAdj, bel );
118  MyDigitalSurface digSurf( ptrSurfContainer ); // acquired
119  trace.endBlock();
120  // Find first bel.
121  MyDigitalSurface::ConstIterator it = digSurf.begin();
122  for ( idxP = idxP % digSurf.size(); idxP != 0; --idxP ) ++it;
123  bel = *it;
125 
127  trace.beginBlock( "Extracting boundary by distance tracking from an initial bel." );
128  typedef CanonicSCellEmbedder<KSpace> SCellEmbedder;
130  typedef RealPoint::Coordinate Scalar;
132 
133  typedef std::binder1st< Distance > DistanceToPoint;
136  MyDistanceVisitor;
137  typedef MyDistanceVisitor::Node MyNode;
138  typedef MyDistanceVisitor::Scalar MySize;
139 
140  SCellEmbedder embedder( ks );
141  Distance distance;
142  DistanceToPoint distanceToPoint = std::bind1st( distance, embedder( bel ) );
143  VertexFunctor vfunctor( embedder, distanceToPoint );
144  MyDistanceVisitor visitor( digSurf, vfunctor, bel );
145 
146  unsigned long nbSurfels = 0;
147  MyNode node;
148  while ( ! visitor.finished() )
149  {
150  node = visitor.current();
151  ++nbSurfels;
152  visitor.expand();
153  }
154  MySize maxDist = node.second;
155  trace.endBlock();
157 
159  trace.beginBlock( "Displaying surface in Viewer3D." );
160  QApplication application(argc,argv);
161  Viewer3D<> viewer( ks );
162  viewer.show();
163  HueShadeColorMap<MySize,1> hueShade( 0, maxDist );
164  MyDistanceVisitor visitor2( digSurf, vfunctor, bel );
165  viewer << SetMode3D( bel.className(), "Basic" );
166  viewer << CustomColors3D( Color::Black, Color::White )
167  << bel;
168  visitor2.expand();
169  std::vector< MyDistanceVisitor::Node > layer;
170  while ( ! visitor2.finished() )
171  {
172  MyNode n = visitor2.current();
173  Color c = hueShade( n.second );
174  viewer << CustomColors3D( Color::Red, c )
175  << n.first;
176  visitor2.expand();
177  }
178  viewer << Viewer3D<>::updateDisplay;
179  trace.info() << "nb surfels = " << nbSurfels << std::endl;
180  trace.endBlock();
181  return application.exec();
183 }
184 
Aim: This class is useful to perform an exploration of a graph given a starting point or set (called ...
void usage(int, char **argv)
void beginBlock(const std::string &keyword="")
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
Trace trace
Definition: Common.h:137
Component Coordinate
Type for Point elements.
Definition: PointVector.h:158
DigitalSurfaceContainer::SurfelConstIterator ConstIterator
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
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...
ConstIterator begin() const
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
int main(int argc, char **argv)
Aim: implements separable l_p metrics with exact predicates.
Aim: Define a new Functor from the composition of two other functors.
std::string className() const
bool init(const Point &lower, const Point &upper, bool isClosed)
Aim: implements methods to read a "Vol" file format.
Definition: VolReader.h:89
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...
ImageContainerBySTLVector< Domain, Value > Image
const Domain & domain() const
Definition: Image.h:192
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
DigitalSetBoundary< KSpace, DigitalSet > MyDigitalSurfaceContainer
std::ostream & error()
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
Image image(domain)
Modifier class in a Display3D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithDisplay3D.