DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Types | Public Member Functions | Private Attributes
DGtal::Pattern< TFraction > Class Template Reference

#include <Pattern.h>

Inheritance diagram for DGtal::Pattern< TFraction >:
Inheritance graph
[legend]
Collaboration diagram for DGtal::Pattern< TFraction >:
Collaboration graph
[legend]

Public Types

typedef TFraction Fraction
typedef Pattern< TFraction > Self
typedef Fraction::Integer Integer
typedef Fraction::Quotient Quotient
typedef IntegerComputer< IntegerIC
typedef IC::Point2I Point2I
typedef IC::Vector2I Vector2I

Public Member Functions

 BOOST_CONCEPT_ASSERT ((CPositiveIrreducibleFraction< Fraction >))
 ~Pattern ()
 Pattern (Fraction f=Fraction(0))
 Pattern (Integer p, Integer q)
 Pattern (const Pattern &other)
Patternoperator= (const Pattern &other)
std::string rE () const
std::string rEs (const std::string &seps="(|)") const
Fraction slope () const
Integer length () const
Integer posU (Quotient k) const
Integer posL (Quotient k) const
Point2I U (Quotient k) const
Point2I L (Quotient k) const
Vector2I bezout () const
Vector2I v () const
Pattern previousPattern () const
bool getSmallestCoveringSubpattern (Pattern &subpattern, Quotient &nb, Vector2I &startPos, Integer posA, Integer posB, bool reversed=false) const
bool getGreatestIncludedSubpattern (Pattern &subpattern, Quotient &nb, Vector2I &startPos, Integer posA, Integer posB, bool reversed=false) const
void selfDisplay (std::ostream &out) const
bool isValid () const

Private Attributes

Fraction mySlope

Detailed Description

template<typename TFraction>
class DGtal::Pattern< TFraction >

Aim: This class represents a pattern, i.e. the path between two consecutive upper leaning points on a digital straight line.

Description of template class 'Pattern'

A pattern is characterized by an irreducible fraction. The choice here is to use SternBrocot::Fraction so as to compute efficiently subpatterns.

Parameters:
TFractionthe type chosen to represent fractions, a model of CPositiveIrreducibleFraction. You may use SternBrocot::Fraction or LighterSternBrocot::Fraction for instance.
Note:
LighterSternBrocot::Fraction takes much less memory than SternBrocot::Fraction and is more efficient for large integers. It is 10% slower than SternBrocot::Fraction for small integers (<1000).
See also:
dgtal_digstraighness_sec2

Definition at line 78 of file Pattern.h.


Member Typedef Documentation

template<typename TFraction>
typedef TFraction DGtal::Pattern< TFraction >::Fraction

Definition at line 81 of file Pattern.h.

template<typename TFraction>
typedef IntegerComputer<Integer> DGtal::Pattern< TFraction >::IC

Definition at line 93 of file Pattern.h.

template<typename TFraction>
typedef Fraction::Integer DGtal::Pattern< TFraction >::Integer

Definition at line 85 of file Pattern.h.

template<typename TFraction>
typedef IC::Point2I DGtal::Pattern< TFraction >::Point2I

Definition at line 94 of file Pattern.h.

template<typename TFraction>
typedef Fraction::Quotient DGtal::Pattern< TFraction >::Quotient

Definition at line 86 of file Pattern.h.

template<typename TFraction>
typedef Pattern<TFraction> DGtal::Pattern< TFraction >::Self

Definition at line 84 of file Pattern.h.

template<typename TFraction>
typedef IC::Vector2I DGtal::Pattern< TFraction >::Vector2I

Definition at line 95 of file Pattern.h.


Constructor & Destructor Documentation

template<typename TFraction >
DGtal::Pattern< TFraction >::~Pattern ( )
inline

Destructor.

Definition at line 44 of file Pattern.ih.

