HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KIN_Chain.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: KIN_Chain.h ( Kinematics Library, C++)
7  *
8  * COMMENTS:
9  * This class holds the specification of a kinematic
10  * chain. The chain is specified as a string of the
11  * form: "length rx ry rz, ..., length rx ry rz;"
12  */
13 
14 #ifndef __KIN_Chain__
15 #define __KIN_Chain__
16 
17 #include "KIN_API.h"
18 #include "KIN_Bone.h"
19 #include <UT/UT_Array.h>
20 #include <UT/UT_Matrix4.h>
21 #include <UT/UT_StringHolder.h>
22 #include <UT/UT_UniquePtr.h>
23 #include <UT/UT_Vector3.h>
24 #include <UT/UT_Vector3Array.h>
25 #include <UT/UT_XformOrder.h>
26 #include <SYS/SYS_Inline.h>
27 #include <SYS/SYS_Math.h>
28 
29 #define KIN_CHAIN_ROT_ORDER UT_XformOrder::ZYX
30 
31 class KIN_Bone;
32 class KIN_Solver;
33 
35 {
41 };
42 
43 // "curve" solver type for Follow Curve
45 {
46 public:
47  KIN_CurveParm() { init(); }
48 
49  void init()
50  {
51  myClosedFlag = false;
52  myOrientToNormalsFlag = false;
53  myTangent = 0;
54  myPoints.clear();
55  myNormals.clear();
56  }
57 
63 };
64 
65 // NOT IMPLEMENTED! DO NOT USE!
67 {
68 public:
69  fpreal myTolerance = 1e-4; // tolerance on solution
70 };
71 
72 // Common parameters for inverse kinematics related solvers
74 {
75 public:
81 };
82 
83 // "constraint" solver type for Inverse Kinematics (IK) with Constraints
85 {
86 public:
87  fpreal myTolerance = 1e-4; // tolerance on solution
88 };
89 
90 // "inverse" solver type for Inverse Kinematics (IK)
92 {
93 public:
94  fpreal myTrackingThresholdFactor = 1e-3;
95  int myResistStraight = true;
96 };
97 
99 {
100 public:
101 
102  KIN_Chain();
103  KIN_Chain(int num_bones);
104  KIN_Chain(const KIN_Chain &copy_from);
105  ~KIN_Chain();
106 
107  static const UT_XformOrder &getXformOrder() { return myXformOrder; }
108 
109  // These functions are for getting and setting bone information.
110  // All access to a chains bones must go through the chain so that we
111  // can make sure our solver is up to date. The separate updateBone,
112  // setBoneRotate, and setBoneRotates functions are only provided
113  // for convenience. Also notice that the Chain creator no longer
114  // has to actually create or remove any bones from the chain,
115  // only to set the number of bones.
116  int getNbones() const;
117  void setNbones(int bones);
118  const KIN_Bone &getBone(int index) const
119  { return myBones(index); }
120  void updateBone(int index, fpreal length, fpreal *rot,
121  fpreal damp, const UT_Matrix4R &xform,
122  const void *data);
123  void setBoneRotate(int index, int axis, fpreal r);
124  void setBoneRotates(int index, const fpreal *r);
125  void setBoneRotates(int index, const fpreal *deg, const fpreal *rads);
126  void getBoneRotates(int index, fpreal *r) const;
127 
128 
129  // The Curve Solver can output rotation matrices directly without conversion.
130  // This isn't used by the other solvers, as they are solving in rotation space.
131  void setBoneRotateMatrix(int index, const UT_Matrix3R &m);
132  const UT_Matrix3R & getBoneRotateMatrix(int index) const;
133 
134  // Currently we are limited to one constraint per bone, so the
135  // interface to the bone constraints can be very simple. Any constraint
136  // that gets passed in here will be deleted by the bone.
137  void setConstraint( int index, fpreal rest[3],
138  fpreal xrange[2], fpreal yrange[2],
139  fpreal zrange[2], fpreal damp[3],
140  fpreal roll[3] );
141  fpreal constrain( int index, int axis,
142  fpreal angle, fpreal step ) const;
143 
144  // Utility functions for getting transform information for each bone
145  // or for just the chain end position.
146  void getEndPosition(UT_Vector3R &result) const;
147  void getBoneStates(UT_Matrix4R *xforms) const;
148  fpreal getLength() const;
149 
150  // Functions for setting solver information for the chain
151  // The solver types are same tokens as found in the "Solver Type" parameter
152  // of the InverseKin CHOP. See the comments for the KIN_*Parm classes above.
153  int solve(const UT_StringHolder &type, const void *parms,
154  KIN_Chain &solution);
155 
156  // Essentially a copy operator
157  void copyFrom(const KIN_Chain &src);
158  void copySubChain(const KIN_Chain &src, int from, int to);
159 
160  int64 getMemoryUsage(bool inclusive) const
161  {
162  int64 mem = inclusive ? sizeof(*this) : 0;
163  mem += myBones.getMemoryUsage(false);
164  mem += mySolverType.getMemoryUsage(false);
165  return mem;
166  }
167 
168  void updateCurrentSolverRestChain();
169 private:
170  void initializeSolver();
171  void chainChanged(KIN_ChangeType type);
172 
173  UT_Array<KIN_Bone> myBones;
174  UT_UniquePtr<KIN_Solver> mySolver;
175  UT_StringHolder mySolverType;
176  static const UT_XformOrder myXformOrder;
177 };
178 
180 void
182 {
183  auto &&bone = myBones(index);
184  if( mySolver )
185  {
186  if( !SYSisEqual( r[0], bone.getRotate(0) ) ||
187  !SYSisEqual( r[1], bone.getRotate(1) ) ||
188  !SYSisEqual( r[2], bone.getRotate(2) ) )
189  chainChanged(BONE_ROTATE);
190  }
191  bone.setRotates(r);
192 }
193 
195 void
196 KIN_Chain::setBoneRotates(int index, const fpreal *r, const fpreal *rads)
197 {
198  auto &&bone = myBones(index);
199  if( mySolver )
200  {
201  if( !SYSisEqual( r[0], bone.getRotate(0) ) ||
202  !SYSisEqual( r[1], bone.getRotate(1) ) ||
203  !SYSisEqual( r[2], bone.getRotate(2) ) )
204  chainChanged(BONE_ROTATE);
205  }
206  bone.setRotates(r,rads);
207 }
208 
210 void
212 {
213  getBone(index).getRotates(r);
214 }
215 
217 void
219 {
220  auto &&bone = myBones(index);
221  return bone.setRotateMatrix(m);
222 }
223 
225 const UT_Matrix3R&
227 {
228  return getBone(index).getRotateMatrix();
229 }
230 
231 #endif
232 
KIN_ChangeType
Definition: KIN_Chain.h:34
void setBoneRotates(int index, const fpreal *r)
Definition: KIN_Chain.h:181
SIM_API const UT_StringHolder angle
void getRotates(fpreal *r) const
Definition: KIN_Bone.h:40
int64 getMemoryUsage(bool inclusive) const
Definition: KIN_Chain.h:160
Transformation order of scales, rotates, and translates.
Definition: UT_XformOrder.h:23
GA_API const UT_StringHolder rot
void setBoneRotateMatrix(int index, const UT_Matrix3R &m)
Definition: KIN_Chain.h:218
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
const KIN_Bone & getBone(int index) const
Definition: KIN_Chain.h:118
**But if you need a result
Definition: thread.h:622
void init()
Definition: KIN_Chain.h:49
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
UT_Vector3R myTangent
Definition: KIN_Chain.h:60
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
fpreal myTwist
Definition: KIN_Chain.h:79
bool myOrientToNormalsFlag
Definition: KIN_Chain.h:59
fpreal myDampen
Definition: KIN_Chain.h:80
void getBoneRotates(int index, fpreal *r) const
Definition: KIN_Chain.h:211
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
long long int64
Definition: SYS_Types.h:116
const UT_Matrix3R & getBoneRotateMatrix(int index) const
Definition: KIN_Chain.h:226
GA_API const UT_StringHolder parms
fpreal64 fpreal
Definition: SYS_Types.h:278
UT_Vector3RArray myPoints
Definition: KIN_Chain.h:61
UT_Vector3R myTwistAffectorPos
Definition: KIN_Chain.h:77
GLuint index
Definition: glcorearb.h:786
#define KIN_API
Definition: KIN_API.h:11
GU_API void solve(const GU_Detail &gdp_a, const GA_Range &pts_a, const GU_Detail &gdp_b, const GA_Range &pts_b, Method method, bool compute_distortion, Result &result)
static const UT_XformOrder & getXformOrder()
Definition: KIN_Chain.h:107
int myTwistAffectorFlag
Definition: KIN_Chain.h:78
GLboolean r
Definition: glcorearb.h:1222
UT_Vector3RArray myNormals
Definition: KIN_Chain.h:62
bool SYSisEqual(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b, S tol=SYS_FTOLERANCE)
Componentwise equality.
Definition: UT_Vector2.h:674
GA_API const UT_StringHolder rest
UT_Vector3R myEndAffectorPos
Definition: KIN_Chain.h:76
Definition: format.h:1821
const UT_Matrix3R & getRotateMatrix() const
Definition: KIN_Bone.h:106
GLenum src
Definition: glcorearb.h:1793
bool myClosedFlag
Definition: KIN_Chain.h:58