Spline IK: support changing individual bone length via Y scaling.
[blender.git] / source / blender / blenkernel / intern / constraint.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup bke
22  */
23
24
25 #include <stdio.h>
26 #include <stddef.h>
27 #include <string.h>
28 #include <math.h>
29 #include <float.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_math.h"
35 #include "BLI_kdopbvh.h"
36 #include "BLI_utildefines.h"
37 #include "BLI_string_utils.h"
38 #include "BLT_translation.h"
39
40 #include "DNA_armature_types.h"
41 #include "DNA_cachefile_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_modifier_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_action_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49
50 #include "DNA_lattice_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_tracking_types.h"
53 #include "DNA_movieclip_types.h"
54
55
56 #include "BKE_action.h"
57 #include "BKE_anim.h" /* for the curve calculation part */
58 #include "BKE_armature.h"
59 #include "BKE_bvhutils.h"
60 #include "BKE_cachefile.h"
61 #include "BKE_camera.h"
62 #include "BKE_constraint.h"
63 #include "BKE_curve.h"
64 #include "BKE_deform.h"
65 #include "BKE_displist.h"
66 #include "BKE_editmesh.h"
67 #include "BKE_global.h"
68 #include "BKE_idprop.h"
69 #include "BKE_library.h"
70 #include "BKE_mesh_runtime.h"
71 #include "BKE_movieclip.h"
72 #include "BKE_object.h"
73 #include "BKE_scene.h"
74 #include "BKE_shrinkwrap.h"
75 #include "BKE_tracking.h"
76
77 #include "BIK_api.h"
78
79 #include "DEG_depsgraph.h"
80 #include "DEG_depsgraph_query.h"
81
82 #include "CLG_log.h"
83
84 #ifdef WITH_PYTHON
85 #  include "BPY_extern.h"
86 #endif
87
88 #ifdef WITH_ALEMBIC
89 #  include "ABC_alembic.h"
90 #endif
91
92 /* ---------------------------------------------------------------------------- */
93 /* Useful macros for testing various common flag combinations */
94
95 /* Constraint Target Macros */
96 #define VALID_CONS_TARGET(ct) ((ct) && (ct->tar))
97
98 static CLG_LogRef LOG = {"bke.constraint"};
99
100 /* ************************ Constraints - General Utilities *************************** */
101 /* These functions here don't act on any specific constraints, and are therefore should/will
102  * not require any of the special function-pointers afforded by the relevant constraint
103  * type-info structs.
104  */
105
106 static void damptrack_do_transform(float matrix[4][4], const float tarvec[3], int track_axis);
107
108 static bConstraint *constraint_find_original(Object *ob, bPoseChannel *pchan, bConstraint *con, Object **r_orig_ob);
109 static bConstraint *constraint_find_original_for_update(bConstraintOb *cob, bConstraint *con);
110
111 /* -------------- Naming -------------- */
112
113 /* Find the first available, non-duplicate name for a given constraint */
114 void BKE_constraint_unique_name(bConstraint *con, ListBase *list)
115 {
116         BLI_uniquename(list, con, DATA_("Const"), '.', offsetof(bConstraint, name), sizeof(con->name));
117 }
118
119 /* ----------------- Evaluation Loop Preparation --------------- */
120
121 /* package an object/bone for use in constraint evaluation */
122 /* This function MEM_calloc's a bConstraintOb struct, that will need to be freed after evaluation */
123 bConstraintOb *BKE_constraints_make_evalob(Depsgraph *depsgraph, Scene *scene, Object *ob, void *subdata, short datatype)
124 {
125         bConstraintOb *cob;
126
127         /* create regardless of whether we have any data! */
128         cob = MEM_callocN(sizeof(bConstraintOb), "bConstraintOb");
129
130         /* for system time, part of deglobalization, code nicer later with local time (ton) */
131         cob->scene = scene;
132         cob->depsgraph = depsgraph;
133
134         /* based on type of available data */
135         switch (datatype) {
136                 case CONSTRAINT_OBTYPE_OBJECT:
137                 {
138                         /* disregard subdata... calloc should set other values right */
139                         if (ob) {
140                                 cob->ob = ob;
141                                 cob->type = datatype;
142
143                                 if (cob->ob->rotmode > 0) {
144                                         /* Should be some kind of Euler order, so use it */
145                                         /* NOTE: Versions <= 2.76 assumed that "default" order
146                                          *       would always get used, so we may seem some rig
147                                          *       breakage as a result. However, this change here
148                                          *       is needed to fix T46599
149                                          */
150                                         cob->rotOrder = ob->rotmode;
151                                 }
152                                 else {
153                                         /* Quats/Axis-Angle, so Eulers should just use default order */
154                                         cob->rotOrder = EULER_ORDER_DEFAULT;
155                                 }
156                                 copy_m4_m4(cob->matrix, ob->obmat);
157                         }
158                         else
159                                 unit_m4(cob->matrix);
160
161                         copy_m4_m4(cob->startmat, cob->matrix);
162                         break;
163                 }
164                 case CONSTRAINT_OBTYPE_BONE:
165                 {
166                         /* only set if we have valid bone, otherwise default */
167                         if (ob && subdata) {
168                                 cob->ob = ob;
169                                 cob->pchan = (bPoseChannel *)subdata;
170                                 cob->type = datatype;
171
172                                 if (cob->pchan->rotmode > 0) {
173                                         /* should be some type of Euler order */
174                                         cob->rotOrder = cob->pchan->rotmode;
175                                 }
176                                 else {
177                                         /* Quats, so eulers should just use default order */
178                                         cob->rotOrder = EULER_ORDER_DEFAULT;
179                                 }
180
181                                 /* matrix in world-space */
182                                 mul_m4_m4m4(cob->matrix, ob->obmat, cob->pchan->pose_mat);
183                         }
184                         else
185                                 unit_m4(cob->matrix);
186
187                         copy_m4_m4(cob->startmat, cob->matrix);
188                         break;
189                 }
190                 default: /* other types not yet handled */
191                         unit_m4(cob->matrix);
192                         unit_m4(cob->startmat);
193                         break;
194         }
195
196         return cob;
197 }
198
199 /* cleanup after constraint evaluation */
200 void BKE_constraints_clear_evalob(bConstraintOb *cob)
201 {
202         float delta[4][4], imat[4][4];
203
204         /* prevent crashes */
205         if (cob == NULL)
206                 return;
207
208         /* calculate delta of constraints evaluation */
209         invert_m4_m4(imat, cob->startmat);
210         /* XXX This would seem to be in wrong order. However, it does not work in 'right' order - would be nice to
211          *     understand why premul is needed here instead of usual postmul?
212          *     In any case, we **do not get a delta** here (e.g. startmat & matrix having same location, still gives
213          *     a 'delta' with non-null translation component :/ ).*/
214         mul_m4_m4m4(delta, cob->matrix, imat);
215
216         /* copy matrices back to source */
217         switch (cob->type) {
218                 case CONSTRAINT_OBTYPE_OBJECT:
219                 {
220                         /* cob->ob might not exist! */
221                         if (cob->ob) {
222                                 /* copy new ob-matrix back to owner */
223                                 copy_m4_m4(cob->ob->obmat, cob->matrix);
224
225                                 /* copy inverse of delta back to owner */
226                                 invert_m4_m4(cob->ob->constinv, delta);
227                         }
228                         break;
229                 }
230                 case CONSTRAINT_OBTYPE_BONE:
231                 {
232                         /* cob->ob or cob->pchan might not exist */
233                         if (cob->ob && cob->pchan) {
234                                 /* copy new pose-matrix back to owner */
235                                 mul_m4_m4m4(cob->pchan->pose_mat, cob->ob->imat, cob->matrix);
236
237                                 /* copy inverse of delta back to owner */
238                                 invert_m4_m4(cob->pchan->constinv, delta);
239                         }
240                         break;
241                 }
242         }
243
244         /* free tempolary struct */
245         MEM_freeN(cob);
246 }
247
248 /* -------------- Space-Conversion API -------------- */
249
250 /* This function is responsible for the correct transformations/conversions
251  * of a matrix from one space to another for constraint evaluation.
252  * For now, this is only implemented for Objects and PoseChannels.
253  */
254 void BKE_constraint_mat_convertspace(
255         Object *ob, bPoseChannel *pchan, float mat[4][4], short from, short to, const bool keep_scale)
256 {
257         float diff_mat[4][4];
258         float imat[4][4];
259
260         /* prevent crashes in these unlikely events  */
261         if (ob == NULL || mat == NULL) return;
262         /* optimize trick - check if need to do anything */
263         if (from == to) return;
264
265         /* are we dealing with pose-channels or objects */
266         if (pchan) {
267                 /* pose channels */
268                 switch (from) {
269                         case CONSTRAINT_SPACE_WORLD: /* ---------- FROM WORLDSPACE ---------- */
270                         {
271                                 /* world to pose */
272                                 invert_m4_m4(imat, ob->obmat);
273                                 mul_m4_m4m4(mat, imat, mat);
274
275                                 /* use pose-space as stepping stone for other spaces... */
276                                 if (ELEM(to, CONSTRAINT_SPACE_LOCAL, CONSTRAINT_SPACE_PARLOCAL)) {
277                                         /* call self with slightly different values */
278                                         BKE_constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to, keep_scale);
279                                 }
280                                 break;
281                         }
282                         case CONSTRAINT_SPACE_POSE: /* ---------- FROM POSESPACE ---------- */
283                         {
284                                 /* pose to world */
285                                 if (to == CONSTRAINT_SPACE_WORLD) {
286                                         mul_m4_m4m4(mat, ob->obmat, mat);
287                                 }
288                                 /* pose to local */
289                                 else if (to == CONSTRAINT_SPACE_LOCAL) {
290                                         if (pchan->bone) {
291                                                 BKE_armature_mat_pose_to_bone(pchan, mat, mat);
292                                         }
293                                 }
294                                 /* pose to local with parent */
295                                 else if (to == CONSTRAINT_SPACE_PARLOCAL) {
296                                         if (pchan->bone) {
297                                                 invert_m4_m4(imat, pchan->bone->arm_mat);
298                                                 mul_m4_m4m4(mat, imat, mat);
299                                         }
300                                 }
301                                 break;
302                         }
303                         case CONSTRAINT_SPACE_LOCAL: /* ------------ FROM LOCALSPACE --------- */
304                         {
305                                 /* local to pose - do inverse procedure that was done for pose to local */
306                                 if (pchan->bone) {
307                                         /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */
308                                         BKE_armature_mat_bone_to_pose(pchan, mat, mat);
309                                 }
310
311                                 /* use pose-space as stepping stone for other spaces */
312                                 if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_PARLOCAL)) {
313                                         /* call self with slightly different values */
314                                         BKE_constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to, keep_scale);
315                                 }
316                                 break;
317                         }
318                         case CONSTRAINT_SPACE_PARLOCAL: /* -------------- FROM LOCAL WITH PARENT ---------- */
319                         {
320                                 /* local + parent to pose */
321                                 if (pchan->bone) {
322                                         mul_m4_m4m4(mat, pchan->bone->arm_mat, mat);
323                                 }
324
325                                 /* use pose-space as stepping stone for other spaces */
326                                 if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL)) {
327                                         /* call self with slightly different values */
328                                         BKE_constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to, keep_scale);
329                                 }
330                                 break;
331                         }
332                 }
333         }
334         else {
335                 /* objects */
336                 if (from == CONSTRAINT_SPACE_WORLD && to == CONSTRAINT_SPACE_LOCAL) {
337                         /* check if object has a parent */
338                         if (ob->parent) {
339                                 /* 'subtract' parent's effects from owner */
340                                 mul_m4_m4m4(diff_mat, ob->parent->obmat, ob->parentinv);
341                                 invert_m4_m4_safe(imat, diff_mat);
342                                 mul_m4_m4m4(mat, imat, mat);
343                         }
344                         else {
345                                 /* Local space in this case will have to be defined as local to the owner's
346                                  * transform-property-rotated axes. So subtract this rotation component.
347                                  */
348                                 /* XXX This is actually an ugly hack, local space of a parent-less object *is* the same as
349                                  *     global space!
350                                  *     Think what we want actually here is some kind of 'Final Space', i.e. once transformations
351                                  *     are applied - users are often confused about this too, this is not consistent with bones
352                                  *     local space either... Meh :|
353                                  *     --mont29
354                                  */
355                                 BKE_object_to_mat4(ob, diff_mat);
356                                 if (!keep_scale) {
357                                         normalize_m4(diff_mat);
358                                 }
359                                 zero_v3(diff_mat[3]);
360
361                                 invert_m4_m4_safe(imat, diff_mat);
362                                 mul_m4_m4m4(mat, imat, mat);
363                         }
364                 }
365                 else if (from == CONSTRAINT_SPACE_LOCAL && to == CONSTRAINT_SPACE_WORLD) {
366                         /* check that object has a parent - otherwise this won't work */
367                         if (ob->parent) {
368                                 /* 'add' parent's effect back to owner */
369                                 mul_m4_m4m4(diff_mat, ob->parent->obmat, ob->parentinv);
370                                 mul_m4_m4m4(mat, diff_mat, mat);
371                         }
372                         else {
373                                 /* Local space in this case will have to be defined as local to the owner's
374                                  * transform-property-rotated axes. So add back this rotation component.
375                                  */
376                                 /* XXX See comment above for world->local case... */
377                                 BKE_object_to_mat4(ob, diff_mat);
378                                 if (!keep_scale) {
379                                         normalize_m4(diff_mat);
380                                 }
381                                 zero_v3(diff_mat[3]);
382
383                                 mul_m4_m4m4(mat, diff_mat, mat);
384                         }
385                 }
386         }
387 }
388
389 /* ------------ General Target Matrix Tools ---------- */
390
391 /* function that sets the given matrix based on given vertex group in mesh */
392 static void contarget_get_mesh_mat(Object *ob, const char *substring, float mat[4][4])
393 {
394         /* when not in EditMode, use the 'final' evaluated mesh, depsgraph
395          * ensures we build with CD_MDEFORMVERT layer
396          */
397         Mesh *me_eval = ob->runtime.mesh_eval;
398         BMEditMesh *em = BKE_editmesh_from_object(ob);
399         float plane[3];
400         float imat[3][3], tmat[3][3];
401         const int defgroup = defgroup_name_index(ob, substring);
402
403         /* initialize target matrix using target matrix */
404         copy_m4_m4(mat, ob->obmat);
405
406         /* get index of vertex group */
407         if (defgroup == -1) {
408                 return;
409         }
410
411         float vec[3] = {0.0f, 0.0f, 0.0f};
412         float normal[3] = {0.0f, 0.0f, 0.0f};
413         float weightsum = 0.0f;
414         if (me_eval) {
415                 MDeformVert *dvert = CustomData_get_layer(&me_eval->vdata, CD_MDEFORMVERT);
416                 int numVerts = me_eval->totvert;
417
418                 /* check that dvert is a valid pointers (just in case) */
419                 if (dvert) {
420                         MDeformVert *dv = dvert;
421                         MVert *mv = me_eval->mvert;
422
423                         /* get the average of all verts with that are in the vertex-group */
424                         for (int i = 0; i < numVerts; i++, dv++, mv++) {
425                                 MDeformWeight *dw = defvert_find_index(dv, defgroup);
426
427                                 if (dw && dw->weight > 0.0f) {
428                                         float nor[3];
429                                         normal_short_to_float_v3(nor, mv->no);
430                                         madd_v3_v3fl(vec, mv->co, dw->weight);
431                                         madd_v3_v3fl(normal, nor, dw->weight);
432                                         weightsum += dw->weight;
433                                 }
434                         }
435                 }
436         }
437         else if (em) {
438                 if (CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT)) {
439                         BMVert *v;
440                         BMIter iter;
441
442                         BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
443                                 MDeformVert *dv = CustomData_bmesh_get(&em->bm->vdata, v->head.data, CD_MDEFORMVERT);
444                                 MDeformWeight *dw = defvert_find_index(dv, defgroup);
445
446                                 if (dw && dw->weight > 0.0f) {
447                                         madd_v3_v3fl(vec, v->co, dw->weight);
448                                         madd_v3_v3fl(normal, v->no, dw->weight);
449                                         weightsum += dw->weight;
450                                 }
451                         }
452                 }
453         }
454         else {
455                 /* No valid edit or evaluated mesh, just abort. */
456                 return;
457         }
458
459         /* calculate averages of normal and coordinates */
460         if (weightsum > 0) {
461                 mul_v3_fl(vec, 1.0f / weightsum);
462                 mul_v3_fl(normal, 1.0f / weightsum);
463         }
464
465         /* derive the rotation from the average normal:
466          * - code taken from transform_gizmo.c,
467          *   calc_gizmo_stats, V3D_ORIENT_NORMAL case
468          */
469         /*      we need the transpose of the inverse for a normal... */
470         copy_m3_m4(imat, ob->obmat);
471
472         invert_m3_m3(tmat, imat);
473         transpose_m3(tmat);
474         mul_m3_v3(tmat, normal);
475
476         normalize_v3(normal);
477         copy_v3_v3(plane, tmat[1]);
478
479         cross_v3_v3v3(mat[0], normal, plane);
480         if (len_squared_v3(mat[0]) < SQUARE(1e-3f)) {
481                 copy_v3_v3(plane, tmat[0]);
482                 cross_v3_v3v3(mat[0], normal, plane);
483         }
484
485         copy_v3_v3(mat[2], normal);
486         cross_v3_v3v3(mat[1], mat[2], mat[0]);
487
488         normalize_m4(mat);
489
490         /* apply the average coordinate as the new location */
491         mul_v3_m4v3(mat[3], ob->obmat, vec);
492 }
493
494 /* function that sets the given matrix based on given vertex group in lattice */
495 static void contarget_get_lattice_mat(Object *ob, const char *substring, float mat[4][4])
496 {
497         Lattice *lt = (Lattice *)ob->data;
498
499         DispList *dl = ob->runtime.curve_cache ? BKE_displist_find(&ob->runtime.curve_cache->disp, DL_VERTS) : NULL;
500         const float *co = dl ? dl->verts : NULL;
501         BPoint *bp = lt->def;
502
503         MDeformVert *dv = lt->dvert;
504         int tot_verts = lt->pntsu * lt->pntsv * lt->pntsw;
505         float vec[3] = {0.0f, 0.0f, 0.0f}, tvec[3];
506         int grouped = 0;
507         int i, n;
508         const int defgroup = defgroup_name_index(ob, substring);
509
510         /* initialize target matrix using target matrix */
511         copy_m4_m4(mat, ob->obmat);
512
513         /* get index of vertex group */
514         if (defgroup == -1) return;
515         if (dv == NULL) return;
516
517         /* 1. Loop through control-points checking if in nominated vertex-group.
518          * 2. If it is, add it to vec to find the average point.
519          */
520         for (i = 0; i < tot_verts; i++, dv++) {
521                 for (n = 0; n < dv->totweight; n++) {
522                         MDeformWeight *dw = defvert_find_index(dv, defgroup);
523                         if (dw && dw->weight > 0.0f) {
524                                 /* copy coordinates of point to temporary vector, then add to find average */
525                                 memcpy(tvec, co ? co : bp->vec, 3 * sizeof(float));
526
527                                 add_v3_v3(vec, tvec);
528                                 grouped++;
529                         }
530                 }
531
532                 /* advance pointer to coordinate data */
533                 if (co) co += 3;
534                 else bp++;
535         }
536
537         /* find average location, then multiply by ob->obmat to find world-space location */
538         if (grouped)
539                 mul_v3_fl(vec, 1.0f / grouped);
540         mul_v3_m4v3(tvec, ob->obmat, vec);
541
542         /* copy new location to matrix */
543         copy_v3_v3(mat[3], tvec);
544 }
545
546 /* generic function to get the appropriate matrix for most target cases */
547 /* The cases where the target can be object data have not been implemented */
548 static void constraint_target_to_mat4(Object *ob, const char *substring, float mat[4][4], short from, short to, short flag, float headtail)
549 {
550         /* Case OBJECT */
551         if (substring[0] == '\0') {
552                 copy_m4_m4(mat, ob->obmat);
553                 BKE_constraint_mat_convertspace(ob, NULL, mat, from, to, false);
554         }
555         /*  Case VERTEXGROUP */
556         /* Current method just takes the average location of all the points in the
557          * VertexGroup, and uses that as the location value of the targets. Where
558          * possible, the orientation will also be calculated, by calculating an
559          * 'average' vertex normal, and deriving the rotation from that.
560          *
561          * NOTE: EditMode is not currently supported, and will most likely remain that
562          *       way as constraints can only really affect things on object/bone level.
563          */
564         else if (ob->type == OB_MESH) {
565                 contarget_get_mesh_mat(ob, substring, mat);
566                 BKE_constraint_mat_convertspace(ob, NULL, mat, from, to, false);
567         }
568         else if (ob->type == OB_LATTICE) {
569                 contarget_get_lattice_mat(ob, substring, mat);
570                 BKE_constraint_mat_convertspace(ob, NULL, mat, from, to, false);
571         }
572         /* Case BONE */
573         else {
574                 bPoseChannel *pchan;
575
576                 pchan = BKE_pose_channel_find_name(ob->pose, substring);
577                 if (pchan) {
578                         /* Multiply the PoseSpace accumulation/final matrix for this
579                          * PoseChannel by the Armature Object's Matrix to get a worldspace
580                          * matrix.
581                          */
582                         bool is_bbone = (pchan->bone) && (pchan->bone->segments > 1) && (flag & CONSTRAINT_BBONE_SHAPE);
583                         bool full_bbone = (flag & CONSTRAINT_BBONE_SHAPE_FULL) != 0;
584
585                         if (headtail < 0.000001f && !(is_bbone && full_bbone)) {
586                                 /* skip length interpolation if set to head */
587                                 mul_m4_m4m4(mat, ob->obmat, pchan->pose_mat);
588                         }
589                         else if (is_bbone && pchan->bone->segments == pchan->runtime.bbone_segments) {
590                                 /* use point along bbone */
591                                 Mat4 *bbone = pchan->runtime.bbone_pose_mats;
592                                 float tempmat[4][4];
593                                 float loc[3], fac;
594                                 int index;
595
596                                 /* figure out which segment(s) the headtail value falls in */
597                                 BKE_pchan_bbone_deform_segment_index(pchan, headtail, &index, &fac);
598
599                                 /* apply full transformation of the segment if requested */
600                                 if (full_bbone) {
601                                         interp_m4_m4m4(tempmat, bbone[index].mat, bbone[index + 1].mat, fac);
602
603                                         mul_m4_m4m4(tempmat, pchan->pose_mat, tempmat);
604                                 }
605                                 /* only interpolate location */
606                                 else {
607                                         interp_v3_v3v3(loc, bbone[index].mat[3], bbone[index + 1].mat[3], fac);
608
609                                         copy_m4_m4(tempmat, pchan->pose_mat);
610                                         mul_v3_m4v3(tempmat[3], pchan->pose_mat, loc);
611                                 }
612
613                                 mul_m4_m4m4(mat, ob->obmat, tempmat);
614                         }
615                         else {
616                                 float tempmat[4][4], loc[3];
617
618                                 /* interpolate along length of bone */
619                                 interp_v3_v3v3(loc, pchan->pose_head, pchan->pose_tail, headtail);
620
621                                 /* use interpolated distance for subtarget */
622                                 copy_m4_m4(tempmat, pchan->pose_mat);
623                                 copy_v3_v3(tempmat[3], loc);
624
625                                 mul_m4_m4m4(mat, ob->obmat, tempmat);
626                         }
627                 }
628                 else
629                         copy_m4_m4(mat, ob->obmat);
630
631                 /* convert matrix space as required */
632                 BKE_constraint_mat_convertspace(ob, pchan, mat, from, to, false);
633         }
634 }
635
636 /* ************************* Specific Constraints ***************************** */
637 /* Each constraint defines a set of functions, which will be called at the appropriate
638  * times. In addition to this, each constraint should have a type-info struct, where
639  * its functions are attached for use.
640  */
641
642 /* Template for type-info data:
643  * - make a copy of this when creating new constraints, and just change the functions
644  *   pointed to as necessary
645  * - although the naming of functions doesn't matter, it would help for code
646  *   readability, to follow the same naming convention as is presented here
647  * - any functions that a constraint doesn't need to define, don't define
648  *   for such cases, just use NULL
649  * - these should be defined after all the functions have been defined, so that
650  *   forward-definitions/prototypes don't need to be used!
651  * - keep this copy #if-def'd so that future constraints can get based off this
652  */
653 #if 0
654 static bConstraintTypeInfo CTI_CONSTRNAME = {
655         CONSTRAINT_TYPE_CONSTRNAME, /* type */
656         sizeof(bConstrNameConstraint), /* size */
657         "ConstrName", /* name */
658         "bConstrNameConstraint", /* struct name */
659         constrname_free, /* free data */
660         constrname_id_looper, /* id looper */
661         constrname_copy, /* copy data */
662         constrname_new_data, /* new data */
663         constrname_get_tars, /* get constraint targets */
664         constrname_flush_tars, /* flush constraint targets */
665         constrname_get_tarmat, /* get target matrix */
666         constrname_evaluate, /* evaluate */
667 };
668 #endif
669
670 /* This function should be used for the get_target_matrix member of all
671  * constraints that are not picky about what happens to their target matrix.
672  */
673 static void default_get_tarmat(struct Depsgraph *UNUSED(depsgraph), bConstraint *con, bConstraintOb *UNUSED(cob), bConstraintTarget *ct, float UNUSED(ctime))
674 {
675         if (VALID_CONS_TARGET(ct))
676                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->flag, con->headtail);
677         else if (ct)
678                 unit_m4(ct->matrix);
679 }
680
681 /* This is a variant that extracts full transformation from B-Bone segments.
682  */
683 static void default_get_tarmat_full_bbone(struct Depsgraph *UNUSED(depsgraph), bConstraint *con, bConstraintOb *UNUSED(cob), bConstraintTarget *ct, float UNUSED(ctime))
684 {
685         if (VALID_CONS_TARGET(ct))
686                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->flag | CONSTRAINT_BBONE_SHAPE_FULL, con->headtail);
687         else if (ct)
688                 unit_m4(ct->matrix);
689 }
690
691 /* This following macro should be used for all standard single-target *_get_tars functions
692  * to save typing and reduce maintenance woes.
693  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
694  *  really just to help this code easier to read)
695  */
696 // TODO: cope with getting rotation order...
697 #define SINGLETARGET_GET_TARS(con, datatar, datasubtarget, ct, list) \
698         { \
699                 ct = MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
700                  \
701                 ct->tar = datatar; \
702                 BLI_strncpy(ct->subtarget, datasubtarget, sizeof(ct->subtarget)); \
703                 ct->space = con->tarspace; \
704                 ct->flag = CONSTRAINT_TAR_TEMP; \
705                  \
706                 if (ct->tar) { \
707                         if ((ct->tar->type == OB_ARMATURE) && (ct->subtarget[0])) { \
708                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ct->tar->pose, ct->subtarget); \
709                                 ct->type = CONSTRAINT_OBTYPE_BONE; \
710                                 ct->rotOrder = (pchan) ? (pchan->rotmode) : EULER_ORDER_DEFAULT; \
711                         } \
712                         else if (OB_TYPE_SUPPORT_VGROUP(ct->tar->type) && (ct->subtarget[0])) { \
713                                 ct->type = CONSTRAINT_OBTYPE_VERT; \
714                                 ct->rotOrder = EULER_ORDER_DEFAULT; \
715                         } \
716                         else { \
717                                 ct->type = CONSTRAINT_OBTYPE_OBJECT; \
718                                 ct->rotOrder = ct->tar->rotmode; \
719                         } \
720                 } \
721                  \
722                 BLI_addtail(list, ct); \
723         } (void)0
724
725 /* This following macro should be used for all standard single-target *_get_tars functions
726  * to save typing and reduce maintenance woes. It does not do the subtarget related operations
727  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
728  *  really just to help this code easier to read)
729  */
730 // TODO: cope with getting rotation order...
731 #define SINGLETARGETNS_GET_TARS(con, datatar, ct, list) \
732         { \
733                 ct = MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
734                  \
735                 ct->tar = datatar; \
736                 ct->space = con->tarspace; \
737                 ct->flag = CONSTRAINT_TAR_TEMP; \
738                  \
739                 if (ct->tar) ct->type = CONSTRAINT_OBTYPE_OBJECT; \
740                  \
741                 BLI_addtail(list, ct); \
742         } (void)0
743
744 /* This following macro should be used for all standard single-target *_flush_tars functions
745  * to save typing and reduce maintenance woes.
746  * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
747  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
748  *  really just to help this code easier to read)
749  */
750 #define SINGLETARGET_FLUSH_TARS(con, datatar, datasubtarget, ct, list, no_copy) \
751         { \
752                 if (ct) { \
753                         bConstraintTarget *ctn = ct->next; \
754                         if (no_copy == 0) { \
755                                 datatar = ct->tar; \
756                                 BLI_strncpy(datasubtarget, ct->subtarget, sizeof(datasubtarget)); \
757                                 con->tarspace = (char)ct->space; \
758                         } \
759                          \
760                         BLI_freelinkN(list, ct); \
761                         ct = ctn; \
762                 } \
763         } (void)0
764
765 /* This following macro should be used for all standard single-target *_flush_tars functions
766  * to save typing and reduce maintenance woes. It does not do the subtarget related operations.
767  * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
768  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
769  *  really just to help this code easier to read)
770  */
771 #define SINGLETARGETNS_FLUSH_TARS(con, datatar, ct, list, no_copy) \
772         { \
773                 if (ct) { \
774                         bConstraintTarget *ctn = ct->next; \
775                         if (no_copy == 0) { \
776                                 datatar = ct->tar; \
777                                 con->tarspace = (char)ct->space; \
778                         } \
779                          \
780                         BLI_freelinkN(list, ct); \
781                         ct = ctn; \
782                 } \
783         } (void)0
784
785 /* --------- ChildOf Constraint ------------ */
786
787 static void childof_new_data(void *cdata)
788 {
789         bChildOfConstraint *data = (bChildOfConstraint *)cdata;
790
791         data->flag = (CHILDOF_LOCX | CHILDOF_LOCY | CHILDOF_LOCZ |
792                       CHILDOF_ROTX | CHILDOF_ROTY | CHILDOF_ROTZ |
793                       CHILDOF_SIZEX | CHILDOF_SIZEY | CHILDOF_SIZEZ);
794         unit_m4(data->invmat);
795 }
796
797 static void childof_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
798 {
799         bChildOfConstraint *data = con->data;
800
801         /* target only */
802         func(con, (ID **)&data->tar, false, userdata);
803 }
804
805 static int childof_get_tars(bConstraint *con, ListBase *list)
806 {
807         if (con && list) {
808                 bChildOfConstraint *data = con->data;
809                 bConstraintTarget *ct;
810
811                 /* standard target-getting macro for single-target constraints */
812                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
813
814                 return 1;
815         }
816
817         return 0;
818 }
819
820 static void childof_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
821 {
822         if (con && list) {
823                 bChildOfConstraint *data = con->data;
824                 bConstraintTarget *ct = list->first;
825
826                 /* the following macro is used for all standard single-target constraints */
827                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
828         }
829 }
830
831 static void childof_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
832 {
833         bChildOfConstraint *data = con->data;
834         bConstraintTarget *ct = targets->first;
835
836         /* only evaluate if there is a target */
837         if (VALID_CONS_TARGET(ct)) {
838                 float parmat[4][4];
839
840                 /* simple matrix parenting */
841                 if (data->flag == CHILDOF_ALL) {
842
843                         /* multiply target (parent matrix) by offset (parent inverse) to get
844                          * the effect of the parent that will be exerted on the owner
845                          */
846                         mul_m4_m4m4(parmat, ct->matrix, data->invmat);
847
848                         /* now multiply the parent matrix by the owner matrix to get the
849                          * the effect of this constraint (i.e. owner is 'parented' to parent)
850                          */
851                         mul_m4_m4m4(cob->matrix, parmat, cob->matrix);
852                 }
853                 else {
854                         float invmat[4][4], tempmat[4][4];
855                         float loc[3], eul[3], size[3];
856                         float loco[3], eulo[3], sizo[3];
857
858                         /* get offset (parent-inverse) matrix */
859                         copy_m4_m4(invmat, data->invmat);
860
861                         /* extract components of both matrices */
862                         copy_v3_v3(loc, ct->matrix[3]);
863                         mat4_to_eulO(eul, ct->rotOrder, ct->matrix);
864                         mat4_to_size(size, ct->matrix);
865
866                         copy_v3_v3(loco, invmat[3]);
867                         mat4_to_eulO(eulo, cob->rotOrder, invmat);
868                         mat4_to_size(sizo, invmat);
869
870                         /* disable channels not enabled */
871                         if (!(data->flag & CHILDOF_LOCX)) loc[0] = loco[0] = 0.0f;
872                         if (!(data->flag & CHILDOF_LOCY)) loc[1] = loco[1] = 0.0f;
873                         if (!(data->flag & CHILDOF_LOCZ)) loc[2] = loco[2] = 0.0f;
874                         if (!(data->flag & CHILDOF_ROTX)) eul[0] = eulo[0] = 0.0f;
875                         if (!(data->flag & CHILDOF_ROTY)) eul[1] = eulo[1] = 0.0f;
876                         if (!(data->flag & CHILDOF_ROTZ)) eul[2] = eulo[2] = 0.0f;
877                         if (!(data->flag & CHILDOF_SIZEX)) size[0] = sizo[0] = 1.0f;
878                         if (!(data->flag & CHILDOF_SIZEY)) size[1] = sizo[1] = 1.0f;
879                         if (!(data->flag & CHILDOF_SIZEZ)) size[2] = sizo[2] = 1.0f;
880
881                         /* make new target mat and offset mat */
882                         loc_eulO_size_to_mat4(ct->matrix, loc, eul, size, ct->rotOrder);
883                         loc_eulO_size_to_mat4(invmat, loco, eulo, sizo, cob->rotOrder);
884
885                         /* multiply target (parent matrix) by offset (parent inverse) to get
886                          * the effect of the parent that will be exerted on the owner
887                          */
888                         mul_m4_m4m4(parmat, ct->matrix, invmat);
889
890                         /* now multiply the parent matrix by the owner matrix to get the
891                          * the effect of this constraint (i.e.  owner is 'parented' to parent)
892                          */
893                         copy_m4_m4(tempmat, cob->matrix);
894                         mul_m4_m4m4(cob->matrix, parmat, tempmat);
895
896                         /* without this, changes to scale and rotation can change location
897                          * of a parentless bone or a disconnected bone. Even though its set
898                          * to zero above. */
899                         if (!(data->flag & CHILDOF_LOCX)) cob->matrix[3][0] = tempmat[3][0];
900                         if (!(data->flag & CHILDOF_LOCY)) cob->matrix[3][1] = tempmat[3][1];
901                         if (!(data->flag & CHILDOF_LOCZ)) cob->matrix[3][2] = tempmat[3][2];
902                 }
903         }
904 }
905
906 /* XXX note, con->flag should be CONSTRAINT_SPACEONCE for bone-childof, patched in readfile.c */
907 static bConstraintTypeInfo CTI_CHILDOF = {
908         CONSTRAINT_TYPE_CHILDOF, /* type */
909         sizeof(bChildOfConstraint), /* size */
910         "Child Of", /* name */
911         "bChildOfConstraint", /* struct name */
912         NULL, /* free data */
913         childof_id_looper, /* id looper */
914         NULL, /* copy data */
915         childof_new_data, /* new data */
916         childof_get_tars, /* get constraint targets */
917         childof_flush_tars, /* flush constraint targets */
918         default_get_tarmat, /* get a target matrix */
919         childof_evaluate, /* evaluate */
920 };
921
922 /* -------- TrackTo Constraint ------- */
923
924 static void trackto_new_data(void *cdata)
925 {
926         bTrackToConstraint *data = (bTrackToConstraint *)cdata;
927
928         data->reserved1 = TRACK_Y;
929         data->reserved2 = UP_Z;
930 }
931
932 static void trackto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
933 {
934         bTrackToConstraint *data = con->data;
935
936         /* target only */
937         func(con, (ID **)&data->tar, false, userdata);
938 }
939
940 static int trackto_get_tars(bConstraint *con, ListBase *list)
941 {
942         if (con && list) {
943                 bTrackToConstraint *data = con->data;
944                 bConstraintTarget *ct;
945
946                 /* standard target-getting macro for single-target constraints */
947                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
948
949                 return 1;
950         }
951
952         return 0;
953 }
954
955 static void trackto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
956 {
957         if (con && list) {
958                 bTrackToConstraint *data = con->data;
959                 bConstraintTarget *ct = list->first;
960
961                 /* the following macro is used for all standard single-target constraints */
962                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
963         }
964 }
965
966
967 static int basis_cross(int n, int m)
968 {
969         switch (n - m) {
970                 case 1:
971                 case -2:
972                         return 1;
973
974                 case -1:
975                 case 2:
976                         return -1;
977
978                 default:
979                         return 0;
980         }
981 }
982
983 static void vectomat(const float vec[3], const float target_up[3], short axis, short upflag, short flags, float m[3][3])
984 {
985         float n[3];
986         float u[3]; /* vector specifying the up axis */
987         float proj[3];
988         float right[3];
989         float neg = -1;
990         int right_index;
991
992         if (normalize_v3_v3(n, vec) == 0.0f) {
993                 n[0] = 0.0f;
994                 n[1] = 0.0f;
995                 n[2] = 1.0f;
996         }
997         if (axis > 2) axis -= 3;
998         else negate_v3(n);
999
1000         /* n specifies the transformation of the track axis */
1001         if (flags & TARGET_Z_UP) {
1002                 /* target Z axis is the global up axis */
1003                 copy_v3_v3(u, target_up);
1004         }
1005         else {
1006                 /* world Z axis is the global up axis */
1007                 u[0] = 0;
1008                 u[1] = 0;
1009                 u[2] = 1;
1010         }
1011
1012         /* note: even though 'n' is normalized, don't use 'project_v3_v3v3_normalized' below
1013          * because precision issues cause a problem in near degenerate states, see: T53455. */
1014
1015         /* project the up vector onto the plane specified by n */
1016         project_v3_v3v3(proj, u, n); /* first u onto n... */
1017         sub_v3_v3v3(proj, u, proj); /* then onto the plane */
1018         /* proj specifies the transformation of the up axis */
1019
1020         if (normalize_v3(proj) == 0.0f) { /* degenerate projection */
1021                 proj[0] = 0.0f;
1022                 proj[1] = 1.0f;
1023                 proj[2] = 0.0f;
1024         }
1025
1026         /* Normalized cross product of n and proj specifies transformation of the right axis */
1027         cross_v3_v3v3(right, proj, n);
1028         normalize_v3(right);
1029
1030         if (axis != upflag) {
1031                 right_index = 3 - axis - upflag;
1032                 neg = (float)basis_cross(axis, upflag);
1033
1034                 /* account for up direction, track direction */
1035                 m[right_index][0] = neg * right[0];
1036                 m[right_index][1] = neg * right[1];
1037                 m[right_index][2] = neg * right[2];
1038
1039                 copy_v3_v3(m[upflag], proj);
1040
1041                 copy_v3_v3(m[axis], n);
1042         }
1043         /* identity matrix - don't do anything if the two axes are the same */
1044         else {
1045                 unit_m3(m);
1046         }
1047 }
1048
1049
1050 static void trackto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1051 {
1052         bTrackToConstraint *data = con->data;
1053         bConstraintTarget *ct = targets->first;
1054
1055         if (VALID_CONS_TARGET(ct)) {
1056                 float size[3], vec[3];
1057                 float totmat[3][3];
1058
1059                 /* Get size property, since ob->scale is only the object's own relative size, not its global one */
1060                 mat4_to_size(size, cob->matrix);
1061
1062                 /* Clear the object's rotation */
1063                 cob->matrix[0][0] = size[0];
1064                 cob->matrix[0][1] = 0;
1065                 cob->matrix[0][2] = 0;
1066                 cob->matrix[1][0] = 0;
1067                 cob->matrix[1][1] = size[1];
1068                 cob->matrix[1][2] = 0;
1069                 cob->matrix[2][0] = 0;
1070                 cob->matrix[2][1] = 0;
1071                 cob->matrix[2][2] = size[2];
1072
1073                 /* targetmat[2] instead of ownermat[2] is passed to vectomat
1074                  * for backwards compatibility it seems... (Aligorith)
1075                  */
1076                 sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
1077                 vectomat(vec, ct->matrix[2],
1078                          (short)data->reserved1, (short)data->reserved2,
1079                          data->flags, totmat);
1080
1081                 mul_m4_m3m4(cob->matrix, totmat, cob->matrix);
1082         }
1083 }
1084
1085 static bConstraintTypeInfo CTI_TRACKTO = {
1086         CONSTRAINT_TYPE_TRACKTO, /* type */
1087         sizeof(bTrackToConstraint), /* size */
1088         "Track To", /* name */
1089         "bTrackToConstraint", /* struct name */
1090         NULL, /* free data */
1091         trackto_id_looper, /* id looper */
1092         NULL, /* copy data */
1093         trackto_new_data, /* new data */
1094         trackto_get_tars, /* get constraint targets */
1095         trackto_flush_tars, /* flush constraint targets */
1096         default_get_tarmat, /* get target matrix */
1097         trackto_evaluate, /* evaluate */
1098 };
1099
1100 /* --------- Inverse-Kinematics --------- */
1101
1102 static void kinematic_new_data(void *cdata)
1103 {
1104         bKinematicConstraint *data = (bKinematicConstraint *)cdata;
1105
1106         data->weight = 1.0f;
1107         data->orientweight = 1.0f;
1108         data->iterations = 500;
1109         data->dist = 1.0f;
1110         data->flag = CONSTRAINT_IK_TIP | CONSTRAINT_IK_STRETCH | CONSTRAINT_IK_POS;
1111 }
1112
1113 static void kinematic_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1114 {
1115         bKinematicConstraint *data = con->data;
1116
1117         /* chain target */
1118         func(con, (ID **)&data->tar, false, userdata);
1119
1120         /* poletarget */
1121         func(con, (ID **)&data->poletar, false, userdata);
1122 }
1123
1124 static int kinematic_get_tars(bConstraint *con, ListBase *list)
1125 {
1126         if (con && list) {
1127                 bKinematicConstraint *data = con->data;
1128                 bConstraintTarget *ct;
1129
1130                 /* standard target-getting macro for single-target constraints is used twice here */
1131                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1132                 SINGLETARGET_GET_TARS(con, data->poletar, data->polesubtarget, ct, list);
1133
1134                 return 2;
1135         }
1136
1137         return 0;
1138 }
1139
1140 static void kinematic_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1141 {
1142         if (con && list) {
1143                 bKinematicConstraint *data = con->data;
1144                 bConstraintTarget *ct = list->first;
1145
1146                 /* the following macro is used for all standard single-target constraints */
1147                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1148                 SINGLETARGET_FLUSH_TARS(con, data->poletar, data->polesubtarget, ct, list, no_copy);
1149         }
1150 }
1151
1152 static void kinematic_get_tarmat(struct Depsgraph *UNUSED(depsgraph), bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
1153 {
1154         bKinematicConstraint *data = con->data;
1155
1156         if (VALID_CONS_TARGET(ct))
1157                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->flag, con->headtail);
1158         else if (ct) {
1159                 if (data->flag & CONSTRAINT_IK_AUTO) {
1160                         Object *ob = cob->ob;
1161
1162                         if (ob == NULL) {
1163                                 unit_m4(ct->matrix);
1164                         }
1165                         else {
1166                                 float vec[3];
1167                                 /* move grabtarget into world space */
1168                                 mul_v3_m4v3(vec, ob->obmat, data->grabtarget);
1169                                 copy_m4_m4(ct->matrix, ob->obmat);
1170                                 copy_v3_v3(ct->matrix[3], vec);
1171                         }
1172                 }
1173                 else
1174                         unit_m4(ct->matrix);
1175         }
1176 }
1177
1178 static bConstraintTypeInfo CTI_KINEMATIC = {
1179         CONSTRAINT_TYPE_KINEMATIC, /* type */
1180         sizeof(bKinematicConstraint), /* size */
1181         "IK", /* name */
1182         "bKinematicConstraint", /* struct name */
1183         NULL, /* free data */
1184         kinematic_id_looper, /* id looper */
1185         NULL, /* copy data */
1186         kinematic_new_data, /* new data */
1187         kinematic_get_tars, /* get constraint targets */
1188         kinematic_flush_tars, /* flush constraint targets */
1189         kinematic_get_tarmat, /* get target matrix */
1190         NULL, /* evaluate - solved as separate loop */
1191 };
1192
1193 /* -------- Follow-Path Constraint ---------- */
1194
1195 static void followpath_new_data(void *cdata)
1196 {
1197         bFollowPathConstraint *data = (bFollowPathConstraint *)cdata;
1198
1199         data->trackflag = TRACK_Y;
1200         data->upflag = UP_Z;
1201         data->offset = 0;
1202         data->followflag = 0;
1203 }
1204
1205 static void followpath_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1206 {
1207         bFollowPathConstraint *data = con->data;
1208
1209         /* target only */
1210         func(con, (ID **)&data->tar, false, userdata);
1211 }
1212
1213 static int followpath_get_tars(bConstraint *con, ListBase *list)
1214 {
1215         if (con && list) {
1216                 bFollowPathConstraint *data = con->data;
1217                 bConstraintTarget *ct;
1218
1219                 /* standard target-getting macro for single-target constraints without subtargets */
1220                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list);
1221
1222                 return 1;
1223         }
1224
1225         return 0;
1226 }
1227
1228 static void followpath_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1229 {
1230         if (con && list) {
1231                 bFollowPathConstraint *data = con->data;
1232                 bConstraintTarget *ct = list->first;
1233
1234                 /* the following macro is used for all standard single-target constraints */
1235                 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, no_copy);
1236         }
1237 }
1238
1239 static void followpath_get_tarmat(struct Depsgraph *UNUSED(depsgraph),
1240                                   bConstraint *con, bConstraintOb *UNUSED(cob),
1241                                   bConstraintTarget *ct, float UNUSED(ctime))
1242 {
1243         bFollowPathConstraint *data = con->data;
1244
1245         if (VALID_CONS_TARGET(ct) && (ct->tar->type == OB_CURVE)) {
1246                 Curve *cu = ct->tar->data;
1247                 float vec[4], dir[3], radius;
1248                 float curvetime;
1249
1250                 unit_m4(ct->matrix);
1251
1252                 /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
1253                  * currently for paths to work it needs to go through the bevlist/displist system (ton)
1254                  */
1255
1256                 if (ct->tar->runtime.curve_cache && ct->tar->runtime.curve_cache->path && ct->tar->runtime.curve_cache->path->data) {
1257                         float quat[4];
1258                         if ((data->followflag & FOLLOWPATH_STATIC) == 0) {
1259                                 /* animated position along curve depending on time */
1260                                 Nurb *nu = cu->nurb.first;
1261                                 curvetime = cu->ctime - data->offset;
1262
1263                                 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1264                                  * but this will only work if it actually is animated...
1265                                  *
1266                                  * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1267                                  * factor, which then gets clamped to lie within 0.0 - 1.0 range
1268                                  */
1269                                 curvetime /= cu->pathlen;
1270
1271                                 if (nu && nu->flagu & CU_NURB_CYCLIC) {
1272                                         /* If the curve is cyclic, enable looping around if the time is
1273                                          * outside the bounds 0..1 */
1274                                         if ((curvetime < 0.0f) || (curvetime > 1.0f)) {
1275                                                 curvetime -= floorf(curvetime);
1276                                         }
1277                                 }
1278                                 else {
1279                                         /* The curve is not cyclic, so clamp to the begin/end points. */
1280                                         CLAMP(curvetime, 0.0f, 1.0f);
1281                                 }
1282                         }
1283                         else {
1284                                 /* fixed position along curve */
1285                                 curvetime = data->offset_fac;
1286                         }
1287
1288                         if (where_on_path(ct->tar, curvetime, vec, dir, (data->followflag & FOLLOWPATH_FOLLOW) ? quat : NULL, &radius, NULL) ) {  /* quat_pt is quat or NULL*/
1289                                 float totmat[4][4];
1290                                 unit_m4(totmat);
1291
1292                                 if (data->followflag & FOLLOWPATH_FOLLOW) {
1293                                         quat_apply_track(quat, data->trackflag, data->upflag);
1294                                         quat_to_mat4(totmat, quat);
1295                                 }
1296
1297                                 if (data->followflag & FOLLOWPATH_RADIUS) {
1298                                         float tmat[4][4], rmat[4][4];
1299                                         scale_m4_fl(tmat, radius);
1300                                         mul_m4_m4m4(rmat, tmat, totmat);
1301                                         copy_m4_m4(totmat, rmat);
1302                                 }
1303
1304                                 copy_v3_v3(totmat[3], vec);
1305
1306                                 mul_m4_m4m4(ct->matrix, ct->tar->obmat, totmat);
1307                         }
1308                 }
1309         }
1310         else if (ct)
1311                 unit_m4(ct->matrix);
1312 }
1313
1314 static void followpath_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1315 {
1316         bConstraintTarget *ct = targets->first;
1317
1318         /* only evaluate if there is a target */
1319         if (VALID_CONS_TARGET(ct)) {
1320                 float obmat[4][4];
1321                 float size[3];
1322                 bFollowPathConstraint *data = con->data;
1323
1324                 /* get Object transform (loc/rot/size) to determine transformation from path */
1325                 /* TODO: this used to be local at one point, but is probably more useful as-is */
1326                 copy_m4_m4(obmat, cob->matrix);
1327
1328                 /* get scaling of object before applying constraint */
1329                 mat4_to_size(size, cob->matrix);
1330
1331                 /* apply targetmat - containing location on path, and rotation */
1332                 mul_m4_m4m4(cob->matrix, ct->matrix, obmat);
1333
1334                 /* un-apply scaling caused by path */
1335                 if ((data->followflag & FOLLOWPATH_RADIUS) == 0) { /* XXX - assume that scale correction means that radius will have some scale error in it - Campbell */
1336                         float obsize[3];
1337
1338                         mat4_to_size(obsize, cob->matrix);
1339                         if (obsize[0])
1340                                 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1341                         if (obsize[1])
1342                                 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1343                         if (obsize[2])
1344                                 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1345                 }
1346         }
1347 }
1348
1349 static bConstraintTypeInfo CTI_FOLLOWPATH = {
1350         CONSTRAINT_TYPE_FOLLOWPATH, /* type */
1351         sizeof(bFollowPathConstraint), /* size */
1352         "Follow Path", /* name */
1353         "bFollowPathConstraint", /* struct name */
1354         NULL, /* free data */
1355         followpath_id_looper, /* id looper */
1356         NULL, /* copy data */
1357         followpath_new_data, /* new data */
1358         followpath_get_tars, /* get constraint targets */
1359         followpath_flush_tars, /* flush constraint targets */
1360         followpath_get_tarmat, /* get target matrix */
1361         followpath_evaluate, /* evaluate */
1362 };
1363
1364 /* --------- Limit Location --------- */
1365
1366
1367 static void loclimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1368 {
1369         bLocLimitConstraint *data = con->data;
1370
1371         if (data->flag & LIMIT_XMIN) {
1372                 if (cob->matrix[3][0] < data->xmin)
1373                         cob->matrix[3][0] = data->xmin;
1374         }
1375         if (data->flag & LIMIT_XMAX) {
1376                 if (cob->matrix[3][0] > data->xmax)
1377                         cob->matrix[3][0] = data->xmax;
1378         }
1379         if (data->flag & LIMIT_YMIN) {
1380                 if (cob->matrix[3][1] < data->ymin)
1381                         cob->matrix[3][1] = data->ymin;
1382         }
1383         if (data->flag & LIMIT_YMAX) {
1384                 if (cob->matrix[3][1] > data->ymax)
1385                         cob->matrix[3][1] = data->ymax;
1386         }
1387         if (data->flag & LIMIT_ZMIN) {
1388                 if (cob->matrix[3][2] < data->zmin)
1389                         cob->matrix[3][2] = data->zmin;
1390         }
1391         if (data->flag & LIMIT_ZMAX) {
1392                 if (cob->matrix[3][2] > data->zmax)
1393                         cob->matrix[3][2] = data->zmax;
1394         }
1395 }
1396
1397 static bConstraintTypeInfo CTI_LOCLIMIT = {
1398         CONSTRAINT_TYPE_LOCLIMIT, /* type */
1399         sizeof(bLocLimitConstraint), /* size */
1400         "Limit Location", /* name */
1401         "bLocLimitConstraint", /* struct name */
1402         NULL, /* free data */
1403         NULL, /* id looper */
1404         NULL, /* copy data */
1405         NULL, /* new data */
1406         NULL, /* get constraint targets */
1407         NULL, /* flush constraint targets */
1408         NULL, /* get target matrix */
1409         loclimit_evaluate, /* evaluate */
1410 };
1411
1412 /* -------- Limit Rotation --------- */
1413
1414 static void rotlimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1415 {
1416         bRotLimitConstraint *data = con->data;
1417         float loc[3];
1418         float eul[3];
1419         float size[3];
1420
1421         copy_v3_v3(loc, cob->matrix[3]);
1422         mat4_to_size(size, cob->matrix);
1423
1424         mat4_to_eulO(eul, cob->rotOrder, cob->matrix);
1425
1426         /* constraint data uses radians internally */
1427
1428         /* limiting of euler values... */
1429         if (data->flag & LIMIT_XROT) {
1430                 if (eul[0] < data->xmin)
1431                         eul[0] = data->xmin;
1432
1433                 if (eul[0] > data->xmax)
1434                         eul[0] = data->xmax;
1435         }
1436         if (data->flag & LIMIT_YROT) {
1437                 if (eul[1] < data->ymin)
1438                         eul[1] = data->ymin;
1439
1440                 if (eul[1] > data->ymax)
1441                         eul[1] = data->ymax;
1442         }
1443         if (data->flag & LIMIT_ZROT) {
1444                 if (eul[2] < data->zmin)
1445                         eul[2] = data->zmin;
1446
1447                 if (eul[2] > data->zmax)
1448                         eul[2] = data->zmax;
1449         }
1450
1451         loc_eulO_size_to_mat4(cob->matrix, loc, eul, size, cob->rotOrder);
1452 }
1453
1454 static bConstraintTypeInfo CTI_ROTLIMIT = {
1455         CONSTRAINT_TYPE_ROTLIMIT, /* type */
1456         sizeof(bRotLimitConstraint), /* size */
1457         "Limit Rotation", /* name */
1458         "bRotLimitConstraint", /* struct name */
1459         NULL, /* free data */
1460         NULL, /* id looper */
1461         NULL, /* copy data */
1462         NULL, /* new data */
1463         NULL, /* get constraint targets */
1464         NULL, /* flush constraint targets */
1465         NULL, /* get target matrix */
1466         rotlimit_evaluate, /* evaluate */
1467 };
1468
1469 /* --------- Limit Scale --------- */
1470
1471
1472 static void sizelimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1473 {
1474         bSizeLimitConstraint *data = con->data;
1475         float obsize[3], size[3];
1476
1477         mat4_to_size(size, cob->matrix);
1478         mat4_to_size(obsize, cob->matrix);
1479
1480         if (data->flag & LIMIT_XMIN) {
1481                 if (size[0] < data->xmin)
1482                         size[0] = data->xmin;
1483         }
1484         if (data->flag & LIMIT_XMAX) {
1485                 if (size[0] > data->xmax)
1486                         size[0] = data->xmax;
1487         }
1488         if (data->flag & LIMIT_YMIN) {
1489                 if (size[1] < data->ymin)
1490                         size[1] = data->ymin;
1491         }
1492         if (data->flag & LIMIT_YMAX) {
1493                 if (size[1] > data->ymax)
1494                         size[1] = data->ymax;
1495         }
1496         if (data->flag & LIMIT_ZMIN) {
1497                 if (size[2] < data->zmin)
1498                         size[2] = data->zmin;
1499         }
1500         if (data->flag & LIMIT_ZMAX) {
1501                 if (size[2] > data->zmax)
1502                         size[2] = data->zmax;
1503         }
1504
1505         if (obsize[0])
1506                 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1507         if (obsize[1])
1508                 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1509         if (obsize[2])
1510                 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1511 }
1512
1513 static bConstraintTypeInfo CTI_SIZELIMIT = {
1514         CONSTRAINT_TYPE_SIZELIMIT, /* type */
1515         sizeof(bSizeLimitConstraint), /* size */
1516         "Limit Scale", /* name */
1517         "bSizeLimitConstraint", /* struct name */
1518         NULL, /* free data */
1519         NULL, /* id looper */
1520         NULL, /* copy data */
1521         NULL, /* new data */
1522         NULL, /* get constraint targets */
1523         NULL, /* flush constraint targets */
1524         NULL, /* get target matrix */
1525         sizelimit_evaluate, /* evaluate */
1526 };
1527
1528 /* ----------- Copy Location ------------- */
1529
1530 static void loclike_new_data(void *cdata)
1531 {
1532         bLocateLikeConstraint *data = (bLocateLikeConstraint *)cdata;
1533
1534         data->flag = LOCLIKE_X | LOCLIKE_Y | LOCLIKE_Z;
1535 }
1536
1537 static void loclike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1538 {
1539         bLocateLikeConstraint *data = con->data;
1540
1541         /* target only */
1542         func(con, (ID **)&data->tar, false, userdata);
1543 }
1544
1545 static int loclike_get_tars(bConstraint *con, ListBase *list)
1546 {
1547         if (con && list) {
1548                 bLocateLikeConstraint *data = con->data;
1549                 bConstraintTarget *ct;
1550
1551                 /* standard target-getting macro for single-target constraints */
1552                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1553
1554                 return 1;
1555         }
1556
1557         return 0;
1558 }
1559
1560 static void loclike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1561 {
1562         if (con && list) {
1563                 bLocateLikeConstraint *data = con->data;
1564                 bConstraintTarget *ct = list->first;
1565
1566                 /* the following macro is used for all standard single-target constraints */
1567                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1568         }
1569 }
1570
1571 static void loclike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1572 {
1573         bLocateLikeConstraint *data = con->data;
1574         bConstraintTarget *ct = targets->first;
1575
1576         if (VALID_CONS_TARGET(ct)) {
1577                 float offset[3] = {0.0f, 0.0f, 0.0f};
1578
1579                 if (data->flag & LOCLIKE_OFFSET)
1580                         copy_v3_v3(offset, cob->matrix[3]);
1581
1582                 if (data->flag & LOCLIKE_X) {
1583                         cob->matrix[3][0] = ct->matrix[3][0];
1584
1585                         if (data->flag & LOCLIKE_X_INVERT) cob->matrix[3][0] *= -1;
1586                         cob->matrix[3][0] += offset[0];
1587                 }
1588                 if (data->flag & LOCLIKE_Y) {
1589                         cob->matrix[3][1] = ct->matrix[3][1];
1590
1591                         if (data->flag & LOCLIKE_Y_INVERT) cob->matrix[3][1] *= -1;
1592                         cob->matrix[3][1] += offset[1];
1593                 }
1594                 if (data->flag & LOCLIKE_Z) {
1595                         cob->matrix[3][2] = ct->matrix[3][2];
1596
1597                         if (data->flag & LOCLIKE_Z_INVERT) cob->matrix[3][2] *= -1;
1598                         cob->matrix[3][2] += offset[2];
1599                 }
1600         }
1601 }
1602
1603 static bConstraintTypeInfo CTI_LOCLIKE = {
1604         CONSTRAINT_TYPE_LOCLIKE, /* type */
1605         sizeof(bLocateLikeConstraint), /* size */
1606         "Copy Location", /* name */
1607         "bLocateLikeConstraint", /* struct name */
1608         NULL, /* free data */
1609         loclike_id_looper, /* id looper */
1610         NULL, /* copy data */
1611         loclike_new_data, /* new data */
1612         loclike_get_tars, /* get constraint targets */
1613         loclike_flush_tars, /* flush constraint targets */
1614         default_get_tarmat, /* get target matrix */
1615         loclike_evaluate, /* evaluate */
1616 };
1617
1618 /* ----------- Copy Rotation ------------- */
1619
1620 static void rotlike_new_data(void *cdata)
1621 {
1622         bRotateLikeConstraint *data = (bRotateLikeConstraint *)cdata;
1623
1624         data->flag = ROTLIKE_X | ROTLIKE_Y | ROTLIKE_Z;
1625 }
1626
1627 static void rotlike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1628 {
1629         bRotateLikeConstraint *data = con->data;
1630
1631         /* target only */
1632         func(con, (ID **)&data->tar, false, userdata);
1633 }
1634
1635 static int rotlike_get_tars(bConstraint *con, ListBase *list)
1636 {
1637         if (con && list) {
1638                 bRotateLikeConstraint *data = con->data;
1639                 bConstraintTarget *ct;
1640
1641                 /* standard target-getting macro for single-target constraints */
1642                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1643
1644                 return 1;
1645         }
1646
1647         return 0;
1648 }
1649
1650 static void rotlike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1651 {
1652         if (con && list) {
1653                 bRotateLikeConstraint *data = con->data;
1654                 bConstraintTarget *ct = list->first;
1655
1656                 /* the following macro is used for all standard single-target constraints */
1657                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1658         }
1659 }
1660
1661 static void rotlike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1662 {
1663         bRotateLikeConstraint *data = con->data;
1664         bConstraintTarget *ct = targets->first;
1665
1666         if (VALID_CONS_TARGET(ct)) {
1667                 float loc[3];
1668                 float eul[3], obeul[3];
1669                 float size[3];
1670
1671                 copy_v3_v3(loc, cob->matrix[3]);
1672                 mat4_to_size(size, cob->matrix);
1673
1674                 /* to allow compatible rotations, must get both rotations in the order of the owner... */
1675                 mat4_to_eulO(obeul, cob->rotOrder, cob->matrix);
1676                 /* we must get compatible eulers from the beginning because some of them can be modified below (see bug #21875) */
1677                 mat4_to_compatible_eulO(eul, obeul, cob->rotOrder, ct->matrix);
1678
1679                 if ((data->flag & ROTLIKE_X) == 0)
1680                         eul[0] = obeul[0];
1681                 else {
1682                         if (data->flag & ROTLIKE_OFFSET)
1683                                 rotate_eulO(eul, cob->rotOrder, 'X', obeul[0]);
1684
1685                         if (data->flag & ROTLIKE_X_INVERT)
1686                                 eul[0] *= -1;
1687                 }
1688
1689                 if ((data->flag & ROTLIKE_Y) == 0)
1690                         eul[1] = obeul[1];
1691                 else {
1692                         if (data->flag & ROTLIKE_OFFSET)
1693                                 rotate_eulO(eul, cob->rotOrder, 'Y', obeul[1]);
1694
1695                         if (data->flag & ROTLIKE_Y_INVERT)
1696                                 eul[1] *= -1;
1697                 }
1698
1699                 if ((data->flag & ROTLIKE_Z) == 0)
1700                         eul[2] = obeul[2];
1701                 else {
1702                         if (data->flag & ROTLIKE_OFFSET)
1703                                 rotate_eulO(eul, cob->rotOrder, 'Z', obeul[2]);
1704
1705                         if (data->flag & ROTLIKE_Z_INVERT)
1706                                 eul[2] *= -1;
1707                 }
1708
1709                 /* good to make eulers compatible again, since we don't know how much they were changed above */
1710                 compatible_eul(eul, obeul);
1711                 loc_eulO_size_to_mat4(cob->matrix, loc, eul, size, cob->rotOrder);
1712         }
1713 }
1714
1715 static bConstraintTypeInfo CTI_ROTLIKE = {
1716         CONSTRAINT_TYPE_ROTLIKE, /* type */
1717         sizeof(bRotateLikeConstraint), /* size */
1718         "Copy Rotation", /* name */
1719         "bRotateLikeConstraint", /* struct name */
1720         NULL, /* free data */
1721         rotlike_id_looper, /* id looper */
1722         NULL, /* copy data */
1723         rotlike_new_data, /* new data */
1724         rotlike_get_tars, /* get constraint targets */
1725         rotlike_flush_tars, /* flush constraint targets */
1726         default_get_tarmat, /* get target matrix */
1727         rotlike_evaluate, /* evaluate */
1728 };
1729
1730 /* ---------- Copy Scale ---------- */
1731
1732 static void sizelike_new_data(void *cdata)
1733 {
1734         bSizeLikeConstraint *data = (bSizeLikeConstraint *)cdata;
1735
1736         data->flag = SIZELIKE_X | SIZELIKE_Y | SIZELIKE_Z | SIZELIKE_MULTIPLY;
1737 }
1738
1739 static void sizelike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1740 {
1741         bSizeLikeConstraint *data = con->data;
1742
1743         /* target only */
1744         func(con, (ID **)&data->tar, false, userdata);
1745 }
1746
1747 static int sizelike_get_tars(bConstraint *con, ListBase *list)
1748 {
1749         if (con && list) {
1750                 bSizeLikeConstraint *data = con->data;
1751                 bConstraintTarget *ct;
1752
1753                 /* standard target-getting macro for single-target constraints */
1754                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1755
1756                 return 1;
1757         }
1758
1759         return 0;
1760 }
1761
1762 static void sizelike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1763 {
1764         if (con && list) {
1765                 bSizeLikeConstraint *data = con->data;
1766                 bConstraintTarget *ct = list->first;
1767
1768                 /* the following macro is used for all standard single-target constraints */
1769                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1770         }
1771 }
1772
1773 static void sizelike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1774 {
1775         bSizeLikeConstraint *data = con->data;
1776         bConstraintTarget *ct = targets->first;
1777
1778         if (VALID_CONS_TARGET(ct)) {
1779                 float obsize[3], size[3];
1780
1781                 mat4_to_size(size, ct->matrix);
1782                 mat4_to_size(obsize, cob->matrix);
1783
1784                 if (data->flag & SIZELIKE_OFFSET) {
1785                         /* Scale is a multiplicative quantity, so adding it makes no sense.
1786                          * However, the additive mode has to stay for backward compatibility. */
1787                         if (data->flag & SIZELIKE_MULTIPLY) {
1788                                 /* size[i] *= obsize[i] */
1789                                 mul_v3_v3(size, obsize);
1790                         }
1791                         else {
1792                                 /* 2.7 compatibility mode: size[i] += (obsize[i] - 1.0f) */
1793                                 add_v3_v3(size, obsize);
1794                                 add_v3_fl(size, -1.0f);
1795                         }
1796                 }
1797
1798                 if ((data->flag & SIZELIKE_X) && (obsize[0] != 0)) {
1799                         mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1800                 }
1801                 if ((data->flag & SIZELIKE_Y) && (obsize[1] != 0)) {
1802                         mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1803                 }
1804                 if ((data->flag & SIZELIKE_Z) && (obsize[2] != 0)) {
1805                         mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1806                 }
1807         }
1808 }
1809
1810 static bConstraintTypeInfo CTI_SIZELIKE = {
1811         CONSTRAINT_TYPE_SIZELIKE, /* type */
1812         sizeof(bSizeLikeConstraint), /* size */
1813         "Copy Scale", /* name */
1814         "bSizeLikeConstraint", /* struct name */
1815         NULL, /* free data */
1816         sizelike_id_looper, /* id looper */
1817         NULL, /* copy data */
1818         sizelike_new_data, /* new data */
1819         sizelike_get_tars, /* get constraint targets */
1820         sizelike_flush_tars, /* flush constraint targets */
1821         default_get_tarmat, /* get target matrix */
1822         sizelike_evaluate, /* evaluate */
1823 };
1824
1825 /* ----------- Copy Transforms ------------- */
1826
1827 static void translike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1828 {
1829         bTransLikeConstraint *data = con->data;
1830
1831         /* target only */
1832         func(con, (ID **)&data->tar, false, userdata);
1833 }
1834
1835 static int translike_get_tars(bConstraint *con, ListBase *list)
1836 {
1837         if (con && list) {
1838                 bTransLikeConstraint *data = con->data;
1839                 bConstraintTarget *ct;
1840
1841                 /* standard target-getting macro for single-target constraints */
1842                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1843
1844                 return 1;
1845         }
1846
1847         return 0;
1848 }
1849
1850 static void translike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1851 {
1852         if (con && list) {
1853                 bTransLikeConstraint *data = con->data;
1854                 bConstraintTarget *ct = list->first;
1855
1856                 /* the following macro is used for all standard single-target constraints */
1857                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1858         }
1859 }
1860
1861 static void translike_evaluate(bConstraint *UNUSED(con), bConstraintOb *cob, ListBase *targets)
1862 {
1863         bConstraintTarget *ct = targets->first;
1864
1865         if (VALID_CONS_TARGET(ct)) {
1866                 /* just copy the entire transform matrix of the target */
1867                 copy_m4_m4(cob->matrix, ct->matrix);
1868         }
1869 }
1870
1871 static bConstraintTypeInfo CTI_TRANSLIKE = {
1872         CONSTRAINT_TYPE_TRANSLIKE, /* type */
1873         sizeof(bTransLikeConstraint), /* size */
1874         "Copy Transforms", /* name */
1875         "bTransLikeConstraint", /* struct name */
1876         NULL, /* free data */
1877         translike_id_looper, /* id looper */
1878         NULL, /* copy data */
1879         NULL, /* new data */
1880         translike_get_tars, /* get constraint targets */
1881         translike_flush_tars, /* flush constraint targets */
1882         default_get_tarmat_full_bbone, /* get target matrix */
1883         translike_evaluate, /* evaluate */
1884 };
1885
1886 /* ---------- Maintain Volume ---------- */
1887
1888 static void samevolume_new_data(void *cdata)
1889 {
1890         bSameVolumeConstraint *data = (bSameVolumeConstraint *)cdata;
1891
1892         data->flag = SAMEVOL_Y;
1893         data->volume = 1.0f;
1894 }
1895
1896 static void samevolume_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
1897 {
1898         bSameVolumeConstraint *data = con->data;
1899
1900         float volume = data->volume;
1901         float fac = 1.0f, total_scale;
1902         float obsize[3];
1903
1904         mat4_to_size(obsize, cob->matrix);
1905
1906         /* calculate normalizing scale factor for non-essential values */
1907         total_scale = obsize[0] * obsize[1] * obsize[2];
1908         if (total_scale != 0)
1909                 fac = sqrtf(volume / total_scale);
1910
1911         /* apply scaling factor to the channels not being kept */
1912         switch (data->flag) {
1913                 case SAMEVOL_X:
1914                         mul_v3_fl(cob->matrix[1], fac);
1915                         mul_v3_fl(cob->matrix[2], fac);
1916                         break;
1917                 case SAMEVOL_Y:
1918                         mul_v3_fl(cob->matrix[0], fac);
1919                         mul_v3_fl(cob->matrix[2], fac);
1920                         break;
1921                 case SAMEVOL_Z:
1922                         mul_v3_fl(cob->matrix[0], fac);
1923                         mul_v3_fl(cob->matrix[1], fac);
1924                         break;
1925         }
1926 }
1927
1928 static bConstraintTypeInfo CTI_SAMEVOL = {
1929         CONSTRAINT_TYPE_SAMEVOL, /* type */
1930         sizeof(bSameVolumeConstraint), /* size */
1931         "Maintain Volume", /* name */
1932         "bSameVolumeConstraint", /* struct name */
1933         NULL, /* free data */
1934         NULL, /* id looper */
1935         NULL, /* copy data */
1936         samevolume_new_data, /* new data */
1937         NULL, /* get constraint targets */
1938         NULL, /* flush constraint targets */
1939         NULL, /* get target matrix */
1940         samevolume_evaluate, /* evaluate */
1941 };
1942
1943 /* ----------- Python Constraint -------------- */
1944
1945 static void pycon_free(bConstraint *con)
1946 {
1947         bPythonConstraint *data = con->data;
1948
1949         /* id-properties */
1950         IDP_FreeProperty(data->prop);
1951         MEM_freeN(data->prop);
1952
1953         /* multiple targets */
1954         BLI_freelistN(&data->targets);
1955 }
1956
1957 static void pycon_copy(bConstraint *con, bConstraint *srccon)
1958 {
1959         bPythonConstraint *pycon = (bPythonConstraint *)con->data;
1960         bPythonConstraint *opycon = (bPythonConstraint *)srccon->data;
1961
1962         pycon->prop = IDP_CopyProperty(opycon->prop);
1963         BLI_duplicatelist(&pycon->targets, &opycon->targets);
1964 }
1965
1966 static void pycon_new_data(void *cdata)
1967 {
1968         bPythonConstraint *data = (bPythonConstraint *)cdata;
1969
1970         /* everything should be set correctly by calloc, except for the prop->type constant.*/
1971         data->prop = MEM_callocN(sizeof(IDProperty), "PyConstraintProps");
1972         data->prop->type = IDP_GROUP;
1973 }
1974
1975 static int pycon_get_tars(bConstraint *con, ListBase *list)
1976 {
1977         if (con && list) {
1978                 bPythonConstraint *data = con->data;
1979
1980                 list->first = data->targets.first;
1981                 list->last = data->targets.last;
1982
1983                 return data->tarnum;
1984         }
1985
1986         return 0;
1987 }
1988
1989 static void pycon_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1990 {
1991         bPythonConstraint *data = con->data;
1992         bConstraintTarget *ct;
1993
1994         /* targets */
1995         for (ct = data->targets.first; ct; ct = ct->next)
1996                 func(con, (ID **)&ct->tar, false, userdata);
1997
1998         /* script */
1999         func(con, (ID **)&data->text, true, userdata);
2000 }
2001
2002 /* Whether this approach is maintained remains to be seen (aligorith) */
2003 static void pycon_get_tarmat(struct Depsgraph *UNUSED(depsgraph),
2004                              bConstraint *con, bConstraintOb *UNUSED(cob),
2005                              bConstraintTarget *ct, float UNUSED(ctime))
2006 {
2007 #ifdef WITH_PYTHON
2008         bPythonConstraint *data = con->data;
2009 #endif
2010
2011         if (VALID_CONS_TARGET(ct)) {
2012                 if (ct->tar->type == OB_CURVE && ct->tar->runtime.curve_cache == NULL) {
2013                         unit_m4(ct->matrix);
2014                         return;
2015                 }
2016
2017                 /* firstly calculate the matrix the normal way, then let the py-function override
2018                  * this matrix if it needs to do so
2019                  */
2020                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->flag, con->headtail);
2021
2022                 /* only execute target calculation if allowed */
2023 #ifdef WITH_PYTHON
2024                 if (G.f & G_FLAG_SCRIPT_AUTOEXEC)
2025                         BPY_pyconstraint_target(data, ct);
2026 #endif
2027         }
2028         else if (ct)
2029                 unit_m4(ct->matrix);
2030 }
2031
2032 static void pycon_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2033 {
2034 #ifndef WITH_PYTHON
2035         UNUSED_VARS(con, cob, targets);
2036         return;
2037 #else
2038         bPythonConstraint *data = con->data;
2039
2040         /* only evaluate in python if we're allowed to do so */
2041         if ((G.f & G_FLAG_SCRIPT_AUTOEXEC) == 0) return;
2042
2043         /* Now, run the actual 'constraint' function, which should only access the matrices */
2044         BPY_pyconstraint_exec(data, cob, targets);
2045 #endif /* WITH_PYTHON */
2046 }
2047
2048 static bConstraintTypeInfo CTI_PYTHON = {
2049         CONSTRAINT_TYPE_PYTHON, /* type */
2050         sizeof(bPythonConstraint), /* size */
2051         "Script", /* name */
2052         "bPythonConstraint", /* struct name */
2053         pycon_free, /* free data */
2054         pycon_id_looper, /* id looper */
2055         pycon_copy, /* copy data */
2056         pycon_new_data, /* new data */
2057         pycon_get_tars, /* get constraint targets */
2058         NULL, /* flush constraint targets */
2059         pycon_get_tarmat, /* get target matrix */
2060         pycon_evaluate, /* evaluate */
2061 };
2062
2063 /* ----------- Armature Constraint -------------- */
2064
2065 static void armdef_free(bConstraint *con)
2066 {
2067         bArmatureConstraint *data = con->data;
2068
2069         /* Target list. */
2070         BLI_freelistN(&data->targets);
2071 }
2072
2073 static void armdef_copy(bConstraint *con, bConstraint *srccon)
2074 {
2075         bArmatureConstraint *pcon = (bArmatureConstraint *)con->data;
2076         bArmatureConstraint *opcon = (bArmatureConstraint *)srccon->data;
2077
2078         BLI_duplicatelist(&pcon->targets, &opcon->targets);
2079 }
2080
2081 static int armdef_get_tars(bConstraint *con, ListBase *list)
2082 {
2083         if (con && list) {
2084                 bArmatureConstraint *data = con->data;
2085
2086                 *list = data->targets;
2087
2088                 return BLI_listbase_count(&data->targets);
2089         }
2090
2091         return 0;
2092 }
2093
2094 static void armdef_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2095 {
2096         bArmatureConstraint *data = con->data;
2097         bConstraintTarget *ct;
2098
2099         /* Target list. */
2100         for (ct = data->targets.first; ct; ct = ct->next) {
2101                 func(con, (ID **)&ct->tar, false, userdata);
2102         }
2103 }
2104
2105 /* Compute the world space pose matrix of the target bone. */
2106 static void armdef_get_tarmat(struct Depsgraph *UNUSED(depsgraph),
2107                               bConstraint *UNUSED(con), bConstraintOb *UNUSED(cob),
2108                               bConstraintTarget *ct, float UNUSED(ctime))
2109 {
2110         if (ct != NULL) {
2111                 if (ct->tar && ct->tar->type == OB_ARMATURE) {
2112                         bPoseChannel *pchan = BKE_pose_channel_find_name(ct->tar->pose, ct->subtarget);
2113
2114                         if (pchan != NULL) {
2115                                 mul_m4_m4m4(ct->matrix, ct->tar->obmat, pchan->pose_mat);
2116                                 return;
2117                         }
2118                 }
2119
2120                 unit_m4(ct->matrix);
2121         }
2122 }
2123
2124 static void armdef_accumulate_matrix(float obmat[4][4], float iobmat[4][4], float basemat[4][4], float bonemat[4][4], float weight, float r_sum_mat[4][4], DualQuat *r_sum_dq)
2125 {
2126         if (weight == 0.0f)
2127                 return;
2128
2129         /* Convert the selected matrix into object space. */
2130         float mat[4][4];
2131         mul_m4_series(mat, obmat, bonemat, iobmat);
2132
2133         /* Accumulate the transformation. */
2134         if (r_sum_dq != NULL) {
2135                 DualQuat tmpdq;
2136
2137                 mat4_to_dquat(&tmpdq, basemat, mat);
2138                 add_weighted_dq_dq(r_sum_dq, &tmpdq, weight);
2139         }
2140         else {
2141                 madd_m4_m4m4fl(r_sum_mat, r_sum_mat, mat, weight);
2142         }
2143 }
2144
2145 /* Compute and accumulate transformation for a single target bone. */
2146 static void armdef_accumulate_bone(bConstraintTarget *ct, bPoseChannel *pchan, const float wco[3], bool force_envelope, float *r_totweight, float r_sum_mat[4][4], DualQuat *r_sum_dq)
2147 {
2148         float iobmat[4][4], basemat[4][4], co[3];
2149         Bone *bone = pchan->bone;
2150         float weight = ct->weight;
2151
2152         /* Our object's location in target pose space. */
2153         invert_m4_m4(iobmat, ct->tar->obmat);
2154         mul_v3_m4v3(co, iobmat, wco);
2155
2156         /* Multiply by the envelope weight when appropriate. */
2157         if (force_envelope || (bone->flag & BONE_MULT_VG_ENV)) {
2158                 weight *= distfactor_to_bone(co, bone->arm_head, bone->arm_tail,
2159                                              bone->rad_head, bone->rad_tail, bone->dist);
2160         }
2161
2162         /* Compute the quaternion base matrix. */
2163         if (r_sum_dq != NULL) {
2164                 mul_m4_series(basemat, ct->tar->obmat, bone->arm_mat, iobmat);
2165         }
2166
2167         /* Find the correct bone transform matrix in world space. */
2168         if (bone->segments > 1 && bone->segments == pchan->runtime.bbone_segments) {
2169                 Mat4 *b_bone_mats = pchan->runtime.bbone_deform_mats;
2170                 float (*iamat)[4] = b_bone_mats[0].mat;
2171
2172                 /* The target is a B-Bone:
2173                  * FIRST: find the segment (see b_bone_deform in armature.c)
2174                  * Need to transform co back to bonespace, only need y. */
2175                 float y = iamat[0][1] * co[0] + iamat[1][1] * co[1] + iamat[2][1] * co[2] + iamat[3][1];
2176
2177                 /* Blend the matrix. */
2178                 int index;
2179                 float blend;
2180                 BKE_pchan_bbone_deform_segment_index(pchan, y / bone->length, &index, &blend);
2181
2182                 armdef_accumulate_matrix(ct->tar->obmat, iobmat, basemat, b_bone_mats[index + 1].mat, weight * (1.0f - blend), r_sum_mat, r_sum_dq);
2183                 armdef_accumulate_matrix(ct->tar->obmat, iobmat, basemat, b_bone_mats[index + 2].mat, weight * blend, r_sum_mat, r_sum_dq);
2184         }
2185         else {
2186                 /* Simple bone. This requires DEG_OPCODE_BONE_DONE dependency due to chan_mat. */
2187                 armdef_accumulate_matrix(ct->tar->obmat, iobmat, basemat, pchan->chan_mat, weight, r_sum_mat, r_sum_dq);
2188         }
2189
2190         /* Accumulate the weight. */
2191         *r_totweight += weight;
2192 }
2193
2194 static void armdef_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2195 {
2196         bArmatureConstraint *data = con->data;
2197
2198         float sum_mat[4][4], input_co[3];
2199         DualQuat sum_dq;
2200         float weight = 0.0f;
2201
2202         /* Prepare for blending. */
2203         zero_m4(sum_mat);
2204         memset(&sum_dq, 0, sizeof(sum_dq));
2205
2206         DualQuat *pdq = (data->flag & CONSTRAINT_ARMATURE_QUATERNION) ? &sum_dq : NULL;
2207         bool use_envelopes = (data->flag & CONSTRAINT_ARMATURE_ENVELOPE) != 0;
2208
2209         if (cob->pchan && cob->pchan->bone && !(data->flag & CONSTRAINT_ARMATURE_CUR_LOCATION)) {
2210                 /* For constraints on bones, use the rest position to bind b-bone segments
2211                  * and envelopes, to allow safely changing the bone location as if parented. */
2212                 copy_v3_v3(input_co, cob->pchan->bone->arm_head);
2213                 mul_m4_v3(cob->ob->obmat, input_co);
2214         }
2215         else {
2216                 copy_v3_v3(input_co, cob->matrix[3]);
2217         }
2218
2219         /* Process all targets. */
2220         for (bConstraintTarget *ct = targets->first; ct; ct = ct->next) {
2221                 if (ct->weight <= 0.0f) {
2222                         continue;
2223                 }
2224
2225                 /* Lookup the bone and abort if failed. */
2226                 if (!VALID_CONS_TARGET(ct) || ct->tar->type != OB_ARMATURE) {
2227                         return;
2228                 }
2229
2230                 bPoseChannel *pchan = BKE_pose_channel_find_name(ct->tar->pose, ct->subtarget);
2231
2232                 if (pchan == NULL || pchan->bone == NULL) {
2233                         return;
2234                 }
2235
2236                 armdef_accumulate_bone(ct, pchan, input_co, use_envelopes, &weight, sum_mat, pdq);
2237         }
2238
2239         /* Compute the final transform. */
2240         if (weight > 0.0f) {
2241                 if (pdq != NULL) {
2242                         normalize_dq(pdq, weight);
2243                         dquat_to_mat4(sum_mat, pdq);
2244                 }
2245                 else {
2246                         mul_m4_fl(sum_mat, 1.0f / weight);
2247                 }
2248
2249                 /* Apply the transform to the result matrix. */
2250                 mul_m4_m4m4(cob->matrix, sum_mat, cob->matrix);
2251         }
2252 }
2253
2254 static bConstraintTypeInfo CTI_ARMATURE = {
2255         CONSTRAINT_TYPE_ARMATURE, /* type */
2256         sizeof(bArmatureConstraint), /* size */
2257         "Armature", /* name */
2258         "bArmatureConstraint", /* struct name */
2259         armdef_free, /* free data */
2260         armdef_id_looper, /* id looper */
2261         armdef_copy, /* copy data */
2262         NULL, /* new data */
2263         armdef_get_tars, /* get constraint targets */
2264         NULL, /* flush constraint targets */
2265         armdef_get_tarmat, /* get target matrix */
2266         armdef_evaluate, /* evaluate */
2267 };
2268
2269 /* -------- Action Constraint ----------- */
2270
2271 static void actcon_new_data(void *cdata)
2272 {
2273         bActionConstraint *data = (bActionConstraint *)cdata;
2274
2275         /* set type to 20 (Loc X), as 0 is Rot X for backwards compatibility */
2276         data->type = 20;
2277 }
2278
2279 static void actcon_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2280 {
2281         bActionConstraint *data = con->data;
2282
2283         /* target */
2284         func(con, (ID **)&data->tar, false, userdata);
2285
2286         /* action */
2287         func(con, (ID **)&data->act, true, userdata);
2288 }
2289
2290 static int actcon_get_tars(bConstraint *con, ListBase *list)
2291 {
2292         if (con && list) {
2293                 bActionConstraint *data = con->data;
2294                 bConstraintTarget *ct;
2295
2296                 /* standard target-getting macro for single-target constraints */
2297                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2298
2299                 return 1;
2300         }
2301
2302         return 0;
2303 }
2304
2305 static void actcon_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2306 {
2307         if (con && list) {
2308                 bActionConstraint *data = con->data;
2309                 bConstraintTarget *ct = list->first;
2310
2311                 /* the following macro is used for all standard single-target constraints */
2312                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2313         }
2314 }
2315
2316 static void actcon_get_tarmat(struct Depsgraph *UNUSED(depsgraph), bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float UNUSED(ctime))
2317 {
2318         bActionConstraint *data = con->data;
2319
2320         if (VALID_CONS_TARGET(ct)) {
2321                 float tempmat[4][4], vec[3];
2322                 float s, t;
2323                 short axis;
2324
2325                 /* initialize return matrix */
2326                 unit_m4(ct->matrix);
2327
2328                 /* get the transform matrix of the target */
2329                 constraint_target_to_mat4(ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->flag, con->headtail);
2330
2331                 /* determine where in transform range target is */
2332                 /* data->type is mapped as follows for backwards compatibility:
2333                  * 00,01,02 - rotation (it used to be like this)
2334                  * 10,11,12 - scaling
2335                  * 20,21,22 - location
2336                  */
2337                 if (data->type < 10) {
2338                         /* extract rotation (is in whatever space target should be in) */
2339                         mat4_to_eul(vec, tempmat);
2340                         mul_v3_fl(vec, RAD2DEGF(1.0f)); /* rad -> deg */
2341                         axis = data->type;
2342                 }
2343                 else if (data->type < 20) {
2344                         /* extract scaling (is in whatever space target should be in) */
2345                         mat4_to_size(vec, tempmat);
2346                         axis = data->type - 10;
2347                 }
2348                 else {
2349                         /* extract location */
2350                         copy_v3_v3(vec, tempmat[3]);
2351                         axis = data->type - 20;
2352                 }
2353
2354                 BLI_assert((unsigned int)axis < 3);
2355
2356                 /* Target defines the animation */
2357                 s = (vec[axis] - data->min) / (data->max - data->min);
2358                 CLAMP(s, 0, 1);
2359                 t = (s * (data->end - data->start)) + data->start;
2360
2361                 if (G.debug & G_DEBUG)
2362                         printf("do Action Constraint %s - Ob %s Pchan %s\n", con->name, cob->ob->id.name + 2, (cob->pchan) ? cob->pchan->name : NULL);
2363
2364                 /* Get the appropriate information from the action */
2365                 if (cob->type == CONSTRAINT_OBTYPE_OBJECT || (data->flag & ACTCON_BONE_USE_OBJECT_ACTION)) {
2366                         Object workob;
2367
2368                         /* evaluate using workob */
2369                         /* FIXME: we don't have any consistent standards on limiting effects on object... */
2370                         what_does_obaction(cob->ob, &workob, NULL, data->act, NULL, t);
2371                         BKE_object_to_mat4(&workob, ct->matrix);
2372                 }
2373                 else if (cob->type == CONSTRAINT_OBTYPE_BONE) {
2374                         Object workob;
2375                         bPose pose = {{0}};
2376                         bPoseChannel *pchan, *tchan;
2377
2378                         /* make a copy of the bone of interest in the temp pose before evaluating action, so that it can get set
2379                          * - we need to manually copy over a few settings, including rotation order, otherwise this fails
2380                          */
2381                         pchan = cob->pchan;
2382
2383                         tchan = BKE_pose_channel_verify(&pose, pchan->name);
2384                         tchan->rotmode = pchan->rotmode;
2385
2386                         /* evaluate action using workob (it will only set the PoseChannel in question) */
2387                         what_does_obaction(cob->ob, &workob, &pose, data->act, pchan->name, t);
2388
2389                         /* convert animation to matrices for use here */
2390                         BKE_pchan_calc_mat(tchan);
2391                         copy_m4_m4(ct->matrix, tchan->chan_mat);
2392
2393                         /* Clean up */
2394                         BKE_pose_free_data(&pose);
2395                 }
2396                 else {
2397                         /* behavior undefined... */
2398                         puts("Error: unknown owner type for Action Constraint");
2399                 }
2400         }
2401 }
2402
2403 static void actcon_evaluate(bConstraint *UNUSED(con), bConstraintOb *cob, ListBase *targets)
2404 {
2405         bConstraintTarget *ct = targets->first;
2406
2407         if (VALID_CONS_TARGET(ct)) {
2408                 float temp[4][4];
2409
2410                 /* Nice and simple... we just need to multiply the matrices, as the get_target_matrix
2411                  * function has already taken care of everything else.
2412                  */
2413                 copy_m4_m4(temp, cob->matrix);
2414                 mul_m4_m4m4(cob->matrix, temp, ct->matrix);
2415         }
2416 }
2417
2418 static bConstraintTypeInfo CTI_ACTION = {
2419         CONSTRAINT_TYPE_ACTION, /* type */
2420         sizeof(bActionConstraint), /* size */
2421         "Action", /* name */
2422         "bActionConstraint", /* struct name */
2423         NULL, /* free data */
2424         actcon_id_looper, /* id looper */
2425         NULL, /* copy data */
2426         actcon_new_data, /* new data */
2427         actcon_get_tars, /* get constraint targets */
2428         actcon_flush_tars, /* flush constraint targets */
2429         actcon_get_tarmat, /* get target matrix */
2430         actcon_evaluate, /* evaluate */
2431 };
2432
2433 /* --------- Locked Track ---------- */
2434
2435 static void locktrack_new_data(void *cdata)
2436 {
2437         bLockTrackConstraint *data = (bLockTrackConstraint *)cdata;
2438
2439         data->trackflag = TRACK_Y;
2440         data->lockflag = LOCK_Z;
2441 }
2442
2443 static void locktrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2444 {
2445         bLockTrackConstraint *data = con->data;
2446
2447         /* target only */
2448         func(con, (ID **)&data->tar, false, userdata);
2449 }
2450
2451 static int locktrack_get_tars(bConstraint *con, ListBase *list)
2452 {
2453         if (con && list) {
2454                 bLockTrackConstraint *data = con->data;
2455                 bConstraintTarget *ct;
2456
2457                 /* the following macro is used for all standard single-target constraints */
2458                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2459
2460                 return 1;
2461         }
2462
2463         return 0;
2464 }
2465
2466 static void locktrack_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2467 {
2468         if (con && list) {
2469                 bLockTrackConstraint *data = con->data;
2470                 bConstraintTarget *ct = list->first;
2471
2472                 /* the following macro is used for all standard single-target constraints */
2473                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2474         }
2475 }
2476
2477 static void locktrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2478 {
2479         bLockTrackConstraint *data = con->data;
2480         bConstraintTarget *ct = targets->first;
2481
2482         if (VALID_CONS_TARGET(ct)) {
2483                 float vec[3], vec2[3];
2484                 float totmat[3][3];
2485                 float tmpmat[3][3];
2486                 float invmat[3][3];
2487                 float mdet;
2488
2489                 /* Vector object -> target */
2490                 sub_v3_v3v3(vec, ct->matrix[3], cob->matrix[3]);
2491                 switch (data->lockflag) {
2492                         case LOCK_X: /* LOCK X */
2493                         {
2494                                 switch (data->trackflag) {
2495                                         case TRACK_Y: /* LOCK X TRACK Y */
2496                                         {
2497                                                 /* Projection of Vector on the plane */
2498                                                 project_v3_v3v3(vec2, vec, cob->matrix[0]);
2499                                                 sub_v3_v3v3(totmat[1], vec, vec2);
2500                                                 normalize_v3(totmat[1]);
2501
2502                                                 /* the x axis is fixed */
2503                                                 normalize_v3_v3(totmat[0], cob->matrix[0]);
2504
2505                                                 /* the z axis gets mapped onto a third orthogonal vector */
2506                                                 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2507                                                 break;
2508                                         }
2509                                         case TRACK_Z: /* LOCK X TRACK Z */
2510                                         {
2511                                                 /* Projection of Vector on the plane */
2512                                                 project_v3_v3v3(vec2, vec, cob->matrix[0]);
2513                                                 sub_v3_v3v3(totmat[2], vec, vec2);
2514                                                 normalize_v3(totmat[2]);
2515
2516                                                 /* the x axis is fixed */
2517                                                 normalize_v3_v3(totmat[0], cob->matrix[0]);
2518
2519                                                 /* the z axis gets mapped onto a third orthogonal vector */
2520                                                 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2521                                                 break;
2522                                         }
2523                                         case TRACK_nY: /* LOCK X TRACK -Y */
2524                                         {
2525                                                 /* Projection of Vector on the plane */
2526                                                 project_v3_v3v3(vec2, vec, cob->matrix[0]);
2527                                                 sub_v3_v3v3(totmat[1], vec, vec2);
2528                                                 normalize_v3(totmat[1]);
2529                                                 negate_v3(totmat[1]);
2530
2531                                                 /* the x axis is fixed */
2532                                                 normalize_v3_v3(totmat[0], cob->matrix[0]);
2533
2534                                                 /* the z axis gets mapped onto a third orthogonal vector */
2535                                                 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2536                                                 break;
2537                                         }
2538                                         case TRACK_nZ: /* LOCK X TRACK -Z */
2539                                         {
2540                                                 /* Projection of Vector on the plane */
2541                                                 project_v3_v3v3(vec2, vec, cob->matrix[0]);
2542                                                 sub_v3_v3v3(totmat[2], vec, vec2);
2543                                                 normalize_v3(totmat[2]);
2544                                                 negate_v3(totmat[2]);
2545
2546                                                 /* the x axis is fixed */
2547                                                 normalize_v3_v3(totmat[0], cob->matrix[0]);
2548
2549                                                 /* the z axis gets mapped onto a third orthogonal vector */
2550                                                 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2551                                                 break;
2552                                         }
2553                                         default:
2554                                         {
2555                                                 unit_m3(totmat);
2556                                                 break;
2557                                         }
2558                                 }
2559                                 break;
2560                         }
2561                         case LOCK_Y: /* LOCK Y */
2562                         {
2563                                 switch (data->trackflag) {
2564                                         case TRACK_X: /* LOCK Y TRACK X */
2565                                         {
2566                                                 /* Projection of Vector on the plane */
2567                                                 project_v3_v3v3(vec2, vec, cob->matrix[1]);
2568                                                 sub_v3_v3v3(totmat[0], vec, vec2);
2569                                                 normalize_v3(totmat[0]);
2570
2571                                                 /* the y axis is fixed */
2572                                                 normalize_v3_v3(totmat[1], cob->matrix[1]);
2573
2574                                                 /* the z axis gets mapped onto a third orthogonal vector */
2575                                                 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2576                                                 break;
2577                                         }
2578                                         case TRACK_Z: /* LOCK Y TRACK Z */
2579                                         {
2580                                                 /* Projection of Vector on the plane */
2581                                                 project_v3_v3v3(vec2, vec, cob->matrix[1]);
2582                                                 sub_v3_v3v3(totmat[2], vec, vec2);
2583                                                 normalize_v3(totmat[2]);
2584
2585                                                 /* the y axis is fixed */
2586                                                 normalize_v3_v3(totmat[1], cob->matrix[1]);
2587
2588                                                 /* the z axis gets mapped onto a third orthogonal vector */
2589                                                 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2590                                                 break;
2591                                         }
2592                                         case TRACK_nX: /* LOCK Y TRACK -X */
2593                                         {
2594                                                 /* Projection of Vector on the plane */
2595                                                 project_v3_v3v3(vec2, vec, cob->matrix[1]);
2596                                                 sub_v3_v3v3(totmat[0], vec, vec2);
2597                                                 normalize_v3(totmat[0]);
2598                                                 negate_v3(totmat[0]);
2599
2600                                                 /* the y axis is fixed */
2601                                                 normalize_v3_v3(totmat[1], cob->matrix[1]);
2602
2603                                                 /* the z axis gets mapped onto a third orthogonal vector */
2604                                                 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2605                                                 break;
2606                                         }
2607                                         case TRACK_nZ: /* LOCK Y TRACK -Z */
2608                                         {
2609                                                 /* Projection of Vector on the plane */
2610                                                 project_v3_v3v3(vec2, vec, cob->matrix[1]);
2611                                                 sub_v3_v3v3(totmat[2], vec, vec2);
2612                                                 normalize_v3(totmat[2]);
2613                                                 negate_v3(totmat[2]);
2614
2615                                                 /* the y axis is fixed */
2616                                                 normalize_v3_v3(totmat[1], cob->matrix[1]);
2617
2618                                                 /* the z axis gets mapped onto a third orthogonal vector */
2619                                                 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2620                                                 break;
2621                                         }
2622                                         default:
2623                                         {
2624                                                 unit_m3(totmat);
2625                                                 break;
2626                                         }
2627                                 }
2628                                 break;
2629                         }
2630                         case LOCK_Z: /* LOCK Z */
2631                         {
2632                                 switch (data->trackflag) {
2633                                         case TRACK_X: /* LOCK Z TRACK X */
2634                                         {
2635                                                 /* Projection of Vector on the plane */
2636                                                 project_v3_v3v3(vec2, vec, cob->matrix[2]);
2637                                                 sub_v3_v3v3(totmat[0], vec, vec2);
2638                                                 normalize_v3(totmat[0]);
2639
2640                                                 /* the z axis is fixed */
2641                                                 normalize_v3_v3(totmat[2], cob->matrix[2]);
2642
2643                                                 /* the x axis gets mapped onto a third orthogonal vector */
2644                                                 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2645                                                 break;
2646                                         }
2647                                         case TRACK_Y: /* LOCK Z TRACK Y */
2648                                         {
2649                                                 /* Projection of Vector on the plane */
2650                                                 project_v3_v3v3(vec2, vec, cob->matrix[2]);
2651                                                 sub_v3_v3v3(totmat[1], vec, vec2);
2652                                                 normalize_v3(totmat[1]);
2653
2654                                                 /* the z axis is fixed */
2655                                                 normalize_v3_v3(totmat[2], cob->matrix[2]);
2656
2657                                                 /* the x axis gets mapped onto a third orthogonal vector */
2658                                                 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2659                                                 break;
2660                                         }
2661                                         case TRACK_nX: /* LOCK Z TRACK -X */
2662                                         {
2663                                                 /* Projection of Vector on the plane */
2664                                                 project_v3_v3v3(vec2, vec, cob->matrix[2]);
2665                                                 sub_v3_v3v3(totmat[0], vec, vec2);
2666                                                 normalize_v3(totmat[0]);
2667                                                 negate_v3(totmat[0]);
2668
2669                                                 /* the z axis is fixed */
2670                                                 normalize_v3_v3(totmat[2], cob->matrix[2]);
2671
2672                                                 /* the x axis gets mapped onto a third orthogonal vector */
2673                                                 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2674                                                 break;
2675                                         }
2676                                         case TRACK_nY: /* LOCK Z TRACK -Y */
2677                                         {
2678                                                 /* Projection of Vector on the plane */
2679                                                 project_v3_v3v3(vec2, vec, cob->matrix[2]);
2680                                                 sub_v3_v3v3(totmat[1], vec, vec2);
2681                                                 normalize_v3(totmat[1]);
2682                                                 negate_v3(totmat[1]);
2683
2684                                                 /* the z axis is fixed */
2685                                                 normalize_v3_v3(totmat[2], cob->matrix[2]);
2686
2687                                                 /* the x axis gets mapped onto a third orthogonal vector */
2688                                                 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2689                                                 break;
2690                                         }
2691                                         default:
2692                                         {
2693                                                 unit_m3(totmat);
2694                                                 break;
2695                                         }
2696                                 }
2697                                 break;
2698                         }
2699                         default:
2700                         {
2701                                 unit_m3(totmat);
2702                                 break;
2703                         }
2704                 }
2705                 /* Block to keep matrix heading */
2706                 copy_m3_m4(tmpmat, cob->matrix);
2707                 normalize_m3(tmpmat);
2708                 invert_m3_m3(invmat, tmpmat);
2709                 mul_m3_m3m3(tmpmat, totmat, invmat);
2710                 totmat[0][0] = tmpmat[0][0]; totmat[0][1] = tmpmat[0][1]; totmat[0][2] = tmpmat[0][2];
2711                 totmat[1][0] = tmpmat[1][0]; totmat[1][1] = tmpmat[1][1]; totmat[1][2] = tmpmat[1][2];
2712                 totmat[2][0] = tmpmat[2][0]; totmat[2][1] = tmpmat[2][1]; totmat[2][2] = tmpmat[2][2];
2713
2714                 mdet = determinant_m3(totmat[0][0], totmat[0][1], totmat[0][2],
2715                                       totmat[1][0], totmat[1][1], totmat[1][2],
2716                                       totmat[2][0], totmat[2][1], totmat[2][2]);
2717                 if (mdet == 0) {
2718                         unit_m3(totmat);
2719                 }
2720
2721                 /* apply out transformation to the object */
2722                 mul_m4_m3m4(cob->matrix, totmat, cob->matrix);
2723         }
2724 }
2725
2726 static bConstraintTypeInfo CTI_LOCKTRACK = {
2727         CONSTRAINT_TYPE_LOCKTRACK, /* type */
2728         sizeof(bLockTrackConstraint), /* size */
2729         "Locked Track", /* name */
2730         "bLockTrackConstraint", /* struct name */
2731         NULL, /* free data */
2732         locktrack_id_looper, /* id looper */
2733         NULL, /* copy data */
2734         locktrack_new_data, /* new data */
2735         locktrack_get_tars, /* get constraint targets */
2736         locktrack_flush_tars, /* flush constraint targets */
2737         default_get_tarmat, /* get target matrix */
2738         locktrack_evaluate, /* evaluate */
2739 };
2740
2741 /* ---------- Limit Distance Constraint ----------- */
2742
2743 static void distlimit_new_data(void *cdata)
2744 {
2745         bDistLimitConstraint *data = (bDistLimitConstraint *)cdata;
2746
2747         data->dist = 0.0f;
2748 }
2749
2750 static void distlimit_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2751 {
2752         bDistLimitConstraint *data = con->data;
2753
2754         /* target only */
2755         func(con, (ID **)&data->tar, false, userdata);
2756 }
2757
2758 static int distlimit_get_tars(bConstraint *con, ListBase *list)
2759 {
2760         if (con && list) {
2761                 bDistLimitConstraint *data = con->data;
2762                 bConstraintTarget *ct;
2763
2764                 /* standard target-getting macro for single-target constraints */
2765                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2766
2767                 return 1;
2768         }
2769
2770         return 0;
2771 }
2772
2773 static void distlimit_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2774 {
2775         if (con && list) {
2776                 bDistLimitConstraint *data = con->data;
2777                 bConstraintTarget *ct = list->first;
2778
2779                 /* the following macro is used for all standard single-target constraints */
2780                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2781         }
2782 }
2783
2784 static void distlimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2785 {
2786         bDistLimitConstraint *data = con->data;
2787         bConstraintTarget *ct = targets->first;
2788
2789         /* only evaluate if there is a target */
2790         if (VALID_CONS_TARGET(ct)) {
2791                 float dvec[3], dist, sfac = 1.0f;
2792                 short clamp_surf = 0;
2793
2794                 /* calculate our current distance from the target */
2795                 dist = len_v3v3(cob->matrix[3], ct->matrix[3]);
2796
2797                 /* set distance (flag is only set when user demands it) */
2798                 if (data->dist == 0) {
2799                         data->dist = dist;
2800
2801                         /* Write the computed distance back to the master copy if in COW evaluation. */
2802                         bConstraint *orig_con = constraint_find_original_for_update(cob, con);
2803
2804                         if (orig_con != NULL) {
2805                                 bDistLimitConstraint *orig_data = orig_con->data;
2806
2807                                 orig_data->dist = data->dist;
2808                         }
2809                 }
2810
2811                 /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
2812                 if (data->mode == LIMITDIST_OUTSIDE) {
2813                         /* if inside, then move to surface */
2814                         if (dist <= data->dist) {
2815                                 clamp_surf = 1;
2816                                 if (dist != 0.0f) sfac = data->dist / dist;
2817                         }
2818                         /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
2819                         else if (data->flag & LIMITDIST_USESOFT) {
2820                                 if (dist <= (data->dist + data->soft)) {
2821                                         /* pass */
2822                                 }
2823                         }
2824                 }
2825                 else if (data->mode == LIMITDIST_INSIDE) {
2826                         /* if outside, then move to surface */
2827                         if (dist >= data->dist) {
2828                                 clamp_surf = 1;
2829                                 if (dist != 0.0f) sfac = data->dist / dist;
2830                         }
2831                         /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
2832                         else if (data->flag & LIMITDIST_USESOFT) {
2833                                 /* FIXME: there's a problem with "jumping" when this kicks in */
2834                                 if (dist >= (data->dist - data->soft)) {
2835                                         sfac = (float)(data->soft * (1.0f - expf(-(dist - data->dist) / data->soft)) + data->dist);
2836                                         if (dist != 0.0f) sfac /= dist;
2837
2838                                         clamp_surf = 1;
2839                                 }
2840                         }
2841                 }
2842                 else {
2843                         if (IS_EQF(dist, data->dist) == 0) {
2844                                 clamp_surf = 1;
2845                                 if (dist != 0.0f) sfac = data->dist / dist;
2846                         }
2847                 }
2848
2849                 /* clamp to 'surface' (i.e. move owner so that dist == data->dist) */
2850                 if (clamp_surf) {
2851                         /* simply interpolate along line formed by target -> owner */
2852                         interp_v3_v3v3(dvec, ct->matrix[3], cob->matrix[3], sfac);
2853
2854                         /* copy new vector onto owner */
2855                         copy_v3_v3(cob->matrix[3], dvec);
2856                 }
2857         }
2858 }
2859
2860 static bConstraintTypeInfo CTI_DISTLIMIT = {
2861         CONSTRAINT_TYPE_DISTLIMIT, /* type */
2862         sizeof(bDistLimitConstraint), /* size */
2863         "Limit Distance", /* name */
2864         "bDistLimitConstraint", /* struct name */
2865         NULL, /* free data */
2866         distlimit_id_looper, /* id looper */
2867         NULL, /* copy data */
2868         distlimit_new_data, /* new data */
2869         distlimit_get_tars, /* get constraint targets */
2870         distlimit_flush_tars, /* flush constraint targets */
2871         default_get_tarmat, /* get a target matrix */
2872         distlimit_evaluate, /* evaluate */
2873 };
2874
2875 /* ---------- Stretch To ------------ */
2876
2877 static void stretchto_new_data(void *cdata)
2878 {
2879         bStretchToConstraint *data = (bStretchToConstraint *)cdata;
2880
2881         data->volmode = 0;
2882         data->plane = 0;
2883         data->orglength = 0.0;
2884         data->bulge = 1.0;
2885         data->bulge_max = 1.0f;
2886         data->bulge_min = 1.0f;
2887 }
2888
2889 static void stretchto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2890 {
2891         bStretchToConstraint *data = con->data;
2892
2893         /* target only */
2894         func(con, (ID **)&data->tar, false, userdata);
2895 }
2896
2897 static int stretchto_get_tars(bConstraint *con, ListBase *list)
2898 {
2899         if (con && list) {
2900                 bStretchToConstraint *data = con->data;
2901                 bConstraintTarget *ct;
2902
2903                 /* standard target-getting macro for single-target constraints */
2904                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2905
2906                 return 1;
2907         }
2908
2909         return 0;
2910 }
2911
2912 static void stretchto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2913 {
2914         if (con && list) {
2915                 bStretchToConstraint *data = con->data;
2916                 bConstraintTarget *ct = list->first;
2917
2918                 /* the following macro is used for all standard single-target constraints */
2919                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2920         }
2921 }
2922
2923 static void stretchto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2924 {
2925         bStretchToConstraint *data = con->data;
2926         bConstraintTarget *ct = targets->first;
2927
2928         /* only evaluate if there is a target */
2929         if (VALID_CONS_TARGET(ct)) {
2930                 float size[3], scale[3], vec[3], xx[3], zz[3], orth[3];
2931                 float totmat[3][3];
2932                 float dist, bulge;
2933
2934                 /* store scaling before destroying obmat */
2935                 mat4_to_size(size, cob->matrix);
2936
2937                 /* store X orientation before destroying obmat */
2938                 normalize_v3_v3(xx, cob->matrix[0]);
2939
2940                 /* store Z orientation before destroying obmat */
2941                 normalize_v3_v3(zz, cob->matrix[2]);
2942
2943                 /* XXX That makes the constraint buggy with asymmetrically scaled objects, see #29940. */
2944 #if 0
2945                 sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
2946                 vec[0] /= size[0];
2947                 vec[1] /= size[1];
2948                 vec[2] /= size[2];
2949
2950                 dist = normalize_v3(vec);
2951 #endif
2952
2953                 dist = len_v3v3(cob->matrix[3], ct->matrix[3]);
2954                 /* Only Y constrained object axis scale should be used, to keep same length when scaling it. */
2955                 dist /= size[1];
2956
2957                 /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
2958                 if (data->orglength == 0) {
2959                         data->orglength = dist;
2960
2961                         /* Write the computed length back to the master copy if in COW evaluation. */
2962                         bConstraint *orig_con = constraint_find_original_for_update(cob, con);
2963
2964                         if (orig_con != NULL) {
2965                                 bStretchToConstraint *orig_data = orig_con->data;
2966
2967                                 orig_data->orglength = data->orglength;
2968                         }
2969                 }
2970
2971                 scale[1] = dist / data->orglength;
2972
2973                 bulge = powf(data->orglength / dist, data->bulge);
2974
2975                 if (bulge > 1.0f) {
2976                         if (data->flag & STRETCHTOCON_USE_BULGE_MAX) {
2977                                 float bulge_max = max_ff(data->bulge_max, 1.0f);
2978                                 float hard = min_ff(bulge, bulge_max);
2979
2980                                 float range = bulge_max - 1.0f;
2981                                 float scale_fac = (range > 0.0f) ? 1.0f / range : 0.0f;
2982                                 float soft = 1.0f + range * atanf((bulge - 1.0f) * scale_fac) / (float)M_PI_2;
2983
2984                                 bulge = interpf(soft, hard, data->bulge_smooth);
2985                         }
2986                 }
2987                 if (bulge < 1.0f) {
2988                         if (data->flag & STRETCHTOCON_USE_BULGE_MIN) {
2989                                 float bulge_min = CLAMPIS(data->bulge_min, 0.0f, 1.0f);
2990                                 float hard = max_ff(bulge, bulge_min);
2991
2992                                 float range = 1.0f - bulge_min;
2993                                 float scale_fac = (range > 0.0f) ? 1.0f / range : 0.0f;
2994                                 float soft = 1.0f - range * atanf((1.0f - bulge) * scale_fac) / (float)M_PI_2;
2995
2996                                 bulge = interpf(soft, hard, data->bulge_smooth);
2997                         }
2998                 }
2999
3000                 switch (data->volmode) {
3001                         /* volume preserving scaling */
3002                         case VOLUME_XZ:
3003                                 scale[0] = sqrtf(bulge);
3004                                 scale[2] = scale[0];
3005                                 break;
3006                         case VOLUME_X:
3007                                 scale[0] = bulge;
3008                                 scale[2] = 1.0;
3009                                 break;
3010                         case VOLUME_Z:
3011                                 scale[0] = 1.0;
3012                                 scale[2] = bulge;
3013                                 break;
3014                         /* don't care for volume */
3015                         case NO_VOLUME:
3016                                 scale[0] = 1.0;
3017                                 scale[2] = 1.0;
3018                                 break;
3019                         default: /* should not happen, but in case*/
3020                                 return;
3021                 } /* switch (data->volmode) */
3022
3023                 /* Clear the object's rotation and scale */
3024                 cob->matrix[0][0] = size[0] * scale[0];
3025                 cob->matrix[0][1] = 0;
3026                 cob->matrix[0][2] = 0;
3027                 cob->matrix[1][0] = 0;
3028                 cob->matrix[1][1] = size[1] * scale[1];
3029                 cob->matrix[1][2] = 0;
3030                 cob->matrix[2][0] = 0;
3031                 cob->matrix[2][1] = 0;
3032                 cob->matrix[2][2] = size[2] * scale[2];
3033
3034                 sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
3035                 normalize_v3(vec);
3036
3037                 /* new Y aligns  object target connection*/
3038                 negate_v3_v3(totmat[1], vec);
3039                 switch (data->plane) {
3040                         case PLANE_X:
3041                                 /* build new Z vector */
3042                                 /* othogonal to "new Y" "old X! plane */
3043                                 cross_v3_v3v3(orth, vec, xx);
3044                                 normalize_v3(orth);
3045
3046                                 /* new Z*/
3047                                 copy_v3_v3(totmat[2], orth);