DGtal  1.2.0
geometry/curves/exampleArithmeticalDSS.cpp

This example shows the basic usage of a 8-connected and a 4-connected arithmetical DSS.

See also
Digital straight lines and segments
#include <iostream>
#include <exception>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/geometry/curves/ArithmeticalDSS.h"
using namespace std;
using namespace DGtal;
using namespace Z2i;
{
trace.beginBlock ( "Naive DSS" );
using namespace Z2i;
// Construct a naive DSS
NaiveDSS8<Integer> segment( 5, 8, //slope
Point(0,0), Point(8,5), //ending points
Point(0,0), Point(8,5), //upper points
Point(3,1), Point(3,1) //lower points
);
// Trace to the standard output
trace.info() << segment << std::endl;
// Trace the position and remainder of each point
it = segment.begin(),
ite = segment.end();
it != ite; ++it )
{
trace.info() << "("
<< segment.position( *it ) << ","
<< segment.remainder( *it )
<< ") ";
}
trace.info() << std::endl;
Board2D board;
// Draw the grid
Domain domain( Point(0,0), Point(8,5) );
board << SetMode(domain.className(), "Grid")
<< domain;
//Draw the points of the DSS
board << SetMode("PointVector", "Both");
board << SetMode(segment.className(), "Points")
<< segment;
// Draw the bounding box
board << SetMode(segment.className(), "BoundingBox")
<< segment;
// Save
board.saveSVG("NaiveDSS8.svg");
#ifdef WITH_CAIRO
board.saveCairo("NaiveDSS8.png", Board2D::CairoPNG);
#endif
}
{
trace.beginBlock ( "Standard DSS" );
using namespace Z2i;
// Construct a standard DSS
StandardDSS4<Integer> segment( 5, 8, //slope
Point(0,0), Point(8,5), //ending points
Point(0,0), Point(8,5), //upper points
Point(4,1), Point(4,1) //lower points
);
// Trace to the standard output
trace.info() << segment << std::endl;
// Display the DSS with a domain on a board
Domain domain( Point(0,0), Point(8,5) );
Board2D board;
// Draw the grid
board << SetMode(domain.className(), "Grid")
<< domain;
// Draw the points of the DSS
board << SetMode("PointVector", "Grid")
<< SetMode(segment.className(), "Points")
<< segment;
// Draw the bounding box
board << SetMode(segment.className(), "BoundingBox")
<< segment;
// Save
board.saveSVG("StandardDSS4.svg");
#ifdef WITH_CAIRO
board.saveCairo("StandardDSS4.png", Board2D::CairoPNG);
#endif
board.clear();
// Draw the pixels
board << SetMode(domain.className(), "Paving")
<< domain;
//Draw the points of the DSS
board << SetMode("PointVector", "Both");
board << SetMode(segment.className(), "Points")
<< segment;
// Draw the bounding box
board << SetMode(segment.className(), "BoundingBox")
<< segment;
board.saveSVG("StandardDSS4bis.svg");
#ifdef WITH_CAIRO
board.saveCairo("StandardDSS4bis.png", Board2D::CairoPNG);
#endif
}
{
trace.beginBlock ( "DSSs constructions" );
using namespace Z2i;
{
// Construct a naive DSS from two upper leaning points
NaiveDSS8<Integer> segment( Point(0,0), Point(8,5), true );
//or simply NaiveDSS8<Integer> segment( Point(0,0), Point(8,5) );
trace.info() << segment << std::endl;
}
{
// Construct a naive DSS from two lower leaning points
NaiveDSS8<Integer> segment( Point(0,0), Point(8,5), false );
trace.info() << segment << std::endl;
}
{
// Construct a naive DSS as a DSL subsegment
NaiveDSS8<Integer> segment( NaiveDSL<Integer>(5,8,0), Point(0,0), Point(8,5) );
trace.info() << segment << std::endl;
}
{
NaiveDSS8<Integer> bigDSS( NaiveDSL<Integer>(5,8,0), Point(-8,-5), Point(16,10) );
// Construct a naive DSS as a subsegment of a greater DSS
NaiveDSS8<Integer> segment( bigDSS, Point(0,0), Point(8,5) );
trace.info() << segment << std::endl;
}
std::vector<Point> r; //container for DSS points
{
// Custom a naive DSS
NaiveDSS8<Integer> segment( 5, 8, //slope
Point(0,0), Point(8,5), //ending points
Point(0,0), Point(8,5), //upper points
Point(3,1), Point(3,1) //lower points
);
//You should be sure that your object is valid before using it
if (!segment.isValid()) throw std::exception();
trace.info() << segment << std::endl;
//copy the DSS points into the container r
std::copy( segment.begin(), segment.end(), std::back_inserter(r) );
}
{
// Construct a DSS from a range of points
NaiveDSS8<Integer> segment( r.begin(), r.end() );
trace.info() << segment << std::endl;
}
}
{
trace.beginBlock ( "DSS update" );
using namespace Z2i;
//Construction --------------------------------------------------
Point M(11, 7);
NaiveDSS8<Integer> S( 5, 8, //slope
Point(0,0), Point(10,6), //ending points
Point(0,0), Point(8,5), //upper points
Point(3,1), Point(3,1) //lower points
);
//this segment should be valid:
if (!S.isValid()) throw std::exception();
// Store a copy before any operation
NaiveDSS8<Integer> copyOfS = S;
trace.info() << S << std::endl;
//Display ------------------------------------------------------
{
Board2D board;
// Draw the grid
Domain domain( Point(0,0), M );
board << SetMode(domain.className(), "Grid")
<< domain;
// Draw the points of the DSS and its bounding box
board << SetMode("PointVector", "Both");
board << SetMode(S.className(), "Points")
<< S
<< SetMode(S.className(), "BoundingBox")
<< S;
// Draw the orthonormal base
board.drawArrow(0.0, 0.0, 1.0, 0.0);
board.drawArrow(0.0, 0.0, 0.0, 1.0);
// Draw M
board << SetMode(M.className(), "Both")
<< CustomStyle( M.className(), new CustomColors( Color(255,0,0), Color(192, 0, 0)) )
<< M;
// Save
board.saveSVG("NaiveDSS8ExtInit.svg");
#ifdef WITH_CAIRO
board.saveCairo("NaiveDSS8ExtInit.png", Board2D::CairoPNG);
#endif
}
// Extension -----------------------------------------------------
bool resExtention = S.extendFront( M );
//this segment should be extended:
if (!resExtention) throw std::exception();
trace.info() << S << std::endl;
//Display ------------------------------------------------------
{
Board2D board;
// Draw the grid
Domain domain( Point(0,0), M );
board << SetMode(domain.className(), "Grid")
<< domain;
// Draw the points of the DSS and its bounding box
board << SetMode("PointVector", "Both");
board << SetMode(S.className(), "Points")
<< S
<< SetMode(S.className(), "BoundingBox")
<< S;
// Draw the orthonormal base
board.drawArrow(0.0, 0.0, 1.0, 0.0);
board.drawArrow(0.0, 0.0, 0.0, 1.0);
// Save
board.saveSVG("NaiveDSS8ExtDone.svg");
#ifdef WITH_CAIRO
board.saveCairo("NaiveDSS8ExtDone.png", Board2D::CairoPNG);
#endif
}
// Retraction ----------------------------------------------------
bool resRetraction = S.retractFront();
//this segment should be retracted:
if (!resRetraction) throw std::exception();
trace.info() << S << std::endl;
// Comparaison ----------------------------------------------------
//this segment and the previous copy should be equal:
if ( !S.equalsTo(copyOfS) ) throw std::exception();
}
int main( int argc, char** argv )
{
trace.beginBlock ( "Example exampleArithmeticalDSS" );
trace.info() << "Args:";
for ( int i = 0; i < argc; ++i )
trace.info() << " " << argv[ i ];
trace.info() << endl;
return 0;
}
// //
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
void exampleUpdate()
Function showing how a DSS can be extended and retracted.
void exampleNaiveDSS()
Function that illustrates the basic usage of a naive DSS.
void exampleStandardDSS()
Function that illustrates the basic usage of a standard DSS.
void exampleConstructors()
Function showing the different ways of constructing DSSs.
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
int main(int argc, char **argv)
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
HyperRectDomain< Space > Domain