DGtal  1.3.beta
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;
153 
154  trace.info() << "#Estimations" <<std::endl;
155  trace.info() << "#h true naive 1-sqrt(2) BLUE RosenProffitt DSS MLP FP " <<std::endl;
156  trace.info() << h << " " << trueValue
157  << " " << l1length.eval(rp.c(), rp.c(), h)
158  << " " << locallength.eval(ra.begin(), ra.end(), h)
159  << " " << BLUElength.eval(ra.begin(), ra.end(), h)
160  << " " << RosenProffittlength.eval(ra.begin(), ra.end(), h)
161  << " " << DSSlength.eval(rp.c(), rp.c(), h)
162  << " " << MLPlength.eval(rp.c(), rp.c(), h)
163  << " " << FPlength.eval(rp.c(), rp.c(), h)
164  << std::endl;
165 
166  return true;
167 }
168 
169 
170 
171 bool testDisplay(double radius, double h)
172 {
173 
174  // Types
175  typedef Ball2D<Z2i::Space> Shape;
176  typedef Z2i::Space::Point Point;
181  typedef KSpace::SCell SCell;
182  typedef GridCurve<KSpace>::PointsRange PointsRange;
183  typedef GridCurve<KSpace>::ArrowsRange ArrowsRange;
184  typedef GridCurve<KSpace>::SCellsRange SCellsRange;
185 
186 
187  //Forme
188  Shape aShape(Point(0,0), radius);
189 
190  trace.info() << "#ball created, r=" << radius << endl;
191 
192  // Window for the estimation
193  RealPoint xLow ( -radius-1, -radius-1 );
194  RealPoint xUp( radius+1, radius+1 );
196  dig.attach( aShape ); // attaches the shape.
197  dig.init( xLow, xUp, h );
198  // The domain size is given by the digitizer according to the window
199  // and the step.
200  Domain domain = dig.getDomain();
201  // Create cellular space
202  KSpace K;
203  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
204  if ( ! ok )
205  {
206  std::cerr << " "
207  << " error in creating KSpace." << std::endl;
208  return false;
209  }
210  try {
211 
212  // Extracts shape boundary
214  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
215  // Getting the consecutive surfels of the 2D boundary
216  std::vector<Point> points;
217  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
218  trace.info() << "# tracking..." << endl;
219  // Create GridCurve
220  GridCurve<KSpace> gridcurve;
221  gridcurve.initFromVector( points );
222  trace.info() << "#grid curve created, h=" << h << endl;
223 
224  //ranges
225  ArrowsRange ra = gridcurve.getArrowsRange();
226  PointsRange rp = gridcurve.getPointsRange();
227  SCellsRange rc = gridcurve.getSCellsRange();
228 
229  //Explicit reshaping for drawing purposes
230  Z2i::DigitalSet set(domain);
231  Shapes<Z2i::Domain>::euclideanShaper( set, Shape(Point(0,0), radius/h));
232 
233  Board2D board;
234 
235  board << domain << set;
236  board.saveSVG( "Ranges-Set.svg" );
237 
238  board.clear();
239  board << domain;
240  board << rp;
241  // for( PointsRange::ConstIterator it =rp.begin(), ite=rp.end();
242  // it != ite; ++it)
243  // board << (*it);
244  board.saveSVG( "Ranges-Points.svg" );
245 
246 
247  board.clear();
248  board << domain;
249  board << rc;
250  // for( SCellsRange::ConstIterator it =rc.begin(), ite=rc.end();
251  // it != ite; ++it)
252  // board << (*it);
253  board.saveSVG( "Ranges-SCells.svg" );
254 
255 
256  board.clear();
257  board << domain;
258  board << ra;
259  // Z2i::Space::Vector shift;
260  // board.setPenColor( Color::Black );
261  // for( ArrowsRange::ConstIterator it = ra.begin(), itend = ra.end();
262  // it != itend;
263  // ++it)
264  // {
265  // shift = (*it).second ;
266  // draw(board, shift, (*it).first );
267  // }
268  board.saveSVG( "Ranges-Arrows.svg" );
269 
270  }
271  catch ( InputException& e )
272  {
273  std::cerr << " "
274  << " error in finding a bel." << std::endl;
275  return false;
276  }
277 
278 
279 
280  return true;
281 }
282 
284 // Standard services - public :
285 
286 int main( int argc, char** argv )
287 {
288  trace.beginBlock ( "Testing class LengthEstimators" );
289  trace.info() << "Args:";
290  for ( int i = 0; i < argc; ++i )
291  trace.info() << " " << argv[ i ];
292  trace.info() << endl;
293 
294  double r = 5;
295  bool res = testLengthEstimatorsOnBall(r,1)
296  && testLengthEstimatorsOnBall(r,0.1)
297  && testLengthEstimatorsOnBall(r,0.01)
298  && testLengthEstimatorsOnBall(r,0.001)
299  && testDisplay(r,0.9);
300  ;
301 
302  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
303  trace.endBlock();
304  return res ? 0 : 1;
305 }
306 // //
DGtal::GridCurve::getSCellsRange
SCellsRange getSCellsRange() const
Definition: GridCurve.h:415
DGtal::GaussDigitizer
Aim: A class for computing the Gauss digitization of some Euclidean shape, i.e. its intersection with...
Definition: GaussDigitizer.h:79
DGtal::Surfaces
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
DGtal::GaussDigitizer::getDomain
Domain getDomain() const
DGtal::Astroid2D< Space >
DGtal::FPLengthEstimator::eval
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
DGtal::GridCurve::initFromVector
bool initFromVector(const std::vector< Point > &aVectorOfPoints)
DGtal::HyperRectDomain< Space >
DGtal::Trace::endBlock
double endBlock()
DGtal::L1LengthEstimator
Aim: a simple model of CGlobalCurveEstimator that compute the length of a curve using the l_1 metric ...
Definition: L1LengthEstimator.h:67
DGtal::GaussDigitizer::getUpperBound
const Point & getUpperBound() const
DGtal::ConstRangeAdapter
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
Definition: ConstRangeAdapter.h:86
DGtal::SurfelAdjacency< KSpace::dimension >
DGtal::KhalimskySpaceND::init
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::GaussDigitizer::attach
void attach(ConstAlias< EuclideanShape > shape)
K
KSpace K
Definition: testCubicalComplex.cpp:62
DGtal::GridCurve
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:172
testDisplay
bool testDisplay()
Definition: testFreemanChain.cpp:518
DGtal::TwoStepLocalLengthEstimator
Aim: a simple model of CGlobalCurveEstimator that compute the length of a curve using the l_1 metric ...
Definition: TwoStepLocalLengthEstimator.h:65
DGtal::L1LengthEstimator::eval
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h) const
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::MLPLengthEstimator
Aim: a model of CGlobalCurveEstimator that computes the length of a digital curve using its MLP (give...
Definition: MLPLengthEstimator.h:69
SCell
Z3i::SCell SCell
Definition: fullConvexityShortestPaths3D.cpp:83
LibBoard::Board::clear
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
DGtal::SignedKhalimskyCell
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
Definition: KhalimskySpaceND.h:208
DGtal::MLPLengthEstimator::eval
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
DGtal::Ball2D
Aim: Model of the concept StarShaped represents any circle in the plane.
Definition: Ball2D.h:60
DGtal::SpaceND
Definition: SpaceND.h:95
DGtal::GaussDigitizer::init
void init(const RealPoint &xLow, const RealPoint &xUp, typename RealVector::Component gridStep)
KSpace
Z3i::KSpace KSpace
Definition: testArithmeticalDSSComputerOnSurfels.cpp:48
DGtal::BLUELocalLengthEstimator
Aim: Best Linear Unbiased Two step length estimator.
Definition: BLUELocalLengthEstimator.h:69
DGtal::Trace::info
std::ostream & info()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Domain
HyperRectDomain< Space > Domain
Definition: testSimpleRandomAccessRangeFromPoint.cpp:44
DGtal::GridCurve::getArrowsRange
ArrowsRange getArrowsRange() const
Definition: GridCurve.h:450
DGtal::DSSLengthEstimator
Aim: a model of CGlobalCurveEstimator that segments the digital curve into DSS and computes the lengt...
Definition: DSSLengthEstimator.h:76
DGtal::RosenProffittLocalLengthEstimator
Aim: Rosen-Proffitt Length Estimator.
Definition: RosenProffittLocalLengthEstimator.h:69
LibBoard::Board::saveSVG
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
Shape
Astroid2D< Space > Shape
Definition: testAstroid2D.cpp:48
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
DGtal::TwoStepLocalLengthEstimator::eval
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
Integer
Point::Coordinate Integer
Definition: examplePlaneProbingParallelepipedEstimator.cpp:44
DGtal::DSSLengthEstimator::eval
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
DGtal::Board2D
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:70
DGtal::Shapes
Aim: A utility class for constructing different shapes (balls, diamonds, and others).
Definition: DGtal/shapes/Shapes.h:71
domain
Domain domain
Definition: testProjection.cpp:88
DGtal::PointVector< dim, Integer >
DGtal::FPLengthEstimator
Aim: a model of CGlobalCurveEstimator that computes the length of a digital curve using its FP (faith...
Definition: FPLengthEstimator.h:69
Space
SpaceND< 2 > Space
Definition: testSimpleRandomAccessRangeFromPoint.cpp:42
Point
MyPointD Point
Definition: testClone2.cpp:383
RealPoint
Z2i::RealPoint RealPoint
Definition: testAstroid2D.cpp:46
DGtal::SpaceND::Integer
TInteger Integer
Arithmetic ring induced by (+,-,*) and Integer numbers.
Definition: SpaceND.h:102
DGtal::GaussDigitizer::getLowerBound
const Point & getLowerBound() const
DGtal::GridCurve::getPointsRange
PointsRange getPointsRange() const
Definition: GridCurve.h:426
DGtal::InputException
Definition: Exceptions.h:63
DGtal::DigitalSetByAssociativeContainer
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Definition: DigitalSetByAssociativeContainer.h:89
DGtal::KhalimskySpaceND
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:64