~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * $Id: constraint.c 29026 2010-05-27 11:56:31Z aligorith $
3
 
 *
 
1
/*
4
2
 * ***** BEGIN GPL LICENSE BLOCK *****
5
3
 *
6
4
 * This program is free software; you can redistribute it and/or
27
25
 * ***** END GPL LICENSE BLOCK *****
28
26
 */
29
27
 
 
28
/** \file blender/blenkernel/intern/constraint.c
 
29
 *  \ingroup bke
 
30
 */
 
31
 
 
32
 
30
33
#include <stdio.h> 
31
34
#include <stddef.h>
32
35
#include <string.h>
36
39
#include "MEM_guardedalloc.h"
37
40
 
38
41
#include "BLI_blenlib.h"
39
 
#include "BLI_listbase.h"
40
42
#include "BLI_math.h"
41
 
#include "BLI_editVert.h"
 
43
#include "BLI_kdopbvh.h"
 
44
#include "BLI_utildefines.h"
42
45
 
43
46
#include "DNA_armature_types.h"
 
47
#include "DNA_camera_types.h"
44
48
#include "DNA_constraint_types.h"
45
49
#include "DNA_modifier_types.h"
46
50
#include "DNA_object_types.h"
48
52
#include "DNA_curve_types.h"
49
53
#include "DNA_mesh_types.h"
50
54
#include "DNA_meshdata_types.h"
 
55
 
51
56
#include "DNA_lattice_types.h"
52
57
#include "DNA_scene_types.h"
53
58
#include "DNA_text_types.h"
54
 
 
55
 
#include "BKE_utildefines.h"
 
59
#include "DNA_tracking_types.h"
 
60
#include "DNA_movieclip_types.h"
 
61
 
 
62
 
56
63
#include "BKE_action.h"
57
64
#include "BKE_anim.h" /* for the curve calculation part */
58
65
#include "BKE_armature.h"
59
66
#include "BKE_blender.h"
 
67
#include "BKE_bvhutils.h"
 
68
#include "BKE_camera.h"
60
69
#include "BKE_constraint.h"
61
70
#include "BKE_displist.h"
62
71
#include "BKE_deform.h"
67
76
#include "BKE_global.h"
68
77
#include "BKE_library.h"
69
78
#include "BKE_idprop.h"
 
79
#include "BKE_mesh.h"
70
80
#include "BKE_shrinkwrap.h"
71
81
#include "BKE_mesh.h"
 
82
#include "BKE_tessmesh.h"
 
83
#include "BKE_tracking.h"
 
84
#include "BKE_movieclip.h"
 
85
#include "BKE_tracking.h"
 
86
#include "BKE_movieclip.h"
72
87
 
73
 
#ifndef DISABLE_PYTHON
 
88
#ifdef WITH_PYTHON
74
89
#include "BPY_extern.h"
75
90
#endif
76
91
 
143
158
                                }
144
159
                                
145
160
                                /* matrix in world-space */
146
 
                                mul_m4_m4m4(cob->matrix, cob->pchan->pose_mat, ob->obmat);
 
161
                                mult_m4_m4m4(cob->matrix, ob->obmat, cob->pchan->pose_mat);
147
162
                        }
148
163
                        else
149
164
                                unit_m4(cob->matrix);
172
187
        
173
188
        /* calculate delta of constraints evaluation */
174
189
        invert_m4_m4(imat, cob->startmat);
175
 
        mul_m4_m4m4(delta, imat, cob->matrix);
 
190
        mult_m4_m4m4(delta, cob->matrix, imat);
176
191
        
177
192
        /* copy matrices back to source */
