DGtal  0.9.2
testSegmentComputerEstimators.cpp
1 
32 #include <iostream>
34 #include <cmath>
35 #include "DGtal/base/Common.h"
36 
37 
38 #include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
39 #include "DGtal/geometry/curves/StabbingCircleComputer.h"
40 #include "DGtal/geometry/curves/estimation/SegmentComputerEstimators.h"
41 
42 #include "DGtal/io/boards/Board2D.h"
43 
44 
45 #include "ConfigTest.h"
46 
48 
49 using namespace std;
50 using namespace DGtal;
51 using namespace LibBoard;
52 
54 // Functions for testing classes in SegmentComputerEstimators.h.
56 
57 
62 template<typename DSSComputer>
63 bool testTangentFromDSS(
64  const typename DSSComputer::ConstIterator& itb,
65  const typename DSSComputer::ConstIterator& ite )
66 {
67 
68 
69  trace.info() << "feeding segment computer " << endl;
70 
71  DSSComputer dss;
72  DGtal::longestSegment( dss, itb, ite );
73 
74  trace.info() << dss << endl;
75  trace.info() << endl;
76 
77  trace.info() << "building and using the functor " << endl;
78  const double epsilon = 0.00001;
79 
80  int nb = 0;
81  int nbok = 0;
82 
83  { //angle
85  f.attach(dss);
86  double q1 = f.eval(itb);
87  double q2 = std::atan2((double)dss.a(),(double)dss.b());
88  trace.info() << "Tangent orientation : " << q1 << " == " << q2 << endl;
89  nbok += (std::abs(q1 - q2) < epsilon)?1:0;
90  nb++;
91  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
92  }
93 
94  { //vector
97  f.attach(dss);
98  Quantity q1 = f.eval(itb);
99  Quantity q2 = Quantity(dss.b(), dss.a());
100  trace.info() << "Tangent vector : " << q1 << " == " << q2 << endl;
101  nbok += (q1 == q2)?1:0;
102  nb++;
103  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
104  }
105 
106  { //unit vector
108  typedef typename TangentFromDSSEstimator<DSSComputer>::Quantity Quantity;
109  f.attach(dss);
110  Quantity q1 = f.eval(itb);
111  double n = std::sqrt( (double)dss.a()*dss.a() + (double)dss.b()*dss.b() );
112  Quantity q2 = Quantity((double)dss.b()/n, (double)dss.a()/n);
113  trace.info() << "Normalized tangent vector : " << q1 << " == " << q2 << endl;
114  nbok += ((std::abs(q1[0] - q2[0]) < epsilon)&&(std::abs(q1[1] - q2[1]) < epsilon))?1:0;
115  nb++;
116  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
117  }
118 
119  return (nb == nbok);
120 }
121 
126 template<typename DCAComputer>
127 bool testFromDCA(
128  const typename DCAComputer::ConstIterator& itb,
129  const typename DCAComputer::ConstIterator& ite )
130 {
131 
132 
133  trace.info() << "feeding segment computer " << endl;
134 
135  DCAComputer dca;
136  DGtal::longestSegment( dca, itb, ite );
137 
138  trace.info() << dca << endl;
139  trace.info() << endl;
140 
141  trace.info() << "building and using the functor " << endl;
142  const double epsilon = 0.00001;
143 
144  int nb = 0;
145  int nbok = 0;
146 
147  { //curvature
149  f.init(1.0, itb, ite);
150  f.attach(dca);
151  double q1 = f.eval(itb);
152  f.init(0.1, itb, ite);
153  double q2 = f.eval(itb);
154  trace.info() << "Curvature (h=1): " << q1 << std::endl;
155  trace.info() << "Curvature (h=0.1): " << q2 << std::endl;
156  nbok += ((q1 >= 0)&&(q1 < 1)&&(q2 >= 0)&&(q2 < 10)
157  &&(std::abs(q2-(10*q1)) < epsilon))?1:0;
158  nb++;
159  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
160  }
161 
162  { //tangent
164  f.attach(dca);
165  typedef typename TangentFromDCAEstimator<DCAComputer>::Quantity Quantity;
166  typename DCAComputer::ConstIterator it = DGtal::getMiddleIterator(itb, ite);
167  Quantity q1 = f.eval( it );
168  trace.info() << "Tangent: " << q1 << " == [PointVector] {1, 0} " << std::endl;
169  nbok += (std::abs(q1[0]-1.0) < epsilon)
170  && (std::abs(q1[1]) < epsilon) ? 1 : 0;
171  nb++;
172  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
173  }
174 
175  { //position
177  typedef typename DistanceFromDCAEstimator<DCAComputer>::Quantity Quantity;
178  typename DCAComputer::ConstIterator it = DGtal::getMiddleIterator(itb, ite);
179  f.init(1.0, itb, ite);
180  f.attach(dca);
181  Quantity q1 = f.eval( it );
182  trace.info() << "Position (h=1): " << q1.first << " (<=0), " << q1.second << " (>0) " << std::endl;
183  f.init(0.1, itb, ite);
184  f.attach(dca);
185  Quantity q2 = f.eval( it );
186  trace.info() << "Position (h=0.1): " << q2.first << " (<=0), " << q2.second << " (>0) " << std::endl;
187  nbok += ( (q1.first < epsilon)
188  && (q1.second > -epsilon)
189  && (std::abs((q1.second - q1.first) - 1) < epsilon)
190  && (q2.first < epsilon)
191  && (q2.second > -epsilon)
192  && (std::abs((q2.second - q2.first) - 0.1) < epsilon) )? 1 : 0;
193  nb++;
194  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
195  }
196 
197  return (nb == nbok);
198 }
199 
200 
202 // Standard services - public :
203 
204 int main( int argc, char** argv )
205 {
206  trace.beginBlock ( "Testing segment computer functors" );
207  trace.info() << "Args:";
208  for ( int i = 0; i < argc; ++i )
209  trace.info() << " " << argv[ i ];
210  trace.info() << endl;
211 
212  bool res = true;
213 
214  //types
215  typedef PointVector<2,int> Point;
216 
217 
218  //------------------------------------------ DSS
219  {
220  typedef std::vector<Point> Range;
221  typedef Range::iterator ConstIterator;
224 
225  //input points
226  Range curve4;
227  curve4.push_back(Point(0,0));
228  curve4.push_back(Point(1,0));
229  curve4.push_back(Point(1,1));
230  curve4.push_back(Point(2,1));
231  curve4.push_back(Point(3,1));
232  curve4.push_back(Point(3,2));
233  curve4.push_back(Point(4,2));
234  curve4.push_back(Point(5,2));
235  curve4.push_back(Point(6,2));
236  curve4.push_back(Point(6,3));
237  curve4.push_back(Point(7,3));
238 
239  Range curve8;
240  curve8.push_back(Point(0,0));
241  curve8.push_back(Point(1,1));
242  curve8.push_back(Point(2,1));
243  curve8.push_back(Point(3,2));
244  curve8.push_back(Point(4,2));
245  curve8.push_back(Point(5,2));
246  curve8.push_back(Point(6,3));
247  curve8.push_back(Point(7,3));
248  curve8.push_back(Point(8,4));
249  curve8.push_back(Point(9,4));
250  curve8.push_back(Point(10,5));
251 
252  //tests
253  res = res && testTangentFromDSS<DSS4>(curve4.begin(), curve4.end())
254  && testTangentFromDSS<DSS8>(curve8.begin(), curve8.end());
255  }
256  //------------------------------------------ DCA
257  {
258  typedef std::pair<Point,Point> Pair;
259  typedef std::vector<Pair> Range;
260  typedef Range::const_iterator ConstIterator;
262 
263  Range curve;
264  curve.push_back(std::make_pair(Point(0,0),Point(0,1)));
265  curve.push_back(std::make_pair(Point(1,0),Point(1,1)));
266 
267  curve.push_back(std::make_pair(Point(2,1),Point(2,2)));
268  curve.push_back(std::make_pair(Point(3,1),Point(3,2)));
269  curve.push_back(std::make_pair(Point(4,1),Point(4,2)));
270 
271  curve.push_back(std::make_pair(Point(5,2),Point(5,3)));
272  curve.push_back(std::make_pair(Point(6,2),Point(6,3)));
273  curve.push_back(std::make_pair(Point(7,2),Point(7,3)));
274  curve.push_back(std::make_pair(Point(8,2),Point(8,3)));
275  curve.push_back(std::make_pair(Point(9,2),Point(9,3)));
276 
277  curve.push_back(std::make_pair(Point(10,1),Point(10,2)));
278  curve.push_back(std::make_pair(Point(11,1),Point(11,2)));
279  curve.push_back(std::make_pair(Point(12,1),Point(12,2)));
280 
281  curve.push_back(std::make_pair(Point(13,0),Point(13,1)));
282  curve.push_back(std::make_pair(Point(14,0),Point(14,1)));
283 
284  res = res && testFromDCA<DCA>(curve.begin(), curve.end())
285  && testFromDCA<DCA>(curve.begin()+2, curve.begin()+12);
286  }
287 
288  //----------------------------------------------------------
289  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
290  trace.endBlock();
291 
292  return res ? 0 : 1;
293 }
294 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
STL namespace.
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
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: On-line recognition of a digital circular arcs (DCA) defined as a sequence of connected grid edg...
void longestSegment(SC &s, const typename SC::ConstIterator &i, const typename SC::ConstIterator &end, IteratorType)
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
void init(const double h, const ConstIterator &itb, const ConstIterator &ite)
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
void init(const double h, const ConstIterator &itb, const ConstIterator &ite)
IC getMiddleIterator(const IC &itb, const IC &ite, RandomAccessCategory)