Atoms Crowd  7.0.0
ClipReaderOperator.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 <AtomsGraph/Ports.h>
11 
12 #include <Atoms/Globals.h>
13 #include <Atoms/Graph/Operator.h>
14 #include <Atoms/AnimationClipLooper.h>
15 #include <AtomsCore/Metadata/Vector3Metadata.h>
16 #include <AtomsCore/JointChain.h>
17 #include <Atoms/AgentType.h>
18 
19 namespace Atoms
20 {
21  class ATOMS_EXPORT ClipReaderOperator : public Operator
22  {
23  public:
24 
25  struct BindData
26  {
27  AtomsCore::Matrix rotation;
28  AtomsCore::Matrix matrix;
29  AtomsCore::Matrix inverseMatrix;
30  };
31 
32  NODE_STANDARD_MEMBERS
33 
35 
36  virtual ~ClipReaderOperator();
37 
38  bool compute(const AtomsGraph::ComputeData* computeData);
39 
40  void reset();
41 
42  void setupDefaultPose();
43 
44  void fillSkeletonClipIdMap(AnimationClipCPtr& clip, const AtomsCore::Skeleton& skeleton);
45 
46  double setPoseFromLooper(AnimationClipCPtr& clip, const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose, double time, bool isAtomsClip, bool isParentSpace, const AtomsCore::SkeletonLod* skeletonLod, const long frameOffset);
47  void setPoseFromClip(AnimationClipCPtr& clip, const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose, int clampedFrame, double weight, bool isAtomsClip, bool isParentSpace, const AtomsCore::SkeletonLod* skeletonLod);
48 
49  AtomsCore::Vector3 getPelvisVelocityFromClip(AnimationClipCPtr& clip, const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose, int frame, double reminder, const AtomsCore::SkeletonLod* skeletonLod, const int newFrame, const double newReminder);
50  AtomsCore::Vector3 getPelvisVelocityFromLooper(AnimationClipCPtr& clip, const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose, int frame, double reminder, const AtomsCore::SkeletonLod* skeletonLod, const int newFrame, const double newReminder, const long frameOffset);
51 
52  AtomsCore::Vector3 getDirectionAtFrame(AtomsCore::Poser& poser, AnimationClipCPtr& clip, const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose, const AtomsCore::Vector3& velocity, const unsigned int frame, const unsigned int nextFrame, const double reminder, const int rootPelvisIndex);
53 
54  void removeFootLock(const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose);
55  void scaleMetadata(const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose, double agentScale, double retargetingFactor);
56  void scaleDetachedJoints(const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose, double agentScale, double retargetingFactor);
57  void setLocalDirectionAndVelocity(AtomsCore::Poser& poser,
58  AnimationClipCPtr& clip,
59  const AtomsCore::Skeleton& skeleton,
60  AtomsCore::Pose& pose,
61  const AtomsCore::Vector3& velocity,
62  double retargetingFactor,
63  bool syncede,
64  const AtomsCore::Vector3 &rootOffset,
65  double clipFrame,
66  double startTime,
67  double endTime,
68  const long frameOffset);
69 
70 
73  bool useBind,
74  const AtomsCore::JointChain* chain,
75  const std::vector<AtomsCore::Vector3>& allPositions,
76  const AtomsCore::Vector3 &startPos,
77  const AtomsCore::Vector3 &endPos,
78  AtomsCore::Vector3 &midPos,
79  AtomsCore::Vector3 &poleVector,
80  double threshold = 0.9999);
81 
82  AtomsCore::Vector3 getTargetPoint(
83  const AtomsCore::Vector3 sStartPos,
84  const AtomsCore::Vector3 sEndPos,
85  const AtomsCore::Vector3 tStartPos,
86  const AtomsCore::Vector3 offset,
87  double factor);
88 
89  void resetJointPoses(const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose);
90 
91  void refreshTargetMatrix(
92  const AtomsCore::Skeleton& tSkeleton,
93  AtomsCore::Pose& tPose,
94  unsigned short jointId);
95 
96  void computeRetarget(
97  const Atoms::AgentTypeCPtr tAgentType,
98  const Atoms::AgentTypeCPtr sAgentType,
99  const AtomsCore::Skeleton& tSkeleton,
100  const AtomsCore::Skeleton& sSkeleton,
101  AtomsCore::Pose& tPose,
102  AtomsCore::Pose& sPose,
103  std::map<unsigned short, AtomsCore::MapMetadata> &sMetadata,
104  const double agentScale);
105 
106  inline void useLocomotion(bool value) { m_isLocomotion = value; }
107 
108  void setTimeAndPreviousTime(double time, double prevTime);
109 
110  void resetPreviousDirection() { m_resetPreviousDirection = true; }
111 
112  private:
113 
114  double getIkBlend(
115  const AtomsCore::JointChain* sChain,
116  std::map<unsigned short, AtomsCore::MapMetadata>& sMetadata
117  );
118 
119  AtomsMath::Vector3 computeIkBlendRelativeTarget(
120  const AtomsCore::JointChain* tChain,
121  const AtomsCore::JointChain* sChain,
122  const double agentScale,
123  const AtomsMath::Vector3& agentPlaneNormal
124  );
125 
126  AtomsMath::Vector3 computeIkBlendRelativeTarget(
128  const Atoms::AgentTypeCPtr tAgentType,
129  const Atoms::AgentTypeCPtr sAgentType,
130  AtomsCore::Pose& tPose,
131  const AtomsMath::Vector3& agentPlaneNormal,
132  const AtomsCore::Vector3& sStartPos,
133  const AtomsCore::Vector3& sEndPos,
134  const AtomsCore::Vector3& tStartPos,
135  const double agentScale
136  );
137 
138  AtomsMath::Vector3 computeTargetPos(
139  std::map<unsigned short, AtomsCore::MapMetadata>& sMetadata,
141  const Atoms::AgentTypeCPtr tAgentType,
142  const Atoms::AgentTypeCPtr sAgentType,
143  AtomsCore::Pose& tPose,
144  const AtomsMath::Vector3& agentPlaneNormal,
145  const AtomsCore::Vector3& sStartPos,
146  const AtomsCore::Vector3& sEndPos,
147  const AtomsCore::Vector3& tStartPos,
148  const double agentScale);
149 
150  void computeThreeJointChain(
152  const Atoms::AgentTypeCPtr tAgentType,
153  const Atoms::AgentTypeCPtr sAgentType,
154  AtomsCore::Pose& tPose,
155  const AtomsMath::Vector3& targetPos,
156  const AtomsCore::Vector3& sStartPos,
157  const AtomsCore::Vector3& sEndPos,
158  const AtomsCore::Vector3& tStartPos,
159  const double agentScale,
160  const std::pair<AtomsMath::Vector3, AtomsMath::Vector3>& localPoleVector);
161 
162  void computeFARBIKChain(
164  const Atoms::AgentTypeCPtr tAgentType,
165  const Atoms::AgentTypeCPtr sAgentType,
166  AtomsCore::Pose& tPose,
167  const AtomsMath::Vector3& targetPos,
168  const AtomsCore::Vector3& sStartPos,
169  const AtomsCore::Vector3& sEndPos,
170  const AtomsCore::Vector3& tStartPos);
171 
172  AtomsCore::Matrix getClipJointMatrix(AnimationClipCPtr& clip,
173  const AtomsCore::Skeleton& skeleton,
174  int clampedFrame, double weight,
175  AtomsCore::JointCPtr pelvisJoint);
176 
177  private:
178 
179  AtomsCore::Vector3 m_prevDirection;
180 
181  AtomsGraph::StringPort* m_clipNamePort;
182 
183  AtomsGraph::DoublePort* m_timePort;
184 
185  AtomsGraph::DoublePort* m_startFramePort;
186 
187  AtomsGraph::DoublePort* m_endFramePort;
188 
189  AtomsGraph::DoublePort* m_frameRate;
190 
191  AtomsGraph::DoublePort* m_outframePort;
192 
193  AtomsGraph::BooleanPort* m_overrideSimDirectionPort;
194 
195  AtomsGraph::BooleanPort* m_disableDirectionBlendingPort;
196 
197  AtomsGraph::DoublePort* m_overrideSimDirectionMaxTurnAnglePort;
198 
199  AtomsGraph::LongPort* m_frameOffset;
200 
201  Atoms::AnimationClipLooper *m_clipLooper;
202 
203  unsigned int m_currentLoop;
204 
205  std::vector<int> m_skeletonClipJointId;
206 
207  std::vector<BindData> m_bindData;
208 
209  std::string frameRateOverrideName;
210 
211  AnimationClipCPtr m_clip;
212 
213  double m_retargetingFactor;
214  double m_retargetingIKFactor;
215 
217  Atoms::AgentTypeCPtr m_agentTypeCPtr;
218 
220  AtomsCore::Pose m_sourcePose;
221 
223  std::map<std::string, AtomsCore::Vector3> m_poleVectorInBindMap;
224 
226  std::vector<AtomsCore::Matrix> m_sAllMatrix;
227  std::vector<AtomsCore::Matrix> m_tAllMatrix;
228  std::vector<AtomsCore::Vector3> m_sAllPositions;
229  std::vector<AtomsCore::Vector3> m_tAllPositions;
230  std::vector<bool> m_tAllMatrixDirty;
231  std::vector<std::pair<AtomsMath::Vector3, AtomsMath::Vector3>> m_localPoleVectors;
232  AtomsCore::Quaternion rotationOffset;
233 
234  double m_prevTime;
235 
236  double m_agentTypeScaleMultiplier;
237 
238  bool m_isAtomsClip;
239  bool m_isLoop;
240  bool m_first;
241  bool m_retarget;
242  bool m_isParentSpace;
243  bool m_isLocomotion;
244  bool m_loopBlending;
245  bool m_resetPreviousDirection;
246  };
247 }
Animation clip looper.
Definition: AnimationClipLooper.h:27
Definition: ClipReaderOperator.h:22
bool compute(const AtomsGraph::ComputeData *computeData)
Compute function.
bool getPoleVector(bool useBind, const AtomsCore::JointChain *chain, const std::vector< AtomsCore::Vector3 > &allPositions, const AtomsCore::Vector3 &startPos, const AtomsCore::Vector3 &endPos, AtomsCore::Vector3 &midPos, AtomsCore::Vector3 &poleVector, double threshold=0.9999)
Methods to compute retargeting.
void reset()
Operator reset function.
Operator node.
Definition: Operator.h:26
JointChain class.
Definition: JointChain.h:31
Joint class.
Definition: Joint.h:30
Pose class.
Definition: Pose.h:32
Poser class.
Definition: Poser.h:24
Skeleton class.
Definition: Skeleton.h:68
Definition: Node.h:21
Generic node port class.
Definition: PortTemplate.h:24
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< const AgentType > AgentTypeCPtr
Agent type const pointer.
Definition: AgentType.h:21
Definition: ClipReaderOperator.h:26
Definition: Skeleton.h:25