NL3D::CPSFloatCurveFunctor Class Reference

#include <ps_float.h>


Detailed Description

this functor produce float based on a hermite curve NB : must be init before use, or assert occurs

Definition at line 107 of file ps_float.h.

Public Member Functions

void addControlPoint (const CCtrlPoint &ctrlPoint)
 =======================================================================================

 CPSFloatCurveFunctor ()
void enableSmoothing (bool enable=true)
 =======================================================================================

const CCtrlPointgetControlPoint (uint index) const
 =======================================================================================

float getMaxValue () const
float getMinValue () const
uint getNumCtrlPoints (void) const
 retrieve the number of control points

uint32 getNumSamples (void) const
 get the numer of samples used with this curb

float getValue (float date) const
 =======================================================================================

bool hasSmoothing (void) const
 test wether smoothing is enabled

float operator() (TAnimationTime time) const
void removeCtrlPoint (uint index)
 =======================================================================================

void serial (NLMISC::IStream &f) throw (NLMISC::EStream)
 =======================================================================================

void setCtrlPoint (uint index, const CCtrlPoint &ctrlPoint)
 =======================================================================================

void setNumSamples (uint32 numSamples)
 =======================================================================================


Protected Member Functions

float getSlope (uint index) const
 =======================================================================================

void sortPoints (void)
 =======================================================================================

void updateTab (void)
 =======================================================================================


Protected Attributes

CPSVector< CCtrlPoint >::V _CtrlPoints
float _MaxValue
float _MinValue
uint32 _NumSamples
bool _Smoothing
CPSVector< float >::V _Tab


Constructor & Destructor Documentation

NL3D::CPSFloatCurveFunctor::CPSFloatCurveFunctor  ) 
 

ctor. The default is a cst function whose value is .5 NB : must be init before use, or assert occurs

Definition at line 50 of file ps_float.cpp.

00050                                            : _NumSamples(0), _Smoothing(true)
00051 {
00052         /*
00053         _CtrlPoints.push_back(CCtrlPoint(0, 0.5f));
00054         _CtrlPoints.push_back(CCtrlPoint(1, 0.5f));     
00055         updateTab();    
00056         */
00057 }


Member Function Documentation

void NL3D::CPSFloatCurveFunctor::addControlPoint const CCtrlPoint ctrlPoint  ) 
 

=======================================================================================

Add a control point. There is a sorted insertion based on the date

Parameters:
index renge from 0 to 3

Definition at line 66 of file ps_float.cpp.

References _CtrlPoints, sortPoints(), and updateTab().

00067 {
00068         _CtrlPoints.push_back(ctrlPoint);
00069         sortPoints();
00070         updateTab();
00071 }

void NL3D::CPSFloatCurveFunctor::enableSmoothing bool  enable = true  ) 
 

=======================================================================================

Definition at line 196 of file ps_float.cpp.

References _Smoothing, and updateTab().

00197 { 
00198         _Smoothing = enable;
00199         updateTab();
00200 }

const CPSFloatCurveFunctor::CCtrlPoint & NL3D::CPSFloatCurveFunctor::getControlPoint uint  index  )  const
 

=======================================================================================

get a control point.

Returns:
a <date, value> std::pair

Definition at line 74 of file ps_float.cpp.

References _CtrlPoints, index, and uint.

00075 {
00076         return _CtrlPoints[index];
00077 }

float NL3D::CPSFloatCurveFunctor::getMaxValue void   )  const [inline]
 

Definition at line 178 of file ps_float.h.

00178 { return _MaxValue; }

float NL3D::CPSFloatCurveFunctor::getMinValue void   )  const [inline]
 

Definition at line 177 of file ps_float.h.

00177 { return _MinValue; }

uint NL3D::CPSFloatCurveFunctor::getNumCtrlPoints void   )  const [inline]
 

retrieve the number of control points

Definition at line 133 of file ps_float.h.

References uint.

00133 { return _CtrlPoints.size(); }                  

uint32 NL3D::CPSFloatCurveFunctor::getNumSamples void   )  const [inline]
 

get the numer of samples used with this curb

Definition at line 150 of file ps_float.h.

References uint32.

00150 { return _NumSamples; }

float NL3D::CPSFloatCurveFunctor::getSlope uint  index  )  const [protected]
 

=======================================================================================

