DGtal  1.5.beta
testFrechetShortcut.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include <iterator>
33 #include <cstdio>
34 #include <cmath>
35 #include <fstream>
36 #include <vector>
37 
38 #include "DGtal/base/Common.h"
39 #include "DGtal/base/Exceptions.h"
40 #include "DGtal/kernel/SpaceND.h"
41 
42 #include "DGtal/kernel/domains/HyperRectDomain.h"
43 #include "DGtal/geometry/curves/FrechetShortcut.h"
44 #include "DGtal/io/boards/Board2D.h"
45 
46 #include "DGtal/io/boards/CDrawableWithBoard2D.h"
47 #include "DGtal/geometry/curves/CForwardSegmentComputer.h"
48 #include "DGtal/helpers/StdDefs.h"
49 
50 #include "DGtal/geometry/curves/GreedySegmentation.h"
51 
52 #include "ConfigTest.h"
53 
55 
56 using namespace std;
57 using namespace DGtal;
58 using namespace LibBoard;
59 using namespace Z2i;
60 
62 // Functions for testing class FrechetShortcut.
64 
69 {
70  unsigned int nbok = 0;
71  unsigned int nb = 0;
72 
73  typedef PointVector<2,int> Point;
74  typedef std::vector<Point>::iterator Iterator;
75  typedef FrechetShortcut<Iterator,int> Shortcut;
76 
77  std::vector<Point> contour;
78  contour.push_back(Point(0,0));
79  contour.push_back(Point(1,0));
80  contour.push_back(Point(2,0));
81  contour.push_back(Point(3,0));
82  contour.push_back(Point(4,0));
83  contour.push_back(Point(5,0));
84  contour.push_back(Point(6,0));
85  contour.push_back(Point(7,0));
86  contour.push_back(Point(7,1));
87  contour.push_back(Point(6,1));
88  contour.push_back(Point(5,1));
89  contour.push_back(Point(4,1));
90  contour.push_back(Point(3,1));
91  contour.push_back(Point(2,1));
92  contour.push_back(Point(3,2));
93  contour.push_back(Point(4,2));
94  contour.push_back(Point(5,2));
95  contour.push_back(Point(6,2));
96  contour.push_back(Point(7,2));
97  contour.push_back(Point(8,2));
98  contour.push_back(Point(9,2));
99 
100  trace.beginBlock ( "Testing block ..." );
101 
102  Shortcut s(5);
103  s.init(contour.begin());
104 
105  trace.info() << s << std::endl;
106 
107  while ( (s.end() != contour.end())
108  &&(s.extendFront()) ) {}
109 
110  trace.info() << s << std::endl;
111 
112 
113  trace.endBlock();
114 
115  return nbok == nb;
116 }
117 
118 
119 
121 {
122  typedef PointVector<2,int> Point;
123  typedef std::vector<Point>::const_iterator ConstIterator;
124  typedef FrechetShortcut<ConstIterator,int> Shortcut;
125  BOOST_CONCEPT_ASSERT(( concepts::CDrawableWithBoard2D<Shortcut> ));
126  BOOST_CONCEPT_ASSERT(( concepts::CForwardSegmentComputer<Shortcut> ));
127 }
128 
130 {
131  typedef PointVector<2,int> Point;
132 
133  std::vector<Point> contour;
134  contour.push_back(Point(0,0));
135  contour.push_back(Point(1,0));
136  contour.push_back(Point(2,0));
137  contour.push_back(Point(3,0));
138  contour.push_back(Point(4,0));
139  contour.push_back(Point(5,0));
140  contour.push_back(Point(6,0));
141  contour.push_back(Point(7,0));
142  contour.push_back(Point(7,1));
143  contour.push_back(Point(6,1));
144  contour.push_back(Point(5,1));
145  contour.push_back(Point(4,1));
146  contour.push_back(Point(3,1));
147  contour.push_back(Point(2,1));
148  contour.push_back(Point(2,2));
149  contour.push_back(Point(3,2));
150  contour.push_back(Point(4,2));
151  contour.push_back(Point(5,2));
152  contour.push_back(Point(6,2));
153  contour.push_back(Point(7,2));
154  contour.push_back(Point(8,2));
155  contour.push_back(Point(9,2));
156 
157  trace.beginBlock ( "Testing block ..." );
158 
159  typedef Curve::PointsRange::ConstIterator Iterator;
161 
162  Curve aCurve; //grid curve
163  aCurve.initFromVector(contour);
164 
165  typedef Curve::PointsRange Range; //range
166  Range r = aCurve.getPointsRange(); //range
167 
168  Board2D board;
169  board << r;
170  board << aCurve.getArrowsRange();
171 
172  // Test when error = 3
173 
174 
175  int nbok = 3;
176  int nb=0;
177  trace.beginBlock ( "Greedy segmentation" );
178  {
180  Segmentation theSegmentation( r.begin(), r.end(), SegmentComputer(3) );
181 
182  Segmentation::SegmentComputerIterator it = theSegmentation.begin();
183  Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
184 
185  for ( ; it != itEnd; ++it) {
186  SegmentComputer s(*it);
187  trace.info() << s << std::endl;
188  board << (*it);
189  nb++;
190  }
191  trace.info() << theSegmentation << std::endl;
192  board.saveEPS("FrechetShortcutGreedySegmentationTest.eps", Board2D::BoundingBox, 5000 );
193  }
194 
195  // test when error = 0
196 
197 
198  int nbok2 = 5;
199  int nb2=0;
200  trace.beginBlock ( "Greedy segmentation" );
201  {
203  Segmentation theSegmentation( r.begin(), r.end(), SegmentComputer(0) );
204 
205  Segmentation::SegmentComputerIterator it = theSegmentation.begin();
206  Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
207 
208  for ( ; it != itEnd; ++it) {
209  SegmentComputer s(*it);
210  trace.info() << s << std::endl;
211  board << (*it);
212  nb2++;
213  }
214  trace.info() << theSegmentation << std::endl;
215  board.saveEPS("FrechetShortcutGreedySegmentationTest.eps", Board2D::BoundingBox, 5000 );
216  }
217 
218 
219  /* Saturated segmentation does not work for FrechetShortcut
220  computer. Indeed, given two maximal Frechet shortcuts s1(begin, end) et
221  s2(begin, end), we can have s1.begin < s2.begin < s2.end <
222  s1.end. */
223 
224 
225  trace.endBlock();
226 
227  return (nbok == nb) && (nbok2==nb2);
228 }
229 
230 
231 
232 bool testSegmentationLarger(const string& filename, int min, int max, double delta)
233 {
234  // Build output filename
235 
236  std::size_t pos1 = filename.find("samples/");
237  std::size_t init = pos1+8;
238  std::size_t pos2 = filename.find(".dat");
239  std::size_t end = pos2-init;
240  std::string output = filename.substr(init,end);
241 
242  trace.beginBlock ( "Testing block ..." );
243 
244  trace.beginBlock ( "Greedy segmentation on larger contours" );
245 
246  trace.info() << "Reading input curve" << filename << std::endl;
247 
248  typedef Curve::PointsRange::ConstIterator Iterator;
249 
250  Curve aCurve; //grid curve
251 
252  ifstream instream; // input stream
253  instream.open (filename.c_str(), ifstream::in);
254 
255  aCurve.initFromVectorStream(instream);
256 
257 
258  typedef Curve::PointsRange Range; //range
259  Range r = aCurve.getPointsRange(); //range
260 
261  Board2D board;
262  board << r;
263  board << aCurve.getArrowsRange();
264 
265  trace.info() << "Size of input curve = " << aCurve.size() << std::endl;
266 
267  typedef Curve::PointsRange::ConstIterator Iterator;
270 
271  for(double error = min; error <= max; error+=delta)
272  {
273  trace.info() << "error = " << error << "\t";
274  Segmentation theSegmentation( r.begin(), r.end(), SegmentComputer(error) );
275 
276  Segmentation::SegmentComputerIterator it = theSegmentation.begin();
277  Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
278 
279  int size = 0;
280  for ( ; it != itEnd; ++it)
281  size++;
282 
283  trace.info() << "size of simplified curve = " << size << std::endl;
284 
285  it = theSegmentation.begin();
286  for ( ; it != itEnd; ++it)
287  {
288  SegmentComputer s(*it);
289  board << (*it);
290  }
291  // save simplified curves in eps file
292  string outputFilename = "FrechetShortcut-"+output+".eps";
293  board.saveEPS(outputFilename.c_str(), Board2D::BoundingBox, 5000 );
294  }
295 
296  trace.endBlock();
297  trace.endBlock();
298 
299  return 1;
300 }
301 
302 
303 
304 
306 // Standard services - public :
307 
308 int main( int argc, char** argv )
309 {
310  trace.beginBlock ( "Testing class FrechetShortcut" );
311  trace.info() << "Args:";
312  for ( int i = 0; i < argc; ++i )
313  trace.info() << " " << argv[ i ];
314  trace.info() << endl;
315 
317 
318 
319  std::string Plant054 = testPath + "samples/Plant054.dat";
320  std::string beetle = testPath + "samples/beetle-1.dat";
321 
322 
323  bool res = testFrechetShortcut() && testSegmentation() && testSegmentationLarger(Plant054,0,20,0.5) && testSegmentationLarger(beetle,0,30,5); // && ... other tests
324  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
325  trace.endBlock();
326  return res ? 0 : 1;
327 }
328 // //
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
Aim: On-line computation Computation of the longest shortcut according to the Fréchet distance for a ...
Aim: Computes the greedy segmentation of a range given by a pair of ConstIterators....
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
ArrowsRange getArrowsRange() const
Definition: GridCurve.h:450
Storage::size_type size() const
Storage::const_iterator ConstIterator
Definition: GridCurve.h:314
bool initFromVector(const std::vector< Point > &aVectorOfPoints)
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
Aim: Specific iterator to visit all the maximal segments of a saturated segmentation.
Aim: Computes the saturated segmentation, that is the whole set of maximal segments within a range gi...
SaturatedSegmentation::SegmentComputerIterator end() const
SaturatedSegmentation::SegmentComputerIterator begin() const
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()
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:804
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
MessageStream error
Aim: The concept CDrawableWithBoard2D specifies what are the classes that admit an export with Board2...
Aim: Defines the concept describing a forward segment computer. Like any model of CIncrementalSegment...
ArithmeticalDSSComputer< std::vector< Z2i::Point >::const_iterator, int, 4 > SegmentComputer
SaturatedSegmentation< SegmentComputer > Segmentation
int max(int a, int b)
MyPointD Point
Definition: testClone2.cpp:383
K init(Point(0, 0, 0), Point(512, 512, 512), true)
int main(int argc, char **argv)
bool testSegmentationLarger(const string &filename, int min, int max, double delta)
void testFrechetShortcutConceptChecking()
bool testFrechetShortcut()
bool testSegmentation()