DGtal  1.3.beta
testTrueLocalEstimator.cpp
Go to the documentation of this file.
1 
32 #include <iostream>
34 #include "DGtal/base/Common.h"
35 #include "DGtal/kernel/SpaceND.h"
36 #include "DGtal/kernel/domains/HyperRectDomain.h"
37 #include "DGtal/kernel/sets/DigitalSetSelector.h"
38 #include "DGtal/topology/KhalimskySpaceND.h"
39 #include "DGtal/topology/SurfelAdjacency.h"
40 #include "DGtal/topology/SurfelNeighborhood.h"
41 
42 #include "DGtal/shapes/Shapes.h"
43 #include "DGtal/shapes/ShapeFactory.h"
44 #include "DGtal/shapes/GaussDigitizer.h"
45 
46 #include "DGtal/geometry/curves/GridCurve.h"
47 #include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
48 #include "DGtal/geometry/curves/estimation/CCurveLocalGeometricEstimator.h"
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 #include "DGtal/geometry/curves/estimation/MostCenteredMaximalSegmentEstimator.h"
55 #include "DGtal/geometry/curves/estimation/CompareLocalEstimators.h"
56 
57 
58 #include "ConfigTest.h"
59 
61 
62 using namespace std;
63 using namespace DGtal;
64 
66 // Functions for testing class TrueLocalEstimator.
68 
72 bool testTrueLocalEstimator(const std::string &filename)
73 {
74  trace.info() << "Reading GridCurve " << endl;
75  ifstream instream; // input stream
76  instream.open (filename.c_str(), ifstream::in);
77  typedef KhalimskySpaceND<2> Kspace; //space
79  c.initFromVectorStream(instream); //building grid curve
81  Range r = c.getPointsRange();//building range
82 
83  typedef Ball2D<Z2i::Space> Shape;
84  typedef GridCurve<KhalimskySpaceND<2> >::PointsRange Range;
85  typedef Range::ConstIterator ConstIteratorOnPoints;
89 
90  Shape ball(Z2i::Point(0,0), 30);
91 
92 
96 
99  TrueCurvatureEstimator curvatureEstimator;
100  TrueTangentEstimator tangentEstimator;
101  TrueLengthEstimator lengthEstimator;
102 
103  curvatureEstimator.attach( ball );
104  tangentEstimator.attach( ball );
105  lengthEstimator.attach ( ball );
106 
107  ConstIteratorOnPoints it = r.begin();
108  ConstIteratorOnPoints it2 = it+15;
109 
110  trace.info() << "Current point = " << *it << std::endl;
111  trace.info() << "Current point+15 = " << *it2 << std::endl;
112  // TODO: All these estimations consider projections of points from grid curve c on ball.
113  // These are not estimations on the grid curve c. Not sure that make sense.
114  trace.info() << "Eval curvature (begin, h=1) = " << curvatureEstimator.eval(it2, 1.) << std::endl;
115  trace.info() << "Eval tangent (begin, h=1) = " << tangentEstimator.eval(it2, 1.) << std::endl;
116  trace.info() << "Eval length (h=1) = " << lengthEstimator.eval(it, it2, 1.) << std::endl;
117  trace.info() << "Eval ball length (h=1) = " << lengthEstimator.eval() << std::endl;
118 
119  return true;
120 }
121 
122 template <typename Shape>
123 bool
125  Shape & aShape, double h )
126 {
127  using namespace Z2i;
128 
129  trace.beginBlock ( ( "Testing TrueLocalEstimator on digitization of "
130  + name ). c_str() );
131 
132  // Creates a digitizer on the window (xLow, xUp).
133  typedef Space::RealPoint RealPoint;
134  RealPoint xLow( -10.0, -10.0 );
135  RealPoint xUp( 10.0, 10.0 );
137  dig.attach( aShape ); // attaches the shape.
138  dig.init( xLow, xUp, h );
139 
140  // The domain size is given by the digitizer according to the window
141  // and the step.
142  Domain domain = dig.getDomain();
143 
144  // Create cellular space
145  KSpace K;
146  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
147  if ( ! ok )
148  {
149  std::cerr << "[testTrueLocalEstimatorOnShapeDigitization]"
150  << " error in creating KSpace." << std::endl;
151  }
152  else
153  {
154  try
155  {
156  // Extracts shape boundary
158  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
159  // Getting the consecutive surfels of the 2D boundary
160  std::vector<Point> points;
161  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
162  // Create GridCurve
163  GridCurve<KSpace> gridcurve;
164  gridcurve.initFromVector( points );
165 
166  typedef GridCurve<KhalimskySpaceND<2> >::PointsRange Range;
167  typedef Range::ConstIterator ConstIteratorOnPoints;
169 
171  Range r = gridcurve.getPointsRange();//building range
172  curvatureEstimator.attach( aShape );
173 
174  std::cout << "# idx x y kappa" << endl;
175  unsigned int i = 0;
176  for ( ConstIteratorOnPoints it = r.begin(), ite = r.end();
177  it != ite; ++it, ++i )
178  {
179  const RealPoint& x = *it;
180  const double kappa = curvatureEstimator.eval( it, h );
181  std::cout << i << " " << x[0] << " " << x[1]
182  << " " << kappa << std::endl;
183  }
184  }
185  catch ( InputException& e )
186  {
187  std::cerr << "[testTrueLocalEstimatorOnShapeDigitization]"
188  << " error in finding a bel." << std::endl;
189  ok = false;
190  }
191  }
192  trace.emphase() << ( ok ? "Passed." : "Error." ) << endl;
193  trace.endBlock();
194  return ok;
195 }
196 
197 
199 // Standard services - public :
200 
201 int main( int argc, char** argv )
202 {
203  trace.beginBlock ( "Testing class TrueLocalEstimator" );
204  trace.info() << "Args:";
205  for ( int i = 0; i < argc; ++i )
206  trace.info() << " " << argv[ i ];
207  trace.info() << endl;
208 
209  std::string sinus2D4 = testPath + "samples/sinus2D4.dat";
210 
211  bool res = testTrueLocalEstimator(sinus2D4); // && ... other tests
212  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
213  trace.endBlock();
214 
215  typedef Ellipse2D< Z2i::Space > MyEllipse;
216  MyEllipse ellipse( 1.2, 0.1, 4.0, 3.0, 0.3 );
217  res = res &&
218  testTrueLocalEstimatorOnShapeDigitization<MyEllipse>
219  ( "Ellipse-4-3-0.3-s1", ellipse, 1 );
220  res = res &&
221  testTrueLocalEstimatorOnShapeDigitization<MyEllipse>
222  ( "Ellipse-4-3-0.3-s0.5", ellipse, 0.5 );
223 
224  typedef Flower2D< Z2i::Space > MyFlower;
225  MyFlower flower( 0.5, -2.3, 5.0, 0.7, 6, 0.3 );
226  res = res &&
227  testTrueLocalEstimatorOnShapeDigitization<MyFlower>
228  ( "Flower-5-0.3-6-0.3-s1", flower, 1 );
229  res = res &&
230  testTrueLocalEstimatorOnShapeDigitization<MyFlower>
231  ( "Flower-5-0.3-6-0.3-s0.25", flower, 0.25 );
232 
233  return res ? 0 : 1;
234 }
235 // //
DGtal::concepts::CCurveLocalGeometricEstimator
Aim: This concept describes an object that can process a range so as to return one estimated quantity...
Definition: CCurveLocalGeometricEstimator.h:98
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::GridCurve::initFromVector
bool initFromVector(const std::vector< Point > &aVectorOfPoints)
main
int main(int argc, char **argv)
Definition: testTrueLocalEstimator.cpp:201
DGtal::HyperRectDomain< Space >
DGtal::Trace::endBlock
double endBlock()
DGtal::GaussDigitizer::getUpperBound
const Point & getUpperBound() const
DGtal::TrueLocalEstimatorOnPoints::eval
Quantity eval(const ConstIterator &it, const double h=1.) const
DGtal::ConstRangeAdapter
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
Definition: ConstRangeAdapter.h:86
DGtal::GridCurve::initFromVectorStream
bool initFromVectorStream(std::istream &in)
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::ParametricShapeTangentFunctor
Aim: implements a functor that computes the tangent vector at a given point of a parametric shape.
Definition: ParametricShapeTangentFunctor.h:63
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
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::TrueGlobalEstimatorOnPoints
Aim: Computes the true quantity associated to a parametric shape or to a subrange associated to a par...
Definition: TrueGlobalEstimatorOnPoints.h:71
DGtal::SignedKhalimskyCell
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
Definition: KhalimskySpaceND.h:208
testTrueLocalEstimator
bool testTrueLocalEstimator(const std::string &filename)
Definition: testTrueLocalEstimator.cpp:72
DGtal::Ball2D
Aim: Model of the concept StarShaped represents any circle in the plane.
Definition: Ball2D.h:60
DGtal::TrueLocalEstimatorOnPoints
Aim: Computes the true quantity to each element of a range associated to a parametric shape.
Definition: TrueLocalEstimatorOnPoints.h:68
DGtal::GaussDigitizer::init
void init(const RealPoint &xLow, const RealPoint &xUp, typename RealVector::Component gridStep)
DGtal::Trace::info
std::ostream & info()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::TrueLocalEstimatorOnPoints::attach
void attach(const ParametricShape &aShape)
DGtal::SimpleRandomAccessRangeFromPoint
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
Definition: SimpleRandomAccessRangeFromPoint.h:73
DGtal::ParametricShapeArcLengthFunctor
Aim: implements a functor that estimates the arc length of a paramtric curve.
Definition: ParametricShapeArcLengthFunctor.h:64
Shape
Astroid2D< Space > Shape
Definition: testAstroid2D.cpp:48
Range
Image::Range Range
Definition: testSimpleRandomAccessRangeFromPoint.cpp:46
DGtal::Ellipse2D
Aim: Model of the concept StarShaped represents any ellipse in the plane.
Definition: Ellipse2D.h:64
DGtal::ParametricShapeCurvatureFunctor
Aim: implements a functor that computes the curvature at a given point of a parametric shape.
Definition: ParametricShapeCurvatureFunctor.h:65
domain
Domain domain
Definition: testProjection.cpp:88
DGtal::PointVector< dim, Integer >
testTrueLocalEstimatorOnShapeDigitization
bool testTrueLocalEstimatorOnShapeDigitization(const string &name, Shape &aShape, double h)
Definition: testTrueLocalEstimator.cpp:124
DGtal::Flower2D
Aim: Model of the concept StarShaped represents any flower with k-petals in the plane.
Definition: Flower2D.h:64
RealPoint
Z2i::RealPoint RealPoint
Definition: testAstroid2D.cpp:46
DGtal::SimpleRandomAccessRangeFromPoint::ConstIterator
TConstIterator ConstIterator
Definition: SimpleRandomAccessRangeFromPoint.h:87
DGtal::GaussDigitizer::getLowerBound
const Point & getLowerBound() const
DGtal::GridCurve::getPointsRange
PointsRange getPointsRange() const
Definition: GridCurve.h:426
DGtal::InputException
Definition: Exceptions.h:63
DGtal::KhalimskySpaceND
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:64