NLMISC::CMouseSmoother Class Reference

#include <mouse_smoother.h>


Detailed Description

This smooth position of mouse using cubic splines. The mouse is sampled at the given period. The higher the period, the smoother the movement. However there is a delay of 2 * samplingPeriod between the user moving the mouse and the pointer reaching the wanted poistion.

Author:
Nicolas Vizerie

Nevrax France

Date:
1/2004

Definition at line 44 of file mouse_smoother.h.

Public Member Functions

 CMouseSmoother (double samplingPeriod=0.2)
double getSamplingPeriod () const
void reset ()
CVector2f samplePos (const CVector2f &wantedPos, double date)
void setSamplingPeriod (double period)

Private Attributes

bool _Init
CSample _Sample [4]
double _SamplingPeriod


Constructor & Destructor Documentation

NLMISC::CMouseSmoother::CMouseSmoother double  samplingPeriod = 0.2  ) 
 

Definition at line 53 of file mouse_smoother.cpp.

References _Init, _SamplingPeriod, and nlassert.

00054 {
00055         nlassert(samplingPeriod > 0);
00056         _SamplingPeriod = samplingPeriod;
00057         _Init = false;
00058 }


Member Function Documentation

double NLMISC::CMouseSmoother::getSamplingPeriod  )  const [inline]
 

Definition at line 54 of file mouse_smoother.h.

References _SamplingPeriod.

00054 { return _SamplingPeriod; }     

void NLMISC::CMouseSmoother::reset  ) 
 

Definition at line 136 of file mouse_smoother.cpp.

References _Init.

Referenced by setSamplingPeriod().

00137 {
00138         _Init = false;
00139 }

NLMISC::CVector2f NLMISC::CMouseSmoother::samplePos const CVector2f wantedPos,
double  date
 

Definition at line 72 of file mouse_smoother.cpp.

References _Init, _SamplingPeriod, NLMISC::BuildHermiteVector(), NLMISC::clamp(), NLMISC::CMouseSmoother::CSample::Date, min, NLMISC::CMouseSmoother::CSample::Pos, uint, NLMISC::CVector2f::x, and NLMISC::CVector2f::y.

00073 {
00074         if (!_Init)
00075         {
00076                 _Sample[0] = _Sample[1] = _Sample[2] = _Sample[3] = CSample(date, wantedPos);
00077                 _Init = true;
00078         }
00079         else
00080         {
00081                 // see if enough time has elapsed since last sample
00082                 if (date - _Sample[3].Date >= _SamplingPeriod)
00083                 {
00084                         uint numSamples = (uint) floor((date - _Sample[3].Date) / _SamplingPeriod);
00085                         numSamples = std::min(numSamples, (uint) 4);
00086                         for(uint k = 0; k < numSamples; ++k)
00087                         {                       
00088                                 // add a new sample
00089                                 _Sample[0] = _Sample[1];
00090                                 _Sample[1] = _Sample[2];
00091                                 _Sample[2] = _Sample[3];
00092                                 _Sample[3] = CSample(date, wantedPos);
00093                         }
00094                 }               
00095                 else if (date == _Sample[3].Date)
00096                 {
00097                         // update cur pos
00098                         _Sample[3] = CSample(date, wantedPos);
00099                 }               
00100         }
00101         if (_Sample[1].Pos.x == _Sample[2].Pos.x &&
00102                 _Sample[1].Pos.y == _Sample[2].Pos.y            
00103            )
00104         {
00105                 // special case : if pointer hasn't moved, allow a discontinuity of speed
00106                 return _Sample[2].Pos;
00107         }
00108         double evalDate = date - 2 * _SamplingPeriod;   
00109         clamp(evalDate, _Sample[1].Date, _Sample[2].Date);
00110         CVector2f t0;
00111         double dt = _Sample[2].Date - _Sample[1].Date;
00112         if (_Sample[2].Date != _Sample[0].Date)
00113         {
00114                 t0 = (float) dt * (_Sample[2].Pos - _Sample[0].Pos) / (float) (_Sample[2].Date - _Sample[0].Date);
00115         }
00116         else
00117         {
00118                 t0= NLMISC::CVector::Null;
00119         }
00120         CVector2f t1;
00121         if (_Sample[3].Date != _Sample[1].Date)
00122         {
00123                 t1 = (float) dt * (_Sample[3].Pos - _Sample[1].Pos) / (float) (_Sample[3].Date - _Sample[1].Date);
00124         }
00125         else
00126         {
00127                 t1= NLMISC::CVector::Null;
00128         }       
00129         NLMISC::CVector2f result;
00130         if (dt == 0) return _Sample[2].Pos;
00131         BuildHermiteVector(_Sample[1].Pos, _Sample[2].Pos, t0, t1, result, (float) ((evalDate - _Sample[1].Date) / dt));
00132         return result;
00133 }

void NLMISC::CMouseSmoother::setSamplingPeriod double  period  ) 
 

Change the sampling period. The longer it lasts, the more smooth the movement. NB : this reset the smoother

Definition at line 62 of file mouse_smoother.cpp.

References _SamplingPeriod, nlassert, and reset().

00063 {
00064         if (period == _SamplingPeriod) return;
00065         reset();
00066         nlassert(_SamplingPeriod > 0);
00067         _SamplingPeriod = period;
00068 }


Field Documentation

bool NLMISC::CMouseSmoother::_Init [private]
 

Definition at line 75 of file mouse_smoother.h.

Referenced by CMouseSmoother(), reset(), and samplePos().

CSample NLMISC::CMouseSmoother::_Sample[4] [private]
 

4 samples are needed to compute smoothed position : Sample 0 & 2 are used to compute tangent at sample 1 Sample 1 & 3 are used to compute tangent at sample 2

Definition at line 80 of file mouse_smoother.h.

double NLMISC::CMouseSmoother::_SamplingPeriod [private]
 

Definition at line 74 of file mouse_smoother.h.

Referenced by CMouseSmoother(), getSamplingPeriod(), samplePos(), and setSamplingPeriod().


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