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