Overte C++ Documentation
AnimInverseKinematics.h
1 //
2 // AnimInverseKinematics.h
3 //
4 // Copyright 2015 High Fidelity, Inc.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 //
9 
10 #ifndef hifi_AnimInverseKinematics_h
11 #define hifi_AnimInverseKinematics_h
12 
13 #include <string>
14 
15 #include <map>
16 #include <vector>
17 
18 #include "AnimNode.h"
19 #include "IKTarget.h"
20 
21 #include "RotationAccumulator.h"
22 #include "TranslationAccumulator.h"
23 
24 class RotationConstraint;
25 
26 class AnimInverseKinematics : public AnimNode {
27 public:
28 
29  struct JointInfo {
30  glm::quat rot;
31  glm::vec3 trans;
32  int jointIndex;
33  bool constrained;
34  };
35 
36  struct JointChainInfo {
37  std::vector<JointInfo> jointInfoVec;
38  IKTarget target;
39  float timer { 0.0f };
40  };
41 
42  using JointChainInfoVec = std::vector<JointChainInfo>;
43 
44  explicit AnimInverseKinematics(const QString& id);
45  virtual ~AnimInverseKinematics() override;
46 
47  void loadDefaultPoses(const AnimPoseVec& poses);
48  void loadPoses(const AnimPoseVec& poses);
49  void computeAbsolutePoses(AnimPoseVec& absolutePoses) const;
50 
51  void setTargetVars(const QString& jointName, const QString& positionVar, const QString& rotationVar,
52  const QString& typeVar, const QString& weightVar, float weight, const std::vector<float>& flexCoefficients,
53  const QString& poleVectorEnabledVar, const QString& poleReferenceVectorVar, const QString& poleVectorVar);
54 
55  virtual const AnimPoseVec& evaluate(const AnimVariantMap& animVars, const AnimContext& context, float dt, AnimVariantMap& triggersOut) override;
56  virtual const AnimPoseVec& overlay(const AnimVariantMap& animVars, const AnimContext& context, float dt, AnimVariantMap& triggersOut, const AnimPoseVec& underPoses) override;
57 
58  void clearIKJointLimitHistory();
59 
60  float getMaxErrorOnLastSolve() { return _maxErrorOnLastSolve; }
61 
62  /*@jsdoc
63  * <p>Specifies the initial conditions of the IK solver.</p>
64  * <table>
65  * <thead>
66  * <tr><th>Value</th><th>Name</th><th>Description</th>
67  * </thead>
68  * <tbody>
69  * <tr><td><code>0</code></td><td>RelaxToUnderPoses</td><td>This is a blend: it is 15/16 <code>PreviousSolution</code>
70  * and 1/16 <code>UnderPoses</code>. This provides some of the benefits of using <code>UnderPoses</code> so that the
71  * underlying animation is still visible, while at the same time converging faster then using the
72  * <code>UnderPoses</code> as the only initial solution.</td></tr>
73  * <tr><td><code>1</code></td><td>RelaxToLimitCenterPoses</td><td>This is a blend: it is 15/16
74  * <code>PreviousSolution</code> and 1/16 <code>LimitCenterPoses</code>. This should converge quickly because it is
75  * close to the previous solution, but still provides the benefits of avoiding limb locking.</td></tr>
76  * <tr><td><code>2</code></td><td>PreviousSolution</td><td>
77  * <p>The IK system will begin to solve from the same position and orientations for each joint that was the result
78  * from the previous frame.</p>
79  * <p>Pros: As the end effectors typically do not move much from frame to frame, this is likely to converge quickly
80  * to a valid solution.</p>
81  * <p>Cons: If the previous solution resulted in an awkward or uncomfortable posture, the next frame will also be
82  * awkward and uncomfortable. It can also result in locked elbows and knees.</p>
83  * </td></tr>
84  * <tr><td><code>3</code></td><td>UnderPoses</td><td>The IK occurs at one of the top-most layers. It has access to the
85  * full posture that was computed via canned animations and blends. We call this animated set of poses the "under
86  * pose". The under poses are what would be visible if IK was completely disabled. Using the under poses as the
87  * initial conditions of the CCD solve will cause some of the animated motion to be blended into the result of the
88  * IK. This can result in very natural results, especially if there are only a few IK targets enabled. On the other
89  * hand, because the under poses might be quite far from the desired end effector, it can converge slowly in some
90  * cases, causing it to never reach the IK target in the allotted number of iterations. Also, in situations where all
91  * of the IK targets are being controlled by external sensors, sometimes starting from the under poses can cause
92  * awkward motions from the underlying animations to leak into the IK result.</td></tr>
93  * <tr><td><code>4</code></td><td>LimitCenterPoses</td><td>This pose is taken to be the center of all the joint
94  * constraints. This can prevent the IK solution from getting locked or stuck at a particular constraint. For
95  * example, if the arm is pointing straight outward from the body, as the end effector moves towards the body, at
96  * some point the elbow should bend to accommodate. However, because the CCD solver is stuck at a local maximum, it
97  * will not rotate the elbow, unless the initial conditions already have the elbow bent, which is the case for
98  * <code>LimitCenterPoses</code>. When all the IK targets are enabled, this result will provide a consistent starting
99  * point for each IK solve, hopefully resulting in a consistent, natural result.</td></tr>
100  * </tbody>
101  * </table>
102  * @typedef {number} MyAvatar.AnimIKSolutionSource
103  */
104  enum class SolutionSource {
105  RelaxToUnderPoses = 0,
106  RelaxToLimitCenterPoses,
107  PreviousSolution,
108  UnderPoses,
109  LimitCenterPoses,
110  NumSolutionSources,
111  };
112 
113  void setSecondaryTargetInRigFrame(int jointIndex, const AnimPose& pose);
114  void clearSecondaryTarget(int jointIndex);
115 
116  void setSolutionSource(SolutionSource solutionSource) { _solutionSource = solutionSource; }
117  void setSolutionSourceVar(const QString& solutionSourceVar) { _solutionSourceVar = solutionSourceVar; }
118 
119 protected:
120  void computeTargets(const AnimVariantMap& animVars, std::vector<IKTarget>& targets, const AnimPoseVec& underPoses);
121  void solve(const AnimContext& context, const std::vector<IKTarget>& targets, float dt, JointChainInfoVec& jointChainInfoVec);
122  void solveTargetWithCCD(const AnimContext& context, const IKTarget& target, const AnimPoseVec& absolutePoses,
123  bool debug, JointChainInfo& jointChainInfoOut) const;
124  void solveTargetWithSpline(const AnimContext& context, const IKTarget& target, const AnimPoseVec& absolutePoses,
125  bool debug, JointChainInfo& jointChainInfoOut) const;
126  virtual void setSkeletonInternal(AnimSkeleton::ConstPointer skeleton) override;
127  void debugDrawIKChain(const JointChainInfo& jointChainInfo, const AnimContext& context) const;
128  void debugDrawRelativePoses(const AnimContext& context) const;
129  void debugDrawConstraints(const AnimContext& context) const;
130  void debugDrawSpineSplines(const AnimContext& context, const std::vector<IKTarget>& targets) const;
131  void initRelativePosesFromSolutionSource(SolutionSource solutionSource, const AnimPoseVec& underPose);
132  void blendToPoses(const AnimPoseVec& targetPoses, const AnimPoseVec& underPose, float blendFactor);
133  void preconditionRelativePosesToAvoidLimbLock(const AnimContext& context, const std::vector<IKTarget>& targets);
134  void setSecondaryTargets(const AnimContext& context);
135 
136  // used to pre-compute information about each joint influeced by a spline IK target.
137  struct SplineJointInfo {
138  int jointIndex; // joint in the skeleton that this information pertains to.
139  float ratio; // percentage (0..1) along the spline for this joint.
140  AnimPose offsetPose; // local offset from the spline to the joint.
141  };
142 
143  void computeAndCacheSplineJointInfosForIKTarget(const AnimContext& context, const IKTarget& target) const;
144  const std::vector<SplineJointInfo>* findOrCreateSplineJointInfo(const AnimContext& context, const IKTarget& target) const;
145 
146  // for AnimDebugDraw rendering
147  virtual const AnimPoseVec& getPosesInternal() const override { return _relativePoses; }
148 
149  RotationConstraint* getConstraint(int index) const;
150  void clearConstraints();
151  void initConstraints();
152  void initLimitCenterPoses();
153  float getInterpolationAlpha(float timer);
154 
155  // no copies
156  AnimInverseKinematics(const AnimInverseKinematics&) = delete;
157  AnimInverseKinematics& operator=(const AnimInverseKinematics&) = delete;
158 
159  enum FlexCoefficients { MAX_FLEX_COEFFICIENTS = 10 };
160  struct IKTargetVar {
161  IKTargetVar(const QString& jointNameIn, const QString& positionVarIn, const QString& rotationVarIn,
162  const QString& typeVarIn, const QString& weightVarIn, float weightIn, const std::vector<float>& flexCoefficientsIn,
163  const QString& poleVectorEnabledVar, const QString& poleReferenceVectorVar, const QString& poleVectorVar);
164  IKTargetVar(const IKTargetVar& orig);
165  AnimInverseKinematics::IKTargetVar& operator=(const AnimInverseKinematics::IKTargetVar&) = default;
166 
167  QString jointName;
168  QString positionVar;
169  QString rotationVar;
170  QString typeVar;
171  QString weightVar;
172  QString poleVectorEnabledVar;
173  QString poleReferenceVectorVar;
174  QString poleVectorVar;
175  float weight;
176  float flexCoefficients[MAX_FLEX_COEFFICIENTS];
177  size_t numFlexCoefficients;
178  int jointIndex; // cached joint index
179  };
180 
181  std::map<int, RotationConstraint*> _constraints;
182  std::vector<RotationAccumulator> _rotationAccumulators;
183  std::vector<TranslationAccumulator> _translationAccumulators;
184  std::vector<IKTargetVar> _targetVarVec;
185  AnimPoseVec _defaultRelativePoses; // poses of the relaxed state
186  AnimPoseVec _relativePoses; // current relative poses
187  AnimPoseVec _limitCenterPoses; // relative
188  std::map<int, glm::quat> _rotationOnlyIKRotations;
189 
190  std::map<int, AnimPose> _secondaryTargetsInRigFrame;
191 
192  mutable std::map<int, std::vector<SplineJointInfo>> _splineJointInfoMap;
193 
194  int _headIndex { -1 };
195  int _hipsIndex { -1 };
196  int _hipsParentIndex { -1 };
197  int _hipsTargetIndex { -1 };
198  int _leftHandIndex { -1 };
199  int _rightHandIndex { -1 };
200 
201  float _maxErrorOnLastSolve { FLT_MAX };
202  bool _previousEnableDebugIKTargets { false };
203  SolutionSource _solutionSource { SolutionSource::RelaxToUnderPoses };
204  QString _solutionSourceVar;
205 
206  JointChainInfoVec _prevJointChainInfoVec;
207 };
208 
209 #endif // hifi_AnimInverseKinematics_h