~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/ikplugin/intern/itasc_plugin.cpp

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * The Original Code is: all of this file.
22
22
 *
23
23
 * Original author: Benoit Bolsee
24
 
 * Contributor(s): 
 
24
 * Contributor(s):
25
25
 *
26
26
 * ***** END GPL LICENSE BLOCK *****
27
27
 */
30
30
 *  \ingroup ikplugin
31
31
 */
32
32
 
33
 
 
34
33
#include <stdlib.h>
35
34
#include <string.h>
36
35
#include <vector>
58
57
#include "BKE_global.h"
59
58
#include "BKE_armature.h"
60
59
#include "BKE_action.h"
61
 
#include "BKE_utildefines.h"
62
60
#include "BKE_constraint.h"
63
61
#include "DNA_object_types.h"
64
62
#include "DNA_action_types.h"
73
71
bItasc DefIKParam;
74
72
 
75
73
// in case of animation mode, feedback and timestep is fixed
76
 
#define ANIM_TIMESTEP   1.0
77
 
#define ANIM_FEEDBACK   0.8
78
 
#define ANIM_QMAX               0.52
 
74
#define ANIM_TIMESTEP   1.0
 
75
#define ANIM_FEEDBACK   0.8
 
76
#define ANIM_QMAX       0.52
79
77
 
80
78
 
81
79
// Structure pointed by bPose.ikdata
82
80
// It contains everything needed to simulate the armatures
83
81
// There can be several simulation islands independent to each other
84
 
struct IK_Data
85
 
{
86
 
        struct IK_Scene* first;
 
82
struct IK_Data {
 
83
        struct IK_Scene *first;
87
84
};
88
85
 
89
86
typedef float Vector3[3];
90
87
typedef float Vector4[4];
91
88
struct IK_Target;
92
 
typedef void (*ErrorCallback)(const iTaSC::ConstraintValues* values, unsigned int nvalues, IK_Target* iktarget);
 
89
typedef void (*ErrorCallback)(const iTaSC::ConstraintValues *values, unsigned int nvalues, IK_Target *iktarget);
93
90
 
94
91
// one structure for each target in the scene
95
92
struct IK_Target
160
157
        }
161
158
};
162
159
 
163
 
struct IK_Scene
164
 
