DGtal  1.4.beta
testDistancePropagation.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include <set>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/kernel/CanonicEmbedder.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/graph/CUndirectedSimpleGraph.h"
37 #include "DGtal/graph/CGraphVisitor.h"
38 #include "DGtal/graph/GraphVisitorRange.h"
39 #include "DGtal/graph/DistanceBreadthFirstVisitor.h"
40 #include "DGtal/geometry/volumes/distance/LpMetric.h"
41 #include "DGtal/io/boards/Board2D.h"
42 #include "DGtal/io/Color.h"
43 #include "DGtal/io/colormaps/GradientColorMap.h"
44 #include "DGtal/shapes/Shapes.h"
45 #include "DGtal/images/ImageContainerBySTLVector.h"
47 
48 
49 using namespace std;
50 using namespace DGtal;
51 using namespace DGtal::concepts;
52 
54 // Functions for testing objects as graph.
56 
59 {
60  typedef Z2i::Space Space;
61  typedef Z2i::Point Point;
62  typedef Z2i::Domain Domain;
64  typedef Z2i::Object4_8 Object;
65 
66  BOOST_CONCEPT_ASSERT(( CUndirectedSimpleGraph<Z2i::Object4_8> ));
67 
68  trace.beginBlock( "Distance propagation in 2D object" );
69  Point p1( -41, -36 );
70  Point p2( 18, 18 );
71  Domain domain( p1, p2 );
72  Point c1( -2, -1 );
73  Point c2( -14, 5 );
74  Point c3( -30, -15 );
75  Point c4( -10, -20 );
76  Point c5( 12, -1 );
77  DigitalSet shape_set( domain );
78 
79  Shapes<Domain>::addNorm2Ball( shape_set, c1, 9 );
80  Shapes<Domain>::addNorm1Ball( shape_set, c2, 9 );
81  Shapes<Domain>::addNorm1Ball( shape_set, c3, 10 );
82  Shapes<Domain>::addNorm2Ball( shape_set, c4, 12 );
83  Shapes<Domain>::addNorm1Ball( shape_set, c5, 4 );
84 
85  Object obj(Z2i::dt4_8, shape_set);
86 
87 
88  GradientColorMap<int> cmap_grad( 0, 25);
89  cmap_grad.addColor( Color( 0, 0, 255 ) );
90  cmap_grad.addColor( Color( 0, 255, 0 ) );
91  cmap_grad.addColor( Color( 255, 0, 0 ) );
92 
93  Board2D board;
94  board << SetMode( domain.className(), "Paving" )
95  << domain
96  << SetMode( p1.className(), "Paving" );
97 
98  Image image = ImageFromSet<Image>::create(shape_set, 1);
99 
100  // Type definitions
101  typedef CanonicEmbedder<Space> VertexEmbedder;
103  typedef RealPoint::Coordinate Scalar;
104  typedef LpMetric<Space> Distance;
105  using DistanceToPoint = std::function<double(const Space::RealPoint &)>;
108 
109  BOOST_CONCEPT_ASSERT(( CGraphVisitor<Visitor> ));
110 
111 
112  VertexEmbedder embedder;
113  Distance distance(2.0);
114  DistanceToPoint distanceToPoint = std::bind(distance, embedder(c1), std::placeholders::_1);
115 
116  VertexFunctor vfunctor( embedder, distanceToPoint );
117  Visitor visitor( obj, vfunctor, c1 );
118 
119  while( ! visitor.finished() )
120  {
121  Scalar v = visitor.current().second;
122  image.setValue( visitor.current().first, v );
123  visitor.expand();
124  }
125 
126  string specificStyle = p1.className() + "/Paving";
127 
128  for ( DigitalSet::ConstIterator it = shape_set.begin();
129  it != shape_set.end();
130  ++it )
131  {
132  if( image(*it) == 0)
133  board << CustomStyle( specificStyle,
134  new CustomColors( Color::Black,
135  Color::Red ) );
136  else if( image(*it) > 0 )
137  board << CustomStyle( specificStyle,
138  new CustomColors( Color::Black,
139  cmap_grad( image(*it) ) ) );
140  else
141  board << CustomStyle( specificStyle,
142  new CustomColors( Color::Black,
143  cmap_grad( 0 ) ) );
144  board << *it;
145  }
146 
147  trace.info() << "- Output file testDistancePropagation.eps" << std::endl;
148  board.saveEPS("testDistancePropagation.eps");
149  trace.endBlock();
150 
151  trace.beginBlock( "Distance visitor as a range." );
152  typedef GraphVisitorRange<Visitor> VisitorRange;
153  VisitorRange range( new Visitor( obj, vfunctor, c1 ) );
154  Scalar d = -1.0;
155  unsigned int nb = 0;
156  unsigned int nbok = 0;
157  unsigned int nbperfect = 0;
158  for ( VisitorRange::NodeConstIterator it = range.beginNode(), itEnd = range.endNode();
159  it != itEnd; ++it )
160  { // Vertex is *it.first
161  Scalar next_d = (*it).second;
162  ++nb; nbok += (next_d >= d-0.75 ) ? 1 : 0;
163  nbperfect += (next_d >= d ) ? 1 : 0;
164  d = next_d;
165  }
166  trace.info() << "(" << nbok << "/" << nb
167  << ") number of vertices in approximate Euclidean distance ordering."<< std::endl;
168  trace.info() << "(" << nbperfect << "/" << nb
169  << ") number of vertices in perfect Euclidean distance ordering."<< std::endl;
170  trace.endBlock();
171  return nb == nbok;
172 }
173 
174 int main( int /*argc*/, char** /*argv*/ )
175 {
176  bool res = testDistancePropagation();
177  return res ? 0 : 1;
178 }
179 
180 
181 
182 
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
const Node & current() const
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Container::const_iterator ConstIterator
ConstIterator type of the container;.
Aim: This class is useful to perform an exploration of a graph given a starting point or set (called ...
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
void addColor(const Color &color)
Aim: Transforms a graph visitor into a single pass input range.
std::string className() const
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
Aim: implements l_p metrics.
Definition: LpMetric.h:75
Aim: An object (or digital object) represents a set in some digital space associated with a digital t...
Definition: Object.h:120
Component Coordinate
Type for Point elements.
Definition: PointVector.h:617
Aim: A utility class for constructing different shapes (balls, diamonds, and others).
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Aim: Define a new Functor from the composition of two other functors.
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:804
BreadthFirstVisitor< MyDigitalSurface > Visitor
Aim: Gathers several functions useful for concept checks.
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Aim: A trivial embedder for digital points, which corresponds to the canonic injection of Zn into Rn.
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:279
Aim: Define utilities to convert a digital set into an image.
Definition: ImageFromSet.h:64
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
Aim: Defines the concept of a visitor onto a graph, that is an object that traverses vertices of the ...
Aim: Represents the concept of local graph: each vertex has neighboring vertices, but we do not neces...
MyPointD Point
Definition: testClone2.cpp:383
ImageContainerBySTLVector< Z2i::Domain, int > Image
int main(int, char **)
bool testDistancePropagation()
Domain domain
Image image(domain)
HyperRectDomain< Space > Domain
PointVector< 3, double > RealPoint
Z2i::DigitalSet DigitalSet