AnimEngine
KeyframeAnimation.hpp
1 #pragma once
2 #ifndef KJY_ANIM_ENGINE_COMPONENTS_POSE_KEYFRAME_ANIMATION_HPP_
3 #define KJY_ANIM_ENGINE_COMPONENTS_POSE_KEYFRAME_ANIMATION_HPP_
4 #include <map>
5 #include <stdint.h>
6 #include <exception>
7 #include <util/common.h>
8 #include "Pose.hpp"
9 #include "AttachableComponent.hpp"
10 #include <util/UUID.hpp>
11 #include <vector>
12 
13 /* Time as milliseconds from start of animation */
14 typedef size_t key_time_t;
15 
16 key_time_t seconds_to_key_time(double seconds);
17 double key_time_to_seconds(key_time_t time);
18 
19 /* Based on gltf 2.0 standard */
20 enum class InterpolationType{interp_step, interp_linear, interp_cubicspline};
21 
22 template<typename T>
25  KeyframeTemplate<T>(const T& t) : mKeyValue(t) {}
26  KeyframeTemplate<T>(const T& t, const T& aInTangent, const T& aOutTangent) : mKeyValue(t), mInTangent(aInTangent), mOutTangent(aOutTangent) {}
27  using property_t = T;
28 
29  T mKeyValue;
30  T mInTangent, mOutTangent; // For cubic interpolation
31  InterpolationType mInterpolationType = InterpolationType::interp_linear;
32 
33  virtual const T& getKeyValue() const {return(mKeyValue);}
34  virtual void setKeyValue(const T& value){mKeyValue = value;}
35 
36  T interpolate_with_other(const KeyframeTemplate& other, double parameter) const = delete;
37 };
38 
39 class ChannelNoKeyframeException : public std::exception{
40  public:
41  ChannelNoKeyframeException(const std::string& find_hint, size_t time_ms) : _whatstr("No keyframe exists to be returned when calling " + find_hint + "with timecode " + std::to_string(time_ms)){}
42  const char* what() const noexcept {
43  return(_whatstr.c_str());
44  }
45 
46  private:
47  const std::string _whatstr;
48 };
49 
50 template<typename keyframe_t>
51 class KeyedPropertyChannelTemplate : public std::map<key_time_t, keyframe_t>
52 {
53  public:
54  using property_t = typename keyframe_t::property_t;
55 
56  inline const keyframe_t& at_index(size_t index) const {
57  assert(this->size() > index);
58  return(std::next(this->begin(), index)->second);
59  }
60  inline keyframe_t& at_index(size_t index) {
61  assert(this->size() > index);
62  return(std::next(this->begin(), index)->second);
63  }
64 
65  inline key_time_t getLength() const {return(this->size() == 0 ? 0 : this->rbegin()->first);}
66  std::pair<key_time_t, const keyframe_t&> getNearestKey(key_time_t time_ms) const;
67  std::pair<key_time_t, const keyframe_t&> getNearestKeyBefore(key_time_t time_ms) const;
68  std::pair<key_time_t, const keyframe_t&> getNearestKeyAfter(key_time_t time_ms) const;
69 
70  property_t sample(key_time_t time_ms) const;
71 
72  private:
73  property_t _mFallbackValue;
74 };
75 
76 
77 struct Vec3Keyframe : public KeyframeTemplate<glm::vec3>{
79  using keyframe_template::keyframe_template;
80  using property_t = keyframe_template::property_t; // aka vec3
81 
82  property_t interpolate_with_other(const Vec3Keyframe& other, double parameter) const;
83 };
84 
85 struct QuatKeyframe : public KeyframeTemplate<glm::quat>{
87  using keyframe_template::keyframe_template;
88  using property_t = keyframe_template::property_t; // aka quat
89 
90  property_t interpolate_with_other(const QuatKeyframe& other, double parameter) const;
91 };
92 
93 template<typename P>
95  using property_t = P;
96  virtual key_time_t getLength() const = 0;
97  virtual property_t sample(key_time_t time_ms) const = 0;
98  virtual void sampleInPlace(key_time_t time, P& target_prop) const = 0;
99 };
100 
101 template<typename P, typename K>
103  using property_t = P;
104  using key_t = K;
105  virtual key_time_t getLength() const = 0;
106  virtual property_t sample(K key, key_time_t time_ms) const = 0;
107  virtual void sampleInPlace(K key, key_time_t time, P& target_prop) const = 0;
108 };
109 
110 /* Defines a collection of Keyframe sequences. Each sequence in association with
111  * a key of type K which should indicate something about the target of this
112  * sequence of keyframes */
113 template<typename Sequence, typename K>
114 class AnimationSequenceCollection : public std::map<K, Sequence>{
115  public:
116  using inherited_map = std::map<K, Sequence>;
117  using inherited_map::inherited_map;
118  using sequence_selection = typename inherited_map::iterator;
119  using const_sequence_selection = typename inherited_map::const_iterator;
120  key_time_t getLength() const{
121  key_time_t max = 0;
122  for(auto pair_iter = this->begin(); pair_iter != this->end(); pair_iter++){
123  key_time_t seq_len = pair_iter->second.getLength();
124  max = max < seq_len ? seq_len : max;
125  }
126  return(max);
127  }
128 };
129 
130 class Animator : virtual public AttachableComponent{
131  public:
132  virtual key_time_t getCurrentTime() const = 0;
133  virtual void setCurrentTime(key_time_t time) = 0;
134  virtual std::string getAnimatorName() const = 0;
135  virtual void setAnimatorName(const std::string& name) = 0;
136  virtual bool isEnabled() const = 0;
137  virtual void enable() = 0;
138  virtual void disable() = 0;
139  virtual bool willLoop() const = 0;
140  virtual void enableLoop() = 0;
141  virtual void disableLoop() = 0;
142  /* Override the time index at which the animation loops. */
143  virtual void overrideLoopTime(key_time_t time) = 0;
144  /* Reset loop time to the default (The length of the animation) */
145  virtual void resetLoopTime() = 0;
146 
147  /* Return the length in ms of the animator's animation (or the active animation if multiple are managed) */
148  virtual key_time_t getLength() const = 0;
149 };
150 
151 typedef std::vector<Animator*> AnimatorCollection;
152 
154  protected:
159 
160  /* Insert keyframe for all pose properties at `time` */
161  void keyPose(key_time_t time, const Pose& pose);
162 
163  inline key_time_t getLength() const {return(MAX(translation.getLength(), MAX(scale.getLength(), orientation.getLength())));}
164  };
165 
166  public:
168  PoseKeyframeSequence(const Pose& initial_pose) : _underlying_pose_(initial_pose) {}
169 
170 
171  std::string name;
172  PoseKeyframeChannels keyframes;
173 
174  inline key_time_t getLength() const {return(keyframes.getLength());}
175  inline void setUnderlyingPose(const Pose& pose) {_underlying_pose_ = pose;}
176  Pose sample(key_time_t time_ms) const;
177  void sampleInPlace(key_time_t time_ms, Pose& target) const;
178  Pose sample(key_time_t time_ms, const Pose& initial_state) const;
179 
180  private:
181  Pose _underlying_pose_;
182 };
183 
185  public:
186  /* Collection of pose keyframe sequences' that map to a specific entity UUID */
188  SequenceCollection animations;
189 
191  PoseKeyframeAnimator(const Pose& fallback) : _fallback_pose_(fallback){}
192 
193  virtual key_time_t getCurrentTime() const override;
194  virtual void setCurrentTime(key_time_t time) override;
195  virtual std::string getAnimatorName() const override;
196  virtual void setAnimatorName(const std::string& name) override;
197  virtual void setFallbackPose(const Pose& fallback) {_fallback_pose_ = fallback;}
198  virtual key_time_t getLength() const override ;
199 
200  virtual bool isEnabled() const override {return(_enabled);}
201  virtual void enable() override {_enabled = true;}
202  virtual void disable() override {_enabled = false;}
203 
204  virtual bool willLoop() const override {return(_loops);}
205  virtual void enableLoop() override {_loops = true;};
206  virtual void disableLoop() override {_loops = false;}
207  /* Override the time index at which the animation loops. A time value of 0 resets to the default */
208  virtual void overrideLoopTime(key_time_t time) override {_loop_time = time;}
209  /* Reset loop time to the default (The length of the animation) */
210  virtual void resetLoopTime() override {_loop_time = 0;}
211 
212  protected:
213  std::string _animation_name;
214  key_time_t _current_time;
215 
216  bool _enabled = true;
217  bool _loops = true;
218  key_time_t _loop_time = 0;
219 
220 
221  private:
222  Pose _fallback_pose_;
223 };
224 
225 
226 #endif
Definition: KeyframeAnimation.hpp:51
Definition: KeyframeAnimation.hpp:39
Definition: KeyframeAnimation.hpp:114
Definition: KeyframeAnimation.hpp:77
Definition: KeyframeAnimation.hpp:23
Definition: KeyframeAnimation.hpp:155
Definition: KeyframeAnimation.hpp:94
Definition: KeyframeAnimation.hpp:102
Definition: AttachableComponent.hpp:5
Definition: KeyframeAnimation.hpp:184
Definition: KeyframeAnimation.hpp:85
Definition: Pose.hpp:11
Definition: KeyframeAnimation.hpp:153
Definition: KeyframeAnimation.hpp:130