178
193
        switch (cob->type) {
193
208
                        /* cob->ob or cob->pchan might not exist */
194
209
                        if (cob->ob && cob->pchan) {
195
210
                                /* copy new pose-matrix back to owner */
196
 
                                mul_m4_m4m4(cob->pchan->pose_mat, cob->matrix, cob->ob->imat);
 
211
                                mult_m4_m4m4(cob->pchan->pose_mat, cob->ob->imat, cob->matrix);
197
212
                                
198
213
                                /* copy inverse of delta back to owner */
199
214
                                invert_m4_m4(cob->pchan->constinv, delta);
208
223
 
209
224
/* -------------- Space-Conversion API -------------- */
210
225
 
 
226
#if 0 /* XXX Old code, does the same as one in armature.c, will remove it later. */
 
227
static void constraint_pchan_diff_mat(bPoseChannel *pchan, float diff_mat[4][4])
 
228
{
 
229
        if (pchan->parent) {
 
230
                float offs_bone[4][4];
 
231
 
 
232
                /* construct offs_bone the same way it is done in armature.c */
 
233
                copy_m4_m3(offs_bone, pchan->bone->bone_mat);
 
234
                copy_v3_v3(offs_bone[3], pchan->bone->head);
 
235
                offs_bone[3][1] += pchan->bone->parent->length;
 
236
 
 
237
                if (pchan->bone->flag & BONE_HINGE) {
 
238
                        /* pose_mat = par_pose-space_location * chan_mat */
 
239
                        float tmat[4][4];
 
240
 
 
241
                        /* the rotation of the parent restposition */
 
242
                        copy_m4_m4(tmat, pchan->bone->parent->arm_mat);
 
243
 
 
244
                        /* the location of actual parent transform */
 
245
                        copy_v3_v3(tmat[3], offs_bone[3]);
 
246
                        zero_v3(offs_bone[3]);
 
247
                        mul_m4_v3(pchan->parent->pose_mat, tmat[3]);
 
248
 
 
249
                        mult_m4_m4m4(diff_mat, tmat, offs_bone);
 
250
                }
 
251
                else {
 
252
                        /* pose_mat = par_pose_mat * bone_mat * chan_mat */
 
253
                        if (pchan->bone->flag & BONE_NO_SCALE) {
 
254
                                float tmat[4][4];
 
255
                                copy_m4_m4(tmat, pchan->parent->pose_mat);
 
256
                                normalize_m4(tmat);
 
257
                                mult_m4_m4m4(diff_mat, tmat, offs_bone);
 
258
                        }
 
259
                        else {
 
260
                                mult_m4_m4m4(diff_mat, pchan->parent->pose_mat, offs_bone);
 
261
                        }
 
262
                }
 
263
        }
 
264
        else {
 
265
                /* pose_mat = chan_mat * arm_mat */
 
266
                copy_m4_m4(diff_mat, pchan->bone->arm_mat);
 
267
        }
 
268
}
 
269
#endif
 
270
 
211
271
/* This function is responsible for the correct transformations/conversions 
212
272
 * of a matrix from one space to another for constraint evaluation.
213
273
 * For now, this is only implemented for Objects and PoseChannels.
214
274
 */
215
275
void constraint_mat_convertspace (Object *ob, bPoseChannel *pchan, float mat[][4], short from, short to)
216
276
{
217
 
        float tempmat[4][4];
218
277
        float diff_mat[4][4];
219
278
        float imat[4][4];
220
279
        
221
280
        /* prevent crashes in these unlikely events  */
222
281
        if (ob==NULL || mat==NULL) return;
223
 
        /* optimise trick - check if need to do anything */
 
282
        /* optimize trick - check if need to do anything */
224
283
        if (from == to) return;
225
284
        
226
285
        /* are we dealing with pose-channels or objects */
231
290
                        {
232
291
                                /* world to pose */
233
292
                                invert_m4_m4(imat, ob->obmat);
234
 
                                copy_m4_m4(tempmat, mat);
235
 
                                mul_m4_m4m4(mat, tempmat, imat);
 
293
                                mult_m4_m4m4(mat, imat, mat);
236
294
                                
237
295
                                /* use pose-space as stepping stone for other spaces... */
238
296
                                if (ELEM(to, CONSTRAINT_SPACE_LOCAL, CONSTRAINT_SPACE_PARLOCAL)) {
245
303
                        {
246
304
                                /* pose to world */
247
305
                                if (to == CONSTRAINT_SPACE_WORLD) {
248
 
                                        copy_m4_m4(tempmat, mat);
249
 
                                        mul_m4_m4m4(mat, tempmat, ob->obmat);
 
306
                                        mult_m4_m4m4(mat, ob->obmat, mat);
250
307
                                }
251
308
                                /* pose to local */
252
309
                                else if (to == CONSTRAINT_SPACE_LOCAL) {
253
310
                                        if (pchan->bone) {
254
 
                                                if (pchan->parent) {
255
 
                                                        float offs_bone[4][4];
256
 
                                                                
257
 
                                                        /* construct offs_bone the same way it is done in armature.c */
258
 
                                                        copy_m4_m3(offs_bone, pchan->bone->bone_mat);
259
 
                                                        VECCOPY(offs_bone[3], pchan->bone->head);
260
 
                                                        offs_bone[3][1]+= pchan->bone->parent->length;
261
 
                                                        
262
 
                                                        if (pchan->bone->flag & BONE_HINGE) {
263
 
                                                                /* pose_mat = par_pose-space_location * chan_mat */
264
 
                                                                float tmat[4][4];
265
 
                                                                
266
 
                                                                /* the rotation of the parent restposition */
267
 
                                                                copy_m4_m4(tmat, pchan->bone->parent->arm_mat);
268
 
                                                                
269
 
                                                                /* the location of actual parent transform */
270
 
                                                                VECCOPY(tmat[3], offs_bone[3]);
271
 
                                                                offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
272
 
                                                                mul_m4_v3(pchan->parent->pose_mat, tmat[3]);
273
 
                                                                
274
 
                                                                mul_m4_m4m4(diff_mat, offs_bone, tmat);
275
 
                                                                invert_m4_m4(imat, diff_mat);
276
 
                                                        }
277
 
                                                        else {
278
 
                                                                /* pose_mat = par_pose_mat * bone_mat * chan_mat */
279
 
                                                                mul_m4_m4m4(diff_mat, offs_bone, pchan->parent->pose_mat);
280
 
                                                                invert_m4_m4(imat, diff_mat);
281
 
                                                        }
282
 
                                                }
283
 
                                                else {
284
 
                                                        /* pose_mat = chan_mat * arm_mat */
285
 
                                                        invert_m4_m4(imat, pchan->bone->arm_mat);
286
 
                                                }
287
 
                                                
288
 
                                                copy_m4_m4(tempmat, mat);
289
 
                                                mul_m4_m4m4(mat, tempmat, imat);
 
311
                                                armature_mat_pose_to_bone(pchan, mat, mat);
 
312
#if 0  /* XXX Old code, will remove it later. */
 
313
                                                constraint_pchan_diff_mat(pchan, diff_mat);
 
314
 
 
315
                                                invert_m4_m4(imat, diff_mat);
 
316
                                                mult_m4_m4m4(mat, imat, mat);
 
317
 
 
318
                                                /* override with local location */
 
319
                                                if ((pchan->parent) && (pchan->bone->flag & BONE_NO_LOCAL_LOCATION)) {
 
320
                                                        armature_mat_pose_to_bone_ex(ob, pchan, pchan->pose_mat, tempmat);
 
321
                                                        copy_v3_v3(mat[3], tempmat[3]);
 
322
                                                }
 
323
#endif
290
324
                                        }
291
325
                                }
292
326
                                /* pose to local with parent */
293
327
                                else if (to == CONSTRAINT_SPACE_PARLOCAL) {
294
328
                                        if (pchan->bone) {
295
329
                                                invert_m4_m4(imat, pchan->bone->arm_mat);
296
 
                                                copy_m4_m4(tempmat, mat);
297
 
                                                mul_m4_m4m4(mat, tempmat, imat);
 
330
                                                mult_m4_m4m4(mat, imat, mat);
298
331
                                        }
299
332
                                }
300
333
                        }
303
336
                        {
304
337
                                /* local to pose - do inverse procedure that was done for pose to local */
305
338
                                if (pchan->bone) {
306
 
                                        /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */                                         
307
 
                                        if (pchan->parent) {
308
 
                                                float offs_bone[4][4];
309
 
                                                
310
 
                                                /* construct offs_bone the same way it is done in armature.c */
311
 
                                                copy_m4_m3(offs_bone, pchan->bone->bone_mat);
312
 
                                                VECCOPY(offs_bone[3], pchan->bone->head);
313
 
                                                offs_bone[3][1]+= pchan->bone->parent->length;
314
 
                                                
315
 
                                                if (pchan->bone->flag & BONE_HINGE) {
316
 
                                                        /* pose_mat = par_pose-space_location * chan_mat */
317
 
                                                        float tmat[4][4];
318
 
                                                        
319
 
                                                        /* the rotation of the parent restposition */
320
 
                                                        copy_m4_m4(tmat, pchan->bone->parent->arm_mat);
321
 
                                                        
322
 
                                                        /* the location of actual parent transform */
323
 
                                                        VECCOPY(tmat[3], offs_bone[3]);
324
 
                                                        offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
325
 
                                                        mul_m4_v3(pchan->parent->pose_mat, tmat[3]);
326
 
                                                        
327
 
                                                        mul_m4_m4m4(diff_mat, offs_bone, tmat);
328
 
                                                        copy_m4_m4(tempmat, mat);
329
 
                                                        mul_m4_m4m4(mat, tempmat, diff_mat);
330
 
                                                }
331
 
                                                else {
332
 
                                                        /* pose_mat = par_pose_mat * bone_mat * chan_mat */
333
 
                                                        mul_m4_m4m4(diff_mat, offs_bone, pchan->parent->pose_mat);
334
 
                                                        copy_m4_m4(tempmat, mat);
335
 
                                                        mul_m4_m4m4(mat, tempmat, diff_mat);
336
 
                                                }
337
 
                                        }
338
 
                                        else {
339
 
                                                copy_m4_m4(diff_mat, pchan->bone->arm_mat);
340
 
                                                
341
 
                                                copy_m4_m4(tempmat, mat);
342
 
                                                mul_m4_m4m4(mat, tempmat, diff_mat);
343
 
                                        }
 
339
                                        /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */
 
340
                                        armature_mat_bone_to_pose(pchan, mat, mat);
 
341
#if 0
 
342
                                        constraint_pchan_diff_mat(pchan, diff_mat);
 
343
 
 
344
                                        mult_m4_m4m4(mat, diff_mat, mat);
 
345
#endif
344
346
                                }
345
347
                                
346
348
                                /* use pose-space as stepping stone for other spaces */
355
357
                                /* local + parent to pose */
356
358
                                if (pchan->bone) {                                      
357
359
                                        copy_m4_m4(diff_mat, pchan->bone->arm_mat);
358
 
                                        copy_m4_m4(tempmat, mat);
359
 
                                        mul_m4_m4m4(mat, diff_mat, tempmat);
 
360
                                        mult_m4_m4m4(mat, mat, diff_mat);
360
361
                                }
361
362
                                
362
363
                                /* use pose-space as stepping stone for other spaces */
371
372
        else {
372
373
                /* objects */
373
374
                if (from==CONSTRAINT_SPACE_WORLD && to==CONSTRAINT_SPACE_LOCAL) {
374
 
                        /* check if object has a parent - otherwise this won't work */
 
375
                        /* check if object has a parent */
375
376
                        if (ob->parent) {
376
377
                                /* 'subtract' parent's effects from owner */
377
 
                                mul_m4_m4m4(diff_mat, ob->parentinv, ob->parent->obmat);
378
 
                                invert_m4_m4(imat, diff_mat);
379
 
                                copy_m4_m4(tempmat, mat);
380
 
                                mul_m4_m4m4(mat, tempmat, imat);
 
378
                                mult_m4_m4m4(diff_mat, ob->parent->obmat, ob->parentinv);
 
379
                                invert_m4_m4(imat, diff_mat);
 
380
                                mult_m4_m4m4(mat, imat, mat);
 
381
                        }
 
382
                        else {
 
383
                                /* Local space in this case will have to be defined as local to the owner's 
 
384
                                 * transform-property-rotated axes. So subtract this rotation component.
 
385
                                 */
 
386
                                object_to_mat4(ob, diff_mat);
 
387
                                normalize_m4(diff_mat);
 
388
                                zero_v3(diff_mat[3]);
 
389
                                
 
390
                                invert_m4_m4(imat, diff_mat);
 
391
                                mult_m4_m4m4(mat, imat, mat);
381
392
                        }
382
393
                }
383
394
                else if (from==CONSTRAINT_SPACE_LOCAL && to==CONSTRAINT_SPACE_WORLD) {
384
395
                        /* check that object has a parent - otherwise this won't work */
385
396
                        if (ob->parent) {
386
397
                                /* 'add' parent's effect back to owner */
387
 
                                copy_m4_m4(tempmat, mat);
388
 
                                mul_m4_m4m4(diff_mat, ob->parentinv, ob->parent->obmat);
389
 
                                mul_m4_m4m4(mat, tempmat, diff_mat);
 
398
                                mult_m4_m4m4(diff_mat, ob->parent->obmat, ob->parentinv);
 
399
                                mult_m4_m4m4(mat, diff_mat, mat);
 
400
                        }
 
401
                        else {
 
402
                                /* Local space in this case will have to be defined as local to the owner's 
 
403
                                 * transform-property-rotated axes. So add back this rotation component.
 
404
                                 */
 
405
                                object_to_mat4(ob, diff_mat);
 
406
                                normalize_m4(diff_mat);
 
407
                                zero_v3(diff_mat[3]);
 
408
                                
 
409
                                mult_m4_m4m4(mat, diff_mat, mat);
390
410
                        }
391
411
                }
392
412
        }
395
415
/* ------------ General Target Matrix Tools ---------- */
396
416
 
397
417
/* function that sets the given matrix based on given vertex group in mesh */
398
 
static void contarget_get_mesh_mat (Scene *scene, Object *ob, char *substring, float mat[][4])
 
418
static void contarget_get_mesh_mat (Object *ob, const char *substring, float mat[][4])
399
419
{
400
420
        DerivedMesh *dm = NULL;
401
421
        Mesh *me= ob->data;
402
 
        EditMesh *em = BKE_mesh_get_editmesh(me);
403
 
        float vec[3] = {0.0f, 0.0f, 0.0f}, tvec[3];
 
422
        BMEditMesh *em = me->edit_btmesh;
 
423
        float vec[3] = {0.0f, 0.0f, 0.0f};
404
424
        float normal[3] = {0.0f, 0.0f, 0.0f}, plane[3];
405
425
        float imat[3][3], tmat[3][3];
406
 
        int dgroup;
 
426
        const int defgroup= defgroup_name_index(ob, substring);
407
427
        short freeDM = 0;
408
428
        
409
429
        /* initialize target matrix using target matrix */
410
430
        copy_m4_m4(mat, ob->obmat);
411
431
        
412
432
        /* get index of vertex group */
413
 
        dgroup = defgroup_name_index(ob, substring);
414
 
        if (dgroup < 0) return;
415
 
        
 
433
        if (defgroup == -1) return;
 
434
 
416
435
        /* get DerivedMesh */
417
436
        if (em) {
418
437
                /* target is in editmode, so get a special derived mesh */
419
 
                dm = CDDM_from_editmesh(em, ob->data);
 
438
                dm = CDDM_from_BMEditMesh(em, ob->data, FALSE, FALSE);
420
439
                freeDM= 1;
421
440
        }
422
441
        else {
423
 
                /* when not in EditMode, use the 'final' derived mesh 
424
 
                 *      - check if the custom data masks for derivedFinal mean that we can just use that
425
 
                 *        (this is more effficient + sufficient for most cases)
 
442
                /* when not in EditMode, use the 'final' derived mesh, depsgraph
 
443
                 * ensures we build with CD_MDEFORMVERT layer 
426
444
                 */
427
 
                if (!(ob->lastDataMask & CD_MASK_MDEFORMVERT)) {
428
 
                        dm = mesh_get_derived_final(scene, ob, CD_MASK_MDEFORMVERT);
429
 
                        freeDM= 1;
430
 
                }
431
 
                else 
432
 
                        dm = (DerivedMesh *)ob->derivedFinal;
 
445
                dm = (DerivedMesh *)ob->derivedFinal;
433
446
        }
434
447
        
435
448
        /* only continue if there's a valid DerivedMesh */
436
449
        if (dm) {
437
450
                MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
438
451
                int numVerts = dm->getNumVerts(dm);
439
 
                int i, j, count = 0;
 
452
                int i, count = 0;
440
453
                float co[3], nor[3];
441
454
                
442
455
                /* check that dvert is a valid pointers (just in case) */
443
456
                if (dvert) {
 
457
                        MDeformVert *dv= dvert;
444
458
                        /* get the average of all verts with that are in the vertex-group */
445
 
                        for (i = 0; i < numVerts; i++) {        
446
 
                                for (j = 0; j < dvert[i].totweight; j++) {
447
 
                                        /* does this vertex belong to nominated vertex group? */
448
 
                                        if (dvert[i].dw[j].def_nr == dgroup) {
449
 
                                                dm->getVertCo(dm, i, co);
450
 
                                                dm->getVertNo(dm, i, nor);
451
 
                                                add_v3_v3(vec, co);
452
 
                                                add_v3_v3(normal, nor);
453
 
                                                count++;
454
 
                                                break;
455
 
                                        }
 
459
                        for (i = 0; i < numVerts; i++, dv++) {
 
460
                                MDeformWeight *dw= defvert_find_index(dv, defgroup);
 
461
                                if (dw && dw->weight != 0.0f) {
 
462
                                        dm->getVertCo(dm, i, co);
 
463
                                        dm->getVertNo(dm, i, nor);
 
464
                                        add_v3_v3(vec, co);
 
465
                                        add_v3_v3(normal, nor);
 
466
                                        count++;
456
467
                                        
457
468
                                }
458
469
                        }
459
 
                        
460
 
                        
 
470
 
461
471
                        /* calculate averages of normal and coordinates */
462
472
                        if (count > 0) {
463
473
                                mul_v3_fl(vec, 1.0f / count);
477
487
                        mul_m3_v3(tmat, normal);
478
488
                        
479
489
                        normalize_v3(normal);
480
 
                        VECCOPY(plane, tmat[1]);
481
 
                        
482
 
                        VECCOPY(tmat[2], normal);
483
 
                        cross_v3_v3v3(tmat[0], normal, plane);
484
 
                        cross_v3_v3v3(tmat[1], tmat[2], tmat[0]);
485
 
                        
486
 
                        copy_m4_m3(mat, tmat);
 
490
                        copy_v3_v3(plane, tmat[1]);
 
491
                        
 
492
                        cross_v3_v3v3(mat[0], normal, plane);
 
493
                        if (len_v3(mat[0]) < 1e-3f) {
 
494
                                copy_v3_v3(plane, tmat[0]);
 
495
                                cross_v3_v3v3(mat[0], normal, plane);
 
496
                        }
 
497
 
 
498
                        copy_v3_v3(mat[2], normal);
 
499
                        cross_v3_v3v3(mat[1], mat[2], mat[0]);
 
500
 
487
501
                        normalize_m4(mat);
488
 
                        
 
502
 
489
503
                        
490
504
                        /* apply the average coordinate as the new location */
491
 
                        mul_v3_m4v3(tvec, ob->obmat, vec);
492
 
                        VECCOPY(mat[3], tvec);
 
505
                        mul_v3_m4v3(mat[3], ob->obmat, vec);
493
506
                }
494
507
        }
495
508
        
496
509
        /* free temporary DerivedMesh created (in EditMode case) */
497
510
        if (dm && freeDM)
498
511
                dm->release(dm);
499
 
        if (em)
500
 
                BKE_mesh_end_editmesh(me, em);
501
512
}
502
513
 
503
514
/* function that sets the given matrix based on given vertex group in lattice */
504
 
static void contarget_get_lattice_mat (Object *ob, char *substring, float mat[][4])
 
515
static void contarget_get_lattice_mat (Object *ob, const char *substring, float mat[][4])
505
516
{
506
517
        Lattice *lt= (Lattice *)ob->data;
507
518
        
509
520
        float *co = dl?dl->verts:NULL;
510
521
        BPoint *bp = lt->def;
511
522
        
512
 
        MDeformVert *dvert = lt->dvert;
 
523
        MDeformVert *dv = lt->dvert;
513
524
        int tot_verts= lt->pntsu*lt->pntsv*lt->pntsw;
514
525
        float vec[3]= {0.0f, 0.0f, 0.0f}, tvec[3];
515
 
        int dgroup=0, grouped=0;
 
526
        int grouped=0;
516
527
        int i, n;
 
528
        const int defgroup= defgroup_name_index(ob, substring);
517
529
        
518
530
        /* initialize target matrix using target matrix */
519
531
        copy_m4_m4(mat, ob->obmat);
520
 
        
 
532
 
521
533
        /* get index of vertex group */
522
 
        dgroup = defgroup_name_index(ob, substring);
523
 
        if (dgroup < 0) return;
524
 
        if (dvert == NULL) return;
 
534
        if (defgroup == -1) return;
 
535
        if (dv == NULL) return;
525
536
        
526
537
        /* 1. Loop through control-points checking if in nominated vertex-group.
527
538
         * 2. If it is, add it to vec to find the average point.
528
539
         */
529
 
        for (i=0; i < tot_verts; i++, dvert++) {
530
 
                for (n= 0; n < dvert->totweight; n++) {
531
 
                        /* found match - vert is in vgroup */
532
 
                        if (dvert->dw[n].def_nr == dgroup) {
 
540
        for (i=0; i < tot_verts; i++, dv++) {
 
541
                for (n= 0; n < dv->totweight; n++) {
 
542
                        MDeformWeight *dw= defvert_find_index(dv, defgroup);
 
543
                        if (dw && dw->weight > 0.0f) {
533
544
                                /* copy coordinates of point to temporary vector, then add to find average */
534
 
                                if (co)
535
 
                                        memcpy(tvec, co, 3*sizeof(float));
536
 
                                else
537
 
                                        memcpy(tvec, bp->vec, 3*sizeof(float));
538
 
                                        
 
545
                                memcpy(tvec, co ? co : bp->vec, 3 * sizeof(float));
 
546
 
539
547
                                add_v3_v3(vec, tvec);
540
548
                                grouped++;
541
 
                                
542
 
                                break;
543
549
                        }
544
550
                }
545
551
                
546
552
                /* advance pointer to coordinate data */
547
 
                if (co) co+= 3;
548
 
                else bp++;
 
553
                if (co) co += 3;
 
554
                else    bp++;
549
555
        }
550
556
        
551
557
        /* find average location, then multiply by ob->obmat to find world-space location */
554
560
        mul_v3_m4v3(tvec, ob->obmat, vec);
555
561
        
556
562
        /* copy new location to matrix */
557
 
        VECCOPY(mat[3], tvec);
 
563
        copy_v3_v3(mat[3], tvec);
558
564
}
559
565
 
560
566
/* generic function to get the appropriate matrix for most target cases */
561
567
/* The cases where the target can be object data have not been implemented */
562
 
static void constraint_target_to_mat4 (Scene *scene, Object *ob, char *substring, float mat[][4], short from, short to, float headtail)
 
568
static void constraint_target_to_mat4 (Object *ob, const char *substring, float mat[][4], short from, short to, float headtail)
563
569
{
564
570
        /*      Case OBJECT */
565
571
        if (!strlen(substring)) {
570
576
        /* Current method just takes the average location of all the points in the
571
577
         * VertexGroup, and uses that as the location value of the targets. Where 
572
578
         * possible, the orientation will also be calculated, by calculating an
573
 
         * 'average' vertex normal, and deriving the rotaation from that.
 
579
         * 'average' vertex normal, and deriving the rotation from that.
574
580
         *
575
581
         * NOTE: EditMode is not currently supported, and will most likely remain that
576
582
         *              way as constraints can only really affect things on object/bone level.
577
583
         */
578
584
        else if (ob->type == OB_MESH) {
579
 
                contarget_get_mesh_mat(scene, ob, substring, mat);
 
585
                contarget_get_mesh_mat(ob, substring, mat);
580
586
                constraint_mat_convertspace(ob, NULL, mat, from, to);
581
587
        }
582
588
        else if (ob->type == OB_LATTICE) {
593
599
                         * PoseChannel by the Armature Object's Matrix to get a worldspace
594
600
                         * matrix.
595
601
                         */
596
 
                        if (headtail < 0.000001) {
 
602
                        if (headtail < 0.000001f) {
597
603
                                /* skip length interpolation if set to head */
598
 
                                mul_m4_m4m4(mat, pchan->pose_mat, ob->obmat);
 
604
                                mult_m4_m4m4(mat, ob->obmat, pchan->pose_mat);
599
605
                        }
600
606
                        else {
601
607
                                float tempmat[4][4], loc[3];
607
613
                                copy_m4_m4(tempmat, pchan->pose_mat);   
608
614
                                copy_v3_v3(tempmat[3], loc);
609
615
                                
610
 
                                mul_m4_m4m4(mat, tempmat, ob->obmat);
 
616
                                mult_m4_m4m4(mat, ob->obmat, tempmat);
611
617
                        }
612
618
                } 
613
619
                else
656
662
/* This function should be used for the get_target_matrix member of all 
657
663
 * constraints that are not picky about what happens to their target matrix.
658
664
 */
659
 
static void default_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
665
static void default_get_tarmat (bConstraint *con, bConstraintOb *UNUSED(cob), bConstraintTarget *ct, float UNUSED(ctime))
660
666
{
661
667
        if (VALID_CONS_TARGET(ct))
662
 
                constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
 
668
                constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
663
669
        else if (ct)
664
670
                unit_m4(ct->matrix);
665
671
}
666
672
 
667
673
/* This following macro should be used for all standard single-target *_get_tars functions 
668
 
 * to save typing and reduce maintainance woes.
 
674
 * to save typing and reduce maintenance woes.
669
675
 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
670
676
 *  really just to help this code easier to read)
671
677
 */
675
681
                ct= MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
676
682
                 \
677
683
                ct->tar= datatar; \
678
 
                strcpy(ct->subtarget, datasubtarget); \
 
684
                BLI_strncpy(ct->subtarget, datasubtarget, sizeof(ct->subtarget)); \
679
685
                ct->space= con->tarspace; \
680
686
                ct->flag= CONSTRAINT_TAR_TEMP; \
681
687
                 \
685
691
                                ct->type = CONSTRAINT_OBTYPE_BONE; \
686
692
                                ct->rotOrder= (pchan) ? (pchan->rotmode) : EULER_ORDER_DEFAULT; \
687
693
                        }\
688
 
                        else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) { \
 
694
                        else if (OB_TYPE_SUPPORT_VGROUP(ct->tar->type) && (ct->subtarget[0])) { \
689
695
                                ct->type = CONSTRAINT_OBTYPE_VERT; \
690
696
                                ct->rotOrder = EULER_ORDER_DEFAULT; \
691
697
                        } \
699
705
        }
700
706
        
701
707
/* This following macro should be used for all standard single-target *_get_tars functions 
702
 
 * to save typing and reduce maintainance woes. It does not do the subtarget related operations
 
708
 * to save typing and reduce maintenance woes. It does not do the subtarget related operations
703
709
 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
704
710
 *  really just to help this code easier to read)
705
711
 */
718
724
        }
719
725
 
720
726
/* This following macro should be used for all standard single-target *_flush_tars functions
721
 
 * to save typing and reduce maintainance woes.
 
727
 * to save typing and reduce maintenance woes.
722
728
 * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
723
729
 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
724
730
 *  really just to help this code easier to read)
729
735
                        bConstraintTarget *ctn = ct->next; \
730
736
                        if (nocopy == 0) { \
731
737
                                datatar= ct->tar; \
732
 
                                strcpy(datasubtarget, ct->subtarget); \
 
738
                                BLI_strncpy(datasubtarget, ct->subtarget, sizeof(datasubtarget)); \
733
739
                                con->tarspace= (char)ct->space; \
734
740
                        } \
735
741
                         \
739
745
        }
740
746
        
741
747
/* This following macro should be used for all standard single-target *_flush_tars functions
742
 
 * to save typing and reduce maintainance woes. It does not do the subtarget related operations.
 
748
 * to save typing and reduce maintenance woes. It does not do the subtarget related operations.
743
749
 * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
744
750
 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
745
751
 *  really just to help this code easier to read)
775
781
        bChildOfConstraint *data= con->data;
776
782
        
777
783
        /* target only */
778
 
        func(con, (ID**)&data->tar, userdata);
 
784
        func(con, (ID**)&data->tar, FALSE, userdata);
779
785
}
780
786
 
781
787
static int childof_get_tars (bConstraint *con, ListBase *list)
808
814
{
809
815
        bChildOfConstraint *data= con->data;
810
816
        bConstraintTarget *ct= targets->first;
811
 
        
 
817
 
812
818
        /* only evaluate if there is a target */
813
819
        if (VALID_CONS_TARGET(ct)) {
814
 
                float parmat[4][4], invmat[4][4], tempmat[4][4];
815
 
                float loc[3], eul[3], size[3];
816
 
                float loco[3], eulo[3], sizo[3];
817
 
                
818
 
                /* get offset (parent-inverse) matrix */
819
 
                copy_m4_m4(invmat, data->invmat);
820
 
                
821
 
                /* extract components of both matrices */
822
 
                VECCOPY(loc, ct->matrix[3]);
823
 
                mat4_to_eulO(eul, ct->rotOrder, ct->matrix);
824
 
                mat4_to_size(size, ct->matrix);
825
 
                
826
 
                VECCOPY(loco, invmat[3]);
827
 
                mat4_to_eulO(eulo, cob->rotOrder, invmat);
828
 
                mat4_to_size(sizo, invmat);
829
 
                
830
 
                /* disable channels not enabled */
831
 
                if (!(data->flag & CHILDOF_LOCX)) loc[0]= loco[0]= 0.0f;
832
 
                if (!(data->flag & CHILDOF_LOCY)) loc[1]= loco[1]= 0.0f;
833
 
                if (!(data->flag & CHILDOF_LOCZ)) loc[2]= loco[2]= 0.0f;
834
 
                if (!(data->flag & CHILDOF_ROTX)) eul[0]= eulo[0]= 0.0f;
835
 
                if (!(data->flag & CHILDOF_ROTY)) eul[1]= eulo[1]= 0.0f;
836
 
                if (!(data->flag & CHILDOF_ROTZ)) eul[2]= eulo[2]= 0.0f;
837
 
                if (!(data->flag & CHILDOF_SIZEX)) size[0]= sizo[0]= 1.0f;
838
 
                if (!(data->flag & CHILDOF_SIZEY)) size[1]= sizo[1]= 1.0f;
839
 
                if (!(data->flag & CHILDOF_SIZEZ)) size[2]= sizo[2]= 1.0f;
840
 
                
841
 
                /* make new target mat and offset mat */
842
 
                loc_eulO_size_to_mat4(ct->matrix, loc, eul, size, ct->rotOrder);
843
 
                loc_eulO_size_to_mat4(invmat, loco, eulo, sizo, cob->rotOrder);
844
 
                
845
 
                /* multiply target (parent matrix) by offset (parent inverse) to get 
846
 
                 * the effect of the parent that will be exherted on the owner
847
 
                 */
848
 
                mul_m4_m4m4(parmat, invmat, ct->matrix);
849
 
                
850
 
                /* now multiply the parent matrix by the owner matrix to get the 
851
 
                 * the effect of this constraint (i.e.  owner is 'parented' to parent)
852
 
                 */
853
 
                copy_m4_m4(tempmat, cob->matrix);
854
 
                mul_m4_m4m4(cob->matrix, tempmat, parmat); 
 
820
                float parmat[4][4];
 
821
                
 
822
                /* simple matrix parenting */
 
823
                if (data->flag == CHILDOF_ALL) {
 
824
                        
 
825
                        /* multiply target (parent matrix) by offset (parent inverse) to get 
 
826
                         * the effect of the parent that will be exherted on the owner
 
827
                         */
 
828
                        mult_m4_m4m4(parmat, ct->matrix, data->invmat);
 
829
                        
 
830
                        /* now multiply the parent matrix by the owner matrix to get the 
 
831
                         * the effect of this constraint (i.e.  owner is 'parented' to parent)
 
832
                         */
 
833
                        mult_m4_m4m4(cob->matrix, parmat, cob->matrix);
 
834
                }
 
835
                else {
 
836
                        float invmat[4][4], tempmat[4][4];
 
837
                        float loc[3], eul[3], size[3];
 
838
                        float loco[3], eulo[3], sizo[3];
 
839
                        
 
840
                        /* get offset (parent-inverse) matrix */
 
841
                        copy_m4_m4(invmat, data->invmat);
 
842
                        
 
843
                        /* extract components of both matrices */
 
844
                        copy_v3_v3(loc, ct->matrix[3]);
 
845
                        mat4_to_eulO(eul, ct->rotOrder, ct->matrix);
 
846
                        mat4_to_size(size, ct->matrix);
 
847
                        
 
848
                        copy_v3_v3(loco, invmat[3]);
 
849
                        mat4_to_eulO(eulo, cob->rotOrder, invmat);
 
850
                        mat4_to_size(sizo, invmat);
 
851
                        
 
852
                        /* disable channels not enabled */
 
853
                        if (!(data->flag & CHILDOF_LOCX)) loc[0]= loco[0]= 0.0f;
 
854
                        if (!(data->flag & CHILDOF_LOCY)) loc[1]= loco[1]= 0.0f;
 
855
                        if (!(data->flag & CHILDOF_LOCZ)) loc[2]= loco[2]= 0.0f;
 
856
                        if (!(data->flag & CHILDOF_ROTX)) eul[0]= eulo[0]= 0.0f;
 
857
                        if (!(data->flag & CHILDOF_ROTY)) eul[1]= eulo[1]= 0.0f;
 
858
                        if (!(data->flag & CHILDOF_ROTZ)) eul[2]= eulo[2]= 0.0f;
 
859
                        if (!(data->flag & CHILDOF_SIZEX)) size[0]= sizo[0]= 1.0f;
 
860
                        if (!(data->flag & CHILDOF_SIZEY)) size[1]= sizo[1]= 1.0f;
 
861
                        if (!(data->flag & CHILDOF_SIZEZ)) size[2]= sizo[2]= 1.0f;
 
862
                        
 
863
                        /* make new target mat and offset mat */
 
864
                        loc_eulO_size_to_mat4(ct->matrix, loc, eul, size, ct->rotOrder);
 
865
                        loc_eulO_size_to_mat4(invmat, loco, eulo, sizo, cob->rotOrder);
 
866
                        
 
867
                        /* multiply target (parent matrix) by offset (parent inverse) to get 
 
868
                         * the effect of the parent that will be exherted on the owner
 
869
                         */
 
870
                        mult_m4_m4m4(parmat, ct->matrix, invmat);
 
871
                        
 
872
                        /* now multiply the parent matrix by the owner matrix to get the 
 
873
                         * the effect of this constraint (i.e.  owner is 'parented' to parent)
 
874
                         */
 
875
                        copy_m4_m4(tempmat, cob->matrix);
 
876
                        mult_m4_m4m4(cob->matrix, parmat, tempmat);
 
877
 
 
878
                        /* without this, changes to scale and rotation can change location
 
879
                         * of a parentless bone or a disconnected bone. Even though its set
 
880
                         * to zero above. */
 
881
                        if (!(data->flag & CHILDOF_LOCX)) cob->matrix[3][0]= tempmat[3][0];
 
882
                        if (!(data->flag & CHILDOF_LOCY)) cob->matrix[3][1]= tempmat[3][1];
 
883
                        if (!(data->flag & CHILDOF_LOCZ)) cob->matrix[3][2]= tempmat[3][2];     
 
884
                }
855
885
        }
856
886
}
857
887
 
 
888
/* XXX note, con->flag should be CONSTRAINT_SPACEONCE for bone-childof, patched in readfile.c */
858
889
static bConstraintTypeInfo CTI_CHILDOF = {
859
890
        CONSTRAINT_TYPE_CHILDOF, /* type */
860
891
        sizeof(bChildOfConstraint), /* size */
886
917
        bTrackToConstraint *data= con->data;
887
918
        
888
919
        /* target only */
889
 
        func(con, (ID**)&data->tar, userdata);
 
920
        func(con, (ID**)&data->tar, FALSE, userdata);
890
921
}
891
922
 
892
923
static int trackto_get_tars (bConstraint *con, ListBase *list)
940
971
        float right[3];
941
972
        float neg = -1;
942
973
        int right_index;
943
 
        
944
 
        copy_v3_v3(n, vec);
945
 
        if (normalize_v3(n) == 0.0) { 
946
 
                n[0] = 0.0;
947
 
                n[1] = 0.0;
948
 
                n[2] = 1.0;
 
974
 
 
975
        if (normalize_v3_v3(n, vec) == 0.0f) {
 
976
                n[0] = 0.0f;
 
977
                n[1] = 0.0f;
 
978
                n[2] = 1.0f;
949
979
        }
950
980
        if (axis > 2) axis -= 3;
951
981
        else negate_v3(n);
953
983
        /* n specifies the transformation of the track axis */
954
984
        if (flags & TARGET_Z_UP) { 
955
985
                /* target Z axis is the global up axis */
956
 
                u[0] = target_up[0];
957
 
                u[1] = target_up[1];
958
 
                u[2] = target_up[2];
 
986
                copy_v3_v3(u, target_up);
959
987
        }
960
988
        else { 
961
989
                /* world Z axis is the global up axis */
969
997
        sub_v3_v3v3(proj, u, proj); /* then onto the plane */
970
998
        /* proj specifies the transformation of the up axis */
971
999
 
972
 
        if (normalize_v3(proj) == 0.0) { /* degenerate projection */
973
 
                proj[0] = 0.0;
974
 
                proj[1] = 1.0;
975
 
                proj[2] = 0.0;
 
1000
        if (normalize_v3(proj) == 0.0f) { /* degenerate projection */
 
1001
                proj[0] = 0.0f;
 
1002
                proj[1] = 1.0f;
 
1003
                proj[2] = 0.0f;
976
1004
        }
977
1005
 
978
1006
        /* Normalized cross product of n and proj specifies transformation of the right axis */
988
1016
                m[right_index][1] = neg * right[1];
989
1017
                m[right_index][2] = neg * right[2];
990
1018
                
991
 
                m[upflag][0] = proj[0];
992
 
                m[upflag][1] = proj[1];
993
 
                m[upflag][2] = proj[2];
 
1019
                copy_v3_v3(m[upflag], proj);
994
1020
                
995
 
                m[axis][0] = n[0];
996
 
                m[axis][1] = n[1];
997
 
                m[axis][2] = n[2];
 
1021
                copy_v3_v3(m[axis], n);
998
1022
        }
999
1023
        /* identity matrix - don't do anything if the two axes are the same */
1000
1024
        else {
1001
 
                m[0][0]= m[1][1]= m[2][2]= 1.0;
1002
 
                m[0][1]= m[0][2]= 0.0;
1003
 
                m[1][0]= m[1][2]= 0.0;
1004
 
                m[2][0]= m[2][1]= 0.0;
 
1025
                unit_m3(m);
1005
1026
        }
1006
1027
}
1007
1028
 
1031
1052
                cob->matrix[2][2]=size[2];
1032
1053
                
1033
1054
                /* targetmat[2] instead of ownermat[2] is passed to vectomat
1034
 
                 * for backwards compatability it seems... (Aligorith)
 
1055
                 * for backwards compatibility it seems... (Aligorith)
1035
1056
                 */
1036
1057
                sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
1037
1058
                vectomat(vec, ct->matrix[2], 
1065
1086
{
1066
1087
        bKinematicConstraint *data= (bKinematicConstraint *)cdata;
1067
1088
        
1068
 
        data->weight= (float)1.0;
1069
 
        data->orientweight= (float)1.0;
 
1089
        data->weight= 1.0f;
 
1090
        data->orientweight= 1.0f;
1070
1091
        data->iterations = 500;
1071
 
        data->dist= (float)1.0;
 
1092
        data->dist= 1.0f;
1072
1093
        data->flag= CONSTRAINT_IK_TIP|CONSTRAINT_IK_STRETCH|CONSTRAINT_IK_POS;
1073
1094
}
1074
1095
 
1077
1098
        bKinematicConstraint *data= con->data;
1078
1099
        
1079
1100
        /* chain target */
1080
 
        func(con, (ID**)&data->tar, userdata);
 
1101
        func(con, (ID**)&data->tar, FALSE, userdata);
1081
1102
        
1082
1103
        /* poletarget */
1083
 
        func(con, (ID**)&data->poletar, userdata);
 
1104
        func(con, (ID**)&data->poletar, FALSE, userdata);
1084
1105
}
1085
1106
 
1086
1107
static int kinematic_get_tars (bConstraint *con, ListBase *list)
1111
1132
        }
1112
1133
}
1113
1134
 
1114
 
static void kinematic_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
1135
static void kinematic_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
1115
1136
{
1116
1137
        bKinematicConstraint *data= con->data;
1117
1138
        
1118
1139
        if (VALID_CONS_TARGET(ct)) 
1119
 
                constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
 
1140
                constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1120
1141
        else if (ct) {
1121
1142
                if (data->flag & CONSTRAINT_IK_AUTO) {
1122
1143
                        Object *ob= cob->ob;
1127
1148
                        else {
1128
1149
                                float vec[3];
1129
1150
                                /* move grabtarget into world space */
1130
 
                                VECCOPY(vec, data->grabtarget);
1131
 
                                mul_m4_v3(ob->obmat, vec);
 
1151
                                mul_v3_m4v3(vec, ob->obmat, data->grabtarget);
1132
1152
                                copy_m4_m4(ct->matrix, ob->obmat);
1133
 
                                VECCOPY(ct->matrix[3], vec);
 
1153
                                copy_v3_v3(ct->matrix[3], vec);
1134
1154
                        }
1135
1155
                }
1136
1156
                else
1171
1191
        bFollowPathConstraint *data= con->data;
1172
1192
        
1173
1193
        /* target only */
1174
 
        func(con, (ID**)&data->tar, userdata);
 
1194
        func(con, (ID**)&data->tar, FALSE, userdata);
1175
1195
}
1176
1196
 
1177
1197
static int followpath_get_tars (bConstraint *con, ListBase *list)
1200
1220
        }
1201
1221
}
1202
1222
 
1203
 
static void followpath_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
1223
static void followpath_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
1204
1224
{
1205
1225
        bFollowPathConstraint *data= con->data;
1206
1226
        
1207
1227
        if (VALID_CONS_TARGET(ct)) {
1208
1228
                Curve *cu= ct->tar->data;
1209
 
                float q[4], vec[4], dir[3], quat[4], radius, x1;
1210
 
                float totmat[4][4];
 
1229
                float vec[4], dir[3], radius;
 
1230
                float totmat[4][4]= MAT4_UNITY;
1211
1231
                float curvetime;
1212
 
                
1213
 
                unit_m4(totmat);
 
1232
 
1214
1233
                unit_m4(ct->matrix);
1215
 
                
 
1234
 
1216
1235
                /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
1217
1236
                 *              currently for paths to work it needs to go through the bevlist/displist system (ton) 
1218
1237
                 */
1222
1241
                        makeDispListCurveTypes(cob->scene, ct->tar, 0);
1223
1242
                
1224
1243
                if (cu->path && cu->path->data) {
1225
 
                        if ((data->followflag & FOLLOWPATH_STATIC) == 0) { 
 
1244
                        float quat[4];
 
1245
                        if ((data->followflag & FOLLOWPATH_STATIC) == 0) {
1226
1246
                                /* animated position along curve depending on time */
1227
 
                                if (cob->scene)
1228
 
                                        curvetime= bsystem_time(cob->scene, ct->tar, cu->ctime, 0.0) - data->offset;
1229
 
                                else    
1230
 
                                        curvetime= cu->ctime - data->offset;
 
1247
                                Nurb *nu = cu->nurb.first;
 
1248
                                curvetime= cu->ctime - data->offset;
1231
1249
                                
1232
1250
                                /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1233
1251
                                 * but this will only work if it actually is animated... 
1236
1254
                                 * factor, which then gets clamped to lie within 0.0 - 1.0 range
1237
1255
                                 */
1238
1256
                                curvetime /= cu->pathlen;
1239
 
                                CLAMP(curvetime, 0.0, 1.0);
 
1257
 
 
1258
                                if (nu && nu->flagu & CU_NURB_CYCLIC) {
 
1259
                                        /* If the curve is cyclic, enable looping around if the time is
 
1260
                                         * outside the bounds 0..1 */
 
1261
                                        if ((curvetime < 0.0f) || (curvetime > 1.0f)) {
 
1262
                                                curvetime -= floorf(curvetime);
 
1263
                                        }
 
1264
                                }
 
1265
                                else {
 
1266
                                        /* The curve is not cyclic, so clamp to the begin/end points. */
 
1267
                                        CLAMP(curvetime, 0.0f, 1.0f);
 
1268
                                }
1240
1269
                        }
1241
1270
                        else {
1242
1271
                                /* fixed position along curve */
1243
1272
                                curvetime= data->offset_fac;
1244
1273
                        }
1245
1274
                        
1246
 
                        if ( where_on_path(ct->tar, curvetime, vec, dir, NULL, &radius, NULL) ) {
 
1275
                        if ( where_on_path(ct->tar, curvetime, vec, dir, (data->followflag & FOLLOWPATH_FOLLOW) ? quat : NULL, &radius, NULL) ) { /* quat_pt is quat or NULL*/
1247
1276
                                if (data->followflag & FOLLOWPATH_FOLLOW) {
 
1277
#if 0
 
1278
                                        float x1, q[4];
1248
1279
                                        vec_to_quat(quat, dir, (short)data->trackflag, (short)data->upflag);
1249
1280
                                        
1250
1281
                                        normalize_v3(dir);
1254
1285
                                        q[2]= -x1*dir[1];
1255
1286
                                        q[3]= -x1*dir[2];
1256
1287
                                        mul_qt_qtqt(quat, q, quat);
1257
 
                                        
 
1288
#else
 
1289
                                        quat_apply_track(quat, data->trackflag, data->upflag);
 
1290
#endif
 
1291
 
1258
1292
                                        quat_to_mat4(totmat, quat);
1259
1293
                                }
1260
 
                                
 
1294
 
1261
1295
                                if (data->followflag & FOLLOWPATH_RADIUS) {
1262
1296
                                        float tmat[4][4], rmat[4][4];
1263
1297
                                        scale_m4_fl(tmat, radius);
1264
 
                                        mul_m4_m4m4(rmat, totmat, tmat);
 
1298
                                        mult_m4_m4m4(rmat, tmat, totmat);
1265
1299
                                        copy_m4_m4(totmat, rmat);
1266
1300
                                }
1267
1301
                                
1268
 
                                VECCOPY(totmat[3], vec);
 
1302
                                copy_v3_v3(totmat[3], vec);
1269
1303
                                
1270
1304
                                mul_serie_m4(ct->matrix, ct->tar->obmat, totmat, NULL, NULL, NULL, NULL, NULL, NULL);
1271
1305
                        }
1329
1363
/* --------- Limit Location --------- */
1330
1364
 
1331
1365
 
1332
 
static void loclimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
 
1366
static void loclimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1333
1367
{
1334
1368
        bLocLimitConstraint *data = con->data;
1335
1369
        
1377
1411
 
1378
1412
/* -------- Limit Rotation --------- */
1379
1413
 
1380
 
static void rotlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
 
1414
static void rotlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1381
1415
{
1382
1416
        bRotLimitConstraint *data = con->data;
1383
1417
        float loc[3];
1384
1418
        float eul[3];
1385
1419
        float size[3];
1386
1420
        
1387
 
        VECCOPY(loc, cob->matrix[3]);
 
1421
        copy_v3_v3(loc, cob->matrix[3]);
1388
1422
        mat4_to_size(size, cob->matrix);
1389
 
        
 
1423
 
1390
1424
        mat4_to_eulO(eul, cob->rotOrder, cob->matrix);
1391
 
        
 
1425
 
1392
1426
        /* constraint data uses radians internally */
1393
1427
        
1394
1428
        /* limiting of euler values... */
1436
1470
/* --------- Limit Scaling --------- */
1437
1471
 
1438
1472
 
1439
 
static void sizelimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
 
1473
static void sizelimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1440
1474
{
1441
1475
        bSizeLimitConstraint *data = con->data;
1442
1476
        float obsize[3], size[3];
1507
1541
        bLocateLikeConstraint *data= con->data;
1508
1542
        
1509
1543
        /* target only */
1510
 
        func(con, (ID**)&data->tar, userdata);
 
1544
        func(con, (ID**)&data->tar, FALSE, userdata);
1511
1545
}
1512
1546
 
1513
1547
static int loclike_get_tars (bConstraint *con, ListBase *list)
1545
1579
                float offset[3] = {0.0f, 0.0f, 0.0f};
1546
1580
                
1547
1581
                if (data->flag & LOCLIKE_OFFSET)
1548
 
                        VECCOPY(offset, cob->matrix[3]);
 
1582
                        copy_v3_v3(offset, cob->matrix[3]);
1549
1583
                        
1550
1584
                if (data->flag & LOCLIKE_X) {
1551
1585
                        cob->matrix[3][0] = ct->matrix[3][0];
1598
1632
        bChildOfConstraint *data= con->data;
1599
1633
        
1600
1634
        /* target only */
1601
 
        func(con, (ID**)&data->tar, userdata);
 
1635
        func(con, (ID**)&data->tar, FALSE, userdata);
1602
1636
}
1603
1637
 
1604
1638
static int rotlike_get_tars (bConstraint *con, ListBase *list)
1637
1671
                float   eul[3], obeul[3];
1638
1672
                float   size[3];
1639
1673
                
1640
 
                VECCOPY(loc, cob->matrix[3]);
 
1674
                copy_v3_v3(loc, cob->matrix[3]);
1641
1675
                mat4_to_size(size, cob->matrix);
1642
1676
                
1643
1677
                /* to allow compatible rotations, must get both rotations in the order of the owner... */
1644
 
                mat4_to_eulO(eul, cob->rotOrder, ct->matrix);
1645
1678
                mat4_to_eulO(obeul, cob->rotOrder, cob->matrix);
 
1679
                /* we must get compatible eulers from the beginning because some of them can be modified below (see bug #21875) */
 
1680
                mat4_to_compatible_eulO(eul, obeul, cob->rotOrder, ct->matrix);
1646
1681
                
1647
1682
                if ((data->flag & ROTLIKE_X)==0)
1648
1683
                        eul[0] = obeul[0];
1674
1709
                                eul[2] *= -1;
1675
1710
                }
1676
1711
                
 
1712
                /* good to make eulers compatible again, since we don't know how much they were changed above */
1677
1713
                compatible_eul(eul, obeul);
1678
1714
                loc_eulO_size_to_mat4(cob->matrix, loc, eul, size, cob->rotOrder);
1679
1715
        }
1709
1745
        bSizeLikeConstraint *data= con->data;
1710
1746
        
1711
1747
        /* target only */
1712
 
        func(con, (ID**)&data->tar, userdata);
 
1748
        func(con, (ID**)&data->tar, FALSE, userdata);
1713
1749
}
1714
1750
 
1715
1751
static int sizelike_get_tars (bConstraint *con, ListBase *list)
1799
1835
        bTransLikeConstraint *data= con->data;
1800
1836
        
1801
1837
        /* target only */
1802
 
        func(con, (ID**)&data->tar, userdata);
 
1838
        func(con, (ID**)&data->tar, FALSE, userdata);
1803
1839
}
1804
1840
 
1805
1841
static int translike_get_tars (bConstraint *con, ListBase *list)
1828
1864
        }
1829
1865
}
1830
1866
 
1831
 
static void translike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
 
1867
static void translike_evaluate (bConstraint *UNUSED(con), bConstraintOb *cob, ListBase *targets)
1832
1868
{
1833
1869
        bConstraintTarget *ct= targets->first;
1834
1870
        
1864
1900
        data->volume = 1.0f;
1865
1901
}
1866
1902
 
1867
 
static void samevolume_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
 
1903
static void samevolume_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1868
1904
{
1869
1905
        bSameVolumeConstraint *data= con->data;
1870
1906
 
1874
1910
 
1875
1911
        mat4_to_size(obsize, cob->matrix);
1876
1912
        
1877
 
        /* calculate normalising scale factor for non-essential values */
 
1913
        /* calculate normalizing scale factor for non-essential values */
1878
1914
        if (obsize[data->flag] != 0) 
1879
 
                fac = sqrt(volume / obsize[data->flag]) / obsize[data->flag];
 
1915
                fac = sqrtf(volume / obsize[data->flag]) / obsize[data->flag];
1880
1916
        
1881
1917
        /* apply scaling factor to the channels not being kept */
1882
1918
        switch (data->flag) {
1971
2007
        
1972
2008
        /* targets */
1973
2009
        for (ct= data->targets.first; ct; ct= ct->next)
1974
 
                func(con, (ID**)&ct->tar, userdata);
 
2010
                func(con, (ID**)&ct->tar, FALSE, userdata);
1975
2011
                
1976
2012
        /* script */
1977
 
        func(con, (ID**)&data->text, userdata);
 
2013
        func(con, (ID**)&data->text, TRUE, userdata);
1978
2014
}
1979
2015
 
1980
2016
/* Whether this approach is maintained remains to be seen (aligorith) */
1981
 
static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
2017
static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
1982
2018
{
1983
 
#ifndef DISABLE_PYTHON
 
2019
#ifdef WITH_PYTHON
1984
2020
        bPythonConstraint *data= con->data;
1985
2021
#endif
1986
2022
 
1997
2033
                /* firstly calculate the matrix the normal way, then let the py-function override
1998
2034
                 * this matrix if it needs to do so
1999
2035
                 */
2000
 
                constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
 
2036
                constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
2001
2037
                
2002
2038
                /* only execute target calculation if allowed */
2003
 
#ifndef DISABLE_PYTHON
 
2039
#ifdef WITH_PYTHON
2004
2040
                if (G.f & G_SCRIPT_AUTOEXEC)
2005
2041
                        BPY_pyconstraint_target(data, ct);
2006
2042
#endif
2011
2047
 
2012
2048
static void pycon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2013
2049
{
2014
 
#ifdef DISABLE_PYTHON
 
2050
#ifndef WITH_PYTHON
 
2051
        (void)con; (void)cob; (void)targets; /* unused */
2015
2052
        return;
2016
2053
#else
2017
2054
        bPythonConstraint *data= con->data;
2029
2066
#endif
2030
2067
        
2031
2068
        /* Now, run the actual 'constraint' function, which should only access the matrices */
2032
 
        BPY_pyconstraint_eval(data, cob, targets);
2033
 
#endif /* DISABLE_PYTHON */
 
2069
        BPY_pyconstraint_exec(data, cob, targets);
 
2070
#endif /* WITH_PYTHON */
2034
2071
}
2035
2072
 
2036
2073
static bConstraintTypeInfo CTI_PYTHON = {
2061
2098
{
2062
2099
        bActionConstraint *data= (bActionConstraint *)cdata;
2063
2100
        
2064
 
        /* set type to 20 (Loc X), as 0 is Rot X for backwards compatability */
 
2101
        /* set type to 20 (Loc X), as 0 is Rot X for backwards compatibility */
2065
2102
        data->type = 20;
2066
2103
}
2067
2104
 
2070
2107
        bActionConstraint *data= con->data;
2071
2108
        
2072
2109
        /* target */
2073
 
        func(con, (ID**)&data->tar, userdata);
 
2110
        func(con, (ID**)&data->tar, FALSE, userdata);
2074
2111
        
2075
2112
        /* action */
2076
 
        func(con, (ID**)&data->act, userdata);
 
2113
        func(con, (ID**)&data->act, TRUE, userdata);
2077
2114
}
2078
2115
 
2079
2116
static int actcon_get_tars (bConstraint *con, ListBase *list)
2102
2139
        }
2103
2140
}
2104
2141
 
2105
 
static void actcon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
2142
static void actcon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
2106
2143
{
2107
 
        extern void chan_calc_mat(bPoseChannel *chan);
2108
2144
        bActionConstraint *data = con->data;
2109
2145
        
2110
2146
        if (VALID_CONS_TARGET(ct)) {
2112
2148
                float s, t;
2113
2149
                short axis;
2114
2150
                
2115
 
                /* initialise return matrix */
 
2151
                /* initialize return matrix */
2116
2152
                unit_m4(ct->matrix);
2117
2153
                
2118
2154
                /* get the transform matrix of the target */
2119
 
                constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
 
2155
                constraint_target_to_mat4(ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
2120
2156
                
2121
2157
                /* determine where in transform range target is */
2122
 
                /* data->type is mapped as follows for backwards compatability:
 
2158
                /* data->type is mapped as follows for backwards compatibility:
2123
2159
                 *      00,01,02        - rotation (it used to be like this)
2124
2160
                 *      10,11,12        - scaling
2125
2161
                 *      20,21,22        - location
2127
2163
                if (data->type < 10) {
2128
2164
                        /* extract rotation (is in whatever space target should be in) */
2129
2165
                        mat4_to_eul(vec, tempmat);
2130
 
                        vec[0] *= (float)(180.0/M_PI);
2131
 
                        vec[1] *= (float)(180.0/M_PI);
2132
 
                        vec[2] *= (float)(180.0/M_PI);
 
2166
                        mul_v3_fl(vec, RAD2DEGF(1.0f)); /* rad -> deg */
2133
2167
                        axis= data->type;
2134
2168
                }
2135
2169
                else if (data->type < 20) {
2139
2173
                }
2140
2174
                else {
2141
2175
                        /* extract location */
2142
 
                        VECCOPY(vec, tempmat[3]);
 
2176
                        copy_v3_v3(vec, tempmat[3]);
2143
2177
                        axis= data->type - 20;
2144
2178
                }
2145
2179
                
2148
2182
                CLAMP(s, 0, 1);
2149
2183
                t = (s * (data->end-data->start)) + data->start;
2150
2184
                
2151
 
                if (G.f & G_DEBUG)
2152
 
                        printf("do Action Constraint %s - Ob %s Pchan %s \n", con->name, cob->ob->id.name+2, (cob->pchan)?cob->pchan->name:NULL);
 
2185
                if (G.debug & G_DEBUG)
 
2186
                        printf("do Action Constraint %s - Ob %s Pchan %s\n", con->name, cob->ob->id.name+2, (cob->pchan)?cob->pchan->name:NULL);
2153
2187
                
2154
2188
                /* Get the appropriate information from the action */
2155
2189
                if (cob->type == CONSTRAINT_OBTYPE_BONE) {
2169
2203
                        tchan->rotmode= pchan->rotmode;
2170
2204
                        
2171
2205
                        /* evaluate action using workob (it will only set the PoseChannel in question) */
2172
 
                        what_does_obaction(cob->scene, cob->ob, &workob, pose, data->act, pchan->name, t);
 
2206
                        what_does_obaction(cob->ob, &workob, pose, data->act, pchan->name, t);
2173
2207
                        
2174
2208
                        /* convert animation to matrices for use here */
2175
 
                        chan_calc_mat(tchan);
 
2209
                        pchan_calc_mat(tchan);
2176
2210
                        copy_m4_m4(ct->matrix, tchan->chan_mat);
2177
2211
                        
2178
2212
                        /* Clean up */
2183
2217
                        
2184
2218
                        /* evaluate using workob */
2185
2219
                        // FIXME: we don't have any consistent standards on limiting effects on object...
2186
 
                        what_does_obaction(cob->scene, cob->ob, &workob, NULL, data->act, NULL, t);
 
2220
                        what_does_obaction(cob->ob, &workob, NULL, data->act, NULL, t);
2187
2221
                        object_to_mat4(&workob, ct->matrix);
2188
2222
                }
2189
2223
                else {
2190
 
                        /* behaviour undefined... */
 
2224
                        /* behavior undefined... */
2191
2225
                        puts("Error: unknown owner type for Action Constraint");
2192
2226
                }
2193
2227
        }
2194
2228
}
2195
2229
 
2196
 
static void actcon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
 
2230
static void actcon_evaluate (bConstraint *UNUSED(con), bConstraintOb *cob, ListBase *targets)
2197
2231
{
2198
2232
        bConstraintTarget *ct= targets->first;
2199
2233
        
2204
2238
                 * function has already taken care of everything else.
2205
2239
                 */
2206
2240
                copy_m4_m4(temp, cob->matrix);
2207
 
                mul_m4_m4m4(cob->matrix, ct->matrix, temp);
 
2241
                mult_m4_m4m4(cob->matrix, temp, ct->matrix);
2208
2242
        }
2209
2243
}
2210
2244
 
2239
2273
        bLockTrackConstraint *data= con->data;
2240
2274
        
2241
2275
        /* target only */
2242
 
        func(con, (ID**)&data->tar, userdata);
 
2276
        func(con, (ID**)&data->tar, FALSE, userdata);
2243
2277
}
2244
2278
 
2245
2279
static int locktrack_get_tars (bConstraint *con, ListBase *list)
2283
2317
                
2284
2318
                /* Vector object -> target */
2285
2319
                sub_v3_v3v3(vec, ct->matrix[3], cob->matrix[3]);
2286
 
                switch (data->lockflag){
 
2320
                switch (data->lockflag) {
2287
2321
                case LOCK_X: /* LOCK X */
2288
2322
                {
2289
2323
                        switch (data->trackflag) {
2295
2329
                                        normalize_v3(totmat[1]);
2296
2330
                                        
2297
2331
                                        /* the x axis is fixed */
2298
 
                                        totmat[0][0] = cob->matrix[0][0];
2299
 
                                        totmat[0][1] = cob->matrix[0][1];
2300
 
                                        totmat[0][2] = cob->matrix[0][2];
2301
 
                                        normalize_v3(totmat[0]);
 
2332
                                        normalize_v3_v3(totmat[0], cob->matrix[0]);
2302
2333
                                        
2303
2334
                                        /* the z axis gets mapped onto a third orthogonal vector */
2304
2335
                                        cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2312
2343
                                        normalize_v3(totmat[2]);
2313
2344
                                        
2314
2345
                                        /* the x axis is fixed */
2315
 
                                        totmat[0][0] = cob->matrix[0][0];
2316
 
                                        totmat[0][1] = cob->matrix[0][1];
2317
 
                                        totmat[0][2] = cob->matrix[0][2];
2318
 
                                        normalize_v3(totmat[0]);
 
2346
                                        normalize_v3_v3(totmat[0], cob->matrix[0]);
2319
2347
                                        
2320
2348
                                        /* the z axis gets mapped onto a third orthogonal vector */
2321
2349
                                        cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2330
2358
                                        negate_v3(totmat[1]);
2331
2359
                                        
2332
2360
                                        /* the x axis is fixed */
2333
 
                                        totmat[0][0] = cob->matrix[0][0];
2334
 
                                        totmat[0][1] = cob->matrix[0][1];
2335
 
                                        totmat[0][2] = cob->matrix[0][2];
2336
 
                                        normalize_v3(totmat[0]);
 
2361
                                        normalize_v3_v3(totmat[0], cob->matrix[0]);
2337
2362
                                        
2338
2363
                                        /* the z axis gets mapped onto a third orthogonal vector */
2339
2364
                                        cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2348
2373
                                        negate_v3(totmat[2]);
2349
2374
                                                
2350
2375
                                        /* the x axis is fixed */
2351
 
                                        totmat[0][0] = cob->matrix[0][0];
2352
 
                                        totmat[0][1] = cob->matrix[0][1];
2353
 
                                        totmat[0][2] = cob->matrix[0][2];
2354
 
                                        normalize_v3(totmat[0]);
 
2376
                                        normalize_v3_v3(totmat[0], cob->matrix[0]);
2355
2377
                                                
2356
2378
                                        /* the z axis gets mapped onto a third orthogonal vector */
2357
2379
                                        cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2359
2381
                                        break;
2360
2382
                                default:
2361
2383
                                {
2362
 
                                        totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2363
 
                                        totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2364
 
                                        totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
 
2384
                                        unit_m3(totmat);
2365
2385
                                }
2366
2386
                                        break;
2367
2387
                        }
2378
2398
                                        normalize_v3(totmat[0]);
2379
2399
                                        
2380
2400
                                        /* the y axis is fixed */
2381
 
                                        totmat[1][0] = cob->matrix[1][0];
2382
 
                                        totmat[1][1] = cob->matrix[1][1];
2383
 
                                        totmat[1][2] = cob->matrix[1][2];
2384
 
                                        normalize_v3(totmat[1]);
2385
 
                                        
 
2401
                                        normalize_v3_v3(totmat[1], cob->matrix[1]);
 
2402
 
2386
2403
                                        /* the z axis gets mapped onto a third orthogonal vector */
2387
2404
                                        cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2388
2405
                                }
2395
2412
                                        normalize_v3(totmat[2]);
2396
2413
                                        
2397
2414
                                        /* the y axis is fixed */
2398
 
                                        totmat[1][0] = cob->matrix[1][0];
2399
 
                                        totmat[1][1] = cob->matrix[1][1];
2400
 
                                        totmat[1][2] = cob->matrix[1][2];
2401
 
                                        normalize_v3(totmat[1]);
 
2415
                                        normalize_v3_v3(totmat[1], cob->matrix[1]);
2402
2416
                                        
2403
2417
                                        /* the z axis gets mapped onto a third orthogonal vector */
2404
2418
                                        cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2413
2427
                                        negate_v3(totmat[0]);
2414
2428
                                        
2415
2429
                                        /* the y axis is fixed */
2416
 
                                        totmat[1][0] = cob->matrix[1][0];
2417
 
                                        totmat[1][1] = cob->matrix[1][1];
2418
 
                                        totmat[1][2] = cob->matrix[1][2];
2419
 
                                        normalize_v3(totmat[1]);
 
2430
                                        normalize_v3_v3(totmat[1], cob->matrix[1]);
2420
2431
                                        
2421
2432
                                        /* the z axis gets mapped onto a third orthogonal vector */
2422
2433
                                        cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2431
2442
                                        negate_v3(totmat[2]);
2432
2443
                                        
2433
2444
                                        /* the y axis is fixed */
2434
 
                                        totmat[1][0] = cob->matrix[1][0];
2435
 
                                        totmat[1][1] = cob->matrix[1][1];
2436
 
                                        totmat[1][2] = cob->matrix[1][2];
2437
 
                                        normalize_v3(totmat[1]);
 
2445
                                        normalize_v3_v3(totmat[1], cob->matrix[1]);
2438
2446
                                        
2439
2447
                                        /* the z axis gets mapped onto a third orthogonal vector */
2440
2448
                                        cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2442
2450
                                        break;
2443
2451
                                default:
2444
2452
                                {
2445
 
                                        totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2446
 
                                        totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2447
 
                                        totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
 
2453
                                        unit_m3(totmat);
2448
2454
                                }
2449
2455
                                        break;
2450
2456
                        }
2461
2467
                                        normalize_v3(totmat[0]);
2462
2468
                                        
2463
2469
                                        /* the z axis is fixed */
2464
 
                                        totmat[2][0] = cob->matrix[2][0];
2465
 
                                        totmat[2][1] = cob->matrix[2][1];
2466
 
                                        totmat[2][2] = cob->matrix[2][2];
2467
 
                                        normalize_v3(totmat[2]);
 
2470
                                        normalize_v3_v3(totmat[2], cob->matrix[2]);
2468
2471
                                        
2469
2472
                                        /* the x axis gets mapped onto a third orthogonal vector */
2470
2473
                                        cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2478
2481
                                        normalize_v3(totmat[1]);
2479
2482
                                        
2480
2483
                                        /* the z axis is fixed */
2481
 
                                        totmat[2][0] = cob->matrix[2][0];
2482
 
                                        totmat[2][1] = cob->matrix[2][1];
2483
 
                                        totmat[2][2] = cob->matrix[2][2];
2484
 
                                        normalize_v3(totmat[2]);
 
2484
                                        normalize_v3_v3(totmat[2], cob->matrix[2]);
2485
2485
                                                
2486
2486
                                        /* the x axis gets mapped onto a third orthogonal vector */
2487
2487
                                        cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2496
2496
                                        negate_v3(totmat[0]);
2497
2497
                                        
2498
2498
                                        /* the z axis is fixed */
2499
 
                                        totmat[2][0] = cob->matrix[2][0];
2500
 
                                        totmat[2][1] = cob->matrix[2][1];
2501
 
                                        totmat[2][2] = cob->matrix[2][2];
2502
 
                                        normalize_v3(totmat[2]);
 
2499
                                        normalize_v3_v3(totmat[2], cob->matrix[2]);
2503
2500
                                        
2504
2501
                                        /* the x axis gets mapped onto a third orthogonal vector */
2505
2502
                                        cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2514
2511
                                        negate_v3(totmat[1]);
2515
2512
                                        
2516
2513
                                        /* the z axis is fixed */
2517
 
                                        totmat[2][0] = cob->matrix[2][0];
2518
 
                                        totmat[2][1] = cob->matrix[2][1];
2519
 
                                        totmat[2][2] = cob->matrix[2][2];
2520
 
                                        normalize_v3(totmat[2]);
 
2514
                                        normalize_v3_v3(totmat[2], cob->matrix[2]);
2521
2515
                                                
2522
2516
                                        /* the x axis gets mapped onto a third orthogonal vector */
2523
2517
                                        cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2525
2519
                                        break;
2526
2520
                                default:
2527
2521
                                {
2528
 
                                        totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2529
 
                                        totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2530
 
                                        totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
 
2522
                                        unit_m3(totmat);
2531
2523
                                }
2532
2524
                                        break;
2533
2525
                        }
2535
2527
                        break;
2536
2528
                default:
2537
2529
                {
2538
 
                        totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2539
 
                        totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2540
 
                        totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
 
2530
                        unit_m3(totmat);
2541
2531
                }
2542
2532
                        break;
2543
2533
                }
2544
2534
                /* Block to keep matrix heading */
2545
 
                tmpmat[0][0] = cob->matrix[0][0];tmpmat[0][1] = cob->matrix[0][1];tmpmat[0][2] = cob->matrix[0][2];
2546
 
                tmpmat[1][0] = cob->matrix[1][0];tmpmat[1][1] = cob->matrix[1][1];tmpmat[1][2] = cob->matrix[1][2];
2547
 
                tmpmat[2][0] = cob->matrix[2][0];tmpmat[2][1] = cob->matrix[2][1];tmpmat[2][2] = cob->matrix[2][2];
2548
 
                normalize_v3(tmpmat[0]);
2549
 
                normalize_v3(tmpmat[1]);
2550
 
                normalize_v3(tmpmat[2]);
 
2535
                copy_m3_m4(tmpmat, cob->matrix);
 
2536
                normalize_m3(tmpmat);
2551
2537
                invert_m3_m3(invmat, tmpmat);
2552
2538
                mul_m3_m3m3(tmpmat, totmat, invmat);
2553
2539
                totmat[0][0] = tmpmat[0][0];totmat[0][1] = tmpmat[0][1];totmat[0][2] = tmpmat[0][2];
2560
2546
                                                totmat[1][0],totmat[1][1],totmat[1][2],
2561
2547
                                                totmat[2][0],totmat[2][1],totmat[2][2]);
2562
2548
                if (mdet==0) {
2563
 
                        totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2564
 
                        totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2565
 
                        totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
 
2549
                        unit_m3(totmat);
2566
2550
                }
2567
2551
                
2568
2552
                /* apply out transformaton to the object */
2600
2584
        bDistLimitConstraint *data= con->data;
2601
2585
        
2602
2586
        /* target only */
2603
 
        func(con, (ID**)&data->tar, userdata);
 
2587
        func(con, (ID**)&data->tar, FALSE, userdata);
2604
2588
}
2605
2589
 
2606
2590
static int distlimit_get_tars (bConstraint *con, ListBase *list)
2651
2635
                        /* if inside, then move to surface */
2652
2636
                        if (dist <= data->dist) {
2653
2637
                                clamp_surf= 1;
2654
 
                                sfac= data->dist / dist;
 
2638
                                if (dist != 0.0f) sfac= data->dist / dist;
2655
2639
                        }
2656
2640
                        /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
2657
2641
                        else if (data->flag & LIMITDIST_USESOFT) {
2664
2648
                        /* if outside, then move to surface */
2665
2649
                        if (dist >= data->dist) {
2666
2650
                                clamp_surf= 1;
2667
 
                                sfac= data->dist / dist;
 
2651
                                if (dist != 0.0f) sfac= data->dist / dist;
2668
2652
                        }
2669
2653
                        /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
2670
2654
                        else if (data->flag & LIMITDIST_USESOFT) {
2671
2655
                                // FIXME: there's a problem with "jumping" when this kicks in
2672
2656
                                if (dist >= (data->dist - data->soft)) {
2673
 
                                        sfac = (float)( data->soft*(1.0 - exp(-(dist - data->dist)/data->soft)) + data->dist );
2674
 
                                        sfac /= dist;
 
2657
                                        sfac = (float)( data->soft*(1.0f - expf(-(dist - data->dist)/data->soft)) + data->dist );
 
2658
                                        if (dist != 0.0f) sfac /= dist;
2675
2659
                                        
2676
2660
                                        clamp_surf= 1;
2677
2661
                                }
2678
2662
                        }
2679
2663
                }
2680
2664
                else {
2681
 
                        if (IS_EQ(dist, data->dist)==0) {
 
2665
                        if (IS_EQF(dist, data->dist)==0) {
2682
2666
                                clamp_surf= 1;
2683
 
                                sfac= data->dist / dist;
 
2667
                                if (dist != 0.0f) sfac= data->dist / dist;
2684
2668
                        }
2685
2669
                }
2686
2670
                
2690
2674
                        interp_v3_v3v3(dvec, ct->matrix[3], cob->matrix[3], sfac);
2691
2675
                        
2692
2676
                        /* copy new vector onto owner */
2693
 
                        VECCOPY(cob->matrix[3], dvec);
 
2677
                        copy_v3_v3(cob->matrix[3], dvec);
2694
2678
                }
2695
2679
        }
2696
2680
}
2728
2712
        bStretchToConstraint *data= con->data;
2729
2713
        
2730
2714
        /* target only */
2731
 
        func(con, (ID**)&data->tar, userdata);
 
2715
        func(con, (ID**)&data->tar, FALSE, userdata);
2732
2716
}
2733
2717
 
2734
2718
static int stretchto_get_tars (bConstraint *con, ListBase *list)
2773
2757
                mat4_to_size(size, cob->matrix);
2774
2758
                
2775
2759
                /* store X orientation before destroying obmat */
2776
 
                xx[0] = cob->matrix[0][0];
2777
 
                xx[1] = cob->matrix[0][1];
2778
 
                xx[2] = cob->matrix[0][2];
2779
 
                normalize_v3(xx);
 
2760
                normalize_v3_v3(xx, cob->matrix[0]);
2780
2761
                
2781
2762
                /* store Z orientation before destroying obmat */
2782
 
                zz[0] = cob->matrix[2][0];
2783
 
                zz[1] = cob->matrix[2][1];
2784
 
                zz[2] = cob->matrix[2][2];
2785
 
                normalize_v3(zz);
2786
 
                
2787
 
                sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
2788
 
                vec[0] /= size[0];
2789
 
                vec[1] /= size[1];
2790
 
                vec[2] /= size[2];
2791
 
                
2792
 
                dist = normalize_v3(vec);
2793
 
                //dist = len_v3v3( ob->obmat[3], targetmat[3]);
 
2763
                normalize_v3_v3(zz, cob->matrix[2]);
 
2764
                
 
2765
                /* XXX That makes the constraint buggy with asymmetrically scaled objects, see #29940. */
 
2766
/*              sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);*/
 
2767
/*              vec[0] /= size[0];*/
 
2768
/*              vec[1] /= size[1];*/
 
2769
/*              vec[2] /= size[2];*/
 
2770
                
 
2771
/*              dist = normalize_v3(vec);*/
 
2772
                
 
2773
                dist = len_v3v3(cob->matrix[3], ct->matrix[3]);
 
2774
                /* Only Y constrained object axis scale should be used, to keep same length when scaling it. */
 
2775
                dist /= size[1];
2794
2776
                
2795
2777
                /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
2796
 
                if (data->orglength == 0)  
 
2778
                if (data->orglength == 0)
2797
2779
                        data->orglength = dist;
2798
 
                if (data->bulge == 0) 
 
2780
                if (data->bulge == 0)
2799
2781
                        data->bulge = 1.0;
2800
2782
                
2801
2783
                scale[1] = dist/data->orglength;
2837
2819
                normalize_v3(vec);
2838
2820
                
2839
2821
                /* new Y aligns  object target connection*/
2840
 
                totmat[1][0] = -vec[0];
2841
 
                totmat[1][1] = -vec[1];
2842
 
                totmat[1][2] = -vec[2];
 
2822
                negate_v3_v3(totmat[1], vec);
2843
2823
                switch (data->plane) {
2844
2824
                case PLANE_X:
2845
2825
                        /* build new Z vector */
2848
2828
                        normalize_v3(orth);
2849
2829
                        
2850
2830
                        /* new Z*/
2851
 
                        totmat[2][0] = orth[0];
2852
 
                        totmat[2][1] = orth[1];
2853
 
                        totmat[2][2] = orth[2];
 
2831
                        copy_v3_v3(totmat[2], orth);
2854
2832
                        
2855
2833
                        /* we decided to keep X plane*/
2856
2834
                        cross_v3_v3v3(xx, orth, vec);
2857
 
                        normalize_v3(xx);
2858
 
                        totmat[0][0] = xx[0];
2859
 
                        totmat[0][1] = xx[1];
2860
 
                        totmat[0][2] = xx[2];
 
2835
                        normalize_v3_v3(totmat[0], xx);
2861
2836
                        break;
2862
2837
                case PLANE_Z:
2863
2838
                        /* build new X vector */
2866
2841
                        normalize_v3(orth);
2867
2842
                        
2868
2843
                        /* new X */
2869
 
                        totmat[0][0] = -orth[0];
2870
 
                        totmat[0][1] = -orth[1];
2871
 
                        totmat[0][2] = -orth[2];
 
2844
                        negate_v3_v3(totmat[0], orth);
2872
2845
                        
2873
2846
                        /* we decided to keep Z */
2874
2847
                        cross_v3_v3v3(zz, orth, vec);
2875
 
                        normalize_v3(zz);
2876
 
                        totmat[2][0] = zz[0];
2877
 
                        totmat[2][1] = zz[1];
2878
 
                        totmat[2][2] = zz[2];
 
2848
                        normalize_v3_v3(totmat[2], zz);
2879
2849
                        break;
2880
2850
                } /* switch (data->plane) */
2881
2851
                
2908
2878
        
2909
2879
        data->minmaxflag = TRACK_Z;
2910
2880
        data->offset = 0.0f;
2911
 
        data->cache[0] = data->cache[1] = data->cache[2] = 0.0f;
 
2881
        zero_v3(data->cache);
2912
2882
        data->flag = 0;
2913
2883
}
2914
2884
 
2917
2887
        bMinMaxConstraint *data= con->data;
2918
2888
        
2919
2889
        /* target only */
2920
 
        func(con, (ID**)&data->tar, userdata);
 
2890
        func(con, (ID**)&data->tar, FALSE, userdata);
2921
2891
}
2922
2892
 
