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