DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
exampleCurvature.cpp
1 
30 
31 #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/GeometricalDCA.h"
47 
49 
50 using namespace std;
51 using namespace DGtal;
52 
53 #include <boost/program_options/options_description.hpp>
54 #include <boost/program_options/parsers.hpp>
55 #include <boost/program_options/variables_map.hpp>
56 
57 namespace po = boost::program_options;
58 
60 template <typename Shape, typename RealPoint>
61 bool
62 estimatorOnShapeDigitization( const string& name,
63  Shape & aShape,
64  const RealPoint& low, const RealPoint& up,
65  double h )
66 {
67  using namespace Z2i;
68 
69  trace.beginBlock ( ( "Curvature estimation on digitization of "
70  + name ). c_str() );
71 
72  // Creates a digitizer on the window (low, up).
74  dig.attach( aShape ); // attaches the shape.
75  dig.init( low, up, h );
76 
77  // The domain size is given by the digitizer
78  // according to the window and the step.
79  Domain domain = dig.getDomain();
80 
81  // Create cellular space
82  KSpace K;
83  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
84  if ( ! ok )
85  {
86  std::cerr << "[estimatorOnShapeDigitization]"
87  << " error in creating KSpace." << std::endl;
88  }
89  else
90  try {
91  // Extracts shape boundary
93  SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
94  // Getting the consecutive surfels of the 2D boundary
95  std::vector<Point> points;
96  Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
97  // Create GridCurve
98  GridCurve<KSpace> gridcurve( K );
99  gridcurve.initFromVector( points );
100  // Create range of incident points
102  typedef Range::ConstIterator ClassicIterator;
103  typedef Range::ConstCirculator CircularIterator;
104  Range r = gridcurve.getIncidentPointsRange();//building range
105  // Estimation
106  std::vector<double> estimations;
107  if (gridcurve.isOpen())
108  {
109  typedef GeometricalDCA<ClassicIterator> SegmentComputer;
110  typedef CurvatureFromDCAEstimator<SegmentComputer> SCEstimator;
112  SegmentComputer sc;
113  SCEstimator sce;
114  CurvatureEstimator estimator(sc, sce);
115  std::cout << "# open grid curve" << endl;
116  estimator.init( h, r.begin(), r.end() );
117  estimator.eval( r.begin(), r.end(), std::back_inserter(estimations) );
118  }
119  else
120  {
121  typedef GeometricalDCA<CircularIterator> SegmentComputer;
122  typedef CurvatureFromDCAEstimator<SegmentComputer> SCEstimator;
124  SegmentComputer sc;
125  SCEstimator sce;
126  CurvatureEstimator estimator(sc, sce);
127  std::cout << "# closed grid curve" << endl;
128  estimator.init( h, r.c(), r.c() );
129  estimator.eval( r.c(), r.c(), std::back_inserter(estimations) );
130  }
131  // Print (standard output)
132  std::cout << "# idx kappa" << endl;
133  unsigned int i = 0;
134  for ( ClassicIterator it = r.begin(), ite = r.end();
135  it != ite; ++it, ++i )
136  {
137  std::cout << i << " " << estimations.at(i) << std::endl;
138  }
139  }
140  catch ( InputException e )
141  {
142  std::cerr << "[estimatorOnShapeDigitization]"
143  << " error in finding a bel." << std::endl;
144  ok = false;
145  }
146  trace.emphase() << ( ok ? "Passed." : "Error." ) << endl;
147  trace.endBlock();
148  return ok;
149 }
150 
152 int main( int argc, char** argv )
153 {
154  trace.beginBlock ( "Example exampleCurvature" );
155 
156  // parse command line
157  po::options_description general_opt("Allowed options are");
158  general_opt.add_options()
159  ("help,h", "display this message")
160  ("shape,s", po::value<string>()->default_value("flower"), "Shape to digitize: flower, ellipse, ball" )
161  ("gridStep,g", po::value<double>()->default_value(0.01), "Grid step" );
162 
163  po::variables_map vm;
164  po::store(po::parse_command_line(argc, argv, general_opt), vm);
165  po::notify(vm);
166  trace.info()<< "curvature estimation" << std::endl
167  << "Basic usage: "<<std::endl
168  << argv[0] << " [other options] -i <vol file> -t <threshold> " << std::endl
169  << general_opt << "\n";
170 
171  // grid step
172  double h = vm["gridStep"].as<double>();
173  // shape
174  string shapeName = vm["shape"].as<string>();
175 
176 
177  // parse shape
178  bool res = true;
179  typedef Z2i::Space Space;
180  typedef Space::RealPoint RealPoint;
181  if (shapeName == "flower")
182  {
183  Flower2D<Space> flower( 0.5, 0.5, 5.0, 3.0, 5, 0.3 );
184  res = estimatorOnShapeDigitization("flower", flower,
185  RealPoint::diagonal(-10),
186  RealPoint::diagonal(10),
187  h);
188  }
189  else if (shapeName == "ellipse")
190  {
191  Ellipse2D<Space> ellipse( 0.5, 0.5, 5.0, 3.0, 0.3 );
192  res = estimatorOnShapeDigitization("ellipse", ellipse,
193  RealPoint::diagonal(-10),
194  RealPoint::diagonal(10),
195  h);
196  }
197  else if (shapeName == "ball")
198  {
199  Ball2D<Space> ball( 0.5, 0.5, 5.0 );
200  res = estimatorOnShapeDigitization("ball", ball,
201  RealPoint::diagonal(-10),
202  RealPoint::diagonal(10),
203  h);
204  }
205  else
206  {
207  trace.error() << "Unknown shape. Use option -h" << std::endl;
208  res = false;
209  }
210 
211  trace.endBlock();
212 
213  return res;
214 }
215 // //