2923
2893
static int minmax_get_tars (bConstraint *con, ListBase *list)
2963
2933
                if (data->flag & MINMAX_USEROT) {
2964
2934
                        /* take rotation of target into account by doing the transaction in target's localspace */
2965
2935
                        invert_m4_m4(imat, tarmat);
2966
 
                        mul_m4_m4m4(tmat, obmat, imat);
 
2936
                        mult_m4_m4m4(tmat, imat, obmat);
2967
2937
                        copy_m4_m4(obmat, tmat);
2968
2938
                        unit_m4(tarmat);
2969
2939
                }
3007
2977
                        obmat[3][index] = tarmat[3][index] + data->offset;
3008
2978
                        if (data->flag & MINMAX_STICKY) {
3009
2979
                                if (data->flag & MINMAX_STUCK) {
3010
 
                                        VECCOPY(obmat[3], data->cache);
 
2980
                                        copy_v3_v3(obmat[3], data->cache);
3011
2981
                                } 
3012
2982
                                else {
3013
 
                                        VECCOPY(data->cache, obmat[3]);
 
2983
                                        copy_v3_v3(data->cache, obmat[3]);
3014
2984
                                        data->flag |= MINMAX_STUCK;
3015
2985
                                }
3016
2986
                        }
3017
2987
                        if (data->flag & MINMAX_USEROT) {
3018
2988
                                /* get out of localspace */
3019
 
                                mul_m4_m4m4(tmat, obmat, ct->matrix);
 
2989
                                mult_m4_m4m4(tmat, ct->matrix, obmat);
3020
2990
                                copy_m4_m4(cob->matrix, tmat);
3021
2991
                        } 
