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