DGtal  0.9.2
exampleArithmeticalDSS.cpp
1 
30 #include <iostream>
32 #include <exception>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "DGtal/io/boards/Board2D.h"
36 
38 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
41 
42 using namespace std;
43 using namespace DGtal;
44 using namespace Z2i;
46 
51 void exampleNaiveDSS()
52 {
53  trace.beginBlock ( "Naive DSS" );
54 
55  using namespace Z2i;
56 
58  // Construct a naive DSS
59  NaiveDSS8<Integer> segment( 5, 8, //slope
60  Point(0,0), Point(8,5), //ending points
61  Point(0,0), Point(8,5), //upper points
62  Point(3,1), Point(3,1) //lower points
63  );
65 
66  // Trace to the standard output
67  trace.info() << segment << std::endl;
68 
70  // Trace the position and remainder of each point
72  it = segment.begin(),
73  ite = segment.end();
74  it != ite; ++it )
75  {
76  trace.info() << "("
77  << segment.position( *it ) << ","
78  << segment.remainder( *it )
79  << ") ";
80  }
82  trace.info() << std::endl;
83 
85  Board2D board;
86 
87  // Draw the grid
88  Domain domain( Point(0,0), Point(8,5) );
89  board << SetMode(domain.className(), "Grid")
90  << domain;
91 
92  //Draw the points of the DSS
93  board << SetMode("PointVector", "Both");
94  board << SetMode(segment.className(), "Points")
95  << segment;
96 
97  // Draw the bounding box
98  board << SetMode(segment.className(), "BoundingBox")
99  << segment;
101 
102 
103  // Save
104  board.saveSVG("NaiveDSS8.svg");
105 #ifdef WITH_CAIRO
106  board.saveCairo("NaiveDSS8.png", Board2D::CairoPNG);
107 #endif
108 
109  trace.endBlock();
110 }
111 
112 
117 void exampleStandardDSS()
118 {
119  trace.beginBlock ( "Standard DSS" );
120 
121  using namespace Z2i;
122 
124  // Construct a standard DSS
125  StandardDSS4<Integer> segment( 5, 8, //slope
126  Point(0,0), Point(8,5), //ending points
127  Point(0,0), Point(8,5), //upper points
128  Point(4,1), Point(4,1) //lower points
129  );
131 
132  // Trace to the standard output
133  trace.info() << segment << std::endl;
134 
135  // Display the DSS with a domain on a board
136  Domain domain( Point(0,0), Point(8,5) );
137  Board2D board;
138 
140  // Draw the grid
141  board << SetMode(domain.className(), "Grid")
142  << domain;
143 
144  // Draw the points of the DSS
145  board << SetMode("PointVector", "Grid")
146  << SetMode(segment.className(), "Points")
147  << segment;
148 
149  // Draw the bounding box
150  board << SetMode(segment.className(), "BoundingBox")
151  << segment;
153 
154  // Save
155  board.saveSVG("StandardDSS4.svg");
156 #ifdef WITH_CAIRO
157  board.saveCairo("StandardDSS4.png", Board2D::CairoPNG);
158 #endif
159 
160  board.clear();
162  // Draw the pixels
163  board << SetMode(domain.className(), "Paving")
164  << domain;
165 
166  //Draw the points of the DSS
167  board << SetMode("PointVector", "Both");
168  board << SetMode(segment.className(), "Points")
169  << segment;
170 
171  // Draw the bounding box
172  board << SetMode(segment.className(), "BoundingBox")
173  << segment;
175 
176  board.saveSVG("StandardDSS4bis.svg");
177 #ifdef WITH_CAIRO
178  board.saveCairo("StandardDSS4bis.png", Board2D::CairoPNG);
179 #endif
180 
181  trace.endBlock();
182 }
183 
188 void exampleConstructors()
189 {
190  trace.beginBlock ( "DSSs constructions" );
191 
192  using namespace Z2i;
193 
194  {
196  // Construct a naive DSS from two upper leaning points
197  NaiveDSS8<Integer> segment( Point(0,0), Point(8,5), true );
198  //or simply NaiveDSS8<Integer> segment( Point(0,0), Point(8,5) );
200  trace.info() << segment << std::endl;
201  }
202 
203  {
205  // Construct a naive DSS from two lower leaning points
206  NaiveDSS8<Integer> segment( Point(0,0), Point(8,5), false );
208  trace.info() << segment << std::endl;
209  }
210 
211  {
213  // Construct a naive DSS as a DSL subsegment
214  NaiveDSS8<Integer> segment( NaiveDSL<Integer>(5,8,0), Point(0,0), Point(8,5) );
216  trace.info() << segment << std::endl;
217  }
218 
219  {
220  NaiveDSS8<Integer> bigDSS( NaiveDSL<Integer>(5,8,0), Point(-8,-5), Point(16,10) );
222  // Construct a naive DSS as a subsegment of a greater DSS
223  NaiveDSS8<Integer> segment( bigDSS, Point(0,0), Point(8,5) );
225  trace.info() << segment << std::endl;
226  }
227 
228  std::vector<Point> r; //container for DSS points
229  {
231  // Custom a naive DSS
232  NaiveDSS8<Integer> segment( 5, 8, //slope
233  Point(0,0), Point(8,5), //ending points
234  Point(0,0), Point(8,5), //upper points
235  Point(3,1), Point(3,1) //lower points
236  );
237  //You should be sure that your object is valid before using it
238  if (!segment.isValid()) throw std::exception();
240  trace.info() << segment << std::endl;
241 
242  //copy the DSS points into the container r
243  std::copy( segment.begin(), segment.end(), std::back_inserter(r) );
244  }
245 
246  {
248  // Construct a DSS from a range of points
249  NaiveDSS8<Integer> segment( r.begin(), r.end() );
251  trace.info() << segment << std::endl;
252  }
253 
254 
255  trace.endBlock();
256 }
257 
261 void exampleUpdate()
262 {
263  trace.beginBlock ( "DSS update" );
264 
265  using namespace Z2i;
266 
267  //Construction --------------------------------------------------
269  Point M(11, 7);
270  NaiveDSS8<Integer> S( 5, 8, //slope
271  Point(0,0), Point(10,6), //ending points
272  Point(0,0), Point(8,5), //upper points
273  Point(3,1), Point(3,1) //lower points
274  );
276 
277  //this segment should be valid:
278  if (!S.isValid()) throw std::exception();
279  // Store a copy before any operation
280  NaiveDSS8<Integer> copyOfS = S;
281 
282  trace.info() << S << std::endl;
283 
284 
285  //Display ------------------------------------------------------
286  {
287  Board2D board;
288 
289  // Draw the grid
290  Domain domain( Point(0,0), M );
291  board << SetMode(domain.className(), "Grid")
292  << domain;
293  // Draw the points of the DSS and its bounding box
294  board << SetMode("PointVector", "Both");
295  board << SetMode(S.className(), "Points")
296  << S
297  << SetMode(S.className(), "BoundingBox")
298  << S;
299  // Draw the orthonormal base
300  board.drawArrow(0.0, 0.0, 1.0, 0.0);
301  board.drawArrow(0.0, 0.0, 0.0, 1.0);
302  // Draw M
303  board << SetMode(M.className(), "Both")
304  << CustomStyle( M.className(), new CustomColors( Color(255,0,0), Color(192, 0, 0)) )
305  << M;
306 
307 
308  // Save
309  board.saveSVG("NaiveDSS8ExtInit.svg");
310 #ifdef WITH_CAIRO
311  board.saveCairo("NaiveDSS8ExtInit.png", Board2D::CairoPNG);
312 #endif
313  }
314 
315  // Extension -----------------------------------------------------
317  bool resExtention = S.extendFront( M );
319  //this segment should be extended:
320  if (!resExtention) throw std::exception();
321 
322  trace.info() << S << std::endl;
323 
324  //Display ------------------------------------------------------
325  {
326  Board2D board;
327 
328  // Draw the grid
329  Domain domain( Point(0,0), M );
330  board << SetMode(domain.className(), "Grid")
331  << domain;
332  // Draw the points of the DSS and its bounding box
333  board << SetMode("PointVector", "Both");
334  board << SetMode(S.className(), "Points")
335  << S
336  << SetMode(S.className(), "BoundingBox")
337  << S;
338  // Draw the orthonormal base
339  board.drawArrow(0.0, 0.0, 1.0, 0.0);
340  board.drawArrow(0.0, 0.0, 0.0, 1.0);
341 
342  // Save
343  board.saveSVG("NaiveDSS8ExtDone.svg");
344 #ifdef WITH_CAIRO
345  board.saveCairo("NaiveDSS8ExtDone.png", Board2D::CairoPNG);
346 #endif
347  }
348 
349  // Retraction ----------------------------------------------------
351  bool resRetraction = S.retractFront();
353  //this segment should be retracted:
354  if (!resRetraction) throw std::exception();
355 
356  trace.info() << S << std::endl;
357 
358  // Comparaison ----------------------------------------------------
360  //this segment and the previous copy should be equal:
361  if ( !S.equalsTo(copyOfS) ) throw std::exception();
363 
364  trace.endBlock();
365 }
366 
367 
369 int main( int argc, char** argv )
370 {
371  trace.beginBlock ( "Example exampleArithmeticalDSS" );
372  trace.info() << "Args:";
373  for ( int i = 0; i < argc; ++i )
374  trace.info() << " " << argv[ i ];
375  trace.info() << endl;
376 
377  exampleNaiveDSS();
378  exampleStandardDSS();
379  exampleConstructors();
380  exampleUpdate();
381 
382  trace.endBlock();
383  return 0;
384 }
385 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
ConstIterator begin() const
STL namespace.
double endBlock()
void drawArrow(double x1, double y1, double x2, double y2, bool filled=true, int depthValue=-1)
Definition: Board.cpp:400
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1139
std::string className() const
Aim: This class is an alias of ArithmeticalDSS for naive DSL. It represents a naive digital straight ...
Aim: This class represents a standard digital straight segment (DSS), ie. the sequence of simply 4-co...
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
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
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
Aim: This class represents a standard digital straight segment (DSS), ie. the sequence of simply 8-co...
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70