3022
2992
                        else {                  
3023
 
                                VECCOPY(cob->matrix[3], obmat[3]);
 
2993
                                copy_v3_v3(cob->matrix[3], obmat[3]);
3024
2994
                        }
3025
2995
                } 
3026
2996
                else {
3060
3030
        bRigidBodyJointConstraint *data= con->data;
3061
3031
        
3062
3032
        /* target only */
3063
 
        func(con, (ID**)&data->tar, userdata);
 
3033
        func(con, (ID**)&data->tar, FALSE, userdata);
 
3034
        func(con, (ID**)&data->child, FALSE, userdata);
3064
3035
}
3065
3036
 
3066
3037
static int rbj_get_tars (bConstraint *con, ListBase *list)
3092
3063
static bConstraintTypeInfo CTI_RIGIDBODYJOINT = {
3093
3064
        CONSTRAINT_TYPE_RIGIDBODYJOINT, /* type */
3094
3065
        sizeof(bRigidBodyJointConstraint), /* size */
3095
 
        "RigidBody Joint", /* name */
 
3066
        "Rigid Body Joint", /* name */
3096
3067
        "bRigidBodyJointConstraint", /* struct name */
3097
3068
        NULL, /* free data */
3098
3069
        NULL, /* relink data */
3112
3083
        bClampToConstraint *data= con->data;
3113
3084
        
3114
3085
        /* target only */
3115
 
        func(con, (ID**)&data->tar, userdata);
 
3086
        func(con, (ID**)&data->tar, FALSE, userdata);
3116
3087
}
3117
3088
 
