DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Data Structures | Public Types | Public Member Functions | Protected Attributes
DGtal::FrechetShortcut< TIterator, TInteger > Class Template Reference

#include <FrechetShortcut.h>

Inheritance diagram for DGtal::FrechetShortcut< TIterator, TInteger >:
Inheritance graph
[legend]
Collaboration diagram for DGtal::FrechetShortcut< TIterator, TInteger >:
Collaboration graph
[legend]

Data Structures

class  Backpath
class  Cone
struct  Tools

Public Types

typedef TInteger Integer
typedef TIterator ConstIterator
typedef FrechetShortcut
< ConstIterator, Integer
Self
typedef FrechetShortcut
< std::reverse_iterator
< ConstIterator >, Integer
Reverse
typedef
IteratorCirculatorTraits
< ConstIterator >::Value 
Point
typedef
IteratorCirculatorTraits
< ConstIterator >::Value 
Vector
typedef Vector::Coordinate Coordinate

Public Member Functions

 BOOST_CONCEPT_ASSERT ((CInteger< TInteger >))
 FrechetShortcut ()
 FrechetShortcut (double error)
void init (const ConstIterator &it)
FrechetShortcut getSelf ()
 FrechetShortcut (const Self &other)
FrechetShortcutoperator= (const Self &other)
Reverse getReverse () const
bool operator== (const Self &other) const
bool operator!= (const Self &other) const
 ~FrechetShortcut ()
bool isExtendableForward ()
bool extendForward ()
ConstIterator begin () const
ConstIterator end () const
std::string className () const
bool isValid () const
bool updateBackpath ()
bool testUpdateBackpath ()
bool isBackpathOk ()
void resetBackpath ()
void resetCone ()
bool testUpdateWidth ()
Cone computeNewCone ()
bool updateWidth ()
void selfDisplay (std::ostream &out) const

Protected Attributes

double myError
std::vector< BackpathmyBackpath
Cone myCone
ConstIterator myBegin
ConstIterator myEnd

Detailed Description

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
class DGtal::FrechetShortcut< TIterator, TInteger >

Aim: On-line computation Computation of the longest shortcut according to the Fréchet distance for a given error. See related article: Sivignon, I., (2011). A Near-Linear Time Guaranteed Algorithm for Digital Curve Simplification under the Fréchet Distance. DGCI 2011. Retrieved from http://link.springer.com/chapter/10.1007/978-3-642-19867-0_28.

Description of class 'FrechetShortcut' <p>

The algorithm we propose uses an approximation of the Fréchet distance, but a guarantee over the quality of the simplification is proved. Moreover, even if the theoretical complexity of the algorithm is in \( O(n log(n)) \), experiments show a linear behaviour in practice.

We denote by \( error(i,j) \) the Fréchet distance between the segment \([p_ip_j]\) and the part of the curve between the points \(p_i\) and \(p_j\). The approximation of the Fréchet distance is based on the fact that \( error(i,j) \) can be upper and lower bounded by a function of two values, namely \( w(i,j)$ \) and \( b(i,j) \), where \( w(i,j)$ \) is the width of the points between \(p_i\) and \(p_j\) in the direction \( p_ip_j \) and \( b(i,j) \) is the length of the longest backpath in this direction.

Then, the algorithm consists in greedily reading the points of the curves from a first point given by myBegin, while updating the values \( w(i,j) \) and \( b(i,j) \) on the fly.

This class is a model of the concept CForwardSegmentComputer

It should be used with the Curve object (defined in StdDefs.h) and

*its PointsRange as follows:

