DGtal  0.9.4.1
testIntegralInvariantCovarianceEstimator.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
33 #include "DGtal/base/Common.h"
34 
36 #include "DGtal/shapes/implicit/ImplicitBall.h"
37 
39 #include "DGtal/shapes/GaussDigitizer.h"
40 #include "DGtal/topology/LightImplicitDigitalSurface.h"
41 #include "DGtal/topology/DigitalSurface.h"
42 #include "DGtal/graph/DepthFirstVisitor.h"
43 #include "DGtal/graph/GraphVisitorRange.h"
44 
46 #include "DGtal/geometry/surfaces/estimation/IIGeometricFunctors.h"
47 #include "DGtal/geometry/surfaces/estimation/IntegralInvariantCovarianceEstimator.h"
48 
49 
51 
52 
53 using namespace DGtal;
54 
56 // Functions for testing class IntegralInvariantCovarianceEstimator and IIGeometricFunctor.
58 
59 bool testGaussianCurvature3d( double h, double delta )
60 {
66  typedef GraphVisitorRange< Visitor > VisitorRange;
67  typedef VisitorRange::ConstIterator VisitorConstIterator;
68 
69  typedef functors::IIGaussianCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
72 
73  double re = 5.0;
74  double radius = 5.0;
75  double realValue = 1.0/(radius * radius);
76 
77  trace.beginBlock( "Shape initialisation ..." );
78 
79  ImplicitShape ishape( Z3i::RealPoint( 0, 0, 0 ), radius );
80  DigitalShape dshape;
81  dshape.attach( ishape );
82  dshape.init( Z3i::RealPoint( -10.0, -10.0, -10.0 ), Z3i::RealPoint( 10.0, 10.0, 10.0 ), h );
83 
84  Z3i::KSpace K;
85  if ( !K.init( dshape.getLowerBound(), dshape.getUpperBound(), true ) )
86  {
87  trace.error() << "Problem with Khalimsky space" << std::endl;
88  return false;
89  }
90 
92  Boundary boundary( K, dshape, SurfelAdjacency<Z3i::KSpace::dimension>( true ), bel );
93  MyDigitalSurface surf ( boundary );
94 
95  trace.endBlock();
96 
97  trace.beginBlock( "Curvature estimator initialisation ...");
98 
99  VisitorRange range( new Visitor( surf, *surf.begin() ));
100  VisitorConstIterator ibegin = range.begin();
101  VisitorConstIterator iend = range.end();
102 
103  MyIICurvatureFunctor curvatureFunctor;
104  curvatureFunctor.init( h, re );
105 
106  MyIICurvatureEstimator curvatureEstimator( curvatureFunctor );
107  curvatureEstimator.attach( K, dshape );
108  curvatureEstimator.setParams( re/h );
109  curvatureEstimator.init( h, ibegin, iend );
110 
111  trace.endBlock();
112 
113  trace.beginBlock( "Curvature estimator evaluation ...");
114 
115  std::vector< Value > results;
116  std::back_insert_iterator< std::vector< Value > > resultsIt( results );
117  curvatureEstimator.eval( ibegin, iend, resultsIt );
118 
119  trace.endBlock();
120 
121  trace.beginBlock ( "Comparing results of integral invariant 3D Gaussian curvature ..." );
122 
123  double mean = 0.0;
124  unsigned int rsize = static_cast<unsigned int>(results.size());
125 
126  if( rsize == 0 )
127  {
128  trace.error() << "ERROR: surface is empty" << std::endl;
129  trace.endBlock();
130  return false;
131  }
132 
133  for ( unsigned int i = 0; i < rsize; ++i )
134  {
135  mean += results[ i ];
136  }
137  mean /= rsize;
138 
139  if( mean != mean ) //NaN
140  {
141  trace.error() << "ERROR: result is NaN" << std::endl;
142  trace.endBlock();
143  return false;
144  }
145 
146  double v = std::abs ( realValue - mean );
147 
148  trace.warning() << "True value: " << realValue << std::endl;
149  trace.warning() << "Mean value: " << mean << std::endl;
150  trace.warning() << "Delta: " << delta << " |true - mean|: " << v << std::endl;
151 
152  if( v > delta )
153  {
154  trace.endBlock();
155  return false;
156  }
157  trace.endBlock();
158  return true;
159 }
160 
162 {
168  typedef GraphVisitorRange< Visitor > VisitorRange;
169  typedef VisitorRange::ConstIterator VisitorConstIterator;
170 
171  typedef functors::IIPrincipalCurvatures3DFunctor<Z3i::Space> MyIICurvatureFunctor;
174 
175  double re = 5.0;
176  double radius = 5.0;
177 
178  trace.beginBlock( "Shape initialisation ..." );
179 
180  ImplicitShape ishape( Z3i::RealPoint( 0, 0, 0 ), radius );
181  DigitalShape dshape;
182  dshape.attach( ishape );
183  dshape.init( Z3i::RealPoint( -10.0, -10.0, -10.0 ), Z3i::RealPoint( 10.0, 10.0, 10.0 ), h );
184 
185  Z3i::KSpace K;
186  if ( !K.init( dshape.getLowerBound(), dshape.getUpperBound(), true ) )
187  {
188  trace.error() << "Problem with Khalimsky space" << std::endl;
189  return false;
190  }
191 
192  Z3i::KSpace::Surfel bel = Surfaces<Z3i::KSpace>::findABel( K, dshape, 10000 );
193  Boundary boundary( K, dshape, SurfelAdjacency<Z3i::KSpace::dimension>( true ), bel );
194  MyDigitalSurface surf ( boundary );
195 
196  trace.endBlock();
197 
198  trace.beginBlock( "Curvature estimator initialisation ...");
199 
200  VisitorRange range( new Visitor( surf, *surf.begin() ));
201  VisitorConstIterator ibegin = range.begin();
202  VisitorConstIterator iend = range.end();
203 
204  MyIICurvatureFunctor curvatureFunctor;
205  curvatureFunctor.init( h, re );
206 
207  MyIICurvatureEstimator curvatureEstimator( curvatureFunctor );
208  curvatureEstimator.attach( K, dshape );
209  curvatureEstimator.setParams( re/h );
210  curvatureEstimator.init( h, ibegin, iend );
211 
212  trace.endBlock();
213 
214  trace.beginBlock( "Curvature estimator evaluation ...");
215 
216  std::vector< Value > results;
217  std::back_insert_iterator< std::vector< Value > > resultsIt( results );
218  curvatureEstimator.eval( ibegin, iend, resultsIt );
219 
220  trace.endBlock();
221 
222  trace.beginBlock ( "Comparing results of integral invariant 3D Gaussian curvature ..." );
223 
224  unsigned int error_order = 0;
225  unsigned int rsize = static_cast<unsigned int>(results.size());
226 
227  if( rsize == 0 )
228  {
229  trace.error() << "ERROR: surface is empty" << std::endl;
230  trace.endBlock();
231  return false;
232  }
233 
234  for ( unsigned int i = 0; i < rsize; ++i )
235  {
236  if( std::abs(results[i].first) < std::abs(results[i].second) )
237  {
238  ++error_order;
239  }
240  }
241 
242  trace.warning() << "Error order: " << error_order << std::endl;
243  trace.warning() << "If not equals to 0, something is wrong..." << std::endl;
244 
245  trace.endBlock();
246 
247  if( error_order != 0 )
248  {
249  return false;
250  }
251  return true;
252 }
253 
255 // Standard services - public :
256 
257 int main( int /*argc*/, char** /*argv*/ )
258 {
259  trace.beginBlock ( "Testing class IntegralInvariantCovarianceEstimator and 3d functors" );
260  bool res = testGaussianCurvature3d( 0.6, 0.007 ) && testPrincipalCurvatures3d( 0.6 );
261  trace.emphase() << ( res ? "Passed." : "Error." ) << std::endl;
262  trace.endBlock();
263  return res ? 0 : 1;
264 }
265 // //
void beginBlock(const std::string &keyword="")
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
MyDigitalSurface::ConstIterator ConstIterator
const Point & getLowerBound() const
Trace trace
Definition: Common.h:137
const Point & getUpperBound() const
Aim: This class is useful to perform a depth-first exploration of a graph given a starting point or s...
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
double endBlock()
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
BreadthFirstVisitor< MyDigitalSurface > Visitor
Aim: This class implement an Integral Invariant estimator which computes for each surfel the covarian...
ConstIterator begin() const
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
static SCell findABel(const KSpace &K, const PointPredicate &pp, unsigned int nbtries=1000)
void attach(const EuclideanShape &shape)
Aim: A functor Matrix -> std::pair<Real,Real> that returns the first and the second principal curvatu...
void init(const RealPoint &xLow, const RealPoint &xUp, typename RealVector::Component gridStep)
std::ostream & emphase()
bool init(const Point &lower, const Point &upper, bool isClosed)
bool testGaussianCurvature3d(double h, double delta)
Aim: A class for computing the Gauss digitization of some Euclidean shape, i.e. its intersection with...
Aim: model of CEuclideanOrientedShape concepts to create a shape from a polynomial.
Aim: model of CEuclideanOrientedShape and CEuclideanBoundedShape concepts to create a ball in nD...
Definition: ImplicitBall.h:64
bool testPrincipalCurvatures3d(double h)
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: Transforms a graph visitor into a single pass input range.
std::ostream & warning()
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value...
KSpace K
std::ostream & error()
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
Aim: A functor Matrix -> Real that returns the Gaussian curvature by diagonalizing the given covarian...