DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testTrueLocalEstimator.cpp
1 
32 
33 #include <iostream>
34 #include "DGtal/base/Common.h"
35 
36 #include "DGtal/shapes/Shapes.h"
37 #include "DGtal/shapes/ShapeFactory.h"
38 
39 #include "DGtal/geometry/curves/estimation/TrueLocalEstimatorOnPoints.h"
40 #include "DGtal/geometry/curves/estimation/TrueGlobalEstimatorOnPoints.h"
41 
42 #include "DGtal/geometry/curves/estimation/ParametricShapeCurvatureFunctor.h"
43 #include "DGtal/geometry/curves/estimation/ParametricShapeTangentFunctor.h"
44 #include "DGtal/geometry/curves/estimation/ParametricShapeArcLengthFunctor.h"
45 #include "DGtal/geometry/curves/estimation/MostCenteredMaximalSegmentEstimator.h"
46 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
47 
48 #include "DGtal/kernel/SpaceND.h"
49 #include "DGtal/kernel/domains/HyperRectDomain.h"
50 #include "DGtal/kernel/sets/DigitalSetSelector.h"
51 #include "DGtal/topology/KhalimskySpaceND.h"
52 #include "DGtal/topology/SurfelAdjacency.h"
53 #include "DGtal/topology/SurfelNeighborhood.h"
54 
55 #include "DGtal/shapes/GaussDigitizer.h"
56 #include "DGtal/geometry/curves/GridCurve.h"
57 
58 #include "DGtal/geometry/curves/estimation/CompareLocalEstimators.h"
59 
60 
61 #include "ConfigTest.h"
62 
64 
65 using namespace std;
66 using namespace DGtal;
67 
69 // Functions for testing class TrueLocalEstimator.
71 
75 bool testTrueLocalEstimator(const std::string &filename)
76 {
77  trace.info() << "Reading GridCurve " << endl;
78  ifstream instream; // input stream
79  instream.open (filename.c_str(), ifstream::in);
80  typedef KhalimskySpaceND<2> Kspace; //space
82  c.initFromVectorStream(instream); //building grid curve
84  Range r = c.getPointsRange();//building range
85 
86 
87  typedef Ball2D<Z2i::Space> Shape;
88  typedef GridCurve<KhalimskySpaceND<2> >::PointsRange Range;
89  typedef Range::ConstIterator ConstIteratorOnPoints;
93 
94  Shape ball(Z2i::Point(0,0), 30);
95 
96 
100 
101  curvatureEstimator.init( 1, r.begin(), r.end() );
102  curvatureEstimator.attach( &ball );
103  tangentEstimator.init( 1, r.begin(), r.end() );
104  tangentEstimator.attach( &ball );
105 
106 
107  ConstIteratorOnPoints it = r.begin();
108  ConstIteratorOnPoints it2 = it+15;
109  lengthEstimator.init( 1, it, it2, &ball, true);
110 
111 
112  trace.info() << "Current point = "<<*it<<std::endl;
113  trace.info() << "Current point+15 = "<<*it2<<std::endl;
114  trace.info() << "Eval curvature (begin, h=1) = "<< curvatureEstimator.eval(it2)<<std::endl;
115  trace.info() << "Eval tangent (begin, h=1) = "<< tangentEstimator.eval(it2)<<std::endl;
116  trace.info() << "Eval length ( h=1) = "<< lengthEstimator.eval(it,it2)<<std::endl;
117 
118  return true;
119 
120 }
121 
122 template <typename Shape>
123 bool
124 testTrueLocalEstimatorOnShapeDigitization( const string & name,
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  try {
154  // Extracts shape boundary
156  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
157  // Getting the consecutive surfels of the 2D boundary
158  std::vector<Point> points;
159  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
160  // Create GridCurve
161  GridCurve<KSpace> gridcurve;
162  gridcurve.initFromVector( points );
163  typedef GridCurve<KhalimskySpaceND<2> >::PointsRange Range;
164  typedef Range::ConstIterator ConstIteratorOnPoints;
167  Range r = gridcurve.getPointsRange();//building range
168  curvatureEstimator.init( h, r.begin(), r.end() );
169  curvatureEstimator.attach( &aShape );
170  std::cout << "# idx x y kappa" << endl;
171  unsigned int i = 0;
172  for ( ConstIteratorOnPoints it = r.begin(), ite = r.end();
173  it != ite; ++it, ++i )
174  {
175  RealPoint x = *it;
176  double kappa = curvatureEstimator.eval( it );
177  std::cout << i << " " << x.at( 0 ) << " " << x.at( 1 )
178  << " " << kappa << std::endl;
179  }
180  }
181  catch ( InputException e )
182  {
183  std::cerr << "[testTrueLocalEstimatorOnShapeDigitization]"
184  << " error in finding a bel." << std::endl;
185  ok = false;
186  }
187  trace.emphase() << ( ok ? "Passed." : "Error." ) << endl;
188  trace.endBlock();
189  return ok;
190 }
191 
192 
194 // Standard services - public :
195 
196 int main( int argc, char** argv )
197 {
198  trace.beginBlock ( "Testing class TrueLocalEstimator" );
199  trace.info() << "Args:";
200  for ( int i = 0; i < argc; ++i )
201  trace.info() << " " << argv[ i ];
202  trace.info() << endl;
203 
204 
205 
206  std::string sinus2D4 = testPath + "samples/sinus2D4.dat";
207 
208  bool res = testTrueLocalEstimator(sinus2D4); // && ... other tests
209  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
210  trace.endBlock();
211 
212  typedef Ellipse2D< Z2i::Space > MyEllipse;
213  MyEllipse ellipse( 1.2, 0.1, 4.0, 3.0, 0.3 );
214  res = res &&
215  testTrueLocalEstimatorOnShapeDigitization<MyEllipse>
216  ( "Ellipse-4-3-0.3-s1", ellipse, 1 );
217  res = res &&
218  testTrueLocalEstimatorOnShapeDigitization<MyEllipse>
219  ( "Ellipse-4-3-0.3-s0.5", ellipse, 0.5 );
220 
221  typedef Flower2D< Z2i::Space > MyFlower;
222  MyFlower flower( 0.5, -2.3, 5.0, 0.7, 6, 0.3 );
223  res = res &&
224  testTrueLocalEstimatorOnShapeDigitization<MyFlower>
225  ( "Flower-5-0.3-6-0.3-s1", flower, 1 );
226  res = res &&
227  testTrueLocalEstimatorOnShapeDigitization<MyFlower>
228  ( "Flower-5-0.3-6-0.3-s0.25", flower, 0.25 );
229 
230  return res ? 0 : 1;
231 
232 }
233 // //