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