{
 
160
struct IK_Scene {
165
161
        struct Scene            *blscene;
166
162
        IK_Scene*                       next;
167
163
        int                                     numchan;        // number of channel in pchan
176
172
        KDL::JntArray           jointArray;     // buffer for storing temporary joint array
177
173
        iTaSC::Solver*          solver;
178
174
        Object*                         blArmature;
 
175
        float                           blScale;        // scale of the Armature object (assume uniform scaling)
 
176
        float                           blInvScale;     // inverse of Armature object scale
179
177
        struct bConstraint*     polarConstraint;
180
178
        std::vector<IK_Target*>         targets;
181
179
 
188
186
                scene = NULL;
189
187
                base = NULL;
190
188
                solver = NULL;
 
189
                blScale = blInvScale = 1.0f;
191
190
                blArmature = NULL;
192
191
                numchan = 0;
193
192
                numjoint = 0;
198
197
                // delete scene first
199
198
                if (scene)
200
199
                        delete scene;
201
 
                for (std::vector<IK_Target*>::iterator it = targets.begin();    it != targets.end(); ++it)
 
200
                for (std::vector<IK_Target *>::iterator it = targets.begin(); it != targets.end(); ++it)
202
201
                        delete (*it);
203
202
                targets.clear();
204
203
                if (channels)
205
 
                        delete [] channels;
 
204
                        delete[] channels;
206
205
                if (solver)
207
206
                        delete solver;
208
207
                if (armature)
236
235
 
237
236
static int initialize_chain(Object *ob, bPoseChannel *pchan_tip, bConstraint *con)
238
237
{
239
 
        bPoseChannel *curchan, *pchan_root=NULL, *chanlist[256], **oldchan;
 
238
        bPoseChannel *curchan, *pchan_root = NULL, *chanlist[256], **oldchan;
240
239
        PoseTree *tree;
241
240
        PoseTarget *target;
242
241
        bKinematicConstraint *data;
243
 
        int a, t, segcount= 0, size, newsize, *oldparent, parent, rootbone, treecount;
244
 
 
245
 
        data=(bKinematicConstraint*)con->data;
246
 
        
 
242
        int a, t, segcount = 0, size, newsize, *oldparent, parent, rootbone, treecount;
 
243
 
 
244
        data = (bKinematicConstraint *)con->data;
 
245
 
247
246
        /* exclude tip from chain? */
248
247
        if (!(data->flag & CONSTRAINT_IK_TIP))
249
 
                pchan_tip= pchan_tip->parent;
250
 
        
 
248
                pchan_tip = pchan_tip->parent;
 
249
 
251
250
        rootbone = data->rootbone;
252
251
        /* Find the chain's root & count the segments needed */
253
 
        for (curchan = pchan_tip; curchan; curchan=curchan->parent) {
 
252
        for (curchan = pchan_tip; curchan; curchan = curchan->parent) {
254
253
                pchan_root = curchan;
255
 
                
256
 
                if (++segcount > 255)           // 255 is weak
 
254
 
 
255
                if (++segcount > 255)       // 255 is weak
257
256
                        break;
258
257
 
259
 
                if (segcount==rootbone) {
260
 
                        // reached this end of the chain but if the chain is overlapping with a 
 
258
                if (segcount == rootbone) {
 
259
                        // reached this end of the chain but if the chain is overlapping with a
261
260
                        // previous one, we must go back up to the root of the other chain
262
261
                        if ((curchan->flag & POSE_CHAIN) && curchan->iktree.first == NULL) {
263
262
                                rootbone++;
264
263
                                continue;
265
264
                        }
266
 
                        break; 
 
265
                        break;
267
266
                }
268
267
 
269
268
                if (curchan->iktree.first != NULL)
270
 
                        // Oh oh, there is already a chain starting from this channel and our chain is longer... 
 
269
                        // Oh oh, there is already a chain starting from this channel and our chain is longer...
271
270
                        // Should handle this by moving the previous chain up to the beginning of our chain
272
271
                        // For now we just stop here
273
272
                        break;
277
276
        if ((pchan_root->flag & POSE_CHAIN) && pchan_root->iktree.first == NULL) return 0;
278
277
 
279
278
        // now that we know how many segment we have, set the flag
280
 
        for (rootbone = segcount, segcount = 0, curchan = pchan_tip; segcount < rootbone; segcount++, curchan=curchan->parent) {
281
 
                chanlist[segcount]=curchan;
 
279
        for (rootbone = segcount, segcount = 0, curchan = pchan_tip; segcount < rootbone; segcount++, curchan = curchan->parent) {
 
280
                chanlist[segcount] = curchan;
282
281
                curchan->flag |= POSE_CHAIN;
283
282
        }
284
283
 
285
284
        /* setup the chain data */
286
285
        /* create a target */
287
 
        target= (PoseTarget*)MEM_callocN(sizeof(PoseTarget), "posetarget");
288
 
        target->con= con;
 
286
        target = (PoseTarget *)MEM_callocN(sizeof(PoseTarget), "posetarget");
 
287
        target->con = con;
289
288
        // by contruction there can be only one tree per channel and each channel can be part of at most one tree.
290
 
        tree = (PoseTree*)pchan_root->iktree.first;
 
289
        tree = (PoseTree *)pchan_root->iktree.first;
291
290
 
292
 
        if (tree==NULL) {
 
291
        if (tree == NULL) {
293
292
                /* make new tree */
294
 
                tree= (PoseTree*)MEM_callocN(sizeof(PoseTree), "posetree");
 
293
                tree = (PoseTree *)MEM_callocN(sizeof(PoseTree), "posetree");
295
294
 
296
 
                tree->iterations= data->iterations;
297
 
                tree->totchannel= segcount;
 
295
                tree->iterations = data->iterations;
 
296
                tree->totchannel = segcount;
298
297
                tree->stretch = (data->flag & CONSTRAINT_IK_STRETCH);
299
 
                
300
 
                tree->pchan= (bPoseChannel**)MEM_callocN(segcount*sizeof(void*), "ik tree pchan");
301
 
                tree->parent= (int*)MEM_callocN(segcount*sizeof(int), "ik tree parent");
302
 
                for (a=0; a<segcount; a++) {
303
 
                        tree->pchan[a]= chanlist[segcount-a-1];
304
 
                        tree->parent[a]= a-1;
 
298
 
 
299
                tree->pchan = (bPoseChannel **)MEM_callocN(segcount * sizeof(void *), "ik tree pchan");
 
300
                tree->parent = (int *)MEM_callocN(segcount * sizeof(int), "ik tree parent");
 
301
                for (a = 0; a < segcount; a++) {
 
302
                        tree->pchan[a] = chanlist[segcount - a - 1];
 
303
                        tree->parent[a] = a - 1;
305
304
                }
306
 
                target->tip= segcount-1;
307
 
                
 
305
                target->tip = segcount - 1;
 
306
 
308
307
                /* AND! link the tree to the root */
309
308
                BLI_addtail(&pchan_root->iktree, tree);
310
309
                // new tree
311
310
                treecount = 1;
312
311
        }
313
312
        else {
314
 
                tree->iterations= MAX2(data->iterations, tree->iterations);
315
 
                tree->stretch= tree->stretch && !(data->flag & CONSTRAINT_IK_STRETCH);
 
313
                tree->iterations = MAX2(data->iterations, tree->iterations);
 
314
                tree->stretch = tree->stretch && !(data->flag & CONSTRAINT_IK_STRETCH);
316
315
 
317
316
                /* skip common pose channels and add remaining*/
318
 
                size= MIN2(segcount, tree->totchannel);
 
317
                size = MIN2(segcount, tree->totchannel);
319
318
                a = t = 0;
320
 
                while (a<size && t<tree->totchannel) {
 
319
                while (a < size && t < tree->totchannel) {
321
320
                        // locate first matching channel
322
 
                        for (;t<tree->totchannel && tree->pchan[t]!=chanlist[segcount-a-1];t++);
323
 
                        if (t>=tree->totchannel)
 
321
                        for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) ;
 
322
                        if (t >= tree->totchannel)
324
323
                                break;
325
 
                        for (; a<size && t<tree->totchannel && tree->pchan[t]==chanlist[segcount-a-1]; a++, t++);
 
324
                        for (; a < size && t < tree->totchannel && tree->pchan[t] == chanlist[segcount - a - 1]; a++, t++) ;
326
325
                }
327
326
 
328
 
                segcount= segcount-a;
329
 
                target->tip= tree->totchannel + segcount - 1;
 
327
                segcount = segcount - a;
 
328
                target->tip = tree->totchannel + segcount - 1;
330
329
 
331
330
                if (segcount > 0) {
332
331
                        for (parent = a - 1; parent < tree->totchannel; parent++)
333
 
                                if (tree->pchan[parent] == chanlist[segcount-1]->parent)
 
332
                                if (tree->pchan[parent] == chanlist[segcount - 1]->parent)
334
333
                                        break;
335
 
                        
 
334
 
336
335
                        /* shouldn't happen, but could with dependency cycles */
337
336
                        if (parent == tree->totchannel)
338
337
                                parent = a - 1;
339
338
 
340
339
                        /* resize array */
341
 
                        newsize= tree->totchannel + segcount;
342
 
                        oldchan= tree->pchan;
343
 
                        oldparent= tree->parent;
 
340
                        newsize = tree->totchannel + segcount;
 
341
                        oldchan = tree->pchan;
 
342
                        oldparent = tree->parent;
344
343
 
345
 
                        tree->pchan= (bPoseChannel**)MEM_callocN(newsize*sizeof(void*), "ik tree pchan");
346
 
                        tree->parent= (int*)MEM_callocN(newsize*sizeof(int), "ik tree parent");
347
 
                        memcpy(tree->pchan, oldchan, sizeof(void*)*tree->totchannel);
348
 
                        memcpy(tree->parent, oldparent, sizeof(int)*tree->totchannel);
 
344
                        tree->pchan = (bPoseChannel **)MEM_callocN(newsize * sizeof(void *), "ik tree pchan");
 
345
                        tree->parent = (int *)MEM_callocN(newsize * sizeof(int), "ik tree parent");
 
346
                        memcpy(tree->pchan, oldchan, sizeof(void *) * tree->totchannel);
 
347
                        memcpy(tree->parent, oldparent, sizeof(int) * tree->totchannel);
349
348
                        MEM_freeN(oldchan);
350
349
                        MEM_freeN(oldparent);
351
350
 
352
351
                        /* add new pose channels at the end, in reverse order */
353
 
                        for (a=0; a<segcount; a++) {
354
 
                                tree->pchan[tree->totchannel+a]= chanlist[segcount-a-1];
355
 
                                tree->parent[tree->totchannel+a]= tree->totchannel+a-1;
 
352
                        for (a = 0; a < segcount; a++) {
 
353
                                tree->pchan[tree->totchannel + a] = chanlist[segcount - a - 1];
 
354
                                tree->parent[tree->totchannel + a] = tree->totchannel + a - 1;
356
355
                        }
357
 
                        tree->parent[tree->totchannel]= parent;
358
 
                        
359
 
                        tree->totchannel= newsize;
 
356
                        tree->parent[tree->totchannel] = parent;
 
357
 
 
358
                        tree->totchannel = newsize;
360
359
                }
361
360
                // reusing tree
362
361
                treecount = 0;
371
370
 
372
371
static bool is_cartesian_constraint(bConstraint *con)
373
372
{
374
 
        //bKinematicConstraint* data=(bKinematicConstraint*)con->data;
 
373
        //bKinematicConstraint* data=(bKinematicConstraint *)con->data;
375
374
 
376
375
        return true;
377
376
}
378
377
 
379
378
static bool constraint_valid(bConstraint *con)
380
379
{
381
 
        bKinematicConstraint* data=(bKinematicConstraint*)con->data;
 
380
        bKinematicConstraint *data = (bKinematicConstraint *)con->data;
382
381
 
383
382
        if (data->flag & CONSTRAINT_IK_AUTO)
384
383
                return true;
386
385
                return false;
387
386
        if (is_cartesian_constraint(con)) {
388
387
                /* cartesian space constraint */
389
 
                if (data->tar==NULL) 
 
388
                if (data->tar == NULL)
390
389
                        return false;
391
 
                if (data->tar->type==OB_ARMATURE && data->subtarget[0]==0) 
 
390
                if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0)
392
391
                        return false;
393
392
        }
394
393
        return true;
395
394
}
396
395
 
397
 
int initialize_scene(Object *ob, bPoseChannel *pchan_tip)
 
396
static int initialize_scene(Object *ob, bPoseChannel *pchan_tip)
398
397
{
399
398
        bConstraint *con;
400
399
        int treecount;
401
400
 
402
401
        /* find all IK constraints and validate them */
403
402
        treecount = 0;
404
 
        for (con= (bConstraint *)pchan_tip->constraints.first; con; con= (bConstraint *)con->next) {
405
 
                if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
 
403
        for (con = (bConstraint *)pchan_tip->constraints.first; con; con = (bConstraint *)con->next) {
 
404
                if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
406
405
                        if (constraint_valid(con))
407
406
                                treecount += initialize_chain(ob, pchan_tip, con);
408
407
                }
410
409
        return treecount;
411
410
}
412
411
 
413
 
static IK_Data* get_ikdata(bPose *pose)
 
412
static IK_Data *get_ikdata(bPose *pose)
414
413
{
415
414
        if (pose->ikdata)
416
 
                return (IK_Data*)pose->ikdata;
 
415
                return (IK_Data *)pose->ikdata;
417
416
        pose->ikdata = MEM_callocN(sizeof(IK_Data), "iTaSC ikdata");
418
417
        // here init ikdata if needed
419
418
        // now that we have scene, make sure the default param are initialized
420
419
        if (!DefIKParam.iksolver)
421
 
                init_pose_itasc(&DefIKParam);
 
420
                BKE_pose_itasc_init(&DefIKParam);
422
421
 
423
 
        return (IK_Data*)pose->ikdata;
 
422
        return (IK_Data *)pose->ikdata;
424
423
}
425
424
static double EulerAngleFromMatrix(const KDL::Rotation& R, int axis)
426
425
{
427
 
        double t = KDL::sqrt(R(0,0)*R(0,0) + R(0,1)*R(0,1));
 
426
        double t = KDL::sqrt(R(0, 0) * R(0, 0) + R(0, 1) * R(0, 1));
428
427
 
429
 
        if (t > 16.0*KDL::epsilon) {
430
 
                if (axis == 0) return -KDL::atan2(R(1,2), R(2,2));
431
 
                else if (axis == 1) return KDL::atan2(-R(0,2), t);
432
 
                else return -KDL::atan2(R(0,1), R(0,0));
 
428
        if (t > 16.0 * KDL::epsilon) {
 
429
                if (axis == 0) return -KDL::atan2(R(1, 2), R(2, 2));
 
430
                else if (axis == 1) return KDL::atan2(-R(0, 2), t);
 
431
                else return -KDL::atan2(R(0, 1), R(0, 0));
433
432
        }
434
433
        else {
435
 
                if (axis == 0) return -KDL::atan2(-R(2,1), R(1,1));
436
 
                else if (axis == 1) return KDL::atan2(-R(0,2), t);
 
434
                if (axis == 0) return -KDL::atan2(-R(2, 1), R(1, 1));
 
435
                else if (axis == 1) return KDL::atan2(-R(0, 2), t);
437
436
                else return 0.0f;
438
437
        }
439
438
}
441
440
static double ComputeTwist(const KDL::Rotation& R)
442
441
{
443
442
        // qy and qw are the y and w components of the quaternion from R
444
 
        double qy = R(0,2) - R(2,0);
445
 
        double qw = R(0,0) + R(1,1) + R(2,2) + 1;
 
443
        double qy = R(0, 2) - R(2, 0);
 
444
        double qw = R(0, 0) + R(1, 1) + R(2, 2) + 1;
446
445
 
447
 
        double tau = 2*KDL::atan2(qy, qw);
 
446
        double tau = 2 * KDL::atan2(qy, qw);
448
447
 
449
448
        return tau;
450
449
}
454
453
        // compute twist parameter
455
454
        KDL::Rotation T;
456
455
        switch (axis) {
457
 
        case 0:
458
 
                T = KDL::Rotation::RotX(-angle);
459
 
                break;
460
 
        case 1:
461
 
                T = KDL::Rotation::RotY(-angle);
462
 
                break;
463
 
        case 2:
464
 
                T = KDL::Rotation::RotZ(-angle);
465
 
                break;
466
 
        default:
467
 
                return;
 
456
                case 0:
 
457
                        T = KDL::Rotation::RotX(-angle);
 
458
                        break;
 
459
                case 1:
 
460
                        T = KDL::Rotation::RotY(-angle);
 
461
                        break;
 
462
                case 2:
 
463
                        T = KDL::Rotation::RotZ(-angle);
 
464
                        break;
 
465
                default:
 
466
                        return;
468
467
        }
469
468
        // remove angle
470
 
        R = R*T;
 
469
        R = R * T;
471
470
}
472
471
 
473
472
#if 0
474
 
static void GetEulerXZY(const KDL::Rotation& R, double& X,double& Z,double& Y)
 
473
static void GetEulerXZY(const KDL::Rotation& R, double& X, double& Z, double& Y)
475
474
{
476
 
        if (fabs(R(0,1)) > 1.0 - KDL::epsilon ) {
477
 
                X = -KDL::sign(R(0,1)) * KDL::atan2(R(1,2), R(1,0));
478
 
                Z = -KDL::sign(R(0,1)) * KDL::PI / 2;
 
475
        if (fabs(R(0, 1)) > 1.0 - KDL::epsilon) {
 
476
                X = -KDL::sign(R(0, 1)) * KDL::atan2(R(1, 2), R(1, 0));
 
477
                Z = -KDL::sign(R(0, 1)) * KDL::PI / 2;
479
478
                Y = 0.0;
480
479
        }
481
480
        else {
482
 
                X = KDL::atan2(R(2,1), R(1,1));
483
 
                Z = KDL::atan2(-R(0,1), KDL::sqrt( KDL::sqr(R(0,0)) + KDL::sqr(R(0,2))));
484
 
                Y = KDL::atan2(R(0,2), R(0,0));
 
481
                X = KDL::atan2(R(2, 1), R(1, 1));
 
482
                Z = KDL::atan2(-R(0, 1), KDL::sqrt(KDL::sqr(R(0, 0)) + KDL::sqr(R(0, 2))));
 
483
                Y = KDL::atan2(R(0, 2), R(0, 0));
485
484
        }
486
485
}
487
486
 
488
 
static void GetEulerXYZ(const KDL::Rotation& R, double& X,double& Y,double& Z)
 
487
static void GetEulerXYZ(const KDL::Rotation& R, double& X, double& Y, double& Z)
489
488
{
490
 
        if (fabs(R(0,2)) > 1.0 - KDL::epsilon ) {
491
 
                X = KDL::sign(R(0,2)) * KDL::atan2(-R(1,0), R(1,1));
492
 
                Y = KDL::sign(R(0,2)) * KDL::PI / 2;
 
489
        if (fabs(R(0, 2)) > 1.0 - KDL::epsilon) {
 
490
                X = KDL::sign(R(0, 2)) * KDL::atan2(-R(1, 0), R(1, 1));
 
491
                Y = KDL::sign(R(0, 2)) * KDL::PI / 2;
493
492
                Z = 0.0;
494
493
        }
495
494
        else {
496
 
                X = KDL::atan2(-R(1,2), R(2,2));
497
 
                Y = KDL::atan2(R(0,2), KDL::sqrt( KDL::sqr(R(0,0)) + KDL::sqr(R(0,1))));
498
 
                Z = KDL::atan2(-R(0,1), R(0,0));
 
495
                X = KDL::atan2(-R(1, 2), R(2, 2));
 
496
                Y = KDL::atan2(R(0, 2), KDL::sqrt(KDL::sqr(R(0, 0)) + KDL::sqr(R(0, 1))));
 
497
                Z = KDL::atan2(-R(0, 1), R(0, 0));
499
498
        }
500
499
}
501
500
#endif
502
501
 
503
 
static void GetJointRotation(KDL::Rotation& boneRot, int type, double* rot)
 
502
static void GetJointRotation(KDL::Rotation& boneRot, int type, double *rot)
504
503
{
505
 
        switch (type & ~IK_TRANSY)
506
 
        {
507
 
        default:
508
 
                // fixed bone, no joint
509
 
                break;
510
 
        case IK_XDOF:
511
 
                // RX only, get the X rotation
512
 
                rot[0] = EulerAngleFromMatrix(boneRot, 0);
513
 
                break;
514
 
        case IK_YDOF:
515
 
                // RY only, get the Y rotation
516
 
                rot[0] = ComputeTwist(boneRot);
517
 
                break;
518
 
        case IK_ZDOF:
519
 
                // RZ only, get the Z rotation
520
 
                rot[0] = EulerAngleFromMatrix(boneRot, 2);
521
 
                break;
522
 
        case IK_XDOF|IK_YDOF:
523
 
                rot[1] = ComputeTwist(boneRot);
524
 
                RemoveEulerAngleFromMatrix(boneRot, rot[1], 1);
525
 
                rot[0] = EulerAngleFromMatrix(boneRot, 0);
526
 
                break;
527
 
        case IK_SWING:
528
 
                // RX+RZ
529
 
                boneRot.GetXZRot().GetValue(rot);
530
 
                break;
531
 
        case IK_YDOF|IK_ZDOF:
532
 
                // RZ+RY
533
 
                rot[1] = ComputeTwist(boneRot);
534
 
                RemoveEulerAngleFromMatrix(boneRot, rot[1], 1);
535
 
                rot[0] = EulerAngleFromMatrix(boneRot, 2);
536
 
                break;
537
 
        case IK_SWING|IK_YDOF:
538
 
                rot[2] = ComputeTwist(boneRot);
539
 
                RemoveEulerAngleFromMatrix(boneRot, rot[2], 1);
540
 
                boneRot.GetXZRot().GetValue(rot);
541
 
                break;
542
 
        case IK_REVOLUTE:
543
 
                boneRot.GetRot().GetValue(rot);
544
 
                break;
 
504
        switch (type & ~IK_TRANSY) {
 
505
                default:
 
506
                        // fixed bone, no joint
 
507
                        break;
 
508
                case IK_XDOF:
 
509
                        // RX only, get the X rotation
 
510
                        rot[0] = EulerAngleFromMatrix(boneRot, 0);
 
511
                        break;
 
512
                case IK_YDOF:
 
513
                        // RY only, get the Y rotation
 
514
                        rot[0] = ComputeTwist(boneRot);
 
515
                        break;
 
516
                case IK_ZDOF:
 
517
                        // RZ only, get the Z rotation
 
518
                        rot[0] = EulerAngleFromMatrix(boneRot, 2);
 
519
                        break;
 
520
                case IK_XDOF | IK_YDOF:
 
521
                        rot[1] = ComputeTwist(boneRot);
 
522
                        RemoveEulerAngleFromMatrix(boneRot, rot[1], 1);
 
523
                        rot[0] = EulerAngleFromMatrix(boneRot, 0);
 
524
                        break;
 
525
                case IK_SWING:
 
526
                        // RX+RZ
 
527
                        boneRot.GetXZRot().GetValue(rot);
 
528
                        break;
 
529
                case IK_YDOF | IK_ZDOF:
 
530
                        // RZ+RY
 
531
                        rot[1] = ComputeTwist(boneRot);
 
532
                        RemoveEulerAngleFromMatrix(boneRot, rot[1], 1);
 
533
                        rot[0] = EulerAngleFromMatrix(boneRot, 2);
 
534
                        break;
 
535
                case IK_SWING | IK_YDOF:
 
536
                        rot[2] = ComputeTwist(boneRot);
 
537
                        RemoveEulerAngleFromMatrix(boneRot, rot[2], 1);
 
538
                        boneRot.GetXZRot().GetValue(rot);
 
539
                        break;
 
540
                case IK_REVOLUTE:
 
541
                        boneRot.GetRot().GetValue(rot);
 
542
                        break;
545
543
        }
546
544
}
547
545
 
548
546
static bool target_callback(const iTaSC::Timestamp& timestamp, const iTaSC::Frame& current, iTaSC::Frame& next, void *param)
549
547
{
550
 
        IK_Target* target = (IK_Target*)param;
 
548
        IK_Target *target = (IK_Target *)param;
551
549
        // compute next target position
552
550
        // get target matrix from constraint.
553
 
        bConstraint* constraint = (bConstraint*)target->blenderConstraint;
 
551
        bConstraint *constraint = (bConstraint *)target->blenderConstraint;
554
552
        float tarmat[4][4];
555
553
 
556
 
        get_constraint_target_matrix(target->blscene, constraint, 0, CONSTRAINT_OBTYPE_OBJECT, target->owner, tarmat, 1.0);
 
554
        BKE_get_constraint_target_matrix(target->blscene, constraint, 0, CONSTRAINT_OBTYPE_OBJECT, target->owner, tarmat, 1.0);
557
555
 
558
556
        // rootmat contains the target pose in world coordinate
559
557
        // if enforce is != 1.0, blend the target position with the end effector position
562
560
                // eeRest is relative to the reference frame of the IK root
563
561
                // get this frame in world reference
564
562
                float restmat[4][4];
565
 
                bPoseChannel* pchan = target->rootChannel;
 
563
                bPoseChannel *pchan = target->rootChannel;
566
564
                if (pchan->parent) {
567
565
                        pchan = pchan->parent;
568
566
                        float chanmat[4][4];
569
567
                        copy_m4_m4(chanmat, pchan->pose_mat);
570
568
                        copy_v3_v3(chanmat[3], pchan->pose_tail);
571
569
                        mul_serie_m4(restmat, target->owner->obmat, chanmat, target->eeRest, NULL, NULL, NULL, NULL, NULL);
572
 
                } 
 
570
                }
573
571
                else {
574
572
                        mult_m4_m4m4(restmat, target->owner->obmat, target->eeRest);
575
573
                }
582
580
 
583
581
static bool base_callback(const iTaSC::Timestamp& timestamp, const iTaSC::Frame& current, iTaSC::Frame& next, void *param)
584
582
{
585
 
        IK_Scene* ikscene = (IK_Scene*)param;
 
583
        IK_Scene *ikscene = (IK_Scene *)param;
586
584
        // compute next armature base pose
587
 
        // algorithm: 
 
585
        // algorithm:
588
586
        // ikscene->pchan[0] is the root channel of the tree
589
587
        // if it has a parent, get the pose matrix from it and replace [3] by parent pchan->tail
590
588
        // then multiply by the armature matrix to get ikscene->armature base position
591
 
        bPoseChannel* pchan = ikscene->channels[0].pchan;
 
589
        bPoseChannel *pchan = ikscene->channels[0].pchan;
592
590
        float rootmat[4][4];
593
591
        if (pchan->parent) {
594
592
                pchan = pchan->parent;
595
593
                float chanmat[4][4];
596
594
                copy_m4_m4(chanmat, pchan->pose_mat);
597
595
                copy_v3_v3(chanmat[3], pchan->pose_tail);
598
 
                // save the base as a frame too so that we can compute deformation
599
 
                // after simulation
 
596
                // save the base as a frame too so that we can compute deformation after simulation
600
597
                ikscene->baseFrame.setValue(&chanmat[0][0]);
 
598
                // iTaSC armature is scaled to object scale, scale the base frame too
 
599
                ikscene->baseFrame.p *= ikscene->blScale;
601
600
                mult_m4_m4m4(rootmat, ikscene->blArmature->obmat, chanmat);
602
 
        } 
 
601
        }
603
602
        else {
604
603
                copy_m4_m4(rootmat, ikscene->blArmature->obmat);
605
604
                ikscene->baseFrame = iTaSC::F_identity;
608
607
        // if there is a polar target (only during solving otherwise we don't have end efffector)
609
608
        if (ikscene->polarConstraint && timestamp.update) {
610
609
                // compute additional rotation of base frame so that armature follows the polar target
611
 
                float imat[4][4];               // IK tree base inverse matrix
612
 
                float polemat[4][4];    // polar target in IK tree base frame
613
 
                float goalmat[4][4];    // target in IK tree base frame
614
 
                float mat[4][4];                // temp matrix
615
 
                bKinematicConstraint* poledata = (bKinematicConstraint*)ikscene->polarConstraint->data;
 
610
                float imat[4][4];       // IK tree base inverse matrix
 
611
                float polemat[4][4];    // polar target in IK tree base frame
 
612
                float goalmat[4][4];    // target in IK tree base frame
 
613
                float mat[4][4];        // temp matrix
 
614
                bKinematicConstraint *poledata = (bKinematicConstraint *)ikscene->polarConstraint->data;
616
615
 
617
616
                invert_m4_m4(imat, rootmat);
618
617
                // polar constraint imply only one target
621
620
                IK_Channel &rootchan = ikscene->channels[0];
622
621
 
623
622
                // get polar target matrix in world space
624
 
                get_constraint_target_matrix(ikscene->blscene, ikscene->polarConstraint, 1, CONSTRAINT_OBTYPE_OBJECT, ikscene->blArmature, mat, 1.0);
 
623
                BKE_get_constraint_target_matrix(ikscene->blscene, ikscene->polarConstraint, 1, CONSTRAINT_OBTYPE_OBJECT, ikscene->blArmature, mat, 1.0);
625
624
                // convert to armature space
626
625
                mult_m4_m4m4(polemat, imat, mat);
627
626
                // get the target in world space (was computed before as target object are defined before base object)
639
638
                KDL::Vector rootz = rootframe.M.UnitZ();
640
639
                // and compute root bone head
641
640
                double q_rest[3], q[3], length;
642
 
                const KDL::Joint* joint;
643
 
                const KDL::Frame* tip;
 
641
                const KDL::Joint *joint;
 
642
                const KDL::Frame *tip;
644
643
                ikscene->armature->getSegment(rootchan.tail, 3, joint, q_rest[0], q[0], tip);
645
644
                length = (joint->getType() == KDL::Joint::TransY) ? q[0] : tip->p(1);
646
 
                KDL::Vector rootpos = rootframe.p - length*rootframe.M.UnitY();
 
645
                KDL::Vector rootpos = rootframe.p - length *rootframe.M.UnitY();
647
646
 
648
 
                // compute main directions 
 
647
                // compute main directions
649
648
                KDL::Vector dir = KDL::Normalize(endpos - rootpos);
650
 
                KDL::Vector poledir = KDL::Normalize(goalpos-rootpos);
 
649
                KDL::Vector poledir = KDL::Normalize(goalpos - rootpos);
651
650
                // compute up directions
652
 
                KDL::Vector poleup = KDL::Normalize(polepos-rootpos);
653
 
                KDL::Vector up = rootx*KDL::cos(poledata->poleangle) + rootz*KDL::sin(poledata->poleangle);
 
651
                KDL::Vector poleup = KDL::Normalize(polepos - rootpos);
 
652
                KDL::Vector up = rootx * KDL::cos(poledata->poleangle) + rootz *KDL::sin(poledata->poleangle);
654
653
                // from which we build rotation matrix
655
654
                KDL::Rotation endrot, polerot;
656
655
                // for the armature, using the root bone orientation
657
 
                KDL::Vector x = KDL::Normalize(dir*up);
 
656
                KDL::Vector x = KDL::Normalize(dir * up);
658
657
                endrot.UnitX(x);
659
 
                endrot.UnitY(KDL::Normalize(x*dir));
 
658
                endrot.UnitY(KDL::Normalize(x * dir));
660
659
                endrot.UnitZ(-dir);
661
 
                // for the polar target 
662
 
                x = KDL::Normalize(poledir*poleup);
 
660
                // for the polar target
 
661
                x = KDL::Normalize(poledir * poleup);
663
662
                polerot.UnitX(x);
664
 
                polerot.UnitY(KDL::Normalize(x*poledir));
 
663
                polerot.UnitY(KDL::Normalize(x * poledir));
665
664
                polerot.UnitZ(-poledir);
666
665
                // the difference between the two is the rotation we want to apply
667
 
                KDL::Rotation result(polerot*endrot.Inverse());
 
666
                KDL::Rotation result(polerot * endrot.Inverse());
668
667
                // apply on base frame as this is an artificial additional rotation
669
 
                next.M = next.M*result;
670
 
                ikscene->baseFrame.M = ikscene->baseFrame.M*result;
 
668
                next.M = next.M * result;
 
669
                ikscene->baseFrame.M = ikscene->baseFrame.M * result;
671
670
        }
672
671
        return true;
673
672
}
674
673
 
675
 
static bool copypose_callback(const iTaSC::Timestamp& timestamp, iTaSC::ConstraintValues* const _values, unsigned int _nvalues, void* _param)
 
674
static bool copypose_callback(const iTaSC::Timestamp& timestamp, iTaSC::ConstraintValues *const _values, unsigned int _nvalues, void *_param)
676
675
{
677
 
        IK_Target* iktarget =(IK_Target*)_param;
 
676
        IK_Target *iktarget = (IK_Target *)_param;
678
677
        bKinematicConstraint *condata = (bKinematicConstraint *)iktarget->blenderConstraint->data;
679
 
        iTaSC::ConstraintValues* values = _values;
680
 
        bItasc* ikparam = (bItasc*) iktarget->owner->pose->ikparam;
 
678
        iTaSC::ConstraintValues *values = _values;
 
679
        bItasc *ikparam = (bItasc *) iktarget->owner->pose->ikparam;
681
680
 
682
681
        // we need default parameters
683
 
        if (!ikparam) 
 
682
        if (!ikparam)
684
683
                ikparam = &DefIKParam;
685
684
 
686
685
        if (iktarget->blenderConstraint->flag & CONSTRAINT_OFF) {
699
698
                if (iktarget->controlType & iTaSC::CopyPose::CTL_POSITION) {
700
699
                        // update error
701
700
                        values->alpha = condata->weight;
702
 
                        values->action = iTaSC::ACT_ALPHA|iTaSC::ACT_FEEDBACK;
 
701
                        values->action = iTaSC::ACT_ALPHA | iTaSC::ACT_FEEDBACK;
703
702
                        values->feedback = (iktarget->simulation) ? ikparam->feedback : ANIM_FEEDBACK;
704
703
                        values++;
705
704
                }
706
705
                if (iktarget->controlType & iTaSC::CopyPose::CTL_ROTATION) {
707
706
                        // update error
708
707
                        values->alpha = condata->orientweight;
709
 
                        values->action = iTaSC::ACT_ALPHA|iTaSC::ACT_FEEDBACK;
 
708
                        values->action = iTaSC::ACT_ALPHA | iTaSC::ACT_FEEDBACK;
710
709
                        values->feedback = (iktarget->simulation) ? ikparam->feedback : ANIM_FEEDBACK;
711
710
                        values++;
712
711
                }
714
713
        return true;
715
714
}
716
715
 
717
 
static void copypose_error(const iTaSC::ConstraintValues* values, unsigned int nvalues, IK_Target* iktarget)
 
716
static void copypose_error(const iTaSC::ConstraintValues *values, unsigned int nvalues, IK_Target *iktarget)
718
717
{
719
 
        iTaSC::ConstraintSingleValue* value;
 
718
        iTaSC::ConstraintSingleValue *value;
720
719
        double error;
721
720
        int i;
722
721
 
723
722
        if (iktarget->controlType & iTaSC::CopyPose::CTL_POSITION) {
724
723
                // update error
725
 
                for (i=0, error=0.0, value=values->values; i<values->number; ++i, ++value)
 
724
                for (i = 0, error = 0.0, value = values->values; i < values->number; ++i, ++value)
726
725
                        error += KDL::sqr(value->y - value->yd);
727
726
                iktarget->blenderConstraint->lin_error = (float)KDL::sqrt(error);
728
727
                values++;
729
728
        }
730
729
        if (iktarget->controlType & iTaSC::CopyPose::CTL_ROTATION) {
731
730
                // update error
732
 
                for (i=0, error=0.0, value=values->values; i<values->number; ++i, ++value)
 
731
                for (i = 0, error = 0.0, value = values->values; i < values->number; ++i, ++value)
733
732
                        error += KDL::sqr(value->y - value->yd);
734
733
                iktarget->blenderConstraint->rot_error = (float)KDL::sqrt(error);
735
734
                values++;
736
735
        }
737
736
}
738
737
 
739
 
static bool distance_callback(const iTaSC::Timestamp& timestamp, iTaSC::ConstraintValues* const _values, unsigned int _nvalues, void* _param)
 
738
static bool distance_callback(const iTaSC::Timestamp& timestamp, iTaSC::ConstraintValues *const _values, unsigned int _nvalues, void *_param)
740
739
{
741
 
        IK_Target* iktarget =(IK_Target*)_param;
 
740
        IK_Target *iktarget = (IK_Target *)_param;
742
741
        bKinematicConstraint *condata = (bKinematicConstraint *)iktarget->blenderConstraint->data;
743
 
        iTaSC::ConstraintValues* values = _values;
744
 
        bItasc* ikparam = (bItasc*) iktarget->owner->pose->ikparam;
 
742
        iTaSC::ConstraintValues *values = _values;
 
743
        bItasc *ikparam = (bItasc *) iktarget->owner->pose->ikparam;
745
744
        // we need default parameters
746
 
        if (!ikparam) 
 
745
        if (!ikparam)
747
746
                ikparam = &DefIKParam;
748
747
 
749
748
        // update weight according to mode
752
751
        }
753
752
        else {
754
753
                switch (condata->mode) {
755
 
                case LIMITDIST_INSIDE:
756
 
                        values->alpha = (values->values[0].y > condata->dist) ? condata->weight : 0.0;
757
 
                        break;
758
 
                case LIMITDIST_OUTSIDE:
759
 
                        values->alpha = (values->values[0].y < condata->dist) ? condata->weight : 0.0;
760
 
                        break;
761
 
                default:
762
 
                        values->alpha = condata->weight;
763
 
                        break;
764
 
                }       
 
754
                        case LIMITDIST_INSIDE:
 
755
                                values->alpha = (values->values[0].y > condata->dist) ? condata->weight : 0.0;
 
756
                                break;
 
757
                        case LIMITDIST_OUTSIDE:
 
758
                                values->alpha = (values->values[0].y < condata->dist) ? condata->weight : 0.0;
 
759
                                break;
 
760
                        default:
 
761
                                values->alpha = condata->weight;
 
762
                                break;
 
763
                }
765
764
                if (!timestamp.substep) {
766
765
                        // only update value on first timestep
767
766
                        switch (condata->mode) {
768
 
                        case LIMITDIST_INSIDE:
769
 
                                values->values[0].yd = condata->dist*0.95;
770
 
                                break;
771
 
                        case LIMITDIST_OUTSIDE:
772
 
                                values->values[0].yd = condata->dist*1.05;
773
 
                                break;
774
 
                        default:
775
 
                                values->values[0].yd = condata->dist;
776
 
                                break;
 
767
                                case LIMITDIST_INSIDE:
 
768
                                        values->values[0].yd = condata->dist * 0.95;
 
769
                                        break;
 
770
                                case LIMITDIST_OUTSIDE:
 
771
                                        values->values[0].yd = condata->dist * 1.05;
 
772
                                        break;
 
773
                                default:
 
774
                                        values->values[0].yd = condata->dist;
 
775
                                        break;
777
776
                        }
778
 
                        values->values[0].action = iTaSC::ACT_VALUE|iTaSC::ACT_FEEDBACK;
 
777
                        values->values[0].action = iTaSC::ACT_VALUE | iTaSC::ACT_FEEDBACK;
779
778
                        values->feedback = (iktarget->simulation) ? ikparam->feedback : ANIM_FEEDBACK;
780
779
                }
781
780
        }
783
782
        return true;
784
783
}
785
784
 
786
 
static void distance_error(const iTaSC::ConstraintValues* values, unsigned int _nvalues, IK_Target* iktarget)
 
785
static void distance_error(const iTaSC::ConstraintValues *values, unsigned int _nvalues, IK_Target *iktarget)
787
786
{
788
787
        iktarget->blenderConstraint->lin_error = (float)(values->values[0].y - values->values[0].yd);
789
788
}
790
789
 
791
 
static bool joint_callback(const iTaSC::Timestamp& timestamp, iTaSC::ConstraintValues* const _values, unsigned int _nvalues, void* _param)
 
790
static bool joint_callback(const iTaSC::Timestamp& timestamp, iTaSC::ConstraintValues *const _values, unsigned int _nvalues, void *_param)
792
791
{
793
 
        IK_Channel* ikchan = (IK_Channel*)_param;
794
 
        bItasc* ikparam = (bItasc*)ikchan->owner->pose->ikparam;
795
 
        bPoseChannel* chan = ikchan->pchan;
 
792
        IK_Channel *ikchan = (IK_Channel *)_param;
 
793
        bItasc *ikparam = (bItasc *)ikchan->owner->pose->ikparam;
 
794
        bPoseChannel *chan = ikchan->pchan;
796
795
        int dof;
797
796
 
798
797
        // a channel can be splitted into multiple joints, so we get called multiple
805
804
 
806
805
                if (chan->rotmode > 0) {
807
806
                        /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
808
 
                        eulO_to_mat3( rmat,chan->eul, chan->rotmode);
 
807
                        eulO_to_mat3(rmat, chan->eul, chan->rotmode);
809
808
                }
810
809
                else if (chan->rotmode == ROT_MODE_AXISANGLE) {
811
810
                        /* axis-angle - stored in quaternion data, but not really that great for 3D-changing orientations */
812
 
                        axis_angle_to_mat3( rmat,&chan->quat[1], chan->quat[0]);
 
811
                        axis_angle_to_mat3(rmat, &chan->quat[1], chan->quat[0]);
813
812
                }
814
813
                else {
815
 
                        /* quats are normalised before use to eliminate scaling issues */
 
814
                        /* quats are normalized before use to eliminate scaling issues */
816
815
                        normalize_qt(chan->quat);
817
 
                        quat_to_mat3( rmat,chan->quat);
 
816
                        quat_to_mat3(rmat, chan->quat);
818
817
                }
819
818
                KDL::Rotation jointRot(
820
 
                        rmat[0][0], rmat[1][0], rmat[2][0],
821
 
                        rmat[0][1], rmat[1][1], rmat[2][1],
822
 
                        rmat[0][2], rmat[1][2], rmat[2][2]);
 
819
                    rmat[0][0], rmat[1][0], rmat[2][0],
 
820
                    rmat[0][1], rmat[1][1], rmat[2][1],
 
821
                    rmat[0][2], rmat[1][2], rmat[2][2]);
823
822
                GetJointRotation(jointRot, ikchan->jointType, ikchan->jointValue);
824
823
                ikchan->jointValid = 1;
825
824
        }
826
825
        // determine which part of jointValue is used for this joint
827
826
        // closely related to the way the joints are defined
828
 
        switch (ikchan->jointType & ~IK_TRANSY)
829
 
        {
830
 
        case IK_XDOF:
831
 
        case IK_YDOF:
832
 
        case IK_ZDOF:
833
 
                dof = 0;
834
 
                break;
835
 
        case IK_XDOF|IK_YDOF:
836
 
                // X + Y
837
 
                dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RX) ? 0 : 1;
838
 
                break;
839
 
        case IK_SWING:
840
 
                // XZ
841
 
                dof = 0;
842
 
                break;
843
 
        case IK_YDOF|IK_ZDOF:
844
 
                // Z + Y
845
 
                dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RZ) ? 0 : 1;
846
 
                break;
847
 
        case IK_SWING|IK_YDOF:
848
 
                // XZ + Y
849
 
                dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RY) ? 2 : 0;
850
 
                break;
851
 
        case IK_REVOLUTE:
852
 
                dof = 0;
853
 
                break;
854
 
        default:
855
 
                dof = -1;
856
 
                break;
 
827
        switch (ikchan->jointType & ~IK_TRANSY) {
 
828
                case IK_XDOF:
 
829
                case IK_YDOF:
 
830
                case IK_ZDOF:
 
831
                        dof = 0;
 
832
                        break;
 
833
                case IK_XDOF | IK_YDOF:
 
834
                        // X + Y
 
835
                        dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RX) ? 0 : 1;
 
836
                        break;
 
837
                case IK_SWING:
 
838
                        // XZ
 
839
                        dof = 0;
 
840
                        break;
 
841
                case IK_YDOF | IK_ZDOF:
 
842
                        // Z + Y
 
843
                        dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RZ) ? 0 : 1;
 
844
                        break;
 
845
                case IK_SWING | IK_YDOF:
 
846
                        // XZ + Y
 
847
                        dof = (_values[0].id == iTaSC::Armature::ID_JOINT_RY) ? 2 : 0;
 
848
                        break;
 
849
                case IK_REVOLUTE:
 
850
                        dof = 0;
 
851
                        break;
 
852
                default:
 
853
                        dof = -1;
 
854
                        break;
857
855
        }
858
856
        if (dof >= 0) {
859
 
                for (unsigned int i=0; i<_nvalues; i++, dof++) {
 
857
                for (unsigned int i = 0; i < _nvalues; i++, dof++) {
860
858
                        _values[i].values[0].yd = ikchan->jointValue[dof];
861
859
                        _values[i].alpha = chan->ikrotweight;
862
860
                        _values[i].feedback = ikparam->feedback;
866
864
}
867
865
 
868
866
// build array of joint corresponding to IK chain
869
 
static int convert_channels(IK_Scene *ikscene, PoseTree *tree)
 
867
static int convert_channels(IK_Scene *ikscene, PoseTree *tree, float ctime)
870
868
{
871
869
        IK_Channel *ikchan;
872
870
        bPoseChannel *pchan;
873
871
        int a, flag, njoint;
874
872
 
875
873
        njoint = 0;
876
 
        for (a=0, ikchan = ikscene->channels; a<ikscene->numchan; ++a, ++ikchan) {
877
 
                pchan= tree->pchan[a];
 
874
        for (a = 0, ikchan = ikscene->channels; a < ikscene->numchan; ++a, ++ikchan) {
 
875
                pchan = tree->pchan[a];
878
876
                ikchan->pchan = pchan;
879
 
                ikchan->parent = (a>0) ? tree->parent[a] : -1;
 
877
                ikchan->parent = (a > 0) ? tree->parent[a] : -1;
880
878
                ikchan->owner = ikscene->blArmature;
881
 
                
 
879
 
 
880
                // the constraint and channels must be applied before we build the iTaSC scene,
 
881
                // this is because some of the pose data (e.g. pose head) don't have corresponding 
 
882
                // joint angles and can't be applied to the iTaSC armature dynamically
 
883
                if (!(pchan->flag & POSE_DONE))
 
884
                        BKE_pose_where_is_bone(ikscene->blscene, ikscene->blArmature, pchan, ctime, 1);
 
885
                // tell blender that this channel was controlled by IK, it's cleared on each BKE_pose_where_is()
 
886
                pchan->flag |= (POSE_DONE | POSE_CHAIN);
 
887
 
882
888
                /* set DoF flag */
883
889
                flag = 0;
884
 
                if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP) && 
885
 
                        (!(pchan->ikflag & BONE_IK_XLIMIT) || pchan->limitmin[0]<0.f || pchan->limitmax[0]>0.f))
 
890
                if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP) &&
 
891
                    (!(pchan->ikflag & BONE_IK_XLIMIT) || pchan->limitmin[0] < 0.f || pchan->limitmax[0] > 0.f))
 
892
                {
886
893
                        flag |= IK_XDOF;
 
894
                }
887
895
                if (!(pchan->ikflag & BONE_IK_NO_YDOF) && !(pchan->ikflag & BONE_IK_NO_YDOF_TEMP) &&
888
 
                        (!(pchan->ikflag & BONE_IK_YLIMIT) || pchan->limitmin[1]<0.f || pchan->limitmax[1]>0.f))
 
896
                    (!(pchan->ikflag & BONE_IK_YLIMIT) || pchan->limitmin[1] < 0.f || pchan->limitmax[1] > 0.f))
 
897
                {
889
898
                        flag |= IK_YDOF;
 
899
                }
890
900
                if (!(pchan->ikflag & BONE_IK_NO_ZDOF) && !(pchan->ikflag & BONE_IK_NO_ZDOF_TEMP) &&
891
 
                        (!(pchan->ikflag & BONE_IK_ZLIMIT) || pchan->limitmin[2]<0.f || pchan->limitmax[2]>0.f))
 
901
                    (!(pchan->ikflag & BONE_IK_ZLIMIT) || pchan->limitmin[2] < 0.f || pchan->limitmax[2] > 0.f))
 
902
                {
892
903
                        flag |= IK_ZDOF;
893
 
                
 
904
                }
 
905
 
894
906
                if (tree->stretch && (pchan->ikstretch > 0.0)) {
895
907
                        flag |= IK_TRANSY;
896
908
                }
921
933
                 * bone length is computed from bone->length multiplied by the scaling factor of
922
934
                 * the armature. Non-uniform scaling will give bad result!
923
935
                 */
924
 
                switch (flag & (IK_XDOF|IK_YDOF|IK_ZDOF))
925
 
                {
926
 
                default:
927
 
                        ikchan->jointType = 0;
928
 
                        ikchan->ndof = 0;
929
 
                        break;
930
 
                case IK_XDOF:
931
 
                        // RX only, get the X rotation
932
 
                        ikchan->jointType = IK_XDOF;
933
 
                        ikchan->ndof = 1;
934
 
                        break;
935
 
                case IK_YDOF:
936
 
                        // RY only, get the Y rotation
937
 
                        ikchan->jointType = IK_YDOF;
938
 
                        ikchan->ndof = 1;
939
 
                        break;
940
 
                case IK_ZDOF:
941
 
                        // RZ only, get the Zz rotation
942
 
                        ikchan->jointType = IK_ZDOF;
943
 
                        ikchan->ndof = 1;
944
 
                        break;
945
 
                case IK_XDOF|IK_YDOF:
946
 
                        ikchan->jointType = IK_XDOF|IK_YDOF;
947
 
                        ikchan->ndof = 2;
948
 
                        break;
949
 
                case IK_XDOF|IK_ZDOF:
950
 
                        // RX+RZ
951
 
                        ikchan->jointType = IK_SWING;
952
 
                        ikchan->ndof = 2;
953
 
                        break;
954
 
                case IK_YDOF|IK_ZDOF:
955
 
                        // RZ+RY
956
 
                        ikchan->jointType = IK_ZDOF|IK_YDOF;
957
 
                        ikchan->ndof = 2;
958
 
                        break;
959
 
                case IK_XDOF|IK_YDOF|IK_ZDOF:
960
 
                        // spherical joint
961
 
                        if (pchan->ikflag & (BONE_IK_XLIMIT|BONE_IK_YLIMIT|BONE_IK_ZLIMIT))
962
 
                                // decompose in a Swing+RotY joint
963
 
                                ikchan->jointType = IK_SWING|IK_YDOF;
964
 
                        else
965
 
                                ikchan->jointType = IK_REVOLUTE;
966
 
                        ikchan->ndof = 3;
967
 
                        break;
 
936
                switch (flag & (IK_XDOF | IK_YDOF | IK_ZDOF)) {
 
937
                        default:
 
938
                                ikchan->jointType = 0;
 
939
                                ikchan->ndof = 0;
 
940
                                break;
 
941
                        case IK_XDOF:
 
942
                                // RX only, get the X rotation
 
943
                                ikchan->jointType = IK_XDOF;
 
944
                                ikchan->ndof = 1;
 
945
                                break;
 
946
                        case IK_YDOF:
 
947
                                // RY only, get the Y rotation
 
948
                                ikchan->jointType = IK_YDOF;
 
949
                                ikchan->ndof = 1;
 
950
                                break;
 
951
                        case IK_ZDOF:
 
952
                                // RZ only, get the Zz rotation
 
953
                                ikchan->jointType = IK_ZDOF;
 
954
                                ikchan->ndof = 1;
 
955
                                break;
 
956
                        case IK_XDOF | IK_YDOF:
 
957
                                ikchan->jointType = IK_XDOF | IK_YDOF;
 
958
                                ikchan->ndof = 2;
 
959
                                break;
 
960
                        case IK_XDOF | IK_ZDOF:
 
961
                                // RX+RZ
 
962
                                ikchan->jointType = IK_SWING;
 
963
                                ikchan->ndof = 2;
 
964
                                break;
 
965
                        case IK_YDOF | IK_ZDOF:
 
966
                                // RZ+RY
 
967
                                ikchan->jointType = IK_ZDOF | IK_YDOF;
 
968
                                ikchan->ndof = 2;
 
969
                                break;
 
970
                        case IK_XDOF | IK_YDOF | IK_ZDOF:
 
971
                                // spherical joint
 
972
                                if (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_YLIMIT | BONE_IK_ZLIMIT))
 
973
                                        // decompose in a Swing+RotY joint
 
974
                                        ikchan->jointType = IK_SWING | IK_YDOF;
 
975
                                else
 
976
                                        ikchan->jointType = IK_REVOLUTE;
 
977
                                ikchan->ndof = 3;
 
978
                                break;
968
979
                }
969
980
                if (flag & IK_TRANSY) {
970
981
                        ikchan->jointType |= IK_TRANSY;
985
996
        bPoseChannel *pchan;
986
997
        IK_Channel *ikchan;
987
998
        Bone *bone;
988
 
        float rmat[4][4];       // rest pose of bone with parent taken into account
989
 
        float bmat[4][4];       // difference
 
999
        float rmat[4][4];   // rest pose of bone with parent taken into account
 
1000
        float bmat[4][4];   // difference
990
1001
        float scale;
991
1002
        double *rot;
992
1003
        int a, joint;
993
1004
 
994
1005
        // assume uniform scaling and take Y scale as general scale for the armature
995
1006
        scale = len_v3(ikscene->blArmature->obmat[1]);
996
 
        rot = &ikscene->jointArray(0);
997
 
        for (joint=a=0, ikchan = ikscene->channels; a<ikscene->numchan && joint<ikscene->numjoint; ++a, ++ikchan) {
998
 
                pchan= ikchan->pchan;
999
 
                bone= pchan->bone;
1000
 
                
 
1007
        rot = ikscene->jointArray(0);
 
1008
        for (joint = a = 0, ikchan = ikscene->channels; a < ikscene->numchan && joint < ikscene->numjoint; ++a, ++ikchan) {
 
1009
                pchan = ikchan->pchan;
 
1010
                bone = pchan->bone;
 
1011
 
1001
1012
                if (pchan->parent) {
1002
1013
                        unit_m4(bmat);
1003
1014
                        mul_m4_m4m3(bmat, pchan->parent->pose_mat, bone->bone_mat);
1011
1022
                boneRot.setValue(bmat[0]);
1012
1023
                GetJointRotation(boneRot, ikchan->jointType, rot);
1013
1024
                if (ikchan->jointType & IK_TRANSY) {
1014
 
                        // compute actual length 
1015
 
                        rot[ikchan->ndof-1] = len_v3v3(pchan->pose_tail, pchan->pose_head) * scale;
1016
 
                } 
 
1025
                        // compute actual length
 
1026
                        rot[ikchan->ndof - 1] = len_v3v3(pchan->pose_tail, pchan->pose_head) * scale;
 
1027
                }
1017
1028
                rot += ikchan->ndof;
1018
1029
                joint += ikchan->ndof;
1019
1030
        }
1020
1031
}
1021
1032
 
1022
1033
// compute array of joint value corresponding to current pose
1023
 
static void rest_pose(IK_Scene *ikscene)
 
1034
static void BKE_pose_rest(IK_Scene *ikscene)
1024
1035
{
1025
1036
        bPoseChannel *pchan;
1026
1037
        IK_Channel *ikchan;
1031
1042
 
1032
1043
        // assume uniform scaling and take Y scale as general scale for the armature
1033
1044
        scale = len_v3(ikscene->blArmature->obmat[1]);
1034
 
        // rest pose is 0 
 
1045
        // rest pose is 0
1035
1046
        SetToZero(ikscene->jointArray);
1036
1047
        // except for transY joints
1037
 
        rot = &ikscene->jointArray(0);
1038
 
        for (joint=a=0, ikchan = ikscene->channels; a<ikscene->numchan && joint<ikscene->numjoint; ++a, ++ikchan) {
1039
 
                pchan= ikchan->pchan;
1040
 
                bone= pchan->bone;
 
1048
        rot = ikscene->jointArray(0);
 
1049
        for (joint = a = 0, ikchan = ikscene->channels; a < ikscene->numchan && joint < ikscene->numjoint; ++a, ++ikchan) {
 
1050
                pchan = ikchan->pchan;
 
1051
                bone = pchan->bone;
1041
1052
 
1042
1053
                if (ikchan->jointType & IK_TRANSY)
1043
 
                        rot[ikchan->ndof-1] = bone->length*scale;
 
1054
                        rot[ikchan->ndof - 1] = bone->length * scale;
1044
1055
                rot += ikchan->ndof;
1045
1056
                joint += ikchan->ndof;
1046
1057
        }
1047
1058
}
1048
1059
 
1049
 
static IK_Scene* convert_tree(Scene *blscene, Object *ob, bPoseChannel *pchan)
 
1060
static IK_Scene *convert_tree(Scene *blscene, Object *ob, bPoseChannel *pchan, float ctime)
1050
1061
{
1051
 
        PoseTree *tree = (PoseTree*)pchan->iktree.first;
 
1062
        PoseTree *tree = (PoseTree *)pchan->iktree.first;
1052
1063
        PoseTarget *target;
1053
1064
        bKinematicConstraint *condata;
1054
1065
        bConstraint *polarcon;
1055
1066
        bItasc *ikparam;
1056
 
        iTaSC::Armature* arm;
1057
 
        iTaSC::Scene* scene;
1058
 
        IK_Scene* ikscene;
1059
 
        IK_Channel* ikchan;
 
1067
        iTaSC::Armature *arm;
 
1068
        iTaSC::Scene *scene;
 
1069
        IK_Scene *ikscene;
 
1070
        IK_Channel *ikchan;
1060
1071
        KDL::Frame initPose;
1061
1072
        KDL::Rotation boneRot;
1062
1073
        Bone *bone;
1065
1076
        float length;
1066
1077
        bool ret = true, ingame;
1067
1078
        double *rot;
 
1079
        float start[3];
1068
1080
 
1069
1081
        if (tree->totchannel == 0)
1070
1082
                return NULL;
1077
1089
        ikscene->numchan = tree->totchannel;
1078
1090
        ikscene->armature = arm;
1079
1091
        ikscene->scene = scene;
1080
 
        ikparam = (bItasc*)ob->pose->ikparam;
 
1092
        ikparam = (bItasc *)ob->pose->ikparam;
1081
1093
        ingame = (ob->pose->flag & POSE_GAME_ENGINE);
1082
1094
        if (!ikparam) {
1083
1095
                // you must have our own copy
1089
1101
                // shorter than in animation => move to auto step automatically and set
1090
1102
                // the target substep duration via min/max
1091
1103
                if (!(ikparam->flag & ITASC_AUTO_STEP)) {
1092
 
                        float timestep = blscene->r.frs_sec_base/blscene->r.frs_sec;
 
1104
                        float timestep = blscene->r.frs_sec_base / blscene->r.frs_sec;
1093
1105
                        if (ikparam->numstep > 0)
1094
1106
                                timestep /= ikparam->numstep;
1095
1107
                        // with equal min and max, the algorythm will take this step and the indicative substep most of the time
1102
1114
                ikscene->cache = new iTaSC::Cache();
1103
1115
 
1104
1116
        switch (ikparam->solver) {
1105
 
        case ITASC_SOLVER_SDLS:
1106
 
                ikscene->solver = new iTaSC::WSDLSSolver();
1107
 
                break;
1108
 
        case ITASC_SOLVER_DLS:
1109
 
                ikscene->solver = new iTaSC::WDLSSolver();
1110
 
                break;
1111
 
        default:
1112
 
                delete ikscene;
1113
 
                return NULL;
 
1117
                case ITASC_SOLVER_SDLS:
 
1118
                        ikscene->solver = new iTaSC::WSDLSSolver();
 
1119
                        break;
 
1120
                case ITASC_SOLVER_DLS:
 
1121
                        ikscene->solver = new iTaSC::WDLSSolver();
 
1122
                        break;
 
1123
                default:
 
1124
                        delete ikscene;
 
1125
                        return NULL;
1114
1126
        }
1115
1127
        ikscene->blArmature = ob;
 
1128
        // assume uniform scaling and take Y scale as general scale for the armature
 
1129
        ikscene->blScale = len_v3(ob->obmat[1]);
 
1130
        ikscene->blInvScale = (ikscene->blScale < KDL::epsilon) ? 0.0f : 1.0f / ikscene->blScale;
1116
1131
 
1117
 
        std::string  joint;
1118
 
        std::string  root("root");
1119
 
        std::string  parent;
 
1132
        std::string joint;
 
1133
        std::string root("root");
 
1134
        std::string parent;
1120
1135
        std::vector<double> weights;
1121
1136
        double weight[3];
1122
 
        // assume uniform scaling and take Y scale as general scale for the armature
1123
 
        float scale = len_v3(ob->obmat[1]);
1124
1137
        // build the array of joints corresponding to the IK chain
1125
 
        convert_channels(ikscene, tree);
 
1138
        convert_channels(ikscene, tree, ctime);
1126
1139
        if (ingame) {
1127
1140
                // in the GE, set the initial joint angle to match the current pose
1128
1141
                // this will update the jointArray in ikscene
1130
1143
        }
1131
1144
        else {
1132
1145
                // in Blender, the rest pose is always 0 for joints
1133
 
                rest_pose(ikscene);
 
1146
                BKE_pose_rest(ikscene);
1134
1147
        }
1135
 
        rot = &ikscene->jointArray(0);
1136
 
        for (a=0, ikchan = ikscene->channels; a<tree->totchannel; ++a, ++ikchan) {
1137
 
                pchan= ikchan->pchan;
1138
 
                bone= pchan->bone;
 
1148
        rot = ikscene->jointArray(0);
 
1149
 
 
1150
        for (a = 0, ikchan = ikscene->channels; a < tree->totchannel; ++a, ++ikchan) {
 
1151
                pchan = ikchan->pchan;
 
1152
                bone = pchan->bone;
1139
1153
 
1140
1154
                KDL::Frame tip(iTaSC::F_identity);
 
1155
                // compute the position and rotation of the head from previous segment
1141
1156
                Vector3 *fl = bone->bone_mat;
1142
1157
                KDL::Rotation brot(
1143
 
                                                   fl[0][0], fl[1][0], fl[2][0],
1144
 
                                                   fl[0][1], fl[1][1], fl[2][1],
1145
 
                                                   fl[0][2], fl[1][2], fl[2][2]);
1146
 
                KDL::Vector bpos(bone->head[0], bone->head[1], bone->head[2]);
1147
 
                bpos = bpos*scale;
 
1158
                    fl[0][0], fl[1][0], fl[2][0],
 
1159
                    fl[0][1], fl[1][1], fl[2][1],
 
1160
                    fl[0][2], fl[1][2], fl[2][2]);
 
1161
                // if the bone is disconnected, the head is movable in pose mode
 
1162
                // take that into account by using pose matrix instead of bone
 
1163
                // Note that pose is expressed in armature space, convert to previous bone space
 
1164
                {
 
1165
                        float R_parmat[3][3];
 
1166
                        float iR_parmat[3][3];
 
1167
                        if (pchan->parent)
 
1168
                                copy_m3_m4(R_parmat, pchan->parent->pose_mat);
 
1169
                        else
 
1170
                                unit_m3(R_parmat);
 
1171
                        if (pchan->parent)
 
1172
                                sub_v3_v3v3(start, pchan->pose_head, pchan->parent->pose_tail);
 
1173
                        else
 
1174
                                start[0] = start[1] = start[2] = 0.0f;
 
1175
                        invert_m3_m3(iR_parmat, R_parmat);
 
1176
                        normalize_m3(iR_parmat);
 
1177
                        mul_m3_v3(iR_parmat, start);
 
1178
                }
 
1179
                KDL::Vector bpos(start[0], start[1], start[2]);
 
1180
                bpos *= ikscene->blScale;
1148
1181
                KDL::Frame head(brot, bpos);
1149
 
                
 
1182
 
1150
1183
                // rest pose length of the bone taking scaling into account
1151
 
                length= bone->length*scale;
 
1184
                length = bone->length * ikscene->blScale;
1152
1185
                parent = (a > 0) ? ikscene->channels[tree->parent[a]].tail : root;
1153
1186
                // first the fixed segment to the bone head
1154
 
                if (head.p.Norm() > KDL::epsilon || head.M.GetRot().Norm() > KDL::epsilon) {
 
1187
                if (!(ikchan->pchan->bone->flag & BONE_CONNECTED) || head.M.GetRot().Norm() > KDL::epsilon) {
1155
1188
                        joint = bone->name;
1156
1189
                        joint += ":H";
1157
1190
                        ret = arm->addSegment(joint, parent, KDL::Joint::None, 0.0, head);
1162
1195
                        tip.p[1] = length;
1163
1196
                }
1164
1197
                joint = bone->name;
1165
 
                weight[0] = (1.0-pchan->stiffness[0]);
1166
 
                weight[1] = (1.0-pchan->stiffness[1]);
1167
 
                weight[2] = (1.0-pchan->stiffness[2]);
1168
 
                switch (ikchan->jointType & ~IK_TRANSY)
1169
 
                {
1170
 
                case 0:
1171
 
                        // fixed bone
1172
 
                        if (!(ikchan->jointType & IK_TRANSY)) {
 
1198
                weight[0] = (1.0 - pchan->stiffness[0]);
 
1199
                weight[1] = (1.0 - pchan->stiffness[1]);
 
1200
                weight[2] = (1.0 - pchan->stiffness[2]);
 
1201
                switch (ikchan->jointType & ~IK_TRANSY) {
 
1202
                        case 0:
 
1203
                                // fixed bone
1173
1204
                                joint += ":F";
1174
1205
                                ret = arm->addSegment(joint, parent, KDL::Joint::None, 0.0, tip);
1175
 
                        }
1176
 
                        break;
1177
 
                case IK_XDOF:
1178
 
                        // RX only, get the X rotation
1179
 
                        joint += ":RX";
1180
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::RotX, rot[0], tip);
1181
 
                        weights.push_back(weight[0]);
1182
 
                        break;
1183
 
                case IK_YDOF:
1184
 
                        // RY only, get the Y rotation
1185
 
                        joint += ":RY";
1186
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[0], tip);
1187
 
                        weights.push_back(weight[1]);
1188
 
                        break;
1189
 
                case IK_ZDOF:
1190
 
                        // RZ only, get the Zz rotation
1191
 
                        joint += ":RZ";
1192
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::RotZ, rot[0], tip);
1193
 
                        weights.push_back(weight[2]);
1194
 
                        break;
1195
 
                case IK_XDOF|IK_YDOF:
1196
 
                        joint += ":RX";
1197
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::RotX, rot[0]);
1198
 
                        weights.push_back(weight[0]);
1199
 
                        if (ret) {
1200
 
                                parent = joint;
1201
 
                                joint = bone->name;
1202
 
                                joint += ":RY";
1203
 
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[1], tip);
1204
 
                                weights.push_back(weight[1]);
1205
 
                        }
1206
 
                        break;
1207
 
                case IK_SWING:
1208
 
                        joint += ":SW";
1209
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::Swing, rot[0], tip);
1210
 
                        weights.push_back(weight[0]);
1211
 
                        weights.push_back(weight[2]);
1212
 
                        break;
1213
 
                case IK_YDOF|IK_ZDOF:
1214
 
                        // RZ+RY
1215
 
                        joint += ":RZ";
1216
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::RotZ, rot[0]);
1217
 
                        weights.push_back(weight[2]);
1218
 
                        if (ret) {
1219
 
                                parent = joint;
1220
 
                                joint = bone->name;
1221
 
                                joint += ":RY";
1222
 
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[1], tip);
1223
 
                                weights.push_back(weight[1]);
1224
 
                        }
1225
 
                        break;
1226
 
                case IK_SWING|IK_YDOF:
1227
 
                        // decompose in a Swing+RotY joint
1228
 
                        joint += ":SW";
1229
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::Swing, rot[0]);
1230
 
                        weights.push_back(weight[0]);
1231
 
                        weights.push_back(weight[2]);
1232
 
                        if (ret) {
1233
 
                                parent = joint;
1234
 
                                joint = bone->name;
1235
 
                                joint += ":RY";
1236
 
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[2], tip);
1237
 
                                weights.push_back(weight[1]);
1238
 
                        }
1239
 
                        break;
1240
 
                case IK_REVOLUTE:
1241
 
                        joint += ":SJ";
1242
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::Sphere, rot[0], tip);
1243
 
                        weights.push_back(weight[0]);
1244
 
                        weights.push_back(weight[1]);
1245
 
                        weights.push_back(weight[2]);
1246
 
                        break;
 
1206
                                break;
 
1207
                        case IK_XDOF:
 
1208
                                // RX only, get the X rotation
 
1209
                                joint += ":RX";
 
1210
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotX, rot[0], tip);
 
1211
                                weights.push_back(weight[0]);
 
1212
                                break;
 
1213
                        case IK_YDOF:
 
1214
                                // RY only, get the Y rotation
 
1215
                                joint += ":RY";
 
1216
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[0], tip);
 
1217
                                weights.push_back(weight[1]);
 
1218
                                break;
 
1219
                        case IK_ZDOF:
 
1220
                                // RZ only, get the Zz rotation
 
1221
                                joint += ":RZ";
 
1222
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotZ, rot[0], tip);
 
1223
                                weights.push_back(weight[2]);
 
1224
                                break;
 
1225
                        case IK_XDOF | IK_YDOF:
 
1226
                                joint += ":RX";
 
1227
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotX, rot[0]);
 
1228
                                weights.push_back(weight[0]);
 
1229
                                if (ret) {
 
1230
                                        parent = joint;
 
1231
                                        joint = bone->name;
 
1232
                                        joint += ":RY";
 
1233
                                        ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[1], tip);
 
1234
                                        weights.push_back(weight[1]);
 
1235
                                }
 
