DGtal 1.3.0
Loading...
Searching...
No Matches
testLengthEstimators.cpp
1
31#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
70using namespace std;
71using namespace DGtal;
72
73
75// Functions for testing Length Estimator classes.
77
78bool 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
171bool testDisplay(double radius, double h)
172{
173
174 // Types
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
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
286int 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// //
Aim: Best Linear Unbiased Two step length estimator.
Aim: Model of the concept StarShaped represents any circle in the plane.
Definition: Ball2D.h:61
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
Aim: a model of CGlobalCurveEstimator that segments the digital curve into DSS and computes the lengt...
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Aim: a model of CGlobalCurveEstimator that computes the length of a digital curve using its FP (faith...
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
Aim: A class for computing the Gauss digitization of some Euclidean shape, i.e. its intersection with...
const Point & getUpperBound() const
void attach(ConstAlias< EuclideanShape > shape)
const Point & getLowerBound() const
void init(const RealPoint &xLow, const RealPoint &xUp, typename RealVector::Component gridStep)
Domain getDomain() const
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:173
SCellsRange getSCellsRange() const
Definition: GridCurve.h:415
PointsRange getPointsRange() const
Definition: GridCurve.h:426
ArrowsRange getArrowsRange() const
Definition: GridCurve.h:450
bool initFromVector(const std::vector< Point > &aVectorOfPoints)
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Aim: a simple model of CGlobalCurveEstimator that compute the length of a curve using the l_1 metric ...
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h) const
Aim: a model of CGlobalCurveEstimator that computes the length of a digital curve using its MLP (give...
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
static void euclideanShaper(TDigitalSet &aSet, const TShapeFunctor &aFunctor, const double h=1.0)
TInteger Integer
Arithmetic ring induced by (+,-,*) and Integer numbers.
Definition: SpaceND.h:102
static void track2DBoundaryPoints(std::vector< Point > &aVectorOfPoints, const KSpace &K, const SurfelAdjacency< KSpace::dimension > &surfel_adj, const PointPredicate &pp, const SCell &start_surfel)
static SCell findABel(const KSpace &K, const PointPredicate &pp, unsigned int nbtries=1000)
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Aim: a simple model of CGlobalCurveEstimator that compute the length of a curve using the l_1 metric ...
Quantity eval(const ConstIterator &itb, const ConstIterator &ite, const double h=1.) const
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
Z3i::SCell SCell
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
STL namespace.
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
Astroid2D< Space > Shape
Z2i::RealPoint RealPoint
int main()
Definition: testBits.cpp:56
MyPointD Point
Definition: testClone2.cpp:383
KSpace K
bool testDisplay()
Domain domain
HyperRectDomain< Space > Domain