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