DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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/ArithmeticalDSS.h"
51 #include "DGtal/geometry/curves/estimation/MostCenteredMaximalSegmentEstimator.h"
52 
53 
54 #include "ConfigTest.h"
55 
56 
57 using namespace DGtal;
58 using namespace std;
59 using namespace LibBoard;
60 
62 // Functions for testing class MostCenteredMaximalSegmentEstimator
64 
69 template<typename I>
70 bool test(const I& itb, const I& ite)
71 {
72  typedef I ConstIterator;//constIterator
74  typedef ArithmeticalDSS<ConstIterator,typename Point::Coordinate,4> SegmentComputer;//segmentComputer
75  typedef TangentVectorFromDSSEstimator<SegmentComputer> SCEstimator; //functor
76  typedef typename SCEstimator::Quantity Value; //value
78 
79  SegmentComputer sc;
80  SCEstimator f;
81 
82  Estimator e(sc,f);
83  e.init(1,itb,ite);
84 
85  unsigned int nb = 0;
86  unsigned int nbok = 0;
87  std::vector<Value> v1, v2, v3;
88 
89  {
90  trace.info() << "Eval at one element" << endl;
91  if (isNotEmpty(itb, ite))
92  {
93  ConstIterator it = itb;
94  do
95  {
96  Value q = e.eval(it);
97  cout << q << " ";
98  v1.push_back( q );
99  ++it;
100  } while (it != ite);
101  }
102  cout << endl;
103  }
104 
105  {
106  trace.info() << "Eval for each element between begin and end " << endl;
107  e.eval(itb, ite, std::back_inserter(v2));
108 
109  for (typename std::vector<Value>::iterator i = v2.begin(); i != v2.end(); ++i) {
110  cout << *i << " ";
111  }
112  cout << endl;
113  }
114 
115  nbok += ( ( v1.size() == v2.size() ) &&
116  ( std::equal(v1.begin(), v1.end(), v2.begin() ) ) )?1:0;
117  nb++;
118 
119  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
120 
121  if ( (ite-itb) >= 10)
122  {
123 
124  trace.info() << "Eval for each element between begin+4 and begin+9 " << endl;
125 
126  e.eval((itb+4),(itb+9),std::back_inserter(v3));
127 
128  for (typename vector<Value>::iterator i = v3.begin(); i != v3.end(); ++i) {
129  cout << *i << " ";
130  }
131  cout << endl;
132 
133  nbok += ( (v3.size() == 5) &&
134  ( std::equal( (v1.begin()+4), (v1.begin()+9), v3.begin()) ) )?1:0;
135  nb++;
136 
137  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
138  }
139 
140  return (nb == nbok);
141 }
142 
147 bool testEval(string filename)
148 {
149 
150  trace.info() << endl;
151  trace.info() << "Reading GridCurve from " << filename << endl;
152 
153  ifstream instream; // input stream
154  instream.open (filename.c_str(), ifstream::in);
155  typedef KhalimskySpaceND<2> Kspace; //space
156  GridCurve<Kspace> c; //building grid curve
157  c.initFromVectorStream(instream);
158  typedef GridCurve<Kspace >::PointsRange Range;//range
159  Range r = c.getPointsRange();//building range
160 
161  trace.info() << "Building Estimator (process range as";
162  trace.info() << ( (c.isClosed())?"closed":"open" ) << ")" << endl;
163 
164  if (c.isClosed())
165  return test(r.c(), r.c());
166  else
167  return test(r.begin(), r.end());
168 
169 }
170 
174 
175 int main(int argc, char **argv)
176 {
177 
178  trace.beginBlock ( "Testing class MostCenteredMaximalSegmentEstimator" );
179  trace.info() << "Args:";
180  for ( int i = 0; i < argc; ++i )
181  trace.info() << " " << argv[ i ];
182  trace.info() << endl;
183 
184  std::string sinus2D4 = testPath + "samples/sinus2D4.dat";
185  std::string square = testPath + "samples/smallSquare.dat";
186  std::string dss = testPath + "samples/DSS.dat";
187 
188  bool res = testEval(sinus2D4)
189  && testEval(square)
190  && testEval(dss)
191  //other tests
192  ;
193  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
194  trace.endBlock();
195 
196  return res ? 0 : 1;
197 
198 }