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