DGtal  0.9.3beta
convex-and-concave-parts.cpp
1 
29 #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/ArithmeticalDSSComputer.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 
53 
70 template <typename Iterator, typename Board>
71 void drawCCP(const Iterator& itb, const Iterator& ite, Board& aBoard)
72 {
73 
74  //choose the drawing mode
75  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" );
76  //prepare the drawing style and the pen color
77  string aStyleName = "ArithmeticalDSS/BoundingBox";
78  CustomPenColor* aPenColor;
79 
80  //for each maximal segment
81  for (Iterator i(itb); i != ite; ++i) {
82 
83  //get the current maximal segment
84  typedef typename Iterator::SegmentComputer::Primitive DSS;
85  DSS maximalDSS = i->primitive();
86 
87  //if located at the end of a connected part
88  if ( !(i.intersectNext() && i.intersectPrevious()) ) {
89 
90  aPenColor = new CustomPenColor( Color::Black );
91 
92  //otherwise
93  } else {
94 
95  //get the points located before and after the maximal segment
96  typedef typename DSS::Point Point;
97  Point beforeFirst = *(--(i->begin()));
98  Point afterLast = *(i->end());
99 
100  //remainders and bounds
101  typedef typename DSS::Integer Integer;
102  Integer r1 = maximalDSS.remainder(beforeFirst);
103  Integer r2 = maximalDSS.remainder(afterLast);
104  Integer mu = maximalDSS.mu();
105  Integer omega = maximalDSS.omega();
106 
107  //configurations
108  if ( (r1<=mu-1)&&(r2<=mu-1) ) { //concave
109  aPenColor = new CustomPenColor( Color::Green);
110  } else if ( (r1>=mu+omega)&&(r2>=mu+omega) ) { //convex
111  aPenColor = new CustomPenColor( Color::Blue );
112  } else if ( (r1>=mu+omega)&&(r2<=mu-1) ) { //convex to concave
113  aPenColor = new CustomPenColor( Color::Yellow );
114  } else if ( (r1<=mu-1)&&(r2>=mu+omega) ) { //concave to convex
115  aPenColor = new CustomPenColor( Color::Yellow );
116  } else { //pb
117  aPenColor = new CustomPenColor( Color::Red );
118  }
119 
120  }
121 
122  // draw the maximal segment on the board
123  aBoard << CustomStyle( aStyleName, aPenColor )
124  << maximalDSS;
125 
126  }
127 
128 }
129 
142 template <typename Iterator, typename Board>
143 void segmentationIntoMaximalDSSs(const Iterator& itb, const Iterator& ite,
144  Board& aBoard)
145 {
146  typedef typename IteratorCirculatorTraits<Iterator>::Value::Coordinate Coordinate;
147 
148  //choose the primitive computer and the segmentation
149  typedef ArithmeticalDSSComputer<Iterator,Coordinate,4> RecognitionAlgorithm;
150  typedef SaturatedSegmentation<RecognitionAlgorithm> Segmentation;
151 
152  //create the segmentation
153  RecognitionAlgorithm algo;
154  Segmentation s(itb,ite,algo);
155 
156  //draw the result
157  drawCCP(s.begin(), s.end(), aBoard);
158 }
159 
160 
170 int main( int argc, char** argv )
171 {
172 
173  trace.beginBlock ( "Example convex-and-concave-parts" );
174 
175  Board2D aBoard; //create a board
176 
177  //create a chain code
178  string codes;
179  if (argc >= 2) codes = argv[1];
180  else codes = "030030330303303030300001010101101011010000030330303303030300001010110101011010000033";
181 
182  stringstream ss(stringstream::in | stringstream::out);
183  ss << "0 0 " << codes << endl;
184  FreemanChain<int> theContour( ss );
185 
186  trace.info() << "Processing of " << ss.str() << endl;
187 
188  //draw the digital contour
189  aBoard
190  << SetMode( "PointVector", "Grid" )
191  << theContour;
192 
193  //draw the maximal segments
194  segmentationIntoMaximalDSSs(theContour.begin(), theContour.end(), aBoard);
195 
196  //save the drawing
197  aBoard.saveSVG("convex-and-concave-parts.svg");
198  #ifdef WITH_CAIRO
199  aBoard.saveCairo("convex-and-concave-parts.png");
200  #endif
201 
202  trace.endBlock();
203 
204  return 0;
205 }
206 // //
void beginBlock(const std::string &keyword="")
DGtal::int32_t Integer
Definition: StdDefs.h:74
Trace trace
Definition: Common.h:137
STL namespace.
double endBlock()
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1139
Aim: This class is a wrapper around ArithmeticalDSS that is devoted to the dynamic recognition of dig...
Aim: Provides nested types for both iterators and circulators: Type, Category, Value, Difference, Pointer and Reference.
DGtal is the top-level namespace which contains all DGtal functions and types.
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
std::ostream & info()
Modifier class in a Board2D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithBoard2D.
Definition: Board2D.h:247
Aim: Computes the saturated segmentation, that is the whole set of maximal segments within a range gi...
Custom style class redefining the pen color. You may use Board2D::Color::None for transparent color...
Definition: Board2D.h:312
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70