Blender  V2.93
itasc_plugin.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  * Original author: Benoit Bolsee
19  */
20 
25 #include <cmath>
26 #include <cstdlib>
27 #include <cstring>
28 #include <vector>
29 
30 /* iTaSC headers */
31 #ifdef WITH_IK_ITASC
32 # include "Armature.hpp"
33 # include "Cache.hpp"
34 # include "CopyPose.hpp"
35 # include "Distance.hpp"
36 # include "MovingFrame.hpp"
37 # include "Scene.hpp"
38 # include "WDLSSolver.hpp"
39 # include "WSDLSSolver.hpp"
40 #endif
41 
42 #include "MEM_guardedalloc.h"
43 
44 #include "BIK_api.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_utildefines.h"
48 
49 #include "BKE_action.h"
50 #include "BKE_armature.h"
51 #include "BKE_constraint.h"
52 #include "BKE_global.h"
53 #include "DNA_action_types.h"
54 #include "DNA_armature_types.h"
55 #include "DNA_constraint_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_scene_types.h"
58 
59 #include "itasc_plugin.h"
60 
61 /* default parameters */
63 
64 /* in case of animation mode, feedback and timestep is fixed */
65 // #define ANIM_TIMESTEP 1.0
66 #define ANIM_FEEDBACK 0.8
67 // #define ANIM_QMAX 0.52
68 
69 /* Structure pointed by bPose.ikdata
70  * It contains everything needed to simulate the armatures
71  * There can be several simulation islands independent to each other */
72 struct IK_Data {
73  struct IK_Scene *first;
74 };
75 
76 using Vector3 = float[3];
77 using Vector4 = float[4];
78 struct IK_Target;
79 using ErrorCallback = void (*)(const iTaSC::ConstraintValues *values,
80  unsigned int nvalues,
81  IK_Target *iktarget);
82 
83 /* one structure for each target in the scene */
84 struct IK_Target {
86  struct Scene *blscene;
91  Object *owner; /* for auto IK */
93  std::string targetName;
94  std::string constraintName;
95  unsigned short controlType;
96  short channel; /* index in IK channel array of channel on which this target is defined */
97  short ee; /* end effector number */
98  bool simulation; /* true when simulation mode is used (update feedback) */
99  bool eeBlend; /* end effector affected by enforce blending */
100  float eeRest[4][4]; /* end effector initial pose relative to armature */
101 
103  {
104  bldepsgraph = nullptr;
105  blscene = nullptr;
106  target = nullptr;
107  constraint = nullptr;
108  blenderConstraint = nullptr;
109  rootChannel = nullptr;
110  owner = nullptr;
111  controlType = 0;
112  channel = 0;
113  ee = 0;
114  eeBlend = true;
115  simulation = true;
116  targetName.reserve(32);
117  constraintName.reserve(32);
118  }
120  {
121  delete constraint;
122  delete target;
123  }
124 };
125 
126 struct IK_Channel {
127  bPoseChannel *pchan; /* channel where we must copy matrix back */
128  KDL::Frame frame; /* frame of the bone relative to object base, not armature base */
129  std::string tail; /* segment name of the joint from which we get the bone tail */
130  std::string head; /* segment name of the joint from which we get the bone head */
131  int parent; /* index in this array of the parent channel */
132  short jointType; /* type of joint, combination of IK_SegmentFlag */
133  char ndof; /* number of joint angles for this channel */
134  char jointValid; /* set to 1 when jointValue has been computed */
135  /* for joint constraint */
136  Object *owner; /* for pose and IK param */
137  double jointValue[4]; /* computed joint value */
138 
140  {
141  pchan = nullptr;
142  parent = -1;
143  jointType = 0;
144  ndof = 0;
145  jointValid = 0;
146  owner = nullptr;
147  jointValue[0] = 0.0;
148  jointValue[1] = 0.0;
149  jointValue[2] = 0.0;
150  jointValue[3] = 0.0;
151  }
152 };
153 
154 struct IK_Scene {
156  struct Scene *blscene;
158  int numchan; /* number of channel in pchan */
159  int numjoint; /* number of joint in jointArray */
160  /* array of bone information, one per channel in the tree */
165  iTaSC::MovingFrame *base; /* armature base object */
166  KDL::Frame baseFrame; /* frame of armature base relative to blArmature */
167  KDL::JntArray jointArray; /* buffer for storing temporary joint array */
170  float blScale; /* scale of the Armature object (assume uniform scaling) */
171  float blInvScale; /* inverse of Armature object scale */
173  std::vector<IK_Target *> targets;
174 
176  {
177  bldepsgraph = nullptr;
178  blscene = nullptr;
179  next = nullptr;
180  channels = nullptr;
181  armature = nullptr;
182  cache = nullptr;
183  scene = nullptr;
184  base = nullptr;
185  solver = nullptr;
186  blScale = blInvScale = 1.0f;
187  blArmature = nullptr;
188  numchan = 0;
189  numjoint = 0;
190  polarConstraint = nullptr;
191  }
192 
194  {
195  /* delete scene first */
196  delete scene;
197  for (IK_Target *target : targets) {
198  delete target;
199  }
200  targets.clear();
201  delete[] channels;
202  delete solver;
203  delete armature;
204  delete base;
205  /* delete cache last */
206  delete cache;
207  }
208 };
209 
210 /* type of IK joint, can be combined to list the joints corresponding to a bone */
212  IK_XDOF = 1,
213  IK_YDOF = 2,
214  IK_ZDOF = 4,
215  IK_SWING = 8,
217  IK_TRANSY = 32,
218 };
219 
221  IK_X = 0,
222  IK_Y = 1,
223  IK_Z = 2,
227 };
228 
229 static int initialize_chain(Object *ob, bPoseChannel *pchan_tip, bConstraint *con)
230 {
231  bPoseChannel *curchan, *pchan_root = nullptr, *chanlist[256], **oldchan;
232  PoseTree *tree;
233  PoseTarget *target;
235  int a, t, segcount = 0, size, newsize, *oldparent, parent, rootbone, treecount;
236 
237  data = (bKinematicConstraint *)con->data;
238 
239  /* exclude tip from chain? */
240  if (!(data->flag & CONSTRAINT_IK_TIP)) {
241  pchan_tip = pchan_tip->parent;
242  }
243 
244  rootbone = data->rootbone;
245  /* Find the chain's root & count the segments needed */
246  for (curchan = pchan_tip; curchan; curchan = curchan->parent) {
247  pchan_root = curchan;
248 
249  if (++segcount > 255) { /* 255 is weak */
250  break;
251  }
252 
253  if (segcount == rootbone) {
254  /* reached this end of the chain but if the chain is overlapping with a
255  * previous one, we must go back up to the root of the other chain */
256  if ((curchan->flag & POSE_CHAIN) && BLI_listbase_is_empty(&curchan->iktree)) {
257  rootbone++;
258  continue;
259  }
260  break;
261  }
262 
263  if (BLI_listbase_is_empty(&curchan->iktree) == false) {
264  /* Oh oh, there is already a chain starting from this channel and our chain is longer...
265  * Should handle this by moving the previous chain up to the beginning of our chain
266  * For now we just stop here */
267  break;
268  }
269  }
270  if (!segcount) {
271  return 0;
272  }
273  /* we reached a limit and still not the end of a previous chain, quit */
274  if ((pchan_root->flag & POSE_CHAIN) && BLI_listbase_is_empty(&pchan_root->iktree)) {
275  return 0;
276  }
277 
278  /* now that we know how many segment we have, set the flag */
279  for (rootbone = segcount, segcount = 0, curchan = pchan_tip; segcount < rootbone;
280  segcount++, curchan = curchan->parent) {
281  chanlist[segcount] = curchan;
282  curchan->flag |= POSE_CHAIN;
283  }
284 
285  /* setup the chain data */
286  /* create a target */
287  target = (PoseTarget *)MEM_callocN(sizeof(PoseTarget), "posetarget");
288  target->con = con;
289  /* by construction there can be only one tree per channel
290  * and each channel can be part of at most one tree. */
291  tree = (PoseTree *)pchan_root->iktree.first;
292 
293  if (tree == nullptr) {
294  /* make new tree */
295  tree = (PoseTree *)MEM_callocN(sizeof(PoseTree), "posetree");
296 
297  tree->iterations = data->iterations;
298  tree->totchannel = segcount;
299  tree->stretch = (data->flag & CONSTRAINT_IK_STRETCH);
300 
301  tree->pchan = (bPoseChannel **)MEM_callocN(segcount * sizeof(void *), "ik tree pchan");
302  tree->parent = (int *)MEM_callocN(segcount * sizeof(int), "ik tree parent");
303  for (a = 0; a < segcount; a++) {
304  tree->pchan[a] = chanlist[segcount - a - 1];
305  tree->parent[a] = a - 1;
306  }
307  target->tip = segcount - 1;
308 
309  /* AND! link the tree to the root */
310  BLI_addtail(&pchan_root->iktree, tree);
311  /* new tree */
312  treecount = 1;
313  }
314  else {
315  tree->iterations = MAX2(data->iterations, tree->iterations);
316  tree->stretch = tree->stretch && !(data->flag & CONSTRAINT_IK_STRETCH);
317 
318  /* skip common pose channels and add remaining*/
319  size = MIN2(segcount, tree->totchannel);
320  a = t = 0;
321  while (a < size && t < tree->totchannel) {
322  /* locate first matching channel */
323  for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) {
324  /* pass */
325  }
326  if (t >= tree->totchannel) {
327  break;
328  }
329  for (; a < size && t < tree->totchannel && tree->pchan[t] == chanlist[segcount - a - 1];
330  a++, t++) {
331  /* pass */
332  }
333  }
334 
335  segcount = segcount - a;
336  target->tip = tree->totchannel + segcount - 1;
337 
338  if (segcount > 0) {
339  for (parent = a - 1; parent < tree->totchannel; parent++) {
340  if (tree->pchan[parent] == chanlist[segcount - 1]->parent) {
341  break;
342  }
343  }
344 
345  /* shouldn't happen, but could with dependency cycles */
346  if (parent == tree->totchannel) {
347  parent = a - 1;
348  }
349 
350  /* resize array */
351  newsize = tree->totchannel + segcount;
352  oldchan = tree->pchan;
353  oldparent = tree->parent;
354 
355  tree->pchan = (bPoseChannel **)MEM_callocN(newsize * sizeof(void *), "ik tree pchan");
356  tree->parent = (int *)MEM_callocN(newsize * sizeof(int), "ik tree parent");
357  memcpy(tree->pchan, oldchan, sizeof(void *) * tree->totchannel);
358  memcpy(tree->parent, oldparent, sizeof(int) * tree->totchannel);
359  MEM_freeN(oldchan);
360  MEM_freeN(oldparent);
361 
362  /* add new pose channels at the end, in reverse order */
363  for (a = 0; a < segcount; a++) {
364  tree->pchan[tree->totchannel + a] = chanlist[segcount - a - 1];
365  tree->parent[tree->totchannel + a] = tree->totchannel + a - 1;
366  }
367  tree->parent[tree->totchannel] = parent;
368 
369  tree->totchannel = newsize;
370  }
371  /* reusing tree */
372  treecount = 0;
373  }
374 
375  /* add target to the tree */
376  BLI_addtail(&tree->targets, target);
377  /* mark root channel having an IK tree */
378  pchan_root->flag |= POSE_IKTREE;
379  return treecount;
380 }
381 
383 {
384  // bKinematicConstraint* data=(bKinematicConstraint *)con->data;
385 
386  return true;
387 }
388 
389 static bool constraint_valid(bConstraint *con)
390 {
392 
393  if (data->flag & CONSTRAINT_IK_AUTO) {
394  return true;
395  }
396  if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) {
397  return false;
398  }
399  if (is_cartesian_constraint(con)) {
400  /* cartesian space constraint */
401  if (data->tar == nullptr) {
402  return false;
403  }
404  if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) {
405  return false;
406  }
407  }
408  return true;
409 }
410 
411 static int initialize_scene(Object *ob, bPoseChannel *pchan_tip)
412 {
413  bConstraint *con;
414  int treecount;
415 
416  /* find all IK constraints and validate them */
417  treecount = 0;
418  for (con = (bConstraint *)pchan_tip->constraints.first; con; con = (bConstraint *)con->next) {
419  if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
420  if (constraint_valid(con)) {
421  treecount += initialize_chain(ob, pchan_tip, con);
422  }
423  }
424  }
425  return treecount;
426 }
427 
428 static IK_Data *get_ikdata(bPose *pose)
429 {
430  if (pose->ikdata) {
431  return (IK_Data *)pose->ikdata;
432  }
433  pose->ikdata = MEM_callocN(sizeof(IK_Data), "iTaSC ikdata");
434  /* here init ikdata if needed
435  * now that we have scene, make sure the default param are initialized */
436  if (!DefIKParam.iksolver) {
438  }
439 
440  return (IK_Data *)pose->ikdata;
441 }
442 static double EulerAngleFromMatrix(const KDL::Rotation &R, int axis)
443 {
444  double t = KDL::sqrt(R(0, 0) * R(0, 0) + R(0, 1) * R(0, 1));
445 
446  if (t > 16.0 * KDL::epsilon) {
447  if (axis == 0) {
448  return -KDL::atan2(R(1, 2), R(2, 2));
449  }
450  if (axis == 1) {
451  return KDL::atan2(-R(0, 2), t);
452  }
453 
454  return -KDL::atan2(R(0, 1), R(0, 0));
455  }
456 
457  if (axis == 0) {
458  return -KDL::atan2(-R(2, 1), R(1, 1));
459  }
460  if (axis == 1) {
461  return KDL::atan2(-R(0, 2), t);
462  }
463 
464  return 0.0f;
465 }
466 
467 static double ComputeTwist(const KDL::Rotation &R)
468 {
469  /* qy and qw are the y and w components of the quaternion from R */
470  double qy = R(0, 2) - R(2, 0);
471  double qw = R(0, 0) + R(1, 1) + R(2, 2) + 1;
472 
473  double tau = 2 * KDL::atan2(qy, qw);
474 
475  return tau;
476 }
477 
478 static void RemoveEulerAngleFromMatrix(KDL::Rotation &R, double angle, int axis)
479 {
480  /* compute twist parameter */
482  switch (axis) {
483  case 0:
485  break;
486  case 1:
488  break;
489  case 2:
491  break;
492  default:
493  return;
494  }
495  /* remove angle */
496  R = R * T;
497 }
498 
499 #if 0
500 static void GetEulerXZY(const KDL::Rotation &R, double &X, double &Z, double &Y)
501 {
502  if (fabs(R(0, 1)) > 1.0 - KDL::epsilon) {
503  X = -KDL::sign(R(0, 1)) * KDL::atan2(R(1, 2), R(1, 0));
504  Z = -KDL::sign(R(0, 1)) * KDL::PI / 2;
505  Y = 0.0;
506  }
507  else {
508  X = KDL::atan2(R(2, 1), R(1, 1));
509  Z = KDL::atan2(-R(0, 1), KDL::sqrt(KDL::sqr(R(0, 0)) + KDL::sqr(R(0, 2))));
510  Y = KDL::atan2(R(0, 2), R(0, 0));
511  }
512 }
513 
514 static void GetEulerXYZ(const KDL::Rotation &R, double &X, double &Y, double &Z)
515 {
516  if (fabs(R(0, 2)) > 1.0 - KDL::epsilon) {
517  X = KDL::sign(R(0, 2)) * KDL::atan2(-R(1, 0), R(1, 1));
518  Y = KDL::sign(R(0, 2)) * KDL::PI / 2;
519  Z = 0.0;
520  }
521  else {
522  X = KDL::atan2(-R(1, 2), R(2, 2));
523  Y = KDL::atan2(R(0, 2), KDL::sqrt(KDL::sqr(R(0, 0)) + KDL::sqr(R(0, 1))));
524  Z = KDL::atan2(-R(0, 1), R(0, 0));
525  }
526 }
527 #endif
528 
529 static void GetJointRotation(KDL::Rotation &boneRot, int type, double *rot)
530 {
531  switch (type & ~IK_TRANSY) {
532  default:
533  /* fixed bone, no joint */
534  break;
535  case IK_XDOF:
536  /* RX only, get the X rotation */
537  rot[0] = EulerAngleFromMatrix(boneRot, 0);
538  break;
539  case IK_YDOF:
540  /* RY only, get the Y rotation */
541  rot[0] = ComputeTwist(boneRot);
542  break;
543  case IK_ZDOF:
544  /* RZ only, get the Z rotation */
545  rot[0] = EulerAngleFromMatrix(boneRot, 2);
546  break;
547  case IK_XDOF | IK_YDOF:
548  rot[1] = ComputeTwist(boneRot);
549  RemoveEulerAngleFromMatrix(boneRot, rot[1], 1);
550  rot[0] = EulerAngleFromMatrix(boneRot, 0);
551  break;
552  case IK_SWING:
553  /* RX+RZ */
554  boneRot.GetXZRot().GetValue(rot);
555  break;
556  case IK_YDOF | IK_ZDOF:
557  /* RZ+RY */
558  rot[1] = ComputeTwist(boneRot);
559  RemoveEulerAngleFromMatrix(boneRot, rot[1], 1);
560  rot[0] = EulerAngleFromMatrix(boneRot, 2);
561  break;
562  case IK_SWING | IK_YDOF:
563  rot[2] = ComputeTwist(boneRot);
564  RemoveEulerAngleFromMatrix(boneRot, rot[2], 1);
565  boneRot.GetXZRot().GetValue(rot);
566  break;
567  case IK_REVOLUTE:
568  boneRot.GetRot().GetValue(rot);
569  break;
570  }
571 }
572 
573 static bool target_callback(const iTaSC::Timestamp &timestamp,
574  const iTaSC::Frame &current,
576  void *param)
577 {
578  IK_Target *target = (IK_Target *)param;
579  /* compute next target position
580  * get target matrix from constraint. */
581  bConstraint *constraint = (bConstraint *)target->blenderConstraint;
582  float tarmat[4][4];
583 
585  target->blscene,
586  constraint,
587  0,
589  target->owner,
590  tarmat,
591  1.0);
592 
593  /* rootmat contains the target pose in world coordinate
594  * if enforce is != 1.0, blend the target position with the end effector position
595  * if the armature was in rest position. This information is available in eeRest */
596  if (constraint->enforce != 1.0f && target->eeBlend) {
597  /* eeRest is relative to the reference frame of the IK root
598  * get this frame in world reference */
599  float restmat[4][4];
600  bPoseChannel *pchan = target->rootChannel;
601  if (pchan->parent) {
602  pchan = pchan->parent;
603  float chanmat[4][4];
604  copy_m4_m4(chanmat, pchan->pose_mat);
605  copy_v3_v3(chanmat[3], pchan->pose_tail);
606  mul_m4_series(restmat, target->owner->obmat, chanmat, target->eeRest);
607  }
608  else {
609  mul_m4_m4m4(restmat, target->owner->obmat, target->eeRest);
610  }
611  /* blend the target */
612  blend_m4_m4m4(tarmat, restmat, tarmat, constraint->enforce);
613  }
614  next.setValue(&tarmat[0][0]);
615  return true;
616 }
617 
618 static bool base_callback(const iTaSC::Timestamp &timestamp,
619  const iTaSC::Frame &current,
621  void *param)
622 {
623  IK_Scene *ikscene = (IK_Scene *)param;
624  /* compute next armature base pose
625  * algorithm:
626  * ikscene->pchan[0] is the root channel of the tree
627  * if it has a parent, get the pose matrix from it and replace [3] by parent pchan->tail
628  * then multiply by the armature matrix to get ikscene->armature base position */
629  bPoseChannel *pchan = ikscene->channels[0].pchan;
630  float rootmat[4][4];
631  if (pchan->parent) {
632  pchan = pchan->parent;
633  float chanmat[4][4];
634  copy_m4_m4(chanmat, pchan->pose_mat);
635  copy_v3_v3(chanmat[3], pchan->pose_tail);
636  /* save the base as a frame too so that we can compute deformation after simulation */
637  ikscene->baseFrame.setValue(&chanmat[0][0]);
638  /* iTaSC armature is scaled to object scale, scale the base frame too */
639  ikscene->baseFrame.p *= ikscene->blScale;
640  mul_m4_m4m4(rootmat, ikscene->blArmature->obmat, chanmat);
641  }
642  else {
643  copy_m4_m4(rootmat, ikscene->blArmature->obmat);
644  ikscene->baseFrame = iTaSC::F_identity;
645  }
646  next.setValue(&rootmat[0][0]);
647  /* if there is a polar target (only during solving otherwise we don't have end efffector) */
648  if (ikscene->polarConstraint && timestamp.update) {
649  /* compute additional rotation of base frame so that armature follows the polar target */
650  float imat[4][4]; /* IK tree base inverse matrix */
651  float polemat[4][4]; /* polar target in IK tree base frame */
652  float goalmat[4][4]; /* target in IK tree base frame */
653  float mat[4][4]; /* temp matrix */
655 
656  invert_m4_m4(imat, rootmat);
657  /* polar constraint imply only one target */
658  IK_Target *iktarget = ikscene->targets[0];
659  /* root channel from which we take the bone initial orientation */
660  IK_Channel &rootchan = ikscene->channels[0];
661 
662  /* get polar target matrix in world space */
664  ikscene->blscene,
665  ikscene->polarConstraint,
666  1,
668  ikscene->blArmature,
669  mat,
670  1.0);
671  /* convert to armature space */
672  mul_m4_m4m4(polemat, imat, mat);
673  /* get the target in world space
674  * (was computed before as target object are defined before base object). */
675  iktarget->target->getPose().getValue(mat[0]);
676  /* convert to armature space */
677  mul_m4_m4m4(goalmat, imat, mat);
678  /* take position of target, polar target, end effector, in armature space */
679  KDL::Vector goalpos(goalmat[3]);
680  KDL::Vector polepos(polemat[3]);
681  KDL::Vector endpos = ikscene->armature->getPose(iktarget->ee).p;
682  /* get root bone orientation */
683  KDL::Frame rootframe;
684  ikscene->armature->getRelativeFrame(rootframe, rootchan.tail);
685  KDL::Vector rootx = rootframe.M.UnitX();
686  KDL::Vector rootz = rootframe.M.UnitZ();
687  /* and compute root bone head */
688  double q_rest[3], q[3], length;
689  const KDL::Joint *joint;
690  const KDL::Frame *tip;
691  ikscene->armature->getSegment(rootchan.tail, 3, joint, q_rest[0], q[0], tip);
692  length = (joint->getType() == KDL::Joint::TransY) ? q[0] : tip->p(1);
693  KDL::Vector rootpos = rootframe.p - length * rootframe.M.UnitY();
694 
695  /* compute main directions */
696  KDL::Vector dir = KDL::Normalize(endpos - rootpos);
697  KDL::Vector poledir = KDL::Normalize(goalpos - rootpos);
698  /* compute up directions */
699  KDL::Vector poleup = KDL::Normalize(polepos - rootpos);
700  KDL::Vector up = rootx * KDL::cos(poledata->poleangle) + rootz * KDL::sin(poledata->poleangle);
701  /* from which we build rotation matrix */
702  KDL::Rotation endrot, polerot;
703  /* for the armature, using the root bone orientation */
704  KDL::Vector x = KDL::Normalize(dir * up);
705  endrot.UnitX(x);
706  endrot.UnitY(KDL::Normalize(x * dir));
707  endrot.UnitZ(-dir);
708  /* for the polar target */
709  x = KDL::Normalize(poledir * poleup);
710  polerot.UnitX(x);
711  polerot.UnitY(KDL::Normalize(x * poledir));
712  polerot.UnitZ(-poledir);
713  /* the difference between the two is the rotation we want to apply */
714  KDL::Rotation result(polerot * endrot.Inverse());
715  /* apply on base frame as this is an artificial additional rotation */
716  next.M = next.M * result;
717  ikscene->baseFrame.M = ikscene->baseFrame.M * result;
718  }
719  return true;
720 }
721 
722 static bool copypose_callback(const iTaSC::Timestamp &timestamp,
723  iTaSC::ConstraintValues *const _values,
724  unsigned int _nvalues,
725  void *_param)
726 {
727  IK_Target *iktarget = (IK_Target *)_param;
729  iTaSC::ConstraintValues *values = _values;
730  bItasc *ikparam = (bItasc *)iktarget->owner->pose->ikparam;
731 
732  /* we need default parameters */
733  if (!ikparam) {
734  ikparam = &DefIKParam;
735  }
736 
737  if (iktarget->blenderConstraint->flag & CONSTRAINT_OFF) {
738  if (iktarget->controlType & iTaSC::CopyPose::CTL_POSITION) {
739  values->alpha = 0.0;
740  values->action = iTaSC::ACT_ALPHA;
741  values++;
742  }
743  if (iktarget->controlType & iTaSC::CopyPose::CTL_ROTATION) {
744  values->alpha = 0.0;
745  values->action = iTaSC::ACT_ALPHA;
746  values++;
747  }
748  }
749  else {
750  if (iktarget->controlType & iTaSC::CopyPose::CTL_POSITION) {
751  /* update error */
752  values->alpha = condata->weight;
754  values->feedback = (iktarget->simulation) ? ikparam->feedback : ANIM_FEEDBACK;
755  values++;
756  }
757  if (iktarget->controlType & iTaSC::CopyPose::CTL_ROTATION) {
758  /* update error */
759  values->alpha = condata->orientweight;
761  values->feedback = (iktarget->simulation) ? ikparam->feedback : ANIM_FEEDBACK;
762  values++;
763  }
764  }
765  return true;
766 }
767 
768 static void copypose_error(const iTaSC::ConstraintValues *values,
769  unsigned int nvalues,
770  IK_Target *iktarget)
771 {
773  double error;
774  int i;
775 
776  if (iktarget->controlType & iTaSC::CopyPose::CTL_POSITION) {
777  /* update error */
778  for (i = 0, error = 0.0, value = values->values; i < values->number; i++, value++) {
779  error += KDL::sqr(value->y - value->yd);
780  }
782  values++;
783  }
784  if (iktarget->controlType & iTaSC::CopyPose::CTL_ROTATION) {
785  /* update error */
786  for (i = 0, error = 0.0, value = values->values; i < values->number; i++, value++) {
787  error += KDL::sqr(value->y - value->yd);
788  }
790  values++;
791  }
792 }
793 
794 static bool distance_callback(const iTaSC::Timestamp &timestamp,
795  iTaSC::ConstraintValues *const _values,
796  unsigned int _nvalues,
797  void *_param)
798 {
799  IK_Target *iktarget = (IK_Target *)_param;
801  iTaSC::ConstraintValues *values = _values;
802  bItasc *ikparam = (bItasc *)iktarget->owner->pose->ikparam;
803  /* we need default parameters */
804  if (!ikparam) {
805  ikparam = &DefIKParam;
806  }
807 
808  /* update weight according to mode */
809  if (iktarget->blenderConstraint->flag & CONSTRAINT_OFF) {
810  values->alpha = 0.0;
811  }
812  else {
813  switch (condata->mode) {
814  case LIMITDIST_INSIDE:
815  values->alpha = (values->values[0].y > condata->dist) ? condata->weight : 0.0;
816  break;
817  case LIMITDIST_OUTSIDE:
818  values->alpha = (values->values[0].y < condata->dist) ? condata->weight : 0.0;
819  break;
820  default:
821  values->alpha = condata->weight;
822  break;
823  }
824  if (!timestamp.substep) {
825  /* only update value on first timestep */
826  switch (condata->mode) {
827  case LIMITDIST_INSIDE:
828  values->values[0].yd = condata->dist * 0.95;
829  break;
830  case LIMITDIST_OUTSIDE:
831  values->values[0].yd = condata->dist * 1.05;
832  break;
833  default:
834  values->values[0].yd = condata->dist;
835  break;
836  }
838  values->feedback = (iktarget->simulation) ? ikparam->feedback : ANIM_FEEDBACK;
839  }
840  }
841  values->action |= iTaSC::ACT_ALPHA;
842  return true;
843 }
844 
845 static void distance_error(const iTaSC::ConstraintValues *values,
846  unsigned int _nvalues,
847  IK_Target *iktarget)
848 {
849  iktarget->blenderConstraint->lin_error = (float)(values->values[0].y - values->values[0].yd);
850 }
851 
852 static bool joint_callback(const iTaSC::Timestamp &timestamp,
853  iTaSC::ConstraintValues *const _values,
854  unsigned int _nvalues,
855  void *_param)
856 {
857  IK_Channel *ikchan = (IK_Channel *)_param;
858  bItasc *ikparam = (bItasc *)ikchan->owner->pose->ikparam;
859  bPoseChannel *chan = ikchan->pchan;
860  int dof;
861 
862  /* a channel can be split into multiple joints, so we get called multiple
863  * times for one channel (this callback is only for 1 joint in the armature)
864  * the IK_JointTarget structure is shared between multiple joint constraint
865  * and the target joint values is computed only once, remember this in jointValid
866  * Don't forget to reset it before each frame */
867  if (!ikchan->jointValid) {
868  float rmat[3][3];
869 
870  if (chan->rotmode > 0) {
871  /* Euler rotations (will cause gimbal lock, but this can be alleviated a bit with rotation
872  * orders) */
873  eulO_to_mat3(rmat, chan->eul, chan->rotmode);
874  }
875  else if (chan->rotmode == ROT_MODE_AXISANGLE) {
876  /* axis-angle - stored in quaternion data,
877  * but not really that great for 3D-changing orientations */
878  axis_angle_to_mat3(rmat, &chan->quat[1], chan->quat[0]);
879  }
880  else {
881  /* quats are normalized before use to eliminate scaling issues */
882  normalize_qt(chan->quat);
883  quat_to_mat3(rmat, chan->quat);
884  }
885  KDL::Rotation jointRot(rmat[0][0],
886  rmat[1][0],
887  rmat[2][0],
888  rmat[0][1],
889  rmat[1][1],
890  rmat[2][1],
891  rmat[0][2],
892  rmat[1][2],
893  rmat[2][2]);
894  GetJointRotation(jointRot, ikchan->jointType, ikchan->jointValue);
895  ikchan->jointValid = 1;
896  }
897  /* determine which part of jointValue is used for this joint
898  * closely related to the way the joints are defined */
899  switch (ikchan->jointType & ~IK_TRANSY) {
900  case IK_XDOF:
901  case IK_YDOF:
902  case IK_ZDOF:
903  dof = 0;
904  break;
905  case IK_XDOF | IK_YDOF:
906  /* X + Y */
907  dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RX) ? 0 : 1;
908  break;
909  case IK_SWING:
910  /* XZ */
911  dof = 0;
912  break;
913  case IK_YDOF | IK_ZDOF:
914  /* Z + Y */
915  dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RZ) ? 0 : 1;
916  break;
917  case IK_SWING | IK_YDOF:
918  /* XZ + Y */
919  dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RY) ? 2 : 0;
920  break;
921  case IK_REVOLUTE:
922  dof = 0;
923  break;
924  default:
925  dof = -1;
926  break;
927  }
928  if (dof >= 0) {
929  for (unsigned int i = 0; i < _nvalues; i++, dof++) {
930  _values[i].values[0].yd = ikchan->jointValue[dof];
931  _values[i].alpha = chan->ikrotweight;
932  _values[i].feedback = ikparam->feedback;
933  }
934  }
935  return true;
936 }
937 
938 /* build array of joint corresponding to IK chain */
940  IK_Scene *ikscene,
941  PoseTree *tree,
942  float ctime)
943 {
944  IK_Channel *ikchan;
945  bPoseChannel *pchan;
946  int a, flag, njoint;
947 
948  njoint = 0;
949  for (a = 0, ikchan = ikscene->channels; a < ikscene->numchan; a++, ikchan++) {
950  pchan = tree->pchan[a];
951  ikchan->pchan = pchan;
952  ikchan->parent = (a > 0) ? tree->parent[a] : -1;
953  ikchan->owner = ikscene->blArmature;
954 
955  /* the constraint and channels must be applied before we build the iTaSC scene,
956  * this is because some of the pose data (e.g. pose head) don't have corresponding
957  * joint angles and can't be applied to the iTaSC armature dynamically */
958  if (!(pchan->flag & POSE_DONE)) {
959  BKE_pose_where_is_bone(depsgraph, ikscene->blscene, ikscene->blArmature, pchan, ctime, true);
960  }
961  /* tell blender that this channel was controlled by IK,
962  * it's cleared on each BKE_pose_where_is() */
963  pchan->flag |= (POSE_DONE | POSE_CHAIN);
964 
965  /* set DoF flag */
966  flag = 0;
967  if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP) &&
968  (!(pchan->ikflag & BONE_IK_XLIMIT) || pchan->limitmin[0] < 0.0f ||
969  pchan->limitmax[0] > 0.0f)) {
970  flag |= IK_XDOF;
971  }
972  if (!(pchan->ikflag & BONE_IK_NO_YDOF) && !(pchan->ikflag & BONE_IK_NO_YDOF_TEMP) &&
973  (!(pchan->ikflag & BONE_IK_YLIMIT) || pchan->limitmin[1] < 0.0f ||
974  pchan->limitmax[1] > 0.0f)) {
975  flag |= IK_YDOF;
976  }
977  if (!(pchan->ikflag & BONE_IK_NO_ZDOF) && !(pchan->ikflag & BONE_IK_NO_ZDOF_TEMP) &&
978  (!(pchan->ikflag & BONE_IK_ZLIMIT) || pchan->limitmin[2] < 0.0f ||
979  pchan->limitmax[2] > 0.0f)) {
980  flag |= IK_ZDOF;
981  }
982 
983  if (tree->stretch && (pchan->ikstretch > 0.0)) {
984  flag |= IK_TRANSY;
985  }
986  /*
987  * Logic to create the segments:
988  * RX,RY,RZ = rotational joints with no length
989  * RY(tip) = rotational joints with a fixed length arm = (0,length,0)
990  * TY = translational joint on Y axis
991  * F(pos) = fixed joint with an arm at position pos
992  * Conversion rule of the above flags:
993  * - ==> F(tip)
994  * X ==> RX(tip)
995  * Y ==> RY(tip)
996  * Z ==> RZ(tip)
997  * XY ==> RX+RY(tip)
998  * XZ ==> RX+RZ(tip)
999  * YZ ==> RZ+RY(tip)
1000  * XYZ ==> full spherical unless there are limits, in which case RX+RZ+RY(tip)
1001  * In case of stretch, tip=(0,0,0) and there is an additional TY joint
1002  * The frame at last of these joints represents the tail of the bone.
1003  * The head is computed by a reverse translation on Y axis of the bone length
1004  * or in case of TY joint, by the frame at previous joint.
1005  * In case of separation of bones, there is an additional F(head) joint
1006  *
1007  * Computing rest pose and length is complicated: the solver works in world space
1008  * Here is the logic:
1009  * rest position is computed only from bone->bone_mat.
1010  * bone length is computed from bone->length multiplied by the scaling factor of
1011  * the armature. Non-uniform scaling will give bad result!
1012  */
1013  switch (flag & (IK_XDOF | IK_YDOF | IK_ZDOF)) {
1014  default:
1015  ikchan->jointType = 0;
1016  ikchan->ndof = 0;
1017  break;
1018  case IK_XDOF:
1019  /* RX only, get the X rotation */
1020  ikchan->jointType = IK_XDOF;
1021  ikchan->ndof = 1;
1022  break;
1023  case IK_YDOF:
1024  /* RY only, get the Y rotation */
1025  ikchan->jointType = IK_YDOF;
1026  ikchan->ndof = 1;
1027  break;
1028  case IK_ZDOF:
1029  /* RZ only, get the Zz rotation */
1030  ikchan->jointType = IK_ZDOF;
1031  ikchan->ndof = 1;
1032  break;
1033  case IK_XDOF | IK_YDOF:
1034  ikchan->jointType = IK_XDOF | IK_YDOF;
1035  ikchan->ndof = 2;
1036  break;
1037  case IK_XDOF | IK_ZDOF:
1038  /* RX+RZ */
1039  ikchan->jointType = IK_SWING;
1040  ikchan->ndof = 2;
1041  break;
1042  case IK_YDOF | IK_ZDOF:
1043  /* RZ+RY */
1044  ikchan->jointType = IK_ZDOF | IK_YDOF;
1045  ikchan->ndof = 2;
1046  break;
1047  case IK_XDOF | IK_YDOF | IK_ZDOF:
1048  /* spherical joint */
1049  if (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_YLIMIT | BONE_IK_ZLIMIT)) {
1050  /* decompose in a Swing+RotY joint */
1051  ikchan->jointType = IK_SWING | IK_YDOF;
1052  }
1053  else {
1054  ikchan->jointType = IK_REVOLUTE;
1055  }
1056  ikchan->ndof = 3;
1057  break;
1058  }
1059  if (flag & IK_TRANSY) {
1060  ikchan->jointType |= IK_TRANSY;
1061  ikchan->ndof++;
1062  }
1063  njoint += ikchan->ndof;
1064  }
1065  /* njoint is the joint coordinate, create the Joint Array */
1066  ikscene->jointArray.resize(njoint);
1067  ikscene->numjoint = njoint;
1068  return njoint;
1069 }
1070 
1071 /* compute array of joint value corresponding to current pose */
1072 static void convert_pose(IK_Scene *ikscene)
1073 {
1074  KDL::Rotation boneRot;
1075  bPoseChannel *pchan;
1076  IK_Channel *ikchan;
1077  Bone *bone;
1078  float rmat[4][4]; /* rest pose of bone with parent taken into account */
1079  float bmat[4][4]; /* difference */
1080  float scale;
1081  double *rot;
1082  int a, joint;
1083 
1084  /* assume uniform scaling and take Y scale as general scale for the armature */
1085  scale = len_v3(ikscene->blArmature->obmat[1]);
1086  rot = ikscene->jointArray(0);
1087  for (joint = a = 0, ikchan = ikscene->channels;
1088  a < ikscene->numchan && joint < ikscene->numjoint;
1089  a++, ikchan++) {
1090  pchan = ikchan->pchan;
1091  bone = pchan->bone;
1092 
1093  if (pchan->parent) {
1094  unit_m4(bmat);
1095  mul_m4_m4m3(bmat, pchan->parent->pose_mat, bone->bone_mat);
1096  }
1097  else {
1098  copy_m4_m4(bmat, bone->arm_mat);
1099  }
1100  invert_m4_m4(rmat, bmat);
1101  mul_m4_m4m4(bmat, rmat, pchan->pose_mat);
1102  normalize_m4(bmat);
1103  boneRot.setValue(bmat[0]);
1104  GetJointRotation(boneRot, ikchan->jointType, rot);
1105  if (ikchan->jointType & IK_TRANSY) {
1106  /* compute actual length */
1107  rot[ikchan->ndof - 1] = len_v3v3(pchan->pose_tail, pchan->pose_head) * scale;
1108  }
1109  rot += ikchan->ndof;
1110  joint += ikchan->ndof;
1111  }
1112 }
1113 
1114 /* compute array of joint value corresponding to current pose */
1115 static void BKE_pose_rest(IK_Scene *ikscene)
1116 {
1117  bPoseChannel *pchan;
1118  IK_Channel *ikchan;
1119  Bone *bone;
1120  float scale;
1121  double *rot;
1122  int a, joint;
1123 
1124  /* assume uniform scaling and take Y scale as general scale for the armature */
1125  scale = len_v3(ikscene->blArmature->obmat[1]);
1126  /* rest pose is 0 */
1127  SetToZero(ikscene->jointArray);
1128  /* except for transY joints */
1129  rot = ikscene->jointArray(0);
1130  for (joint = a = 0, ikchan = ikscene->channels;
1131  a < ikscene->numchan && joint < ikscene->numjoint;
1132  a++, ikchan++) {
1133  pchan = ikchan->pchan;
1134  bone = pchan->bone;
1135 
1136  if (ikchan->jointType & IK_TRANSY) {
1137  rot[ikchan->ndof - 1] = bone->length * scale;
1138  }
1139  rot += ikchan->ndof;
1140  joint += ikchan->ndof;
1141  }
1142 }
1143 
1145  struct Depsgraph *depsgraph, Scene *blscene, Object *ob, bPoseChannel *pchan, float ctime)
1146 {
1147  PoseTree *tree = (PoseTree *)pchan->iktree.first;
1148  PoseTarget *target;
1149  bKinematicConstraint *condata;
1150  bConstraint *polarcon;
1151  bItasc *ikparam;
1152  iTaSC::Armature *arm;
1154  IK_Scene *ikscene;
1155  IK_Channel *ikchan;
1156  KDL::Frame initPose;
1157  Bone *bone;
1158  int a, numtarget;
1159  unsigned int t;
1160  float length;
1161  bool ret = true;
1162  double *rot;
1163  float start[3];
1164 
1165  if (tree->totchannel == 0) {
1166  return nullptr;
1167  }
1168 
1169  ikscene = new IK_Scene;
1170  ikscene->blscene = blscene;
1171  ikscene->bldepsgraph = depsgraph;
1172  arm = new iTaSC::Armature();
1173  scene = new iTaSC::Scene();
1174  ikscene->channels = new IK_Channel[tree->totchannel];
1175  ikscene->numchan = tree->totchannel;
1176  ikscene->armature = arm;
1177  ikscene->scene = scene;
1178  ikparam = (bItasc *)ob->pose->ikparam;
1179 
1180  if (!ikparam) {
1181  /* you must have our own copy */
1182  ikparam = &DefIKParam;
1183  }
1184 
1185  if (ikparam->flag & ITASC_SIMULATION) {
1186  /* no cache in animation mode */
1187  ikscene->cache = new iTaSC::Cache();
1188  }
1189 
1190  switch (ikparam->solver) {
1191  case ITASC_SOLVER_SDLS:
1192  ikscene->solver = new iTaSC::WSDLSSolver();
1193  break;
1194  case ITASC_SOLVER_DLS:
1195  ikscene->solver = new iTaSC::WDLSSolver();
1196  break;
1197  default:
1198  delete ikscene;
1199  return nullptr;
1200  }
1201  ikscene->blArmature = ob;
1202  /* assume uniform scaling and take Y scale as general scale for the armature */
1203  ikscene->blScale = len_v3(ob->obmat[1]);
1204  ikscene->blInvScale = (ikscene->blScale < KDL::epsilon) ? 0.0f : 1.0f / ikscene->blScale;
1205 
1206  std::string joint;
1207  std::string root("root");
1208  std::string parent;
1209  std::vector<double> weights;
1210  double weight[3];
1211  /* build the array of joints corresponding to the IK chain */
1212  convert_channels(depsgraph, ikscene, tree, ctime);
1213  /* in Blender, the rest pose is always 0 for joints */
1214  BKE_pose_rest(ikscene);
1215  rot = ikscene->jointArray(0);
1216 
1217  for (a = 0, ikchan = ikscene->channels; a < tree->totchannel; a++, ikchan++) {
1218  pchan = ikchan->pchan;
1219  bone = pchan->bone;
1220 
1222  /* compute the position and rotation of the head from previous segment */
1223  Vector3 *fl = bone->bone_mat;
1224  KDL::Rotation brot(
1225  fl[0][0], fl[1][0], fl[2][0], fl[0][1], fl[1][1], fl[2][1], fl[0][2], fl[1][2], fl[2][2]);
1226  /* if the bone is disconnected, the head is movable in pose mode
1227  * take that into account by using pose matrix instead of bone
1228  * Note that pose is expressed in armature space, convert to previous bone space */
1229  {
1230  float R_parmat[3][3];
1231  float iR_parmat[3][3];
1232  if (pchan->parent) {
1233  copy_m3_m4(R_parmat, pchan->parent->pose_mat);
1234  }
1235  else {
1236  unit_m3(R_parmat);
1237  }
1238  if (pchan->parent) {
1239  sub_v3_v3v3(start, pchan->pose_head, pchan->parent->pose_tail);
1240  }
1241  else {
1242  start[0] = start[1] = start[2] = 0.0f;
1243  }
1244  invert_m3_m3(iR_parmat, R_parmat);
1245  normalize_m3(iR_parmat);
1246  mul_m3_v3(iR_parmat, start);
1247  }
1248  KDL::Vector bpos(start[0], start[1], start[2]);
1249  bpos *= ikscene->blScale;
1250  KDL::Frame head(brot, bpos);
1251 
1252  /* rest pose length of the bone taking scaling into account */
1253  length = bone->length * ikscene->blScale;
1254  parent = (a > 0) ? ikscene->channels[tree->parent[a]].tail : root;
1255  /* first the fixed segment to the bone head */
1256  if (!(ikchan->pchan->bone->flag & BONE_CONNECTED) || head.M.GetRot().Norm() > KDL::epsilon) {
1257  joint = bone->name;
1258  joint += ":H";
1259  ret = arm->addSegment(joint, parent, KDL::Joint::None, 0.0, head);
1260  parent = joint;
1261  }
1262  if (!(ikchan->jointType & IK_TRANSY)) {
1263  /* fixed length, put it in tip */
1264  tip.p[1] = length;
1265  }
1266  joint = bone->name;
1267  weight[0] = (1.0 - pchan->stiffness[0]);
1268  weight[1] = (1.0 - pchan->stiffness[1]);
1269  weight[2] = (1.0 - pchan->stiffness[2]);
1270  switch (ikchan->jointType & ~IK_TRANSY) {
1271  case 0:
1272  /* fixed bone */
1273  joint += ":F";
1274  ret = arm->addSegment(joint, parent, KDL::Joint::None, 0.0, tip);
1275  break;
1276  case IK_XDOF:
1277  /* RX only, get the X rotation */
1278  joint += ":RX";
1279  ret = arm->addSegment(joint, parent, KDL::Joint::RotX, rot[0], tip);
1280  weights.push_back(weight[0]);
1281  break;
1282  case IK_YDOF:
1283  /* RY only, get the Y rotation */
1284  joint += ":RY";
1285  ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[0], tip);
1286  weights.push_back(weight[1]);
1287  break;
1288  case IK_ZDOF:
1289  /* RZ only, get the Zz rotation */
1290  joint += ":RZ";
1291  ret = arm->addSegment(joint, parent, KDL::Joint::RotZ, rot[0], tip);
1292  weights.push_back(weight[2]);
1293  break;
1294  case IK_XDOF | IK_YDOF:
1295  joint += ":RX";
1296  ret = arm->addSegment(joint, parent, KDL::Joint::RotX, rot[0]);
1297  weights.push_back(weight[0]);
1298  if (ret) {
1299  parent = joint;
1300  joint = bone->name;
1301  joint += ":RY";
1302  ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[1], tip);
1303  weights.push_back(weight[1]);
1304  }
1305  break;
1306  case IK_SWING:
1307  joint += ":SW";
1308  ret = arm->addSegment(joint, parent, KDL::Joint::Swing, rot[0], tip);
1309  weights.push_back(weight[0]);
1310  weights.push_back(weight[2]);
1311  break;
1312  case IK_YDOF | IK_ZDOF:
1313  /* RZ+RY */
1314  joint += ":RZ";
1315  ret = arm->addSegment(joint, parent, KDL::Joint::RotZ, rot[0]);
1316  weights.push_back(weight[2]);
1317  if (ret) {
1318  parent = joint;
1319  joint = bone->name;
1320  joint += ":RY";
1321  ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[1], tip);
1322  weights.push_back(weight[1]);
1323  }
1324  break;
1325  case IK_SWING | IK_YDOF:
1326  /* decompose in a Swing+RotY joint */
1327  joint += ":SW";
1328  ret = arm->addSegment(joint, parent, KDL::Joint::Swing, rot[0]);
1329  weights.push_back(weight[0]);
1330  weights.push_back(weight[2]);
1331  if (ret) {
1332  parent = joint;
1333  joint = bone->name;
1334  joint += ":RY";
1335  ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[2], tip);
1336  weights.push_back(weight[1]);
1337  }
1338  break;
1339  case IK_REVOLUTE:
1340  joint += ":SJ";
1341  ret = arm->addSegment(joint, parent, KDL::Joint::Sphere, rot[0], tip);
1342  weights.push_back(weight[0]);
1343  weights.push_back(weight[1]);
1344  weights.push_back(weight[2]);
1345  break;
1346  }
1347  if (ret && (ikchan->jointType & IK_TRANSY)) {
1348  parent = joint;
1349  joint = bone->name;
1350  joint += ":TY";
1351  ret = arm->addSegment(joint, parent, KDL::Joint::TransY, rot[ikchan->ndof - 1]);
1352  const float ikstretch = pchan->ikstretch * pchan->ikstretch;
1353  /* why invert twice here? */
1354  weight[1] = (1.0 - min_ff(1.0 - ikstretch, 1.0f - 0.001f));
1355  weights.push_back(weight[1]);
1356  }
1357  if (!ret) {
1358  /* error making the armature?? */
1359  break;
1360  }
1361  /* joint points to the segment that correspond to the bone per say */
1362  ikchan->tail = joint;
1363  ikchan->head = parent;
1364  /* in case of error */
1365  ret = false;
1366  if ((ikchan->jointType & IK_XDOF) && (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ROTCTL))) {
1367  joint = bone->name;
1368  joint += ":RX";
1369  if (pchan->ikflag & BONE_IK_XLIMIT) {
1370  if (arm->addLimitConstraint(joint, 0, pchan->limitmin[0], pchan->limitmax[0]) < 0) {
1371  break;
1372  }
1373  }
1374  if (pchan->ikflag & BONE_IK_ROTCTL) {
1375  if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) {
1376  break;
1377  }
1378  }
1379  }
1380  if ((ikchan->jointType & IK_YDOF) && (pchan->ikflag & (BONE_IK_YLIMIT | BONE_IK_ROTCTL))) {
1381  joint = bone->name;
1382  joint += ":RY";
1383  if (pchan->ikflag & BONE_IK_YLIMIT) {
1384  if (arm->addLimitConstraint(joint, 0, pchan->limitmin[1], pchan->limitmax[1]) < 0) {
1385  break;
1386  }
1387  }
1388  if (pchan->ikflag & BONE_IK_ROTCTL) {
1389  if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) {
1390  break;
1391  }
1392  }
1393  }
1394  if ((ikchan->jointType & IK_ZDOF) && (pchan->ikflag & (BONE_IK_ZLIMIT | BONE_IK_ROTCTL))) {
1395  joint = bone->name;
1396  joint += ":RZ";
1397  if (pchan->ikflag & BONE_IK_ZLIMIT) {
1398  if (arm->addLimitConstraint(joint, 0, pchan->limitmin[2], pchan->limitmax[2]) < 0) {
1399  break;
1400  }
1401  }
1402  if (pchan->ikflag & BONE_IK_ROTCTL) {
1403  if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) {
1404  break;
1405  }
1406  }
1407  }
1408  if ((ikchan->jointType & IK_SWING) &&
1410  joint = bone->name;
1411  joint += ":SW";
1412  if (pchan->ikflag & BONE_IK_XLIMIT) {
1413  if (arm->addLimitConstraint(joint, 0, pchan->limitmin[0], pchan->limitmax[0]) < 0) {
1414  break;
1415  }
1416  }
1417  if (pchan->ikflag & BONE_IK_ZLIMIT) {
1418  if (arm->addLimitConstraint(joint, 1, pchan->limitmin[2], pchan->limitmax[2]) < 0) {
1419  break;
1420  }
1421  }
1422  if (pchan->ikflag & BONE_IK_ROTCTL) {
1423  if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) {
1424  break;
1425  }
1426  }
1427  }
1428  if ((ikchan->jointType & IK_REVOLUTE) && (pchan->ikflag & BONE_IK_ROTCTL)) {
1429  joint = bone->name;
1430  joint += ":SJ";
1431  if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) {
1432  break;
1433  }
1434  }
1435  /* no error, so restore */
1436  ret = true;
1437  rot += ikchan->ndof;
1438  }
1439  if (!ret) {
1440  delete ikscene;
1441  return nullptr;
1442  }
1443  /* for each target, we need to add an end effector in the armature */
1444  for (numtarget = 0, polarcon = nullptr, ret = true, target = (PoseTarget *)tree->targets.first;
1445  target;
1446  target = (PoseTarget *)target->next) {
1447  condata = (bKinematicConstraint *)target->con->data;
1448  pchan = tree->pchan[target->tip];
1449 
1450  if (is_cartesian_constraint(target->con)) {
1451  /* add the end effector */
1452  IK_Target *iktarget = new IK_Target();
1453  ikscene->targets.push_back(iktarget);
1454  iktarget->ee = arm->addEndEffector(ikscene->channels[target->tip].tail);
1455  if (iktarget->ee == -1) {
1456  ret = false;
1457  break;
1458  }
1459  /* initialize all the fields that we can set at this time */
1460  iktarget->blenderConstraint = target->con;
1461  iktarget->channel = target->tip;
1462  iktarget->simulation = (ikparam->flag & ITASC_SIMULATION);
1463  iktarget->rootChannel = ikscene->channels[0].pchan;
1464  iktarget->owner = ob;
1465  iktarget->targetName = pchan->bone->name;
1466  iktarget->targetName += ":T:";
1467  iktarget->targetName += target->con->name;
1468  iktarget->constraintName = pchan->bone->name;
1469  iktarget->constraintName += ":C:";
1470  iktarget->constraintName += target->con->name;
1471  numtarget++;
1472  if (condata->poletar) {
1473  /* this constraint has a polar target */
1474  polarcon = target->con;
1475  }
1476  }
1477  }
1478  /* deal with polar target if any */
1479  if (numtarget == 1 && polarcon) {
1480  ikscene->polarConstraint = polarcon;
1481  }
1482  /* we can now add the armature
1483  * the armature is based on a moving frame.
1484  * initialize with the correct position in case there is no cache */
1485  base_callback(iTaSC::Timestamp(), iTaSC::F_identity, initPose, ikscene);
1486  ikscene->base = new iTaSC::MovingFrame(initPose);
1487  ikscene->base->setCallback(base_callback, ikscene);
1488  std::string armname;
1489  armname = ob->id.name;
1490  armname += ":B";
1491  ret = scene->addObject(armname, ikscene->base);
1492  armname = ob->id.name;
1493  armname += ":AR";
1494  if (ret) {
1495  ret = scene->addObject(armname, ikscene->armature, ikscene->base);
1496  }
1497  if (!ret) {
1498  delete ikscene;
1499  return nullptr;
1500  }
1501  /* set the weight */
1502  e_matrix &Wq = arm->getWq();
1503  assert(Wq.cols() == (int)weights.size());
1504  for (int q = 0; q < Wq.cols(); q++) {
1505  Wq(q, q) = weights[q];
1506  }
1507  /* get the inverse rest pose frame of the base to compute relative rest pose of end effectors
1508  * this is needed to handle the enforce parameter
1509  * ikscene->pchan[0] is the root channel of the tree
1510  * if it has no parent, then it's just the identify Frame */
1511  float invBaseFrame[4][4];
1512  pchan = ikscene->channels[0].pchan;
1513  if (pchan->parent) {
1514  /* it has a parent, get the pose matrix from it */
1515  float baseFrame[4][4];
1516  pchan = pchan->parent;
1517  copy_m4_m4(baseFrame, pchan->bone->arm_mat);
1518  /* move to the tail and scale to get rest pose of armature base */
1519  copy_v3_v3(baseFrame[3], pchan->bone->arm_tail);
1520  invert_m4_m4(invBaseFrame, baseFrame);
1521  }
1522  else {
1523  unit_m4(invBaseFrame);
1524  }
1525  /* finally add the constraint */
1526  for (t = 0; t < ikscene->targets.size(); t++) {
1527  IK_Target *iktarget = ikscene->targets[t];
1528  iktarget->blscene = blscene;
1529  iktarget->bldepsgraph = depsgraph;
1530  condata = (bKinematicConstraint *)iktarget->blenderConstraint->data;
1531  pchan = tree->pchan[iktarget->channel];
1532  unsigned int controltype, bonecnt;
1533  double bonelen;
1534  float mat[4][4];
1535 
1536  /* add the end effector
1537  * estimate the average bone length, used to clamp feedback error */
1538  for (bonecnt = 0, bonelen = 0.0f, a = iktarget->channel; a >= 0;
1539  a = tree->parent[a], bonecnt++) {
1540  bonelen += ikscene->blScale * tree->pchan[a]->bone->length;
1541  }
1542  bonelen /= bonecnt;
1543 
1544  /* store the rest pose of the end effector to compute enforce target */
1545  copy_m4_m4(mat, pchan->bone->arm_mat);
1546  copy_v3_v3(mat[3], pchan->bone->arm_tail);
1547  /* get the rest pose relative to the armature base */
1548  mul_m4_m4m4(iktarget->eeRest, invBaseFrame, mat);
1549  iktarget->eeBlend = (!ikscene->polarConstraint && condata->type == CONSTRAINT_IK_COPYPOSE) ?
1550  true :
1551  false;
1552  /* use target_callback to make sure the initPose includes enforce coefficient */
1553  target_callback(iTaSC::Timestamp(), iTaSC::F_identity, initPose, iktarget);
1554  iktarget->target = new iTaSC::MovingFrame(initPose);
1555  iktarget->target->setCallback(target_callback, iktarget);
1556  ret = scene->addObject(iktarget->targetName, iktarget->target);
1557  if (!ret) {
1558  break;
1559  }
1560 
1561  switch (condata->type) {
1563  controltype = 0;
1564  if (condata->flag & CONSTRAINT_IK_ROT) {
1565  if (!(condata->flag & CONSTRAINT_IK_NO_ROT_X)) {
1566  controltype |= iTaSC::CopyPose::CTL_ROTATIONX;
1567  }
1568  if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Y)) {
1569  controltype |= iTaSC::CopyPose::CTL_ROTATIONY;
1570  }
1571  if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Z)) {
1572  controltype |= iTaSC::CopyPose::CTL_ROTATIONZ;
1573  }
1574  }
1575  if (condata->flag & CONSTRAINT_IK_POS) {
1576  if (!(condata->flag & CONSTRAINT_IK_NO_POS_X)) {
1577  controltype |= iTaSC::CopyPose::CTL_POSITIONX;
1578  }
1579  if (!(condata->flag & CONSTRAINT_IK_NO_POS_Y)) {
1580  controltype |= iTaSC::CopyPose::CTL_POSITIONY;
1581  }
1582  if (!(condata->flag & CONSTRAINT_IK_NO_POS_Z)) {
1583  controltype |= iTaSC::CopyPose::CTL_POSITIONZ;
1584  }
1585  }
1586  if (controltype) {
1587  iktarget->constraint = new iTaSC::CopyPose(controltype, controltype, bonelen);
1588  /* set the gain */
1589  if (controltype & iTaSC::CopyPose::CTL_POSITION) {
1590  iktarget->constraint->setControlParameter(
1592  }
1593  if (controltype & iTaSC::CopyPose::CTL_ROTATION) {
1594  iktarget->constraint->setControlParameter(
1595  iTaSC::CopyPose::ID_ROTATION, iTaSC::ACT_ALPHA, condata->orientweight);
1596  }
1597  iktarget->constraint->registerCallback(copypose_callback, iktarget);
1598  iktarget->errorCallback = copypose_error;
1599  iktarget->controlType = controltype;
1600  /* add the constraint */
1601  if (condata->flag & CONSTRAINT_IK_TARGETAXIS) {
1602  ret = scene->addConstraintSet(iktarget->constraintName,
1603  iktarget->constraint,
1604  iktarget->targetName,
1605  armname,
1606  "",
1607  ikscene->channels[iktarget->channel].tail);
1608  }
1609  else {
1610  ret = scene->addConstraintSet(iktarget->constraintName,
1611  iktarget->constraint,
1612  armname,
1613  iktarget->targetName,
1614  ikscene->channels[iktarget->channel].tail);
1615  }
1616  }
1617  break;
1619  iktarget->constraint = new iTaSC::Distance(bonelen);
1620  iktarget->constraint->setControlParameter(
1622  iktarget->constraint->registerCallback(distance_callback, iktarget);
1623  iktarget->errorCallback = distance_error;
1624  /* we can update the weight on each substep */
1625  iktarget->constraint->substep(true);
1626  /* add the constraint */
1627  ret = scene->addConstraintSet(iktarget->constraintName,
1628  iktarget->constraint,
1629  armname,
1630  iktarget->targetName,
1631  ikscene->channels[iktarget->channel].tail);
1632  break;
1633  }
1634  if (!ret) {
1635  break;
1636  }
1637  }
1638  if (!ret || !scene->addCache(ikscene->cache) || !scene->addSolver(ikscene->solver) ||
1639  !scene->initialize()) {
1640  delete ikscene;
1641  ikscene = nullptr;
1642  }
1643  return ikscene;
1644 }
1645 
1646 static void create_scene(struct Depsgraph *depsgraph, Scene *scene, Object *ob, float ctime)
1647 {
1648  bPoseChannel *pchan;
1649 
1650  /* create the IK scene */
1651  for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan;
1652  pchan = (bPoseChannel *)pchan->next) {
1653  /* by construction there is only one tree */
1654  PoseTree *tree = (PoseTree *)pchan->iktree.first;
1655  if (tree) {
1656  IK_Data *ikdata = get_ikdata(ob->pose);
1657  /* convert tree in iTaSC::Scene */
1658  IK_Scene *ikscene = convert_tree(depsgraph, scene, ob, pchan, ctime);
1659  if (ikscene) {
1660  ikscene->next = ikdata->first;
1661  ikdata->first = ikscene;
1662  }
1663  /* delete the trees once we are done */
1664  while (tree) {
1665  BLI_remlink(&pchan->iktree, tree);
1666  BLI_freelistN(&tree->targets);
1667  if (tree->pchan) {
1668  MEM_freeN(tree->pchan);
1669  }
1670  if (tree->parent) {
1671  MEM_freeN(tree->parent);
1672  }
1673  if (tree->basis_change) {
1674  MEM_freeN(tree->basis_change);
1675  }
1676  MEM_freeN(tree);
1677  tree = (PoseTree *)pchan->iktree.first;
1678  }
1679  }
1680  }
1681 }
1682 
1683 /* returns 1 if scaling has changed and tree must be reinitialized */
1684 static int init_scene(Object *ob)
1685 {
1686  /* check also if scaling has changed */
1687  float scale = len_v3(ob->obmat[1]);
1688  IK_Scene *scene;
1689 
1690  if (ob->pose->ikdata) {
1691  for (scene = ((IK_Data *)ob->pose->ikdata)->first; scene != nullptr; scene = scene->next) {
1692  if (fabs(scene->blScale - scale) > KDL::epsilon) {
1693  return 1;
1694  }
1695  scene->channels[0].pchan->flag |= POSE_IKTREE;
1696  }
1697  }
1698  return 0;
1699 }
1700 
1701 static void execute_scene(struct Depsgraph *depsgraph,
1702  Scene *blscene,
1703  IK_Scene *ikscene,
1704  bItasc *ikparam,
1705  float ctime,
1706  float frtime)
1707 {
1708  int i;
1709  IK_Channel *ikchan;
1710  if (ikparam->flag & ITASC_SIMULATION) {
1711  for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; i++, ikchan++) {
1712  /* In simulation mode we don't allow external constraint to change our bones,
1713  * mark the channel done also tell Blender that this channel is part of IK tree.
1714  * Cleared on each BKE_pose_where_is() */
1715  ikchan->pchan->flag |= (POSE_DONE | POSE_CHAIN);
1716  ikchan->jointValid = 0;
1717  }
1718  }
1719  else {
1720  /* in animation mode, we must get the bone position from action and constraints */
1721  for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; i++, ikchan++) {
1722  if (!(ikchan->pchan->flag & POSE_DONE)) {
1724  depsgraph, blscene, ikscene->blArmature, ikchan->pchan, ctime, true);
1725  }
1726  /* tell blender that this channel was controlled by IK,
1727  * it's cleared on each BKE_pose_where_is() */
1728  ikchan->pchan->flag |= (POSE_DONE | POSE_CHAIN);
1729  ikchan->jointValid = 0;
1730  }
1731  }
1732  /* only run execute the scene if at least one of our target is enabled */
1733  for (i = ikscene->targets.size(); i > 0; i--) {
1734  IK_Target *iktarget = ikscene->targets[i - 1];
1735  if (!(iktarget->blenderConstraint->flag & CONSTRAINT_OFF)) {
1736  break;
1737  }
1738  }
1739  if (i == 0 && ikscene->armature->getNrOfConstraints() == 0) {
1740  /* all constraint disabled */
1741  return;
1742  }
1743 
1744  /* compute timestep */
1745  double timestamp = ctime * frtime + 2147483.648;
1746  double timestep = frtime;
1747  bool reiterate = (ikparam->flag & ITASC_REITERATION) ? true : false;
1748  int numstep = (ikparam->flag & ITASC_AUTO_STEP) ? 0 : ikparam->numstep;
1749  bool simulation = true;
1750 
1751  if (ikparam->flag & ITASC_SIMULATION) {
1752  ikscene->solver->setParam(iTaSC::Solver::DLS_QMAX, ikparam->maxvel);
1753  }
1754  else {
1755  /* in animation mode we start from the pose after action and constraint */
1756  convert_pose(ikscene);
1757  ikscene->armature->setJointArray(ikscene->jointArray);
1758  /* and we don't handle velocity */
1759  reiterate = true;
1760  simulation = false;
1761  /* time is virtual, so take fixed value for velocity parameters (see itasc_update_param)
1762  * and choose 1s timestep to allow having velocity parameters in radiant */
1763  timestep = 1.0;
1764  /* use auto setup to let the solver test the variation of the joints */
1765  numstep = 0;
1766  }
1767 
1768  if (ikscene->cache && !reiterate && simulation) {
1769  iTaSC::CacheTS sts, cts;
1770  sts = cts = (iTaSC::CacheTS)std::round(timestamp * 1000.0);
1771  if (ikscene->cache->getPreviousCacheItem(ikscene->armature, 0, &cts) == nullptr || cts == 0) {
1772  /* the cache is empty before this time, reiterate */
1773  if (ikparam->flag & ITASC_INITIAL_REITERATION) {
1774  reiterate = true;
1775  }
1776  }
1777  else {
1778  /* can take the cache as a start point. */
1779  sts -= cts;
1780  timestep = sts / 1000.0;
1781  }
1782  }
1783  /* don't cache if we are reiterating because we don't want to destroy the cache unnecessarily */
1784  ikscene->scene->update(timestamp, timestep, numstep, false, !reiterate, simulation);
1785  if (reiterate) {
1786  /* how many times do we reiterate? */
1787  for (i = 0; i < ikparam->numiter; i++) {
1788  if (ikscene->armature->getMaxJointChange() < ikparam->precision ||
1789  ikscene->armature->getMaxEndEffectorChange() < ikparam->precision) {
1790  break;
1791  }
1792  ikscene->scene->update(timestamp, timestep, numstep, true, false, simulation);
1793  }
1794  if (simulation) {
1795  /* one more fake iteration to cache */
1796  ikscene->scene->update(timestamp, 0.0, 1, true, true, true);
1797  }
1798  }
1799  /* compute constraint error */
1800  for (i = ikscene->targets.size(); i > 0; i--) {
1801  IK_Target *iktarget = ikscene->targets[i - 1];
1802  if (!(iktarget->blenderConstraint->flag & CONSTRAINT_OFF) && iktarget->constraint) {
1803  unsigned int nvalues;
1804  const iTaSC::ConstraintValues *values;
1805  values = iktarget->constraint->getControlParameters(&nvalues);
1806  iktarget->errorCallback(values, nvalues, iktarget);
1807  }
1808  }
1809  /* Apply result to bone:
1810  * walk the ikscene->channels
1811  * for each, get the Frame of the joint corresponding to the bone relative to its parent
1812  * combine the parent and the joint frame to get the frame relative to armature
1813  * a backward translation of the bone length gives the head
1814  * if TY, compute the scale as the ratio of the joint length with rest pose length */
1815  iTaSC::Armature *arm = ikscene->armature;
1816  KDL::Frame frame;
1817  double q_rest[3], q[3];
1818  const KDL::Joint *joint;
1819  const KDL::Frame *tip;
1820  bPoseChannel *pchan;
1821  float scale;
1822  float length;
1823  float yaxis[3];
1824  for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; i++, ikchan++) {
1825  if (i == 0) {
1826  if (!arm->getRelativeFrame(frame, ikchan->tail)) {
1827  break;
1828  }
1829  /* this frame is relative to base, make it relative to object */
1830  ikchan->frame = ikscene->baseFrame * frame;
1831  }
1832  else {
1833  if (!arm->getRelativeFrame(frame, ikchan->tail, ikscene->channels[ikchan->parent].tail)) {
1834  break;
1835  }
1836  /* combine with parent frame to get frame relative to object */
1837  ikchan->frame = ikscene->channels[ikchan->parent].frame * frame;
1838  }
1839  /* ikchan->frame is the tail frame relative to object
1840  * get bone length */
1841  if (!arm->getSegment(ikchan->tail, 3, joint, q_rest[0], q[0], tip)) {
1842  break;
1843  }
1844  if (joint->getType() == KDL::Joint::TransY) {
1845  /* stretch bones have a TY joint, compute the scale */
1846  scale = (float)(q[0] / q_rest[0]);
1847  /* the length is the joint itself */
1848  length = (float)q[0];
1849  }
1850  else {
1851  scale = 1.0f;
1852  /* for fixed bone, the length is in the tip (always along Y axis) */
1853  length = tip->p(1);
1854  }
1855  /* ready to compute the pose mat */
1856  pchan = ikchan->pchan;
1857  /* tail mat */
1858  ikchan->frame.getValue(&pchan->pose_mat[0][0]);
1859  /* the scale of the object was included in the ik scene, take it out now
1860  * because the pose channels are relative to the object */
1861  mul_v3_fl(pchan->pose_mat[3], ikscene->blInvScale);
1862  length *= ikscene->blInvScale;
1863  copy_v3_v3(pchan->pose_tail, pchan->pose_mat[3]);
1864  /* shift to head */
1865  copy_v3_v3(yaxis, pchan->pose_mat[1]);
1866  mul_v3_fl(yaxis, length);
1867  sub_v3_v3v3(pchan->pose_mat[3], pchan->pose_mat[3], yaxis);
1868  copy_v3_v3(pchan->pose_head, pchan->pose_mat[3]);
1869  /* add scale */
1870  mul_v3_fl(pchan->pose_mat[0], scale);
1871  mul_v3_fl(pchan->pose_mat[1], scale);
1872  mul_v3_fl(pchan->pose_mat[2], scale);
1873  }
1874  if (i < ikscene->numchan) {
1875  /* big problem */
1876  }
1877 }
1878 
1879 /*---------------------------------------------------
1880  * plugin interface
1881  */
1883  struct Scene *scene,
1884  Object *ob,
1885  float ctime)
1886 {
1887  bPoseChannel *pchan;
1888  int count = 0;
1889 
1890  if (ob->pose->ikdata != nullptr && !(ob->pose->flag & POSE_WAS_REBUILT)) {
1891  if (!init_scene(ob)) {
1892  return;
1893  }
1894  }
1895  /* first remove old scene */
1896  itasc_clear_data(ob->pose);
1897  /* we should handle all the constraint and mark them all disabled
1898  * for blender but we'll start with the IK constraint alone */
1899  for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan;
1900  pchan = (bPoseChannel *)pchan->next) {
1901  if (pchan->constflag & PCHAN_HAS_IK) {
1902  count += initialize_scene(ob, pchan);
1903  }
1904  }
1905  /* if at least one tree, create the scenes from the PoseTree stored in the channels
1906  * postpone until execute_tree: this way the pose constraint are included */
1907  if (count) {
1908  create_scene(depsgraph, scene, ob, ctime);
1909  }
1910  itasc_update_param(ob->pose);
1911  /* make sure we don't rebuilt until the user changes something important */
1912  ob->pose->flag &= ~POSE_WAS_REBUILT;
1913 }
1914 
1916  struct Scene *scene,
1917  Object *ob,
1918  bPoseChannel *pchan_root,
1919  float ctime)
1920 {
1921  if (ob->pose->ikdata) {
1922  IK_Data *ikdata = (IK_Data *)ob->pose->ikdata;
1923  bItasc *ikparam = (bItasc *)ob->pose->ikparam;
1924  /* we need default parameters */
1925  if (!ikparam) {
1926  ikparam = &DefIKParam;
1927  }
1928 
1929  for (IK_Scene *ikscene = ikdata->first; ikscene; ikscene = ikscene->next) {
1930  if (ikscene->channels[0].pchan == pchan_root) {
1931  float timestep = scene->r.frs_sec_base / scene->r.frs_sec;
1932  execute_scene(depsgraph, scene, ikscene, ikparam, ctime, timestep);
1933  break;
1934  }
1935  }
1936  }
1937 }
1938 
1939 void itasc_release_tree(struct Scene *scene, struct Object *ob, float ctime)
1940 {
1941  /* not used for iTaSC */
1942 }
1943 
1944 void itasc_clear_data(struct bPose *pose)
1945 {
1946  if (pose->ikdata) {
1947  IK_Data *ikdata = (IK_Data *)pose->ikdata;
1948  for (IK_Scene *scene = ikdata->first; scene; scene = ikdata->first) {
1949  ikdata->first = scene->next;
1950  delete scene;
1951  }
1952  MEM_freeN(ikdata);
1953  pose->ikdata = nullptr;
1954  }
1955 }
1956 
1957 void itasc_clear_cache(struct bPose *pose)
1958 {
1959  if (pose->ikdata) {
1960  IK_Data *ikdata = (IK_Data *)pose->ikdata;
1961  for (IK_Scene *scene = ikdata->first; scene; scene = scene->next) {
1962  if (scene->cache) {
1963  /* clear all cache but leaving the timestamp 0 (=rest pose) */
1964  scene->cache->clearCacheFrom(nullptr, 1);
1965  }
1966  }
1967  }
1968 }
1969 
1970 void itasc_update_param(struct bPose *pose)
1971 {
1972  if (pose->ikdata && pose->ikparam) {
1973  IK_Data *ikdata = (IK_Data *)pose->ikdata;
1974  bItasc *ikparam = (bItasc *)pose->ikparam;
1975  for (IK_Scene *ikscene = ikdata->first; ikscene; ikscene = ikscene->next) {
1976  double armlength = ikscene->armature->getArmLength();
1977  ikscene->solver->setParam(iTaSC::Solver::DLS_LAMBDA_MAX, ikparam->dampmax * armlength);
1978  ikscene->solver->setParam(iTaSC::Solver::DLS_EPSILON, ikparam->dampeps * armlength);
1979  if (ikparam->flag & ITASC_SIMULATION) {
1980  ikscene->scene->setParam(iTaSC::Scene::MIN_TIMESTEP, ikparam->minstep);
1981  ikscene->scene->setParam(iTaSC::Scene::MAX_TIMESTEP, ikparam->maxstep);
1982  ikscene->solver->setParam(iTaSC::Solver::DLS_QMAX, ikparam->maxvel);
1983  ikscene->armature->setControlParameter(
1985  }
1986  else {
1987  /* in animation mode timestep is 1s by convention => qmax becomes radiant and feedback
1988  * becomes fraction of error gap corrected in one iteration. */
1989  ikscene->scene->setParam(iTaSC::Scene::MIN_TIMESTEP, 1.0);
1990  ikscene->scene->setParam(iTaSC::Scene::MAX_TIMESTEP, 1.0);
1991  ikscene->solver->setParam(iTaSC::Solver::DLS_QMAX, 0.52);
1992  ikscene->armature->setControlParameter(
1994  }
1995  }
1996  }
1997 }
1998 
1999 void itasc_test_constraint(struct Object *ob, struct bConstraint *cons)
2000 {
2001  struct bKinematicConstraint *data = (struct bKinematicConstraint *)cons->data;
2002 
2003  /* only for IK constraint */
2004  if (cons->type != CONSTRAINT_TYPE_KINEMATIC || data == nullptr) {
2005  return;
2006  }
2007 
2008  switch (data->type) {
2011  /* cartesian space constraint */
2012  break;
2013  }
2014 }
typedef float(TangentPoint)[2]
Blender kernel action and pose functionality.
void BKE_pose_itasc_init(struct bItasc *itasc)
Definition: action.c:879
void BKE_pose_where_is_bone(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct bPoseChannel *pchan, float ctime, bool do_extra)
Definition: armature.c:2673
void BKE_constraint_target_matrix_get(struct Depsgraph *depsgraph, struct Scene *scene, struct bConstraint *con, int index, short ownertype, void *ownerdata, float mat[4][4], float ctime)
Definition: constraint.c:6025
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
MINLINE float min_ff(float a, float b)
void mul_m3_v3(const float M[3][3], float r[3])
Definition: math_matrix.c:930
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void unit_m3(float m[3][3])
Definition: math_matrix.c:58
void copy_m3_m4(float m1[3][3], const float m2[4][4])
Definition: math_matrix.c:105
void mul_m4_m4m3(float R[4][4], const float A[4][4], const float B[3][3])
Definition: math_matrix.c:437
void unit_m4(float m[4][4])
Definition: rct.c:1140
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void normalize_m3(float R[3][3]) ATTR_NONNULL()
Definition: math_matrix.c:1919
#define mul_m4_series(...)
bool invert_m3_m3(float R[3][3], const float A[3][3])
Definition: math_matrix.c:1161
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
void blend_m4_m4m4(float out[4][4], const float dst[4][4], const float src[4][4], const float srcweight)
Definition: math_matrix.c:2465
void normalize_m4(float R[4][4]) ATTR_NONNULL()
Definition: math_matrix.c:1952
float normalize_qt(float q[4])
void eulO_to_mat3(float mat[3][3], const float eul[3], const short order)
void axis_angle_to_mat3(float R[3][3], const float axis[3], const float angle)
void quat_to_mat3(float mat[3][3], const float q[4])
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
#define MAX2(a, b)
#define MIN2(a, b)
#define CONSTRAINT_ID_ALL
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
@ ITASC_SOLVER_SDLS
@ ITASC_SOLVER_DLS
@ ROT_MODE_AXISANGLE
@ ITASC_REITERATION
@ ITASC_AUTO_STEP
@ ITASC_SIMULATION
@ ITASC_INITIAL_REITERATION
@ BONE_IK_ZLIMIT
@ BONE_IK_NO_YDOF_TEMP
@ BONE_IK_XLIMIT
@ BONE_IK_NO_XDOF_TEMP
@ BONE_IK_NO_ZDOF
@ BONE_IK_ROTCTL
@ BONE_IK_NO_ZDOF_TEMP
@ BONE_IK_YLIMIT
@ BONE_IK_NO_YDOF
@ BONE_IK_NO_XDOF
@ PCHAN_HAS_IK
@ POSE_DONE
@ POSE_IKTREE
@ POSE_CHAIN
@ POSE_WAS_REBUILT
@ BONE_CONNECTED
@ CONSTRAINT_OFF
@ CONSTRAINT_DISABLE
@ CONSTRAINT_IK_ROT
@ CONSTRAINT_IK_NO_ROT_X
@ CONSTRAINT_IK_TARGETAXIS
@ CONSTRAINT_IK_NO_POS_Z
@ CONSTRAINT_IK_NO_POS_Y
@ CONSTRAINT_IK_NO_ROT_Y
@ CONSTRAINT_IK_POS
@ CONSTRAINT_IK_NO_POS_X
@ CONSTRAINT_IK_NO_ROT_Z
@ CONSTRAINT_IK_AUTO
@ CONSTRAINT_IK_STRETCH
@ CONSTRAINT_IK_TIP
@ CONSTRAINT_TYPE_KINEMATIC
@ CONSTRAINT_OBTYPE_OBJECT
@ LIMITDIST_INSIDE
@ LIMITDIST_OUTSIDE
@ CONSTRAINT_IK_COPYPOSE
@ CONSTRAINT_IK_DISTANCE
Object is a sort of wrapper for general info.
@ OB_ARMATURE
struct Scene Scene
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
#define X
Definition: GeomUtils.cpp:213
#define Z
Definition: GeomUtils.cpp:215
#define Y
Definition: GeomUtils.cpp:214
Read Guarded memory(de)allocation.
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB Hue Distance
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
represents a frame transformation in 3D space (rotation + translation)
Definition: frames.hpp:526
Rotation M
Orientation of the Frame.
Definition: frames.hpp:529
void setValue(float *oglmat)
Definition: frames.inl:724
void getValue(float *oglmat) const
Definition: frames.inl:732
Vector p
origine of the Frame
Definition: frames.hpp:528
void resize(unsigned int newSize)
Definition: jntarray.cpp:66
This class encapsulates a simple joint, that is with one parameterized degree of freedom and with sca...
Definition: joint.hpp:43
@ Sphere
Definition: joint.hpp:45
@ TransY
Definition: joint.hpp:45
const JointType & getType() const
Definition: joint.hpp:88
represents rotations in 3 dimensional space.
Definition: frames.hpp:299
Vector GetRot() const
Definition: frames.cpp:297
Rotation Inverse() const
Gives back the inverse rotation matrix of *this.
Definition: frames.inl:641
Vector UnitY() const
Access to the underlying unitvectors of the rotation matrix.
Definition: frames.hpp:479
Vector UnitZ() const
Access to the underlying unitvectors of the rotation matrix.
Definition: frames.hpp:491
Vector UnitX() const
Access to the underlying unitvectors of the rotation matrix.
Definition: frames.hpp:467
static Rotation RotX(double angle)
The Rot... static functions give the value of the appropriate rotation matrix back.
Definition: frames.inl:609
Vector2 GetXZRot() const
Definition: frames.cpp:330
void setValue(float *oglmat)
Definition: frames.inl:656
static Rotation RotY(double angle)
The Rot... static functions give the value of the appropriate rotation matrix back.
Definition: frames.inl:614
static Rotation RotZ(double angle)
The Rot... static functions give the value of the appropriate rotation matrix back.
Definition: frames.inl:619
void GetValue(double *xy) const
store vector components in array
Definition: frames.inl:786
A concrete implementation of a 3 dimensional vector class.
Definition: frames.hpp:143
void GetValue(double *xyz) const
store vector components in array
Definition: frames.inl:51
double Norm() const
Definition: frames.cpp:115
bool getRelativeFrame(Frame &result, const std::string &segment_name, const std::string &base_name=m_root)
Definition: Armature.cpp:664
virtual const Frame & getPose(const unsigned int end_effector)
Definition: Armature.cpp:657
bool getSegment(const std::string &segment_name, const unsigned int q_size, const Joint *&p_joint, double &q_rest, double &q, const Frame *&p_tip)
Definition: Armature.cpp:251
double getMaxEndEffectorChange()
Definition: Armature.cpp:281
virtual bool setJointArray(const KDL::JntArray &joints)
Definition: Armature.cpp:430
double getMaxJointChange()
Definition: Armature.cpp:267
const void * getPreviousCacheItem(const void *device, int channel, CacheTS *timestamp)
Definition: Cache.cpp:545
bool setControlParameter(int id, ConstraintAction action, double value, double timestep=0.0)
void substep(bool _substep)
virtual EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool registerCallback(ConstraintCallback _function, void *_param)
virtual const ConstraintValues * getControlParameters(unsigned int *_nvalues)=0
virtual const unsigned int getNrOfConstraints()
bool setCallback(MovingFrameCallback _function, void *_param)
Definition: MovingFrame.cpp:86
virtual const KDL::Frame & getPose(const unsigned int end_effector=0)
Definition: Object.hpp:37
@ MIN_TIMESTEP
Definition: Scene.hpp:27
@ MAX_TIMESTEP
Definition: Scene.hpp:28
bool update(double timestamp, double timestep, unsigned int numsubstep=1, bool reiterate=false, bool cache=true, bool interpolate=true)
Definition: Scene.cpp:312
@ DLS_LAMBDA_MAX
Definition: Solver.hpp:20
virtual void setParam(SolverParam param, double value)=0
Scene scene
Simulation simulation
const Depsgraph * depsgraph
void * tree
#define rot(x, k)
#define e_matrix
Definition: eigen_types.hpp:40
IMETHOD void SetToZero(Vector &v)
Definition: frames.inl:1104
int count
static void execute_scene(struct Depsgraph *depsgraph, Scene *blscene, IK_Scene *ikscene, bItasc *ikparam, float ctime, float frtime)
static bItasc DefIKParam
static void RemoveEulerAngleFromMatrix(KDL::Rotation &R, double angle, int axis)
static void distance_error(const iTaSC::ConstraintValues *values, unsigned int _nvalues, IK_Target *iktarget)
void itasc_clear_cache(struct bPose *pose)
static int init_scene(Object *ob)
static IK_Data * get_ikdata(bPose *pose)
static bool target_callback(const iTaSC::Timestamp &timestamp, const iTaSC::Frame &current, iTaSC::Frame &next, void *param)
void itasc_clear_data(struct bPose *pose)
static bool base_callback(const iTaSC::Timestamp &timestamp, const iTaSC::Frame &current, iTaSC::Frame &next, void *param)
float[4] Vector4
#define ANIM_FEEDBACK
static void BKE_pose_rest(IK_Scene *ikscene)
static void convert_pose(IK_Scene *ikscene)
static bool copypose_callback(const iTaSC::Timestamp &timestamp, iTaSC::ConstraintValues *const _values, unsigned int _nvalues, void *_param)
void itasc_update_param(struct bPose *pose)
static int convert_channels(struct Depsgraph *depsgraph, IK_Scene *ikscene, PoseTree *tree, float ctime)
static int initialize_scene(Object *ob, bPoseChannel *pchan_tip)
static bool joint_callback(const iTaSC::Timestamp &timestamp, iTaSC::ConstraintValues *const _values, unsigned int _nvalues, void *_param)
IK_SegmentFlag
@ IK_ZDOF
@ IK_REVOLUTE
@ IK_SWING
@ IK_XDOF
@ IK_YDOF
@ IK_TRANSY
static void create_scene(struct Depsgraph *depsgraph, Scene *scene, Object *ob, float ctime)
static bool constraint_valid(bConstraint *con)
void itasc_release_tree(struct Scene *scene, struct Object *ob, float ctime)
static void GetJointRotation(KDL::Rotation &boneRot, int type, double *rot)
static void copypose_error(const iTaSC::ConstraintValues *values, unsigned int nvalues, IK_Target *iktarget)
static double ComputeTwist(const KDL::Rotation &R)
void itasc_initialize_tree(struct Depsgraph *depsgraph, struct Scene *scene, Object *ob, float ctime)
static bool is_cartesian_constraint(bConstraint *con)
void(*)(const iTaSC::ConstraintValues *values, unsigned int nvalues, IK_Target *iktarget) ErrorCallback
void itasc_execute_tree(struct Depsgraph *depsgraph, struct Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime)
IK_SegmentAxis
@ IK_X
@ IK_TRANS_X
@ IK_Y
@ IK_TRANS_Y
@ IK_TRANS_Z
@ IK_Z
void itasc_test_constraint(struct Object *ob, struct bConstraint *cons)
static int initialize_chain(Object *ob, bPoseChannel *pchan_tip, bConstraint *con)
static IK_Scene * convert_tree(struct Depsgraph *depsgraph, Scene *blscene, Object *ob, bPoseChannel *pchan, float ctime)
static double EulerAngleFromMatrix(const KDL::Rotation &R, int axis)
static bool distance_callback(const iTaSC::Timestamp &timestamp, iTaSC::ConstraintValues *const _values, unsigned int _nvalues, void *_param)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static ulong * next
#define T
#define R
static void error(const char *str)
Definition: meshlaplacian.c:65
static unsigned a[3]
Definition: RandGen.cpp:92
double sign(double arg)
Definition: utility.h:250
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
Vector Normalize(const Vector &, double eps=epsilon)
const double PI
the value of pi
Definition: utility.cpp:19
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:367
double epsilon
default precision while comparing with Equal(..,..) functions. Initialized at 0.0000001.
Definition: utility.cpp:22
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
Definition: rall1d.h:429
INLINE Rall1d< T, V, S > sqr(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:351
const Frame F_identity
unsigned int CacheTS
Definition: Cache.hpp:32
@ ACT_FEEDBACK
return ret
char name[64]
float arm_tail[3]
float length
float arm_mat[4][4]
float bone_mat[3][3]
char name[66]
Definition: DNA_ID.h:283
Object * owner
bPoseChannel * pchan
std::string tail
KDL::Frame frame
std::string head
double jointValue[4]
struct IK_Scene * first
Object * blArmature
KDL::Frame baseFrame
iTaSC::Solver * solver
KDL::JntArray jointArray
IK_Scene * next
float blScale
struct bConstraint * polarConstraint
iTaSC::MovingFrame * base
iTaSC::Cache * cache
iTaSC::Armature * armature
std::vector< IK_Target * > targets
iTaSC::Scene * scene
float blInvScale
struct Scene * blscene
struct Depsgraph * bldepsgraph
IK_Channel * channels
std::string targetName
unsigned short controlType
std::string constraintName
struct Scene * blscene
struct bPoseChannel * rootChannel
iTaSC::ConstraintSet * constraint
struct bConstraint * blenderConstraint
struct Depsgraph * bldepsgraph
ErrorCallback errorCallback
iTaSC::MovingFrame * target
short channel
float eeRest[4][4]
bool simulation
Object * owner
void * first
Definition: DNA_listBase.h:47
struct bPose * pose
float obmat[4][4]
struct bConstraint * con
Definition: BKE_armature.h:122
float frs_sec_base
short flag
struct RenderData r
struct bConstraint * next
float precision
float feedback
float maxvel
short numstep
short numiter
ListBase constraints
struct Bone * bone
struct bPoseChannel * parent
float stiffness[3]
struct ListBase iktree
float pose_head[3]
float pose_tail[3]
struct bPoseChannel * next
float pose_mat[4][4]
ListBase chanbase
void * ikdata
void * ikparam
short flag
struct ConstraintSingleValue * values
unsigned int substep
Definition: Cache.hpp:40
unsigned int update
Definition: Cache.hpp:43
ccl_device_inline float2 fabs(const float2 &a)