DGtal 2.1.1
Loading...
Searching...
No Matches
testArithmeticalDSL.cpp
1
31#include <iostream>
32#include <boost/iterator/iterator_concepts.hpp>
33#include "DGtal/base/Common.h"
34#include "DGtal/kernel/CPointPredicate.h"
35#include "DGtal/geometry/curves/ArithmeticalDSL.h"
37
38using namespace std;
39using namespace DGtal;
40
42// Functions for testing class ArithmeticalDSL.
44
49template <typename DSL>
50bool mainTest()
51{
52 BOOST_CONCEPT_ASSERT(( concepts::CPointPredicate<DSL> ));
53
54 typedef typename DSL::Point Point;
55
56 unsigned int nbok = 0;
57 unsigned int nb = 0;
58
59 trace.beginBlock ( "Main operators..." );
60
61 trace.info() << "constructor, copy, assignment, equality" << std::endl;
62
63 DSL dsl(0, 1, 0);
64 DSL dsl2 = dsl;
65 DSL dsl3(1, 1, 0);
66 DSL dsl4 = dsl3;
67 dsl3 = dsl2 = dsl;
68
69 //egalite, difference
70 DSL dsl5(0, -1, 0);
71
72 if ( (dsl == dsl2)
73 &&(dsl == dsl3)
74 &&(dsl != dsl4)
75 &&(dsl == dsl5) )
76 nbok++;
77 nb++;
78 trace.info() << "(" << nbok << "/" << nb << ") "
79 << std::endl;
80
81 //validite
82 trace.info() << "valid dsl" << std::endl;
83 if ( dsl.isValid() && dsl3.isValid() && dsl5.isValid() )
84 nbok++;
85 nb++;
86 trace.info() << "(" << nbok << "/" << nb << ") "
87 << std::endl;
88
89 DSL dsl6(0, 0, 1);
90
91 trace.info() << "not valid dsl" << std::endl;
92 if (!dsl6.isValid())
93 nbok++;
94 nb++;
95
96 trace.info() << "(" << nbok << "/" << nb << ") "
97 << std::endl;
98
99 //accessors
100 trace.info() << "a,b,mu,omega accessors" << std::endl;
101
102 if ( (dsl.a() == 0)&&(dsl.b() == 1)&&(dsl.mu() == 0)&&(dsl.omega() == 1) )
103 nbok++;
104 nb++;
105
106 trace.info() << "(" << nbok << "/" << nb << ") "
107 << std::endl;
108
109
110 DSL dsl7(5, 8, 0);
111
112 trace.info() << "remainder, position, tests" << std::endl;
113 trace.info() << dsl7 << std::endl;
114
115 if ( (dsl7.isValid())
116 && (dsl7.remainder( Point(8,5) ) == 0)
117 &&(dsl7.remainder( Point(16,10) ) == 0)
118 &&(dsl7.remainder( Point(3,2) ) == -1)
119 &&(dsl7.remainder( Point(5,3) ) == 1) )
120 nbok++;
121 nb++;
122
123 trace.info() << "(" << nbok << "/" << nb << ") "
124 << std::endl;
125
126 if ( (dsl7.orthogonalPosition( Point(0,0) ) == 0)
127 &&(dsl7.orthogonalPosition( Point(8,5) ) == 89)
128 &&(dsl7.orthogonalPosition( Point(1,0) ) == 8)
129 &&(dsl7.orthogonalPosition( Point(-1,0) ) == -8) )
130 nbok++;
131 nb++;
132
133 if ( ( dsl7.before( Point(0,0), Point(8,5) ) )
134 &&(!dsl7.before( Point(8,5), Point(0,0) ) )
135 &&( dsl7.beforeOrEqual( Point(0,0), Point(8,5) ) )
136 &&(!dsl7.beforeOrEqual( Point(8,5), Point(0,0) ) )
137 &&( dsl7.before( Point(-1,0), Point(1,0) ) )
138 &&( !dsl7.before( Point(1,0), Point(-1,0) ) )
139 &&( !dsl7.before( Point(8,5), Point(8,5) ) )
140 &&( dsl7.beforeOrEqual( Point(8,5), Point(8,5) ) )
141 )
142 nbok++;
143 nb++;
144
145 trace.info() << "(" << nbok << "/" << nb << ") "
146 << std::endl;
147
148 if ( (dsl7.isInDSL( Point(0,0) ))
149 &&(dsl7.isInDSL( Point(16,10) ))
150 &&(dsl7.isInDSL( Point(5,3) ))
151 &&(!dsl7.isInDSL( Point(3,2) )) )
152 nbok++;
153 nb++;
154
155 trace.info() << "(" << nbok << "/" << nb << ") "
156 << std::endl;
157
158 if ( (dsl7( Point(0,0) ))
159 &&(dsl7( Point(16,10) ))
160 &&(dsl7( Point(5,3) ))
161 &&(!dsl7( Point(3,2) ))
162 &&(!dsl7( Point(-1,0) ))
163 &&(dsl7( Point(-1,-1) )) )
164 nbok++;
165 nb++;
166
167 trace.info() << "(" << nbok << "/" << nb << ") "
168 << std::endl;
169
170 trace.info() << "shift" << std::endl;
171 if ( (dsl.remainder(dsl.shift()) == dsl.omega())
172 && (DSL::toCoordinate(dsl.omega()) == dsl.patternLength()) )
173 nbok++;
174 nb++;
175 trace.info() << "(" << nbok << "/" << nb << ") "
176 << std::endl;
177
178 if ( (dsl7.getPoint() == Point(0,0))
179 &&(DSL(5,8,dsl7.omega()-1).getPoint() == Point(0,0)+dsl.shift())
180 &&(DSL(5,8,dsl7.omega()).getPoint() == Point(0,0)+dsl.shift())
181 &&(DSL(5,8,dsl7.omega()+1).getPoint() == Point(0,0)+2*dsl.shift())
182 &&(DSL(5,8,-dsl7.omega()+1).getPoint() == Point(0,0))
183 &&(DSL(5,8,-dsl7.omega()).getPoint() == Point(0,0)-dsl.shift())
184 &&(DSL(5,8,-dsl7.omega()-1).getPoint() == Point(0,0)-dsl.shift())
185 )
186 nbok++;
187 nb++;
188
189 trace.info() << "(" << nbok << "/" << nb << ") "
190 << std::endl;
191
192
193 trace.endBlock();
194
195 return nbok == nb;
196}
197
198
200
207template <typename DSL>
208bool rangeTest(const DSL& dsl)
209{
210 typedef typename DSL::Point Point;
211
212 unsigned int nbok = 0;
213 unsigned int nb = 0;
214
215 trace.beginBlock ( "Range/Iterator services..." );
216 trace.info() << dsl << std::endl;
217
218 Point origin = dsl.getPoint();
219 Point first = Point(origin[0]-dsl.b(), origin[1]-dsl.a());
220 Point last = Point(first[0]+dsl.b(), first[1]+dsl.a());
221 trace.info() << "from " << first << " to " << last << std::endl;
222
223 if (dsl.isValid())
224 nbok++;
225 nb++;
226
227 trace.info() << "(" << nbok << "/" << nb << ") "
228 << std::endl;
229
230 {//forward pass
231 typedef typename DSL::ConstIterator I;
232 BOOST_CONCEPT_ASSERT(( boost_concepts::ReadableIteratorConcept<I> ));
233 BOOST_CONCEPT_ASSERT(( boost_concepts::RandomAccessTraversalConcept<I> ));
234 bool res = true;
235 int c = 0;
236 for (I it = dsl.begin(first), itEnd = dsl.end(last);
237 ( (it != itEnd)&&(res)&&(c<100) );
238 ++it, ++c)
239 {
240 trace.info() << "(" << it->operator[](0) << "," << it->operator[](1) << ") ";
241 if ( !dsl(*it) )
242 res = false;
243 }
244 trace.info() << " : " << c << " points " << std::endl;
245 trace.info() << std::endl;
246
247 if ( (res)&&(c == (dsl.omega()+1)) )
248 nbok++;
249 nb++;
250
251 trace.info() << "(" << nbok << "/" << nb << ") "
252 << std::endl;
253 }
254
255 {//backward pass
256 typedef typename DSL::ConstReverseIterator I;
257 BOOST_CONCEPT_ASSERT(( boost_concepts::ReadableIteratorConcept<I> ));
258 BOOST_CONCEPT_ASSERT(( boost_concepts::RandomAccessTraversalConcept<I> ));
259 bool res = true;
260 int c = 0;
261 for (I it = dsl.rbegin(last), itEnd = dsl.rend(first);
262 ( (it != itEnd)&&(res)&&(c<100) );
263 ++it, ++c)
264 {
265 trace.info() << "(" << it->operator[](0) << "," << it->operator[](1) << ") ";
266 if ( !dsl(*it) )
267 res = false;
268 }
269 trace.info() << " : " << c << " points " << std::endl;
270 trace.info() << std::endl;
271
272 if ( (res)&&(c == (dsl.omega()+1)) )
273 nbok++;
274 nb++;
275
276 trace.info() << "(" << nbok << "/" << nb << ") "
277 << std::endl;
278 }
279
280 {//random access services
281 typedef typename DSL::ConstIterator I;
282 BOOST_CONCEPT_ASSERT(( boost_concepts::ReadableIteratorConcept<I> ));
283 BOOST_CONCEPT_ASSERT(( boost_concepts::RandomAccessTraversalConcept<I> ));
284 bool res = true;
285 int c = 0;
286 I itBegin = dsl.begin(first);
287 for (I it = itBegin, itEnd = dsl.end(last);
288 ( (it != itEnd)&&(res)&&(c<100) );
289 ++it, ++c)
290 {
291 trace.info() << "(" << it->operator[](0) << "," << it->operator[](1) << ") " << it.remainder() << ", ";
292 I it2 = ( itBegin + c );
293 if ( (it != it2) || ((it2 - itBegin) != c) )
294 res = false;
295 }
296 int n = c;
297 trace.info() << " : " << c << " points " << std::endl;
298 trace.info() << std::endl;
299
300 if (res)
301 nbok++;
302 nb++;
303
304 trace.info() << "(" << nbok << "/" << nb << ") "
305 << std::endl;
306
307 --n;
308 c = 0;
309 for (I it = (itBegin+n), itEnd = itBegin;
310 ( (it!=itEnd)&&(res)&&(c < 100) );
311 --it, ++c )
312 {
313 trace.info() << "(" << it->operator[](0) << "," << it->operator[](1) << ") " << it.remainder() << ", ";
314 I it2 = ( (itBegin+n) - c );
315 if ( (it != it2) || (((itBegin+n) - it2) != c) )
316 res = false;
317 }
318
319 if (res)
320 nbok++;
321 nb++;
322
323 trace.info() << "(" << nbok << "/" << nb << ") "
324 << std::endl;
325 }
326
327
328 trace.endBlock();
329
330 return nbok == nb;
331}
332
333
334
335template <typename DSL>
336bool sameOctantTest(const DSL& dsl1, const DSL& dsl2)
337{
338 trace.beginBlock ( "Test same octant" );
339 trace.info() << dsl1 << " " << dsl2 << std::endl;
340
341 typename DSL::Octant::first_type oc;
342
343 return dsl1.sameOctant(dsl2,&oc);
344
345 trace.endBlock();
346
347
348}
349
350
351template <typename DSL>
352typename DSL::Octant testOctant(const typename DSL::Coordinate & a, const typename DSL::Coordinate & b)
353{
354
355 DSL aDSL(a,b,0);
356 trace.info() << aDSL << std::endl;
357
358 return aDSL.octant();
359
360}
361
362
363
365int main( int argc, char** argv )
366{
367 trace.beginBlock ( "Testing class ArithmeticalDSL" );
368 trace.info() << "Args:";
369 for ( int i = 0; i < argc; ++i )
370 trace.info() << " " << argv[ i ];
371 trace.info() << endl;
372
373 //main operators
374 bool res = mainTest<DGtal::ArithmeticalDSL<DGtal::int32_t> >()
376 && mainTest<DGtal::NaiveDSL<DGtal::int32_t> >()
378 ;
379
380 { //range services for 8 adjacency
382
383 res = res
384 && rangeTest( DSL(5, 8, 16) )
385 && rangeTest( DSL(8, 5, 14) )
386 && rangeTest( DSL(5, -8, 14) )
387 && rangeTest( DSL(8, -5, 14) )
388 && rangeTest( DSL(-5, 8, 14) )
389 && rangeTest( DSL(-8, 5, 14) )
390 && rangeTest( DSL(-5, -8, 14) )
391 && rangeTest( DSL(-8, -5, 14) )
392 && rangeTest( DSL(1, 0, 14) )
393 && rangeTest( DSL(0, -1, 14) )
394 && rangeTest( DSL(0, 1, 14) )
395 && rangeTest( DSL(-1, 0, 14) )
396 && rangeTest( DSL(1, 1, 14) )
397 && rangeTest( DSL(1, -1, 14) )
398 && rangeTest( DSL(-1, 1, 14) )
399 && rangeTest( DSL(-1, -1, 14) )
400 ;
401 }
402
403
404 { //range services for 4 adjacency
406
407 res = res
408 && rangeTest( DSL(5, 8, -16) )
409 && rangeTest( DSL(8, 5, -17) )
410 && rangeTest( DSL(5, -8, -17) )
411 && rangeTest( DSL(8, -5, -17) )
412 && rangeTest( DSL(-5, 8, -17) )
413 && rangeTest( DSL(-8, 5, -17) )
414 && rangeTest( DSL(-5, -8, -17) )
415 && rangeTest( DSL(-8, -5, -17) )
416 && rangeTest( DSL(1, 0, -17) )
417 && rangeTest( DSL(0, -1, -17) )
418 && rangeTest( DSL(0, 1, -17) )
419 && rangeTest( DSL(-1, 0, -17) )
420 ;
421 }
422
423
424 { // same octant test
426
427 res = res
428 && sameOctantTest(DSL(5,8,16),DSL(1,2,3))==true
429 && sameOctantTest(DSL(5,8,16),DSL(2,1,3))==false
430 && sameOctantTest(DSL(2,2,16),DSL(6,3,3))==true
431 && sameOctantTest(DSL(2,2,16),DSL(3,3,3))==true
432 && sameOctantTest(DSL(5,-8,16),DSL(0,-2,3))==true
433 && sameOctantTest(DSL(5,8,16),DSL(-2,1,3))==false
434 ;
435 }
436
437 // ---------------- octant tests -------------------------
438
439 {
441 typedef DSL::Octant Octant;
442
443 trace.beginBlock("Test octant computation");
444
445 res = res
446 && testOctant<DSL>(0,0) == Octant(-1,-1)
447 && testOctant<DSL>(0,5) == Octant(0,7)
448 && testOctant<DSL>(0,-5) == Octant(3,4)
449 && testOctant<DSL>(5,0) == Octant(1,2)
450 && testOctant<DSL>(-5,0) == Octant(5,6)
451 && testOctant<DSL>(1,1) == Octant(0,1)
452 && testOctant<DSL>(1,-1) == Octant(2,3)
453 && testOctant<DSL>(-1,1) == Octant(6,7)
454 && testOctant<DSL>(-1,-1) == Octant(4,5)
455 ;
456
457 }
458
459 {
461 res = res && rangeTest( DSL(5, 8, -26) ) && rangeTest( DSL(5, 8, 13) )
462 && rangeTest( DSL(5, 8, -17) ) && rangeTest( DSL(5, 8, 11313) );
463 }
464 // Warning: BOOST_CONCEPT_ASSERT(( boost_concepts::RandomAccessTraversalConcept<I> ));
465 // does not accept DGtal::BigInteger as a difference type for random access iterators
466 // because it uses methods is_signed and is_integer of std::numeric_limits
467 // { //does not compile
468 // typedef DGtal::ArithmeticalDSL<DGtal::BigInteger, DGtal::BigInteger, 4> DSL;
469 // res = res && rangeTest( DSL(5, 8, 123654) );
470 // }
471
472 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
473 trace.endBlock();
474 return res ? 0 : 1;
475}
476// //
Aim: This class represents a naive (resp. standard) digital straight line (DSL), ie....
Aim: This class is an alias of ArithmeticalDSS for standard DSL. It represents a standard digital str...
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
STL namespace.
Aim: Defines a predicate on a point.
Go to http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/RandomAccessTraversal....
Go to http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/ReadableIterator.html.
int main()
Definition testBits.cpp:56
MyPointD Point