3118
3089
static int clampto_get_tars (bConstraint *con, ListBase *list)
3141
3112
        }
3142
3113
}
3143
3114
 
3144
 
static void clampto_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
3115
static void clampto_get_tarmat (bConstraint *UNUSED(con), bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
3145
3116
{
3146
3117
        if (VALID_CONS_TARGET(ct)) {
3147
3118
                Curve *cu= ct->tar->data;
3170
3141
        /* only evaluate if there is a target and it is a curve */
3171
3142
        if (VALID_CONS_TARGET(ct) && (ct->tar->type == OB_CURVE)) {
3172
3143
                Curve *cu= data->tar->data;
3173
 
                float obmat[4][4], targetMatrix[4][4], ownLoc[3];
 
3144
                float obmat[4][4], ownLoc[3];
3174
3145
                float curveMin[3], curveMax[3];
 
3146
                float targetMatrix[4][4]= MAT4_UNITY;
3175
3147
                
3176
3148
                copy_m4_m4(obmat, cob->matrix);
3177
 
                unit_m4(targetMatrix);
3178
 
                VECCOPY(ownLoc, obmat[3]);
 
3149
                copy_v3_v3(ownLoc, obmat[3]);
3179
3150
                
3180
3151
                INIT_MINMAX(curveMin, curveMax)
3181
3152
                minmax_object(ct->tar, curveMin, curveMax);
3217
3188
                                        /* find bounding-box range where target is located */
3218
3189
                                        if (ownLoc[clamp_axis] < curveMin[clamp_axis]) {
3219
3190
                                                /* bounding-box range is before */
3220
 
                                                offset= curveMin[clamp_axis];
3221
 
                                                
3222
 
                                                while (ownLoc[clamp_axis] < offset)
3223
 
                                                        offset -= len;
3224
 
                                                
 
3191
                                                offset = curveMin[clamp_axis] - ceil((curveMin[clamp_axis] - ownLoc[clamp_axis]) / len) * len;
 
3192
 
3225
3193
                                                /* now, we calculate as per normal, except using offset instead of curveMin[clamp_axis] */
3226
3194
                                                curvetime = (ownLoc[clamp_axis] - offset) / (len);
3227
3195
                                        }
3228
3196
                                        else if (ownLoc[clamp_axis] > curveMax[clamp_axis]) {
3229
3197
                                                /* bounding-box range is after */
3230
 
                                                offset= curveMax[clamp_axis];
3231
 
                                                
3232
 
                                                while (ownLoc[clamp_axis] > offset) {
3233
 
                                                        if ((offset + len) > ownLoc[clamp_axis])
3234
 
                                                                break;
3235
 
                                                        else
3236
 
                                                                offset += len;
3237
 
                                                }
3238
 
                                                
 
3198
                                                offset= curveMax[clamp_axis] + (int)((ownLoc[clamp_axis] - curveMax[clamp_axis]) / len) * len;
 
3199
 
3239
3200
                                                /* now, we calculate as per normal, except using offset instead of curveMax[clamp_axis] */
3240
3201
                                                curvetime = (ownLoc[clamp_axis] - offset) / (len);
3241
3202
                                        }
3264
3225
                        /* 3. position on curve */
3265
3226
                        if (where_on_path(ct->tar, curvetime, vec, dir, NULL, NULL, NULL) ) {
3266
3227
                                unit_m4(totmat);
3267
 
                                VECCOPY(totmat[3], vec);
 
3228
                                copy_v3_v3(totmat[3], vec);
3268
3229
                                
3269
3230
                                mul_serie_m4(targetMatrix, ct->tar->obmat, totmat, NULL, NULL, NULL, NULL, NULL, NULL);
3270
3231
                        }
3271
3232
                }
3272
3233
                
3273
3234
                /* obtain final object position */
3274
 
                VECCOPY(cob->matrix[3], targetMatrix[3]);
 
3235
                copy_v3_v3(cob->matrix[3], targetMatrix[3]);
3275
3236
        }
3276
3237
}
3277
3238
 
3307
3268
        bTransformConstraint *data= con->data;
3308
3269
        
3309
3270
        /* target only */
3310
 
        func(con, (ID**)&data->tar, userdata);
 
3271
        func(con, (ID**)&data->tar, FALSE, userdata);
3311
3272
}
3312
3273
 