Curve::PointsRange r = c.getPointsRange();
typedef FrechetShortcut<Curve::PointsRange::ConstIterator,int> Shortcut;
// Computation of one shortcut
Shortcut s(error);
s.init( r.begin() );
while ( ( s.end() != r.end() )
&&( s.extendForward() ) ) {}
// Computation of a greedy segmentation
typedef GreedySegmentation<Shortcut> Segmentation;
Segmentation theSegmentation( r.begin(), r.end(), Shortcut(error) );
// the segmentation is computed here
Segmentation::SegmentComputerIterator it = theSegmentation.begin();
Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
for ( ; it != itEnd; ++it) {
s=Shortcut(*it);
trace.info() << s << std::endl;
board << s;
}
board.saveEPS("FrechetShortcutExample.eps", Board2D::BoundingBox, 5000 );
Template Parameters:
TIteratorIterator type on 2D digital points, TInteger type *of integer
See also:
exampleFrechetShortcut.cpp testFrechetShortcut.cpp

Definition at line 107 of file FrechetShortcut.h.


Member Typedef Documentation

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
typedef TIterator DGtal::FrechetShortcut< TIterator, TInteger >::ConstIterator

Definition at line 120 of file FrechetShortcut.h.

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
typedef Vector::Coordinate DGtal::FrechetShortcut< TIterator, TInteger >::Coordinate

Definition at line 127 of file FrechetShortcut.h.

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
typedef TInteger DGtal::FrechetShortcut< TIterator, TInteger >::Integer

Definition at line 115 of file FrechetShortcut.h.

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
typedef IteratorCirculatorTraits<ConstIterator>::Value DGtal::FrechetShortcut< TIterator, TInteger >::Point

Definition at line 125 of file FrechetShortcut.h.

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
typedef FrechetShortcut<std::reverse_iterator<ConstIterator>,Integer> DGtal::FrechetShortcut< TIterator, TInteger >::Reverse

Definition at line 122 of file FrechetShortcut.h.

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
typedef FrechetShortcut<ConstIterator,Integer> DGtal::FrechetShortcut< TIterator, TInteger >::Self

Definition at line 121 of file FrechetShortcut.h.

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
typedef IteratorCirculatorTraits<ConstIterator>::Value DGtal::FrechetShortcut< TIterator, TInteger >::Vector

Definition at line 126 of file FrechetShortcut.h.


Constructor & Destructor Documentation

template<typename TIterator , typename TInteger >
DGtal::FrechetShortcut< TIterator, TInteger >::FrechetShortcut ( )
inline

Default constructor. not valid

FrechetShortcut class

Definition at line 534 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::myBackpath, DGtal::FrechetShortcut< TIterator, TInteger >::myCone, and DGtal::FrechetShortcut< TIterator, TInteger >::myError.

Referenced by DGtal::FrechetShortcut< TIterator, TInteger >::getSelf().

{
myError = 0;
myCone = Cone();
for(int i=0;i<8;i++)
{
//backpath b(i,0);
Backpath b(this,i);
myBackpath.push_back(b);
}
}
template<typename TIterator , typename TInteger >
DGtal::FrechetShortcut< TIterator, TInteger >::FrechetShortcut ( double  error)
inline

Constructor with initialisation

Parameters:
itan iterator on 2D points

Definition at line 551 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::myBackpath, DGtal::FrechetShortcut< TIterator, TInteger >::myCone, and DGtal::FrechetShortcut< TIterator, TInteger >::myError.

