DGtal 1.3.0
Loading...
Searching...
No Matches
exampleArithmeticalDSS.cpp
Go to the documentation of this file.
1
40#include <iostream>
41#include <exception>
42#include "DGtal/base/Common.h"
43#include "DGtal/helpers/StdDefs.h"
44#include "DGtal/io/boards/Board2D.h"
45
47#include "DGtal/geometry/curves/ArithmeticalDSS.h"
50
51using namespace std;
52using namespace DGtal;
53using namespace Z2i;
55
61{
62 trace.beginBlock ( "Naive DSS" );
63
64 using namespace Z2i;
65
67 // Construct a naive DSS
68 NaiveDSS8<Integer> segment( 5, 8, //slope
69 Point(0,0), Point(8,5), //ending points
70 Point(0,0), Point(8,5), //upper points
71 Point(3,1), Point(3,1) //lower points
72 );
74
75 // Trace to the standard output
76 trace.info() << segment << std::endl;
77
79 // Trace the position and remainder of each point
81 it = segment.begin(),
82 ite = segment.end();
83 it != ite; ++it )
84 {
85 trace.info() << "("
86 << segment.position( *it ) << ","
87 << segment.remainder( *it )
88 << ") ";
89 }
91 trace.info() << std::endl;
92
94 Board2D board;
95
96 // Draw the grid
97 Domain domain( Point(0,0), Point(8,5) );
98 board << SetMode(domain.className(), "Grid")
99 << domain;
100
101 //Draw the points of the DSS
102 board << SetMode("PointVector", "Both");
103 board << SetMode(segment.className(), "Points")
104 << segment;
105
106 // Draw the bounding box
107 board << SetMode(segment.className(), "BoundingBox")
108 << segment;
110
111
112 // Save
113 board.saveSVG("NaiveDSS8.svg");
114#ifdef WITH_CAIRO
115 board.saveCairo("NaiveDSS8.png", Board2D::CairoPNG);
116#endif
117
118 trace.endBlock();
119}
120
121
127{
128 trace.beginBlock ( "Standard DSS" );
129
130 using namespace Z2i;
131
133 // Construct a standard DSS
134 StandardDSS4<Integer> segment( 5, 8, //slope
135 Point(0,0), Point(8,5), //ending points
136 Point(0,0), Point(8,5), //upper points
137 Point(4,1), Point(4,1) //lower points
138 );
140
141 // Trace to the standard output
142 trace.info() << segment << std::endl;
143
144 // Display the DSS with a domain on a board
145 Domain domain( Point(0,0), Point(8,5) );
146 Board2D board;
147
149 // Draw the grid
150 board << SetMode(domain.className(), "Grid")
151 << domain;
152
153 // Draw the points of the DSS
154 board << SetMode("PointVector", "Grid")
155 << SetMode(segment.className(), "Points")
156 << segment;
157
158 // Draw the bounding box
159 board << SetMode(segment.className(), "BoundingBox")
160 << segment;
162
163 // Save
164 board.saveSVG("StandardDSS4.svg");
165#ifdef WITH_CAIRO
166 board.saveCairo("StandardDSS4.png", Board2D::CairoPNG);
167#endif
168
169 board.clear();
171 // Draw the pixels
172 board << SetMode(domain.className(), "Paving")
173 << domain;
174
175 //Draw the points of the DSS
176 board << SetMode("PointVector", "Both");
177 board << SetMode(segment.className(), "Points")
178 << segment;
179
180 // Draw the bounding box
181 board << SetMode(segment.className(), "BoundingBox")
182 << segment;
184
185 board.saveSVG("StandardDSS4bis.svg");
186#ifdef WITH_CAIRO
187 board.saveCairo("StandardDSS4bis.png", Board2D::CairoPNG);
188#endif
189
190 trace.endBlock();
191}
192
198{
199 trace.beginBlock ( "DSSs constructions" );
200
201 using namespace Z2i;
202
203 {
205 // Construct a naive DSS from two upper leaning points
206 NaiveDSS8<Integer> segment( Point(0,0), Point(8,5), true );
207 //or simply NaiveDSS8<Integer> segment( Point(0,0), Point(8,5) );
209 trace.info() << segment << std::endl;
210 }
211
212 {
214 // Construct a naive DSS from two lower leaning points
215 NaiveDSS8<Integer> segment( Point(0,0), Point(8,5), false );
217 trace.info() << segment << std::endl;
218 }
219
220 {
222 // Construct a naive DSS as a DSL subsegment
223 NaiveDSS8<Integer> segment( NaiveDSL<Integer>(5,8,0), Point(0,0), Point(8,5) );
225 trace.info() << segment << std::endl;
226 }
227
228 {
229 NaiveDSS8<Integer> bigDSS( NaiveDSL<Integer>(5,8,0), Point(-8,-5), Point(16,10) );
231 // Construct a naive DSS as a subsegment of a greater DSS
232 NaiveDSS8<Integer> segment( bigDSS, Point(0,0), Point(8,5) );
234 trace.info() << segment << std::endl;
235 }
236
237 std::vector<Point> r; //container for DSS points
238 {
240 // Custom a naive DSS
241 NaiveDSS8<Integer> segment( 5, 8, //slope
242 Point(0,0), Point(8,5), //ending points
243 Point(0,0), Point(8,5), //upper points
244 Point(3,1), Point(3,1) //lower points
245 );
246 //You should be sure that your object is valid before using it
247 if (!segment.isValid()) throw std::exception();
249 trace.info() << segment << std::endl;
250
251 //copy the DSS points into the container r
252 std::copy( segment.begin(), segment.end(), std::back_inserter(r) );
253 }
254
255 {
257 // Construct a DSS from a range of points
258 NaiveDSS8<Integer> segment( r.begin(), r.end() );
260 trace.info() << segment << std::endl;
261 }
262
263
264 trace.endBlock();
265}
266
271{
272 trace.beginBlock ( "DSS update" );
273
274 using namespace Z2i;
275
276 //Construction --------------------------------------------------
278 Point M(11, 7);
279 NaiveDSS8<Integer> S( 5, 8, //slope
280 Point(0,0), Point(10,6), //ending points
281 Point(0,0), Point(8,5), //upper points
282 Point(3,1), Point(3,1) //lower points
283 );
285
286 //this segment should be valid:
287 if (!S.isValid()) throw std::exception();
288 // Store a copy before any operation
289 NaiveDSS8<Integer> copyOfS = S;
290
291 trace.info() << S << std::endl;
292
293
294 //Display ------------------------------------------------------
295 {
296 Board2D board;
297
298 // Draw the grid
299 Domain domain( Point(0,0), M );
300 board << SetMode(domain.className(), "Grid")
301 << domain;
302 // Draw the points of the DSS and its bounding box
303 board << SetMode("PointVector", "Both");
304 board << SetMode(S.className(), "Points")
305 << S
306 << SetMode(S.className(), "BoundingBox")
307 << S;
308 // Draw the orthonormal base
309 board.drawArrow(0.0, 0.0, 1.0, 0.0);
310 board.drawArrow(0.0, 0.0, 0.0, 1.0);
311 // Draw M
312 board << SetMode(M.className(), "Both")
313 << CustomStyle( M.className(), new CustomColors( Color(255,0,0), Color(192, 0, 0)) )
314 << M;
315
316
317 // Save
318 board.saveSVG("NaiveDSS8ExtInit.svg");
319#ifdef WITH_CAIRO
320 board.saveCairo("NaiveDSS8ExtInit.png", Board2D::CairoPNG);
321#endif
322 }
323
324 // Extension -----------------------------------------------------
326 bool resExtention = S.extendFront( M );
328 //this segment should be extended:
329 if (!resExtention) throw std::exception();
330
331 trace.info() << S << std::endl;
332
333 //Display ------------------------------------------------------
334 {
335 Board2D board;
336
337 // Draw the grid
338 Domain domain( Point(0,0), M );
339 board << SetMode(domain.className(), "Grid")
340 << domain;
341 // Draw the points of the DSS and its bounding box
342 board << SetMode("PointVector", "Both");
343 board << SetMode(S.className(), "Points")
344 << S
345 << SetMode(S.className(), "BoundingBox")
346 << S;
347 // Draw the orthonormal base
348 board.drawArrow(0.0, 0.0, 1.0, 0.0);
349 board.drawArrow(0.0, 0.0, 0.0, 1.0);
350
351 // Save
352 board.saveSVG("NaiveDSS8ExtDone.svg");
353#ifdef WITH_CAIRO
354 board.saveCairo("NaiveDSS8ExtDone.png", Board2D::CairoPNG);
355#endif
356 }
357
358 // Retraction ----------------------------------------------------
360 bool resRetraction = S.retractFront();
362 //this segment should be retracted:
363 if (!resRetraction) throw std::exception();
364
365 trace.info() << S << std::endl;
366
367 // Comparaison ----------------------------------------------------
369 //this segment and the previous copy should be equal:
370 if ( !S.equalsTo(copyOfS) ) throw std::exception();
372
373 trace.endBlock();
374}
375
376
378int main( int argc, char** argv )
379{
380 trace.beginBlock ( "Example exampleArithmeticalDSS" );
381 trace.info() << "Args:";
382 for ( int i = 0; i < argc; ++i )
383 trace.info() << " " << argv[ i ];
384 trace.info() << endl;
385
389 exampleUpdate();
390
391 trace.endBlock();
392 return 0;
393}
394// //
std::string className() const
bool equalsTo(const ArithmeticalDSS &aOther) const
Position position(const Point &aPoint) const
Integer remainder(const Point &aPoint) const
ConstIterator begin() const
bool extendFront(const Point &aNewPoint)
ConstIterator end() const
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
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 8-co...
std::string className() const
Aim: This class represents a standard digital straight segment (DSS), ie. the sequence of simply 4-co...
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
void drawArrow(double x1, double y1, double x2, double y2, bool filled=true, int depthValue=-1)
Definition: Board.cpp:400
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1139
void exampleUpdate()
Function showing how a DSS can be extended and retracted.
void exampleNaiveDSS()
Function that illustrates the basic usage of a naive DSS.
void exampleStandardDSS()
Function that illustrates the basic usage of a standard DSS.
void exampleConstructors()
Function showing the different ways of constructing DSSs.
Space::Point Point
Definition: StdDefs.h:95
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
STL namespace.
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:279
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
int main()
Definition: testBits.cpp:56
Domain domain