DGtal  1.4.beta
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 255 of file testSeparableMetricAdapter.cpp.

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

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

◆ testMetrics()

bool testMetrics ( )

Definition at line 82 of file testSeparableMetricAdapter.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 }
Aim: implements separable l_p metrics with exact predicates.
Aim: Adapts any model of CMetric to construct a separable metric (model of CSeparableMetric).
Closest
Definition: Common.h:146
@ ClosestFIRST
Definition: Common.h:146

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

Referenced by main().

◆ testSeparableMetricAdapter()

bool testSeparableMetricAdapter ( )

Definition at line 59 of file testSeparableMetricAdapter.cpp.

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 }

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

Referenced by main().

◆ testVoronoiMap()

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

Definition at line 172 of file testSeparableMetricAdapter.cpp.

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 }
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Container::const_iterator ConstIterator
ConstIterator type of the container;.
Iterator for HyperRectDomain.
const ConstIterator & end() const
const ConstIterator & begin() const
Aim: Implementation of the linear in time Voronoi map construction.
Definition: VoronoiMap.h:127
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:151
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1011
MyDigitalSurface::ConstIterator ConstIterator
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:279
Domain domain

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().