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"
49 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
50 #include "DGtal/geometry/curves/FreemanChain.h"
51 #include "DGtal/geometry/curves/GreedyDecomposition.h"
54 #include "ConfigTest.h"
57 using namespace DGtal;
58 using namespace DGtal::deprecated;
60 using namespace LibBoard;
73 typedef int Coordinate;
82 std::string filename = testPath +
"samples/manche.fc";
83 std::cout << filename << std::endl;
86 fst.open (filename.c_str(), std::ios::in);
87 ContourType theContour(fst);
91 PrimitiveType computer;
92 DecompositionType theDecomposition(theContour.begin(), theContour.end(), computer,
false);
96 aBoard.
setUnit(Board::UCentimeter);
98 aBoard <<
SetMode(
"PointVector",
"Grid")
102 unsigned int compteur = 0;
103 DecompositionType::SegmentIterator i = theDecomposition.begin();
104 for ( ; i != theDecomposition.end(); ++i) {
107 PrimitiveType segment(*i);
109 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" )
114 aBoard.saveSVG(
"segmentationDSS4.svg");
117 return (compteur==91);
128 typedef int Coordinate;
130 typedef std::vector<Point> ContourType;
135 std::vector<Point> curve;
136 curve.push_back(Point(0,0));
137 curve.push_back(Point(1,1));
138 curve.push_back(Point(2,1));
139 curve.push_back(Point(3,2));
140 curve.push_back(Point(4,2));
141 curve.push_back(Point(5,2));
142 curve.push_back(Point(6,3));
143 curve.push_back(Point(6,4));
144 curve.push_back(Point(7,4));
145 curve.push_back(Point(8,4));
146 curve.push_back(Point(9,3));
147 curve.push_back(Point(10,2));
148 curve.push_back(Point(11,2));
151 trace.
beginBlock(
"Segmentation of a 8-connected digital curve into DSS");
152 PrimitiveType computer;
153 DecompositionType theDecomposition(curve.begin(), curve.end(), computer,
false);
157 aBoard.
setUnit(Board::UCentimeter);
158 aBoard <<
SetMode(
"PointVector",
"Both");
159 for (ContourType::iterator it = curve.begin(); it != curve.end(); ++it) {
165 unsigned int compteur = 0;
166 DecompositionType::SegmentIterator i = theDecomposition.begin();
167 for ( ; i != theDecomposition.end(); ++i) {
170 trace.
info() <<
"Segment " << compteur << std::endl;
171 PrimitiveType segment(*i);
173 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" )
177 aBoard.saveSVG(
"segmentationDSS8.svg");
181 return (compteur==4);
189 bool testDisconnectedCurve()
192 typedef int Coordinate;
194 typedef std::vector<Point> ContourType;
199 std::vector<Point> curve;
200 curve.push_back(Point(-1,-1));
201 curve.push_back(Point(0,0));
202 curve.push_back(Point(1,0));
203 curve.push_back(Point(1,1));
204 curve.push_back(Point(2,1));
205 curve.push_back(Point(3,2));
206 curve.push_back(Point(4,2));
207 curve.push_back(Point(5,2));
208 curve.push_back(Point(6,2));
209 curve.push_back(Point(6,3));
210 curve.push_back(Point(6,4));
211 curve.push_back(Point(7,4));
212 curve.push_back(Point(8,4));
213 curve.push_back(Point(9,3));
214 curve.push_back(Point(9,2));
215 curve.push_back(Point(10,2));
216 curve.push_back(Point(11,2));
220 PrimitiveType computer;
221 DecompositionType theDecomposition(curve.begin(), curve.end(), computer,
false);
225 aBoard.
setUnit(Board::UCentimeter);
226 aBoard <<
SetMode(
"PointVector",
"Both");
227 for (ContourType::iterator it = curve.begin(); it != curve.end(); ++it) {
233 unsigned int compteur = 0;
234 DecompositionType::SegmentIterator i = theDecomposition.begin();
235 for ( ; i != theDecomposition.end(); ++i) {
238 trace.
info() <<
"Segment " << compteur << std::endl;
239 trace.
info() << i.intersectPrevious() <<
" - " << i.intersectNext() << std::endl;
240 PrimitiveType segment(*i);
242 aBoard <<
SetMode( segment.className(),
"BoundingBox" )
246 aBoard.saveSVG(
"specialCase.svg");
250 return (compteur==5);
258 bool testClosedCurvesProcessedAsClosed()
269 std::stringstream ss(stringstream::in | stringstream::out);
270 ss <<
"31 16 1112121212121221212121221212212222232232323332333333332333332330333033003030000010001001001000100010101010111" << endl;
273 Contour4 theContour( ss );
277 Decomposition4 theDecomposition( theContour.begin(),theContour.end(),computer,true );
280 aBoard <<
SetMode(
"PointVector",
"Grid" )
283 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" );
284 string className =
"ArithmeticalDSS/BoundingBox";
285 for ( Decomposition4::SegmentIterator i = theDecomposition.begin();
286 i != theDecomposition.end(); ++i )
295 aBoard.saveSVG(
"testClosedCurvesProcessedAsClosed.svg");
306 bool testClosedCurvesProcessedAsOpen()
316 std::stringstream ss(stringstream::in | stringstream::out);
317 ss <<
"31 16 1112121212121221212121221212212222232232323332333333332333332330333033003030000010001001001000100010101010111" << endl;
320 Contour4 theContour( ss );
324 Decomposition4 theDecomposition( theContour.begin(),theContour.end(),computer,
false );
327 aBoard <<
SetMode(
"PointVector",
"Grid" )
330 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" );
331 string className =
"ArithmeticalDSS/BoundingBox";
332 for ( Decomposition4::SegmentIterator i = theDecomposition.begin();
333 i != theDecomposition.end(); ++i )
342 aBoard.saveSVG(
"testClosedCurvesProcessedAsOpen.svg");
353 bool testOpenCurvesProcessedAsClosed()
363 std::stringstream ss(stringstream::in | stringstream::out);
364 ss <<
"31 16 1112121212121221212121221212212222232232323332333333332333" << endl;
367 Contour4 theContour( ss );
371 Decomposition4 theDecomposition( theContour.begin(),theContour.end(),computer,true );
374 aBoard <<
SetMode(
"PointVector",
"Grid" )
377 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" );
378 string className =
"ArithmeticalDSS/BoundingBox";
379 for ( Decomposition4::SegmentIterator i = theDecomposition.begin();
380 i != theDecomposition.end(); ++i )
389 aBoard.saveSVG(
"testOpenCurvesProcessedAsClosed.svg");
402 typedef int Coordinate;
407 std::vector<Point> curve;
411 PrimitiveType computer;
412 DecompositionType theDecomposition(curve.begin(), curve.end(), computer,
false);
414 for ( DecompositionType::SegmentIterator i = theDecomposition.begin();
415 i != theDecomposition.end(); ++i )
420 }
catch (std::exception e) {
433 typedef int Coordinate;
435 typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,4> PrimitiveType;
439 std::vector<Point> curve;
440 curve.push_back(Point(5,5));
444 PrimitiveType computer;
445 DecompositionType theDecomposition(curve.begin(), curve.end(), computer,
false);
448 aBoard << curve.at(0);
450 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" );
451 for ( DecompositionType::SegmentIterator i = theDecomposition.begin();
452 i != theDecomposition.end(); ++i )
454 PrimitiveType primitive(*i);
459 aBoard.saveSVG(
"testOnePoint.svg");
463 }
catch (std::exception e) {
473 bool testTwoEndIterators()
475 typedef int Coordinate;
477 typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,4> PrimitiveType;
481 std::vector<Point> curve;
482 curve.push_back(Point(5,5));
487 PrimitiveType computer;
488 DecompositionType theDecomposition(curve.begin(), curve.end(), computer,
false);
490 for ( DecompositionType::SegmentIterator i = theDecomposition.begin();
491 i != theDecomposition.end(); ++i )
497 }
catch (std::exception e) {
512 typedef int Coordinate;
514 typedef ArithmeticalDSS<std::vector<Point>::iterator,Coordinate,8> PrimitiveType;
517 std::vector<Point> curve;
518 curve.push_back(Point(0,0));
519 curve.push_back(Point(1,1));
520 curve.push_back(Point(2,1));
521 curve.push_back(Point(3,2));
522 curve.push_back(Point(4,2));
523 curve.push_back(Point(5,2));
524 curve.push_back(Point(6,3));
525 curve.push_back(Point(7,3));
529 PrimitiveType computer;
530 DecompositionType theDecomposition(curve.begin(), curve.end(), computer,
false);
534 aBoard.
setUnit(Board::UCentimeter);
535 aBoard <<
SetMode(
"PointVector",
"Both");
536 for (std::vector<Point>::iterator it = curve.begin(); it != curve.end(); ++it) {
541 unsigned int compteur = 0;
542 DecompositionType::SegmentIterator i = theDecomposition.begin();
543 for ( ; i != theDecomposition.end(); ++i) {
546 PrimitiveType segment(*i);
548 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" )
552 aBoard.saveSVG(
"oneDSS.svg");
556 return (compteur==1);
564 bool testDec8Reverse()
567 typedef int Coordinate;
569 typedef std::vector<Point> ContourType;
573 std::vector<Point> curve;
574 curve.push_back(Point(1,1));
575 curve.push_back(Point(2,1));
576 curve.push_back(Point(3,2));
577 curve.push_back(Point(4,2));
578 curve.push_back(Point(5,2));
579 curve.push_back(Point(6,2));
580 curve.push_back(Point(7,2));
581 curve.push_back(Point(8,1));
582 curve.push_back(Point(9,1));
585 trace.
beginBlock(
"Segmentation of a 8-connected digital curve into DSS");
586 PrimitiveType computer;
587 DecompositionType theDecomposition(curve.rbegin(), curve.rend(), computer,
false);
591 aBoard.
setUnit(Board::UCentimeter);
592 aBoard <<
SetMode(
"PointVector",
"Both");
593 for (ContourType::iterator it = curve.begin(); it != curve.end(); ++it) {
599 unsigned int compteur = 0;
600 DecompositionType::SegmentIterator i = theDecomposition.begin();
601 for ( ; i != theDecomposition.end(); ++i) {
604 trace.
info() <<
"Segment " << compteur << std::endl;
605 PrimitiveType segment(*i);
607 aBoard <<
SetMode(
"ArithmeticalDSS",
"BoundingBox" )
611 aBoard.saveSVG(
"right_left.svg");
615 return (compteur==2);
622 int main(
int argc,
char **argv)
627 for (
int i = 0; i < argc; ++i )
631 bool res = testDec4()
633 && testDisconnectedCurve()
634 && testClosedCurvesProcessedAsClosed()
635 && testClosedCurvesProcessedAsOpen()
636 && testOpenCurvesProcessedAsClosed()
639 && testTwoEndIterators()
641 && testDec8Reverse();
642 trace.
emphase() << ( res ?
"Passed." :
"Error." ) << endl;