{
myCone = Cone();
for(int i=0;i<8;i++)
{
//backpath b(i,error);
Backpath b(this,i);
myBackpath.push_back(b);
}
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
DGtal::FrechetShortcut< TIterator, TInteger >::FrechetShortcut ( const Self other)

Copy constructor.

Parameters:
otherthe object to clone.
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
DGtal::FrechetShortcut< TIterator, TInteger >::~FrechetShortcut ( )
inline

Destructor.

Definition at line 743 of file FrechetShortcut.h.

{};

Member Function Documentation

template<typename TIterator , typename TInteger >
TIterator DGtal::FrechetShortcut< TIterator, TInteger >::begin ( ) const
inline
Returns:
begin iterator of the FrechetShortcut range.

Definition at line 866 of file FrechetShortcut.ih.

Referenced by DGtal::Display2DFactory::draw().

{
return myBegin;
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
DGtal::FrechetShortcut< TIterator, TInteger >::BOOST_CONCEPT_ASSERT ( (CInteger< TInteger >)  )
template<typename TIterator , typename TInteger >
std::string DGtal::FrechetShortcut< TIterator, TInteger >::className ( ) const
inline
Returns:
the name of the class.

Definition at line 883 of file FrechetShortcut.ih.

{
return "FrechetShortcut";
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
FrechetShortcut< TIterator, TInteger >::Cone FrechetShortcut::computeNewCone ( )
inline

Computes the cone defined by the point *myBegin and the new point *myEnd + 1 and intersect it with myCone (unchanged)

Returns:
the new cone

Definition at line 665 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::Cone::intersectCones(), M_PI, and M_PI_2.

{
double x0, y0,x1,y1;
Point firstP = Point(*myBegin);
Point newP = Point(*(myEnd+1));
Cone newCone=myCone;
// compute the tangent points defined by the first point and the
// circle C(newP,error)
bool intersect = Tools::circleTangentPoints(firstP[0],firstP[1], newP[0], newP[1], myError/(sqrt(2.0F)), &x0, &y0,
&x1, &y1);
if(intersect)
{
// define a cone according to the new tangent points
Cone c;
// case where there is one single tangent point
if(fabs(x0-x1) < 0.00001 && fabs(y0-y1) < 0.00001)
{
double angle = Tools::computeAngle(firstP[0],firstP[1],newP[0],newP[1]);
assert(angle != -1);
double angle0 = angle - M_PI_2;
if(angle0<0)
angle0 = angle0+2*M_PI;
double angle1 = angle + M_PI_2;
if(angle1>2*M_PI)
angle1 = angle1-2*M_PI;
c = Cone(angle0,angle1);
}
else
c = Cone(firstP[0],firstP[1],x0,y0,x1,y1);
newCone.intersectCones(c);
}
return newCone;
}
template<typename TIterator , typename TInteger >
TIterator DGtal::FrechetShortcut< TIterator, TInteger >::end ( ) const
inline
Returns:
end iterator of the FrechetShortcut range.

Definition at line 873 of file FrechetShortcut.ih.

Referenced by DGtal::Display2DFactory::draw().

{
ConstIterator i(myEnd); ++i;
return i;
}
template<typename TIterator , typename TInteger >
bool DGtal::FrechetShortcut< TIterator, TInteger >::extendForward ( )
inline

Tests whether the FrechetShortcut can be extended at the front. Extend the FrechetShortcut if yes.

Returns:
'true' if yes, 'false' otherwise.

Definition at line 642 of file FrechetShortcut.ih.

{
bool flag = (updateWidth() && updateBackpath());
if(flag)
++myEnd;
return flag;
}
template<typename TIterator , typename TInteger >
DGtal::FrechetShortcut< std::reverse_iterator< TIterator >, TInteger > DGtal::FrechetShortcut< TIterator, TInteger >::getReverse ( ) const
inline
Returns:
a reverse version of '*this'.

Definition at line 615 of file FrechetShortcut.ih.

{
return Reverse(myError);
}
template<typename TIterator , typename TInteger >
DGtal::FrechetShortcut< TIterator, TInteger > DGtal::FrechetShortcut< TIterator, TInteger >::getSelf ( )
inline

Definition at line 580 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::FrechetShortcut(), and DGtal::FrechetShortcut< TIterator, TInteger >::myError.

{
FrechetShortcut<TIterator,TInteger> other = FrechetShortcut(myError);
return other;
}
template<typename TIterator , typename TInteger >
void DGtal::FrechetShortcut< TIterator, TInteger >::init ( const ConstIterator it)
inline
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool FrechetShortcut::isBackpathOk ( )
inline

Test if there exist a backpath of length greater than the threshold

Definition at line 808 of file FrechetShortcut.ih.

{
// compute the quadrant of the direction of P(i,j)
Point firstP = Point(*myBegin);
Point P = Point(*(myEnd+1));
int q = Tools::computeQuadrant(firstP,P);
// to handle non simple curves (a point is visited twice)
if(firstP==P)
return true;
// compute the direction vector pipj
Point v;
v[0] = P[0]-firstP[0];
v[1] = P[1]-firstP[1];
// compute the angle between the direction vector and the elementary
// direction (defined by the quadrant)
double angle = Tools::angleVectVect(v,dir_elem);
boost::icl::interval_set<double> intervals = myBackpath[q].myForbiddenIntervals;
if(contains(intervals,angle))
return false;
return true;
}
template<typename TIterator , typename TInteger >
bool DGtal::FrechetShortcut< TIterator, TInteger >::isExtendableForward ( )
inline

Tests whether the FrechetShortcut can be extended at the front.

Returns:
'true' if yes, 'false' otherwise.

Definition at line 656 of file FrechetShortcut.ih.

{
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool DGtal::FrechetShortcut< TIterator, TInteger >::isValid ( ) const

Checks the validity/consistency of the object.

Returns:
'true' if the object is valid, 'false' otherwise.
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool DGtal::FrechetShortcut< TIterator, TInteger >::operator!= ( const Self other) const
inline

Difference operator.

Parameters:
otherthe object to compare with.
Returns:
'false' if equal 'true' otherwise

Definition at line 633 of file FrechetShortcut.ih.

{
return (!(*this == other));
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
DGtal::FrechetShortcut< TIterator, TInteger > & DGtal::FrechetShortcut< TIterator, TInteger >::operator= ( const Self other)
inline

Assignment.

Parameters:
otherthe object to copy.
Returns:
a reference on 'this'.

Definition at line 597 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::myBackpath, DGtal::FrechetShortcut< TIterator, TInteger >::myBegin, DGtal::FrechetShortcut< TIterator, TInteger >::myCone, DGtal::FrechetShortcut< TIterator, TInteger >::myEnd, and DGtal::FrechetShortcut< TIterator, TInteger >::myError.

{
if(this != &other)
{
myError = other.myError;
myBackpath = other.myBackpath;
myCone = other.myCone;
myBegin = other.myBegin;
myEnd = other.myEnd;
}
return *this;
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool DGtal::FrechetShortcut< TIterator, TInteger >::operator== ( const Self other) const
inline

Equality operator.

Parameters:
otherthe object to compare with.
Returns:
'true' if the objects are equal, false otherwise

Definition at line 623 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::myBegin, DGtal::FrechetShortcut< TIterator, TInteger >::myEnd, and DGtal::FrechetShortcut< TIterator, TInteger >::myError.

{
return ((myBegin == other.myBegin) && (myEnd == other.myEnd) && (myError == other.myError));
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
void FrechetShortcut::resetBackpath ( )
inline

Reset the backpaths before the computation of a new shortcut

Definition at line 845 of file FrechetShortcut.ih.

Referenced by DGtal::FrechetShortcut< TIterator, TInteger >::init().

{
for(unsigned int i=0;i<8;i++)
{
myBackpath[i].reset();
}
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
void FrechetShortcut::resetCone ( )
inline

Reset the cone before the computation of a new shortcut

Definition at line 855 of file FrechetShortcut.ih.

Referenced by DGtal::FrechetShortcut< TIterator, TInteger >::init().

{
myCone.myInf = true;
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
void FrechetShortcut::selfDisplay ( std::ostream &  out) const
inline

Writes/Displays the object on an output stream.

Parameters:
outthe output stream where the object is written.

Definition at line 898 of file FrechetShortcut.ih.

{
out << "[FrechetShortcut]" << endl;
out << "(Begin, End)=";
out << "("<< Point(*myBegin) << ", " << Point(*myEnd) << ")\n";
out << "[End FrechetShortcut]" << endl;
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool FrechetShortcut::testUpdateBackpath ( )
inline

Test if there exist a backpath of length greater than the threshold after the addition of the point pointed by *myEnd+1, but does not modify myBackpath.

Definition at line 735 of file FrechetShortcut.ih.

{
// Save the current value of the backpath
vector <typename FrechetShortcut<TIterator,TInteger>::Backpath> BackpathSave;
for(unsigned int i=0;i<8;i++)
{
Backpath b(myBackpath[i]);
BackpathSave.push_back(b);
}
// Check whether the next point could be added or not with respect to the backpath
bool flag = updateBackpath();
// Copy back the values of backpath before the test.
for(unsigned int i=0;i<8;i++)
myBackpath[i] = Backpath(BackpathSave[i]);
return flag;
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool FrechetShortcut::testUpdateWidth ( )
inline

Test if the new direction belongs to the new cone, but does not modify myCone

Definition at line 711 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::Cone::isEmpty(), M_PI, DGtal::FrechetShortcut< TIterator, TInteger >::Cone::myInf, DGtal::FrechetShortcut< TIterator, TInteger >::Cone::myMax, and DGtal::FrechetShortcut< TIterator, TInteger >::Cone::myMin.

{
Cone c = computeNewCone();
Point firstP = Point(*myBegin);
Point newP = Point(*(myEnd+1));
if(!(c.isEmpty()))
if(c.myInf)
return true;
else
{
double angle = Tools::computeAngle(firstP[0], firstP[1], newP[0], newP[1]);
assert(angle != -1);
return Tools::isBetween(angle,c.myMin,c.myMax,2*M_PI);
}
else
return false;
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool FrechetShortcut::updateBackpath ( )
inline

Updates the backpaths (one per octant) according to the new point

Returns:
'true' if the length of the longest backpath is lower than the threshold, false otherwise

Definition at line 791 of file FrechetShortcut.ih.

{
Point prevP = Point(*myEnd);
Point P = Point(*(myEnd+1));
int d = Tools::computeChainCode(prevP,P);
for(unsigned int j=0;j<8;j++)
myBackpath[j].updateBackPathFirstQuad(Tools::rot(d,j),myEnd+1);
return isBackpathOk();
}
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
bool FrechetShortcut::updateWidth ( )
inline

Updates the width according to the new point

Returns:
true if the width is lower than the error, false otherwise

Definition at line 761 of file FrechetShortcut.ih.

References DGtal::FrechetShortcut< TIterator, TInteger >::Cone::isEmpty(), M_PI, DGtal::FrechetShortcut< TIterator, TInteger >::Cone::myInf, DGtal::FrechetShortcut< TIterator, TInteger >::Cone::myMax, and DGtal::FrechetShortcut< TIterator, TInteger >::Cone::myMin.

{
Cone c = computeNewCone();
myCone = c;
Point firstP = Point(*myBegin);
Point newP = Point(*(myEnd+1));
bool flag = true;
if(!(c.isEmpty()))
if(c.myInf)
flag = true;
else
{
double angle = Tools::computeAngle
(firstP[0], firstP[1], newP[0],
newP[1]);
assert(angle != -1);
flag = Tools::isBetween(angle,c.myMin,c.myMax,2*M_PI);
}
else
flag = false;
return flag;
}

Field Documentation

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
std::vector<Backpath> DGtal::FrechetShortcut< TIterator, TInteger >::myBackpath
protected

Vector of 8 backpaths, one per octant. Stores all the information needed to update the length of the longest backpath.

Definition at line 805 of file FrechetShortcut.h.

Referenced by DGtal::FrechetShortcut< TIterator, TInteger >::FrechetShortcut(), and DGtal::FrechetShortcut< TIterator, TInteger >::operator=().

template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
ConstIterator DGtal::FrechetShortcut< TIterator, TInteger >::myBegin
protected
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
Cone DGtal::FrechetShortcut< TIterator, TInteger >::myCone
protected
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
ConstIterator DGtal::FrechetShortcut< TIterator, TInteger >::myEnd
protected
template<typename TIterator, typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate>
double DGtal::FrechetShortcut< TIterator, TInteger >::myError
protected

The documentation for this class was generated from the following files: