DGtal  0.9.2
testDistanceTransformationMetrics.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
35 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
36 #include "DGtal/geometry/volumes/distance/InexactPredicateLpSeparableMetric.h"
37 #include "DGtal/images/ImageContainerBySTLVector.h"
38 #include "DGtal/kernel/sets/DigitalSetBySTLSet.h"
40 
41 using namespace std;
42 using namespace DGtal;
43 
44 
45 //Generate Random seeds
46 template<typename Image>
47 void randomSeeds(Image &input, const unsigned int nb, const int value)
48 {
49  typename Image::Point p, low = input.domain().lowerBound(), up = input.domain().upperBound();
50  typename Image::Vector ext;
51 
52  for (Dimension i = 0; i < Image::Domain::dimension; i++)
53  ext[i] = up[i] - low[i] + 1;
54 
55 
56  for (unsigned int k = 0 ; k < nb; k++)
57  {
58  for (unsigned int dim = 0; dim < Image::dimension; dim++)
59  {
60  p[dim] = rand() % (ext[dim]) + low[dim];
61  }
62  input.setValue(p, value);
63  }
64 }
65 
66 //Bruteforce quadratic computation of the distance map
67 template<typename Image, typename Pred, typename Metric>
68 bool checkVoronoi(Image &result,Pred &pointPredicate, Metric &metric)
69 {
70  typedef typename Image::Domain Domain;
71 
72  for(typename Domain::ConstIterator it = result.domain().begin(),
73  itend=result.domain().end();
74  it != itend; ++it)
75  {
76  typename Metric::Value dist = result(*it);
77 
78  for(typename Domain::ConstIterator itbis = result.domain().begin(),
79  itendbis=result.domain().end();
80  itbis != itendbis; ++itbis)
81  {
82  if (!pointPredicate(*itbis) && (metric(*it, *itbis) < dist))
83  {
84  trace.error()<< "Error in Voronoi map at "<< *it<<" computed="<<dist<<" but find="<<metric(*it, *itbis) << std::endl;
85  return false;
86  }
87  }
88  }
89  return true;
90 }
91 
92 
93 template <typename Space, int norm>
94 bool testCompareExactBruteForce(unsigned int size, unsigned int nb)
95 {
96  trace.beginBlock("Checking Exact/Inexct predicate metrics");
98  typedef HyperRectDomain<Space> Domain;
99  typedef typename Space::Point Point;
100  typedef DigitalSetBySTLSet<Domain> Set;
101  typedef functors::NotPointPredicate<Set> NegPredicate;
102 
103  Point low=Point::diagonal(0),
104  up=Point::diagonal(size);
105 
106  Domain domain(low,up);
107  Set set(domain);
108 
109  for(unsigned int i = 0; i<nb; ++i)
110  {
111  Point p;
112  for(unsigned int dim=0; dim<Space::dimension;++dim)
113  p[dim] = rand() % size;
114  set.insert(p);
115  }
116 
117  trace.info()<< "Testing metrics "<<MetricEx()<<std::endl;
118  trace.info()<< "Testing space dimension "<<Space::dimension<<std::endl;
119  trace.info()<< "Inserting "<<set.size() << " points."<<std::endl;
120 
121  NegPredicate negPred(set);
122 
124  MetricEx metricEx;
125  DTEx dtex(&domain, &negPred, &metricEx);
126 
127  bool res=checkVoronoi(dtex, negPred, metricEx);
128 
129  trace.endBlock();
130  return res;
131 }
132 
133 
134 
135 template <typename Space>
136 bool testCompareInexactBruteForce(double norm, unsigned int size, unsigned int nb)
137 {
138  trace.beginBlock("Checking Exact/Inexct predicate metrics");
139  typedef InexactPredicateLpSeparableMetric<Space> MetricInex;
140  typedef HyperRectDomain<Space> Domain;
141  typedef typename Space::Point Point;
142  typedef DigitalSetBySTLSet<Domain> Set;
143  typedef functors::NotPointPredicate<Set> NegPredicate;
144 
145  Point low=Point::diagonal(0),
146  up=Point::diagonal(size);
147 
148  Domain domain(low,up);
149  Set set(domain);
150 
151  for(unsigned int i = 0; i<nb; ++i)
152  {
153  Point p;
154  for(unsigned int dim=0; dim<Space::dimension;++dim)
155  p[dim] = rand() % size;
156  set.insert(p);
157  }
158 
159  trace.info()<< "Testing metrics "<<MetricInex(norm)<<std::endl;
160  trace.info()<< "Testing space dimension "<<Space::dimension<<std::endl;
161  trace.info()<< "Inserting "<<set.size() << " points."<<std::endl;
162 
163  NegPredicate negPred(set);
164 
166  MetricInex metricInex(norm);
167  DTIn dtinex(&domain, &negPred, &metricInex);
168 
169  bool res=checkVoronoi(dtinex, negPred, metricInex);
170 
171  trace.endBlock();
172  return res;
173 }
174 
176 // Standard services - public :
177 
178 int main( int argc, char** argv )
179 {
180  trace.beginBlock ( "Testing class DistanceTransformationMetrics" );
181  trace.info() << "Args:";
182  for ( int i = 0; i < argc; ++i )
183  trace.info() << " " << argv[ i ];
184  trace.info() << endl;
185 
186  bool res = testCompareExactBruteForce<Z2i::Space, 2>(16, 8)
187  && testCompareExactBruteForce<Z2i::Space, 1>(16, 8)
188  && testCompareExactBruteForce<Z3i::Space, 2>(16, 8)
189  && testCompareExactBruteForce<Z2i::Space, 4>(16, 8)
190  && testCompareInexactBruteForce<Z2i::Space>(2.0,16, 8)
191  && testCompareInexactBruteForce<Z2i::Space>(1.33,16, 8)
192  && testCompareInexactBruteForce<Z2i::Space>(2.6,16, 8)
193  && testCompareInexactBruteForce<Z3i::Space>(2.44,10, 5)
194  && testCompareInexactBruteForce<Z3i::Space>(12.3,10, 5);
195  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
196  trace.endBlock();
197  return res ? 0 : 1;
198 }
199 // //
void beginBlock(const std::string &keyword="")
Aim: The predicate returns true when the point predicate given at construction return false...
static const Dimension dimension
static constants to store the dimension.
Definition: SpaceND.h:132
Aim: Implementation of the linear in time distance transformation for separable metrics.
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:130
DGtal::uint32_t Dimension
Definition: Common.h:113
Aim: implements separable l_p metrics with approximated predicates.
STL namespace.
double endBlock()
void setValue(const Point &aPoint, const Value &aValue)
Definition: Image.h:247
Aim: implements separable l_p metrics with exact predicates.
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
const Domain & domain() const
Definition: Image.h:192
std::ostream & error()
Aim: A container class for storing sets of digital points within some given domain.