171276d118ed572b75c6d49c1c92b8c5ccf1c2cd
[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                                 VECCOPY(vec, data->grabtarget);
1131                                 mul_m4_v3(ob->obmat, vec);
1132                                 copy_m4_m4(ct->matrix, ob->obmat);
1133                                 VECCOPY(ct->matrix[3], vec);
1134                         }
1135                 }
1136                 else
1137                         unit_m4(ct->matrix);
1138         }
1139 }
1140
1141 static bConstraintTypeInfo CTI_KINEMATIC = {
1142         CONSTRAINT_TYPE_KINEMATIC, /* type */
1143         sizeof(bKinematicConstraint), /* size */
1144         "IK", /* name */
1145         "bKinematicConstraint", /* struct name */
1146         NULL, /* free data */
1147         NULL, /* relink data */
1148         kinematic_id_looper, /* id looper */
1149         NULL, /* copy data */
1150         kinematic_new_data, /* new data */
1151         kinematic_get_tars, /* get constraint targets */
1152         kinematic_flush_tars, /* flush constraint targets */
1153         kinematic_get_tarmat, /* get target matrix */
1154         NULL /* evaluate - solved as separate loop */
1155 };
1156
1157 /* -------- Follow-Path Constraint ---------- */
1158
1159 static void followpath_new_data (void *cdata)
1160 {
1161         bFollowPathConstraint *data= (bFollowPathConstraint *)cdata;
1162         
1163         data->trackflag = TRACK_Y;
1164         data->upflag = UP_Z;
1165         data->offset = 0;
1166         data->followflag = 0;
1167 }
1168
1169 static void followpath_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
1170 {
1171         bFollowPathConstraint *data= con->data;
1172         
1173         /* target only */
1174         func(con, (ID**)&data->tar, userdata);
1175 }
1176
1177 static int followpath_get_tars (bConstraint *con, ListBase *list)
1178 {
1179         if (con && list) {
1180                 bFollowPathConstraint *data= con->data;
1181                 bConstraintTarget *ct;
1182                 
1183                 /* standard target-getting macro for single-target constraints without subtargets */
1184                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
1185                 
1186                 return 1;
1187         }
1188         
1189         return 0;
1190 }
1191
1192 static void followpath_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1193 {
1194         if (con && list) {
1195                 bFollowPathConstraint *data= con->data;
1196                 bConstraintTarget *ct= list->first;
1197                 
1198                 /* the following macro is used for all standard single-target constraints */
1199                 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, nocopy)
1200         }
1201 }
1202
1203 static void followpath_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1204 {
1205         bFollowPathConstraint *data= con->data;
1206         
1207         if (VALID_CONS_TARGET(ct)) {
1208                 Curve *cu= ct->tar->data;
1209                 float q[4], vec[4], dir[3], quat[4], radius, x1;
1210                 float totmat[4][4];
1211                 float curvetime;
1212                 
1213                 unit_m4(totmat);
1214                 unit_m4(ct->matrix);
1215                 
1216                 /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
1217                  *              currently for paths to work it needs to go through the bevlist/displist system (ton) 
1218                  */
1219                 
1220                 /* only happens on reload file, but violates depsgraph still... fix! */
1221                 if (cu->path==NULL || cu->path->data==NULL)
1222                         makeDispListCurveTypes(cob->scene, ct->tar, 0);
1223                 
1224                 if (cu->path && cu->path->data) {
1225                         if ((data->followflag & FOLLOWPATH_STATIC) == 0) { 
1226                                 /* animated position along curve depending on time */
1227                                 if (cob->scene)
1228                                         curvetime= bsystem_time(cob->scene, ct->tar, cu->ctime, 0.0) - data->offset;
1229                                 else    
1230                                         curvetime= cu->ctime - data->offset;
1231                                 
1232                                 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1233                                  * but this will only work if it actually is animated... 
1234                                  *
1235                                  * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1236                                  * factor, which then gets clamped to lie within 0.0 - 1.0 range
1237                                  */
1238                                 curvetime /= cu->pathlen;
1239                                 CLAMP(curvetime, 0.0, 1.0);
1240                         }
1241                         else {
1242                                 /* fixed position along curve */
1243                                 curvetime= data->offset_fac;
1244                         }
1245                         
1246                         if ( where_on_path(ct->tar, curvetime, vec, dir, NULL, &radius, NULL) ) {
1247                                 if (data->followflag & FOLLOWPATH_FOLLOW) {
1248                                         vec_to_quat(quat, dir, (short)data->trackflag, (short)data->upflag);
1249                                         
1250                                         normalize_v3(dir);
1251                                         q[0]= (float)cos(0.5*vec[3]);
1252                                         x1= (float)sin(0.5*vec[3]);
1253                                         q[1]= -x1*dir[0];
1254                                         q[2]= -x1*dir[1];
1255                                         q[3]= -x1*dir[2];
1256                                         mul_qt_qtqt(quat, q, quat);
1257                                         
1258                                         quat_to_mat4(totmat, quat);
1259                                 }
1260                                 
1261                                 if (data->followflag & FOLLOWPATH_RADIUS) {
1262                                         float tmat[4][4], rmat[4][4];
1263                                         scale_m4_fl(tmat, radius);
1264                                         mul_m4_m4m4(rmat, totmat, tmat);
1265                                         copy_m4_m4(totmat, rmat);
1266                                 }
1267                                 
1268                                 VECCOPY(totmat[3], vec);
1269                                 
1270                                 mul_serie_m4(ct->matrix, ct->tar->obmat, totmat, NULL, NULL, NULL, NULL, NULL, NULL);
1271                         }
1272                 }
1273         }
1274         else if (ct)
1275                 unit_m4(ct->matrix);
1276 }
1277
1278 static void followpath_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1279 {
1280         bConstraintTarget *ct= targets->first;
1281         
1282         /* only evaluate if there is a target */
1283         if (VALID_CONS_TARGET(ct)) {
1284                 float obmat[4][4];
1285                 float size[3];
1286                 bFollowPathConstraint *data= con->data;
1287                 
1288                 /* get Object transform (loc/rot/size) to determine transformation from path */
1289                 // TODO: this used to be local at one point, but is probably more useful as-is
1290                 copy_m4_m4(obmat, cob->matrix);
1291                 
1292                 /* get scaling of object before applying constraint */
1293                 mat4_to_size(size, cob->matrix);
1294                 
1295                 /* apply targetmat - containing location on path, and rotation */
1296                 mul_serie_m4(cob->matrix, ct->matrix, obmat, NULL, NULL, NULL, NULL, NULL, NULL);
1297                 
1298                 /* un-apply scaling caused by path */
1299                 if ((data->followflag & FOLLOWPATH_RADIUS)==0) { /* XXX - assume that scale correction means that radius will have some scale error in it - Campbell */
1300                         float obsize[3];
1301                         
1302                         mat4_to_size( obsize,cob->matrix);
1303                         if (obsize[0])
1304                                 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1305                         if (obsize[1])
1306                                 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1307                         if (obsize[2])
1308                                 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1309                 }
1310         }
1311 }
1312
1313 static bConstraintTypeInfo CTI_FOLLOWPATH = {
1314         CONSTRAINT_TYPE_FOLLOWPATH, /* type */
1315         sizeof(bFollowPathConstraint), /* size */
1316         "Follow Path", /* name */
1317         "bFollowPathConstraint", /* struct name */
1318         NULL, /* free data */
1319         NULL, /* relink data */
1320         followpath_id_looper, /* id looper */
1321         NULL, /* copy data */
1322         followpath_new_data, /* new data */
1323         followpath_get_tars, /* get constraint targets */
1324         followpath_flush_tars, /* flush constraint targets */
1325         followpath_get_tarmat, /* get target matrix */
1326         followpath_evaluate /* evaluate */
1327 };
1328
1329 /* --------- Limit Location --------- */
1330
1331
1332 static void loclimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1333 {
1334         bLocLimitConstraint *data = con->data;
1335         
1336         if (data->flag & LIMIT_XMIN) {
1337                 if (cob->matrix[3][0] < data->xmin)
1338                         cob->matrix[3][0] = data->xmin;
1339         }
1340         if (data->flag & LIMIT_XMAX) {
1341                 if (cob->matrix[3][0] > data->xmax)
1342                         cob->matrix[3][0] = data->xmax;
1343         }
1344         if (data->flag & LIMIT_YMIN) {
1345                 if (cob->matrix[3][1] < data->ymin)
1346                         cob->matrix[3][1] = data->ymin;
1347         }
1348         if (data->flag & LIMIT_YMAX) {
1349                 if (cob->matrix[3][1] > data->ymax)
1350                         cob->matrix[3][1] = data->ymax;
1351         }
1352         if (data->flag & LIMIT_ZMIN) {
1353                 if (cob->matrix[3][2] < data->zmin) 
1354                         cob->matrix[3][2] = data->zmin;
1355         }
1356         if (data->flag & LIMIT_ZMAX) {
1357                 if (cob->matrix[3][2] > data->zmax)
1358                         cob->matrix[3][2] = data->zmax;
1359         }
1360 }
1361
1362 static bConstraintTypeInfo CTI_LOCLIMIT = {
1363         CONSTRAINT_TYPE_LOCLIMIT, /* type */
1364         sizeof(bLocLimitConstraint), /* size */
1365         "Limit Location", /* name */
1366         "bLocLimitConstraint", /* struct name */
1367         NULL, /* free data */
1368         NULL, /* relink data */
1369         NULL, /* id looper */
1370         NULL, /* copy data */
1371         NULL, /* new data */
1372         NULL, /* get constraint targets */
1373         NULL, /* flush constraint targets */
1374         NULL, /* get target matrix */
1375         loclimit_evaluate /* evaluate */
1376 };
1377
1378 /* -------- Limit Rotation --------- */
1379
1380 static void rotlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1381 {
1382         bRotLimitConstraint *data = con->data;
1383         float loc[3];
1384         float eul[3];
1385         float size[3];
1386         
1387         VECCOPY(loc, cob->matrix[3]);
1388         mat4_to_size(size, cob->matrix);
1389         
1390         mat4_to_eulO(eul, cob->rotOrder, cob->matrix);
1391         
1392         /* constraint data uses radians internally */
1393         
1394         /* limiting of euler values... */
1395         if (data->flag & LIMIT_XROT) {
1396                 if (eul[0] < data->xmin) 
1397                         eul[0] = data->xmin;
1398                         
1399                 if (eul[0] > data->xmax)
1400                         eul[0] = data->xmax;
1401         }
1402         if (data->flag & LIMIT_YROT) {
1403                 if (eul[1] < data->ymin)
1404                         eul[1] = data->ymin;
1405                         
1406                 if (eul[1] > data->ymax)
1407                         eul[1] = data->ymax;
1408         }
1409         if (data->flag & LIMIT_ZROT) {
1410                 if (eul[2] < data->zmin)
1411                         eul[2] = data->zmin;
1412                         
1413                 if (eul[2] > data->zmax)
1414                         eul[2] = data->zmax;
1415         }
1416                 
1417         loc_eulO_size_to_mat4(cob->matrix, loc, eul, size, cob->rotOrder);
1418 }
1419
1420 static bConstraintTypeInfo CTI_ROTLIMIT = {
1421         CONSTRAINT_TYPE_ROTLIMIT, /* type */
1422         sizeof(bRotLimitConstraint), /* size */
1423         "Limit Rotation", /* name */
1424         "bRotLimitConstraint", /* struct name */
1425         NULL, /* free data */
1426         NULL, /* relink data */
1427         NULL, /* id looper */
1428         NULL, /* copy data */
1429         NULL, /* new data */
1430         NULL, /* get constraint targets */
1431         NULL, /* flush constraint targets */
1432         NULL, /* get target matrix */
1433         rotlimit_evaluate /* evaluate */
1434 };
1435
1436 /* --------- Limit Scaling --------- */
1437
1438
1439 static void sizelimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1440 {
1441         bSizeLimitConstraint *data = con->data;
1442         float obsize[3], size[3];
1443         
1444         mat4_to_size( size,cob->matrix);
1445         mat4_to_size( obsize,cob->matrix);
1446         
1447         if (data->flag & LIMIT_XMIN) {
1448                 if (size[0] < data->xmin) 
1449                         size[0] = data->xmin;   
1450         }
1451         if (data->flag & LIMIT_XMAX) {
1452                 if (size[0] > data->xmax) 
1453                         size[0] = data->xmax;
1454         }
1455         if (data->flag & LIMIT_YMIN) {
1456                 if (size[1] < data->ymin) 
1457                         size[1] = data->ymin;   
1458         }
1459         if (data->flag & LIMIT_YMAX) {
1460                 if (size[1] > data->ymax) 
1461                         size[1] = data->ymax;
1462         }
1463         if (data->flag & LIMIT_ZMIN) {
1464                 if (size[2] < data->zmin) 
1465                         size[2] = data->zmin;   
1466         }
1467         if (data->flag & LIMIT_ZMAX) {
1468                 if (size[2] > data->zmax) 
1469                         size[2] = data->zmax;
1470         }
1471         
1472         if (obsize[0]) 
1473                 mul_v3_fl(cob->matrix[0], size[0]/obsize[0]);
1474         if (obsize[1]) 
1475                 mul_v3_fl(cob->matrix[1], size[1]/obsize[1]);
1476         if (obsize[2]) 
1477                 mul_v3_fl(cob->matrix[2], size[2]/obsize[2]);
1478 }
1479
1480 static bConstraintTypeInfo CTI_SIZELIMIT = {
1481         CONSTRAINT_TYPE_SIZELIMIT, /* type */
1482         sizeof(bSizeLimitConstraint), /* size */
1483         "Limit Scaling", /* name */
1484         "bSizeLimitConstraint", /* struct name */
1485         NULL, /* free data */
1486         NULL, /* relink data */
1487         NULL, /* id looper */
1488         NULL, /* copy data */
1489         NULL, /* new data */
1490         NULL, /* get constraint targets */
1491         NULL, /* flush constraint targets */
1492         NULL, /* get target matrix */
1493         sizelimit_evaluate /* evaluate */
1494 };
1495
1496 /* ----------- Copy Location ------------- */
1497
1498 static void loclike_new_data (void *cdata)
1499 {
1500         bLocateLikeConstraint *data= (bLocateLikeConstraint *)cdata;
1501         
1502         data->flag = LOCLIKE_X|LOCLIKE_Y|LOCLIKE_Z;
1503 }
1504
1505 static void loclike_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
1506 {
1507         bLocateLikeConstraint *data= con->data;
1508         
1509         /* target only */
1510         func(con, (ID**)&data->tar, userdata);
1511 }
1512
1513 static int loclike_get_tars (bConstraint *con, ListBase *list)
1514 {
1515         if (con && list) {
1516                 bLocateLikeConstraint *data= con->data;
1517                 bConstraintTarget *ct;
1518                 
1519                 /* standard target-getting macro for single-target constraints */
1520                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1521                 
1522                 return 1;
1523         }
1524         
1525         return 0;
1526 }
1527
1528 static void loclike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1529 {
1530         if (con && list) {
1531                 bLocateLikeConstraint *data= con->data;
1532                 bConstraintTarget *ct= list->first;
1533                 
1534                 /* the following macro is used for all standard single-target constraints */
1535                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1536         }
1537 }
1538
1539 static void loclike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1540 {
1541         bLocateLikeConstraint *data= con->data;
1542         bConstraintTarget *ct= targets->first;
1543         
1544         if (VALID_CONS_TARGET(ct)) {
1545                 float offset[3] = {0.0f, 0.0f, 0.0f};
1546                 
1547                 if (data->flag & LOCLIKE_OFFSET)
1548                         VECCOPY(offset, cob->matrix[3]);
1549                         
1550                 if (data->flag & LOCLIKE_X) {
1551                         cob->matrix[3][0] = ct->matrix[3][0];
1552                         
1553                         if (data->flag & LOCLIKE_X_INVERT) cob->matrix[3][0] *= -1;
1554                         cob->matrix[3][0] += offset[0];
1555                 }
1556                 if (data->flag & LOCLIKE_Y) {
1557                         cob->matrix[3][1] = ct->matrix[3][1];
1558                         
1559                         if (data->flag & LOCLIKE_Y_INVERT) cob->matrix[3][1] *= -1;
1560                         cob->matrix[3][1] += offset[1];
1561                 }
1562                 if (data->flag & LOCLIKE_Z) {
1563                         cob->matrix[3][2] = ct->matrix[3][2];
1564                         
1565                         if (data->flag & LOCLIKE_Z_INVERT) cob->matrix[3][2] *= -1;
1566                         cob->matrix[3][2] += offset[2];
1567                 }
1568         }
1569 }
1570
1571 static bConstraintTypeInfo CTI_LOCLIKE = {
1572         CONSTRAINT_TYPE_LOCLIKE, /* type */
1573         sizeof(bLocateLikeConstraint), /* size */
1574         "Copy Location", /* name */
1575         "bLocateLikeConstraint", /* struct name */
1576         NULL, /* free data */
1577         NULL, /* relink data */
1578         loclike_id_looper, /* id looper */
1579         NULL, /* copy data */
1580         loclike_new_data, /* new data */
1581         loclike_get_tars, /* get constraint targets */
1582         loclike_flush_tars, /* flush constraint targets */
1583         default_get_tarmat, /* get target matrix */
1584         loclike_evaluate /* evaluate */
1585 };
1586
1587 /* ----------- Copy Rotation ------------- */
1588
1589 static void rotlike_new_data (void *cdata)
1590 {
1591         bRotateLikeConstraint *data= (bRotateLikeConstraint *)cdata;
1592         
1593         data->flag = ROTLIKE_X|ROTLIKE_Y|ROTLIKE_Z;
1594 }
1595
1596 static void rotlike_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
1597 {
1598         bChildOfConstraint *data= con->data;
1599         
1600         /* target only */
1601         func(con, (ID**)&data->tar, userdata);
1602 }
1603
1604 static int rotlike_get_tars (bConstraint *con, ListBase *list)
1605 {
1606         if (con && list) {
1607                 bRotateLikeConstraint *data= con->data;
1608                 bConstraintTarget *ct;
1609                 
1610                 /* standard target-getting macro for single-target constraints */
1611                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1612                 
1613                 return 1;
1614         }
1615         
1616         return 0;
1617 }
1618
1619 static void rotlike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1620 {
1621         if (con && list) {
1622                 bRotateLikeConstraint *data= con->data;
1623                 bConstraintTarget *ct= list->first;
1624                 
1625                 /* the following macro is used for all standard single-target constraints */
1626                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1627         }
1628 }
1629
1630 static void rotlike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1631 {
1632         bRotateLikeConstraint *data= con->data;
1633         bConstraintTarget *ct= targets->first;
1634         
1635         if (VALID_CONS_TARGET(ct)) {
1636                 float   loc[3];
1637                 float   eul[3], obeul[3];
1638                 float   size[3];
1639                 
1640                 VECCOPY(loc, cob->matrix[3]);
1641                 mat4_to_size(size, cob->matrix);
1642                 
1643                 /* to allow compatible rotations, must get both rotations in the order of the owner... */
1644                 mat4_to_eulO(eul, cob->rotOrder, ct->matrix);
1645                 mat4_to_eulO(obeul, cob->rotOrder, cob->matrix);
1646                 
1647                 if ((data->flag & ROTLIKE_X)==0)
1648                         eul[0] = obeul[0];
1649                 else {
1650                         if (data->flag & ROTLIKE_OFFSET)
1651                                 rotate_eulO(eul, cob->rotOrder, 'X', obeul[0]);
1652                         
1653                         if (data->flag & ROTLIKE_X_INVERT)
1654                                 eul[0] *= -1;
1655                 }
1656                 
1657                 if ((data->flag & ROTLIKE_Y)==0)
1658                         eul[1] = obeul[1];
1659                 else {
1660                         if (data->flag & ROTLIKE_OFFSET)
1661                                 rotate_eulO(eul, cob->rotOrder, 'Y', obeul[1]);
1662                         
1663                         if (data->flag & ROTLIKE_Y_INVERT)
1664                                 eul[1] *= -1;
1665                 }
1666                 
1667                 if ((data->flag & ROTLIKE_Z)==0)
1668                         eul[2] = obeul[2];
1669                 else {
1670                         if (data->flag & ROTLIKE_OFFSET)
1671                                 rotate_eulO(eul, cob->rotOrder, 'Z', obeul[2]);
1672                         
1673                         if (data->flag & ROTLIKE_Z_INVERT)
1674                                 eul[2] *= -1;
1675                 }
1676                 
1677                 compatible_eul(eul, obeul);
1678                 loc_eulO_size_to_mat4(cob->matrix, loc, eul, size, cob->rotOrder);
1679         }
1680 }
1681
1682 static bConstraintTypeInfo CTI_ROTLIKE = {
1683         CONSTRAINT_TYPE_ROTLIKE, /* type */
1684         sizeof(bRotateLikeConstraint), /* size */
1685         "Copy Rotation", /* name */
1686         "bRotateLikeConstraint", /* struct name */
1687         NULL, /* free data */
1688         NULL, /* relink data */
1689         rotlike_id_looper, /* id looper */
1690         NULL, /* copy data */
1691         rotlike_new_data, /* new data */
1692         rotlike_get_tars, /* get constraint targets */
1693         rotlike_flush_tars, /* flush constraint targets */
1694         default_get_tarmat, /* get target matrix */
1695         rotlike_evaluate /* evaluate */
1696 };
1697
1698 /* ---------- Copy Scaling ---------- */
1699
1700 static void sizelike_new_data (void *cdata)
1701 {
1702         bSizeLikeConstraint *data= (bSizeLikeConstraint *)cdata;
1703         
1704         data->flag = SIZELIKE_X|SIZELIKE_Y|SIZELIKE_Z;
1705 }
1706
1707 static void sizelike_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
1708 {
1709         bSizeLikeConstraint *data= con->data;
1710         
1711         /* target only */
1712         func(con, (ID**)&data->tar, userdata);
1713 }
1714
1715 static int sizelike_get_tars (bConstraint *con, ListBase *list)
1716 {
1717         if (con && list) {
1718                 bSizeLikeConstraint *data= con->data;
1719                 bConstraintTarget *ct;
1720                 
1721                 /* standard target-getting macro for single-target constraints */
1722                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1723                 
1724                 return 1;
1725         }
1726         
1727         return 0;
1728 }
1729
1730 static void sizelike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1731 {
1732         if (con && list) {
1733                 bSizeLikeConstraint *data= con->data;
1734                 bConstraintTarget *ct= list->first;
1735                 
1736                 /* the following macro is used for all standard single-target constraints */
1737                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1738         }
1739 }
1740
1741 static void sizelike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1742 {
1743         bSizeLikeConstraint *data= con->data;
1744         bConstraintTarget *ct= targets->first;
1745         
1746         if (VALID_CONS_TARGET(ct)) {
1747                 float obsize[3], size[3];
1748                 
1749                 mat4_to_size(size, ct->matrix);
1750                 mat4_to_size(obsize, cob->matrix);
1751                 
1752                 if ((data->flag & SIZELIKE_X) && (obsize[0] != 0)) {
1753                         if (data->flag & SIZELIKE_OFFSET) {
1754                                 size[0] += (obsize[0] - 1.0f);
1755                                 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1756                         }
1757                         else
1758                                 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1759                 }
1760                 if ((data->flag & SIZELIKE_Y) && (obsize[1] != 0)) {
1761                         if (data->flag & SIZELIKE_OFFSET) {
1762                                 size[1] += (obsize[1] - 1.0f);
1763                                 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1764                         }
1765                         else
1766                                 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1767                 }
1768                 if ((data->flag & SIZELIKE_Z) && (obsize[2] != 0)) {
1769                         if (data->flag & SIZELIKE_OFFSET) {
1770                                 size[2] += (obsize[2] - 1.0f);
1771                                 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1772                         }
1773                         else
1774                                 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1775                 }
1776         }
1777 }
1778
1779 static bConstraintTypeInfo CTI_SIZELIKE = {
1780         CONSTRAINT_TYPE_SIZELIKE, /* type */
1781         sizeof(bSizeLikeConstraint), /* size */
1782         "Copy Scale", /* name */
1783         "bSizeLikeConstraint", /* struct name */
1784         NULL, /* free data */
1785         NULL, /* relink data */
1786         sizelike_id_looper, /* id looper */
1787         NULL, /* copy data */
1788         sizelike_new_data, /* new data */
1789         sizelike_get_tars, /* get constraint targets */
1790         sizelike_flush_tars, /* flush constraint targets */
1791         default_get_tarmat, /* get target matrix */
1792         sizelike_evaluate /* evaluate */
1793 };
1794
1795 /* ----------- Copy Transforms ------------- */
1796
1797 static void translike_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
1798 {
1799         bTransLikeConstraint *data= con->data;
1800         
1801         /* target only */
1802         func(con, (ID**)&data->tar, userdata);
1803 }
1804
1805 static int translike_get_tars (bConstraint *con, ListBase *list)
1806 {
1807         if (con && list) {
1808                 bTransLikeConstraint *data= con->data;
1809                 bConstraintTarget *ct;
1810                 
1811                 /* standard target-getting macro for single-target constraints */
1812                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1813                 
1814                 return 1;
1815         }
1816         
1817         return 0;
1818 }
1819
1820 static void translike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1821 {
1822         if (con && list) {
1823                 bTransLikeConstraint *data= con->data;
1824                 bConstraintTarget *ct= list->first;
1825                 
1826                 /* the following macro is used for all standard single-target constraints */
1827                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1828         }
1829 }
1830
1831 static void translike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1832 {
1833         bConstraintTarget *ct= targets->first;
1834         
1835         if (VALID_CONS_TARGET(ct)) {
1836                 /* just copy the entire transform matrix of the target */
1837                 copy_m4_m4(cob->matrix, ct->matrix);
1838         }
1839 }
1840
1841 static bConstraintTypeInfo CTI_TRANSLIKE = {
1842         CONSTRAINT_TYPE_TRANSLIKE, /* type */
1843         sizeof(bTransLikeConstraint), /* size */
1844         "Copy Transforms", /* name */
1845         "bTransLikeConstraint", /* struct name */
1846         NULL, /* free data */
1847         NULL, /* relink data */
1848         translike_id_looper, /* id looper */
1849         NULL, /* copy data */
1850         NULL, /* new data */
1851         translike_get_tars, /* get constraint targets */
1852         translike_flush_tars, /* flush constraint targets */
1853         default_get_tarmat, /* get target matrix */
1854         translike_evaluate /* evaluate */
1855 };
1856
1857 /* ---------- Maintain Volume ---------- */
1858
1859 static void samevolume_new_data (void *cdata)
1860 {
1861         bSameVolumeConstraint *data= (bSameVolumeConstraint *)cdata;
1862
1863         data->flag = SAMEVOL_Y;
1864         data->volume = 1.0f;
1865 }
1866
1867 static void samevolume_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1868 {
1869         bSameVolumeConstraint *data= con->data;
1870
1871         float volume = data->volume;
1872         float fac = 1.0f;
1873         float obsize[3];
1874
1875         mat4_to_size(obsize, cob->matrix);
1876         
1877         /* calculate normalising scale factor for non-essential values */
1878         if (obsize[data->flag] != 0) 
1879                 fac = sqrt(volume / obsize[data->flag]) / obsize[data->flag];
1880         
1881         /* apply scaling factor to the channels not being kept */
1882         switch (data->flag) {
1883                 case SAMEVOL_X:
1884                         mul_v3_fl(cob->matrix[1], fac);
1885                         mul_v3_fl(cob->matrix[2], fac);
1886                         break;
1887                 case SAMEVOL_Y:
1888                         mul_v3_fl(cob->matrix[0], fac);
1889                         mul_v3_fl(cob->matrix[2], fac);
1890                         break;
1891                 case SAMEVOL_Z:
1892                         mul_v3_fl(cob->matrix[0], fac);
1893                         mul_v3_fl(cob->matrix[1], fac);
1894                         break;
1895         }
1896 }
1897
1898 static bConstraintTypeInfo CTI_SAMEVOL = {
1899         CONSTRAINT_TYPE_SAMEVOL, /* type */
1900         sizeof(bSameVolumeConstraint), /* size */
1901         "Maintain Volume", /* name */
1902         "bSameVolumeConstraint", /* struct name */
1903         NULL, /* free data */
1904         NULL, /* relink data */
1905         NULL, /* id looper */
1906         NULL, /* copy data */
1907         samevolume_new_data, /* new data */
1908         NULL, /* get constraint targets */
1909         NULL, /* flush constraint targets */
1910         NULL, /* get target matrix */
1911         samevolume_evaluate /* evaluate */
1912 };
1913
1914 /* ----------- Python Constraint -------------- */
1915
1916 static void pycon_free (bConstraint *con)
1917 {
1918         bPythonConstraint *data= con->data;
1919         
1920         /* id-properties */
1921         IDP_FreeProperty(data->prop);
1922         MEM_freeN(data->prop);
1923         
1924         /* multiple targets */
1925         BLI_freelistN(&data->targets);
1926 }       
1927
1928 static void pycon_relink (bConstraint *con)
1929 {
1930         bPythonConstraint *data= con->data;
1931         
1932         ID_NEW(data->text);
1933 }
1934
1935 static void pycon_copy (bConstraint *con, bConstraint *srccon)
1936 {
1937         bPythonConstraint *pycon = (bPythonConstraint *)con->data;
1938         bPythonConstraint *opycon = (bPythonConstraint *)srccon->data;
1939         
1940         pycon->prop = IDP_CopyProperty(opycon->prop);
1941         BLI_duplicatelist(&pycon->targets, &opycon->targets);
1942 }
1943
1944 static void pycon_new_data (void *cdata)
1945 {
1946         bPythonConstraint *data= (bPythonConstraint *)cdata;
1947         
1948         /* everything should be set correctly by calloc, except for the prop->type constant.*/
1949         data->prop = MEM_callocN(sizeof(IDProperty), "PyConstraintProps");
1950         data->prop->type = IDP_GROUP;
1951 }
1952
1953 static int pycon_get_tars (bConstraint *con, ListBase *list)
1954 {
1955         if (con && list) {
1956                 bPythonConstraint *data= con->data;
1957                 
1958                 list->first = data->targets.first;
1959                 list->last = data->targets.last;
1960                 
1961                 return data->tarnum;
1962         }
1963         
1964         return 0;
1965 }
1966
1967 static void pycon_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
1968 {
1969         bPythonConstraint *data= con->data;
1970         bConstraintTarget *ct;
1971         
1972         /* targets */
1973         for (ct= data->targets.first; ct; ct= ct->next)
1974                 func(con, (ID**)&ct->tar, userdata);
1975                 
1976         /* script */
1977         func(con, (ID**)&data->text, userdata);
1978 }
1979
1980 /* Whether this approach is maintained remains to be seen (aligorith) */
1981 static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1982 {
1983 #ifndef DISABLE_PYTHON
1984         bPythonConstraint *data= con->data;
1985 #endif
1986
1987         if (VALID_CONS_TARGET(ct)) {
1988                 /* special exception for curves - depsgraph issues */
1989                 if (ct->tar->type == OB_CURVE) {
1990                         Curve *cu= ct->tar->data;
1991                         
1992                         /* this check is to make sure curve objects get updated on file load correctly.*/
1993                         if (cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1994                                 makeDispListCurveTypes(cob->scene, ct->tar, 0);                         
1995                 }
1996                 
1997                 /* firstly calculate the matrix the normal way, then let the py-function override
1998                  * this matrix if it needs to do so
1999                  */
2000                 constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
2001                 
2002                 /* only execute target calculation if allowed */
2003 #ifndef DISABLE_PYTHON
2004                 if (G.f & G_SCRIPT_AUTOEXEC)
2005                         BPY_pyconstraint_target(data, ct);
2006 #endif
2007         }
2008         else if (ct)
2009                 unit_m4(ct->matrix);
2010 }
2011
2012 static void pycon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2013 {
2014 #ifdef DISABLE_PYTHON
2015         return;
2016 #else
2017         bPythonConstraint *data= con->data;
2018         
2019         /* only evaluate in python if we're allowed to do so */
2020         if ((G.f & G_SCRIPT_AUTOEXEC)==0)  return;
2021         
2022 /* currently removed, until I this can be re-implemented for multiple targets */
2023 #if 0
2024         /* Firstly, run the 'driver' function which has direct access to the objects involved 
2025          * Technically, this is potentially dangerous as users may abuse this and cause dependency-problems,
2026          * but it also allows certain 'clever' rigging hacks to work.
2027          */
2028         BPY_pyconstraint_driver(data, cob, targets);
2029 #endif
2030         
2031         /* Now, run the actual 'constraint' function, which should only access the matrices */
2032         BPY_pyconstraint_eval(data, cob, targets);
2033 #endif /* DISABLE_PYTHON */
2034 }
2035
2036 static bConstraintTypeInfo CTI_PYTHON = {
2037         CONSTRAINT_TYPE_PYTHON, /* type */
2038         sizeof(bPythonConstraint), /* size */
2039         "Script", /* name */
2040         "bPythonConstraint", /* struct name */
2041         pycon_free, /* free data */
2042         pycon_relink, /* relink data */
2043         pycon_id_looper, /* id looper */
2044         pycon_copy, /* copy data */
2045         pycon_new_data, /* new data */
2046         pycon_get_tars, /* get constraint targets */
2047         NULL, /* flush constraint targets */
2048         pycon_get_tarmat, /* get target matrix */
2049         pycon_evaluate /* evaluate */
2050 };
2051
2052 /* -------- Action Constraint ----------- */
2053
2054 static void actcon_relink (bConstraint *con)
2055 {
2056         bActionConstraint *data= con->data;
2057         ID_NEW(data->act);
2058 }
2059
2060 static void actcon_new_data (void *cdata)
2061 {
2062         bActionConstraint *data= (bActionConstraint *)cdata;
2063         
2064         /* set type to 20 (Loc X), as 0 is Rot X for backwards compatability */
2065         data->type = 20;
2066 }
2067
2068 static void actcon_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
2069 {
2070         bActionConstraint *data= con->data;
2071         
2072         /* target */
2073         func(con, (ID**)&data->tar, userdata);
2074         
2075         /* action */
2076         func(con, (ID**)&data->act, userdata);
2077 }
2078
2079 static int actcon_get_tars (bConstraint *con, ListBase *list)
2080 {
2081         if (con && list) {
2082                 bActionConstraint *data= con->data;
2083                 bConstraintTarget *ct;
2084                 
2085                 /* standard target-getting macro for single-target constraints */
2086                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2087                 
2088                 return 1;
2089         }
2090         
2091         return 0;
2092 }
2093
2094 static void actcon_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2095 {
2096         if (con && list) {
2097                 bActionConstraint *data= con->data;
2098                 bConstraintTarget *ct= list->first;
2099                 
2100                 /* the following macro is used for all standard single-target constraints */
2101                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2102         }
2103 }
2104
2105 static void actcon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
2106 {
2107         extern void chan_calc_mat(bPoseChannel *chan);
2108         bActionConstraint *data = con->data;
2109         
2110         if (VALID_CONS_TARGET(ct)) {
2111                 float tempmat[4][4], vec[3];
2112                 float s, t;
2113                 short axis;
2114                 
2115                 /* initialise return matrix */
2116                 unit_m4(ct->matrix);
2117                 
2118                 /* get the transform matrix of the target */
2119                 constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
2120                 
2121                 /* determine where in transform range target is */
2122                 /* data->type is mapped as follows for backwards compatability:
2123                  *      00,01,02        - rotation (it used to be like this)
2124                  *      10,11,12        - scaling
2125                  *      20,21,22        - location
2126                  */
2127                 if (data->type < 10) {
2128                         /* extract rotation (is in whatever space target should be in) */
2129                         mat4_to_eul(vec, tempmat);
2130                         vec[0] *= (float)(180.0/M_PI);
2131                         vec[1] *= (float)(180.0/M_PI);
2132                         vec[2] *= (float)(180.0/M_PI);
2133                         axis= data->type;
2134                 }
2135                 else if (data->type < 20) {
2136                         /* extract scaling (is in whatever space target should be in) */
2137                         mat4_to_size(vec, tempmat);
2138                         axis= data->type - 10;
2139                 }
2140                 else {
2141                         /* extract location */
2142                         VECCOPY(vec, tempmat[3]);
2143                         axis= data->type - 20;
2144                 }
2145                 
2146                 /* Target defines the animation */
2147                 s = (vec[axis]-data->min) / (data->max-data->min);
2148                 CLAMP(s, 0, 1);
2149                 t = (s * (data->end-data->start)) + data->start;
2150                 
2151                 if (G.f & G_DEBUG)
2152                         printf("do Action Constraint %s - Ob %s Pchan %s \n", con->name, cob->ob->id.name+2, (cob->pchan)?cob->pchan->name:NULL);
2153                 
2154                 /* Get the appropriate information from the action */
2155                 if (cob->type == CONSTRAINT_OBTYPE_BONE) {
2156                         Object workob;
2157                         bPose *pose;
2158                         bPoseChannel *pchan, *tchan;
2159                         
2160                         /* make a temporary pose and evaluate using that */
2161                         pose = MEM_callocN(sizeof(bPose), "pose");
2162                         
2163                         /* make a copy of the bone of interest in the temp pose before evaluating action, so that it can get set 
2164                          *      - we need to manually copy over a few settings, including rotation order, otherwise this fails
2165                          */
2166                         pchan = cob->pchan;
2167                         
2168                         tchan= verify_pose_channel(pose, pchan->name);
2169                         tchan->rotmode= pchan->rotmode;
2170                         
2171                         /* evaluate action using workob (it will only set the PoseChannel in question) */
2172                         what_does_obaction(cob->scene, cob->ob, &workob, pose, data->act, pchan->name, t);
2173                         
2174                         /* convert animation to matrices for use here */
2175                         chan_calc_mat(tchan);
2176                         copy_m4_m4(ct->matrix, tchan->chan_mat);
2177                         
2178                         /* Clean up */
2179                         free_pose(pose);
2180                 }
2181                 else if (cob->type == CONSTRAINT_OBTYPE_OBJECT) {
2182                         Object workob;
2183                         
2184                         /* evaluate using workob */
2185                         // FIXME: we don't have any consistent standards on limiting effects on object...
2186                         what_does_obaction(cob->scene, cob->ob, &workob, NULL, data->act, NULL, t);
2187                         object_to_mat4(&workob, ct->matrix);
2188                 }
2189                 else {
2190                         /* behaviour undefined... */
2191                         puts("Error: unknown owner type for Action Constraint");
2192                 }
2193         }
2194 }
2195
2196 static void actcon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2197 {
2198         bConstraintTarget *ct= targets->first;
2199         
2200         if (VALID_CONS_TARGET(ct)) {
2201                 float temp[4][4];
2202                 
2203                 /* Nice and simple... we just need to multiply the matrices, as the get_target_matrix
2204                  * function has already taken care of everything else.
2205                  */
2206                 copy_m4_m4(temp, cob->matrix);
2207                 mul_m4_m4m4(cob->matrix, ct->matrix, temp);
2208         }
2209 }
2210
2211 static bConstraintTypeInfo CTI_ACTION = {
2212         CONSTRAINT_TYPE_ACTION, /* type */
2213         sizeof(bActionConstraint), /* size */
2214         "Action", /* name */
2215         "bActionConstraint", /* struct name */
2216         NULL, /* free data */
2217         actcon_relink, /* relink data */
2218         actcon_id_looper, /* id looper */
2219         NULL, /* copy data */
2220         actcon_new_data, /* new data */
2221         actcon_get_tars, /* get constraint targets */
2222         actcon_flush_tars, /* flush constraint targets */
2223         actcon_get_tarmat, /* get target matrix */
2224         actcon_evaluate /* evaluate */
2225 };
2226
2227 /* --------- Locked Track ---------- */
2228
2229 static void locktrack_new_data (void *cdata)
2230 {
2231         bLockTrackConstraint *data= (bLockTrackConstraint *)cdata;
2232         
2233         data->trackflag = TRACK_Y;
2234         data->lockflag = LOCK_Z;
2235 }       
2236
2237 static void locktrack_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
2238 {
2239         bLockTrackConstraint *data= con->data;
2240         
2241         /* target only */
2242         func(con, (ID**)&data->tar, userdata);
2243 }
2244
2245 static int locktrack_get_tars (bConstraint *con, ListBase *list)
2246 {
2247         if (con && list) {
2248                 bLockTrackConstraint *data= con->data;
2249                 bConstraintTarget *ct;
2250                 
2251                 /* the following macro is used for all standard single-target constraints */
2252                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2253                 
2254                 return 1;
2255         }
2256         
2257         return 0;
2258 }
2259
2260 static void locktrack_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2261 {
2262         if (con && list) {
2263                 bLockTrackConstraint *data= con->data;
2264                 bConstraintTarget *ct= list->first;
2265                 
2266                 /* the following macro is used for all standard single-target constraints */
2267                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2268         }
2269 }
2270
2271 static void locktrack_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2272 {
2273         bLockTrackConstraint *data= con->data;
2274         bConstraintTarget *ct= targets->first;
2275         
2276         if (VALID_CONS_TARGET(ct)) {
2277                 float vec[3],vec2[3];
2278                 float totmat[3][3];
2279                 float tmpmat[3][3];
2280                 float invmat[3][3];
2281                 float tmat[4][4];
2282                 float mdet;
2283                 
2284                 /* Vector object -> target */
2285                 sub_v3_v3v3(vec, ct->matrix[3], cob->matrix[3]);
2286                 switch (data->lockflag){
2287                 case LOCK_X: /* LOCK X */
2288                 {
2289                         switch (data->trackflag) {
2290                                 case TRACK_Y: /* LOCK X TRACK Y */
2291                                 {
2292                                         /* Projection of Vector on the plane */
2293                                         project_v3_v3v3(vec2, vec, cob->matrix[0]);
2294                                         sub_v3_v3v3(totmat[1], vec, vec2);
2295                                         normalize_v3(totmat[1]);
2296                                         
2297                                         /* the x axis is fixed */
2298                                         totmat[0][0] = cob->matrix[0][0];
2299                                         totmat[0][1] = cob->matrix[0][1];
2300                                         totmat[0][2] = cob->matrix[0][2];
2301                                         normalize_v3(totmat[0]);
2302                                         
2303                                         /* the z axis gets mapped onto a third orthogonal vector */
2304                                         cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2305                                 }
2306                                         break;
2307                                 case TRACK_Z: /* LOCK X TRACK Z */
2308                                 {
2309                                         /* Projection of Vector on the plane */
2310                                         project_v3_v3v3(vec2, vec, cob->matrix[0]);
2311                                         sub_v3_v3v3(totmat[2], vec, vec2);
2312                                         normalize_v3(totmat[2]);
2313                                         
2314                                         /* the x axis is fixed */
2315                                         totmat[0][0] = cob->matrix[0][0];
2316                                         totmat[0][1] = cob->matrix[0][1];
2317                                         totmat[0][2] = cob->matrix[0][2];
2318                                         normalize_v3(totmat[0]);
2319                                         
2320                                         /* the z axis gets mapped onto a third orthogonal vector */
2321                                         cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2322                                 }
2323                                         break;
2324                                 case TRACK_nY: /* LOCK X TRACK -Y */
2325                                 {
2326                                         /* Projection of Vector on the plane */
2327                                         project_v3_v3v3(vec2, vec, cob->matrix[0]);
2328                                         sub_v3_v3v3(totmat[1], vec, vec2);
2329                                         normalize_v3(totmat[1]);
2330                                         negate_v3(totmat[1]);
2331                                         
2332                                         /* the x axis is fixed */
2333                                         totmat[0][0] = cob->matrix[0][0];
2334                                         totmat[0][1] = cob->matrix[0][1];
2335                                         totmat[0][2] = cob->matrix[0][2];
2336                                         normalize_v3(totmat[0]);
2337                                         
2338                                         /* the z axis gets mapped onto a third orthogonal vector */
2339                                         cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2340                                 }
2341                                         break;
2342                                 case TRACK_nZ: /* LOCK X TRACK -Z */
2343                                 {
2344                                         /* Projection of Vector on the plane */
2345                                         project_v3_v3v3(vec2, vec, cob->matrix[0]);
2346                                         sub_v3_v3v3(totmat[2], vec, vec2);
2347                                         normalize_v3(totmat[2]);
2348                                         negate_v3(totmat[2]);
2349                                                 
2350                                         /* the x axis is fixed */
2351                                         totmat[0][0] = cob->matrix[0][0];
2352                                         totmat[0][1] = cob->matrix[0][1];
2353                                         totmat[0][2] = cob->matrix[0][2];
2354                                         normalize_v3(totmat[0]);
2355                                                 
2356                                         /* the z axis gets mapped onto a third orthogonal vector */
2357                                         cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2358                                 }
2359                                         break;
2360                                 default:
2361                                 {
2362                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2363                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2364                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2365                                 }
2366                                         break;
2367                         }
2368                 }
2369                         break;
2370                 case LOCK_Y: /* LOCK Y */
2371                 {
2372                         switch (data->trackflag) {
2373                                 case TRACK_X: /* LOCK Y TRACK X */
2374                                 {
2375                                         /* Projection of Vector on the plane */
2376                                         project_v3_v3v3(vec2, vec, cob->matrix[1]);
2377                                         sub_v3_v3v3(totmat[0], vec, vec2);
2378                                         normalize_v3(totmat[0]);
2379                                         
2380                                         /* the y axis is fixed */
2381                                         totmat[1][0] = cob->matrix[1][0];
2382                                         totmat[1][1] = cob->matrix[1][1];
2383                                         totmat[1][2] = cob->matrix[1][2];
2384                                         normalize_v3(totmat[1]);
2385                                         
2386                                         /* the z axis gets mapped onto a third orthogonal vector */
2387                                         cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2388                                 }
2389                                         break;
2390                                 case TRACK_Z: /* LOCK Y TRACK Z */
2391                                 {
2392                                         /* Projection of Vector on the plane */
2393                                         project_v3_v3v3(vec2, vec, cob->matrix[1]);
2394                                         sub_v3_v3v3(totmat[2], vec, vec2);
2395                                         normalize_v3(totmat[2]);
2396                                         
2397                                         /* the y axis is fixed */
2398                                         totmat[1][0] = cob->matrix[1][0];
2399                                         totmat[1][1] = cob->matrix[1][1];
2400                                         totmat[1][2] = cob->matrix[1][2];
2401                                         normalize_v3(totmat[1]);
2402                                         
2403                                         /* the z axis gets mapped onto a third orthogonal vector */
2404                                         cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2405                                 }
2406                                         break;
2407                                 case TRACK_nX: /* LOCK Y TRACK -X */
2408                                 {
2409                                         /* Projection of Vector on the plane */
2410                                         project_v3_v3v3(vec2, vec, cob->matrix[1]);
2411                                         sub_v3_v3v3(totmat[0], vec, vec2);
2412                                         normalize_v3(totmat[0]);
2413                                         negate_v3(totmat[0]);
2414                                         
2415                                         /* the y axis is fixed */
2416                                         totmat[1][0] = cob->matrix[1][0];
2417                                         totmat[1][1] = cob->matrix[1][1];
2418                                         totmat[1][2] = cob->matrix[1][2];
2419                                         normalize_v3(totmat[1]);
2420                                         
2421                                         /* the z axis gets mapped onto a third orthogonal vector */
2422                                         cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
2423                                 }
2424                                         break;
2425                                 case TRACK_nZ: /* LOCK Y TRACK -Z */
2426                                 {
2427                                         /* Projection of Vector on the plane */
2428                                         project_v3_v3v3(vec2, vec, cob->matrix[1]);
2429                                         sub_v3_v3v3(totmat[2], vec, vec2);
2430                                         normalize_v3(totmat[2]);
2431                                         negate_v3(totmat[2]);
2432                                         
2433                                         /* the y axis is fixed */
2434                                         totmat[1][0] = cob->matrix[1][0];
2435                                         totmat[1][1] = cob->matrix[1][1];
2436                                         totmat[1][2] = cob->matrix[1][2];
2437                                         normalize_v3(totmat[1]);
2438                                         
2439                                         /* the z axis gets mapped onto a third orthogonal vector */
2440                                         cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2441                                 }
2442                                         break;
2443                                 default:
2444                                 {
2445                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2446                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2447                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2448                                 }
2449                                         break;
2450                         }
2451                 }
2452                         break;
2453                 case LOCK_Z: /* LOCK Z */
2454                 {
2455                         switch (data->trackflag) {
2456                                 case TRACK_X: /* LOCK Z TRACK X */
2457                                 {
2458                                         /* Projection of Vector on the plane */
2459                                         project_v3_v3v3(vec2, vec, cob->matrix[2]);
2460                                         sub_v3_v3v3(totmat[0], vec, vec2);
2461                                         normalize_v3(totmat[0]);
2462                                         
2463                                         /* the z axis is fixed */
2464                                         totmat[2][0] = cob->matrix[2][0];
2465                                         totmat[2][1] = cob->matrix[2][1];
2466                                         totmat[2][2] = cob->matrix[2][2];
2467                                         normalize_v3(totmat[2]);
2468                                         
2469                                         /* the x axis gets mapped onto a third orthogonal vector */
2470                                         cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2471                                 }
2472                                         break;
2473                                 case TRACK_Y: /* LOCK Z TRACK Y */
2474                                 {
2475                                         /* Projection of Vector on the plane */
2476                                         project_v3_v3v3(vec2, vec, cob->matrix[2]);
2477                                         sub_v3_v3v3(totmat[1], vec, vec2);
2478                                         normalize_v3(totmat[1]);
2479                                         
2480                                         /* the z axis is fixed */
2481                                         totmat[2][0] = cob->matrix[2][0];
2482                                         totmat[2][1] = cob->matrix[2][1];
2483                                         totmat[2][2] = cob->matrix[2][2];
2484                                         normalize_v3(totmat[2]);
2485                                                 
2486                                         /* the x axis gets mapped onto a third orthogonal vector */
2487                                         cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2488                                 }
2489                                         break;
2490                                 case TRACK_nX: /* LOCK Z TRACK -X */
2491                                 {
2492                                         /* Projection of Vector on the plane */
2493                                         project_v3_v3v3(vec2, vec, cob->matrix[2]);
2494                                         sub_v3_v3v3(totmat[0], vec, vec2);
2495                                         normalize_v3(totmat[0]);
2496                                         negate_v3(totmat[0]);
2497                                         
2498                                         /* the z axis is fixed */
2499                                         totmat[2][0] = cob->matrix[2][0];
2500                                         totmat[2][1] = cob->matrix[2][1];
2501                                         totmat[2][2] = cob->matrix[2][2];
2502                                         normalize_v3(totmat[2]);
2503                                         
2504                                         /* the x axis gets mapped onto a third orthogonal vector */
2505                                         cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
2506                                 }
2507                                         break;
2508                                 case TRACK_nY: /* LOCK Z TRACK -Y */
2509                                 {
2510                                         /* Projection of Vector on the plane */
2511                                         project_v3_v3v3(vec2, vec, cob->matrix[2]);
2512                                         sub_v3_v3v3(totmat[1], vec, vec2);
2513                                         normalize_v3(totmat[1]);
2514                                         negate_v3(totmat[1]);
2515                                         
2516                                         /* the z axis is fixed */
2517                                         totmat[2][0] = cob->matrix[2][0];
2518                                         totmat[2][1] = cob->matrix[2][1];
2519                                         totmat[2][2] = cob->matrix[2][2];
2520                                         normalize_v3(totmat[2]);
2521                                                 
2522                                         /* the x axis gets mapped onto a third orthogonal vector */
2523                                         cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
2524                                 }
2525                                         break;
2526                                 default:
2527                                 {
2528                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2529                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2530                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2531                                 }
2532                                         break;
2533                         }
2534                 }
2535                         break;
2536                 default:
2537                 {
2538                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2539                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2540                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2541                 }
2542                         break;
2543                 }
2544                 /* Block to keep matrix heading */
2545                 tmpmat[0][0] = cob->matrix[0][0];tmpmat[0][1] = cob->matrix[0][1];tmpmat[0][2] = cob->matrix[0][2];
2546                 tmpmat[1][0] = cob->matrix[1][0];tmpmat[1][1] = cob->matrix[1][1];tmpmat[1][2] = cob->matrix[1][2];
2547                 tmpmat[2][0] = cob->matrix[2][0];tmpmat[2][1] = cob->matrix[2][1];tmpmat[2][2] = cob->matrix[2][2];
2548                 normalize_v3(tmpmat[0]);
2549                 normalize_v3(tmpmat[1]);
2550                 normalize_v3(tmpmat[2]);
2551                 invert_m3_m3(invmat, tmpmat);
2552                 mul_m3_m3m3(tmpmat, totmat, invmat);
2553                 totmat[0][0] = tmpmat[0][0];totmat[0][1] = tmpmat[0][1];totmat[0][2] = tmpmat[0][2];
2554                 totmat[1][0] = tmpmat[1][0];totmat[1][1] = tmpmat[1][1];totmat[1][2] = tmpmat[1][2];
2555                 totmat[2][0] = tmpmat[2][0];totmat[2][1] = tmpmat[2][1];totmat[2][2] = tmpmat[2][2];
2556                 
2557                 copy_m4_m4(tmat, cob->matrix);
2558                 
2559                 mdet = determinant_m3(  totmat[0][0],totmat[0][1],totmat[0][2],
2560                                                 totmat[1][0],totmat[1][1],totmat[1][2],
2561                                                 totmat[2][0],totmat[2][1],totmat[2][2]);
2562                 if (mdet==0) {
2563                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2564                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2565                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2566                 }
2567                 
2568                 /* apply out transformaton to the object */
2569                 mul_m4_m3m4(cob->matrix, totmat, tmat);
2570         }
2571 }
2572
2573 static bConstraintTypeInfo CTI_LOCKTRACK = {
2574         CONSTRAINT_TYPE_LOCKTRACK, /* type */
2575         sizeof(bLockTrackConstraint), /* size */
2576         "Locked Track", /* name */
2577         "bLockTrackConstraint", /* struct name */
2578         NULL, /* free data */
2579         NULL, /* relink data */
2580         locktrack_id_looper, /* id looper */
2581         NULL, /* copy data */
2582         locktrack_new_data, /* new data */
2583         locktrack_get_tars, /* get constraint targets */
2584         locktrack_flush_tars, /* flush constraint targets */
2585         default_get_tarmat, /* get target matrix */
2586         locktrack_evaluate /* evaluate */
2587 };
2588
2589 /* ---------- Limit Distance Constraint ----------- */
2590
2591 static void distlimit_new_data (void *cdata)
2592 {
2593         bDistLimitConstraint *data= (bDistLimitConstraint *)cdata;
2594         
2595         data->dist= 0.0f;
2596 }
2597
2598 static void distlimit_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
2599 {
2600         bDistLimitConstraint *data= con->data;
2601         
2602         /* target only */
2603         func(con, (ID**)&data->tar, userdata);
2604 }
2605
2606 static int distlimit_get_tars (bConstraint *con, ListBase *list)
2607 {
2608         if (con && list) {
2609                 bDistLimitConstraint *data= con->data;
2610                 bConstraintTarget *ct;
2611                 
2612                 /* standard target-getting macro for single-target constraints */
2613                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2614                 
2615                 return 1;
2616         }
2617         
2618         return 0;
2619 }
2620
2621 static void distlimit_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2622 {
2623         if (con && list) {
2624                 bDistLimitConstraint *data= con->data;
2625                 bConstraintTarget *ct= list->first;
2626                 
2627                 /* the following macro is used for all standard single-target constraints */
2628                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2629         }
2630 }
2631
2632 static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2633 {
2634         bDistLimitConstraint *data= con->data;
2635         bConstraintTarget *ct= targets->first;
2636         
2637         /* only evaluate if there is a target */
2638         if (VALID_CONS_TARGET(ct)) {
2639                 float dvec[3], dist=0.0f, sfac=1.0f;
2640                 short clamp_surf= 0;
2641                 
2642                 /* calculate our current distance from the target */
2643                 dist= len_v3v3(cob->matrix[3], ct->matrix[3]);
2644                 
2645                 /* set distance (flag is only set when user demands it) */
2646                 if (data->dist == 0)
2647                         data->dist= dist;
2648                 
2649                 /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
2650                 if (data->mode == LIMITDIST_OUTSIDE) {
2651                         /* if inside, then move to surface */
2652                         if (dist <= data->dist) {
2653                                 clamp_surf= 1;
2654                                 sfac= data->dist / dist;
2655                         }
2656                         /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
2657                         else if (data->flag & LIMITDIST_USESOFT) {
2658                                 if (dist <= (data->dist + data->soft)) {
2659                                         
2660                                 }
2661                         }
2662                 }
2663                 else if (data->mode == LIMITDIST_INSIDE) {
2664                         /* if outside, then move to surface */
2665                         if (dist >= data->dist) {
2666                                 clamp_surf= 1;
2667                                 sfac= data->dist / dist;
2668                         }
2669                         /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
2670                         else if (data->flag & LIMITDIST_USESOFT) {
2671                                 // FIXME: there's a problem with "jumping" when this kicks in
2672                                 if (dist >= (data->dist - data->soft)) {
2673                                         sfac = (float)( data->soft*(1.0 - exp(-(dist - data->dist)/data->soft)) + data->dist );
2674                                         sfac /= dist;
2675                                         
2676                                         clamp_surf= 1;
2677                                 }
2678                         }
2679                 }
2680                 else {
2681                         if (IS_EQ(dist, data->dist)==0) {
2682                                 clamp_surf= 1;
2683                                 sfac= data->dist / dist;
2684                         }
2685                 }
2686                 
2687                 /* clamp to 'surface' (i.e. move owner so that dist == data->dist) */
2688                 if (clamp_surf) {
2689                         /* simply interpolate along line formed by target -> owner */
2690                         interp_v3_v3v3(dvec, ct->matrix[3], cob->matrix[3], sfac);
2691                         
2692                         /* copy new vector onto owner */
2693                         VECCOPY(cob->matrix[3], dvec);
2694                 }
2695         }
2696 }
2697
2698 static bConstraintTypeInfo CTI_DISTLIMIT = {
2699         CONSTRAINT_TYPE_DISTLIMIT, /* type */
2700         sizeof(bDistLimitConstraint), /* size */
2701         "Limit Distance", /* name */
2702         "bDistLimitConstraint", /* struct name */
2703         NULL, /* free data */
2704         NULL, /* relink data */
2705         distlimit_id_looper, /* id looper */
2706         NULL, /* copy data */
2707         distlimit_new_data, /* new data */
2708         distlimit_get_tars, /* get constraint targets */
2709         distlimit_flush_tars, /* flush constraint targets */
2710         default_get_tarmat, /* get a target matrix */
2711         distlimit_evaluate /* evaluate */
2712 };
2713
2714 /* ---------- Stretch To ------------ */
2715
2716 static void stretchto_new_data (void *cdata)
2717 {
2718         bStretchToConstraint *data= (bStretchToConstraint *)cdata;
2719         
2720         data->volmode = 0;
2721         data->plane = 0;
2722         data->orglength = 0.0; 
2723         data->bulge = 1.0;
2724 }
2725
2726 static void stretchto_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
2727 {
2728         bStretchToConstraint *data= con->data;
2729         
2730         /* target only */
2731         func(con, (ID**)&data->tar, userdata);
2732 }
2733
2734 static int stretchto_get_tars (bConstraint *con, ListBase *list)
2735 {
2736         if (con && list) {
2737                 bStretchToConstraint *data= con->data;
2738                 bConstraintTarget *ct;
2739                 
2740                 /* standard target-getting macro for single-target constraints */
2741                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2742                 
2743                 return 1;
2744         }
2745         
2746         return 0;
2747 }
2748
2749 static void stretchto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2750 {
2751         if (con && list) {
2752                 bStretchToConstraint *data= con->data;
2753                 bConstraintTarget *ct= list->first;
2754                 
2755                 /* the following macro is used for all standard single-target constraints */
2756                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2757         }
2758 }
2759
2760 static void stretchto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2761 {
2762         bStretchToConstraint *data= con->data;
2763         bConstraintTarget *ct= targets->first;
2764         
2765         /* only evaluate if there is a target */
2766         if (VALID_CONS_TARGET(ct)) {
2767                 float size[3], scale[3], vec[3], xx[3], zz[3], orth[3];
2768                 float totmat[3][3];
2769                 float tmat[4][4];
2770                 float dist;
2771                 
2772                 /* store scaling before destroying obmat */
2773                 mat4_to_size(size, cob->matrix);
2774                 
2775                 /* store X orientation before destroying obmat */
2776                 xx[0] = cob->matrix[0][0];
2777                 xx[1] = cob->matrix[0][1];
2778                 xx[2] = cob->matrix[0][2];
2779                 normalize_v3(xx);
2780                 
2781                 /* store Z orientation before destroying obmat */
2782                 zz[0] = cob->matrix[2][0];
2783                 zz[1] = cob->matrix[2][1];
2784                 zz[2] = cob->matrix[2][2];
2785                 normalize_v3(zz);
2786                 
2787                 sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
2788                 vec[0] /= size[0];
2789                 vec[1] /= size[1];
2790                 vec[2] /= size[2];
2791                 
2792                 dist = normalize_v3(vec);
2793                 //dist = len_v3v3( ob->obmat[3], targetmat[3]);
2794                 
2795                 /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
2796                 if (data->orglength == 0)  
2797                         data->orglength = dist;
2798                 if (data->bulge == 0) 
2799                         data->bulge = 1.0;
2800                 
2801                 scale[1] = dist/data->orglength;
2802                 switch (data->volmode) {
2803                 /* volume preserving scaling */
2804                 case VOLUME_XZ :
2805                         scale[0] = 1.0f - (float)sqrt(data->bulge) + (float)sqrt(data->bulge*(data->orglength/dist));
2806                         scale[2] = scale[0];
2807                         break;
2808                 case VOLUME_X:
2809                         scale[0] = 1.0f + data->bulge * (data->orglength /dist - 1);
2810                         scale[2] = 1.0;
2811                         break;
2812                 case VOLUME_Z:
2813                         scale[0] = 1.0;
2814                         scale[2] = 1.0f + data->bulge * (data->orglength /dist - 1);
2815                         break;
2816                         /* don't care for volume */
2817                 case NO_VOLUME:
2818                         scale[0] = 1.0;
2819                         scale[2] = 1.0;
2820                         break;
2821                 default: /* should not happen, but in case*/
2822                         return;    
2823                 } /* switch (data->volmode) */
2824
2825                 /* Clear the object's rotation and scale */
2826                 cob->matrix[0][0]=size[0]*scale[0];
2827                 cob->matrix[0][1]=0;
2828                 cob->matrix[0][2]=0;
2829                 cob->matrix[1][0]=0;
2830                 cob->matrix[1][1]=size[1]*scale[1];
2831                 cob->matrix[1][2]=0;
2832                 cob->matrix[2][0]=0;
2833                 cob->matrix[2][1]=0;
2834                 cob->matrix[2][2]=size[2]*scale[2];
2835                 
2836                 sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
2837                 normalize_v3(vec);
2838                 
2839                 /* new Y aligns  object target connection*/
2840                 totmat[1][0] = -vec[0];
2841                 totmat[1][1] = -vec[1];
2842                 totmat[1][2] = -vec[2];
2843                 switch (data->plane) {
2844                 case PLANE_X:
2845                         /* build new Z vector */
2846                         /* othogonal to "new Y" "old X! plane */
2847                         cross_v3_v3v3(orth, vec, xx);
2848                         normalize_v3(orth);
2849                         
2850                         /* new Z*/
2851                         totmat[2][0] = orth[0];
2852                         totmat[2][1] = orth[1];
2853                         totmat[2][2] = orth[2];
2854                         
2855                         /* we decided to keep X plane*/
2856                         cross_v3_v3v3(xx, orth, vec);
2857                         normalize_v3(xx);
2858                         totmat[0][0] = xx[0];
2859                         totmat[0][1] = xx[1];
2860                         totmat[0][2] = xx[2];
2861                         break;
2862                 case PLANE_Z:
2863                         /* build new X vector */
2864                         /* othogonal to "new Y" "old Z! plane */
2865                         cross_v3_v3v3(orth, vec, zz);
2866                         normalize_v3(orth);
2867                         
2868                         /* new X */
2869                         totmat[0][0] = -orth[0];
2870                         totmat[0][1] = -orth[1];
2871                         totmat[0][2] = -orth[2];
2872                         
2873                         /* we decided to keep Z */
2874                         cross_v3_v3v3(zz, orth, vec);
2875                         normalize_v3(zz);
2876                         totmat[2][0] = zz[0];
2877                         totmat[2][1] = zz[1];
2878                         totmat[2][2] = zz[2];
2879                         break;
2880                 } /* switch (data->plane) */
2881                 
2882                 copy_m4_m4(tmat, cob->matrix);
2883                 mul_m4_m3m4(cob->matrix, totmat, tmat);
2884         }
2885 }
2886
2887 static bConstraintTypeInfo CTI_STRETCHTO = {
2888         CONSTRAINT_TYPE_STRETCHTO, /* type */
2889         sizeof(bStretchToConstraint), /* size */
2890         "Stretch To", /* name */
2891         "bStretchToConstraint", /* struct name */
2892         NULL, /* free data */
2893         NULL, /* relink data */
2894         stretchto_id_looper, /* id looper */
2895         NULL, /* copy data */
2896         stretchto_new_data, /* new data */
2897         stretchto_get_tars, /* get constraint targets */
2898         stretchto_flush_tars, /* flush constraint targets */
2899         default_get_tarmat, /* get target matrix */
2900         stretchto_evaluate /* evaluate */
2901 };
2902
2903 /* ---------- Floor ------------ */
2904
2905 static void minmax_new_data (void *cdata)
2906 {
2907         bMinMaxConstraint *data= (bMinMaxConstraint *)cdata;
2908         
2909         data->minmaxflag = TRACK_Z;
2910         data->offset = 0.0f;
2911         data->cache[0] = data->cache[1] = data->cache[2] = 0.0f;
2912         data->flag = 0;
2913 }
2914
2915 static void minmax_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
2916 {
2917         bMinMaxConstraint *data= con->data;
2918         
2919         /* target only */
2920         func(con, (ID**)&data->tar, userdata);
2921 }
2922
2923 static int minmax_get_tars (bConstraint *con, ListBase *list)
2924 {