Atoms Crowd  7.0.0
AnimationClip.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <Atoms/Globals.h>
11 #include <AtomsCore/AtomsMath.h>
12 #include <Atoms/AgentType.h>
13 #include <AtomsCore/Metadata/MetadataFactory.h>
14 #include <AtomsCore/Metadata/TypedArrayMetadata.h>
15 #include <AtomsCore/Metadata/ArrayMetadata.h>
16 #include <AtomsCore/Metadata/MapMetadata.h>
17 #include <AtomsCore/Metadata/Vector2ArrayMetadata.h>
18 #include <AtomsCore/Metadata/Vector3ArrayMetadata.h>
19 #include <AtomsCore/Skeleton.h>
20 #include <vector>
21 #include <map>
22 
23 #define DOUBLE_FOOT_DOWN_TRESHOLD 0.95
24 
25 namespace Atoms
26 {
27  class AnimationClip;
28  class JointClipData;
29 
30  typedef AtomsPtr<AnimationClip> AnimationClipPtr;
31 
32  typedef AtomsPtr<const AnimationClip> AnimationClipCPtr;
33 
35 
37  class ATOMS_EXPORT JointClipData
38  {
39 
40  public:
41 
43 
47  JointClipData(const unsigned int numberOfFrames);
48 
51 
53 
56  inline const std::string& name() const;
57  //inline const char* name() const;
58 
60 
63  inline void setName(const std::string& name);
64 
66 
69  unsigned int getNumberOfFrames() { return m_numberOfFrames; };
70 
72  void setNumberOfFrames(const int value);
73 
75  inline void setTranslation(const AtomsCore::Vector3 &translation, const unsigned int frame);
76 
78  inline void setRotation(const AtomsCore::Quaternion &rotation, const unsigned int frame);
79 
81  inline void setScale(const AtomsCore::Vector3 &scale, const unsigned int frame);
82 
84  const inline AtomsCore::Vector3& getTranslation(const unsigned int frame) const;
85 
87  const inline AtomsCore::Quaternion& getRotation(const unsigned int frame) const;
88 
90  const inline AtomsCore::Vector3& getScale(const unsigned int frame) const;
91 
93  AtomsCore::Vector3 getBlendedTranslation(const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
94 
96  AtomsCore::Quaternion getBlendedRotation(const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
97 
99  AtomsCore::Vector3 getBlendedScale(const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
100 
102  void getBlendedTranslationInPlace(AtomsCore::Vector3& out, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
103 
105  void getBlendedRotationInPlace(AtomsCore::Quaternion& out, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
106 
108  void getBlendedScaleInPlace(AtomsCore::Vector3& out, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
109 
110  inline unsigned int getArrayMetadataType(const std::string &name) const;
111 
112  template <class T> inline void setArrayMetadataValue(const std::string &name, const unsigned int frame, T value);
113 
114  inline AtomsPtr<AtomsCore::Metadata> getArrayMetadataAtFrame(const std::string &name, const unsigned int frame) const;
115 
116  template <class T> inline T getArrayMetadataValueAtFrame(const std::string &name, const unsigned int frame) const;
117 
118  AtomsPtr<AtomsCore::Metadata> getBlendedArrayMetadata(const std::string &name, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
119 
120  bool getBlendedArrayMetadataInPlace(const std::string& name, const unsigned int initialFrame, const unsigned int finalFrame, const double weight, AtomsPtr<AtomsCore::Metadata>& OutMeta) const;
121 
122  template <class T> inline T getBlendedArrayMetadataValue(const std::string &name, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const;
123 
124  AtomsPtr<AtomsCore::Metadata> getDoubleBlendedArrayMetadata(const std::string &name, const unsigned int initialFrame1, const unsigned int finalFrame1, const double weight1, const unsigned int initialFrame2, const unsigned int finalFrame2, const double weight2, const double finalWeight) const;
125  bool getDoubleBlendedArrayMetadataInPlace(const std::string& name, const unsigned int initialFrame1, const unsigned int finalFrame1, const double weight1, const unsigned int initialFrame2, const unsigned int finalFrame2, const double weight2, const double finalWeight, AtomsPtr<AtomsCore::Metadata>& OutMeta) const;
126 
127  inline std::vector<std::string> getArrayMetadataNames() const;
128 
129  template<class T> inline void addArrayMetadata(const std::string &name, const bool force = false);
130 
131  template<typename T, typename V> inline void getArrayMetadataValues(const std::string& name, std::vector<V>& values);
132 
133  inline AtomsPtr<AtomsCore::BaseTypedArrayMetadata> getArrayMetadata(const std::string& name);
134 
135  inline void addArrayMetadata(const std::string &name, AtomsPtr<AtomsCore::BaseTypedArrayMetadata>& data);
136 
137  inline bool hasArrayMetadata(const std::string &name) const;
138 
139  inline void deleteArrayMetadata(const std::string& name);
140 
141  inline std::vector<std::string> getStaticMetadataNames() const;
142 
143  inline void setStaticMetadata(const std::string &name, AtomsCore::Metadata &metadata);
144 
145  inline AtomsPtr<AtomsCore::Metadata> getStaticMetadata(const std::string &name);
146 
147  inline AtomsPtr<const AtomsCore::Metadata> getStaticMetadata(const std::string &name) const;
148 
149  inline AtomsCore::MapMetadata& staticMetadata();
150 
151  inline const AtomsCore::MapMetadata& staticMetadata() const;
152 
153  inline std::map<std::string, AtomsPtr<AtomsCore::BaseTypedArrayMetadata>>& metadata();
154 
155  inline const std::map<std::string, AtomsPtr<AtomsCore::BaseTypedArrayMetadata>>& metadata() const;
156 
157  // Revers the animation
158  void reverse();
159 
160  // Compute the transform animated tag and animated metadata
161  void computeIsAnimated();
162 
163  // Return true if this joint data has animations
164  inline bool isTransformAnimated() const;
165 
166  // Contains a list of all non static metadata
167  inline const std::vector<std::string>& animatedMetadata() const;
168 
169  // Add the metadata to the animated metadata list
170  void addAnimatedMetadata(const std::string& metadataName);
171 
172  void copyFrame(unsigned int from, unsigned int to);
173 
174  public:
175 
176  static bool getBindPoseData(
177  const Atoms::JointClipData& jcd,
178  int jointId,
179  AtomsCore::Vector3& bindTranslate,
180  AtomsCore::Vector3& bindScale,
181  AtomsCore::Quaternion& bindRotate,
182  AtomsCore::Quaternion& bindRotateOffset,
183  const AtomsCore::Skeleton& skeleton
184  );
185 
186  static bool getBindPoseData(
187  const AtomsCore::MapMetadata& jcd,
188  int jointId,
189  AtomsCore::Vector3& bindTranslate,
190  AtomsCore::Vector3& bindScale,
191  AtomsCore::Quaternion& bindRotate,
192  AtomsCore::Quaternion& bindRotateOffset,
193  const AtomsCore::Skeleton& skeleton
194  );
195 
196 
197  private:
198 
199  std::vector<AtomsCore::Vector3> m_translation;
200 
201  std::vector<AtomsCore::Quaternion> m_rotation;
202 
203  std::vector<AtomsCore::Vector3> m_scale;
204 
205  std::map<std::string, AtomsPtr<AtomsCore::BaseTypedArrayMetadata>> m_metadata;
206 
207  std::vector<std::string> m_animatedMetadata;
208 
209  AtomsCore::MapMetadata m_staticMetadata;
210 
211  std::string m_name;
212 
213  unsigned int m_numberOfFrames;
214 
215  bool m_isTransformAnimated;
216 
217  };
218 
220  class ATOMS_EXPORT AnimationClip
221  {
222 
223  public:
224 
225  enum DirectionType
226  {
227  kPelvis = 0,
228  kStatic,
229  kJoints,
230  kClip,
231  kLocal
232  };
233 
234  enum AdditiveMode
235  {
236  kDisabled = 0,
237  kBindPose,
238  kReferenceClip
239  };
240 
241  enum MirrorPlane
242  {
243  kX = 0,
244  kY,
245  kZ
246  };
247 
248  enum MirrorSideJointsSearch
249  {
250  kAuto = 0,
251  kByName
252  };
253 
254  enum MirrorStringReplacementType
255  {
256  kAny = 0,
257  kPrefix,
258  kPostfix
259  };
260 
263 
264  AnimationClip(const unsigned int numberOfJoints, const unsigned int numberOfFrames, const double fps);
265 
268 
270  inline int numJoints() const;
271 
273  inline int numFrames() const;
274 
276  void setNumFrames(const unsigned int numberOfFrames);
277 
279  void setNumJoints(const unsigned int numberOfJoints);
280 
282  inline double fps() const;
283 
285  inline void setFps(double value);
286 
288  inline unsigned int mapTimeToFrame(const double time, double &reminder) const;
289 
291  inline void setJointName(const unsigned int index, const std::string &jointName);
292 
294  inline int jointId(const std::string& jointName) const;
295 
297  inline JointClipData& operator[](unsigned short index);
298 
300  inline const JointClipData& operator[](unsigned short index) const;
301 
303  inline JointClipData& at(unsigned short index);
304 
306  inline const JointClipData& at(unsigned short index) const;
307 
309  inline AtomsCore::MapMetadata& metadata();
310 
312  inline const AtomsCore::MapMetadata& metadata() const;
313 
315  inline void setDirection(const AtomsCore::Vector3& direction);
316 
318  inline AtomsCore::Vector3 direction() const;
319 
321  /* if directionType is kStatic then the clip use alway the direction saved by the setDireciton function,
322  otherwise it takes the direction form the pelvis
323  */
324  inline void setDirectionType(unsigned int directionType);
325 
327  inline unsigned int directionType() const;
328 
330  inline void setDirectionFromJoints(unsigned int fromJoint, unsigned int toJoint);
331 
333  inline const std::pair<unsigned int, unsigned int>& directionFromJoints() const;
334 
336  inline void disableFootLock(bool value);
337 
339  inline bool isFootLockDisabled() const;
340 
342  inline const std::vector<int>& transitionFrames() const;
343 
345  void setTransitionFrames(const std::vector<int>& frames);
346 
348  void setCompatibleAgentType(const std::string &agentType);
349 
351  const std::string& getCompatibleAgentType() const;
352 
354  void setGlobalIKData(const std::string& jointName, const AtomsCore::Vector2ArrayMetadata& data);
355 
357  void setPerchingData(const AtomsCore::Vector2ArrayMetadata& data);
358 
360  void setSittingData(const AtomsCore::Vector2ArrayMetadata& data);
361 
364 
366  void setFootDownData(const std::string& jointName, const AtomsCore::Vector2ArrayMetadata& data);
367 
369  void addJointMetadata(const std::string& metadataName, const std::string& jointName, const int metadataType, const AtomsCore::Vector2ArrayMetadata& data);
370 
372  void setAdditiveMode(const unsigned int additiveMode);
373 
375  void setAdditiveReferenceClip(const std::string &clipName);
376 
378  inline unsigned int additiveMode() const;
379 
381  inline const std::string & additiveReferenceClip() const;
382 
384  void cacheFrameRootRotation(const std::string &agentType);
385 
387  void computeClipDirections(const std::string& agentType, std::vector<AtomsCore::Vector3> &directions) const;
388 
391 
393  /*
394  \param height global y coordinated of the ground
395  */
396  void computePelvisAndFeetHeight(double height);
397 
399  void reverse();
400 
402  /*
403  \param animMirrorPlane normal axis of the mirror plane in the animation
404  \param bindMirrorPlane normal axis of the mirror plane in bind pose
405  \param searchMode search mode to fin pair joints from left and right
406  \param left string identifier of joints on the left
407  \param right string identifier of joints on the right
408  \param rawMirror when on the differences in bind pose on left and right joints is not used (DEPRECATED)
409  \paremt stringReplacementType the strign replacement type: any, prefix or post
410  */
412  const unsigned int animMirrorPlane,
413  const unsigned int bindMirrorPlane,
414  const unsigned int searchMode,
415  const std::string &left,
416  const std::string &right,
417  const bool rawMirror = false,
418  const unsigned int stringReplacementType = 0,
419  const std::vector<std::string> skipJoints = {},
420  const bool affectSkipJointsHierarchy = false);
421 
423  /*
424  This function fills the transformAnimated and animatedMetadata members on each joint clip data
425  */
427 
429  /*
430  Replace . with _ inside the joint names
431  */
433 
435  /*
436  Makes sure no joint in the clip has 0 scale on any of the axis by replacing any 0 value with small non-zero value
437  */
439 
441  double computeAverageVelocity() const;
442 
445 
448  const AtomsCore::Skeleton& skeleton,
449  int clampedFrame, double weight,
450  AtomsCore::JointCPtr pelvisJoint) const;
451 
453  /*
454  This function converts the atoms_footOnGround metadata from a bool array to a double array metadata
455  */
457 
459  /*
460  This function transfers the motion from the skeleton root to the virtual root. Only works when the compatible agent type is set and virtual root is enabled.
461  */
463 
470  void setTransferMotionToVirtualRoot(const bool x, const bool y, const bool z, const bool overrideVirtualRootPos, const AtomsCore::Vector3& virtualRootPos);
471 
473  bool transferMotionToVirtualRootX() const { return m_transferMotionToVirtualRootX; };
474 
476  bool transferMotionToVirtualRootY() const { return m_transferMotionToVirtualRootY; };
477 
479  bool transferMotionToVirtualRootZ() const { return m_transferMotionToVirtualRootZ; };
480 
482  bool overrideVirtualRootPosition() const { return m_overrideVirtualRootPosition; };
483 
485  AtomsCore::Vector3 virtualRootPositionOverride() const { return m_virtualRootPositionOverride; };
486 
489 
490 
491  private:
492 
494  std::vector<JointClipData> m_joints;
495 
497  std::vector<AtomsCore::Vector2> m_sittingFramesData;
498 
500 
503  std::vector<int> m_transitionFrames;
504 
506  std::map<std::string, unsigned int> m_jointIndex;
507 
509  AtomsCore::MapMetadata m_metadata;
510 
512  double m_fps;
513 
515  unsigned int m_numberOfFrames;
516 
518  AtomsCore::Vector3 m_direction;
519 
521  AtomsCore::Vector3 m_virtualRootPositionOverride;
522 
524  std::pair<unsigned int, unsigned int> m_directionFromJoints;
525 
527  unsigned int m_directionType;
528 
530  bool m_isFootLockDisabled;
531 
533  std::string m_compatibleAgentType;
534 
536  unsigned int m_additiveMode;
537 
539  std::string m_additiveReferenceClip;
540 
542  bool m_frameRootRotationCached;
543 
545  bool m_transferMotionToVirtualRootX;
546 
548  bool m_transferMotionToVirtualRootY;
549 
551  bool m_transferMotionToVirtualRootZ;
552 
554  bool m_overrideVirtualRootPosition;
555 
556  bool m_updateVirtualRootMotion;
557  };
558 }
559 
560 
561 #include "AnimationClip.impl.h"
Animation clip.
Definition: AnimationClip.h:221
void computeAnimatedJoints()
Compute the animated joints.
void refreshSittingData()
refresh the sitting data
void setAdditiveMode(const unsigned int additiveMode)
Set the additive mode.
void setAdditiveReferenceClip(const std::string &clipName)
Set additive reference clip.
void setTransitionFrames(const std::vector< int > &frames)
Get the transition frames.
void setNumJoints(const unsigned int numberOfJoints)
Sets the number of frames.
void setTransferMotionToVirtualRoot(const bool x, const bool y, const bool z, const bool overrideVirtualRootPos, const AtomsCore::Vector3 &virtualRootPos)
double computeAverageAngularVelocity() const
Get the mean angular velocity of this clip.
void computePelvisAndFeetHeight(double height)
Compute the the pelvis and feet height.
void sanitizeJointNames()
Sanitize joint names.
double computeAverageVelocity() const
Get the mean velocity of this clip.
bool overrideVirtualRootPosition() const
Get override virtual root position flag.
Definition: AnimationClip.h:482
void setCompatibleAgentType(const std::string &agentType)
Set compatible agent type name.
bool transferMotionToVirtualRootZ() const
Get transfer Z motion from root to virtual root flag.
Definition: AnimationClip.h:479
void sanitizeJointScales()
Sanitize joint scales.
void computeMirror(const unsigned int animMirrorPlane, const unsigned int bindMirrorPlane, const unsigned int searchMode, const std::string &left, const std::string &right, const bool rawMirror=false, const unsigned int stringReplacementType=0, const std::vector< std::string > skipJoints={}, const bool affectSkipJointsHierarchy=false)
Compute the mirror of the clip in the given mirror plane.
AnimationClip()
Constructor.
void cacheFrameRootRotation(const std::string &agentType)
Cache frame root rotation.
void computeClipDirections(const std::string &agentType, std::vector< AtomsCore::Vector3 > &directions) const
Compute and returns the clip directions.
AtomsCore::Vector3 computeClipDirection(Atoms::AgentTypeCPtr atPtr, const int frame) const
Compute and returns the clip direction for a frame.
void setSittingData(const AtomsCore::Vector2ArrayMetadata &data)
Set sitting data.
void conformFootDownDataForLocomotion()
Conforms the foot down data for locomotion.
bool transferMotionToVirtualRootX() const
Get transfer X motion from root to virtual root flag.
Definition: AnimationClip.h:473
void setFootDownData(const std::string &jointName, const AtomsCore::Vector2ArrayMetadata &data)
Set foot data for a specific joint.
void setPerchingData(const AtomsCore::Vector2ArrayMetadata &data)
Set perching data on the joint root.
AtomsCore::Vector3 virtualRootPositionOverride() const
Get override virtual root position flag.
Definition: AnimationClip.h:485
~AnimationClip()
Destructor.
Definition: AnimationClip.h:267
void setNumFrames(const unsigned int numberOfFrames)
Sets the number of frames.
void transferMotionToVirtualRoot()
Transfers the motion from the skeleton root to the virtual root.
void refreshVirtualRoot()
marks the this clip so that the virtual roo data is rebuilt
AtomsCore::Matrix clipJointWorldMatrix(const AtomsCore::Skeleton &skeleton, int clampedFrame, double weight, AtomsCore::JointCPtr pelvisJoint) const
Get a world matrix form the clip.
const std::string & getCompatibleAgentType() const
Get compatible agent type name.
void reverse()
Reverse clip.
void setGlobalIKData(const std::string &jointName, const AtomsCore::Vector2ArrayMetadata &data)
Set global ik data for a specific joint.
void addJointMetadata(const std::string &metadataName, const std::string &jointName, const int metadataType, const AtomsCore::Vector2ArrayMetadata &data)
Add a joint metadata.
bool transferMotionToVirtualRootY() const
Get transfer Y motion from root to virtual root flag.
Definition: AnimationClip.h:476
Joint Clip data.
Definition: AnimationClip.h:38
AtomsCore::Vector3 getBlendedScale(const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const
Gets blended scale between two frames.
void getBlendedScaleInPlace(AtomsCore::Vector3 &out, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const
Gets blended scale between two frames.
void setNumberOfFrames(const int value)
set number of frames
void getBlendedTranslationInPlace(AtomsCore::Vector3 &out, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const
Gets blended translation between two frames.
~JointClipData()
Destructr.
unsigned int getNumberOfFrames()
get number of frames
Definition: AnimationClip.h:69
AtomsCore::Quaternion getBlendedRotation(const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const
Gets blended rotation between two frames.
JointClipData(const unsigned int numberOfFrames)
Cosntructor.
AtomsCore::Vector3 getBlendedTranslation(const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const
Gets blended translation between two frames.
void getBlendedRotationInPlace(AtomsCore::Quaternion &out, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const
Gets blended rotation between two frames.
Joint class.
Definition: Joint.h:30
MapMetadata class.
Definition: MapMetadata.h:24
Base Metadata class.
Definition: Metadata.h:24
Skeleton class.
Definition: Skeleton.h:68
AtomsMath::Quaternion Quaternion
Quaternion class.
Definition: AtomsMath.h:67
AtomsMath::Vector3 Vector3
Vector3 class.
Definition: AtomsMath.h:57
AtomsMath::Matrix Matrix
Matrix class.
Definition: AtomsMath.h:63
Atoms namespace.
Definition: Agent.h:29
AtomsPtr< const AnimationClip > AnimationClipCPtr
animation clip const pointer
Definition: AnimationClip.h:32
AtomsPtr< AnimationClip > AnimationClipPtr
animation clip pointer
Definition: AnimationClip.h:28
AtomsPtr< const AgentType > AgentTypeCPtr
Agent type const pointer.
Definition: AgentType.h:21