3313
3274
static int transform_get_tars (bConstraint *con, ListBase *list)
3354
3315
                                break;
3355
3316
                        case 1: /* rotation (convert to degrees first) */
3356
3317
                                mat4_to_eulO(dvec, cob->rotOrder, ct->matrix);
3357
 
                                for (i=0; i<3; i++)
3358
 
                                        dvec[i] = (float)(dvec[i] / M_PI * 180);
 
3318
                                mul_v3_fl(dvec, RAD2DEGF(1.0f)); /* rad -> deg */
3359
3319
                                break;
3360
3320
                        default: /* location */
3361
3321
                                copy_v3_v3(dvec, ct->matrix[3]);
3363
3323
                }
3364
3324
                
3365
3325
                /* extract components of owner's matrix */
3366
 
                VECCOPY(loc, cob->matrix[3]);
 
3326
                copy_v3_v3(loc, cob->matrix[3]);
3367
3327
                mat4_to_eulO(eul, cob->rotOrder, cob->matrix);
3368
3328
                mat4_to_size(size, cob->matrix);        
3369
3329
                
3405
3365
                                        eul[i]= tmin + (sval[(int)data->map[i]] * (tmax - tmin)); 
3406
3366
                                        
3407
3367
                                        /* now convert final value back to radians */
3408
 
                                        eul[i] = (float)(eul[i] / 180 * M_PI);
 
3368
                                        eul[i] = DEG2RADF(eul[i]);
3409
3369
                                }
3410
3370
                                break;
3411
3371
                        default: /* location */
3443
3403
 
3444
3404
static void shrinkwrap_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
3445
3405
{
3446
 
        bShrinkwrapConstraint *data= con->data;
 
3406
        bShrinkwrapConstraint *data = con->data;
3447
3407
        
3448
3408
        /* target only */
3449
 
        func(con, (ID**)&data->target, userdata);
 
3409
        func(con, (ID**)&data->target, FALSE, userdata);
3450
3410
}
3451
3411
 
3452
3412
static int shrinkwrap_get_tars (bConstraint *con, ListBase *list)
3475
3435
}
3476
3436
 
3477
3437
 
3478
 
static void shrinkwrap_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
3438
static void shrinkwrap_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
3479
3439
{
3480
3440
        bShrinkwrapConstraint *scon = (bShrinkwrapConstraint *) con->data;
3481
3441
        
3482
 
        if( VALID_CONS_TARGET(ct) && (ct->tar->type == OB_MESH) )
 
3442
        if ( VALID_CONS_TARGET(ct) && (ct->tar->type == OB_MESH) )
3483
3443
        {
3484
3444
                int fail = FALSE;
3485
3445
                float co[3] = {0.0f, 0.0f, 0.0f};
3487
3447
                float dist;
3488
3448
                
3489
3449
                SpaceTransform transform;
3490
 
                DerivedMesh *target = object_get_derived_final(cob->scene, ct->tar, CD_MASK_BAREMESH);
 
3450
                DerivedMesh *target = object_get_derived_final(ct->tar);
3491
3451
                BVHTreeRayHit hit;
3492
3452
                BVHTreeNearest nearest;
3493
3453
                
3494
 
                BVHTreeFromMesh treeData;
3495
 
                memset(&treeData, 0, sizeof(treeData));
 
3454
                BVHTreeFromMesh treeData= {NULL};
3496
3455
                
3497
3456
                nearest.index = -1;
3498
3457
                nearest.dist = FLT_MAX;
3502
3461
                
3503
3462
                unit_m4(ct->matrix);
3504
3463
                
3505
 
                if(target != NULL)
 
3464
                if (target != NULL)
3506
3465
                {
3507
3466
                        space_transform_from_matrixs(&transform, cob->matrix, ct->tar->obmat);
3508
3467
                        
3511
3470
                                case MOD_SHRINKWRAP_NEAREST_SURFACE:
3512
3471
                                case MOD_SHRINKWRAP_NEAREST_VERTEX:
3513
3472
                                        
3514
 
                                        if(scon->shrinkType == MOD_SHRINKWRAP_NEAREST_VERTEX)
 
3473
                                        if (scon->shrinkType == MOD_SHRINKWRAP_NEAREST_VERTEX)
3515
3474
                                                bvhtree_from_mesh_verts(&treeData, target, 0.0, 2, 6);
3516
3475
                                        else
3517
3476
                                                bvhtree_from_mesh_faces(&treeData, target, 0.0, 2, 6);
3518
3477
                                        
3519
 
                                        if(treeData.tree == NULL)
 
3478
                                        if (treeData.tree == NULL)
3520
3479
                                        {
3521
3480
                                                fail = TRUE;
3522
3481
                                                break;
3527
3486
                                        BLI_bvhtree_find_nearest(treeData.tree, co, &nearest, treeData.nearest_callback, &treeData);
3528
3487
                                        
3529
3488
                                        dist = len_v3v3(co, nearest.co);
3530
 
                                        interp_v3_v3v3(co, co, nearest.co, (dist - scon->dist)/dist);   /* linear interpolation */
 
3489
                                        if (dist != 0.0f) {
 
3490
                                                interp_v3_v3v3(co, co, nearest.co, (dist - scon->dist)/dist);   /* linear interpolation */
 
3491
                                        }
3531
3492
                                        space_transform_invert(&transform, co);
3532
3493
                                break;
3533
3494
                                
3534
3495
                                case MOD_SHRINKWRAP_PROJECT:
3535
 
                                        if(scon->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_X_AXIS) no[0] = 1.0f;
3536
 
                                        if(scon->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_Y_AXIS) no[1] = 1.0f;
3537
 
                                        if(scon->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_Z_AXIS) no[2] = 1.0f;
 
3496
                                        if (scon->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_X_AXIS) no[0] = 1.0f;
 
3497
                                        if (scon->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_Y_AXIS) no[1] = 1.0f;
 
3498
                                        if (scon->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_Z_AXIS) no[2] = 1.0f;
3538
3499
                                        
3539
 
                                        if(INPR(no,no) < FLT_EPSILON)
3540
 
                                        {
 
3500
                                        if (dot_v3v3(no, no) < FLT_EPSILON) {
3541
3501
                                                fail = TRUE;
3542
3502
                                                break;
3543
3503
                                        }
3546
3506
                                        
3547
3507
                                        
3548
3508
                                        bvhtree_from_mesh_faces(&treeData, target, scon->dist, 4, 6);
3549
 
                                        if(treeData.tree == NULL)
 
3509
                                        if (treeData.tree == NULL)
3550
3510
                                        {
3551
3511
                                                fail = TRUE;
3552
3512
                                                break;
3553
3513
                                        }
3554
3514
                                        
3555
 
                                        if(normal_projection_project_vertex(0, co, no, &transform, treeData.tree, &hit, treeData.raycast_callback, &treeData) == FALSE)
 
3515
                                        if (normal_projection_project_vertex(0, co, no, &transform, treeData.tree, &hit, treeData.raycast_callback, &treeData) == FALSE)
3556
3516
                                        {
3557
3517
                                                fail = TRUE;
3558
3518
                                                break;
3559
3519
                                        }
3560
 
                                        VECCOPY(co, hit.co);
 
3520
                                        copy_v3_v3(co, hit.co);
3561
3521
                                break;
3562
3522
                        }
3563
3523
                        
3565
3525
                        
3566
3526
                        target->release(target);
3567
3527
                        
3568
 
                        if(fail == TRUE)
3569
 
                        {
 
3528
                        if (fail == TRUE) {
3570
3529
                                /* Don't move the point */
3571
 
                                co[0] = co[1] = co[2] = 0.0f;
 
3530
                                zero_v3(co);
3572
3531
                        }
3573
3532
                        
3574
3533
                        /* co is in local object coordinates, change it to global and update target position */
3575
3534
                        mul_m4_v3(cob->matrix, co);
3576
 
                        VECCOPY(ct->matrix[3], co);
 
3535
                        copy_v3_v3(ct->matrix[3], co);
3577
3536
                }
3578
3537
        }
3579
3538
}
3580
3539
 
3581
 
static void shrinkwrap_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
 
3540
static void shrinkwrap_evaluate (bConstraint *UNUSED(con), bConstraintOb *cob, ListBase *targets)
3582
3541
{
3583
3542
        bConstraintTarget *ct= targets->first;
3584
3543
        
3585
3544
        /* only evaluate if there is a target */
3586
3545
        if (VALID_CONS_TARGET(ct))
3587
3546
        {
3588
 
                VECCOPY(cob->matrix[3], ct->matrix[3]);
 
3547
                copy_v3_v3(cob->matrix[3], ct->matrix[3]);
3589
3548
        }
3590
3549
}
3591
3550
 
3619
3578
        bDampTrackConstraint *data= con->data;
3620
3579
        
3621
3580
        /* target only */
3622
 
        func(con, (ID**)&data->tar, userdata);
 
3581
        func(con, (ID**)&data->tar, FALSE, userdata);
3623
3582
}
3624
3583
 
3625
3584
static int damptrack_get_tars (bConstraint *con, ListBase *list)
3666
3625
                
3667
3626
                /* find the (unit) direction that the axis we're interested in currently points 
3668
3627
                 *      - mul_mat3_m4_v3() only takes the 3x3 (rotation+scaling) components of the 4x4 matrix 
3669
 
                 *      - the normalisation step at the end should take care of any unwanted scaling
 
3628
                 *      - the normalization step at the end should take care of any unwanted scaling
3670
3629
                 *        left over in the 3x3 matrix we used
3671
3630
                 */
3672
 
                VECCOPY(obvec, track_dir_vecs[data->trackflag]);
 
3631
                copy_v3_v3(obvec, track_dir_vecs[data->trackflag]);
3673
3632
                mul_mat3_m4_v3(cob->matrix, obvec);
3674
3633
                
3675
3634
                if (normalize_v3(obvec) == 0.0f) {
3676
3635
                        /* exceptional case - just use the track vector as appropriate */
3677
 
                        VECCOPY(obvec, track_dir_vecs[data->trackflag]);
 
3636
                        copy_v3_v3(obvec, track_dir_vecs[data->trackflag]);
3678
3637
                }
3679
3638
                
3680
3639
                /* find the (unit) direction vector going from the owner to the target */
3681
 
                VECCOPY(obloc, cob->matrix[3]);
 
3640
                copy_v3_v3(obloc, cob->matrix[3]);
3682
3641
                sub_v3_v3v3(tarvec, ct->matrix[3], obloc);
3683
3642
                
3684
3643
                if (normalize_v3(tarvec) == 0.0f) {
3685
3644
                        /* the target is sitting on the owner, so just make them use the same direction vectors */
3686
3645
                        // FIXME: or would it be better to use the pure direction vector?
3687
 
                        VECCOPY(tarvec, obvec);
3688
 
                        //VECCOPY(tarvec, track_dir_vecs[data->trackflag]);
 
3646
                        copy_v3_v3(tarvec, obvec);
 
3647
                        //copy_v3_v3(tarvec, track_dir_vecs[data->trackflag]);
3689
3648
                }
3690
3649
                
3691
3650
                /* determine the axis-angle rotation, which represents the smallest possible rotation
3713
3672
                mul_m4_m3m4(tmat, rmat, cob->matrix); // m1, m3, m2
3714
3673
                
3715
3674
                copy_m4_m4(cob->matrix, tmat);
3716
 
                VECCOPY(cob->matrix[3], obloc);
 
3675
                copy_v3_v3(cob->matrix[3], obloc);
3717
3676
        }
3718
3677
}
3719
3678
 
3765
3724
        bSplineIKConstraint *data= con->data;
3766
3725
        
3767
3726
        /* target only */
3768
 
        func(con, (ID**)&data->tar, userdata);
 
3727
        func(con, (ID**)&data->tar, FALSE, userdata);
3769
3728
}
3770
3729
 
