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. More...
 
void clear ()
 Clears all samples in curve. More...
 
linear (const float t) const
 Linearly interpolates a value from the curve. More...
 
size_t numSamples () const
 Returns the number of samples in the curve. More...
 
const SampleVecsamples () const
 Returns a const reference to the samples in the curve. More...
 

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. More...
 
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. More...
 

Private Attributes

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

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 78 of file Curve.h.

Member Typedef Documentation

◆ Sample

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

Definition at line 84 of file Curve.h.

◆ SampleVec

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

Definition at line 85 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 172 of file Curve.h.

173 {
174  using namespace std;
175  // Check that sample time is not already in curve
176  typename SampleVec::iterator i =
177  find_if(m_samples.begin(), m_samples.end(), CheckTEqual(t));
178  if (i != m_samples.end()) {
179  // Sample position already exists, so we replace it
180  i->second = value;
181  return;
182  }
183  // Find the first sample location that is greater than the interpolation
184  // position
185  i = find_if(m_samples.begin(), m_samples.end(), CheckTGreaterThan(t));
186  // If we get something other than end() back then we insert the new
187  // sample before that. If there wasn't a larger value we add this sample
188  // to the end of the vector.
189  if (i != m_samples.end()) {
190  m_samples.insert(i, make_pair(t, value));
191  } else {
192  m_samples.push_back(make_pair(t, value));
193  }
194 }

Referenced by MatrixFieldMapping::setLocalToWorld(), FrustumFieldMapping::setTransforms(), and MatrixFieldMapping::updateTransform().

◆ 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 199 of file Curve.h.

200 {
201  using namespace std;
202  // If there are no samples, return zero
203  if (m_samples.size() == 0) {
204  return defaultReturnValue();
205  }
206  // Find the first sample location that is greater than the interpolation
207  // position
208  typename SampleVec::const_iterator i =
209  find_if(m_samples.begin(), m_samples.end(), CheckTGreaterThan(t));
210  // If we get end() back then there was no sample larger, so we return the
211  // last value. If we got the first value then there is only one value and
212  // we return that.
213  if (i == m_samples.end()) {
214  return m_samples.back().second;
215  } else if (i == m_samples.begin()) {
216  return m_samples.front().second;
217  }
218  // Interpolate between the nearest two samples.
219  const Sample &upper = *i;
220  const Sample &lower = *(--i);
221  const float interpT = Imath::lerpfactor(t, lower.first, upper.first);
222  return lerp(lower, upper, interpT);
223 }

Referenced by FrustumFieldMapping::localToWorld(), MatrixFieldMapping::updateTransform(), and FrustumFieldMapping::worldToLocal().

◆ numSamples()

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

Returns the number of samples in the curve.

Definition at line 99 of file Curve.h.

100  { return m_samples.size(); }

Referenced by MatrixFieldMapping::setLocalToWorld(), and MatrixFieldMapping::updateTransform().

◆ samples()

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

Returns a const reference to the samples in the curve.

Definition at line 103 of file Curve.h.

104  { return m_samples; }

Referenced by MatrixFieldMapping::isIdentical(), FrustumFieldMapping::isIdentical(), and MatrixFieldMapping::updateTransform().

◆ clear()

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

Clears all samples in curve.

Definition at line 107 of file Curve.h.

108  { SampleVec().swap(m_samples); }

Referenced by FrustumFieldMapping::clearCurves(), MatrixFieldMapping::makeIdentity(), and MatrixFieldMapping::updateTransform().

◆ 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 150 of file Curve.h.

151  { return T(0); }

◆ 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 156 of file Curve.h.

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

◆ defaultReturnValue() [2/3]

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

Definition at line 231 of file Curve.h.

232 {
233  Imath::Matrix44<float> identity;
234  identity.makeIdentity();
235  return identity;
236 }

◆ defaultReturnValue() [3/3]

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

Definition at line 242 of file Curve.h.

243 {
244  Imath::Matrix44<double> identity;
245  identity.makeIdentity();
246  return identity;
247 }

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 163 of file Curve.h.

Referenced by Curve< Imath::M44d >::clear(), Curve< Imath::M44d >::numSamples(), and Curve< Imath::M44d >::samples().


The documentation for this class was generated from the following file:
Curve::m_samples
SampleVec m_samples
Stores the samples that define the curve. Sample insertion ensures that the samples are sorted accord...
Definition: Curve.h:163
Curve::Sample
std::pair< float, T > Sample
Definition: Curve.h:84
Curve::lerp
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:156
Curve::SampleVec
std::vector< Sample > SampleVec
Definition: Curve.h:85
Curve::defaultReturnValue
T defaultReturnValue() const
The default return value is used when no sample points are available. This defaults to zero,...
Definition: Curve.h:150