Field3D
Curve< T > Class Template Reference

Implements a simple function curve where samples of type T can be added along a 1D axis. Once samples exist they can be interpolated using the linear() call. More...

#include <Curve.h>

Classes

struct  CheckTEqual
 Used when finding values in the m_samples vector. More...
 
struct  CheckTGreaterThan
 Used when finding values in the m_samples vector. More...
 

Public Types

typedef std::pair< float, T > Sample
 
typedef std::vector< SampleSampleVec
 

Public Member Functions

void addSample (const float t, const T &value)
 Adds a sample point to the curve.
 
void clear ()
 Clears all samples in curve.
 
linear (const float t) const
 Linearly interpolates a value from the curve.
 
size_t numSamples () const
 Returns the number of samples in the curve.
 
const SampleVecsamples () const
 Returns a const reference to the samples in the curve.
 

Private Member Functions

defaultReturnValue () const
 The default return value is used when no sample points are available. This defaults to zero, but for some types (for example Quaternion), We need more arguments to the constructor. In these cases the method is specialized for the given T type.
 
Imath::Matrix44< float > defaultReturnValue () const
 
Imath::Matrix44< double > defaultReturnValue () const
 
lerp (const Sample &lower, const Sample &upper, const float t) const
 The default implementation for linear interpolation. Works for all classes for which Imath::lerp is implemented (i.e float/double, V2f, V3f). For other types this method needs to be specialized.
 

Private Attributes

SampleVec m_samples
 Stores the samples that define the curve. Sample insertion ensures that the samples are sorted according to Sample.first.
 

Detailed Description

template<typename T>
class Curve< T >

Implements a simple function curve where samples of type T can be added along a 1D axis. Once samples exist they can be interpolated using the linear() call.

Definition at line 95 of file Curve.h.

Member Typedef Documentation

◆ Sample

template<typename T>
typedef std::pair<float, T> Curve< T >::Sample

Definition at line 101 of file Curve.h.

◆ SampleVec

template<typename T>
typedef std::vector<Sample> Curve< T >::SampleVec

Definition at line 102 of file Curve.h.

Member Function Documentation

◆ addSample()

template<typename T>
void Curve< T >::addSample ( const float t,
const T & value )

Adds a sample point to the curve.

Parameters
tSample position
valueSample value

Definition at line 189 of file Curve.h.

190{
191 using namespace std;
192 // Check that sample time is not already in curve
193 typename SampleVec::iterator i =
194 find_if(m_samples.begin(), m_samples.end(), CheckTEqual(t));
195 if (i != m_samples.end()) {
196 // Sample position already exists, so we replace it
197 i->second = value;
198 return;
199 }
200 // Find the first sample location that is greater than the interpolation
201 // position
202 i = find_if(m_samples.begin(), m_samples.end(), CheckTGreaterThan(t));
203 // If we get something other than end() back then we insert the new
204 // sample before that. If there wasn't a larger value we add this sample
205 // to the end of the vector.
206 if (i != m_samples.end()) {
207 m_samples.insert(i, make_pair(t, value));
208 } else {
209 m_samples.push_back(make_pair(t, value));
210 }
211}
Implements a simple function curve where samples of type T can be added along a 1D axis....
Definition Curve.h:96
SampleVec m_samples
Stores the samples that define the curve. Sample insertion ensures that the samples are sorted accord...
Definition Curve.h:180
Used when finding values in the m_samples vector.
Definition Curve.h:149
Used when finding values in the m_samples vector.
Definition Curve.h:134

References m_samples.

◆ linear()

template<typename T>
T Curve< T >::linear ( const float t) const

Linearly interpolates a value from the curve.

Parameters
tPosition along curve

Definition at line 216 of file Curve.h.

217{
218 using namespace std;
219 // If there are no samples, return zero
220 if (m_samples.size() == 0) {
221 return defaultReturnValue();
222 }
223 // Find the first sample location that is greater than the interpolation
224 // position
227 // If we get end() back then there was no sample larger, so we return the
228 // last value. If we got the first value then there is only one value and
229 // we return that.
230 if (i == m_samples.end()) {
231 return m_samples.back().second;
232 } else if (i == m_samples.begin()) {
233 return m_samples.front().second;
234 }
235 // Interpolate between the nearest two samples.
236 const Sample &upper = *i;
237 const Sample &lower = *(--i);
238 const float interpT = Imath::lerpfactor(t, lower.first, upper.first);
239 return lerp(lower, upper, interpT);
240}
T lerp(const Sample &lower, const Sample &upper, const float t) const
The default implementation for linear interpolation. Works for all classes for which Imath::lerp is i...
Definition Curve.h:173
std::pair< float, T > Sample
Definition Curve.h:101
T defaultReturnValue() const
The default return value is used when no sample points are available. This defaults to zero,...
Definition Curve.h:167

References defaultReturnValue(), lerp(), and m_samples.

◆ numSamples()

template<typename T>
size_t Curve< T >::numSamples ( ) const
inline

Returns the number of samples in the curve.

Definition at line 116 of file Curve.h.

117 { return m_samples.size(); }

◆ samples()

template<typename T>
const SampleVec & Curve< T >::samples ( ) const
inline

Returns a const reference to the samples in the curve.

Definition at line 120 of file Curve.h.

121 { return m_samples; }

◆ clear()

template<typename T>
void Curve< T >::clear ( )
inline

Clears all samples in curve.

Definition at line 124 of file Curve.h.

125 { SampleVec().swap(m_samples); }
std::vector< Sample > SampleVec
Definition Curve.h:102

◆ defaultReturnValue() [1/3]

template<typename T>
T Curve< T >::defaultReturnValue ( ) const
inlineprivate

The default return value is used when no sample points are available. This defaults to zero, but for some types (for example Quaternion), We need more arguments to the constructor. In these cases the method is specialized for the given T type.

Definition at line 167 of file Curve.h.

168 { return T(0); }

Referenced by linear().

◆ lerp()

template<typename T>
T Curve< T >::lerp ( const Sample & lower,
const Sample & upper,
const float t ) const
inlineprivate

The default implementation for linear interpolation. Works for all classes for which Imath::lerp is implemented (i.e float/double, V2f, V3f). For other types this method needs to be specialized.

Definition at line 173 of file Curve.h.

174 { return Imath::lerp(lower.second, upper.second, t); }

Referenced by linear().

◆ defaultReturnValue() [2/3]

Imath::Matrix44< float > Curve< Imath::Matrix44< float > >::defaultReturnValue ( ) const
inlineprivate

Definition at line 248 of file Curve.h.

249{
251 identity.makeIdentity();
252 return identity;
253}

◆ defaultReturnValue() [3/3]

Imath::Matrix44< double > Curve< Imath::Matrix44< double > >::defaultReturnValue ( ) const
inlineprivate

Definition at line 259 of file Curve.h.

260{
262 identity.makeIdentity();
263 return identity;
264}

Member Data Documentation

◆ m_samples

template<typename T>
SampleVec Curve< T >::m_samples
private

Stores the samples that define the curve. Sample insertion ensures that the samples are sorted according to Sample.first.

Definition at line 180 of file Curve.h.

Referenced by addSample(), and linear().


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