DGtal  0.9.2
testMetrics.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 
35 #include "DGtal/geometry/volumes/distance/CSeparableMetric.h"
36 #include "DGtal/geometry/volumes/distance/CPowerSeparableMetric.h"
37 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
38 #include "DGtal/geometry/volumes/distance/ExactPredicateLpPowerSeparableMetric.h"
39 #include "DGtal/geometry/volumes/distance/InexactPredicateLpSeparableMetric.h"
41 
42 using namespace std;
43 using namespace DGtal;
44 
46 // Functions for testing class Metrics.
48 bool testMetrics()
49 {
50  unsigned int nbok = 0;
51  unsigned int nb = 0;
52 
53  trace.beginBlock ( "Testing separable metrics l_2 ..." );
54 
55  Z2i::Point a( 0,0), b(5, 0), bb(5,-10), bbb(5,5),c(10,0), d(3,3);
56  Z2i::Point starting( 0, 5), endpoint(10,5);
57 
59 
60  trace.info()<< "a= "<<a<<std::endl;
61  trace.info()<< "b= "<<b<<std::endl;
62  trace.info()<< "bb= "<<bb<<std::endl;
63  trace.info()<< "bbb= "<<bbb<<std::endl;
64  trace.info()<< "c= "<<c<<std::endl;
65 
66  trace.info() << "distance between a and bb = "<< metric(a,bb)<< std::endl;
67 
68 
69  DGtal::Closest closest =metric.closest(a,d,c);
70  nbok += (closest == ClosestFIRST) ? 1 : 0;
71  nb++;
72  trace.info() << "(" << nbok << "/" << nb << ") "
73  << "closest(a,d,c) returns d" << std::endl;
74 
75  bool hidden =metric.hiddenBy(a,b,c,starting,endpoint,0);
76  nbok += (!hidden) ? 1 : 0;
77  nb++;
78  trace.info() << "(" << nbok << "/" << nb << ") "
79  << "(a,b,c) returns false" << std::endl;
80 
81  hidden =metric.hiddenBy(a,bb,c,starting,endpoint,0);
82  nbok += (hidden) ? 1 : 0;
83  nb++;
84  trace.info() << "(" << nbok << "/" << nb << ") "
85  << "(a,bb,c) returns true" << std::endl;
86 
87  hidden =metric.hiddenBy(a,bbb,c,starting,endpoint,0);
88  nbok += (!hidden) ? 1 : 0;
89  nb++;
90  trace.info() << "(" << nbok << "/" << nb << ") "
91  << "(a,bbb,c) returns false" << std::endl;
92 
93  trace.endBlock();
94 
95  trace.beginBlock ( "Testing separable metrics l_3 ..." );
96 
97 
99 
100  trace.info()<< "a= "<<a<<std::endl;
101  trace.info()<< "b= "<<b<<std::endl;
102  trace.info()<< "bb= "<<bb<<std::endl;
103  trace.info()<< "bbb= "<<bbb<<std::endl;
104  trace.info()<< "c= "<<c<<std::endl;
105 
106 
107  hidden =metric3.hiddenBy(a,b,c,starting,endpoint,0);
108  nbok += (!hidden) ? 1 : 0;
109  nb++;
110  trace.info() << "(" << nbok << "/" << nb << ") "
111  << "(a,b,c) returns false" << std::endl;
112 
113  hidden =metric3.hiddenBy(a,bb,c,starting,endpoint,0);
114  nbok += (hidden) ? 1 : 0;
115  nb++;
116  trace.info() << "(" << nbok << "/" << nb << ") "
117  << "(a,bb,c) returns true" << std::endl;
118 
119  hidden =metric3.hiddenBy(a,bbb,c,starting,endpoint,0);
120  nbok += (!hidden) ? 1 : 0;
121  nb++;
122  trace.info() << "(" << nbok << "/" << nb << ") "
123  << "(a,bbb,c) returns false" << std::endl;
124 
125  trace.endBlock();
126 
127  return nbok == nb;
128 }
129 
130 template <typename Value>
131 bool testInexactMetrics()
132 {
133  unsigned int nbok = 0;
134  unsigned int nb = 0;
135 
136  trace.beginBlock ( "Testing inexact predicate separable metrics l_2.1 ..." );
137 
138  Z2i::Point a( 0,0), b(5, 0), bb(5,-10), bbb(5,5),c(10,0);
139  Z2i::Point starting( 0, 5), endpoint(10,5);
140 
142 
143  trace.info()<< "a= "<<a<<std::endl;
144  trace.info()<< "b= "<<b<<std::endl;
145  trace.info()<< "bb= "<<bb<<std::endl;
146  trace.info()<< "bbb= "<<bbb<<std::endl;
147  trace.info()<< "c= "<<c<<std::endl;
148 
149 
150  bool hidden =metric.hiddenBy(a,b,c,starting,endpoint,0);
151  nbok += (!hidden) ? 1 : 0;
152  nb++;
153  trace.info() << "(" << nbok << "/" << nb << ") "
154  << "(a,b,c) returns false" << std::endl;
155 
156  hidden =metric.hiddenBy(a,bb,c,starting,endpoint,0);
157  nbok += (hidden) ? 1 : 0;
158  nb++;
159  trace.info() << "(" << nbok << "/" << nb << ") "
160  << "(a,bb,c) returns true" << std::endl;
161 
162  hidden =metric.hiddenBy(a,bbb,c,starting,endpoint,0);
163  nbok += (!hidden) ? 1 : 0;
164  nb++;
165  trace.info() << "(" << nbok << "/" << nb << ") "
166  << "(a,bbb,c) returns false" << std::endl;
167 
168  trace.endBlock();
169 
170  trace.beginBlock ( "Testing inexact predicate separable metrics l_3.1 ..." );
171 
172 
174 
175  trace.info()<< "a= "<<a<<std::endl;
176  trace.info()<< "b= "<<b<<std::endl;
177  trace.info()<< "bb= "<<bb<<std::endl;
178  trace.info()<< "bbb= "<<bbb<<std::endl;
179  trace.info()<< "c= "<<c<<std::endl;
180 
181 
182  hidden =metric3.hiddenBy(a,b,c,starting,endpoint,0);
183  nbok += (!hidden) ? 1 : 0;
184  nb++;
185  trace.info() << "(" << nbok << "/" << nb << ") "
186  << "(a,b,c) returns false" << std::endl;
187 
188  hidden =metric3.hiddenBy(a,bb,c,starting,endpoint,0);
189  nbok += (hidden) ? 1 : 0;
190  nb++;
191  trace.info() << "(" << nbok << "/" << nb << ") "
192  << "(a,bb,c) returns true" << std::endl;
193 
194  hidden =metric3.hiddenBy(a,bbb,c,starting,endpoint,0);
195  nbok += (!hidden) ? 1 : 0;
196  nb++;
197  trace.info() << "(" << nbok << "/" << nb << ") "
198  << "(a,bbb,c) returns false" << std::endl;
199 
200  trace.endBlock();
201 
202  return nbok == nb;
203 }
204 
205 
206 bool testPowerMetrics()
207 {
208  unsigned int nbok = 0;
209  unsigned int nb = 0;
210 
211  trace.beginBlock ( "Testing separable weighted metrics ..." );
212 
213  Z2i::Point a( 0,0), bbis(4, 1), b(5,0), bb(5,-10), bbb(5,5),c(10,0);
214  Z2i::Point d(5,-6);
215  Z2i::Point starting( 0, 5), endpoint(10,5);
216 
218  Metric metric;
219 
220  trace.info()<< "a= "<<a<<std::endl;
221  trace.info()<< "b= "<<b<<std::endl;
222  trace.info()<< "bb= "<<bb<<std::endl;
223  trace.info()<< "bbb= "<<bbb<<std::endl;
224  trace.info()<< "c= "<<c<<std::endl;
225  trace.info()<< "d= "<<d<<std::endl;
226 
227  bool closer = (metric.closestPower(bbis,a,0,c,0) == DGtal::ClosestFIRST);
228  nbok += (closer) ? 1 : 0;
229  nb++;
230  trace.info() << "(" << nbok << "/" << nb << ") "
231  << "a is closer" << std::endl;
232 
233  closer = (metric.closestPower(bbis,a,10,c,35) == DGtal::ClosestFIRST);
234  nbok += (!closer) ? 1 : 0;
235  nb++;
236  trace.info() << "(" << nbok << "/" << nb << ") "
237  << "c is closer with w_a=10 w_c=35" << std::endl;
238  trace.endBlock();
239 
240 
241  trace.beginBlock("Testing Hidden with w=0");
242  bool hidden =metric.hiddenByPower(a,0,b,0,c,0,starting,endpoint,0);
243  nbok += (!hidden) ? 1 : 0;
244  nb++;
245  trace.info() << "(" << nbok << "/" << nb << ") "
246  << "(a,b,c) returns false" << std::endl;
247 
248  hidden =metric.hiddenByPower(a,0,bb,0,c,0,starting,endpoint,0);
249  nbok += (hidden) ? 1 : 0;
250  nb++;
251  trace.info() << "(" << nbok << "/" << nb << ") "
252  << "(a,bb,c) returns true" << std::endl;
253 
254  hidden =metric.hiddenByPower(a,0,bbb,0,c,0,starting,endpoint,0);
255  nbok += (!hidden) ? 1 : 0;
256  nb++;
257  trace.info() << "(" << nbok << "/" << nb << ") "
258  << "(a,bbb,c) returns false" << std::endl;
259 
260  hidden =metric.hiddenByPower(a,0,d,0,c,0,starting,endpoint,0);
261  nbok += (hidden) ? 1 : 0;
262  nb++;
263  trace.info() << "(" << nbok << "/" << nb << ") "
264  << "(a,d,c) returns true" << std::endl;
265  trace.endBlock();
266 
267  trace.beginBlock("Testing Hidden with w!=0");
268 
269  hidden =metric.hiddenByPower(a,0,d,30,c,0,starting,endpoint,0);
270  nbok += (hidden) ? 1 : 0;
271  nb++;
272  trace.info() << "(" << nbok << "/" << nb << ") "
273  << "(a,0,d,30,c,0) returns true" << std::endl;
274 
275  hidden =metric.hiddenByPower(a,10,d,10,c,10,starting,endpoint,0);
276  nbok += (hidden) ? 1 : 0;
277  nb++;
278  trace.info() << "(" << nbok << "/" << nb << ") "
279  << "(a,10,d,10,c,10) returns true" << std::endl;
280 
281 
282  trace.endBlock();
283 
284  return nbok == nb;
285 }
286 
287 bool testBinarySearch()
288 {
289  unsigned int nbok = 0;
290  unsigned int nb = 0;
291 
292  trace.beginBlock ( "Testing binary search of Voronoi abscissa..." );
296 
297  trace.beginBlock("Classical case");
298  Z2i::Point a(5,5), b(7,10);
299 
300  partialA = 5;
301  partialB = 7;
302 
303  // (0,9) strict in B
304  // distance( (0,9), (5,5) ) = 9
305  // distance( (0,9), (7,10)) = 8
306  //
307  // (0,8) strict in A
308  // distance( (0,8), (5,5) ) = 8
309  // distance( (0,8), (7,10) ) = 9
310 
311  Abscissa res = metric.binarySearchHidden(5, 10, partialA, partialB, 0, 15);
312  trace.info() << "Abscissa ="<<res<<std::endl;
313 
314  nbok += (res == 8) ? 1 : 0;
315  nb++;
316  trace.info() << "(" << nbok << "/" << nb << ") " << "last strict in A==(0,8)" << std::endl;
317  trace.endBlock();
318 
319  trace.beginBlock("Equidistant case");
320  Z2i::Point aa(5,5), bb(6,10);
321 
322  partialA = 5;
323  partialB = 5;
324 
325  // (0,8) in BB and AA
326  // distance( (0,8), (5,5) ) = 8
327  // distance( (0,8), (6,10)) = 8
328  //
329  // (0,7) strict in AA
330  // distance( (0,7), (5,5) ) = 7
331  // distance( (0,7), (6,10) ) = 9
332 
333  Abscissa res2 = metric.binarySearchHidden(5, 10, partialA, partialB, 0, 15);
334  trace.info() << "Abscissa ="<<res2<<std::endl;
335 
336  nbok += (res2 == 7) ? 1 : 0;
337  nb++;
338  trace.info() << "(" << nbok << "/" << nb << ") " << "last strict in AA==(0,7) (VoroVertex)" << std::endl;
339  trace.endBlock();
340 
341  //trace.beginBlock("Lower than lowerBound case");
342  //Z2i::Point aaa(105,5), bbb(0,10);
343  //partialA = 105;
344  //partialB = 0;
345  // (0,0) strict in BBB
346  // distance( (0,0), (105,5) ) = 120
347  // distance( (0,0), (0,10)) = 10
348  // Not tested since in the precondition of binarySearch
349 
350  trace.beginBlock("Greater than lowerBound case");
351 
352  partialA = 0;
353  partialB = 105;
354 
355  // (0,15) strict in AAAA
356  // distance( (0,15), (0,5) ) = 10
357  // distance( (0,15), (105,10)) = 110
358 
359  Abscissa res4 = metric.binarySearchHidden(5, 10, partialA, partialB, 0, 15);
360  trace.info() << "Abscissa ="<<res4<<std::endl;
361 
362  nbok += (res4 >= 15) ? 1 : 0;
363  nb++;
364  trace.info() << "(" << nbok << "/" << nb << ") " << "should be >= upper bound (15)" << std::endl;
365  trace.endBlock();
366 
367 
368  trace.endBlock();
369  return nbok == nb;
370 }
371 
372 
373 bool testSpecialCasesLp()
374 {
375  unsigned int nbok = 0;
376  unsigned int nb = 0;
377 
378  trace.beginBlock ( "Testing Special Cases Lp..." );
380  Z2i::Point a(5,7),b(5,8),bb(6,8),bbb(7,8),c(5,9), bbbb(105,8);
381  Z2i::Point starting(4,0), endpoint(4,15);
382 
383  bool hidden =metric.hiddenBy(a,b,c,starting,endpoint,1);
384  nbok += (!hidden) ? 1 : 0;
385  nb++;
386  trace.info() << "(" << nbok << "/" << nb << ") "
387  << "(a,b,c) returns false" << std::endl;
388  trace.info() << "Distances at (4,8) "<<metric(a, Z2i::Point(4,8))<<" "
389  << metric(b, Z2i::Point(4,8))<<" "
390  << metric(c, Z2i::Point(4,8))<<std::endl;
391 
392  //(a,bb,c)
393  hidden =metric.hiddenBy(a,bb,c,starting,endpoint,1);
394  nbok += (!hidden) ? 1 : 0;
395  nb++;
396  trace.info() << "(" << nbok << "/" << nb << ") "
397  << "(a,bb,c) returns false" << std::endl;
398 
399  trace.info() << "Distances at (4,8) "<<metric(a, Z2i::Point(4,8))<<" "
400  << metric(bb, Z2i::Point(4,8))<<" "
401  << metric(c, Z2i::Point(4,8))<<std::endl;
402 
403 
404  //(a,bbb,c)
405  hidden =metric.hiddenBy(a,bbb,c,starting,endpoint,1);
406  nbok += (hidden) ? 1 : 0;
407  nb++;
408  trace.info() << "(" << nbok << "/" << nb << ") "
409  << "(a,bbb,c) returns true" << std::endl;
410  trace.info() << "Distances at (4,8) "<<metric(a, Z2i::Point(4,8))<<" "
411  << metric(bbb, Z2i::Point(4,8))<<" "
412  << metric(c, Z2i::Point(4,8))<<std::endl;
413 
414  //(a,bbbb,c) x_abbbb should be > upper
415  hidden =metric.hiddenBy(a,bbbb,c,starting,endpoint,1);
416  nbok += (hidden) ? 1 : 0;
417  nb++;
418  trace.info() << "(" << nbok << "/" << nb << ") "
419  << "(a,bbbb,c) returns true" << std::endl;
420 
421 
422  trace.endBlock();
423  return nbok == nb;
424 }
425 
426 bool testSpecialCasesL2()
427 {
428  unsigned int nbok = 0;
429  unsigned int nb = 0;
430 
431  //Pythagorician triplet to check predicate
432  trace.beginBlock ( "Testing Special Cases L2..." );
434  Z2i::Point a(8,5),b(8,8),bb(9,8),bbb(10,8),c(8,11);
435  Z2i::Point starting(4,0), endpoint(4,15);
436 
437  bool hidden =metric.hiddenBy(a,b,c,starting,endpoint,1);
438  nbok += (!hidden) ? 1 : 0;
439  nb++;
440  trace.info() << "(" << nbok << "/" << nb << ") "
441  << "(a,b,c) returns false" << std::endl;
442  trace.info() << "Distances at (4,8) "<<metric(a, Z2i::Point(4,8))<<" "
443  << metric(b, Z2i::Point(4,8))<<" "
444  << metric(c, Z2i::Point(4,8))<<std::endl;
445 
446  hidden =metric.hiddenBy(a,bb,c,starting,endpoint,1);
447  nbok += (!hidden) ? 1 : 0;
448  nb++;
449  trace.info() << "(" << nbok << "/" << nb << ") "
450  << "(a,bb,c) returns false" << std::endl;
451 
452  trace.info() << "Distances at (4,8) "<<metric(a, Z2i::Point(4,8))<<" "
453  << metric(bb, Z2i::Point(4,8))<<" "
454  << metric(c, Z2i::Point(4,8))<<std::endl;
455 
456 
457  hidden =metric.hiddenBy(a,bbb,c,starting,endpoint,1);
458  nbok += (hidden) ? 1 : 0;
459  nb++;
460  trace.info() << "(" << nbok << "/" << nb << ") "
461  << "(a,bbb,c) returns true" << std::endl;
462  trace.info() << "Distances at (4,8) "<<metric(a, Z2i::Point(4,8))<<" "
463  << metric(bbb, Z2i::Point(4,8))<<" "
464  << metric(c, Z2i::Point(4,8))<<std::endl;
465 
466  trace.endBlock();
467  return nbok == nb;
468 }
469 
470 
471 bool testConcepts()
472 {
476  return true;
477 }
478 
480 // Standard services - public :
481 
482 int main( int argc, char** argv )
483 {
484  trace.beginBlock ( "Testing class Metrics" );
485  trace.info() << "Args:";
486  for ( int i = 0; i < argc; ++i )
487  trace.info() << " " << argv[ i ];
488  trace.info() << endl;
489 
490  bool res = testMetrics()
491  && testInexactMetrics<double>()
492  && testInexactMetrics<float>()
493  && testPowerMetrics()
494  && testBinarySearch()
495  && testSpecialCasesL2()
496  && testSpecialCasesLp()
497  && testConcepts();
498  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
499  trace.endBlock();
500  return res ? 0 : 1;
501 }
502 // //
void beginBlock(const std::string &keyword="")
Aim: implements weighted separable l_p metrics with exact predicates.
Trace trace
Definition: Common.h:130
Aim: implements separable l_p metrics with approximated predicates.
STL namespace.
double endBlock()
Aim: implements separable l_p metrics with exact predicates.
Abscissa binarySearchHidden(const Abscissa &udim, const Abscissa &vdim, const RawValue &nu, const RawValue &nv, const Abscissa &lower, const Abscissa &upper) const
std::ostream & emphase()
Aim: defines the concept of separable metrics.
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: defines the concept of separable metrics.
std::ostream & info()
bool hiddenBy(const Point &u, const Point &v, const Point &w, const Point &startingPoint, const Point &endPoint, const typename Point::UnsignedComponent dim) const
Closest closest(const Point &origin, const Point &first, const Point &second) const
TRawValue RawValue
Type for internal distance values.
Closest
Definition: Common.h:123