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