DGtal  0.9.2
testMostCenteredMSEstimator.cpp
1 
16 //LICENSE-END
33 #include <cstdio>
34 #include <cmath>
35 #include <fstream>
36 #include <vector>
37 #include <iostream>
38 #include <iterator>
39 
40 
41 
42 
43 #include "DGtal/base/Common.h"
44 #include "DGtal/base/Exceptions.h"
45 #include "DGtal/io/boards/Board2D.h"
46 
47 #include "DGtal/topology/KhalimskySpaceND.h"
48 
49 #include "DGtal/geometry/curves/GridCurve.h"
50 #include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
51 #include "DGtal/geometry/curves/estimation/MostCenteredMaximalSegmentEstimator.h"
52 #include "DGtal/geometry/curves/estimation/CCurveLocalGeometricEstimator.h"
53 
54 
55 #include "ConfigTest.h"
56 
57 
58 using namespace DGtal;
59 using namespace std;
60 using namespace LibBoard;
61 
63 // Functions for testing class MostCenteredMaximalSegmentEstimator
65 
70 template<typename I>
71 bool test(const I& itb, const I& ite)
72 {
73  typedef I ConstIterator;//constIterator
75  typedef ArithmeticalDSSComputer<ConstIterator,typename Point::Coordinate,4> SegmentComputer;//segmentComputer
76  typedef TangentVectorFromDSSEstimator<SegmentComputer> SCEstimator; //functor
77  typedef typename SCEstimator::Quantity Value; //value
80  SegmentComputer sc;
81  SCEstimator f;
82 
83  Estimator e(sc,f);
84  e.init(1,itb,ite);
85 
86  unsigned int nb = 0;
87  unsigned int nbok = 0;
88  std::vector<Value> v1, v2, v3;
89 
90  {
91  trace.info() << "Eval at one element" << endl;
92  if (isNotEmpty(itb, ite))
93  {
94  ConstIterator it = itb;
95  do
96  {
97  Value q = e.eval(it);
98  cout << q << " ";
99  v1.push_back( q );
100  ++it;
101  } while (it != ite);
102  }
103  cout << endl;
104  }
105 
106  {
107  trace.info() << "Eval for each element between begin and end " << endl;
108  e.eval(itb, ite, std::back_inserter(v2));
109 
110  for (typename std::vector<Value>::iterator i = v2.begin(); i != v2.end(); ++i) {
111  cout << *i << " ";
112  }
113  cout << endl;
114  }
115 
116  nbok += ( ( v1.size() == v2.size() ) &&
117  ( std::equal(v1.begin(), v1.end(), v2.begin() ) ) )?1:0;
118  nb++;
119 
120  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
121 
122  if ( (ite-itb) >= 10)
123  {
124 
125  trace.info() << "Eval for each element between begin+4 and begin+9 " << endl;
126 
127  e.eval((itb+4),(itb+9),std::back_inserter(v3));
128 
129  for (typename vector<Value>::iterator i = v3.begin(); i != v3.end(); ++i) {
130  cout << *i << " ";
131  }
132  cout << endl;
133 
134  nbok += ( (v3.size() == 5) &&
135  ( std::equal( (v1.begin()+4), (v1.begin()+9), v3.begin()) ) )?1:0;
136  nb++;
137 
138  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
139  }
140 
141  return (nb == nbok);
142 }
143 
148 bool testEval(string filename)
149 {
150 
151  trace.info() << endl;
152  trace.info() << "Reading GridCurve from " << filename << endl;
153 
154  ifstream instream; // input stream
155  instream.open (filename.c_str(), ifstream::in);
156  typedef KhalimskySpaceND<2> Kspace; //space
157  GridCurve<Kspace> c; //building grid curve
158  c.initFromVectorStream(instream);
159  typedef GridCurve<Kspace >::PointsRange Range;//range
160  Range r = c.getPointsRange();//building range
161 
162  trace.info() << "Building Estimator (process range as";
163  trace.info() << ( (c.isClosed())?"closed":"open" ) << ")" << endl;
164 
165  if (c.isClosed())
166  return test(r.c(), r.c());
167  else
168  return test(r.begin(), r.end());
169 
170 }
171 
175 
176 int main(int argc, char **argv)
177 {
178 
179  trace.beginBlock ( "Testing class MostCenteredMaximalSegmentEstimator" );
180  trace.info() << "Args:";
181  for ( int i = 0; i < argc; ++i )
182  trace.info() << " " << argv[ i ];
183  trace.info() << endl;
184 
185  std::string sinus2D4 = testPath + "samples/sinus2D4.dat";
186  std::string square = testPath + "samples/smallSquare.dat";
187  std::string dss = testPath + "samples/DSS.dat";
188 
189  bool res = testEval(sinus2D4)
190  && testEval(square)
191  && testEval(dss)
192  //other tests
193  ;
194  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
195  trace.endBlock();
196 
197  return res ? 0 : 1;
198 
199 }
void beginBlock(const std::string &keyword="")
PointsRange getPointsRange() const
Definition: GridCurve.h:426
Trace trace
Definition: Common.h:130
Aim: A model of CLocalCurveGeometricEstimator that assigns to each element of a (sub)range a quantity...
STL namespace.
double endBlock()
bool isClosed() const
Struct representing a 2D point.
Definition: Point.h:27
Aim: This class is a wrapper around ArithmeticalDSS that is devoted to the dynamic recognition of dig...
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
std::ostream & emphase()
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...
Aim: This concept describes an object that can process a range so as to return one estimated quantity...
bool initFromVectorStream(std::istream &in)
std::ostream & info()
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:172
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
bool isNotEmpty(const IC &itb, const IC &ite)