Definition at line 171 of file ps_float.cpp.

References _CtrlPoints, index, and uint.

Referenced by getValue().

00172 {       
00173         // tangent for first point
00174         if (index == 0)
00175         {
00176                 return _CtrlPoints[1].Date != _CtrlPoints[0].Date ? (_CtrlPoints[1].Value - _CtrlPoints[0].Value) 
00177                                                                                                                          / (_CtrlPoints[1].Date - _CtrlPoints[0].Date)
00178                                                                                                                   : 1e6f;
00179         }
00180 
00181         // tangent for last point
00182         if (index == _CtrlPoints.size() - 1)
00183         {
00184                 return _CtrlPoints[index].Date != _CtrlPoints[index - 1].Date ? (_CtrlPoints[index].Value - _CtrlPoints[index - 1].Value) 
00185                                                                                                                                                 / (_CtrlPoints[index].Date - _CtrlPoints[index - 1].Date)
00186                                                                                                                                           : 1e6f;
00187         }
00188 
00189         // tangent for other points
00190         return _CtrlPoints[index + 1].Date != _CtrlPoints[index - 1].Date ? (_CtrlPoints[index + 1].Value - _CtrlPoints[index - 1].Value)
00191                                                                                                                                                 / (_CtrlPoints[index + 1].Date - _CtrlPoints[index - 1].Date)
00192                                                                                                                                           : 1e6f;       
00193 }

float NL3D::CPSFloatCurveFunctor::getValue float  date  )  const
 

=======================================================================================

Definition at line 105 of file ps_float.cpp.

References _CtrlPoints, _Smoothing, NLMISC::clamp(), getSlope(), index, NLMISC::sqr(), uint, and width.

Referenced by updateTab().

00106 {
00107         if (_CtrlPoints.empty()) return 0.f;
00108         NLMISC::clamp(date, 0, 1);      
00109         // find a key that has a higher value
00110         CPSVector<CCtrlPoint>::V::const_iterator it = _CtrlPoints.begin();
00111         while ( it != _CtrlPoints.end() && it->Date <= date ) ++it;
00112 
00113         if (it == _CtrlPoints.begin()) return _CtrlPoints[0].Value;
00114         if (it == _CtrlPoints.end()) return _CtrlPoints[_CtrlPoints.size() - 1].Value;
00115         CPSVector<CCtrlPoint>::V::const_iterator precIt = it - 1;
00116         if (precIt->Date == it->Date) return 0.5f * (precIt->Value + it->Value);
00117         const float lambda = (date - precIt->Date) / (it->Date - precIt->Date);
00118         if (!_Smoothing) // linear interpolation
00119         {               
00120                 return lambda * it->Value + (1.f - lambda) * precIt->Value;
00121         }
00122         else // hermite interpolation
00123         {
00124                 float width = it->Date - precIt->Date;
00125                 uint index = precIt - _CtrlPoints.begin();
00126                 float t1 = getSlope(index) * width, t2 = getSlope(index + 1) * width;
00127                 const float lambda2 = NLMISC::sqr(lambda);
00128                 const float lambda3 = lambda2 * lambda;
00129                 const float h1 = 2 * lambda3 - 3 * lambda2 + 1; 
00130                 const float h2 = - 2 * lambda3 + 3 * lambda2; 
00131                 const float h3 = lambda3 - 2 * lambda2 + lambda; 
00132                 const float h4 = lambda3 - lambda2; 
00133 
00134                 return h1 * precIt->Value + h2 * it->Value + h3 * t1 + h4 * t2;
00135         }
00136 }

bool NL3D::CPSFloatCurveFunctor::hasSmoothing void   )  const [inline]
 

test wether smoothing is enabled

Definition at line 156 of file ps_float.h.

00156 { return _Smoothing;}

float NL3D::CPSFloatCurveFunctor::operator() TAnimationTime  time  )  const [inline]
 

This return a sampled value from the hermite curb. The more steps there are, the more accurate it is You can also get an 'exact value'. This must be called between beginFastFloor() and endFastFloor() statements

See also:
getValue

Definition at line 166 of file ps_float.h.

References NLMISC::OptFastFloor(), and NL3D::TAnimationTime.

00167                 {
00168                         return _Tab[NLMISC::OptFastFloor(time * _NumSamples)];
00169                 }

