DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
convex-and-concave-parts.cpp
1 
29 
30 #include <cmath>
31 #include <iostream>
32 #include <sstream>
33 #include <fstream>
34 #include "DGtal/base/Common.h"
35 #include "DGtal/io/boards/Board2D.h"
36 #include "DGtal/io/Color.h"
37 #include "DGtal/shapes/Shapes.h"
38 #include "DGtal/helpers/StdDefs.h"
39 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
40 #include "DGtal/geometry/curves/FreemanChain.h"
41 #include "DGtal/geometry/curves/SaturatedSegmentation.h"
43 #include "ConfigExamples.h"
44 
45 using namespace std;
46 using namespace DGtal;
47 using namespace Z2i;
48 
50 
51 typedef FreemanChain<int> Range;
52 
54 
58 template <typename Iterator, typename Board>
59 void drawCCP(const Iterator& itb, const Iterator& ite, Board& aBoard)
60 {
61 
62  typedef typename Iterator::SegmentComputer::ConstIterator PointIterator;
63 
64  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" );
65  string aStyleName = "ArithmeticalDSS/BoundingBox";
66 
67  for (Iterator i(itb); i != ite; ++i) {
68 
69  //choose pen color
70  CustomPenColor* aPenColor;
71 
72  if ( !(i.intersectNext() && i.intersectPrevious()) ) {
73 
74  aPenColor = new CustomPenColor( Color::Black );
75 
76  } else {
77 
78  //end points
79 
80  PointIterator begin = i->begin(); --begin;
81  PointIterator end = i->end();
82 
83  //parameters
84  int mu = i->getMu();
85  int omega = i->getOmega();
86 
87  //configurations
88  if ( (i->getRemainder(begin)<=mu-1)&&
89  (i->getRemainder(end)<=mu-1) ) { //concave
90  aPenColor = new CustomPenColor( Color::Green);
91  } else if ( (i->getRemainder(begin)>=mu+omega)&&
92  (i->getRemainder(end)>=mu+omega) ) { //convex
93  aPenColor = new CustomPenColor( Color::Blue );
94  } else if ( (i->getRemainder(begin)>=mu+omega)&&
95  (i->getRemainder(end)<=mu-1) ) { //convex to concave
96  aPenColor = new CustomPenColor( Color::Yellow );
97  } else if ( (i->getRemainder(begin)<=mu-1)&&
98  (i->getRemainder(end)>=mu+omega) ) { //concave to convex
99  aPenColor = new CustomPenColor( Color::Yellow );
100  } else { //pb
101  aPenColor = new CustomPenColor( Color::Red );
102  }
103 
104  }
105 
106  // draw each segment
107  aBoard << CustomStyle( aStyleName, aPenColor )
108  << *i;
109 
110  }
111 
112 }
113 
117 template <typename Iterator, typename Board>
118 void segmentationIntoMaximalDSSs(const Iterator& itb, const Iterator& ite,
119  Board& aBoard)
120 {
121  typedef typename IteratorCirculatorTraits<Iterator>::Value::Coordinate Coordinate;
122  typedef ArithmeticalDSS<Iterator,Coordinate,4> RecognitionAlgorithm;
123  typedef SaturatedSegmentation<RecognitionAlgorithm> Segmentation;
124 
125  RecognitionAlgorithm algo;
126  Segmentation s(itb,ite,algo);
127 
128  typename Segmentation::SegmentComputerIterator i = s.begin();
129  typename Segmentation::SegmentComputerIterator end = s.end();
130 
131  drawCCP<typename Segmentation::SegmentComputerIterator, Board>
132  (i,end,aBoard);
133 
134 }
135 
136 
137 int main( int argc, char** argv )
138 {
139 
140  trace.beginBlock ( "Example convex-and-concave-parts" );
141 
142  trace.info() << "Args:";
143  for ( int i = 0; i < argc; ++i )
144  trace.info() << " " << argv[ i ];
145  trace.info() << endl;
146 
147 
148  string codes;
149  if (argc >= 2) codes = argv[1];
150  else codes = "030030330303303030300001010101101011010000030330303303030300001010110101011010000033";
151 
152  stringstream ss(stringstream::in | stringstream::out);
153  ss << "0 0 " << codes << endl;
154  Range theContour( ss );
155 
156  trace.info() << "Processing of " << ss.str() << endl;
157 
158  //Maximal Segments
159  Board2D aBoard;
160  aBoard
161  << SetMode( "PointVector", "Grid" )
162  << theContour;
163 
164  segmentationIntoMaximalDSSs(theContour.begin(), theContour.end(), aBoard);
165 
166  aBoard.saveSVG("convex-and-concave-parts.svg");
167 
168  trace.endBlock();
169 
170  return 0;
171 }
172 // //