Merge of trunk into blender 2.5:
[blender.git] / source / blender / blenkernel / intern / constraint.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): 2007, Joshua Leung, major recode
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdio.h> 
31 #include <stddef.h>
32 #include <string.h>
33 #include <math.h>
34 #include <float.h>
35
36 #include "MEM_guardedalloc.h"
37 //XXX #include "nla.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_arithb.h"
41
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_action_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_text_types.h"
51
52 #include "BKE_utildefines.h"
53 #include "BKE_action.h"
54 #include "BKE_anim.h" /* for the curve calculation part */
55 #include "BKE_armature.h"
56 #include "BKE_blender.h"
57 #include "BKE_constraint.h"
58 #include "BKE_displist.h"
59 #include "BKE_deform.h"
60 #include "BKE_DerivedMesh.h"    /* for geometry targets */
61 #include "BKE_cdderivedmesh.h" /* for geometry targets */
62 #include "BKE_object.h"
63 #include "BKE_ipo.h"
64 #include "BKE_global.h"
65 #include "BKE_library.h"
66 #include "BKE_idprop.h"
67
68 #ifndef DISABLE_PYTHON
69 #include "BPY_extern.h"
70 #endif
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         BLI_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         BLI_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
1818 /* ----------- Python Constraint -------------- */
1819
1820 static void pycon_free (bConstraint *con)
1821 {
1822         bPythonConstraint *data= con->data;
1823         
1824         /* id-properties */
1825         IDP_FreeProperty(data->prop);
1826         MEM_freeN(data->prop);
1827         
1828         /* multiple targets */
1829         BLI_freelistN(&data->targets);
1830 }       
1831
1832 static void pycon_relink (bConstraint *con)
1833 {
1834         bPythonConstraint *data= con->data;
1835         
1836         ID_NEW(data->text);
1837 }
1838
1839 static void pycon_copy (bConstraint *con, bConstraint *srccon)
1840 {
1841         bPythonConstraint *pycon = (bPythonConstraint *)con->data;
1842         bPythonConstraint *opycon = (bPythonConstraint *)srccon->data;
1843         
1844         pycon->prop = IDP_CopyProperty(opycon->prop);
1845         BLI_duplicatelist(&pycon->targets, &opycon->targets);
1846 }
1847
1848 static void pycon_new_data (void *cdata)
1849 {
1850         bPythonConstraint *data= (bPythonConstraint *)cdata;
1851         
1852         /* everything should be set correctly by calloc, except for the prop->type constant.*/
1853         data->prop = MEM_callocN(sizeof(IDProperty), "PyConstraintProps");
1854         data->prop->type = IDP_GROUP;
1855 }
1856
1857 static int pycon_get_tars (bConstraint *con, ListBase *list)
1858 {
1859         if (con && list) {
1860                 bPythonConstraint *data= con->data;
1861                 
1862                 list->first = data->targets.first;
1863                 list->last = data->targets.last;
1864                 
1865                 return data->tarnum;
1866         }
1867         
1868         return 0;
1869 }
1870
1871 /* Whether this approach is maintained remains to be seen (aligorith) */
1872 static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1873 {
1874         bPythonConstraint *data= con->data;
1875         
1876         if (VALID_CONS_TARGET(ct)) {
1877                 /* special exception for curves - depsgraph issues */
1878                 if (ct->tar->type == OB_CURVE) {
1879                         Curve *cu= ct->tar->data;
1880                         
1881                         /* this check is to make sure curve objects get updated on file load correctly.*/
1882                         if (cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1883                                 makeDispListCurveTypes(ct->tar, 0);                             
1884                 }
1885                 
1886                 /* firstly calculate the matrix the normal way, then let the py-function override
1887                  * this matrix if it needs to do so
1888                  */
1889                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1890                 
1891                 /* only execute target calculation if allowed */
1892 #ifndef DISABLE_PYTHON
1893                 if (G.f & G_DOSCRIPTLINKS)
1894                         BPY_pyconstraint_target(data, ct);
1895 #endif
1896         }
1897         else if (ct)
1898                 Mat4One(ct->matrix);
1899 }
1900
1901 static void pycon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1902 {
1903 #ifdef DISABLE_PYTHON
1904         return;
1905 #else
1906         bPythonConstraint *data= con->data;
1907         
1908         /* only evaluate in python if we're allowed to do so */
1909         if ((G.f & G_DOSCRIPTLINKS)==0)  return;
1910         
1911 /* currently removed, until I this can be re-implemented for multiple targets */
1912 #if 0
1913         /* Firstly, run the 'driver' function which has direct access to the objects involved 
1914          * Technically, this is potentially dangerous as users may abuse this and cause dependency-problems,
1915          * but it also allows certain 'clever' rigging hacks to work.
1916          */
1917         BPY_pyconstraint_driver(data, cob, targets);
1918 #endif
1919         
1920         /* Now, run the actual 'constraint' function, which should only access the matrices */
1921         BPY_pyconstraint_eval(data, cob, targets);
1922 #endif /* DISABLE_PYTHON */
1923 }
1924
1925 static bConstraintTypeInfo CTI_PYTHON = {
1926         CONSTRAINT_TYPE_PYTHON, /* type */
1927         sizeof(bPythonConstraint), /* size */
1928         "Script", /* name */
1929         "bPythonConstraint", /* struct name */
1930         pycon_free, /* free data */
1931         pycon_relink, /* relink data */
1932         pycon_copy, /* copy data */
1933         pycon_new_data, /* new data */
1934         pycon_get_tars, /* get constraint targets */
1935         NULL, /* flush constraint targets */
1936         pycon_get_tarmat, /* get target matrix */
1937         pycon_evaluate /* evaluate */
1938 };
1939
1940 /* -------- Action Constraint ----------- */
1941
1942 static void actcon_relink (bConstraint *con)
1943 {
1944         bActionConstraint *data= con->data;
1945         ID_NEW(data->act);
1946 }
1947
1948 static void actcon_new_data (void *cdata)
1949 {
1950         bActionConstraint *data= (bActionConstraint *)cdata;
1951         
1952         /* set type to 20 (Loc X), as 0 is Rot X for backwards compatability */
1953         data->type = 20;
1954 }
1955
1956 static int actcon_get_tars (bConstraint *con, ListBase *list)
1957 {
1958         if (con && list) {
1959                 bActionConstraint *data= con->data;
1960                 bConstraintTarget *ct;
1961                 
1962                 /* standard target-getting macro for single-target constraints */
1963                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1964                 
1965                 return 1;
1966         }
1967         
1968         return 0;
1969 }
1970
1971 static void actcon_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1972 {
1973         if (con && list) {
1974                 bActionConstraint *data= con->data;
1975                 bConstraintTarget *ct= list->first;
1976                 
1977                 /* the following macro is used for all standard single-target constraints */
1978                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1979         }
1980 }
1981
1982 static void actcon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1983 {
1984         extern void chan_calc_mat(bPoseChannel *chan);
1985         bActionConstraint *data = con->data;
1986         
1987         if (VALID_CONS_TARGET(ct)) {
1988                 float tempmat[4][4], vec[3];
1989                 float s, t;
1990                 short axis;
1991                 
1992                 /* initialise return matrix */
1993                 Mat4One(ct->matrix);
1994                 
1995                 /* get the transform matrix of the target */
1996                 constraint_target_to_mat4(ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1997                 
1998                 /* determine where in transform range target is */
1999                 /* data->type is mapped as follows for backwards compatability:
2000                  *      00,01,02        - rotation (it used to be like this)
2001                  *      10,11,12        - scaling
2002                  *      20,21,22        - location
2003                  */
2004                 if (data->type < 10) {
2005                         /* extract rotation (is in whatever space target should be in) */
2006                         Mat4ToEul(tempmat, vec);
2007                         vec[0] *= (float)(180.0/M_PI);
2008                         vec[1] *= (float)(180.0/M_PI);
2009                         vec[2] *= (float)(180.0/M_PI);
2010                         axis= data->type;
2011                 }
2012                 else if (data->type < 20) {
2013                         /* extract scaling (is in whatever space target should be in) */
2014                         Mat4ToSize(tempmat, vec);
2015                         axis= data->type - 10;
2016                 }
2017                 else {
2018                         /* extract location */
2019                         VECCOPY(vec, tempmat[3]);
2020                         axis= data->type - 20;
2021                 }
2022                 
2023                 /* Target defines the animation */
2024                 s = (vec[axis]-data->min) / (data->max-data->min);
2025                 CLAMP(s, 0, 1);
2026                 t = ( s * (data->end-data->start)) + data->start;
2027                 
2028                 /* Get the appropriate information from the action */
2029                 if (cob->type == CONSTRAINT_OBTYPE_BONE) {
2030                         bPose *pose;
2031                         bPoseChannel *pchan, *tchan;
2032                         
2033                         /* make a temporary pose and evaluate using that */
2034                         pose = MEM_callocN(sizeof(bPose), "pose");
2035                         
2036                         pchan = cob->pchan;
2037                         tchan= verify_pose_channel(pose, pchan->name);
2038                         extract_pose_from_action(pose, data->act, t);
2039                         
2040                         chan_calc_mat(tchan);
2041                         
2042                         Mat4CpyMat4(ct->matrix, tchan->chan_mat);
2043                         
2044                         /* Clean up */
2045                         free_pose(pose);
2046                 }
2047                 else if (cob->type == CONSTRAINT_OBTYPE_OBJECT) {
2048                         /* evaluate using workob */
2049                         what_does_obaction(cob->ob, data->act, t);
2050                         object_to_mat4(&workob, ct->matrix);
2051                 }
2052                 else {
2053                         /* behaviour undefined... */
2054                         puts("Error: unknown owner type for Action Constraint");
2055                 }
2056         }
2057 }
2058
2059 static void actcon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2060 {
2061         bConstraintTarget *ct= targets->first;
2062         
2063         if (VALID_CONS_TARGET(ct)) {
2064                 float temp[4][4];
2065                 
2066                 /* Nice and simple... we just need to multiply the matrices, as the get_target_matrix
2067                  * function has already taken care of everything else.
2068                  */
2069                 Mat4CpyMat4(temp, cob->matrix);
2070                 Mat4MulMat4(cob->matrix, ct->matrix, temp);
2071         }
2072 }
2073
2074 static bConstraintTypeInfo CTI_ACTION = {
2075         CONSTRAINT_TYPE_ACTION, /* type */
2076         sizeof(bActionConstraint), /* size */
2077         "Action", /* name */
2078         "bActionConstraint", /* struct name */
2079         NULL, /* free data */
2080         actcon_relink, /* relink data */
2081         NULL, /* copy data */
2082         actcon_new_data, /* new data */
2083         actcon_get_tars, /* get constraint targets */
2084         actcon_flush_tars, /* flush constraint targets */
2085         actcon_get_tarmat, /* get target matrix */
2086         actcon_evaluate /* evaluate */
2087 };
2088
2089 /* --------- Locked Track ---------- */
2090
2091 static void locktrack_new_data (void *cdata)
2092 {
2093         bLockTrackConstraint *data= (bLockTrackConstraint *)cdata;
2094         
2095         data->trackflag = TRACK_Y;
2096         data->lockflag = LOCK_Z;
2097 }       
2098
2099 static int locktrack_get_tars (bConstraint *con, ListBase *list)
2100 {
2101         if (con && list) {
2102                 bLockTrackConstraint *data= con->data;
2103                 bConstraintTarget *ct;
2104                 
2105                 /* the following macro is used for all standard single-target constraints */
2106                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2107                 
2108                 return 1;
2109         }
2110         
2111         return 0;
2112 }
2113
2114 static void locktrack_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2115 {
2116         if (con && list) {
2117                 bLockTrackConstraint *data= con->data;
2118                 bConstraintTarget *ct= list->first;
2119                 
2120                 /* the following macro is used for all standard single-target constraints */
2121                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2122         }
2123 }
2124
2125 static void locktrack_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2126 {
2127         bLockTrackConstraint *data= con->data;
2128         bConstraintTarget *ct= targets->first;
2129         
2130         if (VALID_CONS_TARGET(ct)) {
2131                 float vec[3],vec2[3];
2132                 float totmat[3][3];
2133                 float tmpmat[3][3];
2134                 float invmat[3][3];
2135                 float tmat[4][4];
2136                 float mdet;
2137                 
2138                 /* Vector object -> target */
2139                 VecSubf(vec, ct->matrix[3], cob->matrix[3]);
2140                 switch (data->lockflag){
2141                 case LOCK_X: /* LOCK X */
2142                 {
2143                         switch (data->trackflag) {
2144                                 case TRACK_Y: /* LOCK X TRACK Y */
2145                                 {
2146                                         /* Projection of Vector on the plane */
2147                                         Projf(vec2, vec, cob->matrix[0]);
2148                                         VecSubf(totmat[1], vec, vec2);
2149                                         Normalize(totmat[1]);
2150                                         
2151                                         /* the x axis is fixed */
2152                                         totmat[0][0] = cob->matrix[0][0];
2153                                         totmat[0][1] = cob->matrix[0][1];
2154                                         totmat[0][2] = cob->matrix[0][2];
2155                                         Normalize(totmat[0]);
2156                                         
2157                                         /* the z axis gets mapped onto a third orthogonal vector */
2158                                         Crossf(totmat[2], totmat[0], totmat[1]);
2159                                 }
2160                                         break;
2161                                 case TRACK_Z: /* LOCK X TRACK Z */
2162                                 {
2163                                         /* Projection of Vector on the plane */
2164                                         Projf(vec2, vec, cob->matrix[0]);
2165                                         VecSubf(totmat[2], vec, vec2);
2166                                         Normalize(totmat[2]);
2167                                         
2168                                         /* the x axis is fixed */
2169                                         totmat[0][0] = cob->matrix[0][0];
2170                                         totmat[0][1] = cob->matrix[0][1];
2171                                         totmat[0][2] = cob->matrix[0][2];
2172                                         Normalize(totmat[0]);
2173                                         
2174                                         /* the z axis gets mapped onto a third orthogonal vector */
2175                                         Crossf(totmat[1], totmat[2], totmat[0]);
2176                                 }
2177                                         break;
2178                                 case TRACK_nY: /* LOCK X TRACK -Y */
2179                                 {
2180                                         /* Projection of Vector on the plane */
2181                                         Projf(vec2, vec, cob->matrix[0]);
2182                                         VecSubf(totmat[1], vec, vec2);
2183                                         Normalize(totmat[1]);
2184                                         VecMulf(totmat[1],-1);
2185                                         
2186                                         /* the x axis is fixed */
2187                                         totmat[0][0] = cob->matrix[0][0];
2188                                         totmat[0][1] = cob->matrix[0][1];
2189                                         totmat[0][2] = cob->matrix[0][2];
2190                                         Normalize(totmat[0]);
2191                                         
2192                                         /* the z axis gets mapped onto a third orthogonal vector */
2193                                         Crossf(totmat[2], totmat[0], totmat[1]);
2194                                 }
2195                                         break;
2196                                 case TRACK_nZ: /* LOCK X TRACK -Z */
2197                                 {
2198                                         /* Projection of Vector on the plane */
2199                                         Projf(vec2, vec, cob->matrix[0]);
2200                                         VecSubf(totmat[2], vec, vec2);
2201                                         Normalize(totmat[2]);
2202                                         VecMulf(totmat[2],-1);
2203                                                 
2204                                         /* the x axis is fixed */
2205                                         totmat[0][0] = cob->matrix[0][0];
2206                                         totmat[0][1] = cob->matrix[0][1];
2207                                         totmat[0][2] = cob->matrix[0][2];
2208                                         Normalize(totmat[0]);
2209                                                 
2210                                         /* the z axis gets mapped onto a third orthogonal vector */
2211                                         Crossf(totmat[1], totmat[2], totmat[0]);
2212                                 }
2213                                         break;
2214                                 default:
2215                                 {
2216                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2217                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2218                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2219                                 }
2220                                         break;
2221                         }
2222                 }
2223                         break;
2224                 case LOCK_Y: /* LOCK Y */
2225                 {
2226                         switch (data->trackflag) {
2227                                 case TRACK_X: /* LOCK Y TRACK X */
2228                                 {
2229                                         /* Projection of Vector on the plane */
2230                                         Projf(vec2, vec, cob->matrix[1]);
2231                                         VecSubf(totmat[0], vec, vec2);
2232                                         Normalize(totmat[0]);
2233                                         
2234                                         /* the y axis is fixed */
2235                                         totmat[1][0] = cob->matrix[1][0];
2236                                         totmat[1][1] = cob->matrix[1][1];
2237                                         totmat[1][2] = cob->matrix[1][2];
2238                                         Normalize(totmat[1]);
2239                                         
2240                                         /* the z axis gets mapped onto a third orthogonal vector */
2241                                         Crossf(totmat[2], totmat[0], totmat[1]);
2242                                 }
2243                                         break;
2244                                 case TRACK_Z: /* LOCK Y TRACK Z */
2245                                 {
2246                                         /* Projection of Vector on the plane */
2247                                         Projf(vec2, vec, cob->matrix[1]);
2248                                         VecSubf(totmat[2], vec, vec2);
2249                                         Normalize(totmat[2]);
2250
2251                                         /* the y axis is fixed */
2252                                         totmat[1][0] = cob->matrix[1][0];
2253                                         totmat[1][1] = cob->matrix[1][1];
2254                                         totmat[1][2] = cob->matrix[1][2];
2255                                         Normalize(totmat[1]);
2256                                         
2257                                         /* the z axis gets mapped onto a third orthogonal vector */
2258                                         Crossf(totmat[0], totmat[1], totmat[2]);
2259                                 }
2260                                         break;
2261                                 case TRACK_nX: /* LOCK Y TRACK -X */
2262                                 {
2263                                         /* Projection of Vector on the plane */
2264                                         Projf(vec2, vec, cob->matrix[1]);
2265                                         VecSubf(totmat[0], vec, vec2);
2266                                         Normalize(totmat[0]);
2267                                         VecMulf(totmat[0],-1);
2268                                         
2269                                         /* the y axis is fixed */
2270                                         totmat[1][0] = cob->matrix[1][0];
2271                                         totmat[1][1] = cob->matrix[1][1];
2272                                         totmat[1][2] = cob->matrix[1][2];
2273                                         Normalize(totmat[1]);
2274                                         
2275                                         /* the z axis gets mapped onto a third orthogonal vector */
2276                                         Crossf(totmat[2], totmat[0], totmat[1]);
2277                                 }
2278                                         break;
2279                                 case TRACK_nZ: /* LOCK Y TRACK -Z */
2280                                 {
2281                                         /* Projection of Vector on the plane */
2282                                         Projf(vec2, vec, cob->matrix[1]);
2283                                         VecSubf(totmat[2], vec, vec2);
2284                                         Normalize(totmat[2]);
2285                                         VecMulf(totmat[2],-1);
2286                                         
2287                                         /* the y axis is fixed */
2288                                         totmat[1][0] = cob->matrix[1][0];
2289                                         totmat[1][1] = cob->matrix[1][1];
2290                                         totmat[1][2] = cob->matrix[1][2];
2291                                         Normalize(totmat[1]);
2292                                         
2293                                         /* the z axis gets mapped onto a third orthogonal vector */
2294                                         Crossf(totmat[0], totmat[1], totmat[2]);
2295                                 }
2296                                         break;
2297                                 default:
2298                                 {
2299                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2300                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2301                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2302                                 }
2303                                         break;
2304                         }
2305                 }
2306                         break;
2307                 case LOCK_Z: /* LOCK Z */
2308                 {
2309                         switch (data->trackflag) {
2310                                 case TRACK_X: /* LOCK Z TRACK X */
2311                                 {
2312                                         /* Projection of Vector on the plane */
2313                                         Projf(vec2, vec, cob->matrix[2]);
2314                                         VecSubf(totmat[0], vec, vec2);
2315                                         Normalize(totmat[0]);
2316                                         
2317                                         /* the z axis is fixed */
2318                                         totmat[2][0] = cob->matrix[2][0];
2319                                         totmat[2][1] = cob->matrix[2][1];
2320                                         totmat[2][2] = cob->matrix[2][2];
2321                                         Normalize(totmat[2]);
2322                                         
2323                                         /* the x axis gets mapped onto a third orthogonal vector */
2324                                         Crossf(totmat[1], totmat[2], totmat[0]);
2325                                 }
2326                                         break;
2327                                 case TRACK_Y: /* LOCK Z TRACK Y */
2328                                 {
2329                                         /* Projection of Vector on the plane */
2330                                         Projf(vec2, vec, cob->matrix[2]);
2331                                         VecSubf(totmat[1], vec, vec2);
2332                                         Normalize(totmat[1]);
2333                                         
2334                                         /* the z axis is fixed */
2335                                         totmat[2][0] = cob->matrix[2][0];
2336                                         totmat[2][1] = cob->matrix[2][1];
2337                                         totmat[2][2] = cob->matrix[2][2];
2338                                         Normalize(totmat[2]);
2339                                                 
2340                                         /* the x axis gets mapped onto a third orthogonal vector */
2341                                         Crossf(totmat[0], totmat[1], totmat[2]);
2342                                 }
2343                                         break;
2344                                 case TRACK_nX: /* LOCK Z TRACK -X */
2345                                 {
2346                                         /* Projection of Vector on the plane */
2347                                         Projf(vec2, vec, cob->matrix[2]);
2348                                         VecSubf(totmat[0], vec, vec2);
2349                                         Normalize(totmat[0]);
2350                                         VecMulf(totmat[0],-1);
2351                                         
2352                                         /* the z axis is fixed */
2353                                         totmat[2][0] = cob->matrix[2][0];
2354                                         totmat[2][1] = cob->matrix[2][1];
2355                                         totmat[2][2] = cob->matrix[2][2];
2356                                         Normalize(totmat[2]);
2357                                         
2358                                         /* the x axis gets mapped onto a third orthogonal vector */
2359                                         Crossf(totmat[1], totmat[2], totmat[0]);
2360                                 }
2361                                         break;
2362                                 case TRACK_nY: /* LOCK Z TRACK -Y */
2363                                 {
2364                                         /* Projection of Vector on the plane */
2365                                         Projf(vec2, vec, cob->matrix[2]);
2366                                         VecSubf(totmat[1], vec, vec2);
2367                                         Normalize(totmat[1]);
2368                                         VecMulf(totmat[1],-1);
2369                                         
2370                                         /* the z axis is fixed */
2371                                         totmat[2][0] = cob->matrix[2][0];
2372                                         totmat[2][1] = cob->matrix[2][1];
2373                                         totmat[2][2] = cob->matrix[2][2];
2374                                         Normalize(totmat[2]);
2375                                                 
2376                                         /* the x axis gets mapped onto a third orthogonal vector */
2377                                         Crossf(totmat[0], totmat[1], totmat[2]);
2378                                 }
2379                                         break;
2380                                 default:
2381                                 {
2382                                                 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2383                                                 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2384                                                 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2385                                 }
2386                                         break;
2387                         }
2388                 }
2389                         break;
2390                 default:
2391                         {
2392                                 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2393                                 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2394                                 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2395                         }
2396                         break;
2397                 }
2398                 /* Block to keep matrix heading */
2399                 tmpmat[0][0] = cob->matrix[0][0];tmpmat[0][1] = cob->matrix[0][1];tmpmat[0][2] = cob->matrix[0][2];
2400                 tmpmat[1][0] = cob->matrix[1][0];tmpmat[1][1] = cob->matrix[1][1];tmpmat[1][2] = cob->matrix[1][2];
2401                 tmpmat[2][0] = cob->matrix[2][0];tmpmat[2][1] = cob->matrix[2][1];tmpmat[2][2] = cob->matrix[2][2];
2402                 Normalize(tmpmat[0]);
2403                 Normalize(tmpmat[1]);
2404                 Normalize(tmpmat[2]);
2405                 Mat3Inv(invmat, tmpmat);
2406                 Mat3MulMat3(tmpmat, totmat, invmat);
2407                 totmat[0][0] = tmpmat[0][0];totmat[0][1] = tmpmat[0][1];totmat[0][2] = tmpmat[0][2];
2408                 totmat[1][0] = tmpmat[1][0];totmat[1][1] = tmpmat[1][1];totmat[1][2] = tmpmat[1][2];
2409                 totmat[2][0] = tmpmat[2][0];totmat[2][1] = tmpmat[2][1];totmat[2][2] = tmpmat[2][2];
2410                 
2411                 Mat4CpyMat4(tmat, cob->matrix);
2412                 
2413                 mdet = Det3x3(  totmat[0][0],totmat[0][1],totmat[0][2],
2414                                                 totmat[1][0],totmat[1][1],totmat[1][2],
2415                                                 totmat[2][0],totmat[2][1],totmat[2][2]);
2416                 if (mdet==0) {
2417                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2418                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2419                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2420                 }
2421                 
2422                 /* apply out transformaton to the object */
2423                 Mat4MulMat34(cob->matrix, totmat, tmat);
2424         }
2425 }
2426
2427 static bConstraintTypeInfo CTI_LOCKTRACK = {
2428         CONSTRAINT_TYPE_LOCKTRACK, /* type */
2429         sizeof(bLockTrackConstraint), /* size */
2430         "Locked Track", /* name */
2431         "bLockTrackConstraint", /* struct name */
2432         NULL, /* free data */
2433         NULL, /* relink data */
2434         NULL, /* copy data */
2435         locktrack_new_data, /* new data */
2436         locktrack_get_tars, /* get constraint targets */
2437         locktrack_flush_tars, /* flush constraint targets */
2438         default_get_tarmat, /* get target matrix */
2439         locktrack_evaluate /* evaluate */
2440 };
2441
2442 /* ---------- Limit Distance Constraint ----------- */
2443
2444 static void distlimit_new_data (void *cdata)
2445 {
2446         bDistLimitConstraint *data= (bDistLimitConstraint *)cdata;
2447         
2448         data->dist= 0.0;
2449 }
2450
2451 static int distlimit_get_tars (bConstraint *con, ListBase *list)
2452 {
2453         if (con && list) {
2454                 bDistLimitConstraint *data= con->data;
2455                 bConstraintTarget *ct;
2456                 
2457                 /* standard target-getting macro for single-target constraints */
2458                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2459                 
2460                 return 1;
2461         }
2462         
2463         return 0;
2464 }
2465
2466 static void distlimit_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2467 {
2468         if (con && list) {
2469                 bDistLimitConstraint *data= con->data;
2470                 bConstraintTarget *ct= list->first;
2471                 
2472                 /* the following macro is used for all standard single-target constraints */
2473                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2474         }
2475 }
2476
2477 static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2478 {
2479         bDistLimitConstraint *data= con->data;
2480         bConstraintTarget *ct= targets->first;
2481         
2482         /* only evaluate if there is a target */
2483         if (VALID_CONS_TARGET(ct)) {
2484                 float dvec[3], dist=0.0f, sfac=1.0f;
2485                 short clamp_surf= 0;
2486                 
2487                 /* calculate our current distance from the target */
2488                 dist= VecLenf(cob->matrix[3], ct->matrix[3]);
2489                 
2490                 /* set distance (flag is only set when user demands it) */
2491                 if (data->dist == 0)
2492                         data->dist= dist;
2493                 
2494                 /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
2495                 if (data->mode == LIMITDIST_OUTSIDE) {
2496                         /* if inside, then move to surface */
2497                         if (dist <= data->dist) {
2498                                 clamp_surf= 1;
2499                                 sfac= data->dist / dist;
2500                         }
2501                         /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
2502                         else if (data->flag & LIMITDIST_USESOFT) {
2503                                 if (dist <= (data->dist + data->soft)) {
2504                                         
2505                                 }
2506                         }
2507                 }
2508                 else if (data->mode == LIMITDIST_INSIDE) {
2509                         /* if outside, then move to surface */
2510                         if (dist >= data->dist) {
2511                                 clamp_surf= 1;
2512                                 sfac= data->dist / dist;
2513                         }
2514                         /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
2515                         else if (data->flag & LIMITDIST_USESOFT) {
2516                                 // FIXME: there's a problem with "jumping" when this kicks in
2517                                 if (dist >= (data->dist - data->soft)) {
2518                                         sfac = (float)( data->soft*(1.0 - exp(-(dist - data->dist)/data->soft)) + data->dist );
2519                                         sfac /= dist;
2520                                         
2521                                         clamp_surf= 1;
2522                                 }
2523                         }
2524                 }
2525                 else {
2526                         if (IS_EQ(dist, data->dist)==0) {
2527                                 clamp_surf= 1;
2528                                 sfac= data->dist / dist;
2529                         }
2530                 }
2531                 
2532                 /* clamp to 'surface' (i.e. move owner so that dist == data->dist) */
2533                 if (clamp_surf) {
2534                         /* simply interpolate along line formed by target -> owner */
2535                         VecLerpf(dvec, ct->matrix[3], cob->matrix[3], sfac);
2536                         
2537                         /* copy new vector onto owner */
2538                         VECCOPY(cob->matrix[3], dvec);
2539                 }
2540         }
2541 }
2542
2543 static bConstraintTypeInfo CTI_DISTLIMIT = {
2544         CONSTRAINT_TYPE_DISTLIMIT, /* type */
2545         sizeof(bDistLimitConstraint), /* size */
2546         "Limit Distance", /* name */
2547         "bDistLimitConstraint", /* struct name */
2548         NULL, /* free data */
2549         NULL, /* relink data */
2550         NULL, /* copy data */
2551         distlimit_new_data, /* new data */
2552         distlimit_get_tars, /* get constraint targets */
2553         distlimit_flush_tars, /* flush constraint targets */
2554         default_get_tarmat, /* get a target matrix */
2555         distlimit_evaluate /* evaluate */
2556 };
2557
2558 /* ---------- Stretch To ------------ */
2559
2560 static void stretchto_new_data (void *cdata)
2561 {
2562         bStretchToConstraint *data= (bStretchToConstraint *)cdata;
2563         
2564         data->volmode = 0;
2565         data->plane = 0;
2566         data->orglength = 0.0; 
2567         data->bulge = 1.0;
2568 }
2569
2570 static int stretchto_get_tars (bConstraint *con, ListBase *list)
2571 {
2572         if (con && list) {
2573                 bStretchToConstraint *data= con->data;
2574                 bConstraintTarget *ct;
2575                 
2576                 /* standard target-getting macro for single-target constraints */
2577                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2578                 
2579                 return 1;
2580         }
2581         
2582         return 0;
2583 }
2584
2585 static void stretchto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2586 {
2587         if (con && list) {
2588                 bStretchToConstraint *data= con->data;
2589                 bConstraintTarget *ct= list->first;
2590                 
2591                 /* the following macro is used for all standard single-target constraints */
2592                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2593         }
2594 }
2595
2596 static void stretchto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2597 {
2598         bStretchToConstraint *data= con->data;
2599         bConstraintTarget *ct= targets->first;
2600         
2601         /* only evaluate if there is a target */
2602         if (VALID_CONS_TARGET(ct)) {
2603                 float size[3], scale[3], vec[3], xx[3], zz[3], orth[3];
2604                 float totmat[3][3];
2605                 float tmat[4][4];
2606                 float dist;
2607                 
2608                 /* store scaling before destroying obmat */
2609                 Mat4ToSize(cob->matrix, size);
2610                 
2611                 /* store X orientation before destroying obmat */
2612                 xx[0] = cob->matrix[0][0];
2613                 xx[1] = cob->matrix[0][1];
2614                 xx[2] = cob->matrix[0][2];
2615                 Normalize(xx);
2616                 
2617                 /* store Z orientation before destroying obmat */
2618                 zz[0] = cob->matrix[2][0];
2619                 zz[1] = cob->matrix[2][1];
2620                 zz[2] = cob->matrix[2][2];
2621                 Normalize(zz);
2622                 
2623                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2624                 vec[0] /= size[0];
2625                 vec[1] /= size[1];
2626                 vec[2] /= size[2];
2627                 
2628                 dist = Normalize(vec);
2629                 //dist = VecLenf( ob->obmat[3], targetmat[3]);
2630                 
2631                 /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
2632                 if (data->orglength == 0)  
2633                         data->orglength = dist;
2634                 if (data->bulge == 0) 
2635                         data->bulge = 1.0;
2636                 
2637                 scale[1] = dist/data->orglength;
2638                 switch (data->volmode) {
2639                 /* volume preserving scaling */
2640                 case VOLUME_XZ :
2641                         scale[0] = 1.0f - (float)sqrt(data->bulge) + (float)sqrt(data->bulge*(data->orglength/dist));
2642                         scale[2] = scale[0];
2643                         break;
2644                 case VOLUME_X:
2645                         scale[0] = 1.0f + data->bulge * (data->orglength /dist - 1);
2646                         scale[2] = 1.0;
2647                         break;
2648                 case VOLUME_Z:
2649                         scale[0] = 1.0;
2650                         scale[2] = 1.0f + data->bulge * (data->orglength /dist - 1);
2651                         break;
2652                         /* don't care for volume */
2653                 case NO_VOLUME:
2654                         scale[0] = 1.0;
2655                         scale[2] = 1.0;
2656                         break;
2657                 default: /* should not happen, but in case*/
2658                         return;    
2659                 } /* switch (data->volmode) */
2660
2661                 /* Clear the object's rotation and scale */
2662                 cob->matrix[0][0]=size[0]*scale[0];
2663                 cob->matrix[0][1]=0;
2664                 cob->matrix[0][2]=0;
2665                 cob->matrix[1][0]=0;
2666                 cob->matrix[1][1]=size[1]*scale[1];
2667                 cob->matrix[1][2]=0;
2668                 cob->matrix[2][0]=0;
2669                 cob->matrix[2][1]=0;
2670                 cob->matrix[2][2]=size[2]*scale[2];
2671                 
2672                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2673                 Normalize(vec);
2674                 
2675                 /* new Y aligns  object target connection*/
2676                 totmat[1][0] = -vec[0];
2677                 totmat[1][1] = -vec[1];
2678                 totmat[1][2] = -vec[2];
2679                 switch (data->plane) {
2680                 case PLANE_X:
2681                         /* build new Z vector */
2682                         /* othogonal to "new Y" "old X! plane */
2683                         Crossf(orth, vec, xx);
2684                         Normalize(orth);
2685                         
2686                         /* new Z*/
2687                         totmat[2][0] = orth[0];
2688                         totmat[2][1] = orth[1];
2689                         totmat[2][2] = orth[2];
2690                         
2691                         /* we decided to keep X plane*/
2692                         Crossf(xx, orth, vec);
2693                         Normalize(xx);
2694                         totmat[0][0] = xx[0];
2695                         totmat[0][1] = xx[1];
2696                         totmat[0][2] = xx[2];
2697                         break;
2698                 case PLANE_Z:
2699                         /* build new X vector */
2700                         /* othogonal to "new Y" "old Z! plane */
2701                         Crossf(orth, vec, zz);
2702                         Normalize(orth);
2703                         
2704                         /* new X */
2705                         totmat[0][0] = -orth[0];
2706                         totmat[0][1] = -orth[1];
2707                         totmat[0][2] = -orth[2];
2708                         
2709                         /* we decided to keep Z */
2710                         Crossf(zz, orth, vec);
2711                         Normalize(zz);
2712                         totmat[2][0] = zz[0];
2713                         totmat[2][1] = zz[1];
2714                         totmat[2][2] = zz[2];
2715                         break;
2716                 } /* switch (data->plane) */
2717                 
2718                 Mat4CpyMat4(tmat, cob->matrix);
2719                 Mat4MulMat34(cob->matrix, totmat, tmat);
2720         }
2721 }
2722
2723 static bConstraintTypeInfo CTI_STRETCHTO = {
2724         CONSTRAINT_TYPE_STRETCHTO, /* type */
2725         sizeof(bStretchToConstraint), /* size */
2726         "Stretch To", /* name */
2727         "bStretchToConstraint", /* struct name */
2728         NULL, /* free data */
2729         NULL, /* relink data */
2730         NULL, /* copy data */
2731         stretchto_new_data, /* new data */
2732         stretchto_get_tars, /* get constraint targets */
2733         stretchto_flush_tars, /* flush constraint targets */
2734         default_get_tarmat, /* get target matrix */
2735         stretchto_evaluate /* evaluate */
2736 };
2737
2738 /* ---------- Floor ------------ */
2739
2740 static void minmax_new_data (void *cdata)
2741 {
2742         bMinMaxConstraint *data= (bMinMaxConstraint *)cdata;
2743         
2744         data->minmaxflag = TRACK_Z;
2745         data->offset = 0.0f;
2746         data->cache[0] = data->cache[1] = data->cache[2] = 0.0f;
2747         data->flag = 0;
2748 }
2749
2750 static int minmax_get_tars (bConstraint *con, ListBase *list)
2751 {
2752         if (con && list) {
2753                 bMinMaxConstraint *data= con->data;
2754                 bConstraintTarget *ct;
2755                 
2756                 /* standard target-getting macro for single-target constraints */
2757                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2758                 
2759                 return 1;
2760         }
2761         
2762         return 0;
2763 }
2764
2765 static void minmax_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2766 {
2767         if (con && list) {
2768                 bMinMaxConstraint *data= con->data;
2769                 bConstraintTarget *ct= list->first;
2770                 
2771                 /* the following macro is used for all standard single-target constraints */
2772                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2773         }
2774 }
2775
2776 static void minmax_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2777 {
2778         bMinMaxConstraint *data= con->data;
2779         bConstraintTarget *ct= targets->first;
2780         
2781         /* only evaluate if there is a target */
2782         if (VALID_CONS_TARGET(ct)) {
2783                 float obmat[4][4], imat[4][4], tarmat[4][4], tmat[4][4];
2784                 float val1, val2;
2785                 int index;
2786                 
2787                 Mat4CpyMat4(obmat, cob->matrix);
2788                 Mat4CpyMat4(tarmat, ct->matrix);
2789                 
2790                 if (data->flag & MINMAX_USEROT) {
2791                         /* take rotation of target into account by doing the transaction in target's localspace */
2792                         Mat4Invert(imat, tarmat);
2793                         Mat4MulMat4(tmat, obmat, imat);
2794                         Mat4CpyMat4(obmat, tmat);
2795                         Mat4One(tarmat);
2796                 }
2797                 
2798                 switch (data->minmaxflag) {
2799                 case TRACK_Z:
2800                         val1 = tarmat[3][2];
2801                         val2 = obmat[3][2]-data->offset;
2802                         index = 2;
2803                         break;
2804                 case TRACK_Y:
2805                         val1 = tarmat[3][1];
2806                         val2 = obmat[3][1]-data->offset;
2807                         index = 1;
2808                         break;
2809                 case TRACK_X:
2810                         val1 = tarmat[3][0];
2811                         val2 = obmat[3][0]-data->offset;
2812                         index = 0;
2813                         break;
2814                 case TRACK_nZ:
2815                         val2 = tarmat[3][2];
2816                         val1 = obmat[3][2]-data->offset;
2817                         index = 2;
2818                         break;
2819                 case TRACK_nY:
2820                         val2 = tarmat[3][1];
2821                         val1 = obmat[3][1]-data->offset;
2822                         index = 1;
2823                         break;
2824                 case TRACK_nX:
2825                         val2 = tarmat[3][0];
2826                         val1 = obmat[3][0]-data->offset;
2827                         index = 0;
2828                         break;
2829                 default:
2830                         return;
2831                 }
2832                 
2833                 if (val1 > val2) {
2834                         obmat[3][index] = tarmat[3][index] + data->offset;
2835                         if (data->flag & MINMAX_STICKY) {
2836                                 if (data->flag & MINMAX_STUCK) {
2837                                         VECCOPY(obmat[3], data->cache);
2838                                 } 
2839                                 else {
2840                                         VECCOPY(data->cache, obmat[3]);
2841                                         data->flag |= MINMAX_STUCK;
2842                                 }
2843                         }
2844                         if (data->flag & MINMAX_USEROT) {
2845                                 /* get out of localspace */
2846                                 Mat4MulMat4(tmat, obmat, ct->matrix);
2847                                 Mat4CpyMat4(cob->matrix, tmat);
2848                         } 
2849                         else {                  
2850                                 VECCOPY(cob->matrix[3], obmat[3]);
2851                         }
2852                 } 
2853                 else {
2854                         data->flag &= ~MINMAX_STUCK;
2855                 }
2856         }
2857 }
2858
2859 static bConstraintTypeInfo CTI_MINMAX = {
2860         CONSTRAINT_TYPE_MINMAX, /* type */
2861         sizeof(bMinMaxConstraint), /* size */
2862         "Floor", /* name */
2863         "bMinMaxConstraint", /* struct name */
2864         NULL, /* free data */
2865         NULL, /* relink data */
2866         NULL, /* copy data */
2867         minmax_new_data, /* new data */
2868         minmax_get_tars, /* get constraint targets */
2869         minmax_flush_tars, /* flush constraint targets */
2870         default_get_tarmat, /* get target matrix */
2871         minmax_evaluate /* evaluate */
2872 };
2873
2874 /* ------- RigidBody Joint ---------- */
2875
2876 static void rbj_new_data (void *cdata)
2877 {
2878         bRigidBodyJointConstraint *data= (bRigidBodyJointConstraint *)cdata;
2879         
2880         // removed code which set target of this constraint  
2881     data->type=1;
2882 }
2883
2884 static int rbj_get_tars (bConstraint *con, ListBase *list)
2885 {
2886         if (con && list) {
2887                 bRigidBodyJointConstraint *data= con->data;
2888                 bConstraintTarget *ct;
2889                 
2890                 /* standard target-getting macro for single-target constraints without subtargets */
2891                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
2892                 
2893                 return 1;
2894         }
2895         
2896         return 0;
2897 }
2898
2899 static void rbj_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2900 {
2901         if (con && list) {
2902                 bRigidBodyJointConstraint *data= con->data;
2903                 bConstraintTarget *ct= list->first;
2904                 
2905                 /* the following macro is used for all standard single-target constraints */
2906                 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, nocopy)
2907         }
2908 }
2909