DGtal  1.5.beta
convex-and-concave-parts.cpp
Go to the documentation of this file.
1 
54 #include <cmath>
55 #include <iostream>
56 #include <sstream>
57 #include <fstream>
58 #include "DGtal/base/Common.h"
59 #include "DGtal/io/boards/Board2D.h"
60 #include "DGtal/io/Color.h"
61 #include "DGtal/shapes/Shapes.h"
62 #include "DGtal/helpers/StdDefs.h"
63 #include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
64 #include "DGtal/geometry/curves/FreemanChain.h"
65 #include "DGtal/geometry/curves/SaturatedSegmentation.h"
67 #include "ConfigExamples.h"
68 
69 using namespace std;
70 using namespace DGtal;
71 using namespace Z2i;
72 
74 
75 
77 
94 template <typename Iterator, typename Board>
95 void drawCCP(const Iterator& itb, const Iterator& ite, Board& aBoard)
96 {
97 
98  //choose the drawing mode
99  aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" );
100  //prepare the drawing style and the pen color
101  string aStyleName = "ArithmeticalDSS/BoundingBox";
102  CustomPenColor* aPenColor;
103 
104  //for each maximal segment
105  for (Iterator i(itb); i != ite; ++i) {
106 
107  //get the current maximal segment
108  typedef typename Iterator::SegmentComputer::Primitive DSS;
109  DSS maximalDSS = i->primitive();
110 
111  //if located at the end of a connected part
112  if ( !(i.intersectNext() && i.intersectPrevious()) ) {
113 
114  aPenColor = new CustomPenColor( Color::Black );
115 
116  //otherwise
117  } else {
118 
119  //get the points located before and after the maximal segment
120  typedef typename DSS::Point Point;
121  Point beforeFirst = *(--(i->begin()));
122  Point afterLast = *(i->end());
123 
124  //remainders and bounds
125  typedef typename DSS::Integer Integer;
126  Integer r1 = maximalDSS.remainder(beforeFirst);
127  Integer r2 = maximalDSS.remainder(afterLast);
128  Integer mu = maximalDSS.mu();
129  Integer omega = maximalDSS.omega();
130 
131  //configurations
132  if ( (r1<=mu-1)&&(r2<=mu-1) ) { //concave
133  aPenColor = new CustomPenColor( Color::Green);
134  } else if ( (r1>=mu+omega)&&(r2>=mu+omega) ) { //convex
135  aPenColor = new CustomPenColor( Color::Blue );
136  } else if ( (r1>=mu+omega)&&(r2<=mu-1) ) { //convex to concave
137  aPenColor = new CustomPenColor( Color::Yellow );
138  } else if ( (r1<=mu-1)&&(r2>=mu+omega) ) { //concave to convex
139  aPenColor = new CustomPenColor( Color::Yellow );
140  } else { //pb
141  aPenColor = new CustomPenColor( Color::Red );
142  }
143 
144  }
145 
146  // draw the maximal segment on the board
147  aBoard << CustomStyle( aStyleName, aPenColor )
148  << maximalDSS;
149 
150  }
151 
152 }
153 
166 template <typename Iterator, typename Board>
167 void segmentationIntoMaximalDSSs(const Iterator& itb, const Iterator& ite,
168  Board& aBoard)
169 {
170  typedef typename IteratorCirculatorTraits<Iterator>::Value::Coordinate Coordinate;
171 
172  //choose the primitive computer and the segmentation
173  typedef ArithmeticalDSSComputer<Iterator,Coordinate,4> RecognitionAlgorithm;
175 
176  //create the segmentation
177  RecognitionAlgorithm algo;
178  Segmentation s(itb,ite,algo);
179 
180  //draw the result
181  drawCCP(s.begin(), s.end(), aBoard);
182 }
183 
184 
194 int main( int argc, char** argv )
195 {
196 
197  trace.beginBlock ( "Example convex-and-concave-parts" );
198 
199  Board2D aBoard; //create a board
200 
201  //create a chain code
202  string codes;
203  if (argc >= 2) codes = argv[1];
204  else codes = "030030330303303030300001010101101011010000030330303303030300001010110101011010000033";
205 
206  stringstream ss(stringstream::in | stringstream::out);
207  ss << "0 0 " << codes << endl;
208  FreemanChain<int> theContour( ss );
209 
210  trace.info() << "Processing of " << ss.str() << endl;
211 
212  //draw the digital contour
213  aBoard
214  << SetMode( "PointVector", "Grid" )
215  << theContour;
216 
217  //draw the maximal segments
218  segmentationIntoMaximalDSSs(theContour.begin(), theContour.end(), aBoard);
219 
220  //save the drawing
221  aBoard.saveSVG("convex-and-concave-parts.svg");
222  #ifdef WITH_CAIRO
223  aBoard.saveCairo("convex-and-concave-parts.png");
224  #endif
225 
226  trace.endBlock();
227 
228  return 0;
229 }
230 // //
Aim: This class is a wrapper around ArithmeticalDSS that is devoted to the dynamic recognition of dig...
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
ConstIterator end() const
ConstIterator begin() const
Aim: Computes the saturated segmentation, that is the whole set of maximal segments within a range gi...
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1011
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1138
void segmentationIntoMaximalDSSs(const Iterator &itb, const Iterator &ite, Board &aBoard)
Perform a saturated segmentation into maximal digital straight segments of a given range of integer p...
int main(int argc, char **argv)
Program that draws the maximal segments of digital curve whose chain code may be given as an argument...
void drawCCP(const Iterator &itb, const Iterator &ite, Board &aBoard)
Function that draws the maximal segments with a color that depends on the local convexity:
DGtal::int32_t Integer
Definition: StdDefs.h:74
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Custom style class redefining the pen color. You may use Board2D::Color::None for transparent color.
Definition: Board2D.h:313
Aim: Provides nested types for both iterators and circulators: Type, Category, Value,...
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
SaturatedSegmentation< SegmentComputer > Segmentation
MyPointD Point
Definition: testClone2.cpp:383