DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Data Structures | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes
DGtal::AngleLinearMinimizer Class Reference

#include <AngleLinearMinimizer.h>

Inheritance diagram for DGtal::AngleLinearMinimizer:
Inheritance graph
[legend]
Collaboration diagram for DGtal::AngleLinearMinimizer:
Collaboration graph
[legend]

Data Structures

struct  ValueInfo

Public Member Functions

virtual ~AngleLinearMinimizer ()
 AngleLinearMinimizer ()
void reset ()
void init (unsigned int nbMax)
ValueInforw (unsigned int i)
const ValueInforo (unsigned int i) const
unsigned int maxSize () const
unsigned int size () const
void setSize (unsigned int nb)
void setIsCurveOpen (bool is_curve_open=false)
double getEnergy (unsigned int i1, unsigned int i2) const
double getFormerEnergy (unsigned int i1, unsigned int i2) const
std::vector< double > getGradient () const
std::vector< double > getFormerGradient () const
double optimize ()
double optimize (unsigned int i1, unsigned int i2)
double sum () const
double max () const
virtual double lastDelta () const
std::string className () const
void selfDisplay (std::ostream &out) const
bool isValid () const

Protected Member Functions

virtual void oneStep (unsigned int i1, unsigned int i2)

Protected Attributes

bool myIsCurveOpen
ValueInfomyValues
unsigned int mySize

Private Member Functions

 AngleLinearMinimizer (const AngleLinearMinimizer &other)
AngleLinearMinimizeroperator= (const AngleLinearMinimizer &other)

Private Attributes

unsigned int myMaxSize
double mySum
double myMax

Detailed Description

Aim: Used to minimize the angle variation between different angles while taking into accounts min and max constraints. Example (.

Description of class 'AngleLinearMinimizer'

See also:
math/testAngleLinearMinimizer.cpp)

Definition at line 68 of file AngleLinearMinimizer.h.


Constructor & Destructor Documentation

DGtal::AngleLinearMinimizer::~AngleLinearMinimizer ( )
virtual

Destructor.

Definition at line 45 of file AngleLinearMinimizer.cpp.

References reset().

{
reset();
}
DGtal::AngleLinearMinimizer::AngleLinearMinimizer ( )

Constructor.

Definition at line 51 of file AngleLinearMinimizer.cpp.

: myValues( 0 )
{
}
DGtal::AngleLinearMinimizer::AngleLinearMinimizer ( const AngleLinearMinimizer other)
private

Copy constructor.

Parameters:
otherthe object to clone. Forbidden by default.

Member Function Documentation

std::string DGtal::AngleLinearMinimizer::className ( ) const
inline

Default drawing style object.

Returns:
the dyn. alloc. default style for this object.
the style name used for drawing this object.

Definition at line 200 of file AngleLinearMinimizer.ih.

{
return "AngleLinearMinimizer";
}
double DGtal::AngleLinearMinimizer::getEnergy ( unsigned int  i1,
unsigned int  i2 
) const
Parameters:
i1the first value to be optimized (between 0 and 'size()-1').
i2the value after the last to be optimized (between 0 and 'size()-1')
Returns:
the current energy of the system.

Definition at line 87 of file AngleLinearMinimizer.cpp.

References DGtal::AngleComputer::deviation(), DGtal::AngleLinearMinimizer::ValueInfo::distToNext, DGtal::ModuloComputer< TInteger >::next(), DGtal::ModuloComputer< TInteger >::previous(), and DGtal::AngleLinearMinimizer::ValueInfo::value.

{
double E = 0.0;
for ( unsigned int i = mc.next( i1 ); i != i2; )
{
unsigned int inext = mc.next( i );
const ValueInfo & vi = this->ro( i );
const ValueInfo & viprev = this->ro( mc.previous( i ) );
double dev = AngleComputer::deviation( vi.value, viprev.value );
E += (dev * dev) / viprev.distToNext;
i = inext;
}
return E;
}
double DGtal::AngleLinearMinimizer::getFormerEnergy ( unsigned int  i1,
unsigned int  i2 
) const
Parameters:
i1the first value to be optimized (between 0 and 'size()-1').
i2the value after the last to be optimized (between 0 and 'size()-1')
Returns:
the former energy of the system.

Definition at line 106 of file AngleLinearMinimizer.cpp.

References DGtal::AngleComputer::deviation(), DGtal::AngleLinearMinimizer::ValueInfo::distToNext, DGtal::ModuloComputer< TInteger >::next(), DGtal::AngleLinearMinimizer::ValueInfo::oldValue, and DGtal::ModuloComputer< TInteger >::previous().