void NL3D::CPSFloatCurveFunctor::removeCtrlPoint uint  index  ) 
 

=======================================================================================

Definition at line 89 of file ps_float.cpp.

References _CtrlPoints, index, nlassert, uint, and updateTab().

00090 {
00091         nlassert(_CtrlPoints.size() > 1);
00092         _CtrlPoints.erase(_CtrlPoints.begin() + index);
00093         updateTab();
00094 }

void NL3D::CPSFloatCurveFunctor::serial NLMISC::IStream f  )  throw (NLMISC::EStream)
 

=======================================================================================

Definition at line 159 of file ps_float.cpp.

00160 {
00161         f.serialVersion(1);
00162         f.serial(_NumSamples, _Smoothing);
00163         f.serialCont(_CtrlPoints);
00164         if (f.isReading())
00165         {
00166                 updateTab();
00167         }
00168 }

void NL3D::CPSFloatCurveFunctor::setCtrlPoint uint  index,
const CCtrlPoint ctrlPoint
 

=======================================================================================

Definition at line 80 of file ps_float.cpp.

References _CtrlPoints, NL3D::CPSFloatCurveFunctor::CCtrlPoint::Date, index, nlassert, sortPoints(), uint, and updateTab().

00081 {
00082         nlassert(ctrlPoint.Date >= 0 && ctrlPoint.Date <= 1);
00083         _CtrlPoints[index] = ctrlPoint;
00084         sortPoints();
00085         updateTab();
00086 }

void NL3D::CPSFloatCurveFunctor::setNumSamples uint32  numSamples  ) 
 

=======================================================================================

Definition at line 97 of file ps_float.cpp.

References _NumSamples, nlassert, uint32, and updateTab().

00098 {
00099         nlassert(numSamples > 0);
00100         _NumSamples = numSamples;
00101         updateTab();
00102 }

void NL3D::CPSFloatCurveFunctor::sortPoints void   )  [protected]
 

=======================================================================================

Definition at line 60 of file ps_float.cpp.

References _CtrlPoints.

Referenced by addControlPoint(), and setCtrlPoint().

00061 {
00062         std::sort(_CtrlPoints.begin(), _CtrlPoints.end());
00063 }

void NL3D::CPSFloatCurveFunctor::updateTab void   )  [protected]
 

=======================================================================================

Definition at line 139 of file ps_float.cpp.

References _NumSamples, getValue(), min, and uint.

Referenced by addControlPoint(), enableSmoothing(), removeCtrlPoint(), setCtrlPoint(), and setNumSamples().

00140 {
00141         float step  = 1.f / _NumSamples;
00142         float d = 0.f;
00143         _Tab.resize(_NumSamples + 1);
00144         uint k;
00145         for (k = 0; k <= _NumSamples; ++k)
00146         {
00147                 _Tab[k] = getValue(d);
00148                 d += step;
00149         }
00150         _MinValue = _MaxValue = _Tab[0];
00151         for (k = 1; k <= _NumSamples; ++k)
00152         {               
00153                 _MinValue = std::min(_MinValue, _Tab[k]);
00154                 _MaxValue = std::max(_MaxValue, _Tab[k]);
00155         }
00156 }


Field Documentation

CPSVector<CCtrlPoint>::V NL3D::CPSFloatCurveFunctor::_CtrlPoints [protected]
 

Definition at line 187 of file ps_float.h.

Referenced by addControlPoint(), getControlPoint(), getSlope(), getValue(), removeCtrlPoint(), setCtrlPoint(), and sortPoints().

float NL3D::CPSFloatCurveFunctor::_MaxValue [protected]
 

Definition at line 192 of file ps_float.h.

float NL3D::CPSFloatCurveFunctor::_MinValue [protected]
 

Definition at line 191 of file ps_float.h.

uint32 NL3D::CPSFloatCurveFunctor::_NumSamples [protected]
 

Definition at line 188 of file ps_float.h.

Referenced by setNumSamples(), and updateTab().

bool NL3D::CPSFloatCurveFunctor::_Smoothing [protected]
 

Definition at line 190 of file ps_float.h.

Referenced by enableSmoothing(), and getValue().

CPSVector<float>::V NL3D::CPSFloatCurveFunctor::_Tab [protected]
 

Definition at line 189 of file ps_float.h.


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 07:13:32 2004 for NeL by doxygen 1.3.6