1236
                                break;
 
1237
                        case IK_SWING:
 
1238
                                joint += ":SW";
 
1239
                                ret = arm->addSegment(joint, parent, KDL::Joint::Swing, rot[0], tip);
 
1240
                                weights.push_back(weight[0]);
 
1241
                                weights.push_back(weight[2]);
 
1242
                                break;
 
1243
                        case IK_YDOF | IK_ZDOF:
 
1244
                                // RZ+RY
 
1245
                                joint += ":RZ";
 
1246
                                ret = arm->addSegment(joint, parent, KDL::Joint::RotZ, rot[0]);
 
1247
                                weights.push_back(weight[2]);
 
1248
                                if (ret) {
 
1249
                                        parent = joint;
 
1250
                                        joint = bone->name;
 
1251
                                        joint += ":RY";
 
1252
                                        ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[1], tip);
 
1253
                                        weights.push_back(weight[1]);
 
1254
                                }
 
1255
                                break;
 
1256
                        case IK_SWING | IK_YDOF:
 
1257
                                // decompose in a Swing+RotY joint
 
1258
                                joint += ":SW";
 
1259
                                ret = arm->addSegment(joint, parent, KDL::Joint::Swing, rot[0]);
 
1260
                                weights.push_back(weight[0]);
 
1261
                                weights.push_back(weight[2]);
 
