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

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/armature.c

  • 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:
37
37
 
38
38
#include "MEM_guardedalloc.h"
39
39
 
40
 
#include "BLI_bpath.h"
41
40
#include "BLI_math.h"
42
41
#include "BLI_blenlib.h"
43
42
#include "BLI_utildefines.h"
75
74
 
76
75
/* **************** Generic Functions, data level *************** */
77
76
 
78
 
bArmature *add_armature(const char *name)
 
77
bArmature *BKE_armature_add(Main *bmain, const char *name)
79
78
{
80
79
        bArmature *arm;
81
80
 
82
 
        arm = alloc_libblock (&G.main->armature, ID_AR, name);
83
 
        arm->deformflag = ARM_DEF_VGROUP|ARM_DEF_ENVELOPE;
 
81
        arm = BKE_libblock_alloc(&bmain->armature, ID_AR, name);
 
82
        arm->deformflag = ARM_DEF_VGROUP | ARM_DEF_ENVELOPE;
84
83
        arm->flag = ARM_COL_CUSTOM; /* custom bone-group colors */
85
84
        arm->layer = 1;
86
85
        return arm;
87
86
}
88
87
 
89
 
bArmature *get_armature(Object *ob)
 
88
bArmature *BKE_armature_from_object(Object *ob)
90
89
{
91
90
        if (ob->type == OB_ARMATURE)
92
91
                return (bArmature *)ob->data;
93
92
        return NULL;
94
93
}
95
94
 
96
 
void free_bonelist(ListBase *lb)
 
95
void BKE_armature_bonelist_free(ListBase *lb)
97
96
{
98
97
        Bone *bone;
99
98
 
102
101
                        IDP_FreeProperty(bone->prop);
103
102
                        MEM_freeN(bone->prop);
104
103
                }
105
 
                free_bonelist(&bone->childbase);
 
104
                BKE_armature_bonelist_free(&bone->childbase);
106
105
        }
107
106
 
108
107
        BLI_freelistN(lb);
109
108
}
110
109
 
111
 
void free_armature(bArmature *arm)
 
110
void BKE_armature_free(bArmature *arm)
112
111
{
113
112
        if (arm) {
114
 
                free_bonelist(&arm->bonebase);
 
113
                BKE_armature_bonelist_free(&arm->bonebase);
115
114
 
116
115
                /* free editmode data */
117
116
                if (arm->edbo) {
135
134
        }
136
135
}
137
136
 
138
 
void make_local_armature(bArmature *arm)
 
137
void BKE_armature_make_local(bArmature *arm)
139
138
{
140
139
        Main *bmain = G.main;
141
140
        int is_local = FALSE, is_lib = FALSE;
161
160
                id_clear_lib_data(bmain, &arm->id);
162
161
        }
163
162
        else if (is_local && is_lib) {
164
 
                bArmature *arm_new = copy_armature(arm);
 
163
                bArmature *arm_new = BKE_armature_copy(arm);
165
164
                arm_new->id.us = 0;
166
165
 
167
166
                /* Remap paths of new ID using old library as base. */
179
178
        }
180
179
}
181
180
 
182
 
static void copy_bonechildren(Bone* newBone, Bone* oldBone, Bone* actBone, Bone **newActBone)
 
181
static void copy_bonechildren(Bone *newBone, Bone *oldBone, Bone *actBone, Bone **newActBone)
183
182
{
184
183
        Bone *curBone, *newChildBone;
185
184
 
201
200
        }
202
201
}
203
202
 
204
 
bArmature *copy_armature(bArmature *arm)
 
