DGtal  1.4.beta
testFP.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include <fstream>
33 
34 #include "DGtal/base/Common.h"
35 #include "DGtal/kernel/SpaceND.h"
36 #include "DGtal/kernel/domains/HyperRectDomain.h"
37 #include "DGtal/geometry/curves/FreemanChain.h"
38 #include "DGtal/geometry/curves/FP.h"
39 #include "DGtal/io/boards/Board2D.h"
40 
41 #include "ConfigTest.h"
43 
44 using namespace std;
45 
47 // Functions for testing class FP.
49 
50 
51 
56 bool testFP(string filename)
57 {
58 
59  using namespace DGtal;
60 
61  trace.info() << endl;
62  trace.info() << "Reading GridCurve from " << filename << endl;
63 
64  ifstream instream; // input stream
65  instream.open (filename.c_str(), ifstream::in);
66 
67  //range of points
68  typedef int Coordinate;
69  typedef KhalimskySpaceND<2,Coordinate> Kspace; //space
70  GridCurve<Kspace> c; //building grid curve
71  c.initFromVectorStream(instream);
73  Range r = c.getPointsRange();//building range
74 
75 
76  //faithful polyon
77  trace.info() << "Building FP (process digital curve as";
78  trace.info() << ( (c.isClosed())?"closed":"open" ) << ")" << endl;
79 
80  bool res = true;
81  if (c.isClosed())
82  {
84  FP theFP( r.c(), r.c() );
85  res = theFP.isValid();
86  }
87  else
88  {
90  FP theFP( r.begin(), r.end() );
91  res = theFP.isValid();
92  }
93  return res;
94 
95 }
96 
97 template <typename Range1, typename Range2>
98 bool compare(const Range1& pts, const Range2& groundTruth)
99 {
100 
101  DGtal::GridCurve<> curve;
102  curve.initFromPointsRange(pts.begin(), pts.end());
103 
104  typedef DGtal::GridCurve<>::PointsRange::ConstCirculator ConstCirculator;
106  FaithfulPolygon theFP( curve.getPointsRange().c(), curve.getPointsRange().c() );
107 
108  return ( (theFP.polygon().size() == groundTruth.size()) &&
109  std::equal(theFP.polygon().begin(), theFP.polygon().end(), groundTruth.begin()) );
110 }
111 
113 {
114  using namespace DGtal;
115  using namespace Z2i;
116 
117  int nbok = 0;
118  int nb = 0;
119 
120  trace.beginBlock ( "Stopping criterion" );
121 
122  { //inflection part, one leaning point
123  std::vector<Point> pts, pts2;
124  pts.push_back(Point(0,0));
125  pts.push_back(Point(1,0));
126  pts.push_back(Point(1,1));
127  pts.push_back(Point(2,1));
128  pts.push_back(Point(3,1));
129  pts.push_back(Point(3,0));
130  pts.push_back(Point(4,0));
131  pts.push_back(Point(4,1));
132  pts.push_back(Point(4,2));
133  pts.push_back(Point(3,2));
134  pts.push_back(Point(2,2));
135  pts.push_back(Point(1,2));
136  pts.push_back(Point(0,2));
137  pts.push_back(Point(-1,2));
138  pts.push_back(Point(-2,2));
139  pts.push_back(Point(-2,1));
140  pts.push_back(Point(-2,0));
141  pts.push_back(Point(-1,0));
142 
143  pts2.push_back(Point(1,1));
144  pts2.push_back(Point(3,1));
145  pts2.push_back(Point(3,0));
146  pts2.push_back(Point(4,0));
147  pts2.push_back(Point(4,2));
148  pts2.push_back(Point(-2,2));
149  pts2.push_back(Point(-2,0));
150  pts2.push_back(Point(1,0));
151 
152  if(compare(pts, pts2))
153  nbok++;
154  nb++;
155 
156  trace.info() << nbok << " / " << nb << std::endl;
157  }
158 
159  { //inflection part, two distinct leaning points
160  std::vector<Point> pts, pts2;
161  pts.push_back(Point(0,0));
162  pts.push_back(Point(1,0));
163  pts.push_back(Point(1,1));
164  pts.push_back(Point(2,1));
165  pts.push_back(Point(3,1));
166  pts.push_back(Point(4,1));
167  pts.push_back(Point(4,0));
168  pts.push_back(Point(5,0));
169  pts.push_back(Point(5,1));
170  for (int i = 5; i >= -2; --i)
171  pts.push_back(Point(i,2));
172  pts.push_back(Point(-2,1));
173  pts.push_back(Point(-2,0));
174  pts.push_back(Point(-1,0));
175 
176  pts2.push_back(Point(1,1));
177  pts2.push_back(Point(4,1));
178  pts2.push_back(Point(4,0));
179  pts2.push_back(Point(5,0));
180  pts2.push_back(Point(5,2));
181  pts2.push_back(Point(-2,2));
182  pts2.push_back(Point(-2,0));
183  pts2.push_back(Point(1,0));
184 
185  if(compare(pts, pts2))
186  nbok++;
187  nb++;
188 
189  trace.info() << nbok << " / " << nb << std::endl;
190  }
191 
192  { //convex part, one leaning point
193  std::vector<Point> pts, pts2;
194  pts.push_back(Point(0,0));
195  pts.push_back(Point(1,0));
196  pts.push_back(Point(1,1));
197  pts.push_back(Point(2,1));
198  pts.push_back(Point(3,1));
199  pts.push_back(Point(3,2));
200  for (int i = 3; i >= -2; --i)
201  pts.push_back(Point(i,3));
202  pts.push_back(Point(-2,2));
203  pts.push_back(Point(-2,1));
204  pts.push_back(Point(-2,0));
205  pts.push_back(Point(-1,0));
206 
207  //FP begins at the last leaning point of the first MS
208  pts2.push_back(Point(1,0));
209  pts2.push_back(Point(3,1));
210  pts2.push_back(Point(3,3));
211  pts2.push_back(Point(-2,3));
212  pts2.push_back(Point(-2,0));
213 
214  if(compare(pts, pts2))
215  nbok++;
216  nb++;
217 
218  trace.info() << nbok << " / " << nb << std::endl;
219  }
220 
221  { //convex part, two distinct leaning points
222  std::vector<Point> pts, pts2;
223  pts.push_back(Point(0,0));
224  pts.push_back(Point(1,0));
225  pts.push_back(Point(1,1));
226  pts.push_back(Point(2,1));
227  pts.push_back(Point(3,1));
228  pts.push_back(Point(3,2));
229  for (int i = 3; i >= -1; --i)
230  pts.push_back(Point(i,3));
231  pts.push_back(Point(-1,2));
232  pts.push_back(Point(-1,1));
233  pts.push_back(Point(-1,0));
234 
235  //FP begins at the last leaning point of the first MS
236  pts2.push_back(Point(3,1));
237  pts2.push_back(Point(3,3));
238  pts2.push_back(Point(-1,3));
239  pts2.push_back(Point(-1,0));
240  pts2.push_back(Point(1,0));
241 
242  if(compare(pts, pts2))
243  nbok++;
244  nb++;
245 
246  trace.info() << nbok << " / " << nb << std::endl;
247  }
248  trace.endBlock();
249 
250  return (nb == nbok);
251 }
252 
254 // Standard services - public :
255 
256 int main( int argc, char** argv )
257 {
258  using namespace DGtal;
259 
260  trace.beginBlock ( "Testing class FP" );
261  trace.info() << "Args:";
262  for ( int i = 0; i < argc; ++i )
263  trace.info() << " " << argv[ i ];
264  trace.info() << endl;
265 
266  string sinus2D4 = testPath + "samples/sinus2D4.dat";
267  string square = testPath + "samples/smallSquare.dat";
268  string dss = testPath + "samples/DSS.dat";
269 
270  bool res = testFP(sinus2D4)
271  && testFP(square)
272  && testFP(dss)
274  ;
275 
276  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
277  trace.endBlock();
278  return res ? 0 : 1;
279 }
280 // //
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:86
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
ConstCirculator c() const
Aim: Computes the faithful polygon (FP) of a range of 4/8-connected 2D Points.
Definition: FP.h:293
bool isValid() const
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:173
bool initFromVectorStream(std::istream &in)
PointsRange getPointsRange() const
Definition: GridCurve.h:426
bool initFromPointsRange(const TIterator &itb, const TIterator &ite)
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
MyPointD Point
Definition: testClone2.cpp:383
bool compare(const Range1 &pts, const Range2 &groundTruth)
Definition: testFP.cpp:98
int main(int argc, char **argv)
Definition: testFP.cpp:256
bool stoppingCriterionTest()
Definition: testFP.cpp:112
bool testFP(string filename)
Definition: testFP.cpp:56