DGtal  0.9.2
exampleGridCurve2d.cpp
1 
30 #include <iostream>
32 
33 #include "DGtal/base/Common.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "ConfigExamples.h"
36 
37 #include "DGtal/geometry/curves/FreemanChain.h"
38 #include "DGtal/geometry/curves/GridCurve.h"
39 
40 #include "DGtal/topology/helpers/Surfaces.h"
41 #include "DGtal/io/boards/Board2D.h"
42 
44 
45 using namespace std;
46 using namespace DGtal;
47 using namespace Z2i;
48 
51 template <typename CI>
52 void displayAll( const CI& ciBegin, const CI& ciEnd )
53 {
54  if ( isNotEmpty(ciBegin, ciEnd) )
55  { //if the range is not empty
56  CI i( ciBegin);
57  do
58  {
59  trace.info() << *i;
60  i++;
61  } while (i != ciEnd);
62  trace.info() << endl;
63  }
64 }
66 
68 int main( int argc, char** argv )
69 {
70  trace.beginBlock ( "Example for 2d gridcurves" );
71  trace.info() << "Args:";
72  for ( int i = 0; i < argc; ++i )
73  trace.info() << " " << argv[ i ];
74  trace.info() << endl;
75 
76  string square = examplesPath + "samples/smallSquare.dat";
77  string S = examplesPath + "samples/contourS.fc";
78 
79  // domain
80  Point lowerBound( -50, -50 );
81  Point upperBound( 50, 50 );
82 
84  //default construction
85  Curve c1;
86 
87  //from a Khalimsky space
88  K2 ks; ks.init( lowerBound, upperBound, true );
89  Curve c2( ks );
91 
92  trace.emphase() << "Input" << endl;
93  trace.info() << "\t from a data file " << endl;
94  {
96  fstream inputStream;
97  inputStream.open (square.c_str(), ios::in);
98  c1.initFromVectorStream(inputStream);
99  inputStream.close();
101  }
102  trace.info() << "\t from a digital set " << endl;
103  {
104 
105  // digital set: diamond of radius 30 centered at the origin
106  Point o( 0, 0 );
107  Domain domain( lowerBound, upperBound );
108  DigitalSet set( domain );
109  for ( Domain::ConstIterator it = domain.begin(); it != domain.end(); ++it )
110  {
111  if ( (*it - o ).norm1() <= 30 ) set.insertNew( *it );
112  }
113 
115  vector<SCell> contour; //contour
116  SurfelAdjacency<K2::dimension> sAdj( true ); //adjacency
117 
118  //tracking and init grid curve
119  SCell s = Surfaces<KSpace>::findABel( ks, set, 1000 );
120  Surfaces<KSpace>::track2DBoundary( contour, ks, sAdj, set, s );
121  c2.initFromSCellsVector( contour );
123  }
124 
125  trace.info() << "\t from a FreemanChain (from a file) " << endl;
126  {
127  fstream inputStream;
128  inputStream.open (S.c_str(), ios::in);
129  FreemanChain<int> fc(inputStream);
130  inputStream.close();
131 
132  Curve c;
134  c.initFromPointsRange( fc.begin(), fc.end() );
136 
137  }
138 
139  trace.emphase() << "Output" << endl;
140  trace.info() << "\t standard output " << endl;
141  {
143  trace.info() << c1 << std::endl;
145  }
146  trace.info() << "\t into a data file " << endl;
147  {
149  ofstream outputStream("myGridCurve.dat");
150  if (outputStream.is_open())
151  c2.writeVectorToStream(outputStream);
152  outputStream.close();
154  }
155  trace.info() << "\t into a vector graphics file " << endl;
156  {
158  Board2D aBoard;
159  aBoard.setUnit(Board2D::UCentimeter);
160  aBoard << c2;
161  aBoard.saveEPS( "myGridCurve.eps", Board2D::BoundingBox, 5000 );
163  }
164 
165  trace.info() << "\t into a FreemanChain " << endl;
166  {
169  FreemanChain fc;
170  FreemanChain::readFromPointsRange( c1.getPointsRange(), fc );
171  trace.info() << "\t" << fc << endl;
173  }
174 
175  trace.emphase() << "Ranges Ouput" << endl;
176  {
177  Board2D aBoard;
178  aBoard.setUnit(Board2D::UCentimeter);
179 
180  Point low(-1,-1);
181  Point up(3,3);
182  Domain aDomain( low,up );
183 
184  {//1cellsRange
185  Curve::SCellsRange r = c1.getSCellsRange();
186 
187  trace.info() << r << endl;
188 
189  aBoard << SetMode(aDomain.className(), "Grid") << aDomain;
190  aBoard << r;
191  aBoard.saveEPS( "My1CellsRange.eps", Board2D::BoundingBox, 5000 );
192  aBoard.clear();
193  }
194  {//IncidentPointsRange
196  Curve::IncidentPointsRange r = c1.getIncidentPointsRange();
197 
198  trace.info() << r << endl;
199 
200  aBoard << SetMode(aDomain.className(), "Grid") << aDomain;
201  aBoard << r;
202  aBoard.saveEPS( "MyIncidentPointsRange.eps", Board2D::BoundingBox, 5000 );
204  aBoard.clear();
205  }
206  {//CodesRange
207  Curve::CodesRange r = c1.getCodesRange();
208 
209  trace.info() << r << endl;
210  }
211  }
212 
213  trace.emphase() << "Ranges Iterators" << endl;
214  {
215  typedef Curve::CodesRange Range;
216  Range r = c1.getCodesRange();
217 
219  trace.info() << "\t iterate over the range" << endl;
220  Range::ConstIterator it = r.begin();
221  Range::ConstIterator itEnd = r.end();
222  for ( ; it != itEnd; ++it)
223  {
224  trace.info() << *it;
225  }
226  trace.info() << endl;
227 
228  trace.info() << "\t iterate over the range in the reverse way" << endl;
230  Range::ConstReverseIterator ritEnd = r.rend();
231  for ( ; rit != ritEnd; ++rit)
232  {
233  trace.info() << *rit;
234  }
235  trace.info() << endl;
236 
237  trace.info() << "\t iterate over the range in a circular way" << endl;
238  Range::ConstCirculator c = r.c();
239  //set the starting element wherever you want...
240  for (unsigned i = 0; i < 20; ++i) ++c;
241  //... and circulate
242  Range::ConstCirculator cend( c );
243  do
244  {
245  trace.info() << *c;
246  c++;
247  } while (c!=cend);
248  trace.info() << endl;
250 
251  trace.info() << "\t Generic function working with any (circular)iterator" << endl;
252  displayAll<Range::ConstIterator>(r.begin(),r.end());
253  displayAll<Range::ConstReverseIterator>(r.rbegin(),r.rend());
254  displayAll<Range::ConstCirculator>(r.c(),r.c());
255 
256  }
257 
258  trace.endBlock();
259  return 0;
260 }
261 // //
void beginBlock(const std::string &keyword="")
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition: StdDefs.h:100
const ConstIterator & begin() const
const ConstIterator & end() const
Trace trace
Definition: Common.h:130
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
GridCurve< K2 > Curve
Definition: StdDefs.h:116
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
STL namespace.
double endBlock()
KSpace K2
Definition: StdDefs.h:78
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
bool isNotEmpty(const IC &itb, const IC &ite, IteratorType)
std::ostream & emphase()
bool init(const Point &lower, const Point &upper, bool isClosed)
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
std::reverse_iterator< ConstIterator > ConstReverseIterator
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:85
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
Modifier class in a Board2D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithBoard2D.
Definition: Board2D.h:247
void setUnit(Unit unit)
Definition: Board.cpp:240
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70