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