DGtal  0.9.3
testLengthEstimators.cpp
1 
30 #include <cmath>
32 #include "DGtal/base/Common.h"
33 
34 //space / domain
35 #include "DGtal/kernel/SpaceND.h"
36 #include "DGtal/kernel/domains/HyperRectDomain.h"
37 #include "DGtal/topology/KhalimskySpaceND.h"
38 
39 //shape and digitizer
40 #include "DGtal/shapes/ShapeFactory.h"
41 #include "DGtal/shapes/Shapes.h"
42 #include "DGtal/helpers/StdDefs.h"
43 #include "DGtal/topology/helpers/Surfaces.h"
44 
45 #include "DGtal/shapes/GaussDigitizer.h"
46 #include "DGtal/geometry/curves/GridCurve.h"
47 
48 //estimators
49 #include "DGtal/geometry/curves/estimation/TrueLocalEstimatorOnPoints.h"
50 #include "DGtal/geometry/curves/estimation/TrueGlobalEstimatorOnPoints.h"
51 #include "DGtal/geometry/curves/estimation/ParametricShapeCurvatureFunctor.h"
52 #include "DGtal/geometry/curves/estimation/ParametricShapeTangentFunctor.h"
53 #include "DGtal/geometry/curves/estimation/ParametricShapeArcLengthFunctor.h"
54 
55 #include "DGtal/geometry/curves/estimation/L1LengthEstimator.h"
56 #include "DGtal/geometry/curves/estimation/TwoStepLocalLengthEstimator.h"
57 #include "DGtal/geometry/curves/estimation/BLUELocalLengthEstimator.h"
58 #include "DGtal/geometry/curves/estimation/RosenProffittLocalLengthEstimator.h"
59 
60 #include "DGtal/geometry/curves/estimation/MLPLengthEstimator.h"
61 #include "DGtal/geometry/curves/estimation/FPLengthEstimator.h"
62 #include "DGtal/geometry/curves/estimation/DSSLengthEstimator.h"
63 
64 #include "ConfigTest.h"
65 
66 #include "DGtal/io/boards/Board2D.h"
67 
69 
70 using namespace std;
71 using namespace DGtal;
72 
73 
75 // Functions for testing Length Estimator classes.
77 
78 bool testLengthEstimatorsOnBall(double radius, double h)
79 {
80 
81  // Types
82  typedef SpaceND<2,int> Space;
83  typedef Ball2D<Space> Shape;
84  typedef Space::Point Point;
86  typedef Space::Integer Integer;
89  typedef KSpace::SCell SCell;
90  typedef GridCurve<KSpace>::PointsRange PointsRange;
91  typedef GridCurve<KSpace>::ArrowsRange ArrowsRange;
92 
93 
94  //Forme
95  Shape aShape(Point(0,0), radius);
96 
97  trace.info() << "#ball created, r=" << radius << endl;
98 
99  // Window for the estimation
100  RealPoint xLow ( -radius-1, -radius-1 );
101  RealPoint xUp( radius+1, radius+1 );
103  dig.attach( aShape ); // attaches the shape.
104  dig.init( xLow, xUp, h );
105  // The domain size is given by the digitizer according to the window
106  // and the step.
107  Domain domain = dig.getDomain();
108  // Create cellular space
109  KSpace K;
110  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
111  if ( ! ok )
112  {
113  std::cerr << " "
114  << " error in creating KSpace." << std::endl;
115  return false;
116  }
117 
119  SCell bel;
120  try
121  {
122  bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
123  }
124  catch ( InputException e )
125  {
126  std::cerr << " "
127  << " error in finding a bel." << std::endl;
128  return false;
129  }
130 
131  // Getting the consecutive surfels of the 2D boundary
132  std::vector<Point> points;
133  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
134  trace.info() << "#tracking..." << endl;
135  // Create GridCurve
136  GridCurve<KSpace> gridcurve;
137  gridcurve.initFromVector( points );
138  trace.info() << "#grid curve created, h=" << h << endl;
139 
140  //ranges
141  ArrowsRange ra = gridcurve.getArrowsRange();
142  PointsRange rp = gridcurve.getPointsRange();
143 
145  double trueValue = M_PI*2*radius;
147  l1length.init(h, ra.c(), ra.c());
149  locallength.init(h, ra.begin(), ra.end(), gridcurve.isClosed());
151  BLUElength.init(h, ra.begin(), ra.end(), gridcurve.isClosed());
153  RosenProffittlength.init(h, ra.begin(), ra.end(), gridcurve.isClosed());
155  DSSlength.init(h, rp.c(), rp.c());
156  // MLPLengthEstimator< GridCurve<KSpace>::PointsRange::ConstIterator > MLPlength;
157  // MLPlength.init(h, rp.begin(), rp.end(), gridcurve.isClosed());
158  // FPLengthEstimator< GridCurve<KSpace>::PointsRange::ConstIterator > FPlength;
159  // FPlength.init(h, rp.begin(), rp.end(), gridcurve.isClosed());
160 
161  trace.info() << "#Estimations" <<std::endl;
162  trace.info() << "#h true naive 1-sqrt(2) BLUE RosenProffitt DSS MLP FP " <<std::endl;
163  trace.info() << h << " " << trueValue
164  << " " << l1length.eval()
165  << " " << locallength.eval()
166  << " " << BLUElength.eval()
167  << " " << RosenProffittlength.eval()
168  << " " << DSSlength.eval()
169  // << " " << MLPlength.eval()
170  // << " " << FPlength.eval()
171  << std::endl;
172 
173 
174 
175  return true;
176 }
177 
178 
179 
180 bool testDisplay(double radius, double h)
181 {
182 
183  // Types
184  typedef Ball2D<Z2i::Space> Shape;
185  typedef Z2i::Space::Point Point;
186  typedef Z2i::Space::RealPoint RealPoint;
187  typedef Z2i::Space::Integer Integer;
190  typedef KSpace::SCell SCell;
191  typedef GridCurve<KSpace>::PointsRange PointsRange;
192  typedef GridCurve<KSpace>::ArrowsRange ArrowsRange;
193  typedef GridCurve<KSpace>::SCellsRange SCellsRange;
194 
195 
196  //Forme
197  Shape aShape(Point(0,0), radius);
198 
199  trace.info() << "#ball created, r=" << radius << endl;
200 
201  // Window for the estimation
202  RealPoint xLow ( -radius-1, -radius-1 );
203  RealPoint xUp( radius+1, radius+1 );
205  dig.attach( aShape ); // attaches the shape.
206  dig.init( xLow, xUp, h );
207  // The domain size is given by the digitizer according to the window
208  // and the step.
209  Domain domain = dig.getDomain();
210  // Create cellular space
211  KSpace K;
212  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
213  if ( ! ok )
214  {
215  std::cerr << " "
216  << " error in creating KSpace." << std::endl;
217  return false;
218  }
219  try {
220 
221  // Extracts shape boundary
223  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
224  // Getting the consecutive surfels of the 2D boundary
225  std::vector<Point> points;
226  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
227  trace.info() << "# tracking..." << endl;
228  // Create GridCurve
229  GridCurve<KSpace> gridcurve;
230  gridcurve.initFromVector( points );
231  trace.info() << "#grid curve created, h=" << h << endl;
232 
233  //ranges
234  ArrowsRange ra = gridcurve.getArrowsRange();
235  PointsRange rp = gridcurve.getPointsRange();
236  SCellsRange rc = gridcurve.getSCellsRange();
237 
238  //Explicit reshaping for drawing purposes
239  Z2i::DigitalSet set(domain);
240  Shapes<Z2i::Domain>::euclideanShaper( set, Shape(Point(0,0), radius/h));
241 
242  Board2D board;
243 
244  board << domain << set;
245  board.saveSVG( "Ranges-Set.svg" );
246 
247  board.clear();
248  board << domain;
249  board << rp;
250  // for( PointsRange::ConstIterator it =rp.begin(), ite=rp.end();
251  // it != ite; ++it)
252  // board << (*it);
253  board.saveSVG( "Ranges-Points.svg" );
254 
255 
256  board.clear();
257  board << domain;
258  board << rc;
259  // for( SCellsRange::ConstIterator it =rc.begin(), ite=rc.end();
260  // it != ite; ++it)
261  // board << (*it);
262  board.saveSVG( "Ranges-SCells.svg" );
263 
264 
265  board.clear();
266  board << domain;
267  board << ra;
268  // Z2i::Space::Vector shift;
269  // board.setPenColor( Color::Black );
270  // for( ArrowsRange::ConstIterator it = ra.begin(), itend = ra.end();
271  // it != itend;
272  // ++it)
273  // {
274  // shift = (*it).second ;
275  // draw(board, shift, (*it).first );
276  // }
277  board.saveSVG( "Ranges-Arrows.svg" );
278 
279  }
280  catch ( InputException e )
281  {
282  std::cerr << " "
283  << " error in finding a bel." << std::endl;
284  return false;
285  }
286 
287 
288 
289  return true;
290 }
291 
293 // Standard services - public :
294 
295 int main( int argc, char** argv )
296 {
297  trace.beginBlock ( "Testing class LengthEstimators" );
298  trace.info() << "Args:";
299  for ( int i = 0; i < argc; ++i )
300  trace.info() << " " << argv[ i ];
301  trace.info() << endl;
302 
303  double r = 5;
304  bool res = testLengthEstimatorsOnBall(r,1)
305  && testLengthEstimatorsOnBall(r,0.1)
306  && testLengthEstimatorsOnBall(r,0.01)
307  && testLengthEstimatorsOnBall(r,0.001)
308  && testLengthEstimatorsOnBall(r,0.0001)
309  && testDisplay(r,0.9);
310  ;
311 
312  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
313  trace.endBlock();
314  return res ? 0 : 1;
315 }
316 // //
void beginBlock(const std::string &keyword="")
DGtal::int32_t Integer
Definition: StdDefs.h:74
HyperRectDomain< Space > Domain
const Domain domain(Point(1, 2), Point(6, 5))
const Point & getLowerBound() const
Trace trace
Definition: Common.h:137
Quantity eval() const
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
const Point & getUpperBound() const
Aim: a simple model of CGlobalCurveEstimator that compute the length of a curve using the l_1 metric ...
KhalimskySpaceND< 2, Integer > KSpace
Definition: StdDefs.h:77
STL namespace.
double endBlock()
void attach(const EuclideanShape &shape)
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
Aim: Best Linear Unbiased Two step length estimator.
PointsRange getPointsRange() const
Definition: GridCurve.h:426
SCellsRange getSCellsRange() const
Definition: GridCurve.h:415
Aim: Model of the concept StarShaped represents any circle in the plane.
Definition: Ball2D.h:60
bool initFromVector(const std::vector< Point > &aVectorOfPoints)
void init(const RealPoint &xLow, const RealPoint &xUp, typename RealVector::Component gridStep)
bool testDisplay()
std::ostream & emphase()
Aim: A class for computing the Gauss digitization of some Euclidean shape, i.e. its intersection with...
void init(const double h, const ConstIterator &itb, const ConstIterator &ite)
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
void init(const double h, const ConstIterator &itb, const ConstIterator &ite)
TInteger Integer
Arithmetic ring induced by (+,-,*) and Integer numbers.
Definition: SpaceND.h:102
Aim: a model of CGlobalCurveEstimator that segments the digital curve into DSS and computes the lengt...
int main(int argc, char **argv)
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
bool isClosed() const
std::ostream & info()
Quantity eval() const
void init(const double h, const ConstIterator &itb, const ConstIterator &ite, const bool &isClosed)
Aim: a simple model of CGlobalCurveEstimator that compute the length of a curve using the l_1 metric ...
Space::RealPoint RealPoint
Definition: StdDefs.h:97
KSpace K
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:172
Aim: A utility class for constructing different shapes (balls, diamonds, and others).
Domain getDomain() const
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
Aim: This class specializes a &#39;Board&#39; class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70
ArrowsRange getArrowsRange() const
Definition: GridCurve.h:450