{
double E = 0.0;
for ( unsigned int i = mc.next( i1 ); i != i2; )
{
unsigned int inext = mc.next( i );
const ValueInfo & vi = this->ro( i );
const ValueInfo & viprev = this->ro( mc.previous( i ) );
double dev = AngleComputer::deviation( vi.oldValue, viprev.oldValue );
E += (dev * dev) / viprev.distToNext;
i = inext;
}
return E;
}
std::vector< double > DGtal::AngleLinearMinimizer::getFormerGradient ( ) const
Returns:
the energy gradient computed at the former position.

Definition at line 166 of file AngleLinearMinimizer.cpp.

References DGtal::AngleComputer::deviation(), DGtal::AngleLinearMinimizer::ValueInfo::distToNext, DGtal::ModuloComputer< TInteger >::next(), DGtal::AngleLinearMinimizer::ValueInfo::oldValue, and DGtal::ModuloComputer< TInteger >::previous().

{
vector<double> grad( size() );
for ( unsigned int i = 0; i < size(); i++ )
{
unsigned int iprev = mc.previous( i );
unsigned int inext = mc.next( i );
const ValueInfo & vi = this->ro( i );
double val = vi.oldValue;
if ( myIsCurveOpen && ( i == ( size() - 1 ) ) )
{ // free extremity to the front/right.
const ValueInfo & viprev = this->ro( iprev );
double valp = viprev.oldValue;
grad[ i ] = 2.0 * AngleComputer::deviation( val, valp ) / viprev.distToNext;
}
else if ( myIsCurveOpen && ( i == 0 ) )
{ // free extremity to the back/left.
const ValueInfo & vinext = this->ro( inext );
double valn = vinext.oldValue;
grad[ i ] = -2.0 * AngleComputer::deviation( valn, val ) / vi.distToNext;
}
else
{ // standard case.
const ValueInfo & viprev = this->ro( iprev );
const ValueInfo & vinext = this->ro( inext );
double valp = viprev.oldValue;
double valn = vinext.oldValue;
grad[ i ] = 2.0*( AngleComputer::deviation( val, valp ) / viprev.distToNext
- AngleComputer::deviation( valn, val ) / vi.distToNext ) ;
}
}
return grad;
}
std::vector< double > DGtal::AngleLinearMinimizer::getGradient ( ) const
Returns:
the energy gradient computed at the current position.

Definition at line 126 of file AngleLinearMinimizer.cpp.

References DGtal::AngleComputer::deviation(), DGtal::AngleLinearMinimizer::ValueInfo::distToNext, DGtal::ModuloComputer< TInteger >::next(), DGtal::ModuloComputer< TInteger >::previous(), and DGtal::AngleLinearMinimizer::ValueInfo::value.

{
vector<double> grad( size() );
for ( unsigned int i = 0; i < size(); i++ )
{
unsigned int iprev = mc.previous( i );
unsigned int inext = mc.next( i );
const ValueInfo & vi = this->ro( i );
double val = vi.value;
if ( myIsCurveOpen && ( i == ( size() - 1 ) ) )
{ // free extremity to the front/right.
const ValueInfo & viprev = this->ro( iprev );
double valp = viprev.value;
grad[ i ] = 2.0 * AngleComputer::deviation( val, valp ) / viprev.distToNext;
}
else if ( myIsCurveOpen && ( i == 0 ) )
{ // free extremity to the back/left.
const ValueInfo & vinext = this->ro( inext );
double valn = vinext.value;
grad[ i ] = -2.0 * AngleComputer::deviation( valn, val ) / vi.distToNext;
}
else
{ // standard case.
const ValueInfo & viprev = this->ro( iprev );
const ValueInfo & vinext = this->ro( inext );
double valp = viprev.value;
double valn = vinext.value;
grad[ i ] = 2.0*( AngleComputer::deviation( val, valp ) / viprev.distToNext
- AngleComputer::deviation( valn, val ) / vi.distToNext ) ;
}
}
return grad;
}
void DGtal::AngleLinearMinimizer::init ( unsigned int  nbMax)

(Re-)initializes the linear minimizer.

Parameters:
nbMaxan upper bound for the number of values to optimize.

Definition at line 71 of file AngleLinearMinimizer.cpp.

{
reset();
myValues = new ValueInfo[ nbMax ];
myMaxSize = nbMax;
mySize = nbMax;
myIsCurveOpen = false;
}
bool DGtal::AngleLinearMinimizer::isValid ( ) const

Checks the validity/consistency of the object.

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

Definition at line 515 of file AngleLinearMinimizer.cpp.

{
return true;
}
double DGtal::AngleLinearMinimizer::lastDelta ( ) const
virtual

Should be used to stop the minimization process. The smaller is this value, the more the optimization is at an end. May have several meanings, like the infinite norm of the last displacement or the infinite norm of the projected gradient.

Returns:
an upper bound on the norm of the last displacement.

