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