DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testArithDSS.cpp
1 
38 #include <iostream>
39 #include <iterator>
40 #include <cstdio>
41 #include <cmath>
42 #include <fstream>
43 #include <vector>
44 
45 #include "DGtal/base/Common.h"
46 #include "DGtal/base/Exceptions.h"
47 #include "DGtal/kernel/SpaceND.h"
48 #include "DGtal/kernel/domains/HyperRectDomain.h"
49 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
50 #include "DGtal/io/boards/Board2D.h"
51 
52 #include "DGtal/geometry/curves/CBidirectionalSegmentComputer.h"
53 #include "DGtal/io/boards/CDrawableWithBoard2D.h"
54 
55 using namespace DGtal;
56 using namespace std;
57 using namespace LibBoard;
58 
60 // Functions for testing class ArithmeticalDSS.
62 
66 bool testDSS4drawing()
67 {
68 
69  typedef PointVector<2,int> Point;
70  typedef std::vector<Point>::iterator Iterator;
71  typedef ArithmeticalDSS<Iterator,int,4> DSS4;
72 
73  std::vector<Point> contour;
74  contour.push_back(Point(0,0));
75  contour.push_back(Point(1,0));
76  contour.push_back(Point(1,1));
77  contour.push_back(Point(2,1));
78  contour.push_back(Point(2,1));
79  contour.push_back(Point(3,1));
80  contour.push_back(Point(3,2));
81  contour.push_back(Point(4,2));
82  contour.push_back(Point(5,2));
83  contour.push_back(Point(6,2));
84  contour.push_back(Point(6,3));
85  contour.push_back(Point(6,4));
86 
87 
88  // Adding step
89  trace.beginBlock("Add points while it is possible and draw the result");
90 
91  DSS4 theDSS4;
92  theDSS4.init( contour.begin() );
93  trace.info() << theDSS4 << " " << theDSS4.isValid() << std::endl;
94 
95  while ( (theDSS4.end() != contour.end())
96  &&(theDSS4.extendForward()) ) {}
97 
98  trace.info() << theDSS4 << " " << theDSS4.isValid() << std::endl;
99 
100  HyperRectDomain< SpaceND<2,int> > domain( Point(0,0), Point(10,10) );
101 
102  Board2D board;
103  board.setUnit(Board::UCentimeter);
104 
105  board << SetMode(domain.className(), "Grid")
106  << domain;
107  board << SetMode("PointVector", "Grid");
108 
109  board << SetMode(theDSS4.className(), "Points")
110  << theDSS4;
111  board << SetMode(theDSS4.className(), "BoundingBox")
112  << theDSS4;
113 
114  board.saveSVG("DSS4.svg");
115 
116 
117  trace.endBlock();
118 
119  return true;
120 }
121 
126 bool testDSS8drawing()
127 {
128 
129  typedef PointVector<2,int> Point;
130  typedef std::vector<Point>::iterator Iterator;
131  typedef ArithmeticalDSS<Iterator,int,8> DSS8;
132 
133  std::vector<Point> boundary;
134  boundary.push_back(Point(0,0));
135  boundary.push_back(Point(1,1));
136  boundary.push_back(Point(2,1));
137  boundary.push_back(Point(3,2));
138  boundary.push_back(Point(4,2));
139  boundary.push_back(Point(5,2));
140  boundary.push_back(Point(6,3));
141  boundary.push_back(Point(6,4));
142 
143  // Good Initialisation
144  trace.beginBlock("Add points while it is possible and draw the result");
145  DSS8 theDSS8;
146  theDSS8.init( boundary.begin() );
147 
148  trace.info() << theDSS8 << " " << theDSS8.isValid() << std::endl;
149 
150  {
151 
152  while ( (theDSS8.end()!=boundary.end())
153  &&(theDSS8.extendForward()) ) {}
154 
155  trace.info() << theDSS8 << " " << theDSS8.isValid() << std::endl;
156 
157 
158  HyperRectDomain< SpaceND<2,int> > domain( Point(0,0), Point(10,10) );
159 
160 
161  Board2D board;
162  board.setUnit(Board::UCentimeter);
163 
164 
165  board << SetMode(domain.className(), "Paving")
166  << domain;
167  board << SetMode("PointVector", "Both");
168 
169  board << SetMode(theDSS8.className(), "Points")
170  << theDSS8;
171  board << SetMode(theDSS8.className(), "BoundingBox")
172  << theDSS8;
173 
174 
175  board.saveSVG("DSS8.svg");
176 
177  }
178 
179  trace.endBlock();
180 
181  return true;
182 }
183 
188 bool testExtendretractForward()
189 {
190 
191 
192  typedef PointVector<2,int> Point;
193  typedef std::vector<Point>::iterator Iterator;
194  typedef ArithmeticalDSS<Iterator,int,4> DSS4;
195 
196 
197  std::vector<Point> contour;
198  contour.push_back(Point(0,0));
199  contour.push_back(Point(1,0));
200  contour.push_back(Point(1,1));
201  contour.push_back(Point(2,1));
202  contour.push_back(Point(3,1));
203  contour.push_back(Point(3,2));
204  contour.push_back(Point(4,2));
205  contour.push_back(Point(5,2));
206  contour.push_back(Point(6,2));
207  contour.push_back(Point(6,3));
208  contour.push_back(Point(6,4));
209 
210 
211  trace.beginBlock("Checking consistency between adding and removing");
212 
213  std::deque<DSS4 > v1,v2;
214  DSS4 newDSS4;
215  newDSS4.init(contour.begin());
216  v1.push_back(newDSS4);
217 
218  //forward scan and store each DSS4
219  trace.info() << "forward scan" << std::endl;
220 
221  while ( (newDSS4.end() != contour.end())
222  &&(newDSS4.extendForward()) ) {
223  v1.push_back(newDSS4);
224  }
225 
226  //backward scan
227  trace.info() << "backward scan" << std::endl;
228 
229  Iterator i(newDSS4.end());
230  i--;
231  DSS4 reverseDSS4;
232  reverseDSS4.init(i);
233 
234  while ( (reverseDSS4.begin()!=contour.begin())
235  &&(reverseDSS4.extendBackward()) ) {
236  }
237  reverseDSS4.extendBackward(contour.begin());
238 
239  trace.info() << "removing" << std::endl;
240 
241  //removing step, store each DSS4 for comparison
242  v2.push_front(reverseDSS4);
243  while (reverseDSS4.retractBackward()) {
244  v2.push_front(reverseDSS4);
245  }
246 
247 
248  //comparison
249  trace.info() << "comparison" << std::endl;
250  trace.info() << v1.size() << " == " << v2.size() << std::endl;
251  ASSERT(v1.size() == v2.size());
252 
253  bool isOk = true;
254  for (unsigned int k = 0; k < v1.size(); k++) {
255  if (v1.at(k) != v2.at(k)) isOk = false;
256  trace.info() << "DSS4 :" << k << std::endl;
257 
258  trace.info() << v1.at(k) << v2.at(k) << std::endl;
259  }
260 
261 
262  if (isOk) trace.info() << "ok for the " << v1.size() << " DSS4" << std::endl;
263  else trace.info() << "failure" << std::endl;
264 
265  trace.endBlock();
266 
267  return isOk;
268 }
269 
270 
271 #ifdef WITH_BIGINTEGER
272 
276 bool testBIGINTEGER()
277 {
278  bool flag = false;
279 
280 
281  typedef DGtal::BigInteger Coordinate;
282  typedef PointVector<2,Coordinate> Point;
283  typedef std::vector<Point>::iterator Iterator;
285 
286 
287 
288  trace.beginBlock("Add some points of big coordinates");
289 
290  std::vector<Point> contour;
291  contour.push_back(Point(1000000000,1000000000));
292  contour.push_back(Point(1000000001,1000000000));
293  contour.push_back(Point(1000000002,1000000000));
294  contour.push_back(Point(1000000003,1000000000));
295  contour.push_back(Point(1000000003,1000000001));
296  contour.push_back(Point(1000000004,1000000001));
297  contour.push_back(Point(1000000005,1000000001));
298  contour.push_back(Point(1000000005,1000000002));
299 
300  DSS4 theDSS4;
301  theDSS4.init( contour.begin() );
302  while ( (theDSS4.end() != contour.end())
303  &&(theDSS4.extendForward()) ) {}
304 
305  trace.info() << theDSS4 << " " << theDSS4.isValid() << std::endl;
306 
307  Coordinate mu;
308  mu = "-3000000000";
309  if( (theDSS4.getA() == 2)
310  &&(theDSS4.getB() == 5)
311  &&(theDSS4.getMu() == mu)
312  &&(theDSS4.getOmega() == 7) ) {
313  flag = true;
314  } else {
315  flag = false;
316  }
317 
318  trace.endBlock();
319 
320  return flag;
321 }
322 
323 #endif
324 
330 bool testCorner()
331 {
332 
333  typedef PointVector<2,int> Point;
334  typedef std::vector<Point>::iterator Iterator;
335  typedef ArithmeticalDSS<Iterator,int,8> DSS8;
336 
337  std::vector<Point> boundary;
338  boundary.push_back(Point(10,10));
339  boundary.push_back(Point(10,11));
340  boundary.push_back(Point(11,11));
341 
342 
343  DSS8 theDSS8;
344  theDSS8.init(boundary.begin());
345  theDSS8.extendForward();
346  return ( !theDSS8.extendForward() );
347 
348 }
349 
350 
351 
352 void testArithDSSConceptChecking()
353 {
354  typedef PointVector<2,int> Point;
355  typedef std::vector<Point>::iterator Iterator;
356  typedef ArithmeticalDSS<Iterator,int,8> ArithDSS;
357  BOOST_CONCEPT_ASSERT(( CDrawableWithBoard2D<ArithDSS> ));
358  BOOST_CONCEPT_ASSERT(( CBidirectionalSegmentComputer<ArithDSS> ));
359 }
360 
361 
362 int main(int argc, char **argv)
363 {
364 
365  trace.beginBlock ( "Testing class ArithmeticalDSS" );
366  trace.info() << "Args:";
367  for ( int i = 0; i < argc; ++i )
368  trace.info() << " " << argv[ i ];
369  trace.info() << endl;
370 
371 
372  {//concept checking
373  testArithDSSConceptChecking();
374  }
375 
376  bool res = testDSS4drawing()
377  && testDSS8drawing()
378  && testExtendretractForward()
379  && testCorner()
380 #ifdef WITH_BIGINTEGER
381  && testBIGINTEGER()
382 #endif
383  ;
384  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
385  trace.endBlock();
386 
387  return res ? 0 : 1;
388 
389 }