DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testGaussDigitizer.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/io/boards/Board2D.h"
34 #include "DGtal/io/colormaps/GradientColorMap.h"
35 #include "DGtal/kernel/sets/DigitalSetSelector.h"
36 #include "DGtal/shapes/GaussDigitizer.h"
37 #include "DGtal/helpers/StdDefs.h"
38 #include "DGtal/shapes/parametric/Ellipse2D.h"
39 #include "DGtal/shapes/parametric/Flower2D.h"
40 #include "DGtal/shapes/Shapes.h"
41 #include "DGtal/topology/helpers/Surfaces.h"
42 #include "DGtal/geometry/curves/GridCurve.h"
43 #include "DGtal/shapes/CDigitalOrientedShape.h"
44 #include "DGtal/shapes/CDigitalBoundedShape.h"
45 
47 
48 using namespace std;
49 using namespace DGtal;
50 
52 // Functions for testing class GaussDigitizer.
54 
55 
62 bool testConcept()
63 {
65  BOOST_CONCEPT_ASSERT((CDigitalBoundedShape<Dig>));
66  BOOST_CONCEPT_ASSERT((CDigitalOrientedShape<Dig>));
67  return true;
68 }
69 
70 
71 template <typename Space, typename Shape>
72 bool
73 testDigitization( const Shape & aShape, double h,
74  const string & fileName )
75 {
76  typedef typename Space::Point Point;
77  typedef typename Space::RealPoint RealPoint;
79  typedef typename DigitalSetSelector
80  < Domain, BIG_DS + HIGH_ITER_DS + HIGH_BEL_DS >::Type MySet;
81 
83  // Creates a digitizer on the window (xLow, xUp).
84  RealPoint xLow( -5.3, -4.3 );
85  RealPoint xUp( 7.4, 4.7 );
87  dig.attach( aShape ); // attaches the shape.
88  dig.init( xLow, xUp, h );
89 
90  // The domain size is given by the digitizer according to the window
91  // and the step.
92  Domain domain = dig.getDomain();
93  MySet aSet( domain );
94  // Creates a set from the digitizer.
95  Shapes<Domain>::digitalShaper( aSet, dig );
97 
98  // Create cellular space
99  typedef Z2i::KSpace KSpace;
100  typedef Z2i::SCell SCell;
101  KSpace K;
102  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
103 
104  if (!ok)
105  return false;
106 
107 
109 
110 
111 
112  // Extracts shape boundary
113  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
114  // Getting the consecutive surfels of the 2D boundary
115  std::vector<Point> points;
116  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
117  GridCurve<KSpace> gridcurve;
118  gridcurve.initFromVector( points );
119 
120  // Display all
121  Board2D board;
123  board << SetMode( domain.className(), "Paving" )
124  << domain << aSet;
125 
126  board << SetMode( gridcurve.className(), "Edges" )
127  << CustomStyle( bel.className(),
128  new CustomColors( DGtal::Color( 0, 0, 0 ),
129  DGtal::Color( 0, 192, 0 ) ) )
130  << gridcurve;
131  board << SetMode( gridcurve.className(), "Points" )
132  << CustomStyle( bel.className(),
133  new CustomColors( DGtal::Color( 255, 0, 0 ),
134  DGtal::Color( 200, 0, 0 ) ) )
135  << gridcurve;
136 
137  board.saveEPS( ( fileName + ".eps" ).c_str() );
138  board.saveSVG( ( fileName + ".svg" ).c_str() );
139 
140  return true;
141 }
142 
147 bool testGaussDigitizer()
148 {
149  unsigned int nbok = 0;
150  unsigned int nb = 0;
151 
152  trace.beginBlock ( "Testing GaussDigitizer as a Digital Shape functor." );
153 
154  typedef Ellipse2D< Z2i::Space > MyEllipse;
155  MyEllipse ellipse( 1.2, 0.1, 4.0, 3.0, 0.3 );
156  nbok += testDigitization<Z2i::Space,MyEllipse>
157  ( ellipse, 1.0, "gauss-ellipse-1" ) ? 1 : 0;
158  nb++;
159  nbok += testDigitization<Z2i::Space,MyEllipse>
160  ( ellipse, 0.5, "gauss-ellipse-0_5" ) ? 1 : 0;
161  nb++;
162  nbok += testDigitization<Z2i::Space,MyEllipse>
163  ( ellipse, 0.25, "gauss-ellipse-0_25" ) ? 1 : 0;
164  nb++;
165 
166  typedef Flower2D< Z2i::Space > MyFlower;
167  MyFlower flower( 0.5, -2.3, 5.0, 0.7, 6, 0.3 );
168  nbok += testDigitization<Z2i::Space,MyFlower>
169  ( flower, 1.0, "gauss-flower-1" ) ? 1 : 0;
170  nb++;
171  nbok += testDigitization<Z2i::Space,MyFlower>
172  ( flower, 0.5, "gauss-flower-0_5" ) ? 1 : 0;
173  nb++;
174  nbok += testDigitization<Z2i::Space,MyFlower>
175  ( flower, 0.25, "gauss-flower-0_25" ) ? 1 : 0;
176  nb++;
177 
178  trace.info() << "(" << nbok << "/" << nb << ") "
179  << "true == true" << std::endl;
180  trace.endBlock();
181 
182  return nbok == nb;
183 }
184 
186 // Standard services - public :
187 
188 int main( int argc, char** argv )
189 {
190  trace.beginBlock ( "Testing class GaussDigitizer" );
191  trace.info() << "Args:";
192  for ( int i = 0; i < argc; ++i )
193  trace.info() << " " << argv[ i ];
194  trace.info() << endl;
195 
196  bool res = testConcept() && testGaussDigitizer(); // && ... other tests
197  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
198  trace.endBlock();
199  return res ? 0 : 1;
200 }
201 // //