DGtal  0.9.2
testIntegralInvariantVolumeEstimator.cpp
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/IntegralInvariantVolumeEstimator.h"
48 
49 
51 
52 
53 using namespace DGtal;
54 
56 // Functions for testing class IntegralInvariantVolumeEstimator and IIGeometricFunctor.
58 
59 bool testCurvature2d ( double h, double delta )
60 {
66  typedef GraphVisitorRange< Visitor > VisitorRange;
67  typedef VisitorRange::ConstIterator VisitorConstIterator;
68 
69  typedef functors::IICurvatureFunctor<Z2i::Space> MyIICurvatureFunctor;
71  typedef MyIICurvatureFunctor::Value Value;
72 
73  double re = 10;
74  double radius = 15;
75  double realValue = 1.0/radius;
76 
77  trace.beginBlock( "Shape initialisation ..." );
78 
79  ImplicitShape ishape( Z2i::RealPoint( 0, 0 ), radius );
80  DigitalShape dshape;
81  dshape.attach( ishape );
82  dshape.init( Z2i::RealPoint( -20.0, -20.0 ), Z2i::RealPoint( 20.0, 20.0 ), h );
83 
84  Z2i::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 
91  Z2i::KSpace::Surfel bel = Surfaces<Z2i::KSpace>::findABel( K, dshape, 10000 );
92  Boundary boundary( K, dshape, SurfelAdjacency<Z2i::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 2D curvature ..." );
122 
123  double mean = 0.0;
124  double rsize = static_cast<double>(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 
161 bool testMeanCurvature3d( double h, double delta )
162 {
163  typedef ImplicitBall<Z3i::Space> ImplicitShape;
164  typedef GaussDigitizer<Z3i::Space, ImplicitShape> DigitalShape;
166  typedef DigitalSurface< Boundary > MyDigitalSurface;
168  typedef GraphVisitorRange< Visitor > VisitorRange;
169  typedef VisitorRange::ConstIterator VisitorConstIterator;
170 
171  typedef functors::IIMeanCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
173  typedef MyIICurvatureFunctor::Value Value;
174 
175  double re = 5;
176  double radius = 5;
177  double realValue = 1.0/radius;
178 
179  trace.beginBlock( "Shape initialisation ..." );
180 
181  ImplicitShape ishape( Z3i::RealPoint( 0, 0, 0 ), radius );
182  DigitalShape dshape;
183  dshape.attach( ishape );
184  dshape.init( Z3i::RealPoint( -10.0, -10.0, -10.0 ), Z3i::RealPoint( 10.0, 10.0, 10.0 ), h );
185 
186  Z3i::KSpace K;
187  if ( !K.init( dshape.getLowerBound(), dshape.getUpperBound(), true ) )
188  {
189  trace.error() << "Problem with Khalimsky space" << std::endl;
190  return false;
191  }
192 
193  Z3i::KSpace::Surfel bel = Surfaces<Z3i::KSpace>::findABel( K, dshape, 10000 );
194  Boundary boundary( K, dshape, SurfelAdjacency<Z3i::KSpace::dimension>( true ), bel );
195  MyDigitalSurface surf ( boundary );
196 
197  trace.endBlock();
198 
199  trace.beginBlock( "Curvature estimator initialisation ...");
200 
201  VisitorRange range( new Visitor( surf, *surf.begin() ));
202  VisitorConstIterator ibegin = range.begin();
203  VisitorConstIterator iend = range.end();
204 
205  MyIICurvatureFunctor curvatureFunctor;
206  curvatureFunctor.init( h, re );
207 
208  MyIICurvatureEstimator curvatureEstimator( curvatureFunctor );
209  curvatureEstimator.attach( K, dshape );
210  curvatureEstimator.setParams( re/h );
211  curvatureEstimator.init( h, ibegin, iend );
212 
213  trace.endBlock();
214 
215  trace.beginBlock( "Curvature estimator evaluation ...");
216 
217  std::vector< Value > results;
218  std::back_insert_iterator< std::vector< Value > > resultsIt( results );
219  curvatureEstimator.eval( ibegin, iend, resultsIt );
220 
221  trace.endBlock();
222 
223  trace.beginBlock ( "Comparing results of integral invariant 3D mean curvature ..." );
224 
225  double mean = 0.0;
226  unsigned int rsize = results.size();
227 
228  if( rsize == 0 )
229  {
230  trace.error() << "ERROR: surface is empty" << std::endl;
231  trace.endBlock();
232  return false;
233  }
234 
235  for ( unsigned int i = 0; i < rsize; ++i )
236  {
237  mean += results[ i ];
238  }
239  mean /= rsize;
240 
241  if( mean != mean ) //NaN
242  {
243  trace.error() << "ERROR: result is NaN" << std::endl;
244  trace.endBlock();
245  return false;
246  }
247 
248  double v = std::abs ( realValue - mean );
249 
250  trace.warning() << "True value: " << realValue << std::endl;
251  trace.warning() << "Mean value: " << mean << std::endl;
252  trace.warning() << "Delta: " << delta << " |true - mean|: " << v << std::endl;
253 
254  if ( v > delta )
255  {
256  trace.endBlock();
257  return false;
258  }
259 
260  trace.endBlock();
261  return true;
262 }
263 
265 // Standard services - public :
266 
267 int main( int /*argc*/, char** /*argv*/ )
268 {
269  trace.beginBlock ( "Testing class IntegralInvariantVolumeEstimator and 2d/3d mean curvature functors" );
270  bool res = testCurvature2d( 0.05, 0.002 ) && testMeanCurvature3d( 0.6, 0.008 );
271  trace.emphase() << ( res ? "Passed." : "Error." ) << std::endl;
272  trace.endBlock();
273  return res ? 0 : 1;
274 }
275 // //
void beginBlock(const std::string &keyword="")
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
const Point & getUpperBound() const
Trace trace
Definition: Common.h:130
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...
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
Aim: This class implement an Integral Invariant estimator which computes for each surfel the volume o...
static SCell findABel(const KSpace &K, const PointPredicate &pp, unsigned int nbtries=1000)
void attach(const EuclideanShape &shape)
void init(const RealPoint &xLow, const RealPoint &xUp, typename RealVector::Component gridStep)
Aim: A functor Real -> Real that returns the 2d curvature by transforming the given volume...
std::ostream & emphase()
bool init(const Point &lower, const Point &upper, bool isClosed)
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
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: Transforms a graph visitor into a single pass input range.
Aim: A functor Real -> Real that returns the 3d mean curvature by transforming the given volume...
std::ostream & warning()
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value...
const Point & getLowerBound() const
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
std::ostream & error()
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...