3771
3730
static int splineik_get_tars (bConstraint *con, ListBase *list)
3794
3753
        }
3795
3754
}
3796
3755
 
3797
 
static void splineik_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
 
3756
static void splineik_get_tarmat (bConstraint *UNUSED(con), bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
3798
3757
{
3799
3758
        if (VALID_CONS_TARGET(ct)) {
3800
3759
                Curve *cu= ct->tar->data;
3838
3797
        bPivotConstraint *data= con->data;
3839
3798
        
3840
3799
        /* target only */
3841
 
        func(con, (ID**)&data->tar, userdata);
 
3800
        func(con, (ID**)&data->tar, FALSE, userdata);
3842
3801
}
3843
3802
 
3844
3803
static int pivotcon_get_tars (bConstraint *con, ListBase *list)
3874
3833
        
3875
3834
        float pivot[3], vec[3];
3876
3835
        float rotMat[3][3];
 
3836
 
 
3837
        /* pivot correction */
 
3838
        float axis[3], angle;
3877
3839
        
3878
3840
        /* firstly, check if pivoting should take place based on the current rotation */
3879
3841
        if (data->rotAxis != PIVOTCON_AXIS_NONE) {
3908
3870
                }
3909
3871
                else {
3910
3872
                        /* directly use the 'offset' specified as an absolute position instead */
3911
 
                        VECCOPY(pivot, data->offset);
 
3873
                        copy_v3_v3(pivot, data->offset);
3912
3874
                }
3913
3875
        }
3914
3876
        
3916
3878
        // TODO: perhaps we might want to include scaling based on the pivot too?
3917
3879
        copy_m3_m4(rotMat, cob->matrix);
3918
3880
        normalize_m3(rotMat);
3919
 
        
 
3881
 
 
3882
 
 
3883
        /* correct the pivot by the rotation axis otherwise the pivot translates when it shouldnt */
 
3884
        mat3_to_axis_angle(axis, &angle, rotMat);
 
3885
        if (angle) {
 
3886
                float dvec[3];
 
3887
                sub_v3_v3v3(vec, pivot, cob->matrix[3]);
 
3888
                project_v3_v3v3(dvec, vec, axis);
 
3889
                sub_v3_v3(pivot, dvec);
 
3890
        }
 
3891
 
3920
3892
        /* perform the pivoting... */
3921
3893
                /* 1. take the vector from owner to the pivot */
3922
 
        sub_v3_v3v3(vec, pivot, cob->matrix[3]);
 
3894
        sub_v3_v3v3(vec, cob->matrix[3], pivot);
3923
3895
                /* 2. rotate this vector by the rotation of the object... */
3924
3896
        mul_m3_v3(rotMat, vec);
3925
3897
                /* 3. make the rotation in terms of the pivot now */
3936
3908
        NULL, /* relink data */
3937
3909
        pivotcon_id_looper, /* id looper */
3938
3910
        NULL, /* copy data */
3939
 
        NULL, /* new data */ // XXX: might be needed to get 'normal' pivot behaviour...
 
3911
        NULL, /* new data */ // XXX: might be needed to get 'normal' pivot behavior...
3940
3912
        pivotcon_get_tars, /* get constraint targets */
3941
3913
        pivotcon_flush_tars, /* flush constraint targets */
3942
3914
        default_get_tarmat, /* get target matrix */
3943
3915
        pivotcon_evaluate /* evaluate */
3944
3916
};
3945
3917
 
 
3918
/* ----------- Follow Track ------------- */
 
3919
 
 
3920
static void followtrack_new_data(void *cdata)
 
3921
{
 
3922
        bFollowTrackConstraint *data = (bFollowTrackConstraint *)cdata;
 
3923
 
 
3924
        data->clip = NULL;
 
3925
        data->flag |= FOLLOWTRACK_ACTIVECLIP;
 
3926
}
 
3927
 
 
3928
static void followtrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
 
3929
{
 
3930
        bFollowTrackConstraint *data = con->data;
 
3931
 
 
3932
        func(con, (ID**)&data->clip, TRUE, userdata);
 
3933
        func(con, (ID**)&data->camera, FALSE, userdata);
 
3934
        func(con, (ID**)&data->depth_ob, FALSE, userdata);
 
3935
}
 
3936
 
 
3937
static void followtrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
 
