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