DGtal  0.9.3
Functions
testSeparableMetricAdapter.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/geometry/volumes/distance/SeparableMetricAdapter.h"
#include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
#include "DGtal/geometry/volumes/distance/InexactPredicateLpSeparableMetric.h"
#include "DGtal/geometry/volumes/distance/VoronoiMap.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/helpers/StdDefs.h"
Include dependency graph for testSeparableMetricAdapter.cpp:

Go to the source code of this file.

Functions

bool testSeparableMetricAdapter ()
 
bool testMetrics ()
 
template<typename Metric >
bool testVoronoiMap (const Metric &aMetric, string filename)
 
int main (int argc, char **argv)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2013/04/25

Functions for testing class SeparableMetricAdapter.

This file is part of the DGtal library.

Definition in file testSeparableMetricAdapter.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 262 of file testSeparableMetricAdapter.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testMetrics(), testSeparableMetricAdapter(), testVoronoiMap(), and DGtal::trace.

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
bool testVoronoiMap(const Metric &aMetric, string filename)
Aim: implements separable l_p metrics with approximated predicates.
double endBlock()
std::ostream & emphase()
std::ostream & info()
bool testMetrics()
bool testSeparableMetricAdapter()

◆ testMetrics()

bool testMetrics ( )

Definition at line 89 of file testSeparableMetricAdapter.cpp.

References DGtal::Trace::beginBlock(), DGtal::ClosestFIRST, DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
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 & info()
Closest
Definition: Common.h:130

◆ testSeparableMetricAdapter()

bool testSeparableMetricAdapter ( )

Definition at line 59 of file testSeparableMetricAdapter.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
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 & info()

◆ testVoronoiMap()

template<typename Metric >
bool testVoronoiMap ( const Metric &  aMetric,
string  filename 
)

Definition at line 179 of file testSeparableMetricAdapter.cpp.

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::begin(), DGtal::Trace::beginBlock(), LibBoard::Board::clear(), domain(), DGtal::HyperRectDomain< TSpace >::end(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::end(), DGtal::Trace::endBlock(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::erase(), DGtal::Trace::info(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::insertNew(), LibBoard::Board::saveSVG(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Aim: Implementation of the linear in time Voronoi map construction.
Definition: VoronoiMap.h:126
const ConstIterator & end() const
const Domain domain(Point(1, 2), Point(6, 5))
MyDigitalSurface::ConstIterator ConstIterator
Trace trace
Definition: Common.h:137
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).
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
const ConstIterator & begin() const
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
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 &#39;Board&#39; class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70