DGtal  0.9.2
testChamferVoro.cpp
1 
30 #include <iostream>
32 #include <set>
33 #include <algorithm>
34 #include "DGtal/base/Common.h"
35 #include "ConfigTest.h"
36 #include "DGtal/helpers/StdDefs.h"
37 #include "DGtal/geometry/volumes/distance/ChamferNorm2D.h"
38 #include "DGtal/geometry/volumes/distance/CMetricSpace.h"
39 #include "DGtal/geometry/volumes/distance/CSeparableMetric.h"
40 
41 #include "DGtal/geometry/volumes/distance/VoronoiMap.h"
42 
43 #include "DGtal/io/colormaps/HueShadeColorMap.h"
44 #include "DGtal/io/colormaps/RandomColorMap.h"
45 
46 #include "DGtal/io/boards/Board2D.h"
48 
49 using namespace std;
50 using namespace DGtal;
51 using namespace Z2i;
52 
54 // Functions for testing class ChamferDT.
56 template <typename VoroMap>
57 void saveVoroMap(const std::string &filename,const VoroMap &output)
58 {
59  typedef HueShadeColorMap<double,2> Hue;
60  double maxdt=0.0;
61 
62  for ( typename VoroMap::Domain::ConstIterator it = output.domain().begin(), itend = output.domain().end();
63  it != itend; ++it)
64  if ( output(*it) > maxdt)
65  maxdt = output(*it);
66 
67 
68  Board2D board;
69  Hue hue(0,maxdt);
70 
71  for(typename VoroMap::Domain::ConstIterator it = output.domain().begin(),
72  itend = output.domain().end();
73  it != itend; ++it)
74  {
75  typename VoroMap::Value point = output(*it);
76  board << (*it);
77  }
78 
79  board.saveSVG(filename.c_str());
80 }
81 
82 
83 
84 
85 bool testChamferVoro()
86 {
87  unsigned int nbok = 0;
88  unsigned int nb = 0;
89  trace.beginBlock ( "Testing VoronoiMap with chamfer norm...");
90 
91  //5-7-11 metic
93  Metric::Directions dirs5711;
94  Metric::Directions normals5711;
95  //5-7-11 mask
96  dirs5711.push_back(Z2i::Vector(0,-1));
97  dirs5711.push_back(Z2i::Vector(1,-2));
98  dirs5711.push_back(Z2i::Vector(1,-1));
99  dirs5711.push_back(Z2i::Vector(2,-1));
100  dirs5711.push_back(Z2i::Vector(1,0));
101  dirs5711.push_back(Z2i::Vector(2,1));
102  dirs5711.push_back(Z2i::Vector(1,1));
103  dirs5711.push_back(Z2i::Vector(1,2));
104 
105  normals5711.push_back(Z2i::Vector(1,-5));
106  normals5711.push_back(Z2i::Vector(3,-4));
107  normals5711.push_back(Z2i::Vector(4,-3));
108  normals5711.push_back(Z2i::Vector(5,-1));
109  normals5711.push_back(Z2i::Vector(5,1));
110  normals5711.push_back(Z2i::Vector(4,3));
111  normals5711.push_back(Z2i::Vector(3,4));
112  normals5711.push_back(Z2i::Vector(1,5));
113 
114  Metric mask5711(dirs5711,normals5711);
115 
116 
117  Z2i::Point a(-20,-20);
118  Z2i::Point b(20,20);
119  Z2i::Domain domain(a,b);
120  Z2i::DigitalSet mySet(domain);
121 
122  for(Z2i::Domain::ConstIterator it = domain.begin(), itend = domain.end();
123  it != itend;
124  ++it)
125  mySet.insertNew( *it );
126 
127 
128  Z2i::DigitalSet sites(domain);
129  sites.insertNew( Z2i::Point(0,-6));
130  sites.insertNew( Z2i::Point(6,0));
131  sites.insertNew( Z2i::Point(-6,0));
132  sites.insertNew( Z2i::Point(0,4));
133  sites.insertNew( Z2i::Point(0,0));
134  sites.insertNew( Z2i::Point(-12,-12));
135  sites.insertNew( Z2i::Point(0,-12));
136 
137  sites.insertNew( Z2i::Point(12,-12));
138  sites.insertNew( Z2i::Point(3,4));
139  sites.insertNew( Z2i::Point(-3,7));
140 
141  sites.insertNew( Z2i::Point(-16,16));
142  sites.insertNew( Z2i::Point(-15,15));
143  sites.insertNew( Z2i::Point(-13,13));
144 
145 
146  sites.insertNew( Z2i::Point(15,16));
147  sites.insertNew (Z2i::Point(-16, 3));
148 
149 
150 
151  sites.insertNew( Z2i::Point(2,16));
152  sites.insertNew( Z2i::Point(3,16));
153  sites.insertNew (Z2i::Point(4,16));
154  sites.insertNew (Z2i::Point(5,17));
155 
156 
157  for(Z2i::DigitalSet::ConstIterator it = sites.begin(), itend = sites.end();
158  it != itend; ++it)
159  mySet.erase (*it);
160 
162  VoroChamf voro(&domain, &mySet,&mask5711);
163 
164  Board2D board;
165  HueShadeColorMap< int> map(0, 256,2);
166  RandomColorMap map2(0,256);
167 
168  for(VoroChamf::OutputImage::Domain::ConstIterator it = voro.domain().begin(),
169  itend = voro.domain().end();
170  it != itend; ++it)
171  {
172  Z2i::Point p = voro(*it);
173  int c = abs(p[1]*11+ p[0]*17);
174  if (p == *it)
175  board << CustomStyle( (*it).className(), new CustomColors(Color::Black,Color::Black));
176  else
177  if ((*it)[0]==-16 )
178  board << CustomStyle( (*it).className(), new CustomColors(Color(c%256,0,0),Color(c%256,0,0)));
179  else
180  board << CustomStyle( (*it).className(), new CustomColors(map(c%256),map(c%256)));
181 
182  board << (*it);
183  }
184 
185  board.saveSVG("Voromap.svg");
186 
187 
188  for(int j= -20; j <= 20 ; j++)
189  trace.info()<< " Reading "<< voro(Point(-16,j))<<std::endl;
190 
191 
192  return nbok == nb;
193 }
194 
195 
196 
197 bool testChamferVoroLarge()
198 {
199  unsigned int nbok = 0;
200  unsigned int nb = 0;
201  trace.beginBlock ( "Testing VoronoiMap...");
202 
203  //5-7-11 metic
205  Metric::Directions dirs5711;
206  Metric::Directions normals5711;
207  //5-7-11 mask
208  dirs5711.push_back(Z2i::Vector(0,-1));
209  dirs5711.push_back(Z2i::Vector(1,-2));
210  dirs5711.push_back(Z2i::Vector(1,-1));
211  dirs5711.push_back(Z2i::Vector(2,-1));
212  dirs5711.push_back(Z2i::Vector(1,0));
213  dirs5711.push_back(Z2i::Vector(2,1));
214  dirs5711.push_back(Z2i::Vector(1,1));
215  dirs5711.push_back(Z2i::Vector(1,2));
216 
217  normals5711.push_back(Z2i::Vector(1,-5));
218  normals5711.push_back(Z2i::Vector(3,-4));
219  normals5711.push_back(Z2i::Vector(4,-3));
220  normals5711.push_back(Z2i::Vector(5,-1));
221  normals5711.push_back(Z2i::Vector(5,1));
222  normals5711.push_back(Z2i::Vector(4,3));
223  normals5711.push_back(Z2i::Vector(3,4));
224  normals5711.push_back(Z2i::Vector(1,5));
225 
226  Metric mask5711(dirs5711,normals5711);
227 
228  unsigned int N = 64;
229 
230  Z2i::Point a(0,0);
231  Z2i::Point b(N,N);
232  Z2i::Domain domain(a,b);
233  Z2i::DigitalSet mySet(domain);
234 
235  for(Z2i::Domain::ConstIterator it = domain.begin(), itend = domain.end();
236  it != itend;
237  ++it)
238  mySet.insertNew( *it );
239 
240 
241  Z2i::DigitalSet sites(domain);
242  for(unsigned int i = 0; i < N; i++)
243  sites.insert(Z2i::Point( rand() % N , rand()% N ));
244 
245  std::copy(sites.begin(), sites.end() , std::ostream_iterator<Z2i::Point>(std::cout,", "));
246 
247  for(Z2i::DigitalSet::ConstIterator it = sites.begin(), itend = sites.end();
248  it != itend; ++it)
249  mySet.erase (*it);
250 
252  VoroChamf voro(&domain, &mySet,&mask5711);
253 
254  Board2D board;
255  HueShadeColorMap< int> map(0, 256,2);
256  for(VoroChamf::OutputImage::Domain::ConstIterator it = voro.domain().begin(),
257  itend = voro.domain().end();
258  it != itend; ++it)
259  {
260  Z2i::Point p = voro(*it);
261  int c = abs(p[1]*11+ p[0]*17);
262  if (p == *it)
263  board << CustomStyle( (*it).className(), new CustomColors(Color::Black,Color::Black));
264  /* else
265  if ((*it)[0]==1 )
266  board << CustomStyle( (*it).className(), new CustomColors(Color(c%256,0,0),Color(c%256,0,0)));
267  */ else
268  board << CustomStyle( (*it).className(), new CustomColors(map(c),map(c)));
269 
270  board << (*it);
271  }
272 
273 
274 
275  board.saveSVG("Voromap-large.svg");
276 
277  return nbok == nb;
278 }
279 
280 
281 
283 // Standard services - public :
284 
285 int main( int argc, char** argv )
286 {
287  trace.beginBlock ( "Testing class ChamferDT" );
288  trace.info() << "Args:";
289  for ( int i = 0; i < argc; ++i )
290  trace.info() << " " << argv[ i ];
291  trace.info() << endl;
292 
293  bool res = testChamferVoro()&& testChamferVoroLarge(); // && ... other tests
294  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
295  trace.endBlock();
296  return res ? 0 : 1;
297 }
298 // //
void beginBlock(const std::string &keyword="")
Aim: Implementation of the linear in time Voronoi map construction.
Definition: VoronoiMap.h:113
const ConstIterator & begin() const
const ConstIterator & end() const
Trace trace
Definition: Common.h:130
Aim: access to random color from a gradientColorMap.
STL namespace.
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: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: implements a model of CSeparableMetric for Chamfer and path based norms.
Definition: ChamferNorm2D.h:86
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
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 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70