* Shrink/Fatten for bevelled curves
[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_LIVE_UNWRAP)
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         case TFM_CURVE_SHRINKFATTEN:
371                 t->flag |= T_NO_CONSTRAINT;
372                 break;
373         }
374 }
375
376 void drawLine(float *center, float *dir, char axis, short options)
377 {
378         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
379         float v1[3], v2[3], v3[3];
380         char col[3], col2[3];
381         
382         //if(G.obedit) mymultmatrix(G.obedit->obmat);   // sets opengl viewing
383
384         VecCopyf(v3, dir);
385         VecMulf(v3, G.vd->far);
386         
387         VecSubf(v2, center, v3);
388         VecAddf(v1, center, v3);
389
390         if (options & DRAWLIGHT) {
391                 col[0] = col[1] = col[2] = 220;
392         }
393         else {
394                 BIF_GetThemeColor3ubv(TH_GRID, col);
395         }
396         make_axis_color(col, col2, axis);
397         glColor3ubv(col2);
398
399         setlinestyle(0);
400         glBegin(GL_LINE_STRIP); 
401                 glVertex3fv(v1); 
402                 glVertex3fv(v2); 
403         glEnd();
404         
405         myloadmatrix(G.vd->viewmat);
406 }
407
408 void initTrans (TransInfo *t)
409 {
410         
411         /* moving: is shown in drawobject() (transform color) */
412         if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
413         else G.moving= G_TRANSFORM_OBJ;
414
415         t->data = NULL;
416         t->ext = NULL;
417
418         t->flag = 0;
419
420         /* setting PET flag */
421         if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
422                 t->flag |= T_PROP_EDIT;
423                 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED;       // yes i know, has to become define
424         }
425
426         getmouseco_areawin(t->imval);
427         t->con.imval[0] = t->imval[0];
428         t->con.imval[1] = t->imval[1];
429
430         t->transform            = NULL;
431
432         t->total                        =
433                 t->num.idx              =
434                 t->num.idx_max  =
435                 t->num.ctrl[0]  = 
436                 t->num.ctrl[1]  = 
437                 t->num.ctrl[2]  = 0;
438
439         t->val = 0.0f;
440
441         t->num.val[0]           = 
442                 t->num.val[1]   = 
443                 t->num.val[2]   = 0.0f;
444
445         t->vec[0]                       =
446                 t->vec[1]               =
447                 t->vec[2]               = 0.0f;
448         
449         Mat3One(t->mat);
450         
451         t->spacetype = curarea->spacetype;
452         if(t->spacetype==SPACE_VIEW3D) {
453                 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
454                 t->around = G.vd->around;
455         }
456         else
457                 t->around = V3D_CENTRE;
458
459         setTransformViewMatrices(t);
460 }
461
462 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
463 void postTrans (TransInfo *t) 
464 {
465         G.moving = 0; // Set moving flag off (display as usual)
466
467         stopConstraint(t);
468         
469         /* postTrans can be called when nothing is selected, so data is NULL already */
470         if (t->data) {
471                 TransData *td;
472                 int a;
473
474                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
475                 for(a=0, td= t->data; a<t->total; a++, td++) {
476                         if(td->tdi) MEM_freeN(td->tdi);
477                 }
478                 MEM_freeN(t->data);
479         }
480
481         if (t->ext) MEM_freeN(t->ext);
482         if (t->data2d) {
483                 MEM_freeN(t->data2d);
484                 t->data2d= NULL;
485         }
486
487         if(t->spacetype==SPACE_IMAGE) {
488                 if (G.sima->flag & SI_LIVE_UNWRAP)
489                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
490         }
491 }
492
493 static void apply_grid3(TransInfo *t, float *val, int max_index, float fac1, float fac2, float fac3)
494 {
495         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
496         int invert;
497         int ctrl;
498         int i;
499         float asp= 1.0f;
500
501         if(t->mode==TFM_ROTATION || t->mode==TFM_WARP || t->mode==TFM_TILT || t->mode==TFM_TRACKBALL)
502                 invert = U.flag & USER_AUTOROTGRID;
503         else if(t->mode==TFM_RESIZE || t->mode==TFM_SHEAR || t->mode==TFM_BONESIZE || t->mode==TFM_SHRINKFATTEN || t->mode==TFM_CURVE_SHRINKFATTEN)
504                 invert = U.flag & USER_AUTOSIZEGRID;
505         else
506                 invert = U.flag & USER_AUTOGRABGRID;
507         
508         for (i=0; i<=max_index; i++) {
509
510                 /* evil hack - snapping needs to be adapted for image aspect ratio */
511                 if((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
512                         float aspx, aspy;
513                         transform_aspect_ratio_tface_uv(&aspx, &aspy);
514                         if(i==0) asp= aspx;
515                         else asp= aspy;
516                 }
517
518                 if(invert) {
519                         if(G.qual & LR_CTRLKEY) ctrl= 0;
520                         else ctrl= 1;
521                 }
522                 else ctrl= (G.qual & LR_CTRLKEY);
523
524                 if(ctrl && (G.qual & LR_SHIFTKEY)) {
525                         if(fac3!= 0.0) {
526                                 for (i=0; i<=max_index; i++) {
527                                         val[i]= fac3*asp*(float)floor(val[i]/(fac3*asp) +.5);
528                                 }
529                         }
530                 }
531                 else if(ctrl) {
532                         if(fac2!= 0.0) {
533                                 for (i=0; i<=max_index; i++) {
534                                         val[i]= fac2*asp*(float)floor(val[i]/(fac2*asp) +.5);
535                                 }
536                         }
537                 }
538                 else {
539                         if(fac1!= 0.0) {
540                                 for (i=0; i<=max_index; i++) {
541                                         val[i]= fac1*asp*(float)floor(val[i]/(fac1*asp) +.5);
542                                 }
543                         }
544                 }
545         }
546 }
547
548 void snapGrid(TransInfo *t, float *val) {
549         apply_grid3(t, val, t->idx_max, t->snap[0], t->snap[1], t->snap[2]);
550 }
551
552 void applyTransObjects(TransInfo *t)
553 {
554         TransData *td;
555         
556         for (td = t->data; td < t->data + t->total; td++) {
557                 VECCOPY(td->iloc, td->loc);
558                 if (td->ext->rot) {
559                         VECCOPY(td->ext->irot, td->ext->rot);
560                 }
561                 if (td->ext->size) {
562                         VECCOPY(td->ext->isize, td->ext->size);
563                 }
564         }       
565         recalcData(t);
566
567
568 /* helper for below */
569 static void restore_ipokey(float *poin, float *old)
570 {
571         if(poin) {
572                 poin[0]= old[0];
573                 poin[-3]= old[3];
574                 poin[3]= old[6];
575         }
576 }
577
578 static void restoreElement(TransData *td) {
579         /* TransData for crease has no loc */
580         if (td->loc) {
581                 VECCOPY(td->loc, td->iloc);
582         }
583         if (td->val) {
584                 *td->val = td->ival;
585         }
586         if (td->ext) {
587                 if (td->ext->rot) {
588                         VECCOPY(td->ext->rot, td->ext->irot);
589                 }
590                 if (td->ext->size) {
591                         VECCOPY(td->ext->size, td->ext->isize);
592                 }
593                 if(td->flag & TD_USEQUAT) {
594                         if (td->ext->quat) {
595                                 QUATCOPY(td->ext->quat, td->ext->iquat);
596                         }
597                 }
598         }
599         if(td->tdi) {
600                 TransDataIpokey *tdi= td->tdi;
601                 
602                 restore_ipokey(tdi->locx, tdi->oldloc);
603                 restore_ipokey(tdi->locy, tdi->oldloc+1);
604                 restore_ipokey(tdi->locz, tdi->oldloc+2);
605
606                 restore_ipokey(tdi->rotx, tdi->oldrot);
607                 restore_ipokey(tdi->roty, tdi->oldrot+1);
608                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
609                 
610                 restore_ipokey(tdi->sizex, tdi->oldsize);
611                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
612                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
613         }
614 }
615
616 void restoreTransObjects(TransInfo *t)
617 {
618         TransData *td;
619         
620         for (td = t->data; td < t->data + t->total; td++) {
621                 restoreElement(td);
622         }       
623         recalcData(t);
624 }
625
626 void calculateCenter2D(TransInfo *t)
627 {
628         if (t->flag & (T_EDIT|T_POSE)) {
629                 Object *ob= G.obedit?G.obedit:t->poseobj;
630                 float vec[3];
631                 
632                 VECCOPY(vec, t->center);
633                 Mat4MulVecfl(ob->obmat, vec);
634                 projectIntView(t, vec, t->center2d);
635         }
636         else {
637                 projectIntView(t, t->center, t->center2d);
638         }
639 }
640
641 void calculateCenterCursor(TransInfo *t)
642 {
643         float *cursor;
644
645         cursor = give_cursor();
646         VECCOPY(t->center, cursor);
647
648         /* If edit or pose mode, move cursor in local space */
649         if(t->flag & (T_EDIT|T_POSE)) {
650                 Object *ob= G.obedit?G.obedit:t->poseobj;
651                 float mat[3][3], imat[3][3];
652                 
653                 VecSubf(t->center, t->center, ob->obmat[3]);
654                 Mat3CpyMat4(mat, ob->obmat);
655                 Mat3Inv(imat, mat);
656                 Mat3MulVecfl(imat, t->center);
657         }
658         
659         calculateCenter2D(t);
660 }
661
662 void calculateCenterMedian(TransInfo *t)
663 {
664         float partial[3] = {0.0f, 0.0f, 0.0f};
665         int i;
666         for(i = 0; i < t->total; i++) {
667                 if (t->data[i].flag & TD_SELECTED) {
668                         VecAddf(partial, partial, t->data[i].center);
669                 }
670                 else {
671                         /* 
672                            All the selected elements are at the head of the array 
673                            which means we can stop when it finds unselected data
674                         */
675                         break;
676                 }
677         }
678         VecMulf(partial, 1.0f / i);
679         VECCOPY(t->center, partial);
680
681         calculateCenter2D(t);
682 }
683
684 void calculateCenterBound(TransInfo *t)
685 {
686         float max[3];
687         float min[3];
688         int i;
689         for(i = 0; i < t->total; i++) {
690                 if (i) {
691                         if (t->data[i].flag & TD_SELECTED) {
692                                 MinMax3(min, max, t->data[i].center);
693                         }
694                         else {
695                                 /* 
696                                    All the selected elements are at the head of the array 
697                                    which means we can stop when it finds unselected data
698                                 */
699                                 break;
700                         }
701                 }
702                 else {
703                         VECCOPY(max, t->data[i].center);
704                         VECCOPY(min, t->data[i].center);
705                 }
706         }
707         VecAddf(t->center, min, max);
708         VecMulf(t->center, 0.5);
709
710         calculateCenter2D(t);
711 }
712
713 void calculateCenter(TransInfo *t) 
714 {
715         switch(t->around) {
716         case V3D_CENTRE:
717                 calculateCenterBound(t);
718                 break;
719         case V3D_CENTROID:
720                 calculateCenterMedian(t);
721                 break;
722         case V3D_CURSOR:
723                 calculateCenterCursor(t);
724                 break;
725         case V3D_LOCAL:
726                 /* Individual element center uses median center for helpline and such */
727                 calculateCenterMedian(t);
728                 break;
729         case V3D_ACTIVE:
730                 /* set median, and if if if... do object center */
731                 
732                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
733                 if (G.obedit && G.obedit->type == OB_MESH && G.editMesh->selected.last) {
734                         EM_editselection_center(t->center, G.editMesh->selected.last);
735                         calculateCenter2D(t);
736                         break;
737                 } /* END EDIT MODE ACTIVE ELEMENT */
738                 
739                 calculateCenterMedian(t);
740                 if((t->flag & (T_EDIT|T_POSE))==0) {
741                         Object *ob= OBACT;
742                         if(ob) {
743                                 VECCOPY(t->center, ob->obmat[3]);
744                                 projectIntView(t, t->center, t->center2d);
745                         }
746                 }
747         }
748
749         /* setting constraint center */
750         VECCOPY(t->con.center, t->center);
751         if(t->flag & (T_EDIT|T_POSE)) {
752                 Object *ob= G.obedit?G.obedit:t->poseobj;
753                 Mat4MulVecfl(ob->obmat, t->con.center);
754         }
755
756         /* voor panning from cameraview */
757         if(t->flag & T_OBJECT) {
758                 if( G.vd->camera==OBACT && G.vd->persp>1) {
759                         float axis[3];
760                         /* persinv is nasty, use viewinv instead, always right */
761                         VECCOPY(axis, t->viewinv[2]);
762                         Normalise(axis);
763
764                         /* 6.0 = 6 grid units */
765                         axis[0]= t->center[0]- 6.0f*axis[0];
766                         axis[1]= t->center[1]- 6.0f*axis[1];
767                         axis[2]= t->center[2]- 6.0f*axis[2];
768                         
769                         projectIntView(t, axis, t->center2d);
770                         
771                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
772                         if(t->mode==TFM_TRANSLATION) {
773                                 VECCOPY(t->center, axis);
774                                 VECCOPY(t->con.center, t->center);
775                         }
776                 }
777         }       
778
779         if(t->spacetype==SPACE_VIEW3D)
780                 initgrabz(t->center[0], t->center[1], t->center[2]);
781 }
782
783 void calculatePropRatio(TransInfo *t)
784 {
785         TransData *td = t->data;
786         int i;
787         float dist;
788         short connected = t->flag & T_PROP_CONNECTED;
789
790         if (t->flag & T_PROP_EDIT) {
791                 for(i = 0 ; i < t->total; i++, td++) {
792                         if (td->flag & TD_SELECTED) {
793                                 td->factor = 1.0f;
794                         }
795                         else if ((connected && 
796                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
797                                 ||
798                                         (connected == 0 &&
799                                                 td->rdist > t->propsize)) {
800                                 /* 
801                                    The elements are sorted according to their dist member in the array,
802                                    that means we can stop when it finds one element outside of the propsize.
803                                 */
804                                 td->flag |= TD_NOACTION;
805                                 td->factor = 0.0f;
806                                 restoreElement(td);
807                         }
808                         else {
809                                 /* Use rdist for falloff calculations, it is the real distance */
810                                 td->flag &= ~TD_NOACTION;
811                                 dist= (t->propsize-td->rdist)/t->propsize;
812                                 switch(G.scene->prop_mode) {
813                                 case PROP_SHARP:
814                                         td->factor= dist*dist;
815                                         break;
816                                 case PROP_SMOOTH:
817                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
818                                         break;
819                                 case PROP_ROOT:
820                                         td->factor = (float)sqrt(dist);
821                                         break;
822                                 case PROP_LIN:
823                                         td->factor = dist;
824                                         break;
825                                 case PROP_CONST:
826                                         td->factor = 1.0f;
827                                         break;
828                                 case PROP_SPHERE:
829                                         td->factor = (float)sqrt(2*dist - dist * dist);
830                                         break;
831                                 default:
832                                         td->factor = 1;
833                                 }
834                         }
835                 }
836                 switch(G.scene->prop_mode) {
837                 case PROP_SHARP:
838                         strcpy(t->proptext, "(Sharp)");
839                         break;
840                 case PROP_SMOOTH:
841                         strcpy(t->proptext, "(Smooth)");
842                         break;
843                 case PROP_ROOT:
844                         strcpy(t->proptext, "(Root)");
845                         break;
846                 case PROP_LIN:
847                         strcpy(t->proptext, "(Linear)");
848                         break;
849                 case PROP_CONST:
850                         strcpy(t->proptext, "(Constant)");
851                         break;
852                 case PROP_SPHERE:
853                         strcpy(t->proptext, "(Sphere)");
854                         break;
855                 default:
856                         strcpy(t->proptext, "");
857                 }
858         }
859         else {
860                 for(i = 0 ; i < t->total; i++, td++) {
861                         td->factor = 1.0;
862                 }
863                 strcpy(t->proptext, "");
864         }
865 }
866
867 TransInfo * BIF_GetTransInfo() {
868         return &Trans;
869 }
870