3938
{
 
3939
        Scene *scene = cob->scene;
 
3940
        bFollowTrackConstraint *data = con->data;
 
3941
        MovieClip *clip = data->clip;
 
3942
        MovieTracking *tracking;
 
3943
        MovieTrackingTrack *track;
 
3944
        MovieTrackingObject *tracking_object;
 
3945
        Object *camob= data->camera ? data->camera : scene->camera;
 
3946
 
 
3947
        if (data->flag & FOLLOWTRACK_ACTIVECLIP)
 
3948
                clip= scene->clip;
 
3949
 
 
3950
        if (!clip || !data->track[0] || !camob)
 
3951
                return;
 
3952
 
 
3953
        tracking = &clip->tracking;
 
3954
 
 
3955
        if (data->object[0])
 
3956
                tracking_object = BKE_tracking_named_object(tracking, data->object);
 
3957
        else
 
3958
                tracking_object = BKE_tracking_get_camera_object(tracking);
 
3959
 
 
3960
        if (!tracking_object)
 
3961
                return;
 
3962
 
 
3963
        track = BKE_tracking_named_track(tracking, tracking_object, data->track);
 
3964
 
 
3965
        if (!track)
 
3966
                return;
 
3967
 
 
3968
        if (data->flag & FOLLOWTRACK_USE_3D_POSITION) {
 
3969
                if (track->flag & TRACK_HAS_BUNDLE) {
 
3970
                        float obmat[4][4], mat[4][4];
 
3971
 
 
3972
                        copy_m4_m4(obmat, cob->matrix);
 
3973
 
 
3974
                        if ((tracking_object->flag & TRACKING_OBJECT_CAMERA)==0) {
 
3975
                                float imat[4][4];
 
3976
 
 
3977
                                copy_m4_m4(mat, camob->obmat);
 
3978
 
 
3979
                                BKE_tracking_get_interpolated_camera(tracking, tracking_object, scene->r.cfra, imat);
 
3980
                                invert_m4(imat);
 
3981
 
 
3982
                                mul_serie_m4(cob->matrix, obmat, mat, imat, NULL, NULL, NULL, NULL, NULL);
 
3983
                                translate_m4(cob->matrix, track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
 
3984
                        }
 
3985
                        else {
 
3986
                                BKE_get_tracking_mat(cob->scene, camob, mat);
 
3987
 
 
3988
                                mult_m4_m4m4(cob->matrix, obmat, mat);
 
3989
                                translate_m4(cob->matrix, track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
 
3990
                        }
 
3991
                }
 
3992
        }
 
3993
        else {
 
3994
                MovieTrackingMarker *marker;
 
3995
                float vec[3], disp[3], axis[3], mat[4][4];
 
3996
                float aspect= (scene->r.xsch * scene->r.xasp) / (scene->r.ysch * scene->r.yasp);
 
3997
                float len, d;
 
3998
 
 
3999
                where_is_object_mat(scene, camob, mat);
 
4000
 
 
4001
                /* camera axis */
 
4002
                vec[0] = 0.0f;
 
4003
                vec[1] = 0.0f;
 
4004
                vec[2] = 1.0f;
 
4005
                mul_v3_m4v3(axis, mat, vec);
 
4006
 
 
4007
                /* distance to projection plane */
 
4008
                copy_v3_v3(vec, cob->matrix[3]);
 
4009
                sub_v3_v3(vec, mat[3]);
 
4010
                project_v3_v3v3(disp, vec, axis);
 
4011
 
 
4012
                len = len_v3(disp);
 
4013
 
 
4014
                if (len > FLT_EPSILON) {
 
4015
                        CameraParams params;
 
4016
                        float pos[2], rmat[4][4];
 
4017
 
 
4018
                        marker = BKE_tracking_get_marker(track, scene->r.cfra);
 
4019
 
 
4020
                        add_v2_v2v2(pos, marker->pos, track->offset);
 
4021
 
 
4022
                        camera_params_init(&params);
 
4023
                        camera_params_from_object(&params, camob);
 
4024
 
 
4025
                        if (params.is_ortho) {
 
4026
                                vec[0] = params.ortho_scale * (pos[0] - 0.5f + params.shiftx);
 
4027
                                vec[1] = params.ortho_scale * (pos[1] - 0.5f + params.shifty);
 
4028
                                vec[2] = -len;
 
4029
 
 
4030
                                if (aspect > 1.0f)
 
4031
                                        vec[1] /= aspect;
 
4032
                                else
 
4033
                                        vec[0] *= aspect;
 
4034
 
 
4035
                                mul_v3_m4v3(disp, camob->obmat, vec);
 
4036
 
 
4037
                                copy_m4_m4(rmat, camob->obmat);
 
4038
                                zero_v3(rmat[3]);
 
4039
                                mult_m4_m4m4(cob->matrix, cob->matrix, rmat);
 
4040
 
 
4041
                                copy_v3_v3(cob->matrix[3], disp);
 
4042
                        }
 
4043
                        else {
 
4044
                                d=  (len * params.sensor_x) / (2.0f * params.lens);
 
4045
 
 
4046
                                vec[0] = d * (2.0f * (pos[0] + params.shiftx) - 1.0f);
 
4047
                                vec[1] = d * (2.0f * (pos[1] + params.shifty) - 1.0f);
 
4048
                                vec[2] = -len;
 
4049
 
 
4050
                                if (aspect > 1.0f)
 
4051
                                        vec[1] /= aspect;
 
4052
                                else
 
4053
                                        vec[0] *= aspect;
 
4054
 
 
4055
                                mul_v3_m4v3(disp, camob->obmat, vec);
 
4056
 
 
4057
                                /* apply camera rotation so Z-axis would be co-linear */
 
4058
                                copy_m4_m4(rmat, camob->obmat);
 
4059
                                zero_v3(rmat[3]);
 
4060
                                mult_m4_m4m4(cob->matrix, cob->matrix, rmat);
 
4061
 
 
4062
                                copy_v3_v3(cob->matrix[3], disp);
 
4063
                        }
 
4064
 
 
4065
                        if (data->depth_ob && data->depth_ob->derivedFinal) {
 
4066
                                Object *depth_ob = data->depth_ob;
 
4067
                                BVHTreeFromMesh treeData = NULL_BVHTreeFromMesh;
 
4068
                                BVHTreeRayHit hit;
 
4069
                                float ray_start[3], ray_end[3], ray_nor[3], imat[4][4];
 
4070
                                int result;
 
4071
 
 
4072
                                invert_m4_m4(imat, depth_ob->obmat);
 
4073
 
 
4074
                                mul_v3_m4v3(ray_start, imat, camob->obmat[3]);
 
4075
                                mul_v3_m4v3(ray_end, imat, cob->matrix[3]);
 
4076
 
 
4077
                                sub_v3_v3v3(ray_nor, ray_end, ray_start);
 
4078
 
 
4079
                                bvhtree_from_mesh_faces(&treeData, depth_ob->derivedFinal, 0.0f, 4, 6);
 
4080
 
 
4081
                                hit.dist = FLT_MAX;
 
4082
                                hit.index = -1;
 
4083
 
 
4084
                                result = BLI_bvhtree_ray_cast(treeData.tree, ray_start, ray_nor, 0.0f, &hit, treeData.raycast_callback, &treeData);
 
4085
 
 
4086
                                if (result != -1) {
 
4087
                                        mul_v3_m4v3(cob->matrix[3], depth_ob->obmat, hit.co);
 
4088
                                }
 
4089
 
 
4090
                                free_bvhtree_from_mesh(&treeData);
 
4091
                        }
 
4092
                }
 
4093
        }
 
4094
}
 
4095
 
 
4096
static bConstraintTypeInfo CTI_FOLLOWTRACK = {
 
4097
        CONSTRAINT_TYPE_FOLLOWTRACK, /* type */
 
4098
        sizeof(bFollowTrackConstraint), /* size */
 
4099
        "Follow Track", /* name */
 
4100
        "bFollowTrackConstraint", /* struct name */
 
4101
        NULL, /* free data */
 
4102
        NULL, /* relink data */
 
4103
        followtrack_id_looper, /* id looper */
 
4104
        NULL, /* copy data */
 
4105
        followtrack_new_data, /* new data */
 
4106
        NULL, /* get constraint targets */
 
4107
        NULL, /* flush constraint targets */
 
4108
        NULL, /* get target matrix */
 
4109
        followtrack_evaluate /* evaluate */
 
4110
};
 
4111
 
 
4112
/* ----------- Camre Solver ------------- */
 
4113
 
 
4114
static void camerasolver_new_data(void *cdata)
 
4115
{
 
4116
        bCameraSolverConstraint *data = (bCameraSolverConstraint *)cdata;
 
4117
 
 
4118
        data->clip = NULL;
 
4119
        data->flag |= CAMERASOLVER_ACTIVECLIP;
 
4120
}
 
4121
 
 
4122
static void camerasolver_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
 
4123
{
 
4124
        bCameraSolverConstraint *data = con->data;
 
4125
 
 
4126
        func(con, (ID**)&data->clip, TRUE, userdata);
 
4127
}
 
4128
 
 
4129
static void camerasolver_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
 
4130
{
 
4131
        Scene *scene = cob->scene;
 
4132
        bCameraSolverConstraint *data = con->data;
 
4133
        MovieClip *clip = data->clip;
 
4134
 
 
4135
        if (data->flag & CAMERASOLVER_ACTIVECLIP)
 
4136
                clip = scene->clip;
 
4137
 
 
4138
        if (clip) {
 
4139
                float mat[4][4], obmat[4][4];
 
4140
                MovieTracking *tracking = &clip->tracking;
 
4141
                MovieTrackingObject *object = BKE_tracking_get_camera_object(tracking);
 
4142
 
 
4143
                BKE_tracking_get_interpolated_camera(tracking, object, scene->r.cfra, mat);
 
4144
 
 
4145
                copy_m4_m4(obmat, cob->matrix);
 
4146
 
 
4147
                mult_m4_m4m4(cob->matrix, obmat, mat);
 
4148
        }
 
4149
}
 
4150
 
 
4151
static bConstraintTypeInfo CTI_CAMERASOLVER = {
 
4152
        CONSTRAINT_TYPE_CAMERASOLVER, /* type */
 
4153
        sizeof(bCameraSolverConstraint), /* size */
 
4154
        "Camera Solver", /* name */
 
4155
        "bCameraSolverConstraint", /* struct name */
 
4156
        NULL, /* free data */
 
4157
        NULL, /* relink data */
 
4158
        camerasolver_id_looper, /* id looper */
 
4159
        NULL, /* copy data */
 
4160
        camerasolver_new_data, /* new data */
 
4161
        NULL, /* get constraint targets */
 
4162
        NULL, /* flush constraint targets */
 
4163
        NULL, /* get target matrix */
 
4164
        camerasolver_evaluate /* evaluate */
 
4165
};
 
4166
 
 
4167
/* ----------- Object Solver ------------- */
 
4168
 
 
4169
static void objectsolver_new_data(void *cdata)
 
4170
{
 
4171
        bObjectSolverConstraint *data = (bObjectSolverConstraint *)cdata;
 
4172
 
 
4173
        data->clip = NULL;
 
4174
        data->flag |= OBJECTSOLVER_ACTIVECLIP;
 
4175
        unit_m4(data->invmat);
 
4176
}
 
4177
 
 
4178
static void objectsolver_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
 
4179
{
 
4180
        bObjectSolverConstraint *data= con->data;
 
4181
 
 
4182
        func(con, (ID**)&data->clip, FALSE, userdata);
 
4183
        func(con, (ID**)&data->camera, FALSE, userdata);
 
4184
}
 
4185
 
 
4186
static void objectsolver_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
 
4187
{
 
4188
        Scene *scene = cob->scene;
 
4189
        bObjectSolverConstraint *data = con->data;
 
4190
        MovieClip *clip = data->clip;
 
4191
        Object *camob = data->camera ? data->camera : scene->camera;
 
4192
 
 
4193
        if (data->flag & OBJECTSOLVER_ACTIVECLIP)
 
4194
                clip= scene->clip;
 
4195
 
 
4196
        if (!camob || !clip)
 
4197
                return;
 
4198
 
 
4199
        if (clip) {
 
4200
                MovieTracking *tracking = &clip->tracking;
 
4201
                MovieTrackingObject *object;
 
4202
 
 
4203
                object = BKE_tracking_named_object(tracking, data->object);
 
4204
 
 
4205
                if (object) {
 
4206
                        float mat[4][4], obmat[4][4], imat[4][4], cammat[4][4], camimat[4][4], parmat[4][4];
 
4207
 
 
4208
                        where_is_object_mat(scene, camob, cammat);
 
4209
 
 
4210
                        BKE_tracking_get_interpolated_camera(tracking, object, scene->r.cfra, mat);
 
4211
 
 
4212
                        invert_m4_m4(camimat, cammat);
 
4213
                        mult_m4_m4m4(parmat, cammat, data->invmat);
 
4214
 
 
4215
                        copy_m4_m4(cammat, camob->obmat);
 
4216
                        copy_m4_m4(obmat, cob->matrix);
 
4217
 
 
4218
                        invert_m4_m4(imat, mat);
 
4219
 
 
4220
                        mul_serie_m4(cob->matrix, cammat, imat, camimat, parmat, obmat, NULL, NULL, NULL);
 
4221
                }
 
4222
        }
 
4223
}
 
4224
 
 
4225
static bConstraintTypeInfo CTI_OBJECTSOLVER = {
 
4226
        CONSTRAINT_TYPE_OBJECTSOLVER, /* type */
 
4227
        sizeof(bObjectSolverConstraint), /* size */
 
4228
        "Object Solver", /* name */
 
4229
        "bObjectSolverConstraint", /* struct name */
 
4230
        NULL, /* free data */
 
4231
        NULL, /* relink data */
 
4232
        objectsolver_id_looper, /* id looper */
 
4233
        NULL, /* copy data */
 
4234
        objectsolver_new_data, /* new data */
 
4235
        NULL, /* get constraint targets */
 
4236
        NULL, /* flush constraint targets */
 
4237
        NULL, /* get target matrix */
 
4238
        objectsolver_evaluate /* evaluate */
 
4239
};
 
4240
 
3946
4241
/* ************************* Constraints Type-Info *************************** */
3947
4242
/* All of the constraints api functions use bConstraintTypeInfo structs to carry out
3948
4243
 * and operations that involve constraint specific code.
3953
4248
static short CTI_INIT= 1; /* when non-zero, the list needs to be updated */
3954
4249
 
3955
4250
/* This function only gets called when CTI_INIT is non-zero */
3956
 
static void constraints_init_typeinfo () {
 
4251
static void constraints_init_typeinfo (void)
 
4252
{
3957
4253
        constraintsTypeInfo[0]=  NULL;                                  /* 'Null' Constraint */
3958
4254
        constraintsTypeInfo[1]=  &CTI_CHILDOF;                  /* ChildOf Constraint */
3959
4255
        constraintsTypeInfo[2]=  &CTI_TRACKTO;                  /* TrackTo Constraint */
3980
4276
        constraintsTypeInfo[23]= &CTI_TRANSLIKE;                /* Copy Transforms Constraint */
3981
4277
        constraintsTypeInfo[24]= &CTI_SAMEVOL;                  /* Maintain Volume Constraint */
3982
4278
        constraintsTypeInfo[25]= &CTI_PIVOT;                    /* Pivot Constraint */
 
4279
        constraintsTypeInfo[26]= &CTI_FOLLOWTRACK;              /* Follow Track Constraint */
 
4280
        constraintsTypeInfo[27]= &CTI_CAMERASOLVER;             /* Camera Solver Constraint */
 
4281
        constraintsTypeInfo[28]= &CTI_OBJECTSOLVER;             /* Object Solver Constraint */
3983
4282
}
3984
4283
 
3985
4284
/* This function should be used for getting the appropriate type-info when only
3987
4286
 */
3988
4287
bConstraintTypeInfo *get_constraint_typeinfo (int type)
3989
4288
{
3990
 
        /* initialise the type-info list? */
 
4289
        /* initialize the type-info list? */
3991
4290
        if (CTI_INIT) {
3992
4291
                constraints_init_typeinfo();
3993
4292
                CTI_INIT = 0;
4001
4300
                return constraintsTypeInfo[type];
4002
4301
        }
4003
4302
        else {
4004
 
                printf("No valid constraint type-info data available. Type = %i \n", type);
 
4303
                printf("No valid constraint type-info data available. Type = %i\n", type);
4005
4304
        }
4006
4305
        
4007
4306
        return NULL;
4026
4325
 
4027
4326
/* ---------- Data Management ------- */
4028
4327
 
4029
 
/* Free data of a specific constraint if it has any info */
 
4328
/* Free data of a specific constraint if it has any info.
 
4329
 * be sure to run BIK_clear_data() when freeing an IK constraint,
 
4330
 * unless DAG_scene_sort is called. */
4030
4331
void free_constraint_data (bConstraint *con)
4031
4332
{
4032
4333
        if (con->data) {
4067
4368
                return 0;
4068
4369
}
4069
4370
 
4070
 
/* Remove the nth constraint from the given constraint stack */
4071
 
int remove_constraint_index (ListBase *list, int index)
4072
 
{
4073
 
        bConstraint *con= BLI_findlink(list, index);
4074
 
        
4075
 
        if (con)
4076
 
                return remove_constraint(list, con);
4077
 
        else 
4078
 
                return 0;
4079
 
}
4080
 
 
4081
4371
/* Remove all the constraints of the specified type from the given constraint stack */
4082
4372
void remove_constraints_type (ListBase *list, short type, short last_only)
4083
4373
{
4100
4390
 
4101
4391
/* ......... */
4102
4392
 
4103
 
/* Creates a new constraint, initialises its data, and returns it */
 
4393
/* Creates a new constraint, initializes its data, and returns it */
4104
4394
static bConstraint *add_new_constraint_internal (const char *name, short type)
4105
4395
{
4106
4396
        bConstraint *con= MEM_callocN(sizeof(bConstraint), "Constraint");
4114
4404
 
4115
4405
        /* Determine a basic name, and info */
4116
4406
        if (cti) {
4117
 
                /* initialise constraint data */
 
4407
                /* initialize constraint data */
4118
4408
                con->data = MEM_callocN(cti->size, cti->structName);
4119
4409
                
4120
4410
                /* only constraints that change any settings need this */
4167
4457
                constraints_set_active(list, con);
4168
4458
        }
4169
4459
        
 
4460
        /* set type+owner specific immutable settings */
 
4461
        // TODO: does action constraint need anything here - i.e. spaceonce?
 
4462
        switch (type) {
 
4463
                case CONSTRAINT_TYPE_CHILDOF:
 
4464
                {
 
4465
                        /* if this constraint is being added to a posechannel, make sure
 
4466
                         * the constraint gets evaluated in pose-space */
 
4467
                        if (pchan) {
 
4468
                                con->ownspace = CONSTRAINT_SPACE_POSE;
 
4469
                                con->flag |= CONSTRAINT_SPACEONCE;
 
4470
                        }
 
4471
                }
 
4472
                        break;
 
4473
        }
 
4474
        
4170
4475
        return con;
4171
4476
}
4172
4477
 
4237
4542
/* ......... */
4238
4543
 
4239
4544
/* helper for copy_constraints(), to be used for making sure that ID's are valid */
4240
 
static void con_extern_cb(bConstraint *con, ID **idpoin, void *userdata)
 
4545
static void con_extern_cb(bConstraint *UNUSED(con), ID **idpoin, short UNUSED(isReference), void *UNUSED(userData))
4241
4546
{
4242
4547
        if (*idpoin && (*idpoin)->lib)
4243
4548
                id_lib_extern(*idpoin);
4244
4549
}
4245
4550
 
 
4551
/* helper for copy_constraints(), to be used for making sure that usercounts of copied ID's are fixed up */
 
4552
static void con_fix_copied_refs_cb(bConstraint *con, ID **idpoin, short isReference, void *UNUSED(userData))
 
4553
{
 
4554
        /* increment usercount if this is a reference type */
 
4555
        if ((*idpoin) && (isReference))
 
4556
                id_us_plus(*idpoin);
 
4557
}
 
4558
 
4246
4559
/* duplicate all of the constraints in a constraint stack */
4247
4560
void copy_constraints (ListBase *dst, const ListBase *src, int do_extern)
4248
4561
{
4262
4575
                        /* perform custom copying operations if needed */
4263
4576
                        if (cti->copy_data)
4264
4577
                                cti->copy_data(con, srccon);
 
4578
                                
 
4579
                        /* fix usercounts for all referenced data in referenced data */
 
4580
                        if (cti->id_looper)
 
4581
                                cti->id_looper(con, con_fix_copied_refs_cb, NULL);
4265
4582
                        
4266
 
                        /* for proxies we dont want to make extern */
 
4583
                        /* for proxies we don't want to make extern */
4267
4584
                        if (do_extern) {
4268
4585
                                /* go over used ID-links for this constraint to ensure that they are valid for proxies */
4269
4586
                                if (cti->id_looper)
4358
4675
 * constraints either had one or no targets. It used to be called during the main constraint solving
4359
4676
 * loop, but is now only used for the remaining cases for a few constraints. 
4360
4677
 *
4361
 
 * None of the actual calculations of the matricies should be done here! Also, this function is 
 
4678
 * None of the actual calculations of the matrices should be done here! Also, this function is
4362
4679
 * not to be used by any new constraints, particularly any that have multiple targets.
4363
4680
 */
4364
4681
void get_constraint_target_matrix (struct Scene *scene, bConstraint *con, int n, short ownertype, void *ownerdata, float mat[][4], float ctime)
4409
4726
                
4410
4727
                /* only calculate the target matrix on the first target */
4411
4728
                ct= (bConstraintTarget *)targets.first;
4412
 
                while(ct && n-- > 0)
 
4729
                while (ct && n-- > 0)
4413
4730
                        ct= ct->next;
4414
4731
 
4415
4732
                if (ct) {
4428
4745
                unit_m4(mat);
4429
4746
        }
4430
4747
}
 
4748
 
 
4749
/* Get the list of targets required for solving a constraint */
 
4750
void get_constraint_targets_for_solving (bConstraint *con, bConstraintOb *cob, ListBase *targets, float ctime)
 
4751
{
 
4752
        bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
 
4753
        
 
4754
        if (cti && cti->get_constraint_targets) {
 
4755
                bConstraintTarget *ct;
 
4756
                
 
4757
                /* get targets 
 
4758
                 *      - constraints should use ct->matrix, not directly accessing values
 
4759
                 *      - ct->matrix members have not yet been calculated here! 
 
4760
                 */
 
4761
                cti->get_constraint_targets(con, targets);
 
4762
                
 
4763
                /* set matrices 
 
4764
                 *      - calculate if possible, otherwise just initialize as identity matrix 
 
4765
                 */
 
4766
                if (cti->get_target_matrix) {
 
4767
                        for (ct= targets->first; ct; ct= ct->next) 
 
4768
                                cti->get_target_matrix(con, cob, ct, ctime);
 
4769
                }
 
4770
                else {
 
4771
                        for (ct= targets->first; ct; ct= ct->next)
 
4772
                                unit_m4(ct->matrix);
 
4773
                }
 
4774
        }
 
4775
}
4431
4776
 
4432
4777
/* ---------- Evaluation ----------- */
4433
4778
 
4440
4785
void solve_constraints (ListBase *conlist, bConstraintOb *cob, float ctime)
4441
4786
{
4442
4787
        bConstraint *con;
4443
 
        float solution[4][4], delta[4][4];
4444
 
        float oldmat[4][4], imat[4][4];
 
4788
        float oldmat[4][4];
4445
4789
        float enf;
4446
4790
 
4447
4791
        /* check that there is a valid constraint object to evaluate */
4466
4810
                 */
4467
4811
                enf = con->enforce;
4468
4812
                
 
4813
                /* make copy of worldspace matrix pre-constraint for use with blending later */
 
4814
                copy_m4_m4(oldmat, cob->matrix);
 
4815
                
4469
4816
                /* move owner matrix into right space */
4470
4817
                constraint_mat_convertspace(cob->ob, cob->pchan, cob->matrix, CONSTRAINT_SPACE_WORLD, con->ownspace);
4471
 
                copy_m4_m4(oldmat, cob->matrix);
4472
4818
                
4473
4819
                /* prepare targets for constraint solving */
4474
 
                if (cti->get_constraint_targets) {
4475
 
                        bConstraintTarget *ct;
4476
 
                        
4477
 
                        /* get targets 
4478
 
                         *      - constraints should use ct->matrix, not directly accessing values
4479
 
                         *      - ct->matrix members have not yet been calculated here! 
4480
 
                         */
4481
 
                        cti->get_constraint_targets(con, &targets);
4482
 
                        
4483
 
                        /* set matrices 
4484
 
                         *      - calculate if possible, otherwise just initialise as identity matrix 
4485
 
                         */
4486
 
                        if (cti->get_target_matrix) {
4487
 
                                for (ct= targets.first; ct; ct= ct->next) 
4488
 
                                        cti->get_target_matrix(con, cob, ct, ctime);
4489
 
                        }
4490
 
                        else {
4491
 
                                for (ct= targets.first; ct; ct= ct->next)
4492
 
                                        unit_m4(ct->matrix);
4493
 
                        }
4494
 
                }
 
4820
                get_constraint_targets_for_solving(con, cob, &targets, ctime);
4495
4821
                
4496
 
                /* Solve the constraint */
 
4822
                /* Solve the constraint and put result in cob->matrix */
4497
4823
                cti->evaluate_constraint(con, cob, &targets);
4498
4824
                
4499
4825
                /* clear targets after use 
4504
4830
                        cti->flush_constraint_targets(con, &targets, 1);
4505
4831
                }
4506
4832
                
4507
 
                /* Interpolate the enforcement, to blend result of constraint into final owner transform */
4508
 
                /* 1. Remove effects of original matrix from constraint solution ==> delta */
4509
 
                invert_m4_m4(imat, oldmat);
4510
 
                copy_m4_m4(solution, cob->matrix);
4511
 
                mul_m4_m4m4(delta, solution, imat);
4512
 
                
4513
 
                /* 2. If constraint influence is not full strength, then interpolate
4514
 
                 *      identity_matrix --> delta_matrix to get the effect the constraint actually exerts
4515
 
                 */
4516
 
                if (enf < 1.0) {
4517
 
                        float identity[4][4];
4518
 
                        unit_m4(identity);
4519
 
                        blend_m4_m4m4(delta, identity, delta, enf);
4520
 
                }
4521
 
                
4522
 
                /* 3. Now multiply the delta by the matrix in use before the evaluation */
4523
 
                mul_m4_m4m4(cob->matrix, delta, oldmat);
4524
 
                
4525
4833
                /* move owner back into worldspace for next constraint/other business */
4526
4834
                if ((con->flag & CONSTRAINT_SPACEONCE) == 0) 
4527
4835
                        constraint_mat_convertspace(cob->ob, cob->pchan, cob->matrix, con->ownspace, CONSTRAINT_SPACE_WORLD);
 
4836
                        
 
4837
                /* Interpolate the enforcement, to blend result of constraint into final owner transform 
 
4838
                 *      - all this happens in worldspace to prevent any weirdness creeping in ([#26014] and [#25725]),
 
4839
                 *        since some constraints may not convert the solution back to the input space before blending
 
4840
                 *        but all are guaranteed to end up in good "worldspace" result
 
4841
                 */
 
4842
                /* Note: all kind of stuff here before (caused trouble), much easier to just interpolate, or did I miss something? -jahka */
 
4843
                if (enf < 1.0f) {
 
4844
                        float solution[4][4];
 
4845
                        copy_m4_m4(solution, cob->matrix);
 
4846
                        blend_m4_m4m4(cob->matrix, oldmat, solution, enf);
 
4847
                }
4528
4848
        }
4529
4849
}