Cafu Engine
AnimExpr.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_MODEL_ANIM_EXPRESSION_HPP_INCLUDED
8 #define CAFU_MODEL_ANIM_EXPRESSION_HPP_INCLUDED
9 
10 #include "Math3D/Quaternion.hpp"
11 #include "Templates/Array.hpp"
12 #include "Templates/Pointer.hpp"
13 
14 
15 class AnimExpressionT;
16 class CafuModelT;
17 
19 
20 
21 /// Animation expressions describe the "skeleton pose" of a model.
22 ///
23 /// They are used as an input to AnimPoseT instances, which use the "skeleton pose" expressed by an anim
24 /// expression in order to derive the "full pose", including meshes and other features such as collision
25 /// detection.
26 /// In other words, AnimPoseT's refer to an AnimExpressionT for the "configuration" of its pose.
27 ///
28 /// AnimExpressionT's can be hierarchically composed, just like mathematical expressions,
29 /// which is in fact their strongest and most important feature.
30 /// They are also very easy and care-free to use and have very good performance, because when obtained
31 /// from an AnimExprPoolT, the pool minimizes both the number of instances as well as the penalties from
32 /// memory allocations and deletes.
34 {
35  public:
36 
37  /// The constructor.
38  /// (It's ok to have this in "public" instead of "protected": we have pure virtual methods as well.)
39  AnimExpressionT(const CafuModelT& Model);
40 
41  /// The (virtual) destructor.
42  virtual ~AnimExpressionT() { }
43 
44  /// Returns the model that this is an anim expression for.
45  const CafuModelT& GetModel() const { return m_Model; }
46 
47  /// Returns the number of IntrusivePtrT<>'s that currently refer to this anim expression.
48  /// This is especially useful for the implementation of "pools" of AnimExpressionT's,
49  /// so that the pool implementation can learn if this instance is available for being re-used.
50  unsigned int GetRefCount() const { return m_RefCount; }
51 
52  /// For the joint with the given JointNr, this function returns
53  /// - the joint weight,
54  /// - the joints position, quaternion and scale values.
55  virtual void GetData(unsigned int JointNr, float& Weight, Vector3fT& Pos, cf::math::QuaternionfT& Quat, Vector3fT& Scale) const=0;
56 
57  /// Advances the time for this anim expression, that is, frame numbers of
58  /// underlying animation sequences, cross-fades, etc.
59  /// Returns `true` if the end of an underlying animation sequence was reached
60  /// (or in case of a looping sequence, if the sequence was wrapped).
61  virtual bool AdvanceTime(float Time) { return false; }
62 
63  /// The virtual copy constructor.
64  /// Creates a new anim expression that is an exact copy of this, even when called
65  /// via the base class pointer (the caller doesn't need to know the exact derived class).
66  virtual AnimExpressionPtrT Clone() const=0;
67 
68  /// Returns whether this anim expression is equal to \c A.
69  /// Two anim expressions are equal if their GetData() methods return the same data.
70  virtual bool IsEqual(const AnimExpressionPtrT& AE) const=0;
71 
72 
73  private:
74 
75  template<class T> friend class IntrusivePtrT;
76 
77  AnimExpressionT(const AnimExpressionT&); ///< Use of the Copy Constructor is not allowed.
78  void operator = (const AnimExpressionT&); ///< Use of the Assignment Operator is not allowed.
79 
80  const CafuModelT& m_Model; ///< The related model that this is an anim expression for.
81  unsigned int m_RefCount; ///< How many IntrusivePtrT<>'s currently refer to this anim expression?
82 };
83 
84 
85 /// This class implements the "standard" skeleton pose based on a sequence number and frame number.
86 ///
87 /// Note that for backwards-compatibility reasons, this expression never progresses into "other" sequences.
88 /// That is, for any Model.GetAnims()[SequNr].Next, only the values -1 and SequNr are supported; any other
89 /// non-negative value is treated as if it was SequNr.
90 /// This restriction is in place because we have a lot of non-trivial user code that relies on obtaining
91 /// the same value from GetSequNr() that was last set with SetSequNr().
92 /// If in the future any user code requires following the "Next" sequences properly, a new AnimExpressionT-
93 /// derived class is required that fully implements this.
95 {
96  public:
97 
98  /// Create a new "standard" expression from the given parameters.
99  /// The "force loop" setting for the newly created expression is \c false.
100  AnimExprStandardT(const CafuModelT& Model, int SequNr, float FrameNr);
101 
102  // Implementations and overrides for base class methods.
103  virtual void GetData(unsigned int JointNr, float& Weight, Vector3fT& Pos, cf::math::QuaternionfT& Quat, Vector3fT& Scale) const;
104  virtual bool AdvanceTime(float Time);
105  virtual AnimExpressionPtrT Clone() const; // Unfortunately, the proper covariant return type cannot be used with smart pointers.
106  virtual bool IsEqual(const AnimExpressionPtrT& AE) const;
107 
108  /// Returns the sequence number that is currently set in this expression.
109  int GetSequNr() const { return m_SequNr; }
110 
111  /// Sets the number of the animation sequence that is used by this expression.
112  /// @param SequNr The number of the animation sequence to use, -1 for the bind pose.
113  void SetSequNr(int SequNr);
114 
115  /// Returns the frame number that is currently set in this expression.
116  float GetFrameNr() const { return m_FrameNr; }
117 
118  /// Sets the frame number in the current animation sequence.
119  /// @param FrameNr The frame number in the animation sequence to use.
120  void SetFrameNr(float FrameNr);
121 
122  /// Returns if this expression will override the "next" setting at the end of the current animation sequence.
123  bool GetForceLoop() const { return m_ForceLoop; }
124 
125  /// Sets whether this expression should override the "next" setting at the end of the current animation sequence.
126  /// @param ForceLoop Override the "next" setting at the end of the current animation sequence?
127  void SetForceLoop(bool ForceLoop);
128 
129 
130  private:
131 
132  void NormalizeInput();
133 
134  int m_SequNr; ///< The animation sequence number.
135  float m_FrameNr; ///< The frame number in the sequence.
136  bool m_ForceLoop; ///< Override the "next" setting at the end of the current animation sequence?
137 };
138 
139 
140 /// Filters the result of another expression by a "channel".
142 {
143  public:
144 
145  AnimExprFilterT(const CafuModelT& Model, AnimExpressionPtrT SubExpr, unsigned int ChannelNr);
146  AnimExprFilterT(const CafuModelT& Model, AnimExpressionPtrT SubExpr, const std::string& ChannelName);
147 
148  // Implementations and overrides for base class methods.
149  virtual void GetData(unsigned int JointNr, float& Weight, Vector3fT& Pos, cf::math::QuaternionfT& Quat, Vector3fT& Scale) const;
150  virtual bool AdvanceTime(float Time) { return m_SubExpr->AdvanceTime(Time); }
151  virtual AnimExpressionPtrT Clone() const; // Unfortunately, the proper covariant return type cannot be used with smart pointers.
152  virtual bool IsEqual(const AnimExpressionPtrT& AE) const;
153 
154  /// Re-initializes this anim expression, so that it can be re-used with different parameters (on the same model).
155  void ReInit(AnimExpressionPtrT SubExpr, unsigned int ChannelNr);
156 
157 
158  private:
159 
160  AnimExpressionPtrT m_SubExpr;
161  unsigned int m_ChannelNr;
162 };
163 
164 
166 {
167  public:
168 
170 
171  // Implementations and overrides for base class methods.
172  virtual void GetData(unsigned int JointNr, float& Weight, Vector3fT& Pos, cf::math::QuaternionfT& Quat, Vector3fT& Scale) const;
173  virtual bool AdvanceTime(float Time);
174  virtual AnimExpressionPtrT Clone() const; // Unfortunately, the proper covariant return type cannot be used with smart pointers.
175  virtual bool IsEqual(const AnimExpressionPtrT& AE) const;
176 
177  /// Re-initializes this anim expression, so that it can be re-used with different parameters (on the same model).
179 
180 
181  private:
182 
183  AnimExpressionPtrT m_A;
184  AnimExpressionPtrT m_B;
185 };
186 
187 
189 {
190  public:
191 
192  AnimExprBlendT(const CafuModelT& Model, AnimExpressionPtrT A, AnimExpressionPtrT B, float Duration);
193 
194  // Implementations and overrides for base class methods.
195  virtual void GetData(unsigned int JointNr, float& Weight, Vector3fT& Pos, cf::math::QuaternionfT& Quat, Vector3fT& Scale) const;
196  virtual bool AdvanceTime(float Time);
197  virtual AnimExpressionPtrT Clone() const; // Unfortunately, the proper covariant return type cannot be used with smart pointers.
198  virtual bool IsEqual(const AnimExpressionPtrT& AE) const;
199 
200  /// Re-initializes this anim expression, so that it can be re-used with different parameters (on the same model).
201  /// Note that resetting \c A, \c B or \c Duration individually is not possible, because the implementation
202  /// may prune and drop \c A when the blend is complete.
203  void ReInit(AnimExpressionPtrT A, AnimExpressionPtrT B, float Duration);
204 
205  /// Returns the "blend from" sub-expression.
206  AnimExpressionPtrT GetA() const { return m_A; }
207 
208  /// Returns the "blend to" sub-expression.
209  AnimExpressionPtrT GetB() const { return m_B; }
210 
211  /// Returns how far the blend has advanced.
212  float GetFrac() const { return m_Frac; }
213 
214 
215  private:
216 
217  AnimExpressionPtrT m_A;
218  AnimExpressionPtrT m_B;
219  float m_Duration;
220  float m_Frac;
221 };
222 
223 
225 {
226  public:
227 
228  AnimExprPoolT(const CafuModelT& Model) : m_Model(Model) { }
229 
230  // These methods mimic the ctors of the anim expression classes.
231  IntrusivePtrT<AnimExprStandardT> GetStandard(int SequNr, float FrameNr);
232  IntrusivePtrT<AnimExprFilterT> GetFilter(AnimExpressionPtrT SubExpr, unsigned int ChannelNr);
233  IntrusivePtrT<AnimExprFilterT> GetFilter(AnimExpressionPtrT SubExpr, const std::string& ChannelName);
236 
237 
238  private:
239 
240  /// This function makes sure that anim expressions that are unused don't keep pointers
241  /// to subexpressions, such that the subexpressions are available as unused as well.
242  void FlattenUnused();
243 
244  const CafuModelT& m_Model; ///< The related model that this is an anim expression pool for.
249 };
250 
251 #endif
virtual AnimExpressionPtrT Clone() const
The virtual copy constructor.
Definition: AnimExpr.cpp:253
virtual bool IsEqual(const AnimExpressionPtrT &AE) const
Returns whether this anim expression is equal to A.
Definition: AnimExpr.cpp:259
Definition: AnimExpr.hpp:188
virtual AnimExpressionPtrT Clone() const =0
The virtual copy constructor.
virtual void GetData(unsigned int JointNr, float &Weight, Vector3fT &Pos, cf::math::QuaternionfT &Quat, Vector3fT &Scale) const
For the joint with the given JointNr, this function returns.
Definition: AnimExpr.cpp:242
AnimExpressionPtrT GetB() const
Returns the "blend to" sub-expression.
Definition: AnimExpr.hpp:209
virtual bool IsEqual(const AnimExpressionPtrT &AE) const
Returns whether this anim expression is equal to A.
Definition: AnimExpr.cpp:151
This class represents a native Cafu model.
Definition: Model_cmdl.hpp:45
Filters the result of another expression by a "channel".
Definition: AnimExpr.hpp:141
virtual bool IsEqual(const AnimExpressionPtrT &AE) const =0
Returns whether this anim expression is equal to A.
This class implements the "standard" skeleton pose based on a sequence number and frame number...
Definition: AnimExpr.hpp:94
virtual void GetData(unsigned int JointNr, float &Weight, Vector3fT &Pos, cf::math::QuaternionfT &Quat, Vector3fT &Scale) const
For the joint with the given JointNr, this function returns.
Definition: AnimExpr.cpp:289
void ReInit(AnimExpressionPtrT A, AnimExpressionPtrT B, float Duration)
Re-initializes this anim expression, so that it can be re-used with different parameters (on the same...
Definition: AnimExpr.cpp:349
bool GetForceLoop() const
Returns if this expression will override the "next" setting at the end of the current animation seque...
Definition: AnimExpr.hpp:123
AnimExpressionT(const CafuModelT &Model)
The constructor.
Definition: AnimExpr.cpp:11
Definition: AnimExpr.hpp:165
virtual bool AdvanceTime(float Time)
Advances the time for this anim expression, that is, frame numbers of underlying animation sequences...
Definition: AnimExpr.cpp:97
virtual void GetData(unsigned int JointNr, float &Weight, Vector3fT &Pos, cf::math::QuaternionfT &Quat, Vector3fT &Scale) const
For the joint with the given JointNr, this function returns.
Definition: AnimExpr.cpp:32
AnimExpressionPtrT GetA() const
Returns the "blend from" sub-expression.
Definition: AnimExpr.hpp:206
virtual ~AnimExpressionT()
The (virtual) destructor.
Definition: AnimExpr.hpp:42
virtual bool IsEqual(const AnimExpressionPtrT &AE) const
Returns whether this anim expression is equal to A.
Definition: AnimExpr.cpp:326
AnimExprStandardT(const CafuModelT &Model, int SequNr, float FrameNr)
Create a new "standard" expression from the given parameters.
Definition: AnimExpr.cpp:22
float GetFrac() const
Returns how far the blend has advanced.
Definition: AnimExpr.hpp:212
float GetFrameNr() const
Returns the frame number that is currently set in this expression.
Definition: AnimExpr.hpp:116
void ReInit(AnimExpressionPtrT SubExpr, unsigned int ChannelNr)
Re-initializes this anim expression, so that it can be re-used with different parameters (on the same...
Definition: AnimExpr.cpp:233
Definition: AnimExpr.hpp:224
virtual bool AdvanceTime(float Time)
Advances the time for this anim expression, that is, frame numbers of underlying animation sequences...
Definition: AnimExpr.cpp:311
virtual bool AdvanceTime(float Time)
Advances the time for this anim expression, that is, frame numbers of underlying animation sequences...
Definition: AnimExpr.hpp:150
virtual bool IsEqual(const AnimExpressionPtrT &AE) const
Returns whether this anim expression is equal to A.
Definition: AnimExpr.cpp:429
virtual bool AdvanceTime(float Time)
Advances the time for this anim expression, that is, frame numbers of underlying animation sequences...
Definition: AnimExpr.hpp:61
int GetSequNr() const
Returns the sequence number that is currently set in this expression.
Definition: AnimExpr.hpp:109
Animation expressions describe the "skeleton pose" of a model.
Definition: AnimExpr.hpp:33
void SetSequNr(int SequNr)
Sets the number of the animation sequence that is used by this expression.
Definition: AnimExpr.cpp:160
void SetForceLoop(bool ForceLoop)
Sets whether this expression should override the "next" setting at the end of the current animation s...
Definition: AnimExpr.cpp:178
virtual AnimExpressionPtrT Clone() const
The virtual copy constructor.
Definition: AnimExpr.cpp:419
void ReInit(AnimExpressionPtrT A, AnimExpressionPtrT B)
Re-initializes this anim expression, so that it can be re-used with different parameters (on the same...
Definition: AnimExpr.cpp:280
virtual void GetData(unsigned int JointNr, float &Weight, Vector3fT &Pos, cf::math::QuaternionfT &Quat, Vector3fT &Scale) const
For the joint with the given JointNr, this function returns.
Definition: AnimExpr.cpp:358
IntrusivePtrT & operator=(const IntrusivePtrT &IP)
The assignment operator.
Definition: Pointer.hpp:79
virtual void GetData(unsigned int JointNr, float &Weight, Vector3fT &Pos, cf::math::QuaternionfT &Quat, Vector3fT &Scale) const =0
For the joint with the given JointNr, this function returns.
virtual AnimExpressionPtrT Clone() const
The virtual copy constructor.
Definition: AnimExpr.cpp:142
const CafuModelT & GetModel() const
Returns the model that this is an anim expression for.
Definition: AnimExpr.hpp:45
virtual AnimExpressionPtrT Clone() const
The virtual copy constructor.
Definition: AnimExpr.cpp:320
unsigned int GetRefCount() const
Returns the number of IntrusivePtrT<>'s that currently refer to this anim expression.
Definition: AnimExpr.hpp:50
virtual bool AdvanceTime(float Time)
Advances the time for this anim expression, that is, frame numbers of underlying animation sequences...
Definition: AnimExpr.cpp:390
Definition: Renderer.hpp:16
void SetFrameNr(float FrameNr)
Sets the frame number in the current animation sequence.
Definition: AnimExpr.cpp:169