DGtal 1.3.0
Loading...
Searching...
No Matches
exampleGridCurve2d.cpp
Go to the documentation of this file.
1
41#include <iostream>
42
43#include "DGtal/base/Common.h"
44#include "DGtal/helpers/StdDefs.h"
45#include "ConfigExamples.h"
46
47#include "DGtal/geometry/curves/FreemanChain.h"
48#include "DGtal/geometry/curves/GridCurve.h"
49
50#include "DGtal/topology/helpers/Surfaces.h"
51#include "DGtal/io/boards/Board2D.h"
52
54
55using namespace std;
56using namespace DGtal;
57using namespace Z2i;
58
61template <typename CI>
62void displayAll( const CI& ciBegin, const CI& ciEnd )
63{
64 if ( isNotEmpty(ciBegin, ciEnd) )
65 { //if the range is not empty
66 CI i( ciBegin);
67 do
68 {
69 trace.info() << *i;
70 i++;
71 } while (i != ciEnd);
72 trace.info() << endl;
73 }
74}
76
78int main( int argc, char** argv )
79{
80 trace.beginBlock ( "Example for 2d gridcurves" );
81 trace.info() << "Args:";
82 for ( int i = 0; i < argc; ++i )
83 trace.info() << " " << argv[ i ];
84 trace.info() << endl;
85
86 string square = examplesPath + "samples/smallSquare.dat";
87 string S = examplesPath + "samples/contourS.fc";
88
89 // domain
90 Point lowerBound( -50, -50 );
91 Point upperBound( 50, 50 );
92
94 //default construction
95 Curve c1;
96
97 //from a Khalimsky space
98 K2 ks; ks.init( lowerBound, upperBound, true );
99 Curve c2( ks );
101
102 trace.emphase() << "Input" << endl;
103 trace.info() << "\t from a data file " << endl;
104 {
106 fstream inputStream;
107 inputStream.open (square.c_str(), ios::in);
108 c1.initFromVectorStream(inputStream);
109 inputStream.close();
111 }
112 trace.info() << "\t from a digital set " << endl;
113 {
114
115 // digital set: diamond of radius 30 centered at the origin
116 Point o( 0, 0 );
117 Domain domain( lowerBound, upperBound );
118 DigitalSet set( domain );
119 for ( Domain::ConstIterator it = domain.begin(); it != domain.end(); ++it )
120 {
121 if ( (*it - o ).norm1() <= 30 ) set.insertNew( *it );
122 }
123
125 vector<SCell> contour; //contour
126 SurfelAdjacency<K2::dimension> sAdj( true ); //adjacency
127
128 //tracking and init grid curve
129 SCell s = Surfaces<KSpace>::findABel( ks, set, 1000 );
130 Surfaces<KSpace>::track2DBoundary( contour, ks, sAdj, set, s );
131 c2.initFromSCellsVector( contour );
133 }
134
135 trace.info() << "\t from a FreemanChain (from a file) " << endl;
136 {
137 fstream inputStream;
138 inputStream.open (S.c_str(), ios::in);
139 FreemanChain<int> fc(inputStream);
140 inputStream.close();
141
142 Curve c;
144 c.initFromPointsRange( fc.begin(), fc.end() );
146
147 }
148
149 trace.emphase() << "Output" << endl;
150 trace.info() << "\t standard output " << endl;
151 {
153 trace.info() << c1 << std::endl;
155 }
156 trace.info() << "\t into a data file " << endl;
157 {
159 ofstream outputStream("myGridCurve.dat");
160 if (outputStream.is_open())
161 c2.writeVectorToStream(outputStream);
162 outputStream.close();
164 }
165 trace.info() << "\t into a vector graphics file " << endl;
166 {
168 Board2D aBoard;
169 aBoard.setUnit(Board2D::UCentimeter);
170 aBoard << c2;
171 aBoard.saveEPS( "myGridCurve.eps", Board2D::BoundingBox, 5000 );
173 }
174
175 trace.info() << "\t into a FreemanChain " << endl;
176 {
179 FreemanChain fc;
181 trace.info() << "\t" << fc << endl;
183 }
184
185 trace.emphase() << "Ranges Ouput" << endl;
186 {
187 Board2D aBoard;
188 aBoard.setUnit(Board2D::UCentimeter);
189
190 Point low(-1,-1);
191 Point up(3,3);
192 Domain aDomain( low,up );
193
194 {//1cellsRange
196
197 trace.info() << r << endl;
198
199 aBoard << SetMode(aDomain.className(), "Grid") << aDomain;
200 aBoard << r;
201 aBoard.saveEPS( "My1CellsRange.eps", Board2D::BoundingBox, 5000 );
202 aBoard.clear();
203 }
204 {//IncidentPointsRange
207
208 trace.info() << r << endl;
209
210 aBoard << SetMode(aDomain.className(), "Grid") << aDomain;
211 aBoard << r;
212 aBoard.saveEPS( "MyIncidentPointsRange.eps", Board2D::BoundingBox, 5000 );
214 aBoard.clear();
215 }
216 {//CodesRange
218
219 trace.info() << r << endl;
220 }
221 }
222
223 trace.emphase() << "Ranges Iterators" << endl;
224 {
225 typedef Curve::CodesRange Range;
226 Range r = c1.getCodesRange();
227
229 trace.info() << "\t iterate over the range" << endl;
230 Range::ConstIterator it = r.begin();
231 Range::ConstIterator itEnd = r.end();
232 for ( ; it != itEnd; ++it)
233 {
234 trace.info() << *it;
235 }
236 trace.info() << endl;
237
238 trace.info() << "\t iterate over the range in the reverse way" << endl;
239 Range::ConstReverseIterator rit = r.rbegin();
240 Range::ConstReverseIterator ritEnd = r.rend();
241 for ( ; rit != ritEnd; ++rit)
242 {
243 trace.info() << *rit;
244 }
245 trace.info() << endl;
246
247 trace.info() << "\t iterate over the range in a circular way" << endl;
248 Range::ConstCirculator c = r.c();
249 //set the starting element wherever you want...
250 for (unsigned i = 0; i < 20; ++i) ++c;
251 //... and circulate
252 Range::ConstCirculator cend( c );
253 do
254 {
255 trace.info() << *c;
256 c++;
257 } while (c!=cend);
258 trace.info() << endl;
260
261 trace.info() << "\t Generic function working with any (circular)iterator" << endl;
262 displayAll<Range::ConstIterator>(r.begin(),r.end());
263 displayAll<Range::ConstReverseIterator>(r.rbegin(),r.rend());
264 displayAll<Range::ConstCirculator>(r.c(),r.c());
265
266 }
267
268 trace.endBlock();
269 return 0;
270}
271// //
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
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...
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
ConstIterator end() const
ConstIterator begin() const
static void readFromPointsRange(const TConstIterator &itBegin, const TConstIterator &itEnd, FreemanChain &c)
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
Definition: GridCurve.h:173
SCellsRange getSCellsRange() const
Definition: GridCurve.h:415
bool initFromVectorStream(std::istream &in)
PointsRange getPointsRange() const
Definition: GridCurve.h:426
IncidentPointsRange getIncidentPointsRange() const
Definition: GridCurve.h:486
bool initFromSCellsVector(const std::vector< SCell > &aVectorOfSCells)
void writeVectorToStream(std::ostream &out)
bool initFromPointsRange(const TIterator &itb, const TIterator &ite)
GridCurve::CodesRange getCodesRange() const
Definition: GridCurve.h:496
Iterator for HyperRectDomain.
const ConstIterator & begin() const
std::string className() const
const ConstIterator & end() const
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
std::reverse_iterator< ConstIterator > ConstReverseIterator
static SCell findABel(const KSpace &K, const PointPredicate &pp, unsigned int nbtries=1000)
static void track2DBoundary(std::vector< SCell > &aSCellContour2D, const KSpace &K, const SurfelAdjacency< KSpace::dimension > &surfel_adj, const PointPredicate &pp, const SCell &start_surfel)
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
void setUnit(Unit unit)
Definition: Board.cpp:240
void displayAll(const CI &ciBegin, const CI &ciEnd)
[GridCurveGenericFunctionDisplayAll]
DGtal is the top-level namespace which contains all DGtal functions and types.
bool isNotEmpty(const IC &itb, const IC &ite)
Trace trace
Definition: Common.h:154
STL namespace.
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
int main()
Definition: testBits.cpp:56
Domain domain