34 #include "DGtal/base/Common.h"
35 #include "DGtal/kernel/CPointPredicate.h"
36 #include "DGtal/arithmetic/CPositiveIrreducibleFraction.h"
37 #include "DGtal/arithmetic/IntegerComputer.h"
38 #include "DGtal/arithmetic/LightSternBrocot.h"
39 #include "DGtal/arithmetic/Pattern.h"
40 #include "DGtal/arithmetic/StandardDSLQ0.h"
41 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
45 using namespace DGtal;
51 template <
typename Quotient>
53 equalCFrac(
const std::vector<Quotient> & c1,
const std::vector<Quotient> & c2 )
55 unsigned int s = c1.size() < c2.size() ? c1.size() : c2.size();
60 for (
unsigned int i = 0; i < s; ++i )
62 Quotient q1 = c1[ i ];
63 if ( ( s != c1.size() ) && ( i == s - 1 ) ) q1 += c1.back();
64 Quotient q2 = c2[ i ];
65 if ( ( s != c2.size() ) && ( i == s - 1 ) ) q2 += c2.back();
66 if ( q1 != q2 )
return false;
71 template <
typename SB>
72 bool testReducedFraction()
74 typedef typename SB::Integer
Integer;
75 typedef typename SB::Quotient Quotient;
76 typedef typename SB::Fraction Fraction;
77 unsigned int nbok = 0;
79 Integer p = random() / 10000;
80 Integer q = random() / 10000;
83 Integer g = ic.
gcd( p, q );
89 std::vector<Quotient> cf1;
91 Fraction f1 = SB::fraction( p, q );
92 std::vector<Quotient> cf1_bis;
93 f1.getCFrac( cf1_bis );
94 bool ok = equalCFrac<Quotient>( cf1, cf1_bis );
95 trace.
info() <<
" - p / q = " << p <<
" / " << q << std::endl;
99 ++nb, nbok += ok ? 1 : 0;
100 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
103 unsigned int depth = cf1.size();
104 for (
unsigned int k = 1; k < depth; ++k )
106 std::vector<Quotient> cf1_red;
107 Fraction fr = f1.reduced( k );
108 fr.getCFrac( cf1_red );
109 cf1.resize( depth - k );
110 ok = equalCFrac<Quotient>( cf1, cf1_red );
111 ++nb, nbok += ok ? 1 : 0;
112 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
113 <<
"reduced(" << k <<
")=";
115 std::cerr << std::endl;
123 template <
typename SB>
124 bool testInitFraction()
126 typedef typename SB::Integer Integer;
127 typedef typename SB::Fraction Fraction;
128 unsigned int nbok = 0;
130 Integer p = random() / 10000;
131 Integer q = random() / 10000;
134 Integer g = ic.
gcd( p, q );
137 Fraction f1 = SB::fraction( p, q );
138 trace.
info() <<
"p / q = " << p <<
" / " << q << std::endl;
142 nbok += ( ( p == f1.p() ) && ( q == f1.q() ) ) ? 1 : 0;
144 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
145 <<
"( ( p == f1.p() ) && ( q == f1.q() ) )"
147 trace.
info() <<
"- nbFractions = " << SB::instance().nbFractions << std::endl;
153 template <
typename SB>
156 typedef typename SB::Integer Integer;
157 typedef typename SB::Quotient Quotient;
158 typedef typename SB::Fraction Fraction;
160 typedef typename MyPattern::Vector2I Vector2I;
161 unsigned int nbok = 0;
163 Integer p = random() / 10000;
164 Integer q = random() / 10000;
165 MyPattern pattern( p*6, q*6 );
169 trace.
beginBlock (
"Testing block: Smallest covering subpatterns of ODD pattern." );
170 MyPattern pat_odd( 5, 12 );
171 trace.
info() <<
"ODD " << pat_odd <<
" " << pat_odd.rE() << endl;
177 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
179 trace.
info() <<
"sub(0,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
180 ++nb, nbok += sp.slope() == SB::fraction( 5, 12 ) ? 1 : 0;
181 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
183 trace.
info() <<
"sub(1,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
184 ++nb, nbok += sp.slope() == SB::fraction( 5, 12 ) ? 1 : 0;
185 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
187 trace.
info() <<
"sub(7,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
188 ++nb, nbok += sp.slope() == SB::fraction( 3, 7 ) ? 1 : 0;
189 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
191 trace.
info() <<
"sub(8,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
192 ++nb, nbok += sp.slope() == SB::fraction( 3, 7 ) ? 1 : 0;
193 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
195 trace.
info() <<
"sub(13,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
196 ++nb, nbok += sp.slope() == SB::fraction( 3, 7 ) ? 1 : 0;
197 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
199 trace.
info() <<
"sub(14,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
200 ++nb, nbok += sp.slope() == SB::fraction( 1, 2 ) ? 1 : 0;
201 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
203 trace.
info() <<
"sub(15,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
204 ++nb, nbok += sp.slope() == SB::fraction( 1, 2 ) ? 1 : 0;
206 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering left Subpatterns." << endl;
209 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
211 trace.
info() <<
"sub(0,16) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
212 ++nb, nbok += sp.slope() == SB::fraction( 5, 12 ) ? 1 : 0;
213 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
215 trace.
info() <<
"sub(0,15) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
216 ++nb, nbok += sp.slope() == SB::fraction( 5, 12 ) ? 1 : 0;
217 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
219 trace.
info() <<
"sub(0,14) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
220 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 2 ? 1 : 0;
221 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
223 trace.
info() <<
"sub(0,8) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
224 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 2 ? 1 : 0;
225 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
227 trace.
info() <<
"sub(0,7) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
228 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
229 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
231 trace.
info() <<
"sub(0,1) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
232 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
234 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering right Subpatterns." << endl;
237 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
239 trace.
info() <<
"sub(1,16) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
240 ++nb, nbok += sp.slope() == SB::fraction( 5, 12 ) ? 1 : 0;
241 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
243 trace.
info() <<
"sub(2,14) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
244 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 2 ? 1 : 0;
245 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
247 trace.
info() <<
"sub(7,15) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
248 ++nb, nbok += sp.slope() == SB::fraction( 3, 7 ) && np == 1 ? 1 : 0;
249 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
251 trace.
info() <<
"sub(7,14) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
252 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
253 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
255 trace.
info() <<
"sub(3,6) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
256 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
257 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
259 trace.
info() <<
"sub(6,8) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
260 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 2 ? 1 : 0;
261 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
263 trace.
info() <<
"sub(8,12) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
264 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
265 pat_odd.getSmallestCoveringSubpattern( sp, np, start,
267 trace.
info() <<
"sub(15,16) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
268 ++nb, nbok += sp.slope() == SB::fraction( 1, 2 ) && np == 1 ? 1 : 0;
270 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering middle Subpatterns." << endl;
274 trace.
beginBlock (
"Testing block: Smallest covering subpatterns of EVEN pattern." );
275 MyPattern pat_even( 12, 17 );
276 trace.
info() <<
"EVEN " << pat_even <<
" " << pat_even.rE() << endl;
279 pat_even.getSmallestCoveringSubpattern( sp, np, start,
281 trace.
info() <<
"sub(0,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
282 ++nb, nbok += sp.slope() == SB::fraction( 12, 17 ) ? 1 : 0;
283 pat_even.getSmallestCoveringSubpattern( sp, np, start,
285 trace.
info() <<
"sub(0,25) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
286 ++nb, nbok += sp.slope() == SB::fraction( 12, 17 ) ? 1 : 0;
287 pat_even.getSmallestCoveringSubpattern( sp, np, start,
289 trace.
info() <<
"sub(0,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
290 ++nb, nbok += sp.slope() == SB::fraction( 7, 10 ) ? 1 : 0;
291 pat_even.getSmallestCoveringSubpattern( sp, np, start,
293 trace.
info() <<
"sub(0,6) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
294 ++nb, nbok += sp.slope() == SB::fraction( 7, 10 ) ? 1 : 0;
295 pat_even.getSmallestCoveringSubpattern( sp, np, start,
297 trace.
info() <<
"sub(0,5) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
298 ++nb, nbok += sp.slope() == SB::fraction( 2, 3 ) ? 1 : 0;
299 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering left Subpatterns." << endl;
302 pat_even.getSmallestCoveringSubpattern( sp, np, start,
304 trace.
info() <<
"sub(4,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
305 ++nb, nbok += sp.slope() == SB::fraction( 12, 17 ) ? 1 : 0;
306 pat_even.getSmallestCoveringSubpattern( sp, np, start,
308 trace.
info() <<
"sub(5,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
309 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 2 ? 1 : 0;
310 pat_even.getSmallestCoveringSubpattern( sp, np, start,
312 trace.
info() <<
"sub(16,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
313 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 2 ? 1 : 0;
314 pat_even.getSmallestCoveringSubpattern( sp, np, start,
316 trace.
info() <<
"sub(17,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
317 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
318 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering right Subpatterns." << endl;
321 pat_even.getSmallestCoveringSubpattern( sp, np, start,
323 trace.
info() <<
"sub(1,27) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
324 ++nb, nbok += sp.slope() == SB::fraction( 12, 17 ) ? 1 : 0;
325 pat_even.getSmallestCoveringSubpattern( sp, np, start,
327 trace.
info() <<
"sub(5,24) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
328 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 2 ? 1 : 0;
329 pat_even.getSmallestCoveringSubpattern( sp, np, start,
331 trace.
info() <<
"sub(4,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
332 ++nb, nbok += sp.slope() == SB::fraction( 7, 10 ) && np == 1 ? 1 : 0;
333 pat_even.getSmallestCoveringSubpattern( sp, np, start,
335 trace.
info() <<
"sub(5,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
336 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
337 pat_even.getSmallestCoveringSubpattern( sp, np, start,
339 trace.
info() <<
"sub(7,12) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
340 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
341 pat_even.getSmallestCoveringSubpattern( sp, np, start,
343 trace.
info() <<
"sub(1,4) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
344 ++nb, nbok += sp.slope() == SB::fraction( 2, 3 ) && np == 1 ? 1 : 0;
345 pat_even.getSmallestCoveringSubpattern( sp, np, start,
347 trace.
info() <<
"sub(18,20) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
348 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
349 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering middle Subpatterns." << endl;
355 trace.
beginBlock (
"Testing block: greatest included subpatterns of ODD pattern." );
356 trace.
info() <<
"ODD " << pat_odd <<
" " << pat_odd.rE() << endl;
359 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
361 trace.
info() <<
"sub(0,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
362 ++nb, nbok += sp.slope() == SB::fraction( 5, 12 ) ? 1 : 0;
363 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
365 trace.
info() <<
"sub(1,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
366 ++nb, nbok += sp.slope() == SB::fraction( 3, 7 ) ? 1 : 0;
367 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
369 trace.
info() <<
"sub(7,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
370 ++nb, nbok += sp.slope() == SB::fraction( 3, 7 ) ? 1 : 0;
371 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
373 trace.
info() <<
"sub(8,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
374 ++nb, nbok += sp.slope() == SB::fraction( 1, 2 ) ? 1 : 0;
375 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
377 trace.
info() <<
"sub(13,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
378 ++nb, nbok += sp.slope() == SB::fraction( 1, 2 ) ? 1 : 0;
379 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
381 trace.
info() <<
"sub(14,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
382 ++nb, nbok += sp.slope() == SB::fraction( 1, 2 ) ? 1 : 0;
383 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
385 trace.
info() <<
"sub(15,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
386 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
388 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering left Subpatterns." << endl;
391 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
393 trace.
info() <<
"sub(0,15) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
394 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 2 ? 1 : 0;
395 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
397 trace.
info() <<
"sub(0,14) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
398 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 2 ? 1 : 0;
399 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
401 trace.
info() <<
"sub(0,13) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
402 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
403 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
405 trace.
info() <<
"sub(0,7) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
406 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
407 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
409 trace.
info() <<
"sub(0,6) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
410 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
411 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
413 trace.
info() <<
"sub(0,1) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
414 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
416 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering right Subpatterns." << endl;
419 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
421 trace.
info() <<
"sub(1,16) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
422 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) ? 1 : 0;
423 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
425 trace.
info() <<
"sub(2,14) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
426 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
427 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
429 trace.
info() <<
"sub(7,15) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
430 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
431 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
433 trace.
info() <<
"sub(7,14) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
434 ++nb, nbok += sp.slope() == SB::fraction( 2, 5 ) && np == 1 ? 1 : 0;
435 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
437 trace.
info() <<
"sub(3,6) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
438 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
439 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
441 trace.
info() <<
"sub(6,8) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
442 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
443 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
445 trace.
info() <<
"sub(8,12) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
446 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
447 pat_odd.getGreatestIncludedSubpattern( sp, np, start,
449 trace.
info() <<
"sub(15,16) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
450 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
452 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering middle Subpatterns." << endl;
456 trace.
beginBlock (
"Testing block: Greatest included subpatterns of EVEN pattern." );
457 trace.
info() <<
"EVEN " << pat_even <<
" " << pat_even.rE() << endl;
460 pat_even.getGreatestIncludedSubpattern( sp, np, start,
462 trace.
info() <<
"sub(0,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
463 ++nb, nbok += sp.slope() == SB::fraction( 12, 17 ) ? 1 : 0;
464 pat_even.getGreatestIncludedSubpattern( sp, np, start,
466 trace.
info() <<
"sub(0,25) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
467 ++nb, nbok += sp.slope() == SB::fraction( 7, 10 ) ? 1 : 0;
468 pat_even.getGreatestIncludedSubpattern( sp, np, start,
470 trace.
info() <<
"sub(0,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
471 ++nb, nbok += sp.slope() == SB::fraction( 7, 10 ) ? 1 : 0;
472 pat_even.getGreatestIncludedSubpattern( sp, np, start,
474 trace.
info() <<
"sub(0,16) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
475 ++nb, nbok += sp.slope() == SB::fraction( 2, 3 ) ? 1 : 0;
476 pat_even.getGreatestIncludedSubpattern( sp, np, start,
478 trace.
info() <<
"sub(0,6) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
479 ++nb, nbok += sp.slope() == SB::fraction( 2, 3 ) ? 1 : 0;
480 pat_even.getGreatestIncludedSubpattern( sp, np, start,
482 trace.
info() <<
"sub(0,5) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
483 ++nb, nbok += sp.slope() == SB::fraction( 2, 3 ) ? 1 : 0;
484 pat_even.getGreatestIncludedSubpattern( sp, np, start,
486 trace.
info() <<
"sub(0,4) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
487 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
488 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering left Subpatterns." << endl;
491 pat_even.getGreatestIncludedSubpattern( sp, np, start,
493 trace.
info() <<
"sub(4,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
494 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 2 ? 1 : 0;
495 pat_even.getGreatestIncludedSubpattern( sp, np, start,
497 trace.
info() <<
"sub(5,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
498 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 2 ? 1 : 0;
499 pat_even.getGreatestIncludedSubpattern( sp, np, start,
501 trace.
info() <<
"sub(16,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
502 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
503 pat_even.getGreatestIncludedSubpattern( sp, np, start,
505 trace.
info() <<
"sub(17,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
506 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
507 pat_even.getGreatestIncludedSubpattern( sp, np, start,
509 trace.
info() <<
"sub(18,29) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
510 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
511 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering right Subpatterns." << endl;
514 pat_even.getGreatestIncludedSubpattern( sp, np, start,
516 trace.
info() <<
"sub(1,27) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
517 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
518 pat_even.getGreatestIncludedSubpattern( sp, np, start,
520 trace.
info() <<
"sub(5,24) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
521 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
522 pat_even.getGreatestIncludedSubpattern( sp, np, start,
524 trace.
info() <<
"sub(4,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
525 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
526 pat_even.getGreatestIncludedSubpattern( sp, np, start,
528 trace.
info() <<
"sub(5,17) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
529 ++nb, nbok += sp.slope() == SB::fraction( 5, 7 ) && np == 1 ? 1 : 0;
530 pat_even.getGreatestIncludedSubpattern( sp, np, start,
532 trace.
info() <<
"sub(5,16) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
533 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
534 pat_even.getGreatestIncludedSubpattern( sp, np, start,
536 trace.
info() <<
"sub(1,4) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
537 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
538 pat_even.getGreatestIncludedSubpattern( sp, np, start,
540 trace.
info() <<
"sub(18,20) = " << sp <<
" " << sp.rE() <<
"^" << np << endl;
541 ++nb, nbok += sp.slope() == Fraction() ? 1 : 0;
542 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") covering middle Subpatterns." << endl;
545 trace.
info() <<
"Odd pattern " << pat_odd << endl;
546 trace.
info() <<
" U(0)=" << pat_odd.U( 0 )
547 <<
" L(0)=" << pat_odd.L( 0 )
548 <<
" U(1)=" << pat_odd.U( 1 )
549 <<
" L(1)=" << pat_odd.L( 1 ) << endl;
551 trace.
info() <<
"Even pattern " << pat_even << endl;
552 trace.
info() <<
" U(0)=" << pat_even.U( 0 )
553 <<
" L(0)=" << pat_even.L( 0 )
554 <<
" U(1)=" << pat_even.U( 1 )
555 <<
" L(1)=" << pat_even.L( 1 ) << endl;
560 template <
typename Fraction>
561 bool testStandardDSLQ0()
564 typedef typename Fraction::Integer Integer;
565 typedef typename Fraction::Quotient Quotient;
566 typedef typename DSL::Point
Point;
567 typedef typename DSL::Point2I Point2I;
568 typedef typename DSL::Vector2I Vector2I;
571 unsigned int nbok = 0;
574 for ( Integer mu = -5; mu < 30; ++mu )
580 trace.
info() <<
"- U=" << U <<
" r(U)=" << D1.r( U )
581 <<
", L=" << L <<
" r(L)=" << D1.r( L ) << endl;
582 ++nb, nbok += D1.r( U ) == D1.mu() ? 1 : 0;
583 ++nb, nbok += D1.r( L ) == D1.mup() ? 1 : 0;
587 for ( Integer x = -5; x < 30; ++x )
589 Point P = D2.lowestY( x );
590 ++nb, nbok += D2( P ) && ( ! D2( P - Vector2I(0,1) ) ) ? 1 : 0;
591 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
592 <<
"D2(P) && ! D2(P-y) P=" << P <<
" r(P)=" << D2.r( P )
594 P = D2.uppermostY( x );
595 ++nb, nbok += D2( P ) && ( ! D2( P + Vector2I(0,1) ) ) ? 1 : 0;
596 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
597 <<
"D2(P) && ! D2(P+y) P=" << P <<
" r(P)=" << D2.r( P )
600 for ( Integer y = -5; y < 30; ++y )
602 Point P = D2.lowestX( y );
603 ++nb, nbok += D2( P ) && ( ! D2( P - Vector2I(1,0) ) ) ? 1 : 0;
604 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
605 <<
"D2(P) && ! D2(P-x) P=" << P <<
" r(P)=" << D2.r( P )
607 P = D2.uppermostX( y );
608 ++nb, nbok += D2( P ) && ( ! D2( P + Vector2I(1,0) ) ) ? 1 : 0;
609 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
610 <<
"D2(P) && ! D2(P+x) P=" << P <<
" r(P)=" << D2.r( P )
617 template <
typename DSL>
618 bool checkSubStandardDSLQ0(
const DSL & D,
619 const typename DSL::Point & A,
620 const typename DSL::Point & B )
622 typedef typename DSL::Fraction Fraction;
623 typedef typename DSL::Integer Integer;
624 typedef typename DSL::Quotient Quotient;
625 typedef typename DSL::Point Point;
626 typedef typename DSL::ConstIterator ConstIterator;
627 typedef typename DSL::Point2I Point2I;
628 typedef typename DSL::Vector2I Vector2I;
631 DSL S = D.reversedSmartDSS( A, B );
632 ConstIterator it = D.
begin( A );
633 ConstIterator it_end = D.end( B );
636 while ( ( dss.end() != it_end )
637 && ( dss.extendForward() ) ) {}
638 bool ok = S.a() == dss.getA()
639 && S.b() == dss.getB()
640 && S.mu() == dss.getMu();
643 trace.
info() <<
"-------------------------------------------------------"
646 <<
" " << D.pattern().rE() << endl;
647 trace.
info() <<
"S(" << A <<
"," << B <<
") = "
648 << S <<
" " << S.pattern() << endl;
649 trace.
info() <<
"ArithDSS = " << dss << std::endl;
659 template <
typename Fraction>
660 bool testSubStandardDSLQ0()
663 typedef typename Fraction::Integer Integer;
664 typedef typename Fraction::Quotient Quotient;
665 typedef typename DSL::Point Point;
666 typedef typename DSL::ConstIterator ConstIterator;
667 typedef typename DSL::Point2I Point2I;
668 typedef typename DSL::Vector2I Vector2I;
671 unsigned int nbok = 0;
675 for (
unsigned int i = 0; i < 100; ++i )
677 Integer a( random() % 12000 + 1 );
678 Integer b( random() % 12000 + 1 );
679 if ( ic.
gcd( a, b ) == 1 )
682 <<
" Test DSL has slope " << a <<
"/" << b << std::endl;
683 for ( Integer mu = 0; mu < 5; ++mu )
685 DSL D( a, b, random() % 10000 );
686 for ( Integer x = 0; x < 10; ++x )
688 Integer x1 = random() % 1000;
689 Integer x2 = x1 + 1 + ( random() % 1000 );
690 Point A = D.lowestY( x1 );
691 Point B = D.lowestY( x2 );
692 ++nb, nbok += checkSubStandardDSLQ0<DSL>( D, A, B ) ? 1 : 0;
694 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") correct reversedSmartDSS."
696 if ( nbok != nb ) assert(
false);
701 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") correct reversedSmartDSS."
711 bool testLightSternBrocot()
713 unsigned int nbtests = 10;
714 unsigned int nbok = 0;
716 typedef DGtal::BigInteger Integer;
718 typedef SB::Fraction Fraction;
721 for (
unsigned int i = 0; i < nbtests; ++i )
723 nbok += testInitFraction<SB>() ? 1 : 0;
726 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") init fractions." << endl;
730 for (
unsigned int i = 0; i < nbtests; ++i )
732 nbok += testReducedFraction<SB>() ? 1 : 0;
735 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") reduced fractions." << endl;
739 trace.
info() <<
"- nbFractions = " << SB::instance().nbFractions << endl;
751 template <
typename SB>
755 typedef typename SB::Fraction Fraction;
756 typedef typename Fraction::Integer Integer;
758 typedef typename DSL::Point Point;
761 DSL D(1077,1495,6081);
766 ASSERT( D( A ) &&
"Point A belongs to D." );
767 ASSERT( D( B ) &&
"Point A belongs to D." );
768 DSL D1 = D.reversedSmartDSS(A,B);
769 std::cerr << D1 << std::endl;
770 return D1.slope() == Fraction( 1, 1 );
773 int main(
int ,
char** )
776 typedef SB::Fraction Fraction;
777 typedef Fraction::ConstIterator ConstIterator;
780 BOOST_CONCEPT_ASSERT(( boost::InputIterator< ConstIterator > ));
783 bool res = testLightSternBrocot()
785 && testSubStandardDSLQ0<Fraction>()
786 && testAncestors<SB>();
787 trace.
emphase() << ( res ?
"Passed." :
"Error." ) << endl;