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; // 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; // tolerance on solution
88 };
89 
90 // "inverse" solver type for Inverse Kinematics (IK)
92 {
93 public:
95  {
96  myResistStraight = true;
97  myTrackingThresholdFactor = 1e-03;
98  }
99 
102 };
103 
105 {
106 public:
107 
108  KIN_Chain();
109  KIN_Chain(int num_bones);
110  KIN_Chain(const KIN_Chain &copy_from);
111  ~KIN_Chain();
112 
113  static const UT_XformOrder &getXformOrder() { return myXformOrder; }
114 
115  // These functions are for getting and setting bone information.
116  // All access to a chains bones must go through the chain so that we
117  // can make sure our solver is up to date. The separate updateBone,
118  // setBoneRotate, and setBoneRotates functions are only provided
119  // for convenience. Also notice that the Chain creator no longer
120  // has to actually create or remove any bones from the chain,
121  // only to set the number of bones.
122  int getNbones() const;
123  void setNbones(int bones);
124  const KIN_Bone &getBone(int index) const
125  { return myBones(index); }
126  void updateBone(int index, fpreal length, fpreal *rot,
127  fpreal damp, const UT_Matrix4R &xform,
128  const void *data);
129  void setBoneRotate(int index, int axis, fpreal r);
130  void setBoneRotates(int index, const fpreal *r);
131  void setBoneRotates(int index, const fpreal *deg, const fpreal *rads);
132  void getBoneRotates(int index, fpreal *r) const;
133 
134 
135  // The Curve Solver can output rotation matrices directly without conversion.
136  // This isn't used by the other solvers, as they are solving in rotation space.
137  void setBoneRotateMatrix(int index, const UT_Matrix3R &m);
138  const UT_Matrix3R & getBoneRotateMatrix(int index) const;
139 
140  // Currently we are limited to one constraint per bone, so the
141  // interface to the bone constraints can be very simple. Any constraint
142  // that gets passed in here will be deleted by the bone.
143  void setConstraint( int index, fpreal rest[3],
144  fpreal xrange[2], fpreal yrange[2],
145  fpreal zrange[2], fpreal damp[3],
146  fpreal roll[3] );
147  fpreal constrain( int index, int axis,
148  fpreal angle, fpreal step ) const;
149 
150  // Utility functions for getting transform information for each bone
151  // or for just the chain end position.
152  void getEndPosition(UT_Vector3R &result) const;
153  void getBoneStates(UT_Matrix4R *xforms) const;
154  fpreal getLength() const;
155 
156  // Functions for setting solver information for the chain
157  // The solver types are same tokens as found in the "Solver Type" parameter
158  // of the InverseKin CHOP. See the comments for the KIN_*Parm classes above.
159  int solve(const UT_StringHolder &type, const void *parms,
160  KIN_Chain &solution);
161 
162  // Essentially a copy operator
163  void copyFrom(const KIN_Chain &src);
164  void copySubChain(const KIN_Chain &src, int from, int to);
165 
166  int64 getMemoryUsage(bool inclusive) const
167  {
168  int64 mem = inclusive ? sizeof(*this) : 0;
169  mem += myBones.getMemoryUsage(false);
170  mem += mySolverType.getMemoryUsage(false);
171  return mem;
172  }
173 
174  void updateCurrentSolverRestChain();
175 private:
176  void initializeSolver();
177  void chainChanged(KIN_ChangeType type);
178 
179  UT_Array<KIN_Bone> myBones;
180  UT_UniquePtr<KIN_Solver> mySolver;
181  UT_StringHolder mySolverType;
182  static const UT_XformOrder myXformOrder;
183 };
184 
186 void
188 {
189  auto &&bone = myBones(index);
190  if( mySolver )
191  {
192  if( !SYSisEqual( r[0], bone.getRotate(0) ) ||
193  !SYSisEqual( r[1], bone.getRotate(1) ) ||
194  !SYSisEqual( r[2], bone.getRotate(2) ) )
195  chainChanged(BONE_ROTATE);
196  }
197  bone.setRotates(r);
198 }
199 
201 void
202 KIN_Chain::setBoneRotates(int index, const fpreal *r, const fpreal *rads)
203 {
204  auto &&bone = myBones(index);
205  if( mySolver )
206  {
207  if( !SYSisEqual( r[0], bone.getRotate(0) ) ||
208  !SYSisEqual( r[1], bone.getRotate(1) ) ||
209  !SYSisEqual( r[2], bone.getRotate(2) ) )
210  chainChanged(BONE_ROTATE);
211  }
212  bone.setRotates(r,rads);
213 }
214 
216 void
218 {
219  getBone(index).getRotates(r);
220 }
221 
223 void
225 {
226  auto &&bone = myBones(index);
227  return bone.setRotateMatrix(m);
228 }
229 
231 const UT_Matrix3R&
233 {
234  return getBone(index).getRotateMatrix();
235 }
236 
237 #endif
238 
KIN_ChangeType
Definition: KIN_Chain.h:34
void setBoneRotates(int index, const fpreal *r)
Definition: KIN_Chain.h:187
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:166
Transformation order of scales, rotates, and translates.
Definition: UT_XformOrder.h:23
GA_API const UT_StringHolder rot
fpreal myTolerance
Definition: KIN_Chain.h:87
void setBoneRotateMatrix(int index, const UT_Matrix3R &m)
Definition: KIN_Chain.h:224
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
const KIN_Bone & getBone(int index) const
Definition: KIN_Chain.h:124
**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:217
#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:232
fpreal myTolerance
Definition: KIN_Chain.h:69
GA_API const UT_StringHolder parms
fpreal myTrackingThresholdFactor
Definition: KIN_Chain.h:100
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:113
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
int myResistStraight
Definition: KIN_Chain.h:101
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