DGtal  0.9.2
exampleArithmeticalDSL.cpp
1
30 #include <iostream>
32 #include <sstream>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/helpers/StdDefs.h"
35
36 #include "DGtal/io/boards/Board2D.h"
37
39 #include "DGtal/geometry/curves/ArithmeticalDSL.h"
42
43 using namespace std;
44 using namespace DGtal;
45
47
51 void exampleNaiveDSL()
52 {
53  trace.beginBlock ( "Naive DSL" );
54
55  using namespace Z2i;
56
58  // Construct a naive DSL from a, b, mu
59  NaiveDSL<Integer> line( 2, 5, 0 );
61
62  // Trace to the standard output
63  // the two representations
64  // corresponding to the same set of points
65  trace.info() << line << line.negate();
66
67  // Construct a (rectangular) domain
68  Point bottomLeft( 0, 0 );
69  Point topRight( 10, 5 );
70  Domain domain( bottomLeft, topRight );
71
72  // Display the DSL points within the domain with Board2D
73  Board2D board;
74
75  Point firstPoint( bottomLeft[0], (line.a()*bottomLeft[0]/line.b()) );
76  Point lastPoint( topRight[0], (line.a()*topRight[0]/line.b()) );
77
79  // Draw the DSL points between firstPoint and lastPoint
81  it = line.begin(firstPoint),
82  ite = line.end(lastPoint);
83  it != ite; ++it )
84  {
85  board << SetMode( it->className(), "Paving" )
86  << *it; //Draw the point
87  }
89
90  // Draw the grid
91  board << SetMode(domain.className(), "Grid")
92  << domain;
93
94  // Draw the orthonormal base
95  board.drawArrow(0.0, 0.0, 1.0, 0.0);
96  board.drawArrow(0.0, 0.0, 0.0, 1.0);
97
98  // Save
99  board.saveSVG("NaiveDSL.svg");
100  #ifdef WITH_CAIRO
101  board.saveCairo("NaiveDSL.png", Board2D::CairoPNG);
102  #endif
103
104  trace.endBlock();
105 }
106
111 void exampleStandardDSL()
112 {
113  trace.beginBlock ( "Standard DSL" );
114
115  using namespace Z2i;
116
118  // Construct a standard DSL from a, b, mu
119  StandardDSL<Integer> line( 2, 5, 0 );
121
122  // Trace to the standard output
123  // the two representations
124  // corresponding to the same set of points
125  trace.info() << line << line.negate();
126
127  // Construct a (rectangular) domain
128  Point bottomLeft( 0, 0 );
129  Point topRight( 10, 5 );
130  Domain domain( bottomLeft, topRight );
131
132  // Display the DSL points within the domain with Board2D
133  Board2D board;
134
135  Point firstPoint( bottomLeft[0], (line.a()*bottomLeft[0]/line.b()) );
136  Point lastPoint( topRight[0], (line.a()*topRight[0]/line.b()) );
137
138  // Draw the DSL points between firstPoint and lastPoint
140  it = line.begin(firstPoint),
141  ite = line.end(lastPoint);
142  it != ite; ++it )
143  {
144  board << SetMode( it->className(), "Paving" )
145  << *it; //Draw the point
146  }
147
148  // Draw the grid
149  board << SetMode(domain.className(), "Grid")
150  << domain;
151
152  // Draw the orthonormal base
153  board.drawArrow(0.0, 0.0, 1.0, 0.0);
154  board.drawArrow(0.0, 0.0, 0.0, 1.0);
155
156  // Save
157  board.saveSVG("StandardDSL.svg");
158  #ifdef WITH_CAIRO
159  board.saveCairo("StandardDSL.png", Board2D::CairoPNG);
160  #endif
161
162  trace.endBlock();
163 }
164
174 template<typename DSL>
175 void drawArithmeticalDSL(typename DSL::Integer a,
176  typename DSL::Integer b,
177  unsigned short octant,
178  unsigned int n)
179 {
180  std::stringstream ssTitle;
181  ssTitle << " Arithmetical DSL "
182  << "(" << a << ", " << b << ", 0)"
183  << " in octant " << octant;
184  trace.beginBlock ( ssTitle.str().c_str() );
185
186  using namespace Z2i;
187
188  // Construct the DSL to draw
189  DSL line( a, b, 0 );
190
191  // Vectors to draw
192  Vector v = line.steps().first;
193  Vector w = line.steps().second;
194  Vector s = line.shift();
195
196  // Display the DSL points within the domain with Board2D
197  Board2D board;
198
199  // Draw the DSL points of the DSS
200  typename DSL::ConstIterator it;
201  it = line.begin( Point(0,0) );
202  unsigned int c;
203  for (c = 0; c < n; ++it, ++c )
204  {
205  Point p = *it;
206
207  // Draw the point
208  board << SetMode( p.className(), "Paving" )
209  << p;
210  }
211
212  // Construct the domain
213  Point topRight, bottomLeft;
214  Point firstPoint(0,0);
215  Point lastPoint = *it;
216  if (b > 0)
217  {
218  if (a > 0)
219  {
220  bottomLeft = firstPoint;
221  topRight = lastPoint;
222  }
223  else
224  {
225  bottomLeft = Point( firstPoint[0], lastPoint[1] );
226  topRight = Point( lastPoint[0], firstPoint[1] );
227  }
228  }
229  else
230  {
231  if (a > 0)
232  {
233  bottomLeft = Point( lastPoint[0], firstPoint[1] );
234  topRight = Point( firstPoint[0], lastPoint[1] );
235  }
236  else
237  {
238  bottomLeft = lastPoint;
239  topRight = firstPoint;
240  }
241  }
242  Domain domain( bottomLeft, topRight );
243
244
245  // Draw the vectors
246  it = line.begin( Point(0,0) );
247  for (c = 0; c < n; ++c )
248  {
249  Point p = *it;
250
251  // Draw the shift vector
252  if ( line.remainder( p ) == line.mu() )
253  {
254  board.setPenColorRGBi(250, 0, 0);
255  board.drawArrow(p[0], p[1], p[0]+s[0], p[1]+s[1]);
256  }
257
258  // Draw the steps
259  ++it;
260  if (c < (n-1))
261  {
262  Point q = *it;
263  if ( (q-p) == v )
264  board.setPenColorRGBi(0, 0, 250);
265  else if ( (q-p) == w )
266  board.setPenColorRGBi(0, 250, 0);
267  else
268  board.setPenColorRGBi(0, 0, 0);
269
270  board.drawArrow(p[0], p[1], q[0], q[1]);
271  }
272  }
273
274  // Draw the grid
275  board << SetMode(domain.className(), "Grid")
276  << domain;
277
278  // Draw the orthonormal base
279  board.setPenColorRGBi(0, 0, 0);
280  board.drawArrow(0.0, 0.0, 1.0, 0.0);
281  board.drawArrow(0.0, 0.0, 0.0, 1.0);
282
283  // Save
284  std::stringstream ssFileName;
285  ssFileName << "ArithmeticalDSL"
287  << "-" << octant
288  << "-" << a << "-" << b
289  << ".png";
290  #ifdef WITH_CAIRO
291  board.saveCairo(ssFileName.str().c_str(), Board2D::CairoPNG);
292  #endif
293
294  trace.endBlock();
295
296 }
297
303 void exampleArithmeticalDSLOctant()
304 {
305  using namespace Z2i;
306
307  drawArithmeticalDSL<NaiveDSL<Integer> >( 0, 1, 0, 15 );
308  drawArithmeticalDSL<NaiveDSL<Integer> >( 5, 8, 0, 15 );
309  drawArithmeticalDSL<NaiveDSL<Integer> >( 1, 1, 1, 15 );
310  drawArithmeticalDSL<NaiveDSL<Integer> >( 8, 5, 1, 15 );
311  drawArithmeticalDSL<NaiveDSL<Integer> >( 1, 0, 2, 15 );
312  drawArithmeticalDSL<NaiveDSL<Integer> >( 8, -5, 2, 15 );
313  drawArithmeticalDSL<NaiveDSL<Integer> >( 1, -1, 3, 15 );
314  drawArithmeticalDSL<NaiveDSL<Integer> >( 5, -8, 3, 15 );
315  drawArithmeticalDSL<NaiveDSL<Integer> >( 0, -1, 4, 15 );
316  drawArithmeticalDSL<NaiveDSL<Integer> >( -5, -8, 4, 15 );
317  drawArithmeticalDSL<NaiveDSL<Integer> >( -1, -1, 5, 15 );
318  drawArithmeticalDSL<NaiveDSL<Integer> >( -8, -5, 5, 15 );
319  drawArithmeticalDSL<NaiveDSL<Integer> >( -1, 0, 6, 15 );
320  drawArithmeticalDSL<NaiveDSL<Integer> >( -8, 5, 6, 15 );
321  drawArithmeticalDSL<NaiveDSL<Integer> >( -1, 1, 7, 15 );
322  drawArithmeticalDSL<NaiveDSL<Integer> >( -5, 8, 7, 15 );
323
324  drawArithmeticalDSL<StandardDSL<Integer> >( 0, 1, 0, 21 );
325  drawArithmeticalDSL<StandardDSL<Integer> >( 5, 8, 0, 21 );
326  drawArithmeticalDSL<StandardDSL<Integer> >( 1, 1, 1, 21 );
327  drawArithmeticalDSL<StandardDSL<Integer> >( 8, 5, 1, 21 );
328  drawArithmeticalDSL<StandardDSL<Integer> >( 1, 0, 2, 21 );
329  drawArithmeticalDSL<StandardDSL<Integer> >( 8, -5, 2, 21 );
330  drawArithmeticalDSL<StandardDSL<Integer> >( 1, -1, 3, 21 );
331  drawArithmeticalDSL<StandardDSL<Integer> >( 5, -8, 3, 21 );
332  drawArithmeticalDSL<StandardDSL<Integer> >( 0, -1, 4, 21 );
333  drawArithmeticalDSL<StandardDSL<Integer> >( -5, -8, 4, 21 );
334  drawArithmeticalDSL<StandardDSL<Integer> >( -1, -1, 5, 21 );
335  drawArithmeticalDSL<StandardDSL<Integer> >( -8, -5, 5, 21 );
336  drawArithmeticalDSL<StandardDSL<Integer> >( -1, 0, 6, 21 );
337  drawArithmeticalDSL<StandardDSL<Integer> >( -8, 5, 6, 21 );
338  drawArithmeticalDSL<StandardDSL<Integer> >( -1, 1, 7, 21 );
339  drawArithmeticalDSL<StandardDSL<Integer> >( -5, 8, 7, 21 );
340
341 }
342
346 void exampleArithmeticalDSLTypes()
347 {
348  trace.beginBlock ( "Naive DSLs" );
349
350  // // Do not use the default type for the intercepts !
351  // NaiveDSL<DGtal::int16_t> line1( 17711, 28657, 1607895256 );
352  // //NB: 1 607 895 256 is not representable by the type DGtal::int16_t,
353  // //even if it is the remainder of the point (32 767,-32 767)
354  // //whose coordinates are representable by the type DGtal::int16_t
355  // trace.info() << line1; //KO
356
358  NaiveDSL<DGtal::int16_t, DGtal::int32_t> line2( 17711, 28657, 1607895256 );
359  //NB: 1 607 895 256 is the remainder of the point (32 767,-32 767),
360  //whose coordinates are representable by the type DGtal::int16_t
361  trace.info() << line2 << line2.isValid(); //ok
363
364  trace.endBlock();
365 }
366
367
374 int main( int argc, char** argv )
375 {
376  trace.beginBlock ( "Example exampleArithmeticalDSL" );
377  trace.info() << "Args:";
378  for ( int i = 0; i < argc; ++i )
379  trace.info() << " " << argv[ i ];
380  trace.info() << endl;
381
382  exampleNaiveDSL();
383  exampleStandardDSL();
384  exampleArithmeticalDSLOctant();
385  exampleArithmeticalDSLTypes();
386
387  trace.endBlock();
388  return 0;
389 }
390 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
ConstIterator begin(const Point &aPoint) 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
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 is an alias of ArithmeticalDSS for standard DSL. It represents a standard digital str...
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
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition: Board.cpp:278
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70