1262
                                if (ret) {
 
1263
                                        parent = joint;
 
1264
                                        joint = bone->name;
 
1265
                                        joint += ":RY";
 
1266
                                        ret = arm->addSegment(joint, parent, KDL::Joint::RotY, rot[2], tip);
 
1267
                                        weights.push_back(weight[1]);
 
1268
                                }
 
1269
                                break;
 
1270
                        case IK_REVOLUTE:
 
1271
                                joint += ":SJ";
 
1272
                                ret = arm->addSegment(joint, parent, KDL::Joint::Sphere, rot[0], tip);
 
1273
                                weights.push_back(weight[0]);
 
1274
                                weights.push_back(weight[1]);
 
1275
                                weights.push_back(weight[2]);
 
1276
                                break;
1247
1277
                }
1248
1278
                if (ret && (ikchan->jointType & IK_TRANSY)) {
1249
1279
                        parent = joint;
1250
1280
                        joint = bone->name;
1251
1281
                        joint += ":TY";
1252
 
                        ret = arm->addSegment(joint, parent, KDL::Joint::TransY, rot[ikchan->ndof-1]);
1253
 
                        float ikstretch = pchan->ikstretch*pchan->ikstretch;
1254
 
                        weight[1] = (1.0-MIN2(1.0-ikstretch, 0.99));
 
1282
                        ret = arm->addSegment(joint, parent, KDL::Joint::TransY, rot[ikchan->ndof - 1]);
 
1283
                        const float ikstretch = pchan->ikstretch * pchan->ikstretch;
 
1284
                        /* why invert twice here? */
 
1285
                        weight[1] = (1.0 - min_ff(1.0 - ikstretch, 1.0f - 0.001f));
1255
1286
                        weights.push_back(weight[1]);
1256
1287
                }
