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>

List of all members.

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.
template<>
Imath::Matrix44< double > defaultReturnValue () const
template<>
Imath::Matrix44< float > 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 78 of file Curve.h.


Member Typedef Documentation

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

Definition at line 84 of file Curve.h.

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

Definition at line 85 of file Curve.h.


Member Function Documentation

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.

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

{
  using namespace std;
  // Check that sample time is not already in curve
  typename SampleVec::iterator i =                                        
    find_if(m_samples.begin(), m_samples.end(), CheckTEqual(t));
  if (i != m_samples.end()) {
    // Sample position already exists, so we replace it
    i->second = value;
    return;
  }
  // Find the first sample location that is greater than the interpolation
  // position                                                             
  i = find_if(m_samples.begin(), m_samples.end(), CheckTGreaterThan(t));
  // If we get something other than end() back then we insert the new     
  // sample before that. If there wasn't a larger value we add this sample
  // to the end of the vector.
  if (i != m_samples.end()) {
    m_samples.insert(i, make_pair(t, value));
  } else {
    m_samples.push_back(make_pair(t, value));
  }
}
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.

Referenced by FrustumFieldMapping::cameraToWorld(), FrustumFieldMapping::farPlane(), FrustumFieldMapping::localToWorld(), MatrixFieldMapping::localToWorld(), FrustumFieldMapping::nearPlane(), FrustumFieldMapping::screenToWorld(), MatrixFieldMapping::updateTransform(), MatrixFieldMapping::voxelToWorld(), FrustumFieldMapping::worldToLocal(), MatrixFieldMapping::worldToLocal(), and MatrixFieldMapping::worldToVoxel().

{
  using namespace std;
  // If there are no samples, return zero
  if (m_samples.size() == 0) {
    return defaultReturnValue();
  }
  // Find the first sample location that is greater than the interpolation
  // position
  typename SampleVec::const_iterator i =
    find_if(m_samples.begin(), m_samples.end(), CheckTGreaterThan(t));
  // If we get end() back then there was no sample larger, so we return the
  // last value. If we got the first value then there is only one value and
  // we return that.
  if (i == m_samples.end()) {
    return m_samples.back().second;
  } else if (i == m_samples.begin()) {
    return m_samples.front().second;
  }
  // Interpolate between the nearest two samples.
  const Sample &upper = *i;
  const Sample &lower = *(--i);
  const float interpT = Imath::lerpfactor(t, lower.first, upper.first);
  return lerp(lower, upper, interpT);
}
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.

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

  { return m_samples.size(); }
template<typename T>
void Curve< T >::clear ( ) [inline]

Clears all samples in curve.

Definition at line 107 of file Curve.h.

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

  { SampleVec().swap(m_samples); }
template<typename T>
T Curve< T >::defaultReturnValue ( ) const [inline, private]

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.

  { return T(0); }
template<typename T>
T Curve< T >::lerp ( const Sample lower,
const Sample upper,
const float  t 
) const [inline, private]

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.

Referenced by Curve< Imath::M44d >::lerp().

  { return Imath::lerp(lower.second, upper.second, t); }
template<>
Imath::Matrix44< float > Curve< Imath::Matrix44< float > >::defaultReturnValue ( ) const [inline, private]

Definition at line 231 of file Curve.h.

{ 
  Imath::Matrix44<float> identity;
  identity.makeIdentity();
  return identity;
}
template<>
Imath::Matrix44< double > Curve< Imath::Matrix44< double > >::defaultReturnValue ( ) const [inline, private]

Definition at line 242 of file Curve.h.

{ 
  Imath::Matrix44<double> identity;
  identity.makeIdentity();
  return identity;
}

Member Data Documentation

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: