Field3D
Curve.h File Reference

Contains the Curve class which is used to interpolate attributes in time. More...

#include <algorithm>
#include <utility>
#include <vector>
#include <boost/lexical_cast.hpp>
#include <OpenEXR/OpenEXRConfig.h>
#include <OpenEXR/ImathFun.h>
#include <OpenEXR/ImathMatrix.h>
#include "ns.h"

Go to the source code of this file.

Classes

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. More...
 
struct  Curve< T >::CheckTEqual
 Used when finding values in the m_samples vector. More...
 
struct  Curve< T >::CheckTGreaterThan
 Used when finding values in the m_samples vector. More...
 

Macros

#define COMBINED_OPENEXR_VERSION
 

Detailed Description

Contains the Curve class which is used to interpolate attributes in time.

Definition in file Curve.h.

Macro Definition Documentation

◆ COMBINED_OPENEXR_VERSION

#define COMBINED_OPENEXR_VERSION
Value:
((10000*OPENEXR_VERSION_MAJOR) + \
#define FIELD3D_MTX_T
Definition StdMathLib.h:99

Definition at line 59 of file Curve.h.

95{
96public:
97
98 // Typedefs ------------------------------------------------------------------
99
100 typedef std::pair<float, T> Sample;
101 typedef std::vector<Sample> SampleVec;
102
103 // Main methods --------------------------------------------------------------
104
108 void addSample(const float t, const T &value);
109
112 T linear(const float t) const;
113
115 size_t numSamples() const
116 { return m_samples.size(); }
117
119 const SampleVec& samples() const
120 { return m_samples; }
121
123 void clear()
124 { SampleVec().swap(m_samples); }
125
126private:
127
128 // Structs -------------------------------------------------------------------
129
131 struct CheckTGreaterThan :
132 public std::unary_function<std::pair<float, T>, bool>
133 {
134 CheckTGreaterThan(float match)
135 : m_match(match)
136 { }
137 bool operator()(std::pair<float, T> test)
138 {
139 return test.first > m_match;
140 }
141 private:
142 float m_match;
143 };
144
146 struct CheckTEqual :
147 public std::unary_function<std::pair<float, T>, bool>
148 {
149 CheckTEqual(float match)
150 : m_match(match)
151 { }
152 bool operator()(std::pair<float, T> test)
153 {
154 return test.first == m_match;
155 }
156 private:
157 float m_match;
158 };
159
160 // Utility methods -----------------------------------------------------------
161
166 T defaultReturnValue() const
167 { return T(0); }
168
172 T lerp(const Sample &lower, const Sample &upper, const float t) const
173 { return Imath::lerp(lower.second, upper.second, t); }
174
175 // Private data members ------------------------------------------------------
176
179 SampleVec m_samples;
180
181};
182
183//----------------------------------------------------------------------------//
184// Template implementations
185//----------------------------------------------------------------------------//
186
187template <typename T>
188void Curve<T>::addSample(const float t, const T &value)
189{
190 using namespace std;
191 // Check that sample time is not already in curve
192 typename SampleVec::iterator i =
193 find_if(m_samples.begin(), m_samples.end(), CheckTEqual(t));
194 if (i != m_samples.end()) {
195 // Sample position already exists, so we replace it
196 i->second = value;
197 return;
198 }
199 // Find the first sample location that is greater than the interpolation
200 // position
201 i = find_if(m_samples.begin(), m_samples.end(), CheckTGreaterThan(t));
202 // If we get something other than end() back then we insert the new
203 // sample before that. If there wasn't a larger value we add this sample
204 // to the end of the vector.
205 if (i != m_samples.end()) {
206 m_samples.insert(i, make_pair(t, value));
207 } else {
208 m_samples.push_back(make_pair(t, value));
209 }
210}
211
212//----------------------------------------------------------------------------//
213
214template <typename T>
215T Curve<T>::linear(const float t) const
216{
217 using namespace std;
218 // If there are no samples, return zero
219 if (m_samples.size() == 0) {
220 return defaultReturnValue();
221 }
222 // Find the first sample location that is greater than the interpolation
223 // position
224 typename SampleVec::const_iterator i =
225 find_if(m_samples.begin(), m_samples.end(), CheckTGreaterThan(t));
226 // If we get end() back then there was no sample larger, so we return the
227 // last value. If we got the first value then there is only one value and
228 // we return that.
229 if (i == m_samples.end()) {
230 return m_samples.back().second;
231 } else if (i == m_samples.begin()) {
232 return m_samples.front().second;
233 }
234 // Interpolate between the nearest two samples.
235 const Sample &upper = *i;
236 const Sample &lower = *(--i);
237 const float interpT = Imath::lerpfactor(t, lower.first, upper.first);
238 return lerp(lower, upper, interpT);
239}
240
241//----------------------------------------------------------------------------//
242// Template specializations
243//----------------------------------------------------------------------------//
244
245template <>
246inline Imath::Matrix44<float>
247Curve<Imath::Matrix44<float> >::defaultReturnValue() const
248{
249 Imath::Matrix44<float> identity;
250 identity.makeIdentity();
251 return identity;
252}
253
254//----------------------------------------------------------------------------//
255
256template <>
257inline Imath::Matrix44<double>
258Curve<Imath::Matrix44<double> >::defaultReturnValue() const
259{
260 Imath::Matrix44<double> identity;
261 identity.makeIdentity();
262 return identity;
263}
264
265//----------------------------------------------------------------------------//
266
268
269//----------------------------------------------------------------------------//
270
271#endif // Include guard
bool match(const std::string &name, const std::string &attribute, const std::vector< std::string > &patterns, const MatchFlags flags=MatchEmptyPattern)
Matches a <name>:<attribute> string against a set of patterns.
Implements a simple function curve where samples of type T can be added along a 1D axis....
Definition Curve.h:96
void addSample(const float t, const T &value)
Adds a sample point to the curve.
Definition Curve.h:189
T linear(const float t) const
Linearly interpolates a value from the curve.
Definition Curve.h:216
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition ns.h:58