Final merge of HEAD (bf-blender) into the orange branch.
[blender.git] / source / blender / src / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51 #include "DNA_view3d_types.h"
52
53 #include "BIF_screen.h"
54 #include "BIF_resources.h"
55 #include "BIF_mywindow.h"
56 #include "BIF_gl.h"
57 #include "BIF_editarmature.h"
58 #include "BIF_editmesh.h"
59 #include "BIF_editsima.h"
60 #include "BIF_meshtools.h"
61
62 #include "BKE_action.h"
63 #include "BKE_anim.h"
64 #include "BKE_armature.h"
65 #include "BKE_curve.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_displist.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_global.h"
70 #include "BKE_ipo.h"
71 #include "BKE_lattice.h"
72 #include "BKE_mesh.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_utildefines.h"
76
77 #include "BSE_view.h"
78 #include "BDR_unwrapper.h"
79
80 #include "BLI_arithb.h"
81 #include "BLI_blenlib.h"
82 #include "BLI_editVert.h"
83
84 #include "blendef.h"
85
86 #include "mydevice.h"
87
88 #include "transform.h"
89
90 extern ListBase editNurb;
91 extern ListBase editelems;
92
93 extern TransInfo Trans; /* From transform.c */
94
95 /* ************************** Functions *************************** */
96
97
98 void getViewVector(float coord[3], float vec[3])
99 {
100         TransInfo *t = BIF_GetTransInfo();
101
102         if (t->persp)
103         {
104                 float p1[4], p2[4];
105
106                 VECCOPY(p1, coord);
107                 p1[3] = 1.0f;
108                 VECCOPY(p2, p1);
109                 p2[3] = 1.0f;
110                 Mat4MulVec4fl(t->viewmat, p2);
111
112                 p2[0] = 2.0f * p2[0];
113                 p2[1] = 2.0f * p2[1];
114                 p2[2] = 2.0f * p2[2];
115
116                 Mat4MulVec4fl(t->viewinv, p2);
117
118                 VecSubf(vec, p1, p2);
119         }
120         else {
121                 VECCOPY(vec, t->viewinv[2]);
122         }
123         Normalise(vec);
124 }
125
126 /* ************************** GENERICS **************************** */
127
128 static void clipMirrorModifier(TransInfo *t, Object *ob)
129 {
130         ModifierData *md= ob->modifiers.first;
131         float tolerance[3];
132         int axis = 0;
133
134         for (; md; md=md->next) {
135                 if (md->type==eModifierType_Mirror) {
136                         MirrorModifierData *mmd = (MirrorModifierData*) md;     
137                 
138                         if(mmd->flag & MOD_MIR_CLIPPING) {
139                                 switch(mmd->axis){
140                                         case 0:
141                                                 axis |= 1;
142                                                 tolerance[0] = mmd->tolerance;
143                                                 break;
144                                         case 1:
145                                                 axis |= 2;
146                                                 tolerance[1] = mmd->tolerance;
147                                                 break;
148                                         case 2:
149                                                 axis |= 4;
150                                                 tolerance[2] = mmd->tolerance;
151                                 }
152                         }
153                 }
154         }
155         if (axis) {
156                 TransData *td = t->data;
157                 int i;
158                 
159                 for(i = 0 ; i < t->total; i++, td++) {
160                         if (td->flag & TD_NOACTION)
161                                 break;
162                         if (td->loc==NULL)
163                                 break;
164                         
165                         if(axis & 1) {
166                                 if(fabs(td->iloc[0])<=tolerance[0] || td->loc[0]*td->iloc[0]<0.0f) td->loc[0]= 0.0f;
167                         }
168                         
169                         if(axis & 2) {
170                                 if(fabs(td->iloc[1])<=tolerance[1] || td->loc[1]*td->iloc[1]<0.0f) td->loc[1]= 0.0f;
171                         }
172                         if(axis & 4) {
173                                 if(fabs(td->iloc[2])<=tolerance[2] || td->loc[2]*td->iloc[2]<0.0f) td->loc[2]= 0.0f;
174                         }
175                 }
176         }
177 }
178
179 /* assumes G.obedit set to mesh object */
180 static void editmesh_apply_to_mirror(TransInfo *t)
181 {
182         TransData *td = t->data;
183         EditVert *eve;
184         int i;
185         
186         for(i = 0 ; i < t->total; i++, td++) {
187                 if (td->flag & TD_NOACTION)
188                         break;
189                 if (td->loc==NULL)
190                         break;
191                 
192                 eve= td->tdmir;
193                 if(eve) {
194                         eve->co[0]= -td->loc[0];
195                         eve->co[1]= td->loc[1];
196                         eve->co[2]= td->loc[2];
197                 }               
198         }               
199 }
200
201 /* called for updating while transform acts, once per redraw */
202 void recalcData(TransInfo *t)
203 {
204         Base *base;
205                 
206         if (G.obedit) {
207                 if (G.obedit->type == OB_MESH) {
208
209                         /* mirror modifier clipping? */
210                         if(t->state != TRANS_CANCEL)
211                                 clipMirrorModifier(t, G.obedit);
212                         
213                         if(G.scene->toolsettings->editbutflag & B_MESH_X_MIRROR)
214                                 editmesh_apply_to_mirror(t);
215                         
216                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
217                         
218                         recalc_editnormals();
219                 }
220                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
221                         Nurb *nu= editNurb.first;
222                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
223                         
224                         while(nu) {
225                                 test2DNurb(nu);
226                                 testhandlesNurb(nu); /* test for bezier too */
227                                 nu= nu->next;
228                         }
229                 }
230                 else if(G.obedit->type==OB_ARMATURE){   /* no recalc flag, does pose */
231                         bArmature *arm= G.obedit->data;
232                         EditBone *ebo;
233                         
234                         /* Ensure all bones are correctly adjusted */
235                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
236                                 
237                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
238                                         /* If this bone has a parent tip that has been moved */
239                                         if (ebo->parent->flag & BONE_TIPSEL){
240                                                 VECCOPY (ebo->head, ebo->parent->tail);
241                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
242                                         }
243                                         /* If this bone has a parent tip that has NOT been moved */
244                                         else{
245                                                 VECCOPY (ebo->parent->tail, ebo->head);
246                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
247                                         }
248                                 }
249                                 
250                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
251                                 ebo->length= VecLenf(ebo->head, ebo->tail);
252                                 if(ebo->oldlength==0.0f) {
253                                         ebo->rad_head= 0.25f*ebo->length;
254                                         ebo->rad_tail= 0.10f*ebo->length;
255                                         ebo->dist= 0.25f*ebo->length;
256                                         if(ebo->parent) {
257                                                 if(ebo->rad_head > ebo->parent->rad_tail)
258                                                         ebo->rad_head= ebo->parent->rad_tail;
259                                         }
260                                 }
261                                 else if(t->mode!=TFM_BONE_ENVELOPE) {
262                                         /* if bones change length, lets do that for the deform distance as well */
263                                         ebo->dist*= ebo->length/ebo->oldlength;
264                                         ebo->rad_head*= ebo->length/ebo->oldlength;
265                                         ebo->rad_tail*= ebo->length/ebo->oldlength;
266                                         ebo->oldlength= ebo->length;
267                                 }
268                         }
269                         if(arm->flag & ARM_MIRROR_EDIT) 
270                                 transform_armature_mirror_update();
271                         
272                 }
273                 else if(G.obedit->type==OB_LATTICE) {
274                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
275                         
276                         if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
277                 }
278                 else {
279                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
280                 }
281         }
282         else if( (t->flag & T_POSE) && t->poseobj) {
283                 Object *ob= t->poseobj;
284                 bArmature *arm= ob->data;
285                 
286                 /* old optimize trick... this enforces to bypass the depgraph */
287                 if (!(arm->flag & ARM_DELAYDEFORM)) {
288                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
289                         
290                         /* bah, softbody exception... recalcdata doesnt reset */
291                         for(base= FIRSTBASE; base; base= base->next) {
292                                 if(base->object->recalc & OB_RECALC_DATA)
293                                         if(modifiers_isSoftbodyEnabled(base->object)) {
294                                                 base->object->softflag |= OB_SB_REDO;
295                                 }
296                         }
297                 }
298                 else
299                         where_is_pose(ob);
300         }
301         else if(t->spacetype==SPACE_IMAGE) {
302                 flushTransUVs(t);
303                 if (G.sima->flag & SI_LSCM_LIVE)
304                         unwrap_lscm_live_re_solve();
305         }
306         else {
307                 for(base= FIRSTBASE; base; base= base->next) {
308                         /* this flag is from depgraph, was stored in nitialize phase, handled in drawview.c */
309                         if(base->flag & BA_HAS_RECALC_OB)
310                                 base->object->recalc |= OB_RECALC_OB;
311                         if(base->flag & BA_HAS_RECALC_DATA)
312                                 base->object->recalc |= OB_RECALC_DATA;
313                         
314                         /* thanks to ob->ctime usage, ipos are not called in where_is_object,
315                            unless we edit ipokeys */
316                         if(base->flag & BA_DO_IPO) {
317                                 if(base->object->ipo) {
318                                         IpoCurve *icu;
319                                         
320                                         base->object->ctime= -1234567.0;
321                                         
322                                         icu= base->object->ipo->curve.first;
323                                         while(icu) {
324                                                 calchandles_ipocurve(icu);
325                                                 icu= icu->next;
326                                         }
327                                 }                               
328                         }
329                         
330                         /* softbody exception */
331                         if(modifiers_isSoftbodyEnabled(base->object)) {
332                                 if(base->object->recalc & OB_RECALC_DATA)
333                                         base->object->softflag |= OB_SB_REDO;
334                         }
335                 } 
336         }
337         
338         /* update shaded drawmode while transform */
339         if(t->spacetype==SPACE_VIEW3D && G.vd->drawtype == OB_SHADED)
340                 reshadeall_displist();
341         
342 }
343
344 void initTransModeFlags(TransInfo *t, int mode) 
345 {
346         t->mode = mode;
347         t->num.flag = 0;
348
349         /* REMOVING RESTRICTIONS FLAGS */
350         t->flag &= ~T_ALL_RESTRICTIONS;
351         
352         switch (mode) {
353         case TFM_RESIZE:
354                 t->flag |= T_NULL_ONE;
355                 t->num.flag |= NUM_NULL_ONE;
356                 t->num.flag |= NUM_AFFECT_ALL;
357                 if (!G.obedit) {
358                         t->flag |= T_NO_ZERO;
359                         t->num.flag |= NUM_NO_ZERO;
360                 }
361                 break;
362         case TFM_TOSPHERE:
363                 t->num.flag |= NUM_NULL_ONE;
364                 t->num.flag |= NUM_NO_NEGATIVE;
365                 t->flag |= T_NO_CONSTRAINT;
366                 break;
367         case TFM_SHEAR:
368         case TFM_CREASE:
369         case TFM_BONE_ENVELOPE:
370                 t->flag |= T_NO_CONSTRAINT;
371                 break;
372         }
373 }
374
375 void drawLine(float *center, float *dir, char axis, short options)
376 {
377         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
378         float v1[3], v2[3], v3[3];
379         char col[3], col2[3];
380         
381         //if(G.obedit) mymultmatrix(G.obedit->obmat);   // sets opengl viewing
382
383         VecCopyf(v3, dir);
384         VecMulf(v3, G.vd->far);
385         
386         VecSubf(v2, center, v3);
387         VecAddf(v1, center, v3);
388
389         if (options & DRAWLIGHT) {
390                 col[0] = col[1] = col[2] = 220;
391         }
392         else {
393                 BIF_GetThemeColor3ubv(TH_GRID, col);
394         }
395         make_axis_color(col, col2, axis);
396         glColor3ubv(col2);
397
398         setlinestyle(0);
399         glBegin(GL_LINE_STRIP); 
400                 glVertex3fv(v1); 
401                 glVertex3fv(v2); 
402         glEnd();
403         
404         myloadmatrix(G.vd->viewmat);
405 }
406
407 void initTrans (TransInfo *t)
408 {
409         
410         /* moving: is shown in drawobject() (transform color) */
411         if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
412         else G.moving= G_TRANSFORM_OBJ;
413
414         t->data = NULL;
415         t->ext = NULL;
416
417         t->flag = 0;
418
419         /* setting PET flag */
420         if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
421                 t->flag |= T_PROP_EDIT;
422                 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED;       // yes i know, has to become define
423         }
424
425         getmouseco_areawin(t->imval);
426         t->con.imval[0] = t->imval[0];
427         t->con.imval[1] = t->imval[1];
428
429         t->transform            = NULL;
430
431         t->total                        =
432                 t->num.idx              =
433                 t->num.idx_max  =
434                 t->num.ctrl[0]  = 
435                 t->num.ctrl[1]  = 
436                 t->num.ctrl[2]  = 0;
437
438         t->val = 0.0f;
439
440         t->num.val[0]           = 
441                 t->num.val[1]   = 
442                 t->num.val[2]   = 0.0f;
443
444         t->vec[0]                       =
445                 t->vec[1]               =
446                 t->vec[2]               = 0.0f;
447         
448         Mat3One(t->mat);
449         
450         t->spacetype = curarea->spacetype;
451         if(t->spacetype==SPACE_VIEW3D) {
452                 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
453                 t->around = G.vd->around;
454         }
455         else
456                 t->around = V3D_CENTRE;
457
458         setTransformViewMatrices(t);
459 }
460
461 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
462 void postTrans (TransInfo *t) 
463 {
464         G.moving = 0; // Set moving flag off (display as usual)
465
466         stopConstraint(t);
467         
468         /* postTrans can be called when nothing is selected, so data is NULL already */
469         if (t->data) {
470                 TransData *td;
471                 int a;
472
473                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
474                 for(a=0, td= t->data; a<t->total; a++, td++) {
475                         if(td->tdi) MEM_freeN(td->tdi);
476                 }
477                 MEM_freeN(t->data);
478         }
479
480         if (t->ext) MEM_freeN(t->ext);
481         if (t->data2d) {
482                 MEM_freeN(t->data2d);
483                 t->data2d= NULL;
484         }
485
486         if(t->spacetype==SPACE_IMAGE) {
487                 if (G.sima->flag & SI_LSCM_LIVE)
488                         unwrap_lscm_live_end();
489         }
490 }
491
492 static void apply_grid3(TransInfo *t, float *val, int max_index, float fac1, float fac2, float fac3)
493 {
494         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
495         int invert;
496         int ctrl;
497         int i;
498         float asp= 1.0f;
499
500         if(t->mode==TFM_ROTATION || t->mode==TFM_WARP || t->mode==TFM_TILT || t->mode==TFM_TRACKBALL)
501                 invert = U.flag & USER_AUTOROTGRID;
502         else if(t->mode==TFM_RESIZE || t->mode==TFM_SHEAR || t->mode==TFM_BONESIZE || t->mode==TFM_SHRINKFATTEN)
503                 invert = U.flag & USER_AUTOSIZEGRID;
504         else
505                 invert = U.flag & USER_AUTOGRABGRID;
506         
507         for (i=0; i<=max_index; i++) {
508
509                 /* evil hack - snapping needs to be adapted for image aspect ratio */
510                 if(t->spacetype==SPACE_IMAGE) {
511                         float aspx, aspy;
512                         transform_aspect_ratio_tface_uv(&aspx, &aspy);
513                         if(i==0) asp= aspx;
514                         else asp= aspy;
515                 }
516
517                 if(invert) {
518                         if(G.qual & LR_CTRLKEY) ctrl= 0;
519                         else ctrl= 1;
520                 }
521                 else ctrl= (G.qual & LR_CTRLKEY);
522
523                 if(ctrl && (G.qual & LR_SHIFTKEY)) {
524                         if(fac3!= 0.0) {
525                                 for (i=0; i<=max_index; i++) {
526                                         val[i]= fac3*asp*(float)floor(val[i]/(fac3*asp) +.5);
527                                 }
528                         }
529                 }
530                 else if(ctrl) {
531                         if(fac2!= 0.0) {
532                                 for (i=0; i<=max_index; i++) {
533                                         val[i]= fac2*asp*(float)floor(val[i]/(fac2*asp) +.5);
534                                 }
535                         }
536                 }
537                 else {
538                         if(fac1!= 0.0) {
539                                 for (i=0; i<=max_index; i++) {
540                                         val[i]= fac1*asp*(float)floor(val[i]/(fac1*asp) +.5);
541                                 }
542                         }
543                 }
544         }
545 }
546
547 void snapGrid(TransInfo *t, float *val) {
548         apply_grid3(t, val, t->idx_max, t->snap[0], t->snap[1], t->snap[2]);
549 }
550
551 void applyTransObjects(TransInfo *t)
552 {
553         TransData *td;
554         
555         for (td = t->data; td < t->data + t->total; td++) {
556                 VECCOPY(td->iloc, td->loc);
557                 if (td->ext->rot) {
558                         VECCOPY(td->ext->irot, td->ext->rot);
559                 }
560                 if (td->ext->size) {
561                         VECCOPY(td->ext->isize, td->ext->size);
562                 }
563         }       
564         recalcData(t);
565
566
567 /* helper for below */
568 static void restore_ipokey(float *poin, float *old)
569 {
570         if(poin) {
571                 poin[0]= old[0];
572                 poin[-3]= old[3];
573                 poin[3]= old[6];
574         }
575 }
576
577 static void restoreElement(TransData *td) {
578         /* TransData for crease has no loc */
579         if (td->loc) {
580                 VECCOPY(td->loc, td->iloc);
581         }
582         if (td->val) {
583                 *td->val = td->ival;
584         }
585         if (td->ext) {
586                 if (td->ext->rot) {
587                         VECCOPY(td->ext->rot, td->ext->irot);
588                 }
589                 if (td->ext->size) {
590                         VECCOPY(td->ext->size, td->ext->isize);
591                 }
592                 if(td->flag & TD_USEQUAT) {
593                         if (td->ext->quat) {
594                                 QUATCOPY(td->ext->quat, td->ext->iquat);
595                         }
596                 }
597         }
598         if(td->tdi) {
599                 TransDataIpokey *tdi= td->tdi;
600                 
601                 restore_ipokey(tdi->locx, tdi->oldloc);
602                 restore_ipokey(tdi->locy, tdi->oldloc+1);
603                 restore_ipokey(tdi->locz, tdi->oldloc+2);
604
605                 restore_ipokey(tdi->rotx, tdi->oldrot);
606                 restore_ipokey(tdi->roty, tdi->oldrot+1);
607                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
608                 
609                 restore_ipokey(tdi->sizex, tdi->oldsize);
610                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
611                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
612         }
613 }
614
615 void restoreTransObjects(TransInfo *t)
616 {
617         TransData *td;
618         
619         for (td = t->data; td < t->data + t->total; td++) {
620                 restoreElement(td);
621         }       
622         recalcData(t);
623 }
624
625 void calculateCenter2D(TransInfo *t)
626 {
627         if (t->flag & (T_EDIT|T_POSE)) {
628                 Object *ob= G.obedit?G.obedit:t->poseobj;
629                 float vec[3];
630                 
631                 VECCOPY(vec, t->center);
632                 Mat4MulVecfl(ob->obmat, vec);
633                 projectIntView(t, vec, t->center2d);
634         }
635         else {
636                 projectIntView(t, t->center, t->center2d);
637         }
638 }
639
640 void calculateCenterCursor(TransInfo *t)
641 {
642         float *cursor;
643
644         cursor = give_cursor();
645         VECCOPY(t->center, cursor);
646
647         /* If edit or pose mode, move cursor in local space */
648         if(t->flag & (T_EDIT|T_POSE)) {
649                 Object *ob= G.obedit?G.obedit:t->poseobj;
650                 float mat[3][3], imat[3][3];
651                 
652                 VecSubf(t->center, t->center, ob->obmat[3]);
653                 Mat3CpyMat4(mat, ob->obmat);
654                 Mat3Inv(imat, mat);
655                 Mat3MulVecfl(imat, t->center);
656         }
657         
658         calculateCenter2D(t);
659 }
660
661 void calculateCenterMedian(TransInfo *t)
662 {
663         float partial[3] = {0.0f, 0.0f, 0.0f};
664         int i;
665         for(i = 0; i < t->total; i++) {
666                 if (t->data[i].flag & TD_SELECTED) {
667                         VecAddf(partial, partial, t->data[i].center);
668                 }
669                 else {
670                         /* 
671                            All the selected elements are at the head of the array 
672                            which means we can stop when it finds unselected data
673                         */
674                         break;
675                 }
676         }
677         VecMulf(partial, 1.0f / i);
678         VECCOPY(t->center, partial);
679
680         calculateCenter2D(t);
681 }
682
683 void calculateCenterBound(TransInfo *t)
684 {
685         float max[3];
686         float min[3];
687         int i;
688         for(i = 0; i < t->total; i++) {
689                 if (i) {
690                         if (t->data[i].flag & TD_SELECTED) {
691                                 MinMax3(min, max, t->data[i].center);
692                         }
693                         else {
694                                 /* 
695                                    All the selected elements are at the head of the array 
696                                    which means we can stop when it finds unselected data
697                                 */
698                                 break;
699                         }
700                 }
701                 else {
702                         VECCOPY(max, t->data[i].center);
703                         VECCOPY(min, t->data[i].center);
704                 }
705         }
706         VecAddf(t->center, min, max);
707         VecMulf(t->center, 0.5);
708
709         calculateCenter2D(t);
710 }
711
712 void calculateCenter(TransInfo *t) 
713 {
714         switch(t->around) {
715         case V3D_CENTRE:
716                 calculateCenterBound(t);
717                 break;
718         case V3D_CENTROID:
719                 calculateCenterMedian(t);
720                 break;
721         case V3D_CURSOR:
722                 calculateCenterCursor(t);
723                 break;
724         case V3D_LOCAL:
725                 /* Individual element center uses median center for helpline and such */
726                 calculateCenterMedian(t);
727                 break;
728         case V3D_ACTIVE:
729                 /* set median, and if if if... do object center */
730                 calculateCenterMedian(t);
731                 if((t->flag & (T_EDIT|T_POSE))==0) {
732                         Object *ob= OBACT;
733                         if(ob) {
734                                 VECCOPY(t->center, ob->obmat[3]);
735                                 projectIntView(t, t->center, t->center2d);
736                         }
737                 }
738                 
739         }
740
741         /* setting constraint center */
742         VECCOPY(t->con.center, t->center);
743         if(t->flag & (T_EDIT|T_POSE)) {
744                 Object *ob= G.obedit?G.obedit:t->poseobj;
745                 Mat4MulVecfl(ob->obmat, t->con.center);
746         }
747
748         /* voor panning from cameraview */
749         if(t->flag & T_OBJECT) {
750                 if( G.vd->camera==OBACT && G.vd->persp>1) {
751                         float axis[3];
752                         /* persinv is nasty, use viewinv instead, always right */
753                         VECCOPY(axis, t->viewinv[2]);
754                         Normalise(axis);
755
756                         /* 6.0 = 6 grid units */
757                         axis[0]= t->center[0]- 6.0f*axis[0];
758                         axis[1]= t->center[1]- 6.0f*axis[1];
759                         axis[2]= t->center[2]- 6.0f*axis[2];
760                         
761                         projectIntView(t, axis, t->center2d);
762                         
763                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
764                         if(t->mode==TFM_TRANSLATION) {
765                                 VECCOPY(t->center, axis);
766                                 VECCOPY(t->con.center, t->center);
767                         }
768                 }
769         }       
770
771         if(t->spacetype==SPACE_VIEW3D)
772                 initgrabz(t->center[0], t->center[1], t->center[2]);
773 }
774
775 void calculatePropRatio(TransInfo *t)
776 {
777         TransData *td = t->data;
778         int i;
779         float dist;
780         short connected = t->flag & T_PROP_CONNECTED;
781
782         if (t->flag & T_PROP_EDIT) {
783                 for(i = 0 ; i < t->total; i++, td++) {
784                         if (td->flag & TD_SELECTED) {
785                                 td->factor = 1.0f;
786                         }
787                         else if ((connected && 
788                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
789                                 ||
790                                         (connected == 0 &&
791                                                 td->rdist > t->propsize)) {
792                                 /* 
793                                    The elements are sorted according to their dist member in the array,
794                                    that means we can stop when it finds one element outside of the propsize.
795                                 */
796                                 td->flag |= TD_NOACTION;
797                                 td->factor = 0.0f;
798                                 restoreElement(td);
799                         }
800                         else {
801                                 /* Use rdist for falloff calculations, it is the real distance */
802                                 td->flag &= ~TD_NOACTION;
803                                 dist= (t->propsize-td->rdist)/t->propsize;
804                                 switch(G.scene->prop_mode) {
805                                 case PROP_SHARP:
806                                         td->factor= dist*dist;
807                                         break;
808                                 case PROP_SMOOTH:
809                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
810                                         break;
811                                 case PROP_ROOT:
812                                         td->factor = (float)sqrt(dist);
813                                         break;
814                                 case PROP_LIN:
815                                         td->factor = dist;
816                                         break;
817                                 case PROP_CONST:
818                                         td->factor = 1.0f;
819                                         break;
820                                 case PROP_SPHERE:
821                                         td->factor = (float)sqrt(2*dist - dist * dist);
822                                         break;
823                                 default:
824                                         td->factor = 1;
825                                 }
826                         }
827                 }
828                 switch(G.scene->prop_mode) {
829                 case PROP_SHARP:
830                         strcpy(t->proptext, "(Sharp)");
831                         break;
832                 case PROP_SMOOTH:
833                         strcpy(t->proptext, "(Smooth)");
834                         break;
835                 case PROP_ROOT:
836                         strcpy(t->proptext, "(Root)");
837                         break;
838                 case PROP_LIN:
839                         strcpy(t->proptext, "(Linear)");
840                         break;
841                 case PROP_CONST:
842                         strcpy(t->proptext, "(Constant)");
843                         break;
844                 case PROP_SPHERE:
845                         strcpy(t->proptext, "(Sphere)");
846                         break;
847                 default:
848                         strcpy(t->proptext, "");
849                 }
850         }
851         else {
852                 for(i = 0 ; i < t->total; i++, td++) {
853                         td->factor = 1.0;
854                 }
855                 strcpy(t->proptext, "");
856         }
857 }
858
859 TransInfo * BIF_GetTransInfo() {
860         return &Trans;
861 }
862