DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testMaximalSegments.cpp
1 
16 //LICENSE-END
34 #include <cstdio>
35 #include <cmath>
36 #include <fstream>
37 #include <vector>
38 #include <iostream>
39 #include <iterator>
40 
41 
42 
43 
44 #include "DGtal/base/Common.h"
45 #include "DGtal/base/Exceptions.h"
46 #include "DGtal/io/boards/Board2D.h"
47 #include "DGtal/io/Color.h"
48 
49 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
50 #include "DGtal/geometry/curves/FreemanChain.h"
51 #include "DGtal/geometry/curves/MaximalSegments.h"
52 
53 
54 #include "ConfigTest.h"
55 
56 
57 using namespace DGtal;
58 using namespace DGtal::deprecated;
59 using namespace std;
60 using namespace LibBoard;
61 
63 // Functions for testing class MaximalSegments.
65 
70 bool testCover4()
71 {
72 
73  typedef int Coordinate;
75  typedef FreemanChain<Coordinate> ContourType;
76 
78 
79  typedef MaximalSegments<PrimitiveType> DecompositionType;
80 
81  std::string filename = testPath + "samples/france.fc";
82  std::cout << filename << std::endl;
83 
84  std::fstream fst;
85  fst.open (filename.c_str(), std::ios::in);
86  ContourType theContour(fst);
87 
88  //Segmentation
89  trace.beginBlock("Tangential cover of 4-connected digital curves");
90  PrimitiveType primitive;
91  DecompositionType theDecomposition(theContour.begin(), theContour.end(), primitive, false);
92 
93  // Draw the grid
94  Board2D aBoard;
95  aBoard.setUnit(Board::UCentimeter);
96 
97  aBoard << SetMode("PointVector", "Grid")
98  << theContour;
99 
100  //for each segment
101  unsigned int compteur = 0;
102  DecompositionType::SegmentIterator i = theDecomposition.begin();
103  for ( ; i != theDecomposition.end(); ++i) {
104 
105  compteur++;
106  PrimitiveType segment(*i);
107  trace.info() << segment << std::endl; //standard output
108  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" )
109  << segment; // draw each segment
110 
111  }
112 
113  aBoard.saveEPS("segmentationDSS4.eps");
114 
115 trace.info() << "# segments" << compteur << std::endl;
116 
117  trace.endBlock();
118  return true;
119 }
120 
121 bool testDisconnectedCurve()
122 {
123  typedef int Coordinate;
124  typedef PointVector<2,Coordinate> Point;
125  typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,4> PrimitiveType;
126 
127  typedef MaximalSegments<PrimitiveType> DecompositionType;
128 
129  std::vector<Point> curve;
130  curve.push_back(Point(0,0));
131  curve.push_back(Point(1,0));
132  curve.push_back(Point(1,1));
133  curve.push_back(Point(2,1));
134  curve.push_back(Point(3,2));
135  curve.push_back(Point(4,2));
136  curve.push_back(Point(5,2));
137  curve.push_back(Point(6,2));
138  curve.push_back(Point(6,3));
139  curve.push_back(Point(6,4));
140  curve.push_back(Point(7,4));
141  curve.push_back(Point(8,4));
142  curve.push_back(Point(9,3));
143  curve.push_back(Point(9,2));
144  curve.push_back(Point(10,2));
145  curve.push_back(Point(11,2));
146 
147  //Segmentation
148  trace.beginBlock("Tangential cover of disconnected digital curves");
149  PrimitiveType primitive;
150  DecompositionType theDecomposition(curve.begin(), curve.end(), primitive, false);
151 
152  // Draw the pixels
153  Board2D aBoard;
154  aBoard.setUnit(Board::UCentimeter);
155  aBoard << SetMode("PointVector", "Grid");
156  for (std::vector<Point>::iterator it = curve.begin(); it != curve.end(); ++it) {
157  aBoard << (*it);
158  }
159 
160 
161  //for each segment
162  unsigned int compteur = 0;
163  DecompositionType::SegmentIterator i = theDecomposition.begin();
164  for ( ; i != theDecomposition.end(); ++i) {
165 
166  compteur++;
167  trace.info() << "Segment " << compteur << std::endl;
168  PrimitiveType segment(*i);
169  trace.info() << segment << std::endl; //standard output
170  aBoard << SetMode( "ArithmeticalDSS", "Points" )
171  << segment; // draw each segment
172  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" )
173  << segment; // draw each segment
174  }
175 
176  aBoard.saveSVG("specialCase.svg");
177 
178  trace.endBlock();
179 
180  return (compteur==5);
181 
182 }
183 
188 bool testClosedCurves(const bool& aFlag)
189 {
190 
191  trace.beginBlock ( "Test for closed curves" );
192 
193  typedef FreemanChain<int> Contour4;
195  typedef MaximalSegments<DSS4> Decomposition4;
196 
197  // A Freeman chain code is a string composed by the coordinates of the first pixel, and the list of elementary displacements.
198  std::stringstream ss(stringstream::in | stringstream::out);
199  ss << "31 16 11121212121212212121212212122122222322323233323333333323333323303330330030300000100010010010001000101010101111" << endl;
200 
201  // Construct the Freeman chain
202  Contour4 theContour( ss );
203 
204  //Segmentation
205  DSS4 dss;
206  Decomposition4 theDecomposition( theContour.begin(),theContour.end(),dss,aFlag );
207 
208  Board2D aBoard;
209  aBoard << SetMode( "PointVector", "Grid" )
210  << theContour;
211  //for each segment
212  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" );
213  string className = "ArithmeticalDSS/BoundingBox";
214  for ( Decomposition4::SegmentIterator i = theDecomposition.begin();
215  i != theDecomposition.end(); ++i )
216  {
217 
218  DSS4 segment(*i);
219  cout << segment << endl;
220  aBoard << CustomStyle( className,
221  new CustomPenColor( Color::Blue ) )
222  << segment; // draw each segment
223 
224  }
225  std::string filename = "testClosedCurves";
226  if (aFlag) filename += "ProcessedAsClosed";
227  else filename += "ProcessedAsOpen";
228  filename += ".svg";
229  aBoard.saveSVG(filename.c_str());
230 
231  trace.endBlock();
232 
233  return true;
234 }
235 
236 
241 bool testNoPoint()
242 {
243  typedef int Coordinate;
244  typedef PointVector<2,Coordinate> Point;
245  typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,4> PrimitiveType;
246  typedef MaximalSegments<PrimitiveType> DecompositionType;
247 
248 
249  std::vector<Point> curve;
250  try {
251 
252  trace.beginBlock("Digital curve having no point");
253  PrimitiveType primitive;
254  DecompositionType theDecomposition(curve.begin(), curve.end(), primitive, false);
255 
256  for ( DecompositionType::SegmentIterator i = theDecomposition.begin();
257  i != theDecomposition.end(); ++i )
258  { }
259  trace.endBlock();
260 
261  return true;
262  } catch (std::exception e) {
263  return false;
264  }
265 
266 
267 }
268 
273 bool testOnePoint()
274 {
275  typedef int Coordinate;
276  typedef PointVector<2,Coordinate> Point;
277  typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,4> PrimitiveType;
278  typedef MaximalSegments<PrimitiveType> DecompositionType;
279 
280 
281  std::vector<Point> curve;
282  curve.push_back(Point(5,5));
283  try {
284  PrimitiveType primitive;
285  trace.beginBlock("Digital curve having one point");
286  DecompositionType theDecomposition(curve.begin(), curve.end(), primitive, false);
287 
288  Board2D aBoard;
289  aBoard << curve.at(0);
290  //for each segment
291  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" );
292  for ( DecompositionType::SegmentIterator i = theDecomposition.begin();
293  i != theDecomposition.end(); ++i )
294  {
295  PrimitiveType primitive2(*i);
296  trace.info() << primitive2 << endl;
297  aBoard << primitive2;
298 
299  }
300  aBoard.saveSVG("testOnePoint.svg");
301  trace.endBlock();
302 
303  return true;
304  } catch (std::exception e) {
305  return false;
306  }
307 
308 
309 }
314 bool testTwoEndIterators()
315 {
316  typedef int Coordinate;
317  typedef PointVector<2,Coordinate> Point;
318  typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,4> PrimitiveType;
319  typedef MaximalSegments<PrimitiveType> DecompositionType;
320 
321  std::vector<Point> curve;
322  curve.push_back(Point(5,5));
323 
324  try {
325 
326  trace.beginBlock("Two end iterators");
327  PrimitiveType primitive;
328  DecompositionType theDecomposition(curve.begin(), curve.end(), primitive, false);
329 
330  for ( DecompositionType::SegmentIterator i = theDecomposition.begin();
331  i != theDecomposition.end(); ++i )
332  { }
333 
334  trace.endBlock();
335 
336  return true;
337  } catch (std::exception e) {
338  return false;
339  }
340 
341 
342 }
343 
349 bool testOneDSS()
350 {
351 
352  typedef int Coordinate;
353  typedef PointVector<2,Coordinate> Point;
354  typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,8> PrimitiveType;
355  typedef MaximalSegments<PrimitiveType> DecompositionType;
356 
357  std::vector<Point> curve;
358  curve.push_back(Point(0,0));
359  curve.push_back(Point(1,1));
360  curve.push_back(Point(2,1));
361  curve.push_back(Point(3,2));
362  curve.push_back(Point(4,2));
363  curve.push_back(Point(5,2));
364  curve.push_back(Point(6,3));
365  curve.push_back(Point(7,3));
366 
367  //Segmentation
368  trace.beginBlock("Segmentation of one DSS");
369  PrimitiveType primitive;
370  DecompositionType theDecomposition(curve.begin(), curve.end(), primitive, false);
371 
372  // Draw the pixels
373  Board2D aBoard;
374  aBoard.setUnit(Board::UCentimeter);
375  aBoard << SetMode("PointVector", "Both");
376  for (std::vector<Point>::iterator it = curve.begin(); it != curve.end(); ++it) {
377  aBoard << (*it);
378  }
379 
380  //for each segment
381  unsigned int compteur = 0;
382  DecompositionType::SegmentIterator i = theDecomposition.begin();
383  for ( ; i != theDecomposition.end(); ++i) {
384 
385  ++compteur;
386  PrimitiveType segment(*i);
387  trace.info() << segment << std::endl; //standard output
388  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" )
389  << segment; // draw each segment
390  }
391 
392  aBoard.saveSVG("oneDSS.svg");
393 
394  trace.endBlock();
395 
396  return (compteur==1);
397 }
398 
402 
403 int main(int argc, char **argv)
404 {
405 
406  trace.beginBlock ( "Testing class MaximalSegments" );
407  trace.info() << "Args:";
408  for ( int i = 0; i < argc; ++i )
409  trace.info() << " " << argv[ i ];
410  trace.info() << endl;
411 
412  bool res = testCover4()
413  && testDisconnectedCurve()
414  && testClosedCurves(true)
415  && testClosedCurves(false)
416  && testNoPoint()
417  && testOnePoint()
418  && testTwoEndIterators()
419  && testOneDSS()
420 ;
421  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
422  trace.endBlock();
423  return res ? 0 : 1;
424 
425 }