COGS
Computer Graphics Structures library
Interpolator.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef COGS_INTERPOLATOR_H
7 #define COGS_INTERPOLATOR_H
8 #include <vector>
9 #include <algorithm>
10 #include <stdexcept>
11 
12 #include <Utils/ExtGLM.h>
13 #include <Utils/Ulog.h>
14 #include <glm/gtx/spline.hpp>
15 
16 #include <COGS/API.h>
17 
18 
19 namespace cogs
20 {
21 
22  template<typename Type>
23  [[nodiscard]] inline Type Interpolate(const Type val1, const Type val2, const float weight)
24  {
25  return glm::mix(val1, val2, weight);
26  }
27 
28  [[nodiscard]] inline glm::quat Interpolate(const glm::quat val1, const glm::quat val2, const float weight)
29  {
30  return glm::slerp(val1, val2, weight);
31  }
32 
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)
35  {
36  return glm::cubic(val1, val2, val3, val4, weight);
37  }
38 
39 
45  template<typename Type>
47  {
48  public:
50  explicit Interpolator()
51  {}
52 
54  Interpolator(const std::initializer_list<std::pair<float, Type>> &keys)
55  {
56  ClearAndSet(keys);
57  }
58 
60  void Clear()
61  {
62  keys_.clear();
63  }
64 
66  void ClearAndSet(const std::initializer_list<std::pair<float, Type>> &keys)
67  {
68  keys_.reserve(keys.size());
69  for (const auto &k : keys)
70  {
71  keys_.emplace_back(k.first, k.second);
72  }
73  std::sort(
74  keys_.begin(),
75  keys_.end(),
76  [](const Keyframe & a, const Keyframe & b) -> bool { return a.time < b.time; }
77  );
78  }
79 
81  [[nodiscard]] bool IsEmpty() const
82  {
83  return keys_.empty();
84  }
85 
87  [[nodiscard]] size_t GetKeyCount() const
88  {
89  return keys_.size();
90  }
91 
93  void InsertKey(const float time, Type value)
94  {
95  auto keybefore = GetKeyBefore(time) + 1;
96  keys_.insert(keys_.begin() + keybefore, Keyframe(time, value));
97  }
98 
108  [[nodiscard]] Type GetValueAtTime(const float time) const
109  {
110  if (keys_.empty())
111  {
112  ulog::Fail("Can not interpolate to time " + std::to_string(time) + " when no key frames available.", "cogs::Interpolator::GetValueAtTime");
113  return {};
114  }
115  if (keys_.front().time >= time)
116  {
117  return keys_.front().value;
118  }
119  if (keys_.back().time <= time)
120  {
121  return keys_.back().value;
122  }
123  int32_t low = GetKeyBefore(time);
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);
127  }
128 
129 
130  [[nodiscard]] Type GetValueAtTimeCubic(const float time) const
131  {
132  if (keys_.empty())
133  {
134  ulog::Fail("Can not interpolate to time " + std::to_string(time) + " when no key frames available.", "cogs::Interpolator::GetValueAtTimeCubic");
135  return {};
136  }
137  if (keys_.front().time >= time)
138  {
139  return keys_.front().value;
140  }
141  if (keys_.back().time <= time)
142  {
143  return keys_.back().value;
144  }
145  int32_t low = GetKeyBefore(time);
146  int32_t high = low + 1;
147  int32_t lower = low - 1;
148  int32_t higher = high + 1;
149 
150  if (low == 0)
151  {
152  lower = 0;
153  }
154  if (high == int32_t(keys_.size()) - 1)
155  {
156  higher = int32_t(keys_.size()) - 1;
157  }
158 
159  auto param = (time - keys_[low].time) / (keys_[high].time - keys_[low].time);
160 
161  return InterpolateCubic(keys_[lower].value, keys_[low].value, keys_[high].value, keys_[higher].value, param);
162 
163  }
164 
166  [[nodiscard]] Type GetKeyValue(const size_t key_index) const
167  {
168  return keys_.at(key_index).value;
169  }
170 
172  [[nodiscard]] float GetTimeValue(const size_t key_index) const
173  {
174  return keys_.at(key_index).time;
175  }
176 
178  void SetKeyValue(const size_t key_index, const Type value)
179  {
180  keys_.at(key_index).value = value;
181  }
182 
184  [[nodiscard]] float GetLastKeyTime() const
185  {
186  return keys_.empty() ? 0.0f : keys_.back().time;
187  }
188 
196  [[nodiscard]] int32_t GetKeyBefore(const float time) const
197  {
198  if (keys_.empty())
199  {
200  return -1;
201  }
202  if (keys_.back().time <= time)
203  {
204  return static_cast<int32_t>(keys_.size()) - 1;
205  }
206  int32_t low = 0;
207  int32_t high = static_cast<int32_t>(keys_.size()) - 1;
208  while (low < high)
209  {
210  if (low + 1 == high)
211  {
212  return low;
213  }
214  int32_t mid = (low + high) / 2;
215  if (time <= keys_[mid].time)
216  {
217  high = mid;
218  }
219  else
220  {
221  low = mid;
222  }
223  }
224  return -1;
225  }
226 
232  void Reserve(size_t keyframe_count)
233  {
234  keys_.reserve(keyframe_count);
235  }
236 
238  void ShrinkToFit()
239  {
240  keys_.shrink_to_fit();
241  }
242 
243  void RemoveLastKey()
244  {
245  if (!IsEmpty())
246  {
247  keys_.pop_back();
248  }
249  }
250 
251  private:
252  struct Keyframe
253  {
254  Keyframe(float t, Type v) : value(v), time(t) {};
255  Type value;
256  float time;
257  };
258  std::vector<Keyframe> keys_;
259  };
260 
261 }
262 
263 #endif /* !COGS_INTERPOLATOR_H */
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