{
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Pattern ( Fraction  f = Fraction( 0 ))
inline

Constructor from fraction.

Parameters:
fany fraction (default is null pattern).

Definition at line 50 of file Pattern.ih.

: mySlope( f )
{
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Pattern ( Integer  p,
Integer  q 
)
inline

Constructor from numerator / denominator.

Parameters:
pthe numerator.
qthe denominator.

Definition at line 73 of file Pattern.ih.

References DGtal::IntegerComputer< TInteger >::gcd().

{
IntegerComputer<Integer> ic;
Integer g = ic.gcd( p, q );
p /= g;
q /= g;
mySlope = Fraction( p, q );
}
template<typename TFraction>
DGtal::Pattern< TFraction >::Pattern ( const Pattern< TFraction > &  other)

Copy constructor.

Parameters:
otherthe object to clone.

Member Function Documentation

template<typename TFraction >
DGtal::Pattern< TFraction >::Vector2I DGtal::Pattern< TFraction >::bezout ( ) const
inline
Returns:
the Bezout vector for the pattern, such that bezout() is oriented in the first quadrant, slightly to the left of the slope, and shorter.

Definition at line 244 of file Pattern.ih.

References DGtal::Pattern< TFraction >::U().

Referenced by DGtal::StandardDSLQ0< TFraction >::smartDSS().

{
return slope().odd()
? U( 1 ) - previousPattern().U( 1 )
: previousPattern().U( 1 );
}
template<typename TFraction>
DGtal::Pattern< TFraction >::BOOST_CONCEPT_ASSERT ( (CPositiveIrreducibleFraction< Fraction >)  )
template<typename TFraction>
bool DGtal::Pattern< TFraction >::getGreatestIncludedSubpattern ( Pattern< TFraction > &  subpattern,
Quotient nb,
Vector2I startPos,
Integer  posA,
Integer  posB,
bool  reversed = false 
) const
inline

Computes the greatest subpattern that is included in the segment [A,B], a subpart of the pattern. Points A and B are defined by their position with respect to the beginning of this pattern. The subpattern has a starting position startPos and may be repeated nb times.

Parameters:
subpattern(returns) the subpattern (a pattern that is some ascendant of 'this' in the Stern-Brocot tree).
nb(returns) the number of times subpattern is repeated so as to be included in [A,B]
startPos(returns) the starting position of the subpattern in this pattern.
posAthe position of A (number of steps till A).
posBthe position of B (number of steps till B), > posA.
Returns:
'true' iff the subpattern is not null.

Definition at line 369 of file Pattern.ih.

References DGtal::NumberTraits< T >::castToInt64_t(), DGtal::Pattern< TFraction >::length(), and DGtal::Pattern< TFraction >::v().

Referenced by DGtal::StandardDSLQ0< TFraction >::DSSWithinTwoPatterns().

{
bool null_pattern = false;
Integer l = length();
ASSERT( ( 0 <= posA ) && ( posA < posB ) && ( posB <= l ) );
if ( slope().p() == 0 || slope().q() == 0 ) // trivial pattern
{
ASSERT( posA == 0 && posB == 1 );
subpattern = *this;
nb = 1;
null_pattern = false;
}
else if ( reversed ? slope().even() : slope().odd() )
{ // Odd pattern: E(z_n) = E( z_{n-1} )^u E( z_{n-2} )
Self prevP = previousPattern();
Integer prevL = prevP.length();
Integer k1 = ( posA + prevL - 1 ) / prevL;
if ( posB == l )
{ // B at right extremity of the E( z_{n-2} ) part
if ( posA > slope().u() * prevL )
{
subpattern = Fraction();
nb = 0;
null_pattern = true;
}
else
{ // subpattern is E( z_{n-1} )^nb E( z_{n-2} )
nb = (int32_t)
( NumberTraits<Quotient>::castToInt64_t( slope().u() )
- NumberTraits<Integer>::castToInt64_t( k1 ) ); // number of E( z_{n-1} ) from A.
subpattern = Self( slope().father( nb ) );
nb = 1;
startPos = prevP.v() * k1;
null_pattern = false;
}
}
else
{ // B within some pattern E( z_{n-1} ) or strictly in E ( z_{n-2} )
Integer k2 = posB / prevL;
// subpattern is E( z_{n-1} )^nb or null
if ( nb < 0 ) nb = 0;
subpattern = nb == 0 ? Pattern() : prevP;
startPos = prevP.v() * k1;
null_pattern = nb == 0;
}
}
else // slope() is even
{ // Even pattern: E(z_n) = E( z_{n-2} ) E( z_{n-1} )^u
Self prevP = previousPattern();
Integer prevL = prevP.length();
Integer k1 = ( l - posB + prevL - 1 ) / prevL;
if ( posA == 0 )
{ // A at left extremity of the E( z_{n-2} ) part
// subpattern is E( z_{n-2} ) E( z_{n-1} )^nb
if ( ( l - posB ) > slope().u() * prevL )
{
subpattern = Fraction();
nb = 0;
null_pattern = true;
}
else
{
nb = (int32_t)
( NumberTraits<Quotient>::castToInt64_t( slope().u() )
- NumberTraits<Integer>::castToInt64_t( k1 ) ); // number of E( z_{n-1} ) from B.
subpattern = Self( slope().father( nb ) );
nb = 1;
null_pattern = false;
}
}
else
{ // A within some pattern E( z_{n-1} ) or strictly in E ( z_{n-2} )
Integer k2 = ( l - posA ) / prevL;
// subpattern is E( z_{n-1} )^nb or null
if ( nb < 0 ) nb = 0;
subpattern = nb == 0 ? Pattern() : prevP;
startPos = v() - prevP.v() * k2;
null_pattern = nb == 0;
}
}
return ! null_pattern;
}
template<typename TFraction>
bool DGtal::Pattern< TFraction >::getSmallestCoveringSubpattern ( Pattern< TFraction > &  subpattern,
Quotient nb,
Vector2I startPos,
Integer  posA,
Integer  posB,
bool  reversed = false 
) const
inline

Computes the smallest subpattern that contains the segment [A,B] included in the pattern. Points A and B are defined by their position with respect to the beginning of this pattern. The subpattern has a starting position startPos and may be repeated nb times.

Parameters:
subpattern(returns) the subpattern (a pattern that is some ascendant of 'this' in the Stern-Brocot tree).
nb(returns) the number of times subpattern is repeated so as to cover [A,B]
startPos(returns) the starting position of the subpattern in this pattern.
posAthe position of A (number of steps till A).
posBthe position of B (number of steps till B), > posA.
reversedwhen 'false' assume a usual pattern, otherwise assume a reversed pattern (i.e. a path between two lower leaning points). In this case, all positions are relative to the first lower leaning point L(0). Everything returned correspond to reversed pattern(s).
Returns:
'true' iff the subpattern is different from 'this'.

Definition at line 282 of file Pattern.ih.

References DGtal::NumberTraits< T >::castToInt64_t(), DGtal::Pattern< TFraction >::length(), and DGtal::Pattern< TFraction >::v().

Referenced by DGtal::StandardDSLQ0< TFraction >::DSSWithinTwoPatterns(), and DGtal::StandardDSLQ0< TFraction >::reversedSmartDSS().

{
bool different = false;
Integer l = length();
ASSERT( ( 0 <= posA ) && ( posA < posB ) && ( posB <= l ) );
if ( slope().p() == 0 || slope().q() == 0 ) // trivial pattern
{
ASSERT( posA == 0 && posB == 1 );
subpattern = *this;
nb = 1;
different = false;
}
else if ( reversed ? slope().even() : slope().odd() )
{ // Odd pattern: E(z_n) = E( z_{n-1} )^u E( z_{n-2} )
Self prevP = previousPattern();
Integer prevL = prevP.length();
Integer k1 = posA / prevL;
// Integer r1 = posA % prevL;
if ( posB > slope().u() * prevL )
{ // B at extremity in the E( z_{n-2} ) part
nb = (int32_t)
( NumberTraits<Quotient>::castToInt64_t( slope().u() )
- NumberTraits<Integer>::castToInt64_t( k1 ) ); // number of E( z_{n-1} ) from A.
// subpattern is E( z_{n-1} )^nb E( z_{n-2} )
subpattern = Self( slope().father( nb ) );
nb = 1;
startPos = prevP.v() * k1;
different = k1 != 0;
}
else
{ // B within some pattern E( z_{n-1} )
Integer k2 = ( posB + prevL - 1 ) / prevL;
ASSERT( nb > 0 );
// subpattern is E( z_{n-1} )^nb
subpattern = prevP;
startPos = prevP.v() * k1;
different = true;
}
}
else // slope() is even
{ // Even pattern: E(z_n) = E( z_{n-2} ) E( z_{n-1} )^u
Self prevP = previousPattern();
Integer prevL = prevP.length();
Integer k1 = ( l - posB ) / prevL;
// Integer r1 = ( l - posB ) % prevL;
if ( ( l - posA ) > slope().u() * prevL )
{ // A at extremity in the E( z_{n-2} ) part
nb = (int32_t)
( NumberTraits<Quotient>::castToInt64_t( slope().u() )
- NumberTraits<Integer>::castToInt64_t( k1 ) ); // number of E( z_{n-1} ) from B.
// subpattern is E( z_{n-2} ) E( z_{n-1} )^nb
// slope().selfDisplay( std::cerr );
// std::cerr << " nb=" << nb << " ";
// slope().father( nb ).selfDisplay( std::cerr );
// std::cerr << std::endl;
subpattern = Self( slope().father( nb ) );
nb = 1;
different = k1 != 0;
}
else
{ // A within some pattern E( z_{n-1} )
Integer k2 = ( l - posA + prevL - 1 ) / prevL;
ASSERT( nb > 0 );
// subpattern is E( z_{n-1} )^nb
subpattern = prevP;
startPos = v() - prevP.v() * k2;
different = true;
}
}
return different;
}
template<typename TFraction >
bool DGtal::Pattern< TFraction >::isValid ( ) const
inline

Checks the validity/consistency of the object.

Returns:
'true' if the object is valid, 'false' otherwise.

Definition at line 488 of file Pattern.ih.

{
return true;
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Point2I DGtal::Pattern< TFraction >::L ( Quotient  k) const
inline
Returns:
the coordinates of the k-th lower leaning point,
Parameters:
kits index ( L( 0 ) is between U( 0 ) and U( 1 ) ).

Definition at line 228 of file Pattern.ih.

Referenced by DGtal::StandardDSLQ0< TFraction >::DSSWithinTwoPatterns().

{
ASSERT( ! slope().null() );
pL += bezout();
if ( k == NumberTraits<Quotient>::ZERO )
return pL;
else
return pL + U( k );
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Integer DGtal::Pattern< TFraction >::length ( ) const
inline
Returns:
the digital length of the pattern, i.e. slope.p() + slope.q().

Definition at line 175 of file Pattern.ih.

Referenced by DGtal::StandardDSLQ0< TFraction >::DSSWithinTwoPatterns(), DGtal::Pattern< TFraction >::getGreatestIncludedSubpattern(), and DGtal::Pattern< TFraction >::getSmallestCoveringSubpattern().

{
return mySlope.p() + mySlope.q();
}
template<typename TFraction>
DGtal::Pattern< TFraction > & DGtal::Pattern< TFraction >::operator= ( const Pattern< TFraction > &  other)
inline

Assignment.

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

Definition at line 65 of file Pattern.ih.

References DGtal::Pattern< TFraction >::mySlope.

{
mySlope = other.mySlope;
return *this;
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Integer DGtal::Pattern< TFraction >::posL ( Quotient  k) const
inline
Returns:
the position of the k-th lower leaning point,
Parameters:
kits index ( posU( 0 ) <= posL( 0 ) < posU( 1 ) ).

Definition at line 196 of file Pattern.ih.

{
ASSERT( ! slope().null() );
Integer pL = slope().odd() ? length() - previousPattern().length()
if ( k == NumberTraits<Quotient>::ZERO ) return pL;
else if ( k == NumberTraits<Quotient>::ONE ) return pL + length();
else return pL + length() * ((Integer) k);
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Integer DGtal::Pattern< TFraction >::posU ( Quotient  k) const
inline
Returns:
the position of the k-th upper leaning point,
Parameters:
kits index ( posU( 0 ) == 0 ).

Definition at line 184 of file Pattern.ih.

{
ASSERT( ! slope().null() );
if ( k == NumberTraits<Quotient>::ZERO ) return NumberTraits<Integer>::ZERO;
else if ( k == NumberTraits<Quotient>::ONE ) return length();
else return length() * ((Integer) k);
}
template<typename TFraction >
DGtal::Pattern< TFraction > DGtal::Pattern< TFraction >::previousPattern ( ) const
inline
Returns:
the pattern of slope z_{n-1} if z_n was the slope of 'this'.

Definition at line 264 of file Pattern.ih.

{
ASSERT( ( ! slope().null() ) );
// && ( slope().p() != NumberTraits<Quotient>::ZERO ) );
return Self( slope().previousPartial() );
// if ( slope().k() > NumberTraits<Quotient>::ZERO )
// return Self( slope().previousPartial() );
// else // if ( slope().k() == NumberTraits<Quotient>::ZERO )
// return ( slope().p() == NumberTraits<Quotient>::ZERO )
// ? Self( Fraction( 1, 0 ) )
// : Self( Fraction( 0, 1 ) );
}
template<typename TFraction >
std::string DGtal::Pattern< TFraction >::rE ( ) const
inline

The recursive mapping E, which gives the corresponding Christoffel word in {0,1}.

Definition at line 86 of file Pattern.ih.

References DGtal::Pattern< TFraction >::rE().

Referenced by DGtal::StandardDSLQ0< TFraction >::DSSWithinTwoPatterns(), DGtal::Pattern< TFraction >::rE(), and DGtal::StandardDSLQ0< TFraction >::reversedSmartDSS().

{
if ( mySlope.null() ) return "eps";
else if ( mySlope.k() == -2 )
{
return "0";
}
else if ( mySlope.k() == -NumberTraits<Quotient>::ONE )
{
return "1";
}
else if ( mySlope.k() == NumberTraits<Quotient>::ZERO )
{
std::string s( NumberTraits<Quotient>::castToInt64_t( mySlope.p() ), '1' );
return '0' + s;
}
// else if ( mySlope.k() == NumberTraits<Quotient>::ONE )
// {
// std::string s( NumberTraits<Quotient>::castToInt64_t( mySlope.u() ), '0' );
// return s + '1';
// }
else
{
Fraction f1, f2;
Quotient nb1, nb2;
mySlope.getSplitBerstel( f1, nb1, f2, nb2 );
std::string s;
Self p1( f1 );
Self p2( f2 );
for ( Quotient i = 0; i < nb1; ++i ) s += p1.rE();
for ( Quotient i = 0; i < nb2; ++i ) s += p2.rE();
return s;
}
}
template<typename TFraction >
std::string DGtal::Pattern< TFraction >::rEs ( const std::string &  seps = "(|)") const
inline

The recursive mapping E, which gives the corresponding Christoffel word in {0,1}, but also shows the Berstel splits with "(|)".

Parameters:
sepsthe three separators.

Definition at line 125 of file Pattern.ih.

References DGtal::Pattern< TFraction >::rEs().

Referenced by DGtal::Pattern< TFraction >::rEs().

{
if ( mySlope.null() ) return "eps";
else if ( mySlope.k() == -2 )
{
return "0";
}
else if ( mySlope.k() == -NumberTraits<Quotient>::ONE )
{
return "1";
}
else if ( mySlope.k() == NumberTraits<Quotient>::ZERO )
{
std::string s( NumberTraits<Quotient>::castToInt64_t( mySlope.p() ), '1' );
return '0' + s;
}
// else if ( mySlope.k() == NumberTraits<Quotient>::ONE )
// {
// std::string s( NumberTraits<Quotient>::castToInt64_t( mySlope.u() ), '0' );
// return s + '1';
// }
else
{
Fraction f1, f2;
Quotient nb1, nb2;
mySlope.getSplitBerstel( f1, nb1, f2, nb2 );
std::string s( 1, seps[ 0 ] );
Self p1( f1 );
Self p2( f2 );
for ( Quotient i = 0; i < nb1; ++i ) s += p1.rEs( seps );
s += seps[ 1 ];
for ( Quotient i = 0; i < nb2; ++i ) s += p2.rEs( seps );
s += seps[ 2 ];
return s;
}
}
template<typename TFraction >
void DGtal::Pattern< TFraction >::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 475 of file Pattern.ih.

References DGtal::Pattern< TFraction >::selfDisplay().

Referenced by DGtal::Pattern< TFraction >::selfDisplay().

{
out << "[Pattern] f=";
mySlope.selfDisplay( out );
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Fraction DGtal::Pattern< TFraction >::slope ( ) const
inline
Returns:
the slope of this pattern, an irreducible fraction

Definition at line 166 of file Pattern.ih.

Referenced by DGtal::StandardDSLQ0< TFraction >::DSSWithinTwoPatterns(), DGtal::StandardDSLQ0< TFraction >::reversedSmartDSS(), and DGtal::StandardDSLQ0< TFraction >::smartDSS().

{
return mySlope;
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Point2I DGtal::Pattern< TFraction >::U ( Quotient  k) const
inline
Returns:
the coordinates of the k-th upper leaning point,
Parameters:
kits index ( U( 0 ) == (0,0) ).

Definition at line 210 of file Pattern.ih.

Referenced by DGtal::Pattern< TFraction >::bezout().

{
ASSERT( ! slope().null() );
if ( k == NumberTraits<Quotient>::ZERO )
else if ( k == NumberTraits<Quotient>::ONE )
return Point2I( slope().q(),
slope().p() );
else
return Point2I( slope().q() * ((Integer) k ),
slope().p() * ((Integer) k) );
}
template<typename TFraction >
DGtal::Pattern< TFraction >::Vector2I DGtal::Pattern< TFraction >::v ( ) const
inline

Field Documentation

template<typename TFraction>
Fraction DGtal::Pattern< TFraction >::mySlope
private

The fraction that characterizes the slope of the pattern.

Definition at line 259 of file Pattern.h.

Referenced by DGtal::Pattern< TFraction >::operator=().


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