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