DGtalTools  1.3.beta
tangentBC.cpp
1 
30 #include <cmath>
32 #include <iostream>
33 #include <vector>
34 #include <string>
35 
36 #include "CLI11.hpp"
37 
38 #include "DGtal/base/Common.h"
39 #include "DGtal/helpers/StdDefs.h"
40 #include "DGtal/io/readers/PointListReader.h"
41 
42 //Grid curve
43 #include "DGtal/geometry/curves/FreemanChain.h"
44 #include "DGtal/geometry/curves/GridCurve.h"
45 
46 //Estimators
47 #include "DGtal/geometry/curves/BinomialConvolver.h"
48 
49 using namespace DGtal;
50 
52 
102 int main( int argc, char** argv )
103 {
104  // parse command line CLI ----------------------------------------------
105  CLI::App app;
106  std::string fileName;
107  double h {1.0};
108 
109  app.description("Estimates tangent using a binomial convolver.\n Typical use example:\n \t tangentBC [options] --input <fileName>\n");
110  auto filenameOpt = app.add_option("--input,-i,1",fileName,"input file name: FreemanChain (.fc) or a sequence of discrete points (.sdp).")->required()->check(CLI::ExistingFile);
111  app.add_option("--GridStep", h, "Grid step (default 1.0)", true);
112 
113  app.get_formatter()->column_width(40);
114  CLI11_PARSE(app, argc, argv);
115  // END parse command line using CLI ----------------------------------------------
116 
117  if(filenameOpt->count()>0){
118  std::string extension = fileName.substr( fileName.find_last_of(".") + 1 );
119  bool isSDP = extension == "sdp";
120  typedef Z2i::Space Space;
121  typedef Space::Point Point;
123  typedef Space::Integer Integer;
125  typedef std::vector< Point > Storage;
126  typedef Storage::const_iterator ConstIteratorOnPoints;
127 
128  std::vector< FreemanChain > vectFcs;
129  if(!isSDP)
130  {
131  vectFcs =
132  PointListReader< Point >:: getFreemanChainsFromFile<Integer> (fileName);
133  }
134  for(unsigned int i=0; i<vectFcs.size() || (i==0 && isSDP); i++){
135  Storage vectPts;
136  bool isClosed;
137  if(!isSDP)
138  {
139  isClosed = vectFcs.at(i).isClosed();
140  std::cout << "# grid curve " << i << "/" << vectFcs.size() << " "
141  << ( (isClosed)?"closed":"open" ) << std::endl;
142  FreemanChain::getContourPoints( vectFcs.at(i), vectPts );
143  }
144  else
145  {
147  Z2i::Point pf =vectPts[0];
148  Z2i::Point pl =vectPts[vectPts.size()-1];
149  isClosed = (pf[0]-pl[0])+(pf[1]-pl[1]) <= 1;
150  }
151 
152  // Binomial
153  std::cout << "# Curvature estimation from binomial convolution" << std::endl;
154  typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
155  std::cout << "# mask size = " <<
156  MyBinomialConvolver::suggestedSize( h, vectPts.begin(), vectPts.end() ) << std::endl;
157  typedef
159  TangentBCFct;
161  BCTangentEstimator;
162 
163  BCTangentEstimator.init( h, vectPts.begin(), vectPts.end(), isClosed );
164 
165  std::vector<RealPoint> tangents( vectPts.size() );
166  BCTangentEstimator.eval( vectPts.begin(), vectPts.end(),
167  tangents.begin() );
168 
169  // Output
170  std::cout << "# id tangent.x tangent.y angle(atan2(y,x)) x y" << std::endl;
171  unsigned int j = 0;
172  for ( ConstIteratorOnPoints
173  it = vectPts.begin(), it_end = vectPts.end();
174  it != it_end; ++it, ++j )
175  {
176  double x = tangents[ j ][ 0 ];
177  double y = tangents[ j ][ 1 ];
178  std::cout << j << std::setprecision( 15 )
179  << " " << x << " " << y
180  << " " << atan2( y, x ) << " " << (*it)[0] << " " << (*it)[1]
181  << std::endl;
182  }
183 
184  }
185 
186  }
187  return 0;
188 }
189 
DGtal::int32_t Integer
void init(const double h, const ConstIterator &itb, const ConstIterator &ite, const bool isClosed=true)
SpaceND< 2, Integer > Space
int main(int argc, char **argv)
static std::vector< TPoint > getPointsFromFile(const std::string &filename, std::vector< unsigned int > aVectPosition=std::vector< unsigned int >())
typename Self::Point Point
Quantity eval(const ConstIterator &it)
static void getContourPoints(const FreemanChain &fc, std::vector< Point > &aVContour)