DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testEstimatorComparator.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 
41 #include "DGtal/geometry/curves/estimation/ParametricShapeCurvatureFunctor.h"
42 #include "DGtal/geometry/curves/estimation/ParametricShapeTangentFunctor.h"
43 #include "DGtal/geometry/curves/estimation/ParametricShapeArcLengthFunctor.h"
44 #include "DGtal/geometry/curves/estimation/MostCenteredMaximalSegmentEstimator.h"
45 #include "DGtal/geometry/curves/ArithmeticalDSS.h"
46 
47 #include "DGtal/kernel/SpaceND.h"
48 #include "DGtal/kernel/domains/HyperRectDomain.h"
49 #include "DGtal/kernel/sets/DigitalSetSelector.h"
50 #include "DGtal/topology/KhalimskySpaceND.h"
51 #include "DGtal/topology/SurfelAdjacency.h"
52 #include "DGtal/topology/SurfelNeighborhood.h"
53 
54 #include "DGtal/shapes/GaussDigitizer.h"
55 #include "DGtal/geometry/curves/GridCurve.h"
56 
57 #include "DGtal/geometry/curves/estimation/CompareLocalEstimators.h"
58 
59 
60 #include "ConfigTest.h"
61 
63 
64 using namespace std;
65 using namespace DGtal;
66 
67 
68 template <typename Shape>
69 bool testCompareEstimator(const std::string &name, Shape & aShape, double h)
70 {
71  using namespace Z2i;
72 
73  trace.beginBlock ( ( "Testing CompareEstimator on digitization of "
74  + name ). c_str() );
75 
76  // Creates a digitizer on the window (xLow, xUp).
77  typedef Space::RealPoint RealPoint;
78  RealPoint xLow( -10.0, -10.0 );
79  RealPoint xUp( 10.0, 10.0 );
81  dig.attach( aShape ); // attaches the shape.
82  dig.init( xLow, xUp, h );
83 
84  // The domain size is given by the digitizer according to the window
85  // and the step.
86  Domain domain = dig.getDomain();
87 
88  // Create cellular space
89  KSpace K;
90  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
91  if ( ! ok )
92  {
93  std::cerr << "[testCompareEstimators]"
94  << " error in creating KSpace." << std::endl;
95  }
96  else
97  try {
98  // Extracts shape boundary
100  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
101  // Getting the consecutive surfels of the 2D boundary
102  std::vector<Point> points;
103  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
104  // Create GridCurve
105  GridCurve<KSpace> gridcurve;
106  gridcurve.initFromVector( points );
107  typedef GridCurve<KhalimskySpaceND<2> >::PointsRange Range;
108  typedef Range::ConstIterator ConstIteratorOnPoints;
109  Range r = gridcurve.getPointsRange();//building range
110 
111  unsigned int nb = 0;
112  unsigned int nbok = 0;
113  //curvature
116  TrueCurvature curvatureEstimator;
117  TrueCurvature curvatureEstimatorBis;
118  curvatureEstimator.init( h, r.begin(), r.end() );
119  curvatureEstimator.attach( &aShape );
120  curvatureEstimatorBis.init( h, r.begin(), r.end() );
121  curvatureEstimatorBis.attach( &aShape );
122 
124 
125  trace.info()<< "True curvature comparison at "<< *r.begin() << " = "
126  << Comparator::compare(curvatureEstimator,curvatureEstimatorBis, r.begin())
127  << std::endl;
128 
129  typename Comparator::OutputStatistic error
130  =Comparator::compare(curvatureEstimator, curvatureEstimatorBis,
131  r.begin(),
132  r.end());
133 
134  trace.info() << "Nb samples= "<< error.samples()<<std::endl;
135  trace.info() << "Error mean= "<< error.mean()<<std::endl;
136  trace.info() << "Error max= "<< error.max()<<std::endl;
137  nbok += ( ( ( (unsigned int)error.samples() ) == r.size())
138  && (error.max() == 0) )
139  ? 1 : 0;
140  nb++;
141  trace.info() << nbok << "/" << nb << std::endl;
142 
143  //tangents
146 
148  SegmentComputer;
151  MSTangentEstimator;
152 
153  SegmentComputer sc;
154  Functor f;
155 
156  TrueTangent tang1;
157  MSTangentEstimator tang2(sc, f);
158 
159  tang1.init( h, r.begin(), r.end() );
160  tang1.attach( &aShape );
161  tang2.init( h, r.begin(), r.end() );
162 
164 
165  trace.info()<< "Tangent comparison at "<< *r.begin() << " = "
166  << ComparatorTan::compareVectors( tang1, tang2, r.begin())
167  << std::endl;
168 
169  typename ComparatorTan::OutputVectorStatistic error2
170  =ComparatorTan::compareVectors(tang1, tang2,
171  r.begin(),
172  r.end());
173 
174  trace.info()<< "Nb samples= "<< error2.samples()<<std::endl;
175  trace.info()<< "Error mean= "<< error2.mean()<<std::endl;
176  trace.info()<< "Error max= "<< error2.max()<<std::endl;
177  nbok += (error.samples() == r.size())?1:0;
178  nb++;
179  trace.info() << nbok << "/" << nb << std::endl;
180  ok += (nb == nbok);
181 
182  }
183  catch ( InputException e )
184  {
185  std::cerr << "[testCompareEstimator]"
186  << " error in finding a bel." << std::endl;
187  ok = false;
188  }
189  trace.emphase() << ( ok ? "Passed." : "Error." ) << endl;
190  trace.endBlock();
191  return ok;
192 
193 }
194 
195 
197 // Standard services - public :
198 
199 int main( int argc, char** argv )
200 {
201  trace.beginBlock ( "Testing class CompareLocalEstimator" );
202  trace.info() << "Args:";
203  for ( int i = 0; i < argc; ++i )
204  trace.info() << " " << argv[ i ];
205  trace.info() << endl;
206 
207  typedef Flower2D< Z2i::Space > MyFlower;
208  MyFlower flower( 0.5, -2.3, 5.0, 0.7, 6, 0.3 );
209  bool res = testCompareEstimator<MyFlower>("Flower", flower, 0.25);
210  return res ? 0 : 1;
211 
212 }
213 // //