DGtal  0.9.2
testSeparableMetricAdapter.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/geometry/volumes/distance/SeparableMetricAdapter.h"
34 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
35 #include "DGtal/geometry/volumes/distance/InexactPredicateLpSeparableMetric.h"
36 #include "DGtal/geometry/volumes/distance/VoronoiMap.h"
37 #include "DGtal/io/boards/Board2D.h"
38 #include "DGtal/helpers/StdDefs.h"
40 
41 using namespace std;
42 using namespace DGtal;
43 
44 
45 struct D34 {
46 
47  typedef Z2i::Point Point;
48  typedef Z2i::Vector Vector;
49  typedef double Value;
50 
51 
52 };
53 
54 
56 // Functions for testing class SeparableMetricAdapter.
58 
59 bool testSeparableMetricAdapter()
60 {
61  unsigned int nbok = 0;
62  unsigned int nb = 0;
63 
64  trace.beginBlock ( "Testing Type instanciation ..." );
65 
66  //Distance type
68  Distance l2;
69  typedef SeparableMetricAdapter<Distance> AdaptedDistance;
70 
71  AdaptedDistance myMetric(l2);
72 
73  Z2i::Point a(0,0);
74  Z2i::Point b(14,123);
75  trace.info() << "Two point distance= "<<myMetric(a,b)<<std::endl;
76 
77 
78  nbok += true ? 1 : 0;
79  nb++;
80  trace.info() << "(" << nbok << "/" << nb << ") "
81  << "true == true" << std::endl;
82  trace.endBlock();
83 
84  return nbok == nb;
85 }
86 
87 
88 //From testMetrics.cpp
89 bool testMetrics()
90 {
91  unsigned int nbok = 0;
92  unsigned int nb = 0;
93 
94  trace.beginBlock ( "Testing separable metrics l_2 ..." );
95 
96  Z2i::Point a( 0,0), b(5, 0), bb(5,-10), bbb(5,5),c(10,0), d(3,3);
97  Z2i::Point starting( 0, 5), endpoint(10,5);
98 
100  Distance l2;
101  typedef SeparableMetricAdapter<Distance> AdaptedDistance;
102  AdaptedDistance metric(l2);
103 
104 
105  trace.info()<< "a= "<<a<<std::endl;
106  trace.info()<< "b= "<<b<<std::endl;
107  trace.info()<< "bb= "<<bb<<std::endl;
108  trace.info()<< "bbb= "<<bbb<<std::endl;
109  trace.info()<< "c= "<<c<<std::endl;
110 
111  trace.info() << "distance between a and bb = "<< metric(a,bb)<< std::endl;
112 
113 
114  DGtal::Closest closest =metric.closest(a,d,c);
115  nbok += (closest == ClosestFIRST) ? 1 : 0;
116  nb++;
117  trace.info() << "(" << nbok << "/" << nb << ") "
118  << "closest(a,d,c) returns d" << std::endl;
119 
120  bool hidden =metric.hiddenBy(a,b,c,starting,endpoint,0);
121  nbok += (!hidden) ? 1 : 0;
122  nb++;
123  trace.info() << "(" << nbok << "/" << nb << ") "
124  << "(a,b,c) returns false" << std::endl;
125 
126  hidden =metric.hiddenBy(a,bb,c,starting,endpoint,0);
127  nbok += (hidden) ? 1 : 0;
128  nb++;
129  trace.info() << "(" << nbok << "/" << nb << ") "
130  << "(a,bb,c) returns true" << std::endl;
131 
132  hidden =metric.hiddenBy(a,bbb,c,starting,endpoint,0);
133  nbok += (!hidden) ? 1 : 0;
134  nb++;
135  trace.info() << "(" << nbok << "/" << nb << ") "
136  << "(a,bbb,c) returns false" << std::endl;
137 
138  trace.endBlock();
139 
140  trace.beginBlock ( "Testing separable metrics l_3 ..." );
141 
142 
144  Distance3 l3;
145  typedef SeparableMetricAdapter<Distance3> AdaptedDistance3;
146  AdaptedDistance3 metric3(l3);
147 
148  trace.info()<< "a= "<<a<<std::endl;
149  trace.info()<< "b= "<<b<<std::endl;
150  trace.info()<< "bb= "<<bb<<std::endl;
151  trace.info()<< "bbb= "<<bbb<<std::endl;
152  trace.info()<< "c= "<<c<<std::endl;
153 
154 
155  hidden =metric3.hiddenBy(a,b,c,starting,endpoint,0);
156  nbok += (!hidden) ? 1 : 0;
157  nb++;
158  trace.info() << "(" << nbok << "/" << nb << ") "
159  << "(a,b,c) returns false" << std::endl;
160 
161  hidden =metric3.hiddenBy(a,bb,c,starting,endpoint,0);
162  nbok += (hidden) ? 1 : 0;
163  nb++;
164  trace.info() << "(" << nbok << "/" << nb << ") "
165  << "(a,bb,c) returns true" << std::endl;
166 
167  hidden =metric3.hiddenBy(a,bbb,c,starting,endpoint,0);
168  nbok += (!hidden) ? 1 : 0;
169  nb++;
170  trace.info() << "(" << nbok << "/" << nb << ") "
171  << "(a,bbb,c) returns false" << std::endl;
172 
173  trace.endBlock();
174 
175  return nbok == nb;
176 }
177 
178 template <typename Metric>
179 bool testVoronoiMap(const Metric &aMetric, string filename)
180 {
181  typedef SeparableMetricAdapter<Metric> Adapted;
182  Adapted adapted(aMetric);
183 
184  unsigned int nbok = 0;
185  unsigned int nb = 0;
186 
187  trace.beginBlock ( "Checking VoronoiMap ..." );
188 
189  Z2i::Point a(-10,-10);
190  Z2i::Point b(10,10);
191  Z2i::Domain domain(a,b);
192  Z2i::DigitalSet mySet(domain);
193  for(Z2i::Domain::ConstIterator it = domain.begin(), itend = domain.end();
194  it != itend;
195  ++it)
196  mySet.insertNew( *it );
197 
198 
199  Z2i::DigitalSet sites(domain);
200  sites.insertNew( Z2i::Point(0,-6));
201  sites.insertNew( Z2i::Point(6,0));
202  sites.insertNew( Z2i::Point(-6,0));
203  for(Z2i::DigitalSet::ConstIterator it = sites.begin(), itend = sites.end();
204  it != itend; ++it)
205  mySet.erase (*it);
206 
209 
210  VoroExact voroExact(domain, mySet,aMetric);
211  VoroAdapted voroAdapted(domain, mySet, adapted);
212 
213 
214  trace.info()<<"Exporting o SVG"<<std::endl;
215  Board2D board;
216  for(typename VoroExact::OutputImage::Domain::ConstIterator it = voroExact.domain().begin(),
217  itend = voroExact.domain().end();
218  it != itend; ++it)
219  {
220  Z2i::Point p = voroExact(*it);
221  unsigned char c = (p[1]*13 + p[0] * 7) % 256;
222  board << CustomStyle( (*it).className(), new CustomColors(Color(c,c,c),Color(c,c,c)))
223  << (*it);
224  }
225  string out = filename + "-exact.svg";
226  board.saveSVG(out.c_str());
227 
228  board.clear();
229  for(typename VoroAdapted::OutputImage::Domain::ConstIterator it = voroAdapted.domain().begin(),
230  itend = voroAdapted.domain().end();
231  it != itend; ++it)
232  {
233  Z2i::Point p = voroAdapted(*it);
234  unsigned char c = (p[1]*13 + p[0] * 7) % 256;
235  board << CustomStyle( (*it).className(), new CustomColors(Color(c,c,c),Color(c,c,c)))
236  << (*it);
237  }
238  out = filename + "-adapted.svg";
239  board.saveSVG(out.c_str());
240 
241 
242  //Checking Values
243  for(typename VoroExact::OutputImage::Domain::ConstIterator it = voroExact.domain().begin(),
244  itend = voroExact.domain().end();
245  it != itend; ++it)
246  {
247  if (voroExact(*it) != voroAdapted(*it))
248  return false;
249  }
250  nbok += true ? 1 : 0;
251  nb++;
252  trace.info() << "(" << nbok << "/" << nb << ") "
253  << "Voronoi diagram is valid !" << std::endl;
254  trace.endBlock();
255 
256  return nbok == nb;
257 }
258 
260 // Standard services - public :
261 
262 int main( int argc, char** argv )
263 {
264  trace.beginBlock ( "Testing class SeparableMetricAdapter" );
265  trace.info() << "Args:";
266  for ( int i = 0; i < argc; ++i )
267  trace.info() << " " << argv[ i ];
268  trace.info() << endl;
269 
270  bool res = testSeparableMetricAdapter() &&
271  testMetrics() &&
272  testVoronoiMap(Z2i::l2Metric,"voronoiadapted-l2") && // && ... other tests
273  testVoronoiMap(Z2i::l1Metric,"voronoiadapted-l1") && // && ... other tests
274  testVoronoiMap(InexactPredicateLpSeparableMetric<Z2i::Space>(3.444),"voronoiadapted-l3.444"); // && ... other tests
275 
276  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
277  trace.endBlock();
278  return res ? 0 : 1;
279 }
280 // //
void beginBlock(const std::string &keyword="")
Aim: Implementation of the linear in time Voronoi map construction.
Definition: VoronoiMap.h:113
const ConstIterator & begin() const
const ConstIterator & end() const
Trace trace
Definition: Common.h:130
Aim: implements separable l_p metrics with approximated predicates.
STL namespace.
double endBlock()
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
Aim: Adapts any model of CMetric to construct a separable metric (model of CSeparableMetric).
Aim: implements separable l_p metrics with exact predicates.
std::ostream & emphase()
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
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...
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
std::ostream & info()
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
Container::const_iterator ConstIterator
ConstIterator type of the container;.
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70
Closest
Definition: Common.h:123