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