DGtal 1.3.0
Loading...
Searching...
No Matches
testHullFunctions2D.cpp
Go to the documentation of this file.
1
31#include <iostream>
32#include "DGtal/base/Common.h"
33
34#include "DGtal/geometry/tools/Hull2DHelpers.h"
35
36#include "DGtal/geometry/tools/determinant/PredicateFromOrientationFunctor2.h"
37#include "DGtal/geometry/tools/determinant/InHalfPlaneBySimple3x3Matrix.h"
39
40using namespace std;
41using namespace DGtal;
42
44// Functions for testing class HullFunctions2D.
46
51{
52 unsigned int nbok = 0;
53 unsigned int nb = 0;
54
56 typedef std::vector<Point> Container;
57
58 //orientation functor and predicate
60 OrientationFunctor orientationFunctor;
62 predicate( orientationFunctor );
63
64 //functions namespace
65 using namespace functions::Hull2D;
66
67 trace.beginBlock ( "Testing openGrahamScan" );
68
69 trace.info() << "zero point" << endl;
70 {
71 Container input, output;
72 openGrahamScan( input.begin(), input.end(), back_inserter( output ), predicate );
73 if (output.size() == 0)
74 nbok++;
75 nb++;
76 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
77 }
78
79 trace.info() << "one point" << endl;
80 {
81 Container input, output;
82 input.push_back( Point(1,1) );
83 openGrahamScan( input.begin(), input.end(), back_inserter( output ), predicate );
84 if ( (output.size() == 1) &&
85 (output.back() == Point(1,1)) )
86 nbok++;
87 nb++;
88 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
89 }
90
91 trace.info() << "two points" << endl;
92 {
93 Container input, output;
94 input.push_back( Point(1,1) );
95 input.push_back( Point(1,2) );
96 openGrahamScan( input.begin(), input.end(), back_inserter( output ), predicate );
97 if ( (output.size() == 2) &&
98 (output.at(0) == Point(1,1)) &&
99 (output.at(1) == Point(1,2)) )
100 nbok++;
101 nb++;
102 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
103 }
104
105 trace.info() << "three points" << endl;
106 {
107 Container input, output;
108
109 //three points CCW-oriented
110 input.push_back( Point(0,0) );
111 input.push_back( Point(5,0) );
112 input.push_back( Point(10,5) );
113 copy( input.begin(), input.end(), ostream_iterator<Point>( cout, " " ) );
114 cout << endl;
115
116 openGrahamScan( input.begin(), input.end(), back_inserter( output ), predicate );
117
118 copy( output.begin(), output.end(), ostream_iterator<Point>( cout, " " ) );
119 cout << endl;
120
121 if ( (output.size() == 3) &&
122 (output.at(0) == Point(0,0)) &&
123 (output.at(1) == Point(5,0)) &&
124 (output.at(2) == Point(10,5)) )
125 nbok++;
126 nb++;
127 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
128
129 //three points CW-oriented
130 output.clear();
131 input.at(2) = Point(10,-5);
132 copy( input.begin(), input.end(), ostream_iterator<Point>( cout, " " ) );
133 cout << endl;
134
135 openGrahamScan( input.begin(), input.end(), back_inserter( output ), predicate );
136
137 copy( output.begin(), output.end(), ostream_iterator<Point>( cout, " " ) );
138 cout << endl;
139
140 if ( (output.size() == 2) &&
141 (output.at(0) == Point(0,0)) &&
142 (output.at(1) == Point(10,-5)) )
143 nbok++;
144 nb++;
145 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
146 }
147
148 trace.info() << "several points" << endl;
149 {
150 Container input, output;
151 input.push_back( Point(0,5) );
152 input.push_back( Point(0,0) );
153 input.push_back( Point(1,1) );
154 input.push_back( Point(2,4) );
155 input.push_back( Point(3,9) );
156 input.push_back( Point(4,16) );
157 input.push_back( Point(5,0) );
158
159 openGrahamScan( (input.begin()+1), input.end(), back_inserter( output ), predicate );
160 if ( (output.size() == 2) &&
161 (output.at(0) == Point(0,0)) &&
162 (output.at(1) == Point(5,0)) )
163 nbok++;
164 nb++;
165 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
166
167 output.clear();
168 openGrahamScan( input.begin(), input.end(), back_inserter( output ), predicate );
169 if ( (output.size() == 3) &&
170 (output.at(0) == Point(0,5)) &&
171 (output.at(1) == Point(0,0)) &&
172 (output.at(2) == Point(5,0)) )
173 nbok++;
174 nb++;
175 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
176
177 }
178
179 trace.endBlock();
180
181 trace.beginBlock ( "Testing closedGrahamScanFromVertex" );
182
183 trace.info() << "zero point" << endl;
184 {
185 Container input, output;
186 closedGrahamScanFromVertex( input.begin(), input.end(), back_inserter( output ), predicate );
187 if (output.size() == 0)
188 nbok++;
189 nb++;
190 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
191 }
192
193 trace.info() << "one point" << endl;
194 {
195 Container input, output;
196 input.push_back( Point(1,1) );
197 closedGrahamScanFromVertex( input.begin(), input.end(), back_inserter( output ), predicate );
198 if ( (output.size() == 1) &&
199 (output.back() == Point(1,1)) )
200 nbok++;
201 nb++;
202 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
203 }
204
205 trace.info() << "two points" << endl;
206 {
207 Container input, output;
208 input.push_back( Point(1,1) );
209 input.push_back( Point(1,2) );
210 closedGrahamScanFromVertex( input.begin(), input.end(), back_inserter( output ), predicate );
211 if ( (output.size() == 2) &&
212 (output.at(0) == Point(1,1)) &&
213 (output.at(1) == Point(1,2)) )
214 nbok++;
215 nb++;
216 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
217 }
218
219 trace.info() << "three points" << endl;
220 {
221 //three points CCW-oriented
222 Container input, output;
223 input.push_back( Point(0,0) );
224 input.push_back( Point(5,0) );
225 input.push_back( Point(10,5) );
226 copy( input.begin(), input.end(), ostream_iterator<Point>( cout, " " ) );
227 cout << endl;
228
229 closedGrahamScanFromVertex( input.begin(), input.end(), back_inserter( output ), predicate );
230
231 copy( output.begin(), output.end(), ostream_iterator<Point>( cout, " " ) );
232 cout << endl;
233
234 if ( (output.size() == 3) &&
235 (output.at(0) == Point(0,0)) &&
236 (output.at(1) == Point(5,0)) &&
237 (output.at(2) == Point(10,5)) )
238 nbok++;
239 nb++;
240 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
241 }
242
243 trace.info() << "taking into account the first point" << endl;
244 {
245 Container input, output;
246 input.push_back( Point(0,-1) );
247 input.push_back( Point(1,0) );
248 input.push_back( Point(1,5) );
249 input.push_back( Point(-5,5) );
250 input.push_back( Point(-5,0) );
251 input.push_back( Point(-2,1) );
252 copy( input.begin(), input.end(), ostream_iterator<Point>( cout, " " ) );
253 cout << endl;
254
255 closedGrahamScanFromVertex( input.begin(), input.end(), back_inserter( output ), predicate );
256
257 copy( output.begin(), output.end(), ostream_iterator<Point>( cout, " " ) );
258 cout << endl;
259
260 if ( (output.size() == 5) &&
261 (output.at(0) == Point(0,-1)) &&
262 (output.at(1) == Point(1,0)) &&
263 (output.at(2) == Point(1,5)) &&
264 (output.at(3) == Point(-5,5))&&
265 (output.at(4) == Point(-5,0)) )
266 nbok++;
267 nb++;
268 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
269 }
270
271 trace.endBlock();
272
273 trace.beginBlock ( "Testing closedGrahamScanFromAnyPoint" );
274
275 trace.info() << "zero point" << endl;
276 {
277 Container input, output;
278 closedGrahamScanFromAnyPoint( input.begin(), input.end(), back_inserter( output ), predicate );
279 if (output.size() == 0)
280 nbok++;
281 nb++;
282 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
283 }
284
285 trace.info() << "one point" << endl;
286 {
287 Container input, output;
288 input.push_back( Point(1,1) );
289 closedGrahamScanFromAnyPoint( input.begin(), input.end(), back_inserter( output ), predicate );
290 if ( (output.size() == 1) &&
291 (output.back() == Point(1,1)) )
292 nbok++;
293 nb++;
294 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
295 }
296
297 trace.info() << "two points" << endl;
298 {
299 Container input, output;
300 input.push_back( Point(1,1) );
301 input.push_back( Point(1,2) );
302 closedGrahamScanFromAnyPoint( input.begin(), input.end(), back_inserter( output ), predicate );
303 if ( (output.size() == 2) &&
304 (output.at(0) == Point(1,1)) &&
305 (output.at(1) == Point(1,2)) )
306 nbok++;
307 nb++;
308 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
309 }
310
311 trace.info() << "three points" << endl;
312 {
313 //three points CCW-oriented
314 Container input, output;
315 input.push_back( Point(0,0) );
316 input.push_back( Point(5,0) );
317 input.push_back( Point(10,5) );
318 copy( input.begin(), input.end(), ostream_iterator<Point>( cout, " " ) );
319 cout << endl;
320
321 closedGrahamScanFromAnyPoint( input.begin(), input.end(), back_inserter( output ), predicate );
322
323 copy( output.begin(), output.end(), ostream_iterator<Point>( cout, " " ) );
324 cout << endl;
325
326 if ( (output.size() == 3) &&
327 (output.at(0) == Point(0,0)) &&
328 (output.at(1) == Point(5,0)) &&
329 (output.at(2) == Point(10,5)) )
330 nbok++;
331 nb++;
332 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
333 }
334
335 trace.info() << "taking into account the first point" << endl;
336 {
337 Container input, output;
338 input.push_back( Point(3,2) );
339 input.push_back( Point(2,2) );
340 input.push_back( Point(2,1) );
341 input.push_back( Point(1,1) );
342 input.push_back( Point(0,1) );
343 input.push_back( Point(0,0) );
344 input.push_back( Point(9,0) );
345 input.push_back( Point(9,6) );
346 input.push_back( Point(8,6) );
347 input.push_back( Point(8,5) );
348 input.push_back( Point(7,5) );
349 input.push_back( Point(7,4) );
350 input.push_back( Point(6,4) );
351 input.push_back( Point(5,4) );
352 input.push_back( Point(5,3) );
353 input.push_back( Point(4,3) );
354 input.push_back( Point(4,2) );
355
356 copy( input.begin(), input.end(), ostream_iterator<Point>( cout, " " ) );
357 cout << endl;
358
359 closedGrahamScanFromAnyPoint( input.begin(), input.end(), back_inserter( output ), predicate );
360
361 copy( output.begin(), output.end(), ostream_iterator<Point>( cout, " " ) );
362 cout << endl;
363
364 if ( (output.size() == 5) &&
365 (output.at(0) == Point(0,0)) &&
366 (output.at(1) == Point(9,0)) &&
367 (output.at(2) == Point(9,6)) &&
368 (output.at(3) == Point(8,6)) &&
369 (output.at(4) == Point(0,1)) )
370 nbok++;
371 nb++;
372 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
373 }
374
375 trace.endBlock();
376
377 return nbok == nb;
378}
379
381// Standard services - public :
382
383int main( int argc, char** argv )
384{
385 trace.beginBlock ( "Testing hull functions" );
386 trace.info() << "Args:";
387 for ( int i = 0; i < argc; ++i )
388 trace.info() << " " << argv[ i ];
389 trace.info() << endl;
390
391 bool res = testHullFunctions2D(); // && ... other tests
392 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
393 trace.endBlock();
394 return res ? 0 : 1;
395}
396// //
Aim: Class that implements an orientation functor, ie. it provides a way to compute the orientation o...
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
Aim: Small adapter to models of COrientationFunctor2. It is a model of concepts::CPointPredicate....
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
STL namespace.
int main()
Definition: testBits.cpp:56
MyPointD Point
Definition: testClone2.cpp:383
bool testHullFunctions2D()