203
bArmature *BKE_armature_copy(bArmature *arm)
205
204
{
206
205
        bArmature *newArm;
207
206
        Bone *oldBone, *newBone;
208
 
        Bone *newActBone= NULL;
 
207
        Bone *newActBone = NULL;
209
208
 
210
 
        newArm = copy_libblock(&arm->id);
 
209
        newArm = BKE_libblock_copy(&arm->id);
211
210
        BLI_duplicatelist(&newArm->bonebase, &arm->bonebase);
212
211
 
213
 
        /* Duplicate the childrens' lists*/
 
212
        /* Duplicate the childrens' lists */
214
213
        newBone = newArm->bonebase.first;
215
214
        for (oldBone = arm->bonebase.first; oldBone; oldBone = oldBone->next) {
216
215
                newBone->parent = NULL;
245
244
 
246
245
 
247
246
/* Walk the list until the bone is found */
248
 
Bone *get_named_bone(bArmature *arm, const char *name)
 
247
Bone *BKE_armature_find_bone_name(bArmature *arm, const char *name)
249
248
{
250
249
        Bone *bone = NULL, *curBone;
251
250
 
340
339
 
341
340
                while (change) { /* remove extensions */
342
341
                        change = 0;
343
 
                        if (len > 2 && basename[len-2] == '.') {
344
 
                                if (basename[len-1] == 'L' || basename[len-1] == 'R') { /* L R */
345
 
                                        basename[len-2] = '\0';
 
342
                        if (len > 2 && basename[len - 2] == '.') {
 
343
                                if (basename[len - 1] == 'L' || basename[len - 1] == 'R') { /* L R */
 
344
                                        basename[len - 2] = '\0';
346
345
                                        len -= 2;
347
346
                                        change = 1;
348
347
                                }
349
348
                        }
350
 
                        else if (len > 3 && basename[len-3] == '.') {
351
 
                                if ((basename[len-2] == 'F' && basename[len-1] == 'r') || /* Fr */
352
 
                                   (basename[len-2] == 'B' && basename[len-1] == 'k'))   /* Bk */
 
349
                        else if (len > 3 && basename[len - 3] == '.') {
 
350
                                if ((basename[len - 2] == 'F' && basename[len - 1] == 'r') || /* Fr */
 
351
                                    (basename[len - 2] == 'B' && basename[len - 1] == 'k')) /* Bk */
353
352
                                {
354
 
                                        basename[len-3] = '\0';
 
353
                                        basename[len - 3] = '\0';
355
354
                                        len -= 3;
356
355
                                        change = 1;
357
356
                                }
358
357
                        }
359
 
                        else if (len > 4 && basename[len-4] == '.') {
360
 
                                if ((basename[len-3] == 'T' && basename[len-2] == 'o' && basename[len-1] == 'p') || /* Top */
361
 
                                   (basename[len-3] == 'B' && basename[len-2] == 'o' && basename[len-1] == 't'))   /* Bot */
 
358
                        else if (len > 4 && basename[len - 4] == '.') {
 
359
                                if ((basename[len - 3] == 'T' && basename[len - 2] == 'o' && basename[len - 1] == 'p') || /* Top */
 
360
                                    (basename[len - 3] == 'B' && basename[len - 2] == 'o' && basename[len - 1] == 't')) /* Bot */
362
361
                                {
363
 
                                        basename[len-4] = '\0';
 
362
                                        basename[len - 4] = '\0';
364
363
                                        len -= 4;
365
364
                                        change = 1;
366
365
                                }
368
367
                }
369
368
 
370
369
                if ((MAXBONENAME - len) < strlen(extension) + 1) { /* add 1 for the '.' */
371
 
                        strncpy(name, basename, len-strlen(extension));
 
370
                        strncpy(name, basename, len - strlen(extension));
372
371
                }
373
372
 
374
373
                BLI_snprintf(name, MAXBONENAME, "%s.%s", basename, extension);
382
381
 
383
382
/* ************* B-Bone support ******************* */
384
383
 
385
 
#define MAX_BBONE_SUBDIV        32
 
384
#define MAX_BBONE_SUBDIV    32
386
385
 
387
386
/* data has MAX_BBONE_SUBDIV+1 interpolated points, will become desired amount with equal distances */
388
387
static void equalize_bezier(float *data, int desired)
389
388
{
390
389
        float *fp, totdist, ddist, dist, fac1, fac2;
391
 
        float pdist[MAX_BBONE_SUBDIV+1];
392
 
        float temp[MAX_BBONE_SUBDIV+1][4];
 
390
        float pdist[MAX_BBONE_SUBDIV + 1];
 
391
        float temp[MAX_BBONE_SUBDIV + 1][4];
393
392
        int a, nr;
394
393
 
395
394
        pdist[0] = 0.0f;
396
395
        for (a = 0, fp = data; a < MAX_BBONE_SUBDIV; a++, fp += 4) {
397
396
                copy_qt_qt(temp[a], fp);
398
 
                pdist[a+1] = pdist[a] + len_v3v3(fp, fp+4);
 
397
                pdist[a + 1] = pdist[a] + len_v3v3(fp, fp + 4);
399
398
        }
400
399
        /* do last point */
401
400
        copy_qt_qt(temp[a], fp);
402
401
        totdist = pdist[a];
403
402
 
404
403
        /* go over distances and calculate new points */
405
 
        ddist = totdist/((float)desired);
 
404
        ddist = totdist / ((float)desired);
406
405
        nr = 1;
407
 
        for (a = 1, fp = data+4; a < desired; a++, fp += 4) {
408
 
                dist = ((float)a)*ddist;
 
406
        for (a = 1, fp = data + 4; a < desired; a++, fp += 4) {
 
407
                dist = ((float)a) * ddist;
409
408
 
410
409
                /* we're looking for location (distance) 'dist' in the array */
411
410
                while ((dist >= pdist[nr]) && nr < MAX_BBONE_SUBDIV)
412
411
                        nr++;
413
412
 
414
 
                fac1 = pdist[nr] - pdist[nr-1];
 
413
                fac1 = pdist[nr] - pdist[nr - 1];
415
414
                fac2 = pdist[nr] - dist;
416
415
                fac1 = fac2 / fac1;
417
416
                fac2 = 1.0f - fac1;
418
417
 
419
 
                fp[0] = fac1*temp[nr-1][0] + fac2*temp[nr][0];
420
 
                fp[1] = fac1*temp[nr-1][1] + fac2*temp[nr][1];
421
 
                fp[2] = fac1*temp[nr-1][2] + fac2*temp[nr][2];
422
 
                fp[3] = fac1*temp[nr-1][3] + fac2*temp[nr][3];
 
418
                fp[0] = fac1 * temp[nr - 1][0] + fac2 * temp[nr][0];
 
419
                fp[1] = fac1 * temp[nr - 1][1] + fac2 * temp[nr][1];
 
420
                fp[2] = fac1 * temp[nr - 1][2] + fac2 * temp[nr][2];
 
421
                fp[3] = fac1 * temp[nr - 1][3] + fac2 * temp[nr][3];
423
422
        }
424
423
        /* set last point, needed for orientation calculus */
425
424
        copy_qt_qt(fp, temp[MAX_BBONE_SUBDIV]);
436
435
        Bone *bone = pchan->bone;
437
436
        float h1[3], h2[3], scale[3], length, hlength1, hlength2, roll1 = 0.0f, roll2;
438
437
        float mat3[3][3], imat[4][4], posemat[4][4], scalemat[4][4], iscalemat[4][4];
439
 
        float data[MAX_BBONE_SUBDIV+1][4], *fp;
440
 
        int a, doscale = 0;
 
438
        float data[MAX_BBONE_SUBDIV + 1][4], *fp;
 
439
        int a, do_scale = 0;
441
440
 
442
441
        length = bone->length;
443
442
 
455
454
                        invert_m4_m4(iscalemat, scalemat);
456
455
 
457
456
                        length *= scale[1];
458
 
                        doscale = 1;
 
457
                        do_scale = 1;
459
458
                }
460
459
        }
461
460
 
462
 
        hlength1 = bone->ease1*length*0.390464f; /* 0.5*sqrt(2)*kappa, the handle length for near-perfect circles */
463
 
        hlength2 = bone->ease2*length*0.390464f;
 
461
        hlength1 = bone->ease1 * length * 0.390464f; /* 0.5f * sqrt(2) * kappa, the handle length for near-perfect circles */
 
462
        hlength2 = bone->ease2 * length * 0.390464f;
464
463
 
465
464
        /* evaluate next and prev bones */
466
465
        if (bone->flag & BONE_CONNECTED)
471
470
        next = pchan->child;
472
471
 
473
472
        /* find the handle points, since this is inside bone space, the
474
 
         * first point = (0,0,0)
 
473
         * first point = (0, 0, 0)
475
474
         * last point =  (0, length, 0) */
476
475
        if (rest) {
477
476
                invert_m4_m4(imat, pchan->bone->arm_mat);
478
477
        }
479
 
        else if (doscale) {
 
478
        else if (do_scale) {
480
479
                copy_m4_m4(posemat, pchan->pose_mat);
481
480
                normalize_m4(posemat);
482
481
                invert_m4_m4(imat, posemat);
494
493
                        copy_v3_v3(h1, prev->pose_head);
495
494
                mul_m4_v3(imat, h1);
496
495
 
497
 
                if (prev->bone->segments>1) {
 
496
                if (prev->bone->segments > 1) {
498
497
                        /* if previous bone is B-bone too, use average handle direction */
499
498
                        h1[1] -= length;
500
499
                        roll1 = 0.0f;
534
533
                mul_m4_v3(imat, h2);
535
534
 
536
535
                /* if next bone is B-bone too, use average handle direction */
537
 
                if (next->bone->segments>1)
538
 
                        ;
539
 
                else
540
 
                        h2[1]-= length;
 
536
                if (next->bone->segments > 1) {
 
537
                        /* pass */
 
538
                }
 
539
                else {
 
540
                        h2[1] -= length;
 
541
                }
541
542
                normalize_v3(h2);
542
543
 
543
544
                /* find the next roll to interpolate as well */
566
567
        if (bone->segments > MAX_BBONE_SUBDIV)
567
568
                bone->segments = MAX_BBONE_SUBDIV;
568
569
 
569
 
        forward_diff_bezier(0.0,   h1[0],                           h2[0],                           0.0,    data[0],
570
 
                            MAX_BBONE_SUBDIV, 4*sizeof(float));
571
 
        forward_diff_bezier(0.0,   h1[1],                           length + h2[1],                  length, data[0]+1,
572
 
                            MAX_BBONE_SUBDIV, 4*sizeof(float));
573
 
        forward_diff_bezier(0.0,   h1[2],                           h2[2],                           0.0,    data[0]+2,
574
 
                            MAX_BBONE_SUBDIV, 4*sizeof(float));
575
 
        forward_diff_bezier(roll1, roll1 + 0.390464f*(roll2-roll1), roll2 - 0.390464f*(roll2-roll1), roll2,  data[0]+3,
576
 
                            MAX_BBONE_SUBDIV, 4*sizeof(float));
 
570
        BKE_curve_forward_diff_bezier(0.0f,  h1[0],                               h2[0],                               0.0f,   data[0],     MAX_BBONE_SUBDIV, 4 * sizeof(float));
 
571
        BKE_curve_forward_diff_bezier(0.0f,  h1[1],                               length + h2[1],                      length, data[0] + 1, MAX_BBONE_SUBDIV, 4 * sizeof(float));
 
572
        BKE_curve_forward_diff_bezier(0.0f,  h1[2],                               h2[2],                               0.0f,   data[0] + 2, MAX_BBONE_SUBDIV, 4 * sizeof(float));
 
573
        BKE_curve_forward_diff_bezier(roll1, roll1 + 0.390464f * (roll2 - roll1), roll2 - 0.390464f * (roll2 - roll1), roll2,  data[0] + 3, MAX_BBONE_SUBDIV, 4 * sizeof(float));
577
574
 
578
575
        equalize_bezier(data[0], bone->segments); /* note: does stride 4! */
579
576
 
580
577
        /* make transformation matrices for the segments for drawing */
581
578
        for (a = 0, fp = data[0]; a < bone->segments; a++, fp += 4) {
582
 
                sub_v3_v3v3(h1, fp+4, fp);
 
579
                sub_v3_v3v3(h1, fp + 4, fp);
583
580
                vec_roll_to_mat3(h1, fp[3], mat3); /* fp[3] is roll */
584
581
 
585
582
                copy_m4_m3(result_array[a].mat, mat3);
586
583
                copy_v3_v3(result_array[a].mat[3], fp);
587
584
 
588
 
                if (doscale) {
 
585
                if (do_scale) {
589
586
                        /* correct for scaling when this matrix is used in scaled space */
590
587
                        mul_serie_m4(result_array[a].mat, iscalemat, result_array[a].mat, scalemat, NULL, NULL, NULL, NULL, NULL);
591
588
                }
613
610
        int a;
614
611
 
615
612
        /* allocate b_bone matrices and dual quats */
616
 
        b_bone_mats = MEM_mallocN((1+bone->segments)*sizeof(Mat4), "BBone defmats");
 
613
        b_bone_mats = MEM_mallocN((1 + bone->segments) * sizeof(Mat4), "BBone defmats");
617
614
        pdef_info->b_bone_mats = b_bone_mats;
618
615
 
619
616
        if (use_quaternion) {
620
 
                b_bone_dual_quats = MEM_mallocN((bone->segments)*sizeof(DualQuat), "BBone dqs");
 
617
                b_bone_dual_quats = MEM_mallocN((bone->segments) * sizeof(DualQuat), "BBone dqs");
621
618
                pdef_info->b_bone_dual_quats = b_bone_dual_quats;
622
619
        }
623
620
 
634
631
        for (a = 0; a < bone->segments; a++) {
635
632
                invert_m4_m4(tmat, b_bone_rest[a].mat);
636
633
 
637
 
                mul_serie_m4(b_bone_mats[a+1].mat, pchan->chan_mat, bone->arm_mat, b_bone[a].mat, tmat, b_bone_mats[0].mat,
 
634
                mul_serie_m4(b_bone_mats[a + 1].mat, pchan->chan_mat, bone->arm_mat, b_bone[a].mat, tmat, b_bone_mats[0].mat,
638
635
                             NULL, NULL, NULL);
639
636
 
640
637
                if (use_quaternion)
641
 
                        mat4_to_dquat(&b_bone_dual_quats[a], bone->arm_mat, b_bone_mats[a+1].mat);
 
638
                        mat4_to_dquat(&b_bone_dual_quats[a], bone->arm_mat, b_bone_mats[a + 1].mat);
642
639
        }
643
640
}
644
641
 
645
 
static void b_bone_deform(bPoseChanDeform *pdef_info, Bone *bone, float co[3], DualQuat *dq, float defmat[][3])
 
642
static void b_bone_deform(bPoseChanDeform *pdef_info, Bone *bone, float co[3], DualQuat *dq, float defmat[3][3])
646
643
{
647
644
        Mat4 *b_bone = pdef_info->b_bone_mats;
648
645
        float (*mat)[4] = b_bone[0].mat;
650
647
        int a;
651
648
 
652
649
        /* need to transform co back to bonespace, only need y */
653
 
        y = mat[0][1]*co[0] + mat[1][1]*co[1] + mat[2][1]*co[2] + mat[3][1];
 
650
        y = mat[0][1] * co[0] + mat[1][1] * co[1] + mat[2][1] * co[2] + mat[3][1];
654
651
 
655
652
        /* now calculate which of the b_bones are deforming this */
656
 
        segment = bone->length/((float)bone->segments);
657
 
        a = (int)(y/segment);
 
653
        segment = bone->length / ((float)bone->segments);
 
654
        a = (int)(y / segment);
658
655
 
659
656
        /* note; by clamping it extends deform at endpoints, goes best with
660
657
         * straight joints in restpos. */
661
 
        CLAMP(a, 0, bone->segments-1);
 
658
        CLAMP(a, 0, bone->segments - 1);
662
659
 
663
660
        if (dq) {
664
661
                copy_dq_dq(dq, &(pdef_info->b_bone_dual_quats)[a]);
665
662
        }
666
663
        else {
667
 
                mul_m4_v3(b_bone[a+1].mat, co);
 
664
                mul_m4_v3(b_bone[a + 1].mat, co);
668
665
 
669
666
                if (defmat) {
670
 
                        copy_m3_m4(defmat, b_bone[a+1].mat);
 
667
                        copy_m3_m4(defmat, b_bone[a + 1].mat);
671
668
                }
672
669
        }
673
670
}
699
696
                rad = rad2;
700
697
        }
701
698
        else {
702
 
                dist = (hsqr - (a*a));
 
699
                dist = (hsqr - (a * a));
703
700
 
704
701
                if (l != 0.0f) {
705
 
                        rad = a/l;
706
 
                        rad = rad*rad2 + (1.0f-rad)*rad1;
 
702
                        rad = a / l;
 
703
                        rad = rad * rad2 + (1.0f - rad) * rad1;
707
704
                }
708
705
                else
709
706
                        rad = rad1;
710
707
        }
711
708
 
712
 
        a = rad*rad;
 
709
        a = rad * rad;
713
710
        if (dist < a)
714
711
                return 1.0f;
715
712
        else {
716
 
                l = rad+rdist;
 
713
                l = rad + rdist;
717
714
                l *= l;
718
715
                if (rdist == 0.0f || dist >= l)
719
716
                        return 0.0f;
720
717
                else {
721
 
                        a = sqrtf(dist)-rad;
722
 
                        return 1.0f-( a*a )/( rdist*rdist );
 
718
                        a = sqrtf(dist) - rad;
 
719
                        return 1.0f - (a * a) / (rdist * rdist);
723
720
                }
724
721
        }
725
722
}
726
723
 
727
 
static void pchan_deform_mat_add(bPoseChannel *pchan, float weight, float bbonemat[][3], float mat[][3])
 
724
static void pchan_deform_mat_add(bPoseChannel *pchan, float weight, float bbonemat[3][3], float mat[3][3])
728
725
{
729
726
        float wmat[3][3];
730
727
 
738
735
}
739
736
 
740
737
static float dist_bone_deform(bPoseChannel *pchan, bPoseChanDeform *pdef_info, float vec[3], DualQuat *dq,
741
 
                              float mat[][3], float *co)
 
738
                              float mat[3][3], const float co[3])
742
739
{
743
740
        Bone *bone = pchan->bone;
744
741
        float fac, contrib = 0.0;
785
782
}
786
783
 
787
784
static void pchan_bone_deform(bPoseChannel *pchan, bPoseChanDeform *pdef_info, float weight, float vec[3], DualQuat *dq,
788
 
                              float mat[][3], float *co, float *contrib)
 
785
                              float mat[3][3], const float co[3], float *contrib)
789
786
{
790
787
        float cop[3], bbonemat[3][3];
791
788
        DualQuat bbonedq;
796
793
        copy_v3_v3(cop, co);
797
794
 
798
795
        if (vec) {
799
 
                if (pchan->bone->segments>1)
 
796
                if (pchan->bone->segments > 1)
800
797
                        /* applies on cop and bbonemat */
801
798
                        b_bone_deform(pdef_info, pchan->bone, cop, NULL, (mat) ? bbonemat : NULL);
802
799
                else
803
800
                        mul_m4_v3(pchan->chan_mat, cop);
804
801
 
805
 
                vec[0] += (cop[0]-co[0])*weight;
806
 
                vec[1] += (cop[1]-co[1])*weight;
807
 
                vec[2] += (cop[2]-co[2])*weight;
 
802
                vec[0] += (cop[0] - co[0]) * weight;
 
803
                vec[1] += (cop[1] - co[1]) * weight;
 
804
                vec[2] += (cop[2] - co[2]) * weight;
808
805
 
809
806
                if (mat)
810
807
                        pchan_deform_mat_add(pchan, weight, bbonemat, mat);
839
836
        const short invert_vgroup = deformflag & ARM_DEF_INVERT_VGROUP;
840
837
        int defbase_tot = 0;       /* safety for vertexgroup index overflow */
841
838
        int i, target_totvert = 0; /* safety for vertexgroup overflow */
842
 
        int use_dverts = 0;
 
839
        int use_dverts = FALSE;
843
840
        int armature_def_nr;
844
841
        int totchan;
845
842
 
856
853
        totchan = BLI_countlist(&armOb->pose->chanbase);
857
854
 
858
855
        if (use_quaternion) {
859
 
                dualquats = MEM_callocN(sizeof(DualQuat)*totchan, "dualquats");
 
856
                dualquats = MEM_callocN(sizeof(DualQuat) * totchan, "dualquats");
860
857
        }
861
858
 
862
 
        pdef_info_array = MEM_callocN(sizeof(bPoseChanDeform)*totchan, "bPoseChanDeform");
 
859
        pdef_info_array = MEM_callocN(sizeof(bPoseChanDeform) * totchan, "bPoseChanDeform");
863
860
 
864
861
        totchan = 0;
865
862
        pdef_info = pdef_info_array;
891
888
                        Lattice *lt = target->data;
892
889
                        dverts = lt->dvert;
893
890
                        if (dverts)
894
 
                                target_totvert = lt->pntsu*lt->pntsv*lt->pntsw;
 
891
                                target_totvert = lt->pntsu * lt->pntsv * lt->pntsw;
895
892
                }
896
893
        }
897
894
 
899
896
        if (deformflag & ARM_DEF_VGROUP) {
900
897
                if (ELEM(target->type, OB_MESH, OB_LATTICE)) {
901
898
                        /* if we have a DerivedMesh, only use dverts if it has them */
902
 
                        if (dm)
903
 
                                if (dm->getVertData(dm, 0, CD_MDEFORMVERT))
904
 
                                        use_dverts = 1;
905
 
                                else use_dverts = 0;
906
 
                        else if (dverts) use_dverts = 1;
 
899
                        if (dm) {
 
900
                                use_dverts = (dm->getVertData(dm, 0, CD_MDEFORMVERT) != NULL);
 
901
                        }
 
902
                        else if (dverts) {
 
903
                                use_dverts = TRUE;
 
904
                        }
907
905
 
908
906
                        if (use_dverts) {
909
907
                                defnrToPC = MEM_callocN(sizeof(*defnrToPC) * defbase_tot, "defnrToBone");
910
908
                                defnrToPCIndex = MEM_callocN(sizeof(*defnrToPCIndex) * defbase_tot, "defnrToIndex");
911
909
                                for (i = 0, dg = target->defbase.first; dg; i++, dg = dg->next) {
912
 
                                        defnrToPC[i] = get_pose_channel(armOb->pose, dg->name);
 
910
                                        defnrToPC[i] = BKE_pose_channel_find_name(armOb->pose, dg->name);
913
911
                                        /* exclude non-deforming bones */
914
912
                                        if (defnrToPC[i]) {
915
913
                                                if (defnrToPC[i]->bone->flag & BONE_NO_DEFORM) {
948
946
                        }
949
947
                }
950
948
 
951
 
                if (use_dverts || armature_def_nr >= 0) {
 
949
                if (use_dverts || armature_def_nr != -1) {
952
950
                        if (dm)
953
951
                                dvert = dm->getVertData(dm, i, CD_MDEFORMVERT);
954
952
                        else if (dverts && i < target_totvert)
959
957
                else
960
958
                        dvert = NULL;
961
959
 
962
 
                if (armature_def_nr >= 0 && dvert) {
 
960
                if (armature_def_nr != -1 && dvert) {
963
961
                        armature_weight = defvert_find_weight(dvert, armature_def_nr);
964
962
 
965
963
                        if (invert_vgroup)
966
 
                                armature_weight = 1.0f-armature_weight;
 
964
                                armature_weight = 1.0f - armature_weight;
967
965
 
968
966
                        /* hackish: the blending factor can be used for blending with prevCos too */
969
967
                        if (prevCos) {
989
987
 
990
988
                        for (j = dvert->totweight; j != 0; j--, dw++) {
991
989
                                const int index = dw->def_nr;
992
 
                                if (index < defbase_tot && (pchan = defnrToPC[index])) {
 
990
                                if (index >= 0 && index < defbase_tot && (pchan = defnrToPC[index])) {
993
991
                                        float weight = dw->weight;
994
992
                                        Bone *bone = pchan->bone;
995
993
                                        pdef_info = pdef_info_array + defnrToPCIndex[index];
1028
1026
 
1029
1027
                                if (armature_weight != 1.0f) {
1030
1028
                                        copy_v3_v3(dco, co);
1031
 
                                        mul_v3m3_dq( dco, (defMats) ? summat : NULL,dq);
 
1029
                                        mul_v3m3_dq(dco, (defMats) ? summat : NULL, dq);
1032
1030
                                        sub_v3_v3(dco, co);
1033
1031
                                        mul_v3_fl(dco, armature_weight);
1034
1032
                                        add_v3_v3(co, dco);
1035
1033
                                }
1036
1034
                                else
1037
 
                                        mul_v3m3_dq( co, (defMats) ? summat : NULL,dq);
 
1035
                                        mul_v3m3_dq(co, (defMats) ? summat : NULL, dq);
1038
1036
 
1039
1037
                                smat = summat;
1040
1038
                        }
1041
1039
                        else {
1042
 
                                mul_v3_fl(vec, armature_weight/contrib);
 
1040
                                mul_v3_fl(vec, armature_weight / contrib);
1043
1041
                                add_v3_v3v3(co, vec, co);
1044
1042
                        }
1045
1043
 
1051
1049
                                copy_m3_m3(tmpmat, defMats[i]);
1052
1050
 
1053
1051
                                if (!use_quaternion) /* quaternion already is scale corrected */
1054
 
                                        mul_m3_fl(smat, armature_weight/contrib);
 
1052
                                        mul_m3_fl(smat, armature_weight / contrib);
1055
1053
 
1056
1054
                                mul_serie_m3(defMats[i], tmpmat, pre, smat, post, NULL, NULL, NULL, NULL);
1057
1055
                        }
1063
1061
                /* interpolate with previous modifier position using weight group */
1064
1062
                if (prevCos) {
1065
1063
                        float mw = 1.0f - prevco_weight;
1066
 
                        vertexCos[i][0] = prevco_weight*vertexCos[i][0] + mw*co[0];
1067
 
                        vertexCos[i][1] = prevco_weight*vertexCos[i][1] + mw*co[1];
1068
 
                        vertexCos[i][2] = prevco_weight*vertexCos[i][2] + mw*co[2];
 
1064
                        vertexCos[i][0] = prevco_weight * vertexCos[i][0] + mw * co[0];
 
1065
                        vertexCos[i][1] = prevco_weight * vertexCos[i][1] + mw * co[1];
 
1066
                        vertexCos[i][2] = prevco_weight * vertexCos[i][2] + mw * co[2];
1069
1067
                }
1070
1068
        }
1071
1069
 
1090
1088
 
1091
1089
/* ************ END Armature Deform ******************* */
1092
1090
 
1093
 
void get_objectspace_bone_matrix(struct Bone* bone, float M_accumulatedMatrix[][4], int UNUSED(root),
 
1091
void get_objectspace_bone_matrix(struct Bone *bone, float M_accumulatedMatrix[4][4], int UNUSED(root),
1094
1092
                                 int UNUSED(posed))
1095
1093
{
1096
1094
        copy_m4_m4(M_accumulatedMatrix, bone->arm_mat);
1099
1097
/* **************** Space to Space API ****************** */
1100
1098
 
1101
1099
/* Convert World-Space Matrix to Pose-Space Matrix */
1102
 
void armature_mat_world_to_pose(Object *ob, float inmat[][4], float outmat[][4])
 
1100
void BKE_armature_mat_world_to_pose(Object *ob, float inmat[4][4], float outmat[4][4])
1103
1101
{
1104
1102
        float obmat[4][4];
1105
1103
 
1117
1115
/* Convert World-Space Location to Pose-Space Location
1118
1116
 * NOTE: this cannot be used to convert to pose-space location of the supplied
1119
1117
 *       pose-channel into its local space (i.e. 'visual'-keyframing) */
1120
 
void armature_loc_world_to_pose(Object *ob, const float inloc[3], float outloc[3])
 
1118
void BKE_armature_loc_world_to_pose(Object *ob, const float inloc[3], float outloc[3])
1121
1119
{
1122
1120
        float xLocMat[4][4] = MAT4_UNITY;
1123
1121
        float nLocMat[4][4];
1126
1124
        copy_v3_v3(xLocMat[3], inloc);
1127
1125
 
1128
1126
        /* get bone-space cursor matrix and extract location */
1129
 
        armature_mat_world_to_pose(ob, xLocMat, nLocMat);
 
1127
        BKE_armature_mat_world_to_pose(ob, xLocMat, nLocMat);
1130
1128
        copy_v3_v3(outloc, nLocMat[3]);
1131
1129
}
1132
1130
 
1133
1131
/* Simple helper, computes the offset bone matrix.
1134
1132
 *     offs_bone = yoffs(b-1) + root(b) + bonemat(b).
1135
1133
 * Not exported, as it is only used in this file currently... */
1136
 
static void get_offset_bone_mat(Bone *bone, float offs_bone[][4])
 
1134
static void get_offset_bone_mat(Bone *bone, float offs_bone[4][4])
1137
1135
{
1138
1136
        if (!bone->parent)
1139
1137
                return;
1165
1163
 *       pose-channel into its local space (i.e. 'visual'-keyframing).
1166
1164
 *       (note: I don't understand that, so I keep it :p --mont29).
1167
1165
 */
1168
 
void pchan_to_pose_mat(bPoseChannel *pchan, float rotscale_mat[][4], float loc_mat[][4])
 
1166
void BKE_pchan_to_pose_mat(bPoseChannel *pchan, float rotscale_mat[4][4], float loc_mat[4][4])
1169
1167
{
1170
1168
        Bone *bone, *parbone;
1171
1169
        bPoseChannel *parchan;
1208
1206
                else
1209
1207
                        mult_m4_m4m4(rotscale_mat, parchan->pose_mat, offs_bone);
1210
1208
 
1211
 
# if 1
1212
1209
                /* Compose the loc matrix for this bone. */
1213
 
                /* NOTE: That version deos not modify bone's loc when HINGE/NO_SCALE options are set. */
 
1210
                /* NOTE: That version does not modify bone's loc when HINGE/NO_SCALE options are set. */
1214
1211
 
1215
1212
                /* In this case, use the object's space *orientation*. */
1216
1213
                if (bone->flag & BONE_NO_LOCAL_LOCATION) {
1230
1227
                        mult_m4_m4m4(loc_mat, bone_loc, tmat4);
1231
1228
                }
1232
1229
                /* Those flags do not affect position, use plain parent transform space! */
1233
 
                else if (bone->flag & (BONE_HINGE|BONE_NO_SCALE)) {
 
1230
                else if (bone->flag & (BONE_HINGE | BONE_NO_SCALE)) {
1234
1231
                        mult_m4_m4m4(loc_mat, parchan->pose_mat, offs_bone);
1235
1232
                }
1236
1233
                /* Else (i.e. default, usual case), just use the same matrix for rotation/scaling, and location. */
1237
1234
                else
1238
1235
                        copy_m4_m4(loc_mat, rotscale_mat);
1239
 
# endif
1240
 
# if 0
1241
 
                /* Compose the loc matrix for this bone. */
1242
 
                /* NOTE: That version modifies bone's loc when HINGE/NO_SCALE options are set. */
1243
 
 
1244
 
                /* In these cases we need to compute location separately */
1245
 
                if (bone->flag & (BONE_HINGE|BONE_NO_SCALE|BONE_NO_LOCAL_LOCATION)) {
1246
 
                        float bone_loc[4][4], bone_rotscale[3][3], tmat4[4][4], tmat3[3][3];
1247
 
                        unit_m4(bone_loc);
1248
 
                        unit_m4(loc_mat);
1249
 
                        unit_m4(tmat4);
1250
 
 
1251
 
                        mul_v3_m4v3(bone_loc[3], parchan->pose_mat, offs_bone[3]);
1252
 
 
1253
 
                        /* "No local location" is not transformed by bone matrix. */
1254
 
                        /* This only affects orientations (rotations), as scale is always 1.0 here. */
1255
 
                        if (bone->flag & BONE_NO_LOCAL_LOCATION)
1256
 
                                unit_m3(bone_rotscale);
1257
 
                        else
1258
 
                                /* We could also use bone->bone_mat directly, here... */
1259
 
                                copy_m3_m4(bone_rotscale, offs_bone);
1260
 
 
1261
 
                        if (bone->flag & BONE_HINGE) {
1262
 
                                copy_m3_m4(tmat3, parbone->arm_mat);
1263
 
                                /* for hinge-only, we use armature *rotation*, but pose mat *scale*! */
1264
 
                                if (!(bone->flag & BONE_NO_SCALE)) {
1265
 
                                        float size[3], tsmat[3][3];
1266
 
                                        mat4_to_size(size, parchan->pose_mat);
1267
 
                                        size_to_mat3(tsmat, size);
1268
 
                                        mul_m3_m3m3(tmat3, tsmat, tmat3);
1269
 
                                }
1270
 
                                mul_m3_m3m3(bone_rotscale, tmat3, bone_rotscale);
1271
 
                        }
1272
 
                        else if (bone->flag & BONE_NO_SCALE) {
1273
 
                                /* For no-scale only, normalized parent pose mat is enough! */
1274
 
                                copy_m3_m4(tmat3, parchan->pose_mat);
1275
 
                                normalize_m3(tmat3);
1276
 
                                mul_m3_m3m3(bone_rotscale, tmat3, bone_rotscale);
1277
 
                        }
1278
 
                        /* NO_LOCAL_LOCATION only. */
1279
 
                        else {
1280
 
                                copy_m3_m4(tmat3, parchan->pose_mat);
1281
 
                                mul_m3_m3m3(bone_rotscale, tmat3, bone_rotscale);
1282
 
                        }
1283
 
 
1284
 
                        copy_m4_m3(tmat4, bone_rotscale);
1285
 
                        mult_m4_m4m4(loc_mat, bone_loc, tmat4);
1286
 
                }
1287
 
                /* Else, just use the same matrix for rotation/scaling, and location. */
1288
 
                else
1289
 
                        copy_m4_m4(loc_mat, rotscale_mat);
1290
 
# endif
1291
1236
        }
1292
1237
        /* Root bones. */
1293
1238
        else {
1307
1252
/* Convert Pose-Space Matrix to Bone-Space Matrix.
1308
1253
 * NOTE: this cannot be used to convert to pose-space transforms of the supplied
1309
1254
 *       pose-channel into its local space (i.e. 'visual'-keyframing) */
1310
 
void armature_mat_pose_to_bone(bPoseChannel *pchan, float inmat[][4], float outmat[][4])
 
1255
void BKE_armature_mat_pose_to_bone(bPoseChannel *pchan, float inmat[4][4], float outmat[4][4])
1311
1256
{
1312
1257
        float rotscale_mat[4][4], loc_mat[4][4], inmat_[4][4];
1313
1258
 
1314
1259
        /* Security, this allows to call with inmat == outmat! */
1315
1260
        copy_m4_m4(inmat_, inmat);
1316
1261
 
1317
 
        pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
 
1262
        BKE_pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
1318
1263
        invert_m4(rotscale_mat);
1319
1264
        invert_m4(loc_mat);
1320
1265
 
1323
1268
}
1324
1269
 
1325
1270
/* Convert Bone-Space Matrix to Pose-Space Matrix. */
1326
 
void armature_mat_bone_to_pose(bPoseChannel *pchan, float inmat[][4], float outmat[][4])
 
1271
void BKE_armature_mat_bone_to_pose(bPoseChannel *pchan, float inmat[4][4], float outmat[4][4])
1327
1272
{
1328
1273
        float rotscale_mat[4][4], loc_mat[4][4], inmat_[4][4];
1329
1274
 
1330
1275
        /* Security, this allows to call with inmat == outmat! */
1331
1276
        copy_m4_m4(inmat_, inmat);
1332
1277
 
1333
 
        pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
 
1278
        BKE_pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
1334
1279
 
1335
1280
        mult_m4_m4m4(outmat, rotscale_mat, inmat_);
1336
1281
        mul_v3_m4v3(outmat[3], loc_mat, inmat_[3]);
1339
1284
/* Convert Pose-Space Location to Bone-Space Location
1340
1285
 * NOTE: this cannot be used to convert to pose-space location of the supplied
1341
1286
 *       pose-channel into its local space (i.e. 'visual'-keyframing) */
1342
 
void armature_loc_pose_to_bone(bPoseChannel *pchan, const float inloc[3], float outloc[3])
 
1287
void BKE_armature_loc_pose_to_bone(bPoseChannel *pchan, const float inloc[3], float outloc[3])
1343
1288
{
1344
1289
        float xLocMat[4][4] = MAT4_UNITY;
1345
1290
        float nLocMat[4][4];
1348
1293
        copy_v3_v3(xLocMat[3], inloc);
1349
1294
 
1350
1295
        /* get bone-space cursor matrix and extract location */
1351
 
        armature_mat_pose_to_bone(pchan, xLocMat, nLocMat);
 
1296
        BKE_armature_mat_pose_to_bone(pchan, xLocMat, nLocMat);
1352
1297
        copy_v3_v3(outloc, nLocMat[3]);
1353
1298
}
1354
1299
 
1355
 
void armature_mat_pose_to_bone_ex(Object *ob, bPoseChannel *pchan, float inmat[][4], float outmat[][4])
 
1300
void BKE_armature_mat_pose_to_bone_ex(Object *ob, bPoseChannel *pchan, float inmat[4][4], float outmat[4][4])
1356
1301
{
1357
1302
        bPoseChannel work_pchan = *pchan;
1358
1303
 
1359
1304
        /* recalculate pose matrix with only parent transformations,
1360
1305
         * bone loc/sca/rot is ignored, scene and frame are not used. */
1361
 
        where_is_pose_bone(NULL, ob, &work_pchan, 0.0f, FALSE);
 
1306
        BKE_pose_where_is_bone(NULL, ob, &work_pchan, 0.0f, FALSE);
1362
1307
 
1363
1308
        /* find the matrix, need to remove the bone transforms first so this is
1364
1309
         * calculated as a matrix to set rather then a difference ontop of whats
1365
1310
         * already there. */
1366
1311
        unit_m4(outmat);
1367
 
        pchan_apply_mat4(&work_pchan, outmat, FALSE);
 
1312
        BKE_pchan_apply_mat4(&work_pchan, outmat, FALSE);
1368
1313
 
1369
 
        armature_mat_pose_to_bone(&work_pchan, inmat, outmat);
 
1314
        BKE_armature_mat_pose_to_bone(&work_pchan, inmat, outmat);
1370
1315
}
1371
1316
 
1372
 
/* same as object_mat3_to_rot() */
1373
 
void pchan_mat3_to_rot(bPoseChannel *pchan, float mat[][3], short use_compat)
 
1317
/* same as BKE_object_mat3_to_rot() */
 
1318
void BKE_pchan_mat3_to_rot(bPoseChannel *pchan, float mat[3][3], short use_compat)
1374
1319
{
1375
 
        switch(pchan->rotmode) {
 
1320
        switch (pchan->rotmode) {
1376
1321
                case ROT_MODE_QUAT:
1377
1322
                        mat3_to_quat(pchan->quat, mat);
1378
1323
                        break;
1388
1333
}
1389
1334
 
1390
1335
/* Apply a 4x4 matrix to the pose bone,
1391
 
 * similar to object_apply_mat4() */
1392
 
void pchan_apply_mat4(bPoseChannel *pchan, float mat[][4], short use_compat)
 
1336
 * similar to BKE_object_apply_mat4() */
 
1337
void BKE_pchan_apply_mat4(bPoseChannel *pchan, float mat[4][4], short use_compat)
1393
1338
{
1394
1339
        float rot[3][3];
1395
1340
        mat4_to_loc_rot_size(pchan->loc, rot, pchan->size, mat);
1396
 
        pchan_mat3_to_rot(pchan, rot, use_compat);
 
1341
        BKE_pchan_mat3_to_rot(pchan, rot, use_compat);
1397
1342
}
1398
1343
 
1399
1344
/* Remove rest-position effects from pose-transform for obtaining
1400
1345
 * 'visual' transformation of pose-channel.
1401
1346
 * (used by the Visual-Keyframing stuff) */
1402
 
void armature_mat_pose_to_delta(float delta_mat[][4], float pose_mat[][4], float arm_mat[][4])
 
1347
void BKE_armature_mat_pose_to_delta(float delta_mat[4][4], float pose_mat[4][4], float arm_mat[4][4])
1403
1348
{
1404
1349
        float imat[4][4];
1405
1350
 
1413
1358
/* Called from RNA when rotation mode changes
1414
1359
 * - the result should be that the rotations given in the provided pointers have had conversions
1415
1360
 *   applied (as appropriate), such that the rotation of the element hasn't 'visually' changed  */
1416
 
void BKE_rotMode_change_values (float quat[4], float eul[3], float axis[3], float *angle, short oldMode, short newMode)
 
1361
void BKE_rotMode_change_values(float quat[4], float eul[3], float axis[3], float *angle, short oldMode, short newMode)
1417
1362
{
1418
1363
        /* check if any change - if so, need to convert data */
1419
1364
        if (newMode > 0) { /* to euler */
1420
1365
                if (oldMode == ROT_MODE_AXISANGLE) {
1421
1366
                        /* axis-angle to euler */
1422
 
                        axis_angle_to_eulO( eul, newMode,axis, *angle);
 
1367
                        axis_angle_to_eulO(eul, newMode, axis, *angle);
1423
1368
                }
1424
1369
                else if (oldMode == ROT_MODE_QUAT) {
1425
1370
                        /* quat to euler */
1426
1371
                        normalize_qt(quat);
1427
 
                        quat_to_eulO(eul, newMode,quat);
 
1372
                        quat_to_eulO(eul, newMode, quat);
1428
1373
                }
1429
1374
                /* else { no conversion needed } */
1430
1375
        }
1479
1424
 * *************************************************************************** */
1480
1425
/* Computes vector and roll based on a rotation.
1481
1426
 * "mat" must contain only a rotation, and no scaling. */
1482
 
void mat3_to_vec_roll(float mat[][3], float vec[3], float *roll)
 
1427
void mat3_to_vec_roll(float mat[3][3], float r_vec[3], float *r_roll)
1483
1428
{
1484
 
        if (vec)
1485
 
                copy_v3_v3(vec, mat[1]);
 
1429
        if (r_vec) {
 
1430
                copy_v3_v3(r_vec, mat[1]);
 
1431
        }
1486
1432
 
1487
 
        if (roll) {
 
1433
        if (r_roll) {
1488
1434
                float vecmat[3][3], vecmatinv[3][3], rollmat[3][3];
1489
1435
 
1490
1436
                vec_roll_to_mat3(mat[1], 0.0f, vecmat);
1491
1437
                invert_m3_m3(vecmatinv, vecmat);
1492
1438
                mul_m3_m3m3(rollmat, vecmatinv, mat);
1493
1439
 
1494
 
                *roll = (float)atan2(rollmat[2][0], rollmat[2][2]);
 
1440
                *r_roll = atan2f(rollmat[2][0], rollmat[2][2]);
1495
1441
        }
1496
1442
}
1497
1443
 
1498
1444
/* Calculates the rest matrix of a bone based
1499
1445
 * On its vector and a roll around that vector */
1500
 
void vec_roll_to_mat3(const float vec[3], const float roll, float mat[][3])
 
1446
void vec_roll_to_mat3(const float vec[3], const float roll, float mat[3][3])
1501
1447
{
1502
1448
        float nor[3], axis[3], target[3] = {0, 1, 0};
1503
1449
        float theta;
1515
1461
         * so a value inbetween these is needed.
1516
1462
         *
1517
1463
         * was 0.000001, causes bug [#30438] (which is same as [#27675, imho).
1518
 
         * Reseting it to org value seems to cause no more [#23954]...
 
1464
         * Resetting it to org value seems to cause no more [#23954]...
1519
1465
         *
1520
 
         * was 0.0000000000001, caused bug [#], smaller values give unstable
 
1466
         * was 0.0000000000001, caused bug [#31333], smaller values give unstable
1521
1467
         * roll when toggling editmode again...
1522
 
         * No good value here, trying 0.000000001 as best compromize. :/
 
1468
         * No good value here, trying 0.000000001 as best compromise. :/
1523
1469
         */
1524
1470
        if (dot_v3v3(axis, axis) > 1.0e-9f) {
1525
1471
                /* if nor is *not* a multiple of target ... */
1535
1481
                float updown;
1536
1482
 
1537
1483
                /* point same direction, or opposite? */
1538
 
                updown = (dot_v3v3(target,nor) > 0) ? 1.0f : -1.0f;
 
1484
                updown = (dot_v3v3(target, nor) > 0) ? 1.0f : -1.0f;
1539
1485
 
1540
1486
                /* I think this should work... */
1541
1487
                bMatrix[0][0] = updown; bMatrix[0][1] = 0.0;    bMatrix[0][2] = 0.0;
1553
1499
 
1554
1500
/* recursive part, calculates restposition of entire tree of children */
1555
1501
/* used by exiting editmode too */
1556
 
void where_is_armature_bone(Bone *bone, Bone *prevbone)
 
1502
void BKE_armature_where_is_bone(Bone *bone, Bone *prevbone)
1557
1503
{
1558
1504
        float vec[3];
1559
1505
 
1586
1532
        /* and the kiddies */
1587
1533
        prevbone = bone;
1588
1534
        for (bone = bone->childbase.first; bone; bone = bone->next) {
1589
 
                where_is_armature_bone(bone, prevbone);
 
1535
                BKE_armature_where_is_bone(bone, prevbone);
1590
1536
        }
1591
1537
}
1592
1538
 
1593
1539
/* updates vectors and matrices on rest-position level, only needed
1594
1540
 * after editing armature itself, now only on reading file */
1595
 
void where_is_armature(bArmature *arm)
 
1541
void BKE_armature_where_is(bArmature *arm)
1596
1542
{
1597
1543
        Bone *bone;
1598
1544
 
1599
1545
        /* hierarchical from root to children */
1600
1546
        for (bone = arm->bonebase.first; bone; bone = bone->next) {
1601
 
                where_is_armature_bone(bone, NULL);
 
1547
                BKE_armature_where_is_bone(bone, NULL);
1602
1548
        }
1603
1549
}
1604
1550
 
1607
1553
static void pose_proxy_synchronize(Object *ob, Object *from, int layer_protected)
1608
1554
{
1609
1555
        bPose *pose = ob->pose, *frompose = from->pose;
1610
 
        bPoseChannel *pchan, *pchanp, pchanw;
 
1556
        bPoseChannel *pchan, *pchanp;
1611
1557
        bConstraint *con;
1612
1558
        int error = 0;
1613
1559
 
1618
1564
         * to avoid crashing check for possible errors here */
1619
1565
        for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1620
1566
                if (pchan->bone->layer & layer_protected) {
1621
 
                        if (get_pose_channel(frompose, pchan->name) == NULL) {
 
1567
                        if (BKE_pose_channel_find_name(frompose, pchan->name) == NULL) {
1622
1568
                                printf("failed to sync proxy armature because '%s' is missing pose channel '%s'\n",
1623
1569
                                       from->id.name, pchan->name);
1624
1570
                                error = 1;
1630
1576
                return;
1631
1577
 
1632
1578
        /* clear all transformation values from library */
1633
 
        rest_pose(frompose);
 
1579
        BKE_pose_rest(frompose);
1634
1580
 
1635
1581
        /* copy over all of the proxy's bone groups */
1636
 
                /* TODO for later
1637
 
                 * - implement 'local' bone groups as for constraints
1638
 
                 * Note: this isn't trivial, as bones reference groups by index not by pointer,
1639
 
                 *       so syncing things correctly needs careful attention */
 
1582
        /* TODO for later
 
1583
         * - implement 'local' bone groups as for constraints
 
1584
         * Note: this isn't trivial, as bones reference groups by index not by pointer,
 
1585
         *       so syncing things correctly needs careful attention */
1640
1586
        BLI_freelistN(&pose->agroups);
1641
1587
        BLI_duplicatelist(&pose->agroups, &frompose->agroups);
1642
1588
        pose->active_group = frompose->active_group;
1643
1589
 
1644
1590
        for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1645
 
                pchanp = get_pose_channel(frompose, pchan->name);
1646
 
 
1647
 
                if (pchan->bone->layer & layer_protected) {
 
1591
                pchanp = BKE_pose_channel_find_name(frompose, pchan->name);
 
1592
                
 
1593
                if (UNLIKELY(pchanp == NULL)) {
 
1594
                        /* happens for proxies that become invalid because of a missing link
 
1595
                         * for regular cases it shouldn't happen at all */
 
1596
                }
 
1597
                else if (pchan->bone->layer & layer_protected) {
1648
1598
                        ListBase proxylocal_constraints = {NULL, NULL};
1649
 
 
 
1599
                        bPoseChannel pchanw = {NULL};
 
1600
                        
1650
1601
                        /* copy posechannel to temp, but restore important pointers */
1651
1602
                        pchanw = *pchanp;
1652
1603
                        pchanw.prev = pchan->prev;
1653
1604
                        pchanw.next = pchan->next;
1654
1605
                        pchanw.parent = pchan->parent;
1655
1606
                        pchanw.child = pchan->child;
1656
 
 
 
1607
                        
1657
1608
                        /* this is freed so copy a copy, else undo crashes */
1658
1609
                        if (pchanw.prop) {
1659
1610
                                pchanw.prop = IDP_CopyProperty(pchanw.prop);
1660
 
 
 
1611
                                
1661
1612
                                /* use the values from the the existing props */
1662
1613
                                if (pchan->prop) {
1663
1614
                                        IDP_SyncGroupValues(pchanw.prop, pchan->prop);
1664
1615
                                }
1665
1616
                        }
1666
 
 
 
1617
                        
1667
1618
                        /* constraints - proxy constraints are flushed... local ones are added after
1668
1619
                         *     1. extract constraints not from proxy (CONSTRAINT_PROXY_LOCAL) from pchan's constraints
1669
1620
                         *     2. copy proxy-pchan's constraints on-to new
1670
1621
                         *     3. add extracted local constraints back on top
1671
1622
                         *
1672
 
                         * Note for copy_constraints: when copying constraints, disable 'do_extern' otherwise
1673
 
                         *                            we get the libs direct linked in this blend. */
1674
 
                        extract_proxylocal_constraints(&proxylocal_constraints, &pchan->constraints);
1675
 
                        copy_constraints(&pchanw.constraints, &pchanp->constraints, FALSE);
 
1623
                         * Note for BKE_copy_constraints: when copying constraints, disable 'do_extern' otherwise
 
1624
                         *                                we get the libs direct linked in this blend.
 
1625
                         */
 
1626
                        BKE_extract_proxylocal_constraints(&proxylocal_constraints, &pchan->constraints);
 
1627
                        BKE_copy_constraints(&pchanw.constraints, &pchanp->constraints, FALSE);
1676
1628
                        BLI_movelisttolist(&pchanw.constraints, &proxylocal_constraints);
1677
 
 
 
1629
                        
1678
1630
                        /* constraints - set target ob pointer to own object */
1679
1631
                        for (con = pchanw.constraints.first; con; con = con->next) {
1680
 
                                bConstraintTypeInfo *cti = constraint_get_typeinfo(con);
 
1632
                                bConstraintTypeInfo *cti = BKE_constraint_get_typeinfo(con);
1681
1633
                                ListBase targets = {NULL, NULL};
1682
1634
                                bConstraintTarget *ct;
1683
 
 
 
1635
                                
1684
1636
                                if (cti && cti->get_constraint_targets) {
1685
1637
                                        cti->get_constraint_targets(con, &targets);
1686
 
 
 
1638
                                        
1687
1639
                                        for (ct = targets.first; ct; ct = ct->next) {
1688
1640
                                                if (ct->tar == from)
1689
1641
                                                        ct->tar = ob;
1690
1642
                                        }
1691
 
 
 
1643
                                        
1692
1644
                                        if (cti->flush_constraint_targets)
1693
1645
                                                cti->flush_constraint_targets(con, &targets, 0);
1694
1646
                                }
1695
1647
                        }
1696
 
 
 
1648
                        
1697
1649
                        /* free stuff from current channel */
1698
 
                        free_pose_channel(pchan);
1699
 
 
1700
 
                        /* the final copy */
 
1650
                        BKE_pose_channel_free(pchan);
 
1651
                        
 
1652
                        /* copy data in temp back over to the cleaned-out (but still allocated) original channel */
1701
1653
                        *pchan = pchanw;
1702
1654
                }
1703
1655
                else {
1704
1656
                        /* always copy custom shape */
1705
1657
                        pchan->custom = pchanp->custom;
1706
 
                        pchan->custom_tx = pchanp->custom_tx;
 
1658
                        if (pchanp->custom_tx)
 
1659
                                pchan->custom_tx = BKE_pose_channel_find_name(pose, pchanp->custom_tx->name);
1707
1660
 
1708
1661
                        /* ID-Property Syncing */
1709
1662
                        {
1729
1682
 
1730
1683
static int rebuild_pose_bone(bPose *pose, Bone *bone, bPoseChannel *parchan, int counter)
1731
1684
{
1732
 
        bPoseChannel *pchan = verify_pose_channel(pose, bone->name); /* verify checks and/or adds */
 
1685
        bPoseChannel *pchan = BKE_pose_channel_verify(pose, bone->name); /* verify checks and/or adds */
1733
1686
 
1734
1687
        pchan->bone = bone;
1735
1688
        pchan->parent = parchan;
1740
1693
                counter = rebuild_pose_bone(pose, bone, pchan, counter);
1741
1694
                /* for quick detecting of next bone in chain, only b-bone uses it now */
1742
1695
                if (bone->flag & BONE_CONNECTED)
1743
 
                        pchan->child = get_pose_channel(pose, bone->name);
 
1696
                        pchan->child = BKE_pose_channel_find_name(pose, bone->name);
1744
1697
        }
1745
1698
 
1746
1699
        return counter;
1748
1701
 
1749
1702
/* only after leave editmode, duplicating, validating older files, library syncing */
1750
1703
/* NOTE: pose->flag is set for it */
1751
 
void armature_rebuild_pose(Object *ob, bArmature *arm)
 
1704
void BKE_pose_rebuild(Object *ob, bArmature *arm)
1752
1705
{
1753
1706
        Bone *bone;
1754
1707
        bPose *pose;
1780
1733
        for (pchan = pose->chanbase.first; pchan; pchan = next) {
1781
1734
                next = pchan->next;
1782
1735
                if (pchan->bone == NULL) {
1783
 
                        free_pose_channel(pchan);
1784
 
                        free_pose_channels_hash(pose);
 
1736
                        BKE_pose_channel_free(pchan);
 
1737
                        BKE_pose_channels_hash_free(pose);
1785
1738
                        BLI_freelinkN(&pose->chanbase, pchan);
1786
1739
                }
1787
1740
        }
1789
1742
 
1790
1743
        /* synchronize protected layers with proxy */
1791
1744
        if (ob->proxy) {
1792
 
                object_copy_proxy_drivers(ob, ob->proxy);
 
1745
                BKE_object_copy_proxy_drivers(ob, ob->proxy);
1793
1746
                pose_proxy_synchronize(ob, ob->proxy, arm->layer_protected);
1794
1747
        }
1795
1748
 
1796
 
        update_pose_constraint_flags(ob->pose); /* for IK detection for example */
 
1749
        BKE_pose_update_constraint_flags(ob->pose); /* for IK detection for example */
1797
1750
 
1798
1751
        /* the sorting */
1799
 
        if (counter>1)
 
1752
        if (counter > 1)
1800
1753
                DAG_pose_sort(ob);
1801
1754
 
1802
1755
        ob->pose->flag &= ~POSE_RECALC;
1803
1756
        ob->pose->flag |= POSE_WAS_REBUILT;
1804
1757
 
1805
 
        make_pose_channels_hash(ob->pose);
 
1758
        BKE_pose_channels_hash_make(ob->pose);
1806
1759
}
1807
1760
 
1808
1761
 
1849
1802
                        if ((ikData->tar == NULL) || (ikData->tar->type != OB_CURVE))
1850
1803
                                continue;
1851
1804
                        /* skip if disabled */
1852
 
                        if ((con->enforce == 0.0f) || (con->flag & (CONSTRAINT_DISABLE|CONSTRAINT_OFF)))
 
1805
                        if ((con->enforce == 0.0f) || (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)))
1853
1806
                                continue;
1854
1807
 
1855
1808
                        /* otherwise, constraint is ok... */
1870
1823
                 */
1871
1824
 
1872
1825
                /* only happens on reload file, but violates depsgraph still... fix! */
1873
 
                if ((cu->path == NULL) || (cu->path->data == NULL))
1874
 
                        makeDispListCurveTypes(scene, ikData->tar, 0);
 
1826
                if (ELEM(NULL, cu->path, cu->path->data)) {
 
1827
                        BKE_displist_make_curveTypes(scene, ikData->tar, 0);
 
1828
                        
 
1829
                        /* path building may fail in EditMode after removing verts [#33268]*/
 
1830
                        if (ELEM(NULL, cu->path, cu->path->data)) {
 
1831
                                /* BLI_assert(cu->path != NULL); */
 
1832
                                return;
 
1833
                        }
 
1834
                }
1875
1835
        }
1876
1836
 
1877
1837
        /* find the root bone and the chain of bones from the root to the tip
1888
1848
        if (segcount == 0)
1889
1849
                return;
1890
1850
        else
1891
 
                pchanRoot = pchanChain[segcount-1];
 
1851
                pchanRoot = pchanChain[segcount - 1];
1892
1852
 
1893
1853
        /* perform binding step if required */
1894
1854
        if ((ikData->flag & CONSTRAINT_SPLINEIK_BOUND) == 0) {
1898
1858
                /* setup new empty array for the points list */
1899
1859
                if (ikData->points)
1900
1860
                        MEM_freeN(ikData->points);
1901
 
                ikData->numpoints = ikData->chainlen+1;
1902
 
                ikData->points = MEM_callocN(sizeof(float)*ikData->numpoints, "Spline IK Binding");
 
1861
                ikData->numpoints = ikData->chainlen + 1;
 
1862
                ikData->points = MEM_callocN(sizeof(float) * ikData->numpoints, "Spline IK Binding");
1903
1863
 
1904
1864
                /* bind 'tip' of chain (i.e. first joint = tip of bone with the Spline IK Constraint) */
1905
1865
                ikData->points[0] = 1.0f;
1913
1873
                         */
1914
1874
                        if ((ikData->flag & CONSTRAINT_SPLINEIK_EVENSPLITS) || (totLength == 0.0f)) {
1915
1875
                                /* 1) equi-spaced joints */
1916
 
                                ikData->points[i+1] = ikData->points[i] - segmentLen;
 
1876
                                ikData->points[i + 1] = ikData->points[i] - segmentLen;
1917
1877
                        }
1918
1878
                        else {
1919
1879
                                /* 2) to find this point on the curve, we take a step from the previous joint
1920
1880
                                 *    a distance given by the proportion that this bone takes
1921
1881
                                 */
1922
 
                                ikData->points[i+1] = ikData->points[i] - (boneLengths[i] / totLength);
 
1882
                                ikData->points[i + 1] = ikData->points[i] - (boneLengths[i] / totLength);
1923
1883
                        }
1924
1884
                }
1925
1885
 
1973
1933
                tree->chainlen = segcount;
1974
1934
 
1975
1935
                /* copy over the array of links to bones in the chain (from tip to root) */
1976
 
                tree->chain = MEM_callocN(sizeof(bPoseChannel*)*segcount, "SplineIK Chain");
1977
 
                memcpy(tree->chain, pchanChain, sizeof(bPoseChannel*)*segcount);
 
1936
                tree->chain = MEM_callocN(sizeof(bPoseChannel *) * segcount, "SplineIK Chain");
 
1937
                memcpy(tree->chain, pchanChain, sizeof(bPoseChannel *) * segcount);
1978
1938
 
1979
1939
                /* store reference to joint position array */
1980
1940
                tree->points = jointPoints;
2013
1973
{
2014
1974
        bSplineIKConstraint *ikData = tree->ikData;
2015
1975
        float poseHead[3], poseTail[3], poseMat[4][4];
2016
 
        float splineVec[3], scaleFac, radius =1.0f;
 
1976
        float splineVec[3], scaleFac, radius = 1.0f;
2017
1977
 
2018
1978
        /* firstly, calculate the bone matrix the standard way, since this is needed for roll control */
2019
 
        where_is_pose_bone(scene, ob, pchan, ctime, 1);
 
1979
        BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
2020
1980
 
2021
1981
        copy_v3_v3(poseHead, pchan->pose_head);
2022
1982
        copy_v3_v3(poseTail, pchan->pose_tail);
2027
1987
                float tailBlendFac = 1.0f;
2028
1988
 
2029
1989
                /* determine if the bone should still be affected by SplineIK */
2030
 
                if (tree->points[index+1] >= 1.0f) {
 
1990
                if (tree->points[index + 1] >= 1.0f) {
2031
1991
                        /* spline doesn't affect the bone anymore, so done... */
2032
1992
                        pchan->flag |= POSE_DONE;
2033
1993
                        return;
2034
1994
                }
2035
 
                else if ((tree->points[index] >= 1.0f) && (tree->points[index+1] < 1.0f)) {
 
1995
                else if ((tree->points[index] >= 1.0f) && (tree->points[index + 1] < 1.0f)) {
2036
1996
                        /* blending factor depends on the amount of the bone still left on the chain */
2037
 
                        tailBlendFac = (1.0f - tree->points[index+1]) / (tree->points[index] - tree->points[index+1]);
 
1997
                        tailBlendFac = (1.0f - tree->points[index + 1]) / (tree->points[index] - tree->points[index + 1]);
2038
1998
                }
2039
1999
 
2040
2000
                /* tail endpoint */
2054
2014
                }
2055
2015
 
2056
2016
                /* head endpoint */
2057
 
                if (where_on_path(ikData->tar, tree->points[index+1], vec, dir, NULL, &rad, NULL)) {
 
2017
                if (where_on_path(ikData->tar, tree->points[index + 1], vec, dir, NULL, &rad, NULL)) {
2058
2018
                        /* apply curve's object-mode transforms to the position
2059
2019
                         * unless the option to allow curve to be positioned elsewhere is activated (i.e. no root)
2060
2020
                         */
2066
2026
                        copy_v3_v3(poseHead, vec);
2067
2027
 
2068
2028
                        /* set the new radius (it should be the average value) */
2069
 
                        radius = (radius+rad) / 2;
 
2029
                        radius = (radius + rad) / 2;
2070
2030
                }
2071
2031
        }
2072
2032
 
2101
2061
                cross_v3_v3v3(raxis, rmat[1], splineVec);
2102
2062
 
2103
2063
                rangle = dot_v3v3(rmat[1], splineVec);
2104
 
                rangle = acos(MAX2(-1.0f, MIN2(1.0f, rangle)));
 
2064
                CLAMP(rangle, -1.0f, 1.0f);
 
2065
                rangle = acosf(rangle);
2105
2066
 
2106
2067
                /* multiply the magnitude of the angle by the influence of the constraint to
2107
2068
                 * control the influence of the SplineIK effect
2140
2101
                                scale = len_v3(pchan->pose_mat[2]);
2141
2102
                                mul_v3_fl(poseMat[2], scale);
2142
2103
                        }
2143
 
                                break;
 
2104
                        break;
2144
2105
                        case CONSTRAINT_SPLINEIK_XZS_VOLUMETRIC:
2145
2106
                        {
2146
2107
                                /* 'volume preservation' */
2163
2124
                                mul_v3_fl(poseMat[0], scale);
2164
2125
                                mul_v3_fl(poseMat[2], scale);
2165
2126
                        }
2166
 
                                break;
 
2127
                        break;
2167
2128
                }
2168
2129
 
2169
2130
                /* finally, multiply the x and z scaling by the radius of the curve too,
2202
2163
        copy_v3_v3(pchan->pose_head, poseHead);
2203
2164
 
2204
2165
        /* recalculate tail, as it's now outdated after the head gets adjusted above! */
2205
 
        where_is_pose_bone_tail(pchan);
 
2166
        BKE_pose_where_is_bone_tail(pchan);
2206
2167
 
2207
2168
        /* done! */
2208
2169
        pchan->flag |= POSE_DONE;
2221
2182
                 *     - the chain is traversed in the opposite order to storage order (i.e. parent to children)
2222
2183
                 *       so that dependencies are correct
2223
2184
                 */
2224
 
                for (i = tree->chainlen-1; i >= 0; i--) {
 
2185
                for (i = tree->chainlen - 1; i >= 0; i--) {
2225
2186
                        bPoseChannel *pchan = tree->chain[i];
2226
2187
                        splineik_evaluate_bone(tree, scene, ob, pchan, i, ctime);
2227
2188
                }
2240
2201
/* ********************** THE POSE SOLVER ******************* */
2241
2202
 
2242
2203
/* loc/rot/size to given mat4 */
2243
 
void pchan_to_mat4(bPoseChannel *pchan, float chan_mat[4][4])
 
2204
void BKE_pchan_to_mat4(bPoseChannel *pchan, float chan_mat[4][4])
2244
2205
{
2245
2206
        float smat[3][3];
2246
2207
        float rmat[3][3];
2259
2220
                axis_angle_to_mat3(rmat, pchan->rotAxis, pchan->rotAngle);
2260
2221
        }
2261
2222
        else {
2262
 
                /* quats are normalised before use to eliminate scaling issues */
 
2223
                /* quats are normalized before use to eliminate scaling issues */
2263
2224
                float quat[4];
2264
2225
 
2265
2226
                /* NOTE: we now don't normalize the stored values anymore, since this was kindof evil in some cases
2283
2244
 
2284
2245
/* loc/rot/size to mat4 */
2285
2246
/* used in constraint.c too */
2286
 
void pchan_calc_mat(bPoseChannel *pchan)
 
2247
void BKE_pchan_calc_mat(bPoseChannel *pchan)
2287
2248
{
2288
2249
        /* this is just a wrapper around the copy of this function which calculates the matrix
2289
2250
         * and stores the result in any given channel
2290
2251
         */
2291
 
        pchan_to_mat4(pchan, pchan->chan_mat);
 
2252
        BKE_pchan_to_mat4(pchan, pchan->chan_mat);
2292
2253
}
2293
2254
 
2294
2255
#if 0 /* XXX OLD ANIMSYS, NLASTRIPS ARE NO LONGER USED */
2298
2259
{
2299
2260
        bActionModifier *amod;
2300
2261
        bActionStrip *strip, *strip2;
2301
 
        float scene_cfra= (float)scene->r.cfra;
 
2262
        float scene_cfra = BKE_scene_frame_get(scene);
2302
2263
        int do_modif;
2303
2264
 
2304
 
        for (strip=armob->nlastrips.first; strip; strip=strip->next) {
2305
 
                do_modif=0;
 
2265
        for (strip = armob->nlastrips.first; strip; strip = strip->next) {
 
2266
                do_modif = FALSE;
2306
2267
 
2307
 
                if (scene_cfra>=strip->start && scene_cfra<=strip->end)
2308
 
                        do_modif=1;
 
2268
                if (scene_cfra >= strip->start && scene_cfra <= strip->end)
 
2269
                        do_modif = TRUE;
2309
2270
 
2310
2271
                if ((scene_cfra > strip->end) && (strip->flag & ACTSTRIP_HOLDLASTFRAME)) {
2311
 
                        do_modif=1;
 
2272
                        do_modif = TRUE;
2312
2273
 
2313
2274
                        /* if there are any other strips active, ignore modifiers for this strip -
2314
2275
                         * 'hold' option should only hold action modifiers if there are
2315
2276
                         * no other active strips */
2316
 
                        for (strip2=strip->next; strip2; strip2=strip2->next) {
 
2277
                        for (strip2 = strip->next; strip2; strip2 = strip2->next) {
2317
2278
                                if (strip2 == strip) continue;
2318
2279
 
2319
 
                                if (scene_cfra>=strip2->start && scene_cfra<=strip2->end) {
 
2280
                                if (scene_cfra >= strip2->start && scene_cfra <= strip2->end) {
2320
2281
                                        if (!(strip2->flag & ACTSTRIP_MUTE))
2321
 
                                                do_modif=0;
 
2282
                                                do_modif = FALSE;
2322
2283
                                }
2323
2284
                        }
2324
2285
 
2325
2286
                        /* if there are any later, activated, strips with 'hold' set, they take precedence,
2326
2287
                         * so ignore modifiers for this strip */
2327
 
                        for (strip2=strip->next; strip2; strip2=strip2->next) {
 
2288
                        for (strip2 = strip->next; strip2; strip2 = strip2->next) {
2328
2289
                                if (scene_cfra < strip2->start) continue;
2329
2290
                                if ((strip2->flag & ACTSTRIP_HOLDLASTFRAME) && !(strip2->flag & ACTSTRIP_MUTE)) {
2330
 
                                        do_modif=0;
 
2291
                                        do_modif = FALSE;
2331
2292
                                }
2332
2293
                        }
2333
2294
                }
2334
2295
 
2335
2296
                if (do_modif) {
2336
2297
                        /* temporal solution to prevent 2 strips accumulating */
2337
 
                        if (scene_cfra==strip->end && strip->next && strip->next->start==scene_cfra)
 
2298
                        if (scene_cfra == strip->end && strip->next && strip->next->start == scene_cfra)
2338
2299
                                continue;
2339
2300
 
2340
 
                        for (amod= strip->modifiers.first; amod; amod= amod->next) {
 
2301
                        for (amod = strip->modifiers.first; amod; amod = amod->next) {
2341
2302
                                switch (amod->type) {
2342
 
                                case ACTSTRIP_MOD_DEFORM:
2343
 
                                {
2344
 
                                        /* validate first */
2345
 
                                        if (amod->ob && amod->ob->type==OB_CURVE && amod->channel[0]) {
2346
 
 
2347
 
                                                if ( strcmp(pchan->name, amod->channel)==0 ) {
2348
 
                                                        float mat4[4][4], mat3[3][3];
2349
 
 
2350
 
                                                        curve_deform_vector(scene, amod->ob, armob, bone->arm_mat[3], pchan->pose_mat[3], mat3, amod->no_rot_axis);
2351
 
                                                        copy_m4_m4(mat4, pchan->pose_mat);
2352
 
                                                        mul_m4_m3m4(pchan->pose_mat, mat3, mat4);
2353
 
 
 
2303
                                        case ACTSTRIP_MOD_DEFORM:
 
2304
                                        {
 
2305
                                                /* validate first */
 
2306
                                                if (amod->ob && amod->ob->type == OB_CURVE && amod->channel[0]) {
 
2307
 
 
2308
                                                        if (strcmp(pchan->name, amod->channel) == 0) {
 
2309
                                                                float mat4[4][4], mat3[3][3];
 
2310
 
 
2311
                                                                curve_deform_vector(scene, amod->ob, armob, bone->arm_mat[3], pchan->pose_mat[3], mat3, amod->no_rot_axis);
 
2312
                                                                copy_m4_m4(mat4, pchan->pose_mat);
 
2313
                                                                mul_m4_m3m4(pchan->pose_mat, mat3, mat4);
 
2314
 
 
2315
                                                        }
2354
2316
                                                }
2355
2317
                                        }
2356
 
                                }
2357
2318
                                        break;
2358
 
                                case ACTSTRIP_MOD_NOISE:
2359
 
                                {
2360
 
                                        if ( strcmp(pchan->name, amod->channel)==0 ) {
2361
 
                                                float nor[3], loc[3], ofs;
2362
 
                                                float eul[3], size[3], eulo[3], sizeo[3];
2363
 
 
2364
 
                                                /* calculate turbulance */
2365
 
                                                ofs = amod->turbul / 200.0f;
2366
 
 
2367
 
                                                /* make a copy of starting conditions */
2368
 
                                                copy_v3_v3(loc, pchan->pose_mat[3]);
2369
 
                                                mat4_to_eul( eul,pchan->pose_mat);
2370
 
                                                mat4_to_size( size,pchan->pose_mat);
2371
 
                                                copy_v3_v3(eulo, eul);
2372
 
                                                copy_v3_v3(sizeo, size);
2373
 
 
2374
 
                                                /* apply noise to each set of channels */
2375
 
                                                if (amod->channels & 4) {
2376
 
                                                        /* for scaling */
2377
 
                                                        nor[0] = BLI_gNoise(amod->noisesize, size[0]+ofs, size[1], size[2], 0, 0) - ofs;
2378
 
                                                        nor[1] = BLI_gNoise(amod->noisesize, size[0], size[1]+ofs, size[2], 0, 0) - ofs;
2379
 
                                                        nor[2] = BLI_gNoise(amod->noisesize, size[0], size[1], size[2]+ofs, 0, 0) - ofs;
2380
 
                                                        add_v3_v3(size, nor);
2381
 
 
2382
 
                                                        if (sizeo[0] != 0)
2383
 
                                                                mul_v3_fl(pchan->pose_mat[0], size[0] / sizeo[0]);
2384
 
                                                        if (sizeo[1] != 0)
2385
 
                                                                mul_v3_fl(pchan->pose_mat[1], size[1] / sizeo[1]);
2386
 
                                                        if (sizeo[2] != 0)
2387
 
                                                                mul_v3_fl(pchan->pose_mat[2], size[2] / sizeo[2]);
2388
 
                                                }
2389
 
                                                if (amod->channels & 2) {
2390
 
                                                        /* for rotation */
2391
 
                                                        nor[0] = BLI_gNoise(amod->noisesize, eul[0]+ofs, eul[1], eul[2], 0, 0) - ofs;
2392
 
                                                        nor[1] = BLI_gNoise(amod->noisesize, eul[0], eul[1]+ofs, eul[2], 0, 0) - ofs;
2393
 
                                                        nor[2] = BLI_gNoise(amod->noisesize, eul[0], eul[1], eul[2]+ofs, 0, 0) - ofs;
2394
 
 
2395
 
                                                        compatible_eul(nor, eulo);
2396
 
                                                        add_v3_v3(eul, nor);
2397
 
                                                        compatible_eul(eul, eulo);
2398
 
 
2399
 
                                                        loc_eul_size_to_mat4(pchan->pose_mat, loc, eul, size);
2400
 
                                                }
2401
 
                                                if (amod->channels & 1) {
2402
 
                                                        /* for location */
2403
 
                                                        nor[0] = BLI_gNoise(amod->noisesize, loc[0]+ofs, loc[1], loc[2], 0, 0) - ofs;
2404
 
                                                        nor[1] = BLI_gNoise(amod->noisesize, loc[0], loc[1]+ofs, loc[2], 0, 0) - ofs;
2405
 
                                                        nor[2] = BLI_gNoise(amod->noisesize, loc[0], loc[1], loc[2]+ofs, 0, 0) - ofs;
2406
 
 
2407
 
                                                        add_v3_v3v3(pchan->pose_mat[3], loc, nor);
 
2319
                                        case ACTSTRIP_MOD_NOISE:
 
2320
                                        {
 
2321
                                                if (strcmp(pchan->name, amod->channel) == 0) {
 
2322
                                                        float nor[3], loc[3], ofs;
 
2323
                                                        float eul[3], size[3], eulo[3], sizeo[3];
 
2324
 
 
2325
                                                        /* calculate turbulance */
 
2326
                                                        ofs = amod->turbul / 200.0f;
 
2327
 
 
2328
                                                        /* make a copy of starting conditions */
 
2329
                                                        copy_v3_v3(loc, pchan->pose_mat[3]);
 
2330
                                                        mat4_to_eul(eul, pchan->pose_mat);
 
2331
                                                        mat4_to_size(size, pchan->pose_mat);
 
2332
                                                        copy_v3_v3(eulo, eul);
 
2333
                                                        copy_v3_v3(sizeo, size);
 
2334
 
 
2335
                                                        /* apply noise to each set of channels */
 
2336
                                                        if (amod->channels & 4) {
 
2337
                                                                /* for scaling */
 
2338
                                                                nor[0] = BLI_gNoise(amod->noisesize, size[0] + ofs, size[1], size[2], 0, 0) - ofs;
 
2339
                                                                nor[1] = BLI_gNoise(amod->noisesize, size[0], size[1] + ofs, size[2], 0, 0) - ofs;
 
2340
                                                                nor[2] = BLI_gNoise(amod->noisesize, size[0], size[1], size[2] + ofs, 0, 0) - ofs;
 
2341
                                                                add_v3_v3(size, nor);
 
2342
 
 
2343
                                                                if (sizeo[0] != 0)
 
2344
                                                                        mul_v3_fl(pchan->pose_mat[0], size[0] / sizeo[0]);
 
2345
                                                                if (sizeo[1] != 0)
 
2346
                                                                        mul_v3_fl(pchan->pose_mat[1], size[1] / sizeo[1]);
 
2347
                                                                if (sizeo[2] != 0)
 
2348
                                                                        mul_v3_fl(pchan->pose_mat[2], size[2] / sizeo[2]);
 
2349
                                                        }
 
2350
                                                        if (amod->channels & 2) {
 
2351
                                                                /* for rotation */
 
2352
                                                                nor[0] = BLI_gNoise(amod->noisesize, eul[0] + ofs, eul[1], eul[2], 0, 0) - ofs;
 
2353
                                                                nor[1] = BLI_gNoise(amod->noisesize, eul[0], eul[1] + ofs, eul[2], 0, 0) - ofs;
 
2354
                                                                nor[2] = BLI_gNoise(amod->noisesize, eul[0], eul[1], eul[2] + ofs, 0, 0) - ofs;
 
2355
 
 
2356
                                                                compatible_eul(nor, eulo);
 
2357
                                                                add_v3_v3(eul, nor);
 
2358
                                                                compatible_eul(eul, eulo);
 
2359
 
 
2360
                                                                loc_eul_size_to_mat4(pchan->pose_mat, loc, eul, size);
 
2361
                                                        }
 
2362
                                                        if (amod->channels & 1) {
 
2363
                                                                /* for location */
 
2364
                                                                nor[0] = BLI_gNoise(amod->noisesize, loc[0] + ofs, loc[1], loc[2], 0, 0) - ofs;
 
2365
                                                                nor[1] = BLI_gNoise(amod->noisesize, loc[0], loc[1] + ofs, loc[2], 0, 0) - ofs;
 
2366
                                                                nor[2] = BLI_gNoise(amod->noisesize, loc[0], loc[1], loc[2] + ofs, 0, 0) - ofs;
 
2367
 
 
2368
                                                                add_v3_v3v3(pchan->pose_mat[3], loc, nor);
 
2369
                                                        }
2408
2370
                                                }
2409
2371
                                        }
2410
 
                                }
2411
2372
                                        break;
2412
2373
                                }
2413
2374
                        }
2418
2379
#endif
2419
2380
 
2420
2381
/* calculate tail of posechannel */
2421
 
void where_is_pose_bone_tail(bPoseChannel *pchan)
 
2382
void BKE_pose_where_is_bone_tail(bPoseChannel *pchan)
2422
2383
{
2423
2384
        float vec[3];
2424
2385
 
2431
2392
/* pchan is validated, as having bone and parent pointer
2432
2393
 * 'do_extra': when zero skips loc/size/rot, constraints and strip modifiers.
2433
2394
 */
2434
 
void where_is_pose_bone(Scene *scene, Object *ob, bPoseChannel *pchan, float ctime, int do_extra)
 
2395
void BKE_pose_where_is_bone(Scene *scene, Object *ob, bPoseChannel *pchan, float ctime, int do_extra)
2435
2396
{
2436
2397
        /* This gives a chan_mat with actions (ipos) results. */
2437
2398
        if (do_extra)
2438
 
                pchan_calc_mat(pchan);
 
2399
                BKE_pchan_calc_mat(pchan);
2439
2400
        else
2440
2401
                unit_m4(pchan->chan_mat);
2441
2402
 
2442
2403
        /* Construct the posemat based on PoseChannels, that we do before applying constraints. */
2443
2404
        /* pose_mat(b) = pose_mat(b-1) * yoffs(b-1) * d_root(b) * bone_mat(b) * chan_mat(b) */
2444
 
        armature_mat_bone_to_pose(pchan, pchan->chan_mat, pchan->pose_mat);
2445
 
#if 0 /* XXX Old code, will remove this later. */
2446
 
        {
2447
 
                float rotscale_mat[4][4], loc_mat[4][4];
2448
 
                pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
2449
 
                /* Rotation and scale. */
2450
 
                mult_m4_m4m4(pchan->pose_mat, rotscale_mat, pchan->chan_mat);
2451
 
                /* Location. */
2452
 
                mul_v3_m4v3(pchan->pose_mat[3], loc_mat, pchan->chan_mat[3]);
2453
 
        }
2454
 
#endif
 
2405
        BKE_armature_mat_bone_to_pose(pchan, pchan->chan_mat, pchan->pose_mat);
2455
2406
 
2456
2407
        /* Only rootbones get the cyclic offset (unless user doesn't want that). */
2457
2408
        /* XXX That could be a problem for snapping and other "reverse transform" features... */
2461
2412
        }
2462
2413
 
2463
2414
        if (do_extra) {
2464
 
#if 0   /* XXX OLD ANIMSYS, NLASTRIPS ARE NO LONGER USED */
 
2415
#if 0   /* XXX OLD ANIMSYS, NLASTRIPS ARE NO LONGER USED */
2465
2416
                /* do NLA strip modifiers - i.e. curve follow */
2466
2417
                do_strip_modifiers(scene, ob, bone, pchan);
2467
2418
#endif
2477
2428
                        /* prepare PoseChannel for Constraint solving
2478
2429
                         * - makes a copy of matrix, and creates temporary struct to use
2479
2430
                         */
2480
 
                        cob = constraints_make_evalob(scene, ob, pchan, CONSTRAINT_OBTYPE_BONE);
 
2431
                        cob = BKE_constraints_make_evalob(scene, ob, pchan, CONSTRAINT_OBTYPE_BONE);
2481
2432
 
2482
2433
                        /* Solve PoseChannel's Constraints */
2483
 
                        solve_constraints(&pchan->constraints, cob, ctime);     /* ctime doesnt alter objects */
 
2434
                        BKE_solve_constraints(&pchan->constraints, cob, ctime); /* ctime doesnt alter objects */
2484
2435
 
2485
2436
                        /* cleanup after Constraint Solving
2486
2437
                         * - applies matrix back to pchan, and frees temporary struct used
2487
2438
                         */
2488
 
                        constraints_clear_evalob(cob);
 
2439
                        BKE_constraints_clear_evalob(cob);
2489
2440
 
2490
2441
                        /* prevent constraints breaking a chain */
2491
2442
                        if (pchan->bone->flag & BONE_CONNECTED) {
2497
2448
        /* calculate head */
2498
2449
        copy_v3_v3(pchan->pose_head, pchan->pose_mat[3]);
2499
2450
        /* calculate tail */
2500
 
        where_is_pose_bone_tail(pchan);
 
2451
        BKE_pose_where_is_bone_tail(pchan);
2501
2452
}
2502
2453
 
2503
2454
/* This only reads anim data from channels, and writes to channels */
2504
2455
/* This is the only function adding poses */
2505
 
void where_is_pose (Scene *scene, Object *ob)
 
2456
void BKE_pose_where_is(Scene *scene, Object *ob)
2506
2457
{
2507
2458
        bArmature *arm;
2508
2459
        Bone *bone;
2517
2468
        if (ELEM(NULL, arm, scene))
2518
2469
                return;
2519
2470
        if ((ob->pose == NULL) || (ob->pose->flag & POSE_RECALC))
2520
 
                armature_rebuild_pose(ob, arm);
 
2471
                BKE_pose_rebuild(ob, arm);
2521
2472
 
2522
 
        ctime = BKE_curframe(scene); /* not accurate... */
 
2473
        ctime = BKE_scene_frame_get(scene); /* not accurate... */
2523
2474
 
2524
2475
        /* In editmode or restposition we read the data from the bones */
2525
2476
        if (arm->edbo || (arm->flag & ARM_RESTPOS)) {
2537
2488
 
2538
2489
                /* 1. clear flags */
2539
2490
                for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2540
 
                        pchan->flag &= ~(POSE_DONE|POSE_CHAIN|POSE_IKTREE|POSE_IKSPLINE);
 
2491
                        pchan->flag &= ~(POSE_DONE | POSE_CHAIN | POSE_IKTREE | POSE_IKSPLINE);
2541
2492
                }
2542
2493
 
2543
2494
                /* 2a. construct the IK tree (standard IK) */
2561
2512
                        }
2562
2513
                        /* 5. otherwise just call the normal solver */
2563
2514
                        else if (!(pchan->flag & POSE_DONE)) {
2564
 
                                where_is_pose_bone(scene, ob, pchan, ctime, 1);
 
2515
                                BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
2565
2516
                        }
2566
2517
                }
2567
2518
                /* 6. release the IK tree */
2577
2528
        }
2578
2529
}
2579
2530
 
2580
 
 
2581
 
/* Returns total selected vgroups,
2582
 
 * wpi.defbase_sel is assumed malloc'd, all values are set */
2583
 
int get_selected_defgroups(Object *ob, char *dg_selection, int defbase_tot)
2584
 
{
2585
 
        bDeformGroup *defgroup;
2586
 
        unsigned int i;
2587
 
        Object *armob = object_pose_armature_get(ob);
2588
 
        int dg_flags_sel_tot = 0;
2589
 
 
2590
 
        if (armob) {
2591
 
                bPose *pose = armob->pose;
2592
 
                for (i = 0, defgroup = ob->defbase.first; i < defbase_tot && defgroup; defgroup = defgroup->next, i++) {
2593
 
                        bPoseChannel *pchan = get_pose_channel(pose, defgroup->name);
2594
 
                        if (pchan && (pchan->bone->flag & BONE_SELECTED)) {
2595
 
                                dg_selection[i] = TRUE;
2596
 
                                dg_flags_sel_tot++;
2597
 
                        }
2598
 
                        else {
2599
 
                                dg_selection[i] = FALSE;
2600
 
                        }
2601
 
                }
2602
 
        }
2603
 
        else {
2604
 
                memset(dg_selection, FALSE, sizeof(char) * defbase_tot);
2605
 
        }
2606
 
 
2607
 
        return dg_flags_sel_tot;
2608
 
}
2609
 
 
2610
2531
/************** Bounding box ********************/
2611
 
int minmax_armature(Object *ob, float min[3], float max[3])
 
2532
static int minmax_armature(Object *ob, float r_min[3], float r_max[3])
2612
2533
{
2613
2534
        bPoseChannel *pchan;
2614
2535
 
2615
 
        /* For now, we assume where_is_pose has already been called (hence we have valid data in pachan). */
 
2536
        /* For now, we assume BKE_pose_where_is has already been called (hence we have valid data in pachan). */
2616
2537
        for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2617
 
                DO_MINMAX(pchan->pose_head, min, max);
2618
 
                DO_MINMAX(pchan->pose_tail, min, max);
 
2538
                minmax_v3v3_v3(r_min, r_max, pchan->pose_head);
 
2539
                minmax_v3v3_v3(r_min, r_max, pchan->pose_tail);
2619
2540
        }
2620
2541
 
2621
2542
        return (ob->pose->chanbase.first != NULL);
2622
2543
}
2623
2544
 
2624
 
void boundbox_armature(Object *ob, float *loc, float *size)
 
2545
static void boundbox_armature(Object *ob, float loc[3], float size[3])
2625
2546
{
2626
2547
        BoundBox *bb;
2627
2548
        float min[3], max[3];
2648
2569
        size[1] = (max[1] - min[1]) / 2.0f;
2649
2570
        size[2] = (max[2] - min[2]) / 2.0f;
2650
2571
 
2651
 
        boundbox_set_from_min_max(bb, min, max);
 
2572
        BKE_boundbox_init_from_minmax(bb, min, max);
2652
2573
}
2653
2574
 
2654
 
BoundBox *BKE_armature_get_bb(Object *ob)
 
2575
BoundBox *BKE_armature_boundbox_get(Object *ob)
2655
2576
{
2656
2577
        boundbox_armature(ob, NULL, NULL);
2657
2578