1257
1288
                if (!ret)
1262
1293
                ikchan->head = parent;
1263
1294
                // in case of error
1264
1295
                ret = false;
1265
 
                if ((ikchan->jointType & IK_XDOF) && (pchan->ikflag & (BONE_IK_XLIMIT|BONE_IK_ROTCTL))) {
 
1296
                if ((ikchan->jointType & IK_XDOF) && (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ROTCTL))) {
1266
1297
                        joint = bone->name;
1267
1298
                        joint += ":RX";
1268
1299
                        if (pchan->ikflag & BONE_IK_XLIMIT) {
1274
1305
                                        break;
1275
1306
                        }
1276
1307
                }
1277
 
                if ((ikchan->jointType & IK_YDOF) && (pchan->ikflag & (BONE_IK_YLIMIT|BONE_IK_ROTCTL))) {
 
1308
                if ((ikchan->jointType & IK_YDOF) && (pchan->ikflag & (BONE_IK_YLIMIT | BONE_IK_ROTCTL))) {
1278
1309
                        joint = bone->name;
1279
1310
                        joint += ":RY";
1280
1311
                        if (pchan->ikflag & BONE_IK_YLIMIT) {
1286
1317
                                        break;
1287
1318
                        }
1288
1319
                }
1289
 
                if ((ikchan->jointType & IK_ZDOF) && (pchan->ikflag & (BONE_IK_ZLIMIT|BONE_IK_ROTCTL))) {
 
1320
                if ((ikchan->jointType & IK_ZDOF) && (pchan->ikflag & (BONE_IK_ZLIMIT | BONE_IK_ROTCTL))) {
1290
1321
                        joint = bone->name;
1291
1322
                        joint += ":RZ";
1292
1323
                        if (pchan->ikflag & BONE_IK_ZLIMIT) {
1298
1329
                                        break;
1299
1330
                        }
1300
1331
                }
1301
 
                if ((ikchan->jointType & IK_SWING) && (pchan->ikflag & (BONE_IK_XLIMIT|BONE_IK_ZLIMIT|BONE_IK_ROTCTL))) {
 
1332
                if ((ikchan->jointType & IK_SWING) && (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ZLIMIT | BONE_IK_ROTCTL))) {
1302
1333
                        joint = bone->name;
1303
1334
                        joint += ":SW";
1304
1335
                        if (pchan->ikflag & BONE_IK_XLIMIT) {
1329
1360
                return NULL;
1330
1361
        }
1331
1362
        // for each target, we need to add an end effector in the armature
1332
 
        for (numtarget=0, polarcon=NULL, ret = true, target=(PoseTarget*)tree->targets.first; target; target=(PoseTarget*)target->next) {
1333
 
                condata= (bKinematicConstraint*)target->con->data;
 
1363
        for (numtarget = 0, polarcon = NULL, ret = true, target = (PoseTarget *)tree->targets.first; target; target = (PoseTarget *)target->next) {
 
1364
                condata = (bKinematicConstraint *)target->con->data;
1334
1365
                pchan = tree->pchan[target->tip];
1335
1366
 
1336
1367
                if (is_cartesian_constraint(target->con)) {
1337
1368
                        // add the end effector
1338
 
                        IK_Target* iktarget = new IK_Target();
 
1369
                        IK_Target *iktarget = new IK_Target();
1339
1370
                        ikscene->targets.push_back(iktarget);
1340
1371
                        iktarget->ee = arm->addEndEffector(ikscene->channels[target->tip].tail);
1341
1372
                        if (iktarget->ee == -1) {
1365
1396
                ikscene->polarConstraint = polarcon;
1366
1397
        }
1367
1398
        // we can now add the armature
1368
 
        // the armature is based on a moving frame. 
 
1399
        // the armature is based on a moving frame.
1369
1400
        // initialize with the correct position in case there is no cache
1370
1401
        base_callback(iTaSC::Timestamp(), iTaSC::F_identity, initPose, ikscene);
1371
1402
        ikscene->base = new iTaSC::MovingFrame(initPose);
1385
1416
        // set the weight
1386
1417
        e_matrix& Wq = arm->getWq();
1387
1418
        assert(Wq.cols() == (int)weights.size());
1388
 
        for (int q=0; q<Wq.cols(); q++)
1389
 
                Wq(q,q)=weights[q];
 
1419
        for (int q = 0; q < Wq.cols(); q++)
 
1420
                Wq(q, q) = weights[q];
1390
1421
        // get the inverse rest pose frame of the base to compute relative rest pose of end effectors
1391
1422
        // this is needed to handle the enforce parameter
1392
1423
        // ikscene->pchan[0] is the root channel of the tree
1394
1425
        float invBaseFrame[4][4];
1395
1426
        pchan = ikscene->channels[0].pchan;
1396
1427
        if (pchan->parent) {
1397
 
                // it has a parent, get the pose matrix from it 
 
1428
                // it has a parent, get the pose matrix from it
1398
1429
                float baseFrame[4][4];
1399
 
                pchan = pchan->parent;  
 
1430
                pchan = pchan->parent;
1400
1431
                copy_m4_m4(baseFrame, pchan->bone->arm_mat);
1401
1432
                // move to the tail and scale to get rest pose of armature base
1402
1433
                copy_v3_v3(baseFrame[3], pchan->bone->arm_tail);
1406
1437
                unit_m4(invBaseFrame);
1407
1438
        }
1408
1439
        // finally add the constraint
1409
 
        for (t=0; t<ikscene->targets.size(); t++) {
1410
 
                IK_Target* iktarget = ikscene->targets[t];
 
1440
        for (t = 0; t < ikscene->targets.size(); t++) {
 
1441
                IK_Target *iktarget = ikscene->targets[t];
1411
1442
                iktarget->blscene = blscene;
1412
 
                condata= (bKinematicConstraint*)iktarget->blenderConstraint->data;
 
1443
                condata = (bKinematicConstraint *)iktarget->blenderConstraint->data;
1413
1444
                pchan = tree->pchan[iktarget->channel];
1414
1445
                unsigned int controltype, bonecnt;
1415
1446
                double bonelen;
1417
1448
 
1418
1449
                // add the end effector
1419
1450
                // estimate the average bone length, used to clamp feedback error
1420
 
                for (bonecnt=0, bonelen=0.f, a=iktarget->channel; a>=0; a=tree->parent[a], bonecnt++)
1421
 
                        bonelen += scale*tree->pchan[a]->bone->length;
1422
 
                bonelen /= bonecnt;             
 
1451
                for (bonecnt = 0, bonelen = 0.f, a = iktarget->channel; a >= 0; a = tree->parent[a], bonecnt++)
 
1452
                        bonelen += ikscene->blScale * tree->pchan[a]->bone->length;
 
1453
                bonelen /= bonecnt;
1423
1454
 
1424
1455
                // store the rest pose of the end effector to compute enforce target
1425
1456
                copy_m4_m4(mat, pchan->bone->arm_mat);
1426
1457
                copy_v3_v3(mat[3], pchan->bone->arm_tail);
1427
1458
                // get the rest pose relative to the armature base
1428
1459
                mult_m4_m4m4(iktarget->eeRest, invBaseFrame, mat);
1429
 
                iktarget->eeBlend = (!ikscene->polarConstraint && condata->type==CONSTRAINT_IK_COPYPOSE) ? true : false;
 
1460
                iktarget->eeBlend = (!ikscene->polarConstraint && condata->type == CONSTRAINT_IK_COPYPOSE) ? true : false;
1430
1461
                // use target_callback to make sure the initPose includes enforce coefficient
1431
1462
                target_callback(iTaSC::Timestamp(), iTaSC::F_identity, initPose, iktarget);
1432
1463
                iktarget->target = new iTaSC::MovingFrame(initPose);
1436
1467
                        break;
1437
1468
 
1438
1469
                switch (condata->type) {
1439
 
                case CONSTRAINT_IK_COPYPOSE:
1440
 
                        controltype = 0;
1441
 
                        if (condata->flag & CONSTRAINT_IK_ROT) {
1442
 
                                if (!(condata->flag & CONSTRAINT_IK_NO_ROT_X))
1443
 
                                        controltype |= iTaSC::CopyPose::CTL_ROTATIONX;
1444
 
                                if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Y))
1445
 
                                        controltype |= iTaSC::CopyPose::CTL_ROTATIONY;
1446
 
                                if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Z))
1447
 
                                        controltype |= iTaSC::CopyPose::CTL_ROTATIONZ;
1448
 
                        }
1449
 
                        if (condata->flag & CONSTRAINT_IK_POS) {
1450
 
                                if (!(condata->flag & CONSTRAINT_IK_NO_POS_X))
1451
 
                                        controltype |= iTaSC::CopyPose::CTL_POSITIONX;
1452
 
                                if (!(condata->flag & CONSTRAINT_IK_NO_POS_Y))
1453
 
                                        controltype |= iTaSC::CopyPose::CTL_POSITIONY;
1454
 
                                if (!(condata->flag & CONSTRAINT_IK_NO_POS_Z))
1455
 
                                        controltype |= iTaSC::CopyPose::CTL_POSITIONZ;
1456
 
                        }
1457
 
                        if (controltype) {
1458
 
                                iktarget->constraint = new iTaSC::CopyPose(controltype, controltype, bonelen);
1459
 
                                // set the gain
1460
 
                                if (controltype & iTaSC::CopyPose::CTL_POSITION)
1461
 
                                        iktarget->constraint->setControlParameter(iTaSC::CopyPose::ID_POSITION, iTaSC::ACT_ALPHA, condata->weight);
1462
 
                                if (controltype & iTaSC::CopyPose::CTL_ROTATION)
1463
 
                                        iktarget->constraint->setControlParameter(iTaSC::CopyPose::ID_ROTATION, iTaSC::ACT_ALPHA, condata->orientweight);
1464
 
                                iktarget->constraint->registerCallback(copypose_callback, iktarget);
1465
 
                                iktarget->errorCallback = copypose_error;
1466
 
                                iktarget->controlType = controltype;
 
1470
                        case CONSTRAINT_IK_COPYPOSE:
 
1471
                                controltype = 0;
 
1472
                                if (condata->flag & CONSTRAINT_IK_ROT) {
 
1473
                                        if (!(condata->flag & CONSTRAINT_IK_NO_ROT_X))
 
1474
                                                controltype |= iTaSC::CopyPose::CTL_ROTATIONX;
 
1475
                                        if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Y))
 
1476
                                                controltype |= iTaSC::CopyPose::CTL_ROTATIONY;
 
1477
                                        if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Z))
 
1478
                                                controltype |= iTaSC::CopyPose::CTL_ROTATIONZ;
 
1479
                                }
 
1480
                                if (condata->flag & CONSTRAINT_IK_POS) {
 
1481
                                        if (!(condata->flag & CONSTRAINT_IK_NO_POS_X))
 
1482
                                                controltype |= iTaSC::CopyPose::CTL_POSITIONX;
 
1483
                                        if (!(condata->flag & CONSTRAINT_IK_NO_POS_Y))
 
1484
                                                controltype |= iTaSC::CopyPose::CTL_POSITIONY;
 
1485
                                        if (!(condata->flag & CONSTRAINT_IK_NO_POS_Z))
 
1486
                                                controltype |= iTaSC::CopyPose::CTL_POSITIONZ;
 
1487
                                }
 
1488
                                if (controltype) {
 
1489
                                        iktarget->constraint = new iTaSC::CopyPose(controltype, controltype, bonelen);
 
1490
                                        // set the gain
 
1491
                                        if (controltype & iTaSC::CopyPose::CTL_POSITION)
 
1492
                                                iktarget->constraint->setControlParameter(iTaSC::CopyPose::ID_POSITION, iTaSC::ACT_ALPHA, condata->weight);
 
1493
                                        if (controltype & iTaSC::CopyPose::CTL_ROTATION)
 
1494
                                                iktarget->constraint->setControlParameter(iTaSC::CopyPose::ID_ROTATION, iTaSC::ACT_ALPHA, condata->orientweight);
 
1495
                                        iktarget->constraint->registerCallback(copypose_callback, iktarget);
 
1496
                                        iktarget->errorCallback = copypose_error;
 
1497
                                        iktarget->controlType = controltype;
 
1498
                                        // add the constraint
 
1499
                                        if (condata->flag & CONSTRAINT_IK_TARGETAXIS)
 
1500
                                                ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, iktarget->targetName, armname, "", ikscene->channels[iktarget->channel].tail);
 
1501
                                        else
 
1502
                                                ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, armname, iktarget->targetName, ikscene->channels[iktarget->channel].tail);
 
1503
                                }
 
