DGtal  1.4.beta
geometry/curves/exampleGridCurve2d.cpp

This snippet shows how to construct, scan and display a 2d grid curve.

Analysis of one-dimensional discrete structures
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "ConfigExamples.h"
#include "DGtal/geometry/curves/FreemanChain.h"
#include "DGtal/geometry/curves/GridCurve.h"
#include "DGtal/topology/helpers/Surfaces.h"
#include "DGtal/io/boards/Board2D.h"
using namespace std;
using namespace DGtal;
using namespace Z2i;
template <typename CI>
void displayAll( const CI& ciBegin, const CI& ciEnd )
{
if ( isNotEmpty(ciBegin, ciEnd) )
{ //if the range is not empty
CI i( ciBegin);
do
{
trace.info() << *i;
i++;
} while (i != ciEnd);
trace.info() << endl;
}
}
int main( int argc, char** argv )
{
trace.beginBlock ( "Example for 2d gridcurves" );
trace.info() << "Args:";
for ( int i = 0; i < argc; ++i )
trace.info() << " " << argv[ i ];
trace.info() << endl;
string square = examplesPath + "samples/smallSquare.dat";
string S = examplesPath + "samples/contourS.fc";
// domain
Point lowerBound( -50, -50 );
Point upperBound( 50, 50 );
//default construction
Curve c1;
//from a Khalimsky space
K2 ks; ks.init( lowerBound, upperBound, true );
Curve c2( ks );
trace.emphase() << "Input" << endl;
trace.info() << "\t from a data file " << endl;
{
fstream inputStream;
inputStream.open (square.c_str(), ios::in);
c1.initFromVectorStream(inputStream);
inputStream.close();
}
trace.info() << "\t from a digital set " << endl;
{
// digital set: diamond of radius 30 centered at the origin
Point o( 0, 0 );
Domain domain( lowerBound, upperBound );
for ( Domain::ConstIterator it = domain.begin(); it != domain.end(); ++it )
{
if ( (*it - o ).norm1() <= 30 ) set.insertNew( *it );
}
vector<SCell> contour; //contour
//tracking and init grid curve
SCell s = Surfaces<KSpace>::findABel( ks, set, 1000 );
Surfaces<KSpace>::track2DBoundary( contour, ks, sAdj, set, s );
c2.initFromSCellsVector( contour );
}
trace.info() << "\t from a FreemanChain (from a file) " << endl;
{
fstream inputStream;
inputStream.open (S.c_str(), ios::in);
FreemanChain<int> fc(inputStream);
inputStream.close();
Curve c;
c.initFromPointsRange( fc.begin(), fc.end() );
}
trace.emphase() << "Output" << endl;
trace.info() << "\t standard output " << endl;
{
trace.info() << c1 << std::endl;
}
trace.info() << "\t into a data file " << endl;
{
ofstream outputStream("myGridCurve.dat");
if (outputStream.is_open())
c2.writeVectorToStream(outputStream);
outputStream.close();
}
trace.info() << "\t into a vector graphics file " << endl;
{
Board2D aBoard;
aBoard.setUnit(Board2D::UCentimeter);
aBoard << c2;
aBoard.saveEPS( "myGridCurve.eps", Board2D::BoundingBox, 5000 );
}
trace.info() << "\t into a FreemanChain " << endl;
{
trace.info() << "\t" << fc << endl;
}
trace.emphase() << "Ranges Ouput" << endl;
{
Board2D aBoard;
aBoard.setUnit(Board2D::UCentimeter);
Point low(-1,-1);
Point up(3,3);
{//1cellsRange
Curve::SCellsRange r = c1.getSCellsRange();
trace.info() << r << endl;
aBoard << r;
aBoard.saveEPS( "My1CellsRange.eps", Board2D::BoundingBox, 5000 );
aBoard.clear();
}
{//IncidentPointsRange
Curve::IncidentPointsRange r = c1.getIncidentPointsRange();
trace.info() << r << endl;
aBoard << r;
aBoard.saveEPS( "MyIncidentPointsRange.eps", Board2D::BoundingBox, 5000 );
aBoard.clear();
}
{//CodesRange
Curve::CodesRange r = c1.getCodesRange();
trace.info() << r << endl;
}
}
trace.emphase() << "Ranges Iterators" << endl;
{
typedef Curve::CodesRange Range;
Range r = c1.getCodesRange();
trace.info() << "\t iterate over the range" << endl;
Range::ConstIterator it = r.begin();
Range::ConstIterator itEnd = r.end();
for ( ; it != itEnd; ++it)
{
trace.info() << *it;
}
trace.info() << endl;
trace.info() << "\t iterate over the range in the reverse way" << endl;
Range::ConstReverseIterator rit = r.rbegin();
Range::ConstReverseIterator ritEnd = r.rend();
for ( ; rit != ritEnd; ++rit)
{
trace.info() << *rit;
}
trace.info() << endl;
trace.info() << "\t iterate over the range in a circular way" << endl;
//set the starting element wherever you want...
for (unsigned i = 0; i < 20; ++i) ++c;
//... and circulate
do
{
trace.info() << *c;
c++;
} while (c!=cend);
trace.info() << endl;
trace.info() << "\t Generic function working with any (circular)iterator" << endl;
displayAll<Range::ConstIterator>(r.begin(),r.end());
displayAll<Range::ConstReverseIterator>(r.rbegin(),r.rend());
displayAll<Range::ConstCirculator>(r.c(),r.c());
}
return 0;
}
// //
LibBoard::Board::setUnit
void setUnit(Unit unit)
Definition: Board.cpp:240
DGtal::Surfaces
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
DGtal::Circulator
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:85
DGtal::HyperRectDomain< Space >
DGtal::Trace::endBlock
double endBlock()
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
DGtal::KhalimskySpaceND::init
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::FreemanChain< int >
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
displayAll
void displayAll(const CI &ciBegin, const CI &ciEnd)
[GridCurveGenericFunctionDisplayAll]
Definition: exampleGridCurve2d.cpp:62
LibBoard::Board::clear
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
DGtal::SignedKhalimskyCell
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
Definition: KhalimskySpaceND.h:208
DGtal::Trace::info
std::ostream & info()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::functions::square
T square(T x)
Definition: BasicMathFunctions.h:133
DGtal::SimpleRandomAccessRangeFromPoint
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
Definition: SimpleRandomAccessRangeFromPoint.h:73
DGtal::HyperRectDomain::end
const ConstIterator & end() const
Definition: HyperRectDomain.h:201
DGtal::Z2i::Curve
GridCurve< K2 > Curve
Definition: StdDefs.h:116
DGtal::SimpleRandomAccessRangeFromPoint::ConstReverseIterator
std::reverse_iterator< ConstIterator > ConstReverseIterator
Definition: SimpleRandomAccessRangeFromPoint.h:90
Range
Image::Range Range
Definition: testSimpleRandomAccessRangeFromPoint.cpp:46
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
DGtal::Board2D
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:70
DGtal::HyperRectDomain_Iterator
Iterator for HyperRectDomain.
Definition: HyperRectDomain_Iterator.h:142
LibBoard::Board::saveEPS
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
DGtal::detail::isNotEmpty
bool isNotEmpty(const IC &itb, const IC &ite, IteratorType)
domain
Domain domain
Definition: testProjection.cpp:88
DGtal::SetMode
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
DGtal::Z2i::K2
KSpace K2
Definition: StdDefs.h:78
DGtal::HyperRectDomain::begin
const ConstIterator & begin() const
Definition: HyperRectDomain.h:176
Point
MyPointD Point
Definition: testClone2.cpp:383
DGtal::SimpleRandomAccessRangeFromPoint::ConstIterator
TConstIterator ConstIterator
Definition: SimpleRandomAccessRangeFromPoint.h:87
DGtal::DigitalSetByAssociativeContainer
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Definition: DigitalSetByAssociativeContainer.h:89