DGtal  1.2.0
exampleArithmeticalDSL.cpp
Go to the documentation of this file.
1 
41 #include <iostream>
42 #include <sstream>
43 #include "DGtal/base/Common.h"
44 #include "DGtal/helpers/StdDefs.h"
45 
46 #include "DGtal/io/boards/Board2D.h"
47 
49 #include "DGtal/geometry/curves/ArithmeticalDSL.h"
52 
53 using namespace std;
54 using namespace DGtal;
55 
57 
62 {
63  trace.beginBlock ( "Naive DSL" );
64 
65  using namespace Z2i;
66 
68  // Construct a naive DSL from a, b, mu
69  NaiveDSL<Integer> line( 2, 5, 0 );
71 
72  // Trace to the standard output
73  // the two representations
74  // corresponding to the same set of points
75  trace.info() << line << line.negate();
76 
77  // Construct a (rectangular) domain
78  Point bottomLeft( 0, 0 );
79  Point topRight( 10, 5 );
80  Domain domain( bottomLeft, topRight );
81 
82  // Display the DSL points within the domain with Board2D
83  Board2D board;
84 
85  Point firstPoint( bottomLeft[0], (line.a()*bottomLeft[0]/line.b()) );
86  Point lastPoint( topRight[0], (line.a()*topRight[0]/line.b()) );
87 
89  // Draw the DSL points between firstPoint and lastPoint
91  it = line.begin(firstPoint),
92  ite = line.end(lastPoint);
93  it != ite; ++it )
94  {
95  board << SetMode( it->className(), "Paving" )
96  << *it; //Draw the point
97  }
99 
100  // Draw the grid
101  board << SetMode(domain.className(), "Grid")
102  << domain;
103 
104  // Draw the orthonormal base
105  board.drawArrow(0.0, 0.0, 1.0, 0.0);
106  board.drawArrow(0.0, 0.0, 0.0, 1.0);
107 
108  // Save
109  board.saveSVG("NaiveDSL.svg");
110  #ifdef WITH_CAIRO
111  board.saveCairo("NaiveDSL.png", Board2D::CairoPNG);
112  #endif
113 
114  trace.endBlock();
115 }
116 
122 {
123  trace.beginBlock ( "Standard DSL" );
124 
125  using namespace Z2i;
126 
128  // Construct a standard DSL from a, b, mu
129  StandardDSL<Integer> line( 2, 5, 0 );
131 
132  // Trace to the standard output
133  // the two representations
134  // corresponding to the same set of points
135  trace.info() << line << line.negate();
136 
137  // Construct a (rectangular) domain
138  Point bottomLeft( 0, 0 );
139  Point topRight( 10, 5 );
140  Domain domain( bottomLeft, topRight );
141 
142  // Display the DSL points within the domain with Board2D
143  Board2D board;
144 
145  Point firstPoint( bottomLeft[0], (line.a()*bottomLeft[0]/line.b()) );
146  Point lastPoint( topRight[0], (line.a()*topRight[0]/line.b()) );
147 
148  // Draw the DSL points between firstPoint and lastPoint
150  it = line.begin(firstPoint),
151  ite = line.end(lastPoint);
152  it != ite; ++it )
153  {
154  board << SetMode( it->className(), "Paving" )
155  << *it; //Draw the point
156  }
157 
158  // Draw the grid
159  board << SetMode(domain.className(), "Grid")
160  << domain;
161 
162  // Draw the orthonormal base
163  board.drawArrow(0.0, 0.0, 1.0, 0.0);
164  board.drawArrow(0.0, 0.0, 0.0, 1.0);
165 
166  // Save
167  board.saveSVG("StandardDSL.svg");
168  #ifdef WITH_CAIRO
169  board.saveCairo("StandardDSL.png", Board2D::CairoPNG);
170  #endif
171 
172  trace.endBlock();
173 }
174 
184 template<typename DSL>
186  typename DSL::Integer b,
187  unsigned short octant,
188  unsigned int n)
189 {
190  std::stringstream ssTitle;
191  ssTitle << " Arithmetical DSL "
192  << "(" << a << ", " << b << ", 0)"
193  << " in octant " << octant;
194  trace.beginBlock ( ssTitle.str().c_str() );
195 
196  using namespace Z2i;
197 
198  // Construct the DSL to draw
199  DSL line( a, b, 0 );
200 
201  // Vectors to draw
202  Vector v = line.steps().first;
203  Vector w = line.steps().second;
204  Vector s = line.shift();
205 
206  // Display the DSL points within the domain with Board2D
207  Board2D board;
208 
209  // Draw the DSL points of the DSS
210  typename DSL::ConstIterator it;
211  it = line.begin( Point(0,0) );
212  unsigned int c;
213  for (c = 0; c < n; ++it, ++c )
214  {
215  Point p = *it;
216 
217  // Draw the point
218  board << SetMode( p.className(), "Paving" )
219  << p;
220  }
221 
222  // Construct the domain
223  Point topRight, bottomLeft;
224  Point firstPoint(0,0);
225  Point lastPoint = *it;
226  if (b > 0)
227  {
228  if (a > 0)
229  {
230  bottomLeft = firstPoint;
231  topRight = lastPoint;
232  }
233  else
234  {
235  bottomLeft = Point( firstPoint[0], lastPoint[1] );
236  topRight = Point( lastPoint[0], firstPoint[1] );
237  }
238  }
239  else
240  {
241  if (a > 0)
242  {
243  bottomLeft = Point( lastPoint[0], firstPoint[1] );
244  topRight = Point( firstPoint[0], lastPoint[1] );
245  }
246  else
247  {
248  bottomLeft = lastPoint;
249  topRight = firstPoint;
250  }
251  }
252  Domain domain( bottomLeft, topRight );
253 
254 
255  // Draw the vectors
256  it = line.begin( Point(0,0) );
257  for (c = 0; c < n; ++c )
258  {
259  Point p = *it;
260 
261  // Draw the shift vector
262  if ( line.remainder( p ) == line.mu() )
263  {
264  board.setPenColorRGBi(250, 0, 0);
265  board.drawArrow(p[0], p[1], p[0]+s[0], p[1]+s[1]);
266  }
267 
268  // Draw the steps
269  ++it;
270  if (c < (n-1))
271  {
272  Point q = *it;
273  if ( (q-p) == v )
274  board.setPenColorRGBi(0, 0, 250);
275  else if ( (q-p) == w )
276  board.setPenColorRGBi(0, 250, 0);
277  else
278  board.setPenColorRGBi(0, 0, 0);
279 
280  board.drawArrow(p[0], p[1], q[0], q[1]);
281  }
282  }
283 
284  // Draw the grid
285  board << SetMode(domain.className(), "Grid")
286  << domain;
287 
288  // Draw the orthonormal base
289  board.setPenColorRGBi(0, 0, 0);
290  board.drawArrow(0.0, 0.0, 1.0, 0.0);
291  board.drawArrow(0.0, 0.0, 0.0, 1.0);
292 
293  // Save
294  std::stringstream ssFileName;
295  ssFileName << "ArithmeticalDSL"
296  << "-" << DSL::foregroundAdjacency
297  << "-" << octant
298  << "-" << a << "-" << b
299  << ".png";
300  #ifdef WITH_CAIRO
301  board.saveCairo(ssFileName.str().c_str(), Board2D::CairoPNG);
302  #endif
303 
304  trace.endBlock();
305 
306 }
307 
314 {
315  using namespace Z2i;
316 
317  drawArithmeticalDSL<NaiveDSL<Integer> >( 0, 1, 0, 15 );
318  drawArithmeticalDSL<NaiveDSL<Integer> >( 5, 8, 0, 15 );
319  drawArithmeticalDSL<NaiveDSL<Integer> >( 1, 1, 1, 15 );
320  drawArithmeticalDSL<NaiveDSL<Integer> >( 8, 5, 1, 15 );
321  drawArithmeticalDSL<NaiveDSL<Integer> >( 1, 0, 2, 15 );
322  drawArithmeticalDSL<NaiveDSL<Integer> >( 8, -5, 2, 15 );
323  drawArithmeticalDSL<NaiveDSL<Integer> >( 1, -1, 3, 15 );
324  drawArithmeticalDSL<NaiveDSL<Integer> >( 5, -8, 3, 15 );
325  drawArithmeticalDSL<NaiveDSL<Integer> >( 0, -1, 4, 15 );
326  drawArithmeticalDSL<NaiveDSL<Integer> >( -5, -8, 4, 15 );
327  drawArithmeticalDSL<NaiveDSL<Integer> >( -1, -1, 5, 15 );
328  drawArithmeticalDSL<NaiveDSL<Integer> >( -8, -5, 5, 15 );
329  drawArithmeticalDSL<NaiveDSL<Integer> >( -1, 0, 6, 15 );
330  drawArithmeticalDSL<NaiveDSL<Integer> >( -8, 5, 6, 15 );
331  drawArithmeticalDSL<NaiveDSL<Integer> >( -1, 1, 7, 15 );
332  drawArithmeticalDSL<NaiveDSL<Integer> >( -5, 8, 7, 15 );
333 
334  drawArithmeticalDSL<StandardDSL<Integer> >( 0, 1, 0, 21 );
335  drawArithmeticalDSL<StandardDSL<Integer> >( 5, 8, 0, 21 );
336  drawArithmeticalDSL<StandardDSL<Integer> >( 1, 1, 1, 21 );
337  drawArithmeticalDSL<StandardDSL<Integer> >( 8, 5, 1, 21 );
338  drawArithmeticalDSL<StandardDSL<Integer> >( 1, 0, 2, 21 );
339  drawArithmeticalDSL<StandardDSL<Integer> >( 8, -5, 2, 21 );
340  drawArithmeticalDSL<StandardDSL<Integer> >( 1, -1, 3, 21 );
341  drawArithmeticalDSL<StandardDSL<Integer> >( 5, -8, 3, 21 );
342  drawArithmeticalDSL<StandardDSL<Integer> >( 0, -1, 4, 21 );
343  drawArithmeticalDSL<StandardDSL<Integer> >( -5, -8, 4, 21 );
344  drawArithmeticalDSL<StandardDSL<Integer> >( -1, -1, 5, 21 );
345  drawArithmeticalDSL<StandardDSL<Integer> >( -8, -5, 5, 21 );
346  drawArithmeticalDSL<StandardDSL<Integer> >( -1, 0, 6, 21 );
347  drawArithmeticalDSL<StandardDSL<Integer> >( -8, 5, 6, 21 );
348  drawArithmeticalDSL<StandardDSL<Integer> >( -1, 1, 7, 21 );
349  drawArithmeticalDSL<StandardDSL<Integer> >( -5, 8, 7, 21 );
350 
351 }
352 
357 {
358  trace.beginBlock ( "Naive DSLs" );
359 
360  // // Do not use the default type for the intercepts !
361  // NaiveDSL<DGtal::int16_t> line1( 17711, 28657, 1607895256 );
362  // //NB: 1 607 895 256 is not representable by the type DGtal::int16_t,
363  // //even if it is the remainder of the point (32 767,-32 767)
364  // //whose coordinates are representable by the type DGtal::int16_t
365  // trace.info() << line1; //KO
366 
368  NaiveDSL<DGtal::int16_t, DGtal::int32_t> line2( 17711, 28657, 1607895256 );
369  //NB: 1 607 895 256 is the remainder of the point (32 767,-32 767),
370  //whose coordinates are representable by the type DGtal::int16_t
371  trace.info() << line2 << line2.isValid(); //ok
373 
374  trace.endBlock();
375 }
376 
377 
384 int main( int argc, char** argv )
385 {
386  trace.beginBlock ( "Example exampleArithmeticalDSL" );
387  trace.info() << "Args:";
388  for ( int i = 0; i < argc; ++i )
389  trace.info() << " " << argv[ i ];
390  trace.info() << endl;
391 
392  exampleNaiveDSL();
396 
397  trace.endBlock();
398  return 0;
399 }
400 // //
ArithmeticalDSL negate() const
Coordinate b() const
ConstIterator begin(const Point &aPoint) const
ConstIterator end(const Point &aPoint) const
Coordinate a() const
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
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...
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition: Board.cpp:278
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 exampleStandardDSL()
Function that illustrates the basic usage of a standard DSL.
int main(int argc, char **argv)
Program that illustrates the basic usages of instances of ArithmeticalDSL.
void exampleNaiveDSL()
Function that illustrates the basic usage of a naive DSL.
void exampleArithmeticalDSLTypes()
Function that creates a naive DSL with different types.
void exampleArithmeticalDSLOctant()
Function that draws the steps and the shift vector of a naive and a standard DSL in each octant.
void drawArithmeticalDSL(typename DSL::Integer a, typename DSL::Integer b, unsigned short octant, unsigned int n)
Function that draws the steps and the shift vector of a DSL of slope a / b and intercept 0.
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
MyPointD Point
Definition: testClone2.cpp:383
FreemanChain< int >::Vector Vector
Domain domain