1504
                                break;
 
1505
                        case CONSTRAINT_IK_DISTANCE:
 
1506
                                iktarget->constraint = new iTaSC::Distance(bonelen);
 
1507
                                iktarget->constraint->setControlParameter(iTaSC::Distance::ID_DISTANCE, iTaSC::ACT_VALUE, condata->dist);
 
1508
                                iktarget->constraint->registerCallback(distance_callback, iktarget);
 
1509
                                iktarget->errorCallback = distance_error;
 
1510
                                // we can update the weight on each substep
 
1511
                                iktarget->constraint->substep(true);
1467
1512
                                // add the constraint
1468
 
                                if (condata->flag & CONSTRAINT_IK_TARGETAXIS)
1469
 
                                        ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, iktarget->targetName, armname, "", ikscene->channels[iktarget->channel].tail);
1470
 
                                else
1471
 
                                        ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, armname, iktarget->targetName, ikscene->channels[iktarget->channel].tail);
1472
 
                        }
1473
 
                        break;
1474
 
                case CONSTRAINT_IK_DISTANCE:
1475
 
                        iktarget->constraint = new iTaSC::Distance(bonelen);
1476
 
                        iktarget->constraint->setControlParameter(iTaSC::Distance::ID_DISTANCE, iTaSC::ACT_VALUE, condata->dist);
