mc_trajectory::SequenceInterpolator< Value, InterpolationFunction > Struct Template Reference

Interpolate values in a timed sequence. More...

#include <mc_trajectory/SequenceInterpolator.h>

Collaboration diagram for mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >:

Public Types

using TimedValue = typename std::pair< double, Value >
 
using TimedValueVector = std::vector< TimedValue >
 

Public Member Functions

 SequenceInterpolator () noexcept
 Creates an empty interpolator. More...
 
 SequenceInterpolator (const TimedValueVector &values)
 Creates an interpolator with values. More...
 
void values (const TimedValueVector &values)
 Set interpolator values. More...
 
const TimedValueVectorvalues () const noexcept
 
bool hasValues () const noexcept
 
void clear ()
 
Value compute (double currTime)
 

Protected Attributes

InterpolationFunction interpolator_
 Functor for computing the interpolated values. More...
 
TimedValueVector values_
 Interpolation values. More...
 
size_t prevIndex_ = 0
 Cache the previous index to optimize lookup when used sequentially. More...
 
size_t nextIndex_ = 0
 Cache the next index. More...
 
double intervalDuration_ = 0
 Cache the duration of the current interval. More...
 

Detailed Description

template<typename Value, typename InterpolationFunction = LinearInterpolation<Value>>
struct mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >

Interpolate values in a timed sequence.

This class provides an efficient and generic way to perform interpolation within a sequence of (sorted) intervals expressed as pairs of (time, Value):

  • ensures that values are sorted by strictly ascending time
  • selects the appropriate interval corresponding to the desired computation time
    • O(log n) when interpolating at an arbitrary time (binary search)
    • O(1) in most cases when used sequentially (the current interval is cached)
  • perform interpolation by calling the InterpolationFunction functor on this interval (default: linear interpolation). The time ratio within the current interval is computed as a normalized value between 0 and 1. See LinearInterpolation for example.
InterpolationFunction::operator(const Value &intervalStart, const Value &intervalEnd, double ratio)

Example:

auto values = std::vector<double, double>
{
{0., 0.}, // time, value
{1., 100.},
{2., 200},
}
SequenceInterpolator<double> interp{values}; // Creates a linear interpolator
interp.compute(0); // Compute interpolation at time t = 0
interp.compute(0.005); // compute interpolation at time t = 0.005
interp.compute(1.2); // compute interpolation at time t = 1.2
...
Template Parameters
ValueType of values to interpolate. It must meet the requirements of the InterpolationFunction (typically scalar-Value multiplication and Value-Value addition, e.g arithmetic types, Eigen::Vector, etc)
InterpolationFunctionFunctor for computing the interpolated value.

Member Typedef Documentation

◆ TimedValue

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
using mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::TimedValue = typename std::pair<double, Value>

◆ TimedValueVector

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
using mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::TimedValueVector = std::vector<TimedValue>

Constructor & Destructor Documentation

◆ SequenceInterpolator() [1/2]

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::SequenceInterpolator ( )
inlinenoexcept

Creates an empty interpolator.

You must call the values setter before calling compute()

◆ SequenceInterpolator() [2/2]

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::SequenceInterpolator ( const TimedValueVector values)
inline

Creates an interpolator with values.

Parameters
valuesValues must respect the conditions describes in values
Exceptions
std::runtime_errorIf values are invalid

Member Function Documentation

◆ clear()

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
void mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::clear ( )
inline

Clears all values

◆ compute()

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
Value mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::compute ( double  currTime)
inline

Compute interpolated value at the provided time

Calls the interpolation functor with time normalized between 0 and 1

InterpolationFunction::operator(const Value &, const Value &, double ratio)

{.cpp}

Parameters
currTimeTime at which to compute the interpolation. Out-of-bound access return the first or last value.
Exceptions
std::runtime_errorIf values() is empty

◆ hasValues()

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
bool mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::hasValues ( ) const
inlinenoexcept

When true, this interpolator is in a valid state and compute will always return a valid value.

◆ values() [1/2]

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
const TimedValueVector& mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::values ( ) const
inlinenoexcept

Interpolation values

◆ values() [2/2]

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
void mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::values ( const TimedValueVector values)
inline

Set interpolator values.

Parameters
valuesPairs of (time, value) ordered by strictly ascending time and spread apart by more than dt

Member Data Documentation

◆ interpolator_

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
InterpolationFunction mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::interpolator_
protected

Functor for computing the interpolated values.

◆ intervalDuration_

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
double mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::intervalDuration_ = 0
protected

Cache the duration of the current interval.

◆ nextIndex_

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
size_t mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::nextIndex_ = 0
protected

Cache the next index.

◆ prevIndex_

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
size_t mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::prevIndex_ = 0
protected

Cache the previous index to optimize lookup when used sequentially.

◆ values_

template<typename Value , typename InterpolationFunction = LinearInterpolation<Value>>
TimedValueVector mc_trajectory::SequenceInterpolator< Value, InterpolationFunction >::values_
protected

Interpolation values.


The documentation for this struct was generated from the following file:
mc_trajectory::SequenceInterpolator::values
const TimedValueVector & values() const noexcept
Definition: SequenceInterpolator.h:100