6 #ifndef COGS_INTERPOLATOR_H 7 #define COGS_INTERPOLATOR_H 12 #include <Utils/ExtGLM.h> 13 #include <Utils/Ulog.h> 14 #include <glm/gtx/spline.hpp> 22 template<
typename Type>
23 [[nodiscard]]
inline Type Interpolate(
const Type val1,
const Type val2,
const float weight)
25 return glm::mix(val1, val2, weight);
28 [[nodiscard]]
inline glm::quat Interpolate(
const glm::quat val1,
const glm::quat val2,
const float weight)
30 return glm::slerp(val1, val2, weight);
33 template<
typename Type>
34 [[nodiscard]]
inline glm::quat InterpolateCubic(
const Type val1,
const Type val2,
const Type val3,
const Type val4,
const float weight)
36 return glm::cubic(val1, val2, val3, val4, weight);
45 template<
typename Type>
54 Interpolator(
const std::initializer_list<std::pair<float, Type>> &keys)
66 void ClearAndSet(
const std::initializer_list<std::pair<float, Type>> &keys)
68 keys_.reserve(keys.size());
69 for (
const auto &k : keys)
71 keys_.emplace_back(k.first, k.second);
76 [](
const Keyframe & a,
const Keyframe & b) ->
bool {
return a.time < b.time; }
96 keys_.insert(keys_.begin() + keybefore, Keyframe(time, value));
112 ulog::Fail(
"Can not interpolate to time " + std::to_string(time) +
" when no key frames available.",
"cogs::Interpolator::GetValueAtTime");
115 if (keys_.front().time >= time)
117 return keys_.front().value;
119 if (keys_.back().time <= time)
121 return keys_.back().value;
124 int32_t high = low + 1;
125 auto param = (time - keys_[low].time) / (keys_[high].time - keys_[low].time);
126 return Interpolate(keys_[low].value, keys_[high].value, param);
130 [[nodiscard]] Type GetValueAtTimeCubic(
const float time)
const 134 ulog::Fail(
"Can not interpolate to time " + std::to_string(time) +
" when no key frames available.",
"cogs::Interpolator::GetValueAtTimeCubic");
137 if (keys_.front().time >= time)
139 return keys_.front().value;
141 if (keys_.back().time <= time)
143 return keys_.back().value;
146 int32_t high = low + 1;
147 int32_t lower = low - 1;
148 int32_t higher = high + 1;
154 if (high == int32_t(keys_.size()) - 1)
156 higher = int32_t(keys_.size()) - 1;
159 auto param = (time - keys_[low].time) / (keys_[high].time - keys_[low].time);
161 return InterpolateCubic(keys_[lower].value, keys_[low].value, keys_[high].value, keys_[higher].value, param);
168 return keys_.at(key_index).value;
174 return keys_.at(key_index).time;
180 keys_.at(key_index).value = value;
186 return keys_.empty() ? 0.0f : keys_.back().time;
202 if (keys_.back().time <= time)
204 return static_cast<int32_t
>(keys_.size()) - 1;
207 int32_t high =
static_cast<int32_t
>(keys_.size()) - 1;
214 int32_t mid = (low + high) / 2;
215 if (time <= keys_[mid].time)
234 keys_.reserve(keyframe_count);
240 keys_.shrink_to_fit();
254 Keyframe(
float t, Type v) : value(v), time(t) {};
258 std::vector<Keyframe> keys_;
Namespace of "Computer Graphic Structures" library, which implement many core classes and structures...
Definition: AabbHierarchy.h:8
Type GetValueAtTime(const float time) const
Binary searches two keys nearest to specified time and interpolates between them. ...
Definition: Interpolator.h:108
void ShrinkToFit()
Releases allocated and not used memory for new frames.
Definition: Interpolator.h:238
bool IsEmpty() const
Returns true when no key exist.
Definition: Interpolator.h:81
Interpolator()
Creates an Action with no keys.
Definition: Interpolator.h:50
Wrapper class for key frame animation offering effective interpolation between values in time...
Definition: Interpolator.h:46
void ClearAndSet(const std::initializer_list< std::pair< float, Type >> &keys)
Deletes current key array and sets up new array from list of <time,value> pairs.
Definition: Interpolator.h:66
void Reserve(size_t keyframe_count)
Reserves required memory for specified amount of key frames. Improves speed of adding new key frames...
Definition: Interpolator.h:232
Type GetKeyValue(const size_t key_index) const
Returns value of key on specified index (position in array ordered by time).
Definition: Interpolator.h:166
float GetTimeValue(const size_t key_index) const
Returns time of key on specified index.
Definition: Interpolator.h:172
Interpolator(const std::initializer_list< std::pair< float, Type >> &keys)
Creates an Action from list of <time,value> pairs.
Definition: Interpolator.h:54
int32_t GetKeyBefore(const float time) const
Returns index of key at, or immediately before specified time.
Definition: Interpolator.h:196
size_t GetKeyCount() const
Returns number of key frames.
Definition: Interpolator.h:87
void Clear()
Deletes current all current key frames.
Definition: Interpolator.h:60
float GetLastKeyTime() const
Returns time of last existing key frame.
Definition: Interpolator.h:184
void InsertKey(const float time, Type value)
Creates new key frame with specified value and inserts it into array.
Definition: Interpolator.h:93
void SetKeyValue(const size_t key_index, const Type value)
Returns value of key on specified index (position in array ordered by time).
Definition: Interpolator.h:178