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:
52 };
53 
54 // NOT IMPLEMENTED! DO NOT USE!
56 {
57 public:
58  fpreal myTolerance; // tolerance on solution
59 };
60 
61 // Common parameters for inverse kinematics related solvers
63 {
64 public:
70 };
71 
72 // "constraint" solver type for Inverse Kinematics (IK) with Constraints
74 {
75 public:
76  fpreal myTolerance; // tolerance on solution
77 };
78 
79 // "inverse" solver type for Inverse Kinematics (IK)
81 {
82 public:
84  {
85  myResistStraight = true;
86  myTrackingThresholdFactor = 1e-03;
87  }
88 
91 };
92 
94 {
95 public:
96 
97  KIN_Chain();
98  KIN_Chain(int num_bones);
99  KIN_Chain(const KIN_Chain &copy_from);
100  ~KIN_Chain();
101 
102  static const UT_XformOrder &getXformOrder() { return myXformOrder; }
103 
104  // These functions are for getting and setting bone information.
105  // All access to a chains bones must go through the chain so that we
106  // can make sure our solver is up to date. The separate updateBone,
107  // setBoneRotate, and setBoneRotates functions are only provided
108  // for convenience. Also notice that the Chain creator no longer
109  // has to actually create or remove any bones from the chain,
110  // only to set the number of bones.
111  int getNbones() const;
112  void setNbones(int bones);
113  const KIN_Bone &getBone(int index) const
114  { return myBones(index); }
115  void updateBone(int index, fpreal length, fpreal *rot,
116  fpreal damp, const UT_Matrix4R &xform,
117  const void *data);
118  void setBoneRotate(int index, int axis, fpreal r);
119  void setBoneRotates(int index, const fpreal *r);
120  void setBoneRotates(int index, const fpreal *deg, const fpreal *rads);
121  void getBoneRotates(int index, fpreal *r) const;
122 
123 
124  // The Curve Solver can output rotation matrices directly without conversion.
125  // This isn't used by the other solvers, as they are solving in rotation space.
126  void setBoneRotateMatrix(int index, const UT_Matrix3R &m);
127  const UT_Matrix3R & getBoneRotateMatrix(int index) const;
128 
129  // Currently we are limited to one constraint per bone, so the
130  // interface to the bone constraints can be very simple. Any constraint
131  // that gets passed in here will be deleted by the bone.
132  void setConstraint( int index, fpreal rest[3],
133  fpreal xrange[2], fpreal yrange[2],
134  fpreal zrange[2], fpreal damp[3],
135  fpreal roll[3] );
136  fpreal constrain( int index, int axis,
137  fpreal angle, fpreal step ) const;
138 
139  // Utility functions for getting transform information for each bone
140  // or for just the chain end position.
141  void getEndPosition(UT_Vector3R &result) const;
142  void getBoneStates(UT_Matrix4R *xforms) const;
143  fpreal getLength() const;
144 
145  // Functions for setting solver information for the chain
146  // The solver types are same tokens as found in the "Solver Type" parameter
147  // of the InverseKin CHOP. See the comments for the KIN_*Parm classes above.
148  int solve(const UT_StringHolder &type, const void *parms,
149  KIN_Chain &solution);
150 
151  // Essentially a copy operator
152  void copyFrom(const KIN_Chain &src);
153  void copySubChain(const KIN_Chain &src, int from, int to);
154 
155  int64 getMemoryUsage(bool inclusive) const
156  {
157  int64 mem = inclusive ? sizeof(*this) : 0;
158  mem += myBones.getMemoryUsage(false);
159  mem += mySolverType.getMemoryUsage(false);
160  return mem;
161  }
162 
163  void updateCurrentSolverRestChain();
164 private:
165  void initializeSolver();
166  void chainChanged(KIN_ChangeType type);
167 
168  UT_Array<KIN_Bone> myBones;
169  UT_UniquePtr<KIN_Solver> mySolver;
170  UT_StringHolder mySolverType;
171  static const UT_XformOrder myXformOrder;
172 };
173 
175 void
177 {
178  auto &&bone = myBones(index);
179  if( mySolver )
180  {
181  if( !SYSisEqual( r[0], bone.getRotate(0) ) ||
182  !SYSisEqual( r[1], bone.getRotate(1) ) ||
183  !SYSisEqual( r[2], bone.getRotate(2) ) )
184  chainChanged(BONE_ROTATE);
185  }
186  bone.setRotates(r);
187 }
188 
190 void
191 KIN_Chain::setBoneRotates(int index, const fpreal *r, const fpreal *rads)
192 {
193  auto &&bone = myBones(index);
194  if( mySolver )
195  {
196  if( !SYSisEqual( r[0], bone.getRotate(0) ) ||
197  !SYSisEqual( r[1], bone.getRotate(1) ) ||
198  !SYSisEqual( r[2], bone.getRotate(2) ) )
199  chainChanged(BONE_ROTATE);
200  }
201  bone.setRotates(r,rads);
202 }
203 
205 void
207 {
208  getBone(index).getRotates(r);
209 }
210 
212 void
214 {
215  auto &&bone = myBones(index);
216  return bone.setRotateMatrix(m);
217 }
218 
220 const UT_Matrix3R&
222 {
223  return getBone(index).getRotateMatrix();
224 }
225 
226 #endif
227 
KIN_ChangeType
Definition: KIN_Chain.h:34
void setBoneRotates(int index, const fpreal *r)
Definition: KIN_Chain.h:176
GLenum src
Definition: glew.h:2410
void getRotates(fpreal *r) const
Definition: KIN_Bone.h:40
GLuint index
Definition: glew.h:1814
int64 getMemoryUsage(bool inclusive) const
Definition: KIN_Chain.h:155
GA_API const UT_StringHolder rot
fpreal myTolerance
Definition: KIN_Chain.h:76
void setBoneRotateMatrix(int index, const UT_Matrix3R &m)
Definition: KIN_Chain.h:213
const GLdouble * m
Definition: glew.h:9124
const KIN_Bone & getBone(int index) const
Definition: KIN_Chain.h:113
GLdouble angle
Definition: glew.h:9135
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
UT_Vector3R myTangent
Definition: KIN_Chain.h:49
fpreal myTwist
Definition: KIN_Chain.h:68
bool myOrientToNormalsFlag
Definition: KIN_Chain.h:48
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
fpreal myDampen
Definition: KIN_Chain.h:69
void getBoneRotates(int index, fpreal *r) const
Definition: KIN_Chain.h:206
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLuint GLsizei GLsizei * length
Definition: glew.h:1825
long long int64
Definition: SYS_Types.h:116
const UT_Matrix3R & getBoneRotateMatrix(int index) const
Definition: KIN_Chain.h:221
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
fpreal myTolerance
Definition: KIN_Chain.h:58
fpreal myTrackingThresholdFactor
Definition: KIN_Chain.h:89
fpreal64 fpreal
Definition: SYS_Types.h:277
UT_Vector3RArray myPoints
Definition: KIN_Chain.h:50
UT_Vector3R myTwistAffectorPos
Definition: KIN_Chain.h:66
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
#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:102
GLuint64EXT * result
Definition: glew.h:14007
int myTwistAffectorFlag
Definition: KIN_Chain.h:67
UT_Vector3RArray myNormals
Definition: KIN_Chain.h:51
int myResistStraight
Definition: KIN_Chain.h:90
GA_API const UT_StringHolder rest
UT_Vector3R myEndAffectorPos
Definition: KIN_Chain.h:65
const UT_Matrix3R & getRotateMatrix() const
Definition: KIN_Bone.h:106
bool myClosedFlag
Definition: KIN_Chain.h:47