DGtal  0.9.2
exampleFrechetShortcut.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 
35 #include "ConfigExamples.h"
36 #include "DGtal/io/boards/Board2D.h"
37 #include "DGtal/geometry/curves/FrechetShortcut.h"
38 #include "DGtal/geometry/curves/GreedySegmentation.h"
40 
41 using namespace std;
42 using namespace DGtal;
43 using namespace Z2i;
44 
45 
46 
47 
49 
50 int main( int argc, char** argv )
51 {
52  trace.beginBlock ( "Example FrechetShortcut" );
53  trace.info() << "Args:";
54  for ( int i = 0; i < argc; ++i )
55  trace.info() << " " << argv[ i ];
56  trace.info() << endl;
57 
58  std::string filename;
59  double error;
60 
61  if(argc == 1)
62  {
63  trace.info() << "Use default file and error value\n";
64  filename = examplesPath + "samples/plant-frechet.dat";
65  error = 3;
66  }
67  else
68  if(argc != 3)
69  {
70  trace.info() << "Please enter a filename and error value.\n";
71  return 0;
72  }
73  else
74  {
75  filename = argv[1];
76  error = atof(argv[2]);
77  }
78  ifstream instream; // input stream
79  instream.open (filename.c_str(), ifstream::in);
80 
81 
82 
83  Curve c; //grid curve
84  c.initFromVectorStream(instream);
85 
86  Board2D board;
87 
88  // Display the pixels as arrows range to show the way the curve is scanned
89  board << c.getArrowsRange();
90 
91  trace.beginBlock("Simple example");
92 
94  Curve::PointsRange r = c.getPointsRange();
95 
97 
98  // Computation of one shortcut
99  Shortcut s(error);
100 
101  s.init( r.begin() );
102  while ( ( s.end() != r.end() )
103  &&( s.extendFront() ) ) {}
104 
105 
106 
107  // Computation of a greedy segmentation
108 
109  typedef GreedySegmentation<Shortcut> Segmentation;
110 
111  Segmentation theSegmentation( r.begin(), r.end(), Shortcut(error) );
112 
113  // the segmentation is computed here
114  Segmentation::SegmentComputerIterator it = theSegmentation.begin();
115  Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
116 
117  for ( ; it != itEnd; ++it) {
118  s=Shortcut(*it);
119  trace.info() << s << std::endl;
120  board << s;
121  }
122 
123  board.saveEPS("FrechetShortcutExample.eps", Board2D::BoundingBox, 5000 );
124 
126  #ifdef WITH_CAIRO
127  board.saveCairo("FrechetShortcutExample.png");
128  #endif
129 
130 
131  trace.endBlock();
132  return 0;
133 }
134 // //
void beginBlock(const std::string &keyword="")
Aim: On-line computation Computation of the longest shortcut according to the Fr├ęchet distance for a ...
Trace trace
Definition: Common.h:130
GridCurve< K2 > Curve
Definition: StdDefs.h:116
STL namespace.
double endBlock()
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1139
Aim: Computes the greedy segmentation of a range given by a pair of ConstIterators. The last element of a given segment is the first one one of the next segment.
MessageStream error
GreedySegmentation::SegmentComputerIterator begin() const
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70