DGtal  0.9.3beta
exampleCurvature.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 
34 #include "DGtal/kernel/SpaceND.h"
35 #include "DGtal/kernel/domains/HyperRectDomain.h"
36 #include "DGtal/topology/KhalimskySpaceND.h"
37 #include "DGtal/topology/SurfelAdjacency.h"
38 #include "DGtal/topology/SurfelNeighborhood.h"
39 
40 #include "DGtal/shapes/Shapes.h"
41 #include "DGtal/shapes/ShapeFactory.h"
42 #include "DGtal/shapes/GaussDigitizer.h"
43 #include "DGtal/geometry/curves/GridCurve.h"
44 
45 #include "DGtal/geometry/curves/estimation/MostCenteredMaximalSegmentEstimator.h"
46 #include "DGtal/geometry/curves/StabbingCircleComputer.h"
47 
49 
50 using namespace std;
51 using namespace DGtal;
52 
53 
55 template <typename Shape, typename RealPoint>
56 bool
57 estimatorOnShapeDigitization( const string& name,
58  Shape & aShape,
59  const RealPoint& low, const RealPoint& up,
60  double h )
61 {
62  using namespace Z2i;
63 
64  trace.beginBlock ( ( "Curvature estimation on digitization of "
65  + name ). c_str() );
66 
67  // Creates a digitizer on the window (low, up).
69  dig.attach( aShape ); // attaches the shape.
70  dig.init( low, up, h );
71 
72  // The domain size is given by the digitizer
73  // according to the window and the step.
74  Domain domain = dig.getDomain();
75 
76  // Create cellular space
77  KSpace K;
78  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
79  if ( ! ok )
80  {
81  std::cerr << "[estimatorOnShapeDigitization]"
82  << " error in creating KSpace." << std::endl;
83  }
84  else
85  try {
86  // Extracts shape boundary
88  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
89  // Getting the consecutive surfels of the 2D boundary
90  std::vector<Point> points;
91  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
92  // Create GridCurve
93  GridCurve<KSpace> gridcurve( K );
94  gridcurve.initFromVector( points );
95  // Create range of incident points
97  typedef Range::ConstIterator ClassicIterator;
98  typedef Range::ConstCirculator CircularIterator;
99  Range r = gridcurve.getIncidentPointsRange();//building range
100  // Estimation
101  std::vector<double> estimations;
102  if (gridcurve.isOpen())
103  {
105  typedef StabbingCircleComputer<ClassicIterator> SegmentComputer;
106  typedef CurvatureFromDCAEstimator<SegmentComputer> SCEstimator;
108  SegmentComputer sc;
109  SCEstimator sce;
110  CurvatureEstimator estimator(sc, sce);
112 
113  std::cout << "# open grid curve" << endl;
114 
116  estimator.init( h, r.begin(), r.end() );
118 
120  estimator.eval( r.begin(), r.end(), std::back_inserter(estimations) );
122  }
123  else
124  {
125  typedef StabbingCircleComputer<CircularIterator> SegmentComputer;
126  typedef CurvatureFromDCAEstimator<SegmentComputer> SCEstimator;
128  SegmentComputer sc;
129  SCEstimator sce;
130  CurvatureEstimator estimator(sc, sce);
131  std::cout << "# closed grid curve" << endl;
132  estimator.init( h, r.c(), r.c() );
133  estimator.eval( r.c(), r.c(), std::back_inserter(estimations) );
134  }
135  // Print (standard output)
136  std::cout << "# idx kappa" << endl;
137  unsigned int i = 0;
138  for ( ClassicIterator it = r.begin(), ite = r.end();
139  it != ite; ++it, ++i )
140  {
141  std::cout << i << " " << estimations.at(i) << std::endl;
142  }
143  }
144  catch ( InputException e )
145  {
146  std::cerr << "[estimatorOnShapeDigitization]"
147  << " error in finding a bel." << std::endl;
148  ok = false;
149  }
150  trace.emphase() << ( ok ? "Passed." : "Error." ) << endl;
151  trace.endBlock();
152  return ok;
153 }
154 
156 int main( )
157 {
158  trace.beginBlock ( "Example exampleCurvature" );
159  trace.info() << " Curvature estimation using shape to digitize: flower" << std::endl
160  << " with Grid step: 0.01" << std::endl;
161 
162  // grid step
163  double h = 0.01;
164  // shape
165  string shapeName = "flower";
166 
167 
168  // parse shape
169  bool res = true;
170  typedef Z2i::Space Space;
171  typedef Space::RealPoint RealPoint;
172  if (shapeName == "flower")
173  {
174  Flower2D<Space> flower( 0.5, 0.5, 5.0, 3.0, 5, 0.3 );
175  res = estimatorOnShapeDigitization("flower", flower,
176  RealPoint::diagonal(-10),
177  RealPoint::diagonal(10),
178  h);
179  }
180  else if (shapeName == "ellipse")
181  {
182  Ellipse2D<Space> ellipse( 0.5, 0.5, 5.0, 3.0, 0.3 );
183  res = estimatorOnShapeDigitization("ellipse", ellipse,
184  RealPoint::diagonal(-10),
185  RealPoint::diagonal(10),
186  h);
187  }
188  else if (shapeName == "ball")
189  {
190  Ball2D<Space> ball( 0.5, 0.5, 5.0 );
191  res = estimatorOnShapeDigitization("ball", ball,
192  RealPoint::diagonal(-10),
193  RealPoint::diagonal(10),
194  h);
195  }
196 
197 
198  trace.endBlock();
199 
200  return res;
201 }
202 // //
void beginBlock(const std::string &keyword="")
const Point & getUpperBound() const
Trace trace
Definition: Common.h:137
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
KhalimskySpaceND< 2, Integer > KSpace
Definition: StdDefs.h:77
Aim: A model of CLocalCurveGeometricEstimator that assigns to each element of a (sub)range a quantity...
Aim: Model of the concept StarShaped represents any ellipse in the plane.
Definition: Ellipse2D.h:64
STL namespace.
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
void attach(const EuclideanShape &shape)
Aim: Model of the concept StarShaped represents any flower with k-petals in the plane.
Definition: Flower2D.h:64
Aim: On-line recognition of a digital circular arcs (DCA) defined as a sequence of connected grid edg...
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
Aim: Model of the concept StarShaped represents any circle in the plane.
Definition: Ball2D.h:60
void init(const RealPoint &xLow, const RealPoint &xUp, typename RealVector::Component gridStep)
std::ostream & emphase()
Aim: A class for computing the Gauss digitization of some Euclidean shape, i.e. its intersection with...
Domain getDomain() const
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:85
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
std::ostream & info()
const Point & getLowerBound() const
Space::RealPoint RealPoint
Definition: StdDefs.h:97
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:172