Reimplemented in DGtal::AngleLinearMinimizerByAdaptiveStepGradientDescent, DGtal::AngleLinearMinimizerByGradientDescent, and DGtal::AngleLinearMinimizerByRelaxation.

Definition at line 247 of file AngleLinearMinimizer.cpp.

{
return max();
}
double DGtal::AngleLinearMinimizer::max ( ) const
inline

Max of all the absolute displacements of the last optimisation step.

Definition at line 62 of file AngleLinearMinimizer.ih.

{
return myMax;
}
unsigned int DGtal::AngleLinearMinimizer::maxSize ( ) const
inline
Returns:
the maximum number of values stored in the object.

Definition at line 149 of file AngleLinearMinimizer.ih.

{
return myMaxSize;
}
void DGtal::AngleLinearMinimizer::oneStep ( unsigned int  i1,
unsigned int  i2 
)
protectedvirtual

The method which performs the optimization effectively. The user may override it. The optimization is performed on values [i1] included to [i2] excluded. Afterwards, the field ValueInfo::value should contain the new value.

Parameters:
i1the first value to be optimized (between 0 and 'size()-1').
i2the value after the last to be optimized (between 0 and 'size()-1').

Reimplemented in DGtal::AngleLinearMinimizerByAdaptiveStepGradientDescent, DGtal::AngleLinearMinimizerByGradientDescent, and DGtal::AngleLinearMinimizerByRelaxation.

Definition at line 254 of file AngleLinearMinimizer.cpp.

References DGtal::AngleComputer::cast(), DGtal::AngleComputer::deviation(), DGtal::AngleLinearMinimizer::ValueInfo::distToNext, DGtal::AngleComputer::less(), DGtal::AngleLinearMinimizer::ValueInfo::max, DGtal::AngleLinearMinimizer::ValueInfo::min, DGtal::ModuloComputer< TInteger >::next(), DGtal::AngleLinearMinimizer::ValueInfo::oldValue, DGtal::ModuloComputer< TInteger >::previous(), and DGtal::AngleLinearMinimizer::ValueInfo::value.

{
double mid;
unsigned int i = i1;
unsigned int iprev = mc.previous( i );
do
{
unsigned int inext = mc.next( i );
ValueInfo & vi = this->rw( i );
if ( myIsCurveOpen && ( i == ( size() - 1 ) ) )
{ // free extremity to the front/right.
const ValueInfo & viprev = this->ro( iprev );
mid = viprev.oldValue; // - viprev.delta;
}
else if ( myIsCurveOpen && ( i == 0 ) )
{ // free extremity to the back/left.
const ValueInfo & vinext = this->ro( inext );
mid = vinext.oldValue; // + vi.delta;
}
else
{ // standard case.
const ValueInfo & viprev = this->ro( iprev );
const ValueInfo & vinext = this->ro( inext );
double valp = viprev.oldValue; // - viprev.delta;
double valn = vinext.oldValue; // + vi.delta;
// old
double y = AngleComputer::deviation( valn, valp );
mid = ( viprev.distToNext * y )
/ ( vi.distToNext + viprev.distToNext );
mid = AngleComputer::cast( mid + valp );
}
if ( AngleComputer::less( mid, vi.min ) ) mid = vi.min;
if ( AngleComputer::less( vi.max, mid ) ) mid = vi.max;
double mvt = AngleComputer::deviation( mid, vi.oldValue );
vi.value = AngleComputer::cast( vi.oldValue + 0.5 * mvt );
// go to next.
iprev = i;
i = inext;
}
while ( i != i2 );
}
AngleLinearMinimizer& DGtal::AngleLinearMinimizer::operator= ( const AngleLinearMinimizer other)
private

Assignment.

Parameters:
otherthe object to copy.
Returns:
a reference on 'this'. Forbidden by default.
double DGtal::AngleLinearMinimizer::optimize ( )

Move each value on the straight line joining its neighboring values while maintaining the value between its bounds. The optimization is performed on all values. After preparing datas, calls oneStep, which may be overriden.

Returns:
the sum of the displacements.
See also:
oneStep

Definition at line 204 of file AngleLinearMinimizer.cpp.

{
return optimize( 0, 0 );
}
double DGtal::AngleLinearMinimizer::optimize ( unsigned int  i1,
unsigned int  i2 
)

Move each value on the straight line joining its neighboring values while maintaining the value between its bounds. The optimization is performed on values [i1] included to [i2] excluded. After preparing datas, calls oneStep, which may be overriden. Then, computes max and sum of displacements.

Parameters:
i1the first value to be optimized (between 0 and 'size()-1').
i2the value after the last to be optimized (between 0 and 'size()-1').
Returns:
the sum of the displacements.
See also:
oneStep

Definition at line 211 of file AngleLinearMinimizer.cpp.

References DGtal::AngleComputer::deviation(), DGtal::ModuloComputer< TInteger >::next(), DGtal::AngleLinearMinimizer::ValueInfo::oldValue, and DGtal::AngleLinearMinimizer::ValueInfo::value.

{
ASSERT( size() > 2 );
unsigned int i = i1;
// (I) first pass to work on old values.
do
{
ValueInfo & vi = this->rw( i );
vi.oldValue = vi.value;
// go to next.
i = mc.next( i );
}
while ( i != i2 );
this->oneStep( i1, i2 );
mySum = 0.0;
myMax = 0.0;
i = i1;
do
{
const ValueInfo & vi = this->ro( i );
double diff = fabs( AngleComputer::deviation( vi.value, vi.oldValue ) );
if ( diff > myMax )
myMax = diff;
mySum += diff;
i = mc.next( i );
}
while ( i != i2 );
return mySum;
}
void DGtal::AngleLinearMinimizer::reset ( )

Reset all. The object is as if it has just been created.

Definition at line 58 of file AngleLinearMinimizer.cpp.

Referenced by ~AngleLinearMinimizer().

{
if ( myValues != 0 )
{
delete[] myValues;
myValues = 0;
}
mySum = 0.0;
myMax = 0.0;
}
const DGtal::AngleLinearMinimizer::ValueInfo & DGtal::AngleLinearMinimizer::ro ( unsigned int  i) const
inline
Returns:
a const reference on the information structure of the [i]th value.

Definition at line 137 of file AngleLinearMinimizer.ih.

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

{
ASSERT( ( myValues != 0 ) && ( i < maxSize() ) );
return myValues[ i ];
}
DGtal::AngleLinearMinimizer::ValueInfo & DGtal::AngleLinearMinimizer::rw ( unsigned int  i)
inline
Returns:
a reference on the information structure of the [i]th value.

Definition at line 125 of file AngleLinearMinimizer.ih.

{
ASSERT( ( myValues != 0 ) && ( i < maxSize() ) );
return myValues[ i ];
}
void DGtal::AngleLinearMinimizer::selfDisplay ( std::ostream &  aStream) const

Writes/Displays the object on an output stream.

Parameters:
outthe output stream where the object is written.

Reimplemented in DGtal::AngleLinearMinimizerByAdaptiveStepGradientDescent, DGtal::AngleLinearMinimizerByGradientDescent, and DGtal::AngleLinearMinimizerByRelaxation.

Definition at line 474 of file AngleLinearMinimizer.cpp.

{
aStream << "[AngleLinearMinimizer::standard 0.5]";
}
void DGtal::AngleLinearMinimizer::setIsCurveOpen ( bool  isCurveOpen = false)
inline

Specifies if the curve is open or not.

Parameters:
is_curve_openwhen 'true' the curve is open and the last value does not depend on the first one, otherwise the curve is closed and the last value is linked to the first one.

Definition at line 187 of file AngleLinearMinimizer.ih.

{
myIsCurveOpen = isCurveOpen;
}
void DGtal::AngleLinearMinimizer::setSize ( unsigned int  nb)
inline

Specifies the exact number of valid values.

Parameters:
nbany number below 'maxSize()'.

Definition at line 172 of file AngleLinearMinimizer.ih.

{
ASSERT( nb <= maxSize() );
mySize = nb;
}
unsigned int DGtal::AngleLinearMinimizer::size ( ) const
inline
Returns:
the number of values stored in the object.

Definition at line 160 of file AngleLinearMinimizer.ih.

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

{
return mySize;
}
double DGtal::AngleLinearMinimizer::sum ( ) const
inline

Sum of all the absolute displacements of the last optimisation step.

Definition at line 52 of file AngleLinearMinimizer.ih.

References mySum.

{
return mySum;
}

Field Documentation

bool DGtal::AngleLinearMinimizer::myIsCurveOpen
protected

Indicates if the curve is open.

Definition at line 305 of file AngleLinearMinimizer.h.

double DGtal::AngleLinearMinimizer::myMax
private

Max of all the absolute displacements of the last optimisation step.

Definition at line 338 of file AngleLinearMinimizer.h.

unsigned int DGtal::AngleLinearMinimizer::myMaxSize
private

The size of the array myValues, ie the maximal number of valid values.

Definition at line 323 of file AngleLinearMinimizer.h.

unsigned int DGtal::AngleLinearMinimizer::mySize
protected

The meaningful size of the array [myValues], ie the number of valid entries.

Definition at line 316 of file AngleLinearMinimizer.h.

double DGtal::AngleLinearMinimizer::mySum
private

Sum of all the absolute displacements of the last optimisation step.

Definition at line 333 of file AngleLinearMinimizer.h.

Referenced by sum().

ValueInfo* DGtal::AngleLinearMinimizer::myValues
protected

The dynamically allocated array of values and the associated constraints.

Definition at line 310 of file AngleLinearMinimizer.h.


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