DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
exampleGridCurve2d.cpp
1 
30 
31 #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  SetPredicate<DigitalSet> predicate( set ); //predicate (from the digital set)
118 
119  //tracking and init grid curve
120  SCell s = Surfaces<KSpace>::findABel( ks, predicate, 1000 );
121  Surfaces<KSpace>::track2DBoundary( contour, ks, sAdj, predicate, s );
122  c2.initFromSCellsVector( contour );
124  }
125 
126  trace.info() << "\t from a FreemanChain (from a file) " << endl;
127  {
128  fstream inputStream;
129  inputStream.open (S.c_str(), ios::in);
130  FreemanChain<int> fc(inputStream);
131  inputStream.close();
132 
133  Curve c;
135  c.initFromPointsRange( fc.begin(), fc.end() );
137 
138  }
139 
140  trace.emphase() << "Output" << endl;
141  trace.info() << "\t standard output " << endl;
142  {
144  trace.info() << c1 << std::endl;
146  }
147  trace.info() << "\t into a data file " << endl;
148  {
150  ofstream outputStream("myGridCurve.dat");
151  if (outputStream.is_open())
152  c2.writeVectorToStream(outputStream);
153  outputStream.close();
155  }
156  trace.info() << "\t into a vector graphics file " << endl;
157  {
159  Board2D aBoard;
160  aBoard.setUnit(Board2D::UCentimeter);
161  aBoard << c2;
162  aBoard.saveEPS( "myGridCurve.eps", Board2D::BoundingBox, 5000 );
164  }
165 
166  trace.info() << "\t into a FreemanChain " << endl;
167  {
170  FreemanChain fc;
171  FreemanChain::readFromPointsRange( c1.getPointsRange(), fc );
172  trace.info() << "\t" << fc << endl;
174  }
175 
176  trace.emphase() << "Ranges Ouput" << endl;
177  {
178  Board2D aBoard;
179  aBoard.setUnit(Board2D::UCentimeter);
180 
181  Point low(-1,-1);
182  Point up(3,3);
183  Domain aDomain( low,up );
184 
185  {//1cellsRange
186  Curve::SCellsRange r = c1.getSCellsRange();
187 
188  trace.info() << r << endl;
189 
190  aBoard << SetMode(aDomain.className(), "Grid") << aDomain;
191  aBoard << r;
192  aBoard.saveEPS( "My1CellsRange.eps", Board2D::BoundingBox, 5000 );
193  aBoard.clear();
194  }
195  {//IncidentPointsRange
197  Curve::IncidentPointsRange r = c1.getIncidentPointsRange();
198 
199  trace.info() << r << endl;
200 
201  aBoard << SetMode(aDomain.className(), "Grid") << aDomain;
202  aBoard << r;
203  aBoard.saveEPS( "MyIncidentPointsRange.eps", Board2D::BoundingBox, 5000 );
205  aBoard.clear();
206  }
207  {//CodesRange
208  Curve::CodesRange r = c1.getCodesRange();
209 
210  trace.info() << r << endl;
211  }
212  }
213 
214  trace.emphase() << "Ranges Iterators" << endl;
215  {
216  typedef Curve::CodesRange Range;
217  Range r = c1.getCodesRange();
218 
220  trace.info() << "\t iterate over the range" << endl;
221  Range::ConstIterator it = r.begin();
222  Range::ConstIterator itEnd = r.end();
223  for ( ; it != itEnd; ++it)
224  {
225  trace.info() << *it;
226  }
227  trace.info() << endl;
228 
229  trace.info() << "\t iterate over the range in the reverse way" << endl;
230  Range::ConstReverseIterator rit = r.rbegin();
231  Range::ConstReverseIterator ritEnd = r.rend();
232  for ( ; rit != ritEnd; ++rit)
233  {
234  trace.info() << *rit;
235  }
236  trace.info() << endl;
237 
238  trace.info() << "\t iterate over the range in a circular way" << endl;
239  Range::ConstCirculator c = r.c();
240  //set the starting element wherever you want...
241  for (unsigned i = 0; i < 20; ++i) ++c;
242  //... and circulate
243  Range::ConstCirculator cend( c );
244  do
245  {
246  trace.info() << *c;
247  c++;
248  } while (c!=cend);
249  trace.info() << endl;
251 
252  trace.info() << "\t Generic function working with any (circular)iterator" << endl;
253  displayAll<Range::ConstIterator>(r.begin(),r.end());
254  displayAll<Range::ConstReverseIterator>(r.rbegin(),r.rend());
255  displayAll<Range::ConstCirculator>(r.c(),r.c());
256 
257  }
258 
259  trace.endBlock();
260  return 0;
261 }
262 // //