1477
 
                        iktarget->constraint->registerCallback(distance_callback, iktarget);
1478
 
                        iktarget->errorCallback = distance_error;
1479
 
                        // we can update the weight on each substep
1480
 
                        iktarget->constraint->substep(true);
1481
 
                        // add the constraint
1482
 
                        ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, armname, iktarget->targetName, ikscene->channels[iktarget->channel].tail);
1483
 
                        break;
 
1513
                                ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, armname, iktarget->targetName, ikscene->channels[iktarget->channel].tail);
 
1514
                                break;
1484
1515
                }
1485
1516
                if (!ret)
1486
1517
                        break;
1487
1518
        }
1488
1519
        if (!ret ||
1489
 
                !scene->addCache(ikscene->cache) ||
1490
 
                !scene->addSolver(ikscene->solver) ||
1491
 
                !scene->initialize()) {
 
1520
            !scene->addCache(ikscene->cache) ||
 
1521
            !scene->addSolver(ikscene->solver) ||
 
1522
            !scene->initialize()) {
1492
1523
                delete ikscene;
1493
1524
                ikscene = NULL;
1494
1525
        }
1495
1526
        return ikscene;
1496
1527
}
1497
1528
 
1498
 
static void create_scene(Scene *scene, Object *ob)
 
1529
static void create_scene(Scene *scene, Object *ob, float ctime)
1499
1530
{
1500
1531
        bPoseChannel *pchan;
1501
1532
 
1502
1533
        // create the IK scene
1503
 
        for (pchan= (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan= (bPoseChannel *)pchan->next) {
 
1534
        for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = (bPoseChannel *)pchan->next) {
1504
1535
                // by construction there is only one tree
1505
 
                PoseTree *tree = (PoseTree*)pchan->iktree.first;
 
1536
                PoseTree *tree = (PoseTree *)pchan->iktree.first;
1506
1537
                if (tree) {
1507
 
                        IK_Data* ikdata = get_ikdata(ob->pose);
 
1538
                        IK_Data *ikdata = get_ikdata(ob->pose);
1508
1539
                        // convert tree in iTaSC::Scene
1509
 
                        IK_Scene* ikscene = convert_tree(scene, ob, pchan);
 
1540
                        IK_Scene *ikscene = convert_tree(scene, ob, pchan, ctime);
1510
1541
                        if (ikscene) {
1511
1542
                                ikscene->next = ikdata->first;
1512
1543
                                ikdata->first = ikscene;
1513
1544
                        }
1514
1545
                        // delete the trees once we are done
1515
 
                        while(tree) {
 
1546
                        while (tree) {
1516
1547
                                BLI_remlink(&pchan->iktree, tree);
1517
1548
                                BLI_freelistN(&tree->targets);
1518
1549
                                if (tree->pchan) MEM_freeN(tree->pchan);
1519
1550
                                if (tree->parent) MEM_freeN(tree->parent);
1520
1551
                                if (tree->basis_change) MEM_freeN(tree->basis_change);
1521
1552
                                MEM_freeN(tree);
1522
 
                                tree = (PoseTree*)pchan->iktree.first;
 
1553
                                tree = (PoseTree *)pchan->iktree.first;
1523
1554
                        }
1524
1555
                }
1525
1556
        }
1526
1557
}
1527
1558
 
1528
 
static void init_scene(Object *ob)
 
1559
/* returns 1 if scaling has changed and tree must be reinitialized */
 
1560
static int init_scene(Object *ob)
1529
1561
{
 
1562
        // check also if scaling has changed
 
1563
        float scale = len_v3(ob->obmat[1]);
 
1564
        IK_Scene *scene;
 
1565
 
1530
1566
        if (ob->pose->ikdata) {
1531
 
                for (IK_Scene* scene = ((IK_Data*)ob->pose->ikdata)->first;
1532
 
                        scene != NULL;
1533
 
                        scene = scene->next) {
 
1567
                for (scene = ((IK_Data *)ob->pose->ikdata)->first;
 
1568
                     scene != NULL;
 
1569
                     scene = scene->next) {
 
1570
                        if (fabs(scene->blScale - scale) > KDL::epsilon)
 
1571
                                return 1;
1534
1572
                        scene->channels[0].pchan->flag |= POSE_IKTREE;
1535
1573
                }
1536
1574
        }
 
1575
        return 0;
1537
1576
}
1538
1577
 
1539
 
static void execute_scene(Scene* blscene, IK_Scene* ikscene, bItasc* ikparam, float ctime, float frtime)
 
1578
static void execute_scene(Scene *blscene, IK_Scene *ikscene, bItasc *ikparam, float ctime, float frtime)
1540
1579
{
1541
1580
        int i;
1542
 
        IK_Channel* ikchan;
 
1581
        IK_Channel *ikchan;
1543
1582
        if (ikparam->flag & ITASC_SIMULATION) {
1544
 
                for (i=0, ikchan=ikscene->channels; i<ikscene->numchan; i++, ++ikchan) {
 
1583
                for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; i++, ++ikchan) {
1545
1584
                        // In simulation mode we don't allow external contraint to change our bones, mark the channel done
1546
 
                        // also tell Blender that this channel is part of IK tree (cleared on each where_is_pose()
1547
 
                        ikchan->pchan->flag |= (POSE_DONE|POSE_CHAIN);
 
1585
                        // also tell Blender that this channel is part of IK tree (cleared on each BKE_pose_where_is()
 
1586
                        ikchan->pchan->flag |= (POSE_DONE | POSE_CHAIN);
1548
1587
                        ikchan->jointValid = 0;
1549
1588
                }
1550
1589
        }
1551
1590
        else {
1552
1591
                // in animation mode, we must get the bone position from action and constraints
1553
 
                for (i=0, ikchan=ikscene->channels; i<ikscene->numchan; i++, ++ikchan) {
 
1592
                for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; i++, ++ikchan) {
1554
1593
                        if (!(ikchan->pchan->flag & POSE_DONE))
1555
 
                                where_is_pose_bone(blscene, ikscene->blArmature, ikchan->pchan, ctime, 1);
1556
 
                        // tell blender that this channel was controlled by IK, it's cleared on each where_is_pose()
1557
 
                        ikchan->pchan->flag |= (POSE_DONE|POSE_CHAIN);
 
1594
                                BKE_pose_where_is_bone(blscene, ikscene->blArmature, ikchan->pchan, ctime, 1);
 
1595
                        // tell blender that this channel was controlled by IK, it's cleared on each BKE_pose_where_is()
 
1596
                        ikchan->pchan->flag |= (POSE_DONE | POSE_CHAIN);
1558
1597
                        ikchan->jointValid = 0;
1559
1598
                }
1560
1599
        }
1561
1600
        // only run execute the scene if at least one of our target is enabled
1562
 
        for (i=ikscene->targets.size(); i > 0; --i) {
1563
 
                IK_Target* iktarget = ikscene->targets[i-1];
 
1601
        for (i = ikscene->targets.size(); i > 0; --i) {
 
1602
                IK_Target *iktarget = ikscene->targets[i - 1];
1564
1603
                if (!(iktarget->blenderConstraint->flag & CONSTRAINT_OFF))
1565
1604
                        break;
1566
1605
        }
1577
1616
 
1578
1617
        if (ikparam->flag & ITASC_SIMULATION) {
1579
1618
                ikscene->solver->setParam(iTaSC::Solver::DLS_QMAX, ikparam->maxvel);
1580
 
        } 
 
1619
        }
1581
1620
        else {
1582
1621
                // in animation mode we start from the pose after action and constraint
1583
1622
                convert_pose(ikscene);
1586
1625
                reiterate = true;
1587
1626
                simulation = false;
1588
1627
                // time is virtual, so take fixed value for velocity parameters (see itasc_update_param)
1589
 
                // and choose 1s timestep to allow having velocity parameters in radiant 
 
1628
                // and choose 1s timestep to allow having velocity parameters in radiant
1590
1629
                timestep = 1.0;
1591
1630
                // use auto setup to let the solver test the variation of the joints
1592
1631
                numstep = 0;
1593
1632
        }
1594
 
                
 
1633
 
1595
1634
        if (ikscene->cache && !reiterate && simulation) {
1596
1635
                iTaSC::CacheTS sts, cts;
1597
 
                sts = cts = (iTaSC::CacheTS)(timestamp*1000.0+0.5);
 
1636
                sts = cts = (iTaSC::CacheTS)(timestamp * 1000.0 + 0.5);
1598
1637
                if (ikscene->cache->getPreviousCacheItem(ikscene->armature, 0, &cts) == NULL || cts == 0) {
1599
1638
                        // the cache is empty before this time, reiterate
1600
1639
                        if (ikparam->flag & ITASC_INITIAL_REITERATION)
1603
1642
                else {
1604
1643
                        // can take the cache as a start point.
1605
1644
                        sts -= cts;
1606
 
                        timestep = sts/1000.0;
 
1645
                        timestep = sts / 1000.0;
1607
1646
                }
1608
1647
        }
1609
 
        // don't cache if we are reiterating because we don't want to distroy the cache unnecessarily
 
1648
        // don't cache if we are reiterating because we don't want to destroy the cache unnecessarily
1610
1649
        ikscene->scene->update(timestamp, timestep, numstep, false, !reiterate, simulation);
1611
1650
        if (reiterate) {
1612
1651
                // how many times do we reiterate?
1613
 
                for (i=0; i<ikparam->numiter; i++) {
 
1652
                for (i = 0; i < ikparam->numiter; i++) {
1614
1653
                        if (ikscene->armature->getMaxJointChange() < ikparam->precision ||
1615
 
                                ikscene->armature->getMaxEndEffectorChange() < ikparam->precision)
 
1654
                            ikscene->armature->getMaxEndEffectorChange() < ikparam->precision)
 
1655
                        {
1616
1656
                                break;
 
1657
                        }
1617
1658
                        ikscene->scene->update(timestamp, timestep, numstep, true, false, simulation);
1618
1659
                }
1619
1660
                if (simulation) {
1622
1663
                }
1623
1664
        }
1624
1665
        // compute constraint error
1625
 
        for (i=ikscene->targets.size(); i > 0; --i) {
1626
 
                IK_Target* iktarget = ikscene->targets[i-1];
1627
 
                if (!(iktarget->blenderConstraint->flag & CONSTRAINT_OFF)) {
 
1666
        for (i = ikscene->targets.size(); i > 0; --i) {
 
1667
                IK_Target *iktarget = ikscene->targets[i - 1];
 
1668
                if (!(iktarget->blenderConstraint->flag & CONSTRAINT_OFF) && iktarget->constraint) {
1628
1669
                        unsigned int nvalues;
1629
 
                        const iTaSC::ConstraintValues* values;
 
1670
                        const iTaSC::ConstraintValues *values;
1630
1671
                        values = iktarget->constraint->getControlParameters(&nvalues);
1631
1672
                        iktarget->errorCallback(values, nvalues, iktarget);
1632
1673
                }
1637
1678
        // combine the parent and the joint frame to get the frame relative to armature
1638
1679
        // a backward translation of the bone length gives the head
1639
1680
        // if TY, compute the scale as the ratio of the joint length with rest pose length
1640
 
        iTaSC::Armature* arm = ikscene->armature;
 
1681
        iTaSC::Armature *arm = ikscene->armature;
1641
1682
        KDL::Frame frame;
1642
1683
        double q_rest[3], q[3];
1643
 
        const KDL::Joint* joint;
1644
 
        const KDL::Frame* tip;
1645
 
        bPoseChannel* pchan;
 
1684
        const KDL::Joint *joint;
 
1685
        const KDL::Frame *tip;
 
1686
        bPoseChannel *pchan;
1646
1687
        float scale;
1647
1688
        float length;
1648
1689
        float yaxis[3];
1649
 
        for (i=0, ikchan=ikscene->channels; i<ikscene->numchan; ++i, ++ikchan) {
 
1690
        for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; ++i, ++ikchan) {
1650
1691
                if (i == 0) {
1651
1692
                        if (!arm->getRelativeFrame(frame, ikchan->tail))
1652
1693
                                break;
1653
1694
                        // this frame is relative to base, make it relative to object
1654
1695
                        ikchan->frame = ikscene->baseFrame * frame;
1655
 
                } 
 
1696
                }
1656
1697
                else {
1657
1698
                        if (!arm->getRelativeFrame(frame, ikchan->tail, ikscene->channels[ikchan->parent].tail))
1658
1699
                                break;
1665
1706
                        break;
1666
1707
                if (joint->getType() == KDL::Joint::TransY) {
1667
1708
                        // stretch bones have a TY joint, compute the scale
1668
 
                        scale = (float)(q[0]/q_rest[0]);
 
1709
                        scale = (float)(q[0] / q_rest[0]);
1669
1710
                        // the length is the joint itself
1670
1711
                        length = (float)q[0];
1671
 
                } 
 
1712
                }
1672
1713
                else {
1673
1714
                        scale = 1.0f;
1674
1715
                        // for fixed bone, the length is in the tip (always along Y axis)
1678
1719
                pchan = ikchan->pchan;
1679
1720
                // tail mat
1680
1721
                ikchan->frame.getValue(&pchan->pose_mat[0][0]);
 
1722
                // the scale of the object was included in the ik scene, take it out now
 
1723
                // because the pose channels are relative to the object
 
1724
                mul_v3_fl(pchan->pose_mat[3], ikscene->blInvScale);
 
1725
                length *= ikscene->blInvScale;
1681
1726
                copy_v3_v3(pchan->pose_tail, pchan->pose_mat[3]);
1682
1727
                // shift to head
1683
1728
                copy_v3_v3(yaxis, pchan->pose_mat[1]);
1689
1734
                mul_v3_fl(pchan->pose_mat[1], scale);
1690
1735
                mul_v3_fl(pchan->pose_mat[2], scale);
1691
1736
        }
1692
 
        if (i<ikscene->numchan) {
 
1737
        if (i < ikscene->numchan) {
1693
1738
                // big problem
1694
1739
                ;
1695
1740
        }
1704
1749
        int count = 0;
1705
1750
 
1706
1751
        if (ob->pose->ikdata != NULL && !(ob->pose->flag & POSE_WAS_REBUILT)) {
1707
 
                init_scene(ob);
1708
 
                return;
 
1752
                if (!init_scene(ob))
 
1753
                        return;
1709
1754
        }
1710
1755
        // first remove old scene
1711
1756
        itasc_clear_data(ob->pose);
1712
1757
        // we should handle all the constraint and mark them all disabled
1713
1758
        // for blender but we'll start with the IK constraint alone
1714
 
        for (pchan= (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan= (bPoseChannel *)pchan->next) {
 
1759
        for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = (bPoseChannel *)pchan->next) {
1715
1760
                if (pchan->constflag & PCHAN_HAS_IK)
1716
1761
                        count += initialize_scene(ob, pchan);
1717
1762
        }
1718
 
        // if at least one tree, create the scenes from the PoseTree stored in the channels 
1719
 
        if (count)
1720
 
                create_scene(scene, ob);
1721
 
        itasc_update_param(ob->pose);
 
1763
        // if at least one tree, create the scenes from the PoseTree stored in the channels
 
1764
        // postpone until execute_tree: this way the pose constraint are included
 
1765
        //if (count)
 
1766
        //      create_scene(scene, ob, ctime);
 
1767
        //itasc_update_param(ob->pose);
1722
1768
        // make sure we don't rebuilt until the user changes something important
1723
1769
        ob->pose->flag &= ~POSE_WAS_REBUILT;
1724
1770
}
1725
1771
 
1726
1772
void itasc_execute_tree(struct Scene *scene, struct Object *ob,  struct bPoseChannel *pchan, float ctime)
1727
1773
{
 
1774
        if (!ob->pose->ikdata) {
 
1775
                // IK tree not yet created, no it now
 
1776
                create_scene(scene, ob, ctime);
 
1777
                itasc_update_param(ob->pose);
 
1778
        }
1728
1779
        if (ob->pose->ikdata) {
1729
 
                IK_Data* ikdata = (IK_Data*)ob->pose->ikdata;
1730
 
                bItasc* ikparam = (bItasc*) ob->pose->ikparam;
 
1780
                IK_Data *ikdata = (IK_Data *)ob->pose->ikdata;
 
1781
                bItasc *ikparam = (bItasc *) ob->pose->ikparam;
1731
1782
                // we need default parameters
1732
1783
                if (!ikparam) ikparam = &DefIKParam;
1733
1784
 
1734
 
                for (IK_Scene* ikscene = ikdata->first; ikscene; ikscene = ikscene->next) {
 
1785
                for (IK_Scene *ikscene = ikdata->first; ikscene; ikscene = ikscene->next) {
1735
1786
                        if (ikscene->channels[0].pchan == pchan) {
1736
 
                                float timestep = scene->r.frs_sec_base/scene->r.frs_sec;
 
1787
                                float timestep = scene->r.frs_sec_base / scene->r.frs_sec;
1737
1788
                                if (ob->pose->flag & POSE_GAME_ENGINE) {
1738
1789
                                        timestep = ob->pose->ctime;
1739
1790
                                        // limit the timestep to avoid excessive number of iteration
1755
1806
void itasc_clear_data(struct bPose *pose)
1756
1807
{
1757
1808
        if (pose->ikdata) {
1758
 
                IK_Data* ikdata = (IK_Data*)pose->ikdata;
1759
 
                for (IK_Scene* scene = ikdata->first; scene; scene = ikdata->first) {
 
1809
                IK_Data *ikdata = (IK_Data *)pose->ikdata;
 
1810
                for (IK_Scene *scene = ikdata->first; scene; scene = ikdata->first) {
1760
1811
                        ikdata->first = scene->next;
1761
1812
                        delete scene;
1762
1813
                }
1768
1819
void itasc_clear_cache(struct bPose *pose)
1769
1820
{
1770
1821
        if (pose->ikdata) {
1771
 
                IK_Data* ikdata = (IK_Data*)pose->ikdata;
1772
 
                for (IK_Scene* scene = ikdata->first; scene; scene = scene->next) {
 
1822
                IK_Data *ikdata = (IK_Data *)pose->ikdata;
 
1823
                for (IK_Scene *scene = ikdata->first; scene; scene = scene->next) {
1773
1824
                        if (scene->cache)
1774
1825
                                // clear all cache but leaving the timestamp 0 (=rest pose)
1775
1826
                                scene->cache->clearCacheFrom(NULL, 1);
1780
1831
void itasc_update_param(struct bPose *pose)
1781
1832
{
1782
1833
        if (pose->ikdata && pose->ikparam) {
1783
 
                IK_Data* ikdata = (IK_Data*)pose->ikdata;
1784
 
                bItasc* ikparam = (bItasc*)pose->ikparam;
1785
 
                for (IK_Scene* ikscene = ikdata->first; ikscene; ikscene = ikscene->next) {
 
1834
                IK_Data *ikdata = (IK_Data *)pose->ikdata;
 
1835
                bItasc *ikparam = (bItasc *)pose->ikparam;
 
1836
                for (IK_Scene *ikscene = ikdata->first; ikscene; ikscene = ikscene->next) {
1786
1837
                        double armlength = ikscene->armature->getArmLength();
1787
 
                        ikscene->solver->setParam(iTaSC::Solver::DLS_LAMBDA_MAX, ikparam->dampmax*armlength);
1788
 
                        ikscene->solver->setParam(iTaSC::Solver::DLS_EPSILON, ikparam->dampeps*armlength);
 
1838
                        ikscene->solver->setParam(iTaSC::Solver::DLS_LAMBDA_MAX, ikparam->dampmax * armlength);
 
1839
                        ikscene->solver->setParam(iTaSC::Solver::DLS_EPSILON, ikparam->dampeps * armlength);
1789
1840
                        if (ikparam->flag & ITASC_SIMULATION) {
1790
1841
                                ikscene->scene->setParam(iTaSC::Scene::MIN_TIMESTEP, ikparam->minstep);
1791
1842
                                ikscene->scene->setParam(iTaSC::Scene::MAX_TIMESTEP, ikparam->maxstep);
1793
1844
                                ikscene->armature->setControlParameter(CONSTRAINT_ID_ALL, iTaSC::Armature::ID_JOINT, iTaSC::ACT_FEEDBACK, ikparam->feedback);
1794
1845
                        }
1795
1846
                        else {
1796
 
                                // in animation mode timestep is 1s by convention => 
 
1847
                                // in animation mode timestep is 1s by convention =>
1797
1848
                                // qmax becomes radiant and feedback becomes fraction of error gap corrected in one iteration
1798
1849
                                ikscene->scene->setParam(iTaSC::Scene::MIN_TIMESTEP, 1.0);
1799
1850
                                ikscene->scene->setParam(iTaSC::Scene::MAX_TIMESTEP, 1.0);
1813
1864
                return;
1814
1865
 
1815
1866
        switch (data->type) {
1816
 
        case CONSTRAINT_IK_COPYPOSE:
1817
 
        case CONSTRAINT_IK_DISTANCE:
1818
 
                /* cartesian space constraint */
1819
 
                break;
 
1867
                case CONSTRAINT_IK_COPYPOSE:
 
1868
                case CONSTRAINT_IK_DISTANCE:
 
1869
                        /* cartesian space constraint */
 
1870
                        break;
1820
1871
        }
1821
1872
}
1822
1873