More work on the new unwrapper code (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 (G.sima->flag & SI_LSCM_LIVE)
487                 unwrap_lscm_live_end();
488 }
489
490 static void apply_grid3(TransInfo *t, float *val, int max_index, float fac1, float fac2, float fac3)
491 {
492         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
493         int invert = U.flag & USER_AUTOGRABGRID;
494         int ctrl;
495         int i;
496         float asp= 1.0f;
497
498         for (i=0; i<=max_index; i++) {
499
500                 /* evil hack - snapping needs to be adapted for image aspect ratio */
501                 if(t->spacetype==SPACE_IMAGE) {
502                         float aspx, aspy;
503                         transform_aspect_ratio_tface_uv(&aspx, &aspy);
504                         if(i==0) asp= aspx;
505                         else asp= aspy;
506                 }
507
508                 if(invert) {
509                         if(G.qual & LR_CTRLKEY) ctrl= 0;
510                         else ctrl= 1;
511                 }
512                 else ctrl= (G.qual & LR_CTRLKEY);
513
514                 if(ctrl && (G.qual & LR_SHIFTKEY)) {
515                         if(fac3!= 0.0) {
516                                 for (i=0; i<=max_index; i++) {
517                                         val[i]= fac3*asp*(float)floor(val[i]/(fac3*asp) +.5);
518                                 }
519                         }
520                 }
521                 else if(ctrl) {
522                         if(fac2!= 0.0) {
523                                 for (i=0; i<=max_index; i++) {
524                                         val[i]= fac2*asp*(float)floor(val[i]/(fac2*asp) +.5);
525                                 }
526                         }
527                 }
528                 else {
529                         if(fac1!= 0.0) {
530                                 for (i=0; i<=max_index; i++) {
531                                         val[i]= fac1*asp*(float)floor(val[i]/(fac1*asp) +.5);
532                                 }
533                         }
534                 }
535         }
536 }
537
538 void snapGrid(TransInfo *t, float *val) {
539         apply_grid3(t, val, t->idx_max, t->snap[0], t->snap[1], t->snap[2]);
540 }
541
542 void applyTransObjects(TransInfo *t)
543 {
544         TransData *td;
545         
546         for (td = t->data; td < t->data + t->total; td++) {
547                 VECCOPY(td->iloc, td->loc);
548                 if (td->ext->rot) {
549                         VECCOPY(td->ext->irot, td->ext->rot);
550                 }
551                 if (td->ext->size) {
552                         VECCOPY(td->ext->isize, td->ext->size);
553                 }
554         }       
555         recalcData(t);
556
557
558 /* helper for below */
559 static void restore_ipokey(float *poin, float *old)
560 {
561         if(poin) {
562                 poin[0]= old[0];
563                 poin[-3]= old[3];
564                 poin[3]= old[6];
565         }
566 }
567
568 static void restoreElement(TransData *td) {
569         /* TransData for crease has no loc */
570         if (td->loc) {
571                 VECCOPY(td->loc, td->iloc);
572         }
573         if (td->val) {
574                 *td->val = td->ival;
575         }
576         if (td->ext) {
577                 if (td->ext->rot) {
578                         VECCOPY(td->ext->rot, td->ext->irot);
579                 }
580                 if (td->ext->size) {
581                         VECCOPY(td->ext->size, td->ext->isize);
582                 }
583                 if(td->flag & TD_USEQUAT) {
584                         if (td->ext->quat) {
585                                 QUATCOPY(td->ext->quat, td->ext->iquat);
586                         }
587                 }
588         }
589         if(td->tdi) {
590                 TransDataIpokey *tdi= td->tdi;
591                 
592                 restore_ipokey(tdi->locx, tdi->oldloc);
593                 restore_ipokey(tdi->locy, tdi->oldloc+1);
594                 restore_ipokey(tdi->locz, tdi->oldloc+2);
595
596                 restore_ipokey(tdi->rotx, tdi->oldrot);
597                 restore_ipokey(tdi->roty, tdi->oldrot+1);
598                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
599                 
600                 restore_ipokey(tdi->sizex, tdi->oldsize);
601                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
602                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
603         }
604 }
605
606 void restoreTransObjects(TransInfo *t)
607 {
608         TransData *td;
609         
610         for (td = t->data; td < t->data + t->total; td++) {
611                 restoreElement(td);
612         }       
613         recalcData(t);
614 }
615
616 void calculateCenter2D(TransInfo *t)
617 {
618         if (t->flag & (T_EDIT|T_POSE)) {
619                 Object *ob= G.obedit?G.obedit:t->poseobj;
620                 float vec[3];
621                 
622                 VECCOPY(vec, t->center);
623                 Mat4MulVecfl(ob->obmat, vec);
624                 projectIntView(t, vec, t->center2d);
625         }
626         else {
627                 projectIntView(t, t->center, t->center2d);
628         }
629 }
630
631 void calculateCenterCursor(TransInfo *t)
632 {
633         float *cursor;
634
635         cursor = give_cursor();
636         VECCOPY(t->center, cursor);
637
638         /* If edit or pose mode, move cursor in local space */
639         if(t->flag & (T_EDIT|T_POSE)) {
640                 Object *ob= G.obedit?G.obedit:t->poseobj;
641                 float mat[3][3], imat[3][3];
642                 
643                 VecSubf(t->center, t->center, ob->obmat[3]);
644                 Mat3CpyMat4(mat, ob->obmat);
645                 Mat3Inv(imat, mat);
646                 Mat3MulVecfl(imat, t->center);
647         }
648         
649         calculateCenter2D(t);
650 }
651
652 void calculateCenterMedian(TransInfo *t)
653 {
654         float partial[3] = {0.0f, 0.0f, 0.0f};
655         int i;
656         for(i = 0; i < t->total; i++) {
657                 if (t->data[i].flag & TD_SELECTED) {
658                         VecAddf(partial, partial, t->data[i].center);
659                 }
660                 else {
661                         /* 
662                            All the selected elements are at the head of the array 
663                            which means we can stop when it finds unselected data
664                         */
665                         break;
666                 }
667         }
668         VecMulf(partial, 1.0f / i);
669         VECCOPY(t->center, partial);
670
671         calculateCenter2D(t);
672 }
673
674 void calculateCenterBound(TransInfo *t)
675 {
676         float max[3];
677         float min[3];
678         int i;
679         for(i = 0; i < t->total; i++) {
680                 if (i) {
681                         if (t->data[i].flag & TD_SELECTED) {
682                                 MinMax3(min, max, t->data[i].center);
683                         }
684                         else {
685                                 /* 
686                                    All the selected elements are at the head of the array 
687                                    which means we can stop when it finds unselected data
688                                 */
689                                 break;
690                         }
691                 }
692                 else {
693                         VECCOPY(max, t->data[i].center);
694                         VECCOPY(min, t->data[i].center);
695                 }
696         }
697         VecAddf(t->center, min, max);
698         VecMulf(t->center, 0.5);
699
700         calculateCenter2D(t);
701 }
702
703 void calculateCenter(TransInfo *t) 
704 {
705         switch(t->around) {
706         case V3D_CENTRE:
707                 calculateCenterBound(t);
708                 break;
709         case V3D_CENTROID:
710                 calculateCenterMedian(t);
711                 break;
712         case V3D_CURSOR:
713                 calculateCenterCursor(t);
714                 break;
715         case V3D_LOCAL:
716                 /* Individual element center uses median center for helpline and such */
717                 calculateCenterMedian(t);
718                 break;
719         case V3D_ACTIVE:
720                 /* set median, and if if if... do object center */
721                 calculateCenterMedian(t);
722                 if((t->flag & (T_EDIT|T_POSE))==0) {
723                         Object *ob= OBACT;
724                         if(ob) {
725                                 VECCOPY(t->center, ob->obmat[3]);
726                                 projectIntView(t, t->center, t->center2d);
727                         }
728                 }
729                 
730         }
731
732         /* setting constraint center */
733         VECCOPY(t->con.center, t->center);
734         if(t->flag & (T_EDIT|T_POSE)) {
735                 Object *ob= G.obedit?G.obedit:t->poseobj;
736                 Mat4MulVecfl(ob->obmat, t->con.center);
737         }
738
739         /* voor panning from cameraview */
740         if(t->flag & T_OBJECT) {
741                 if( G.vd->camera==OBACT && G.vd->persp>1) {
742                         float axis[3];
743                         /* persinv is nasty, use viewinv instead, always right */
744                         VECCOPY(axis, t->viewinv[2]);
745                         Normalise(axis);
746
747                         /* 6.0 = 6 grid units */
748                         axis[0]= t->center[0]- 6.0f*axis[0];
749                         axis[1]= t->center[1]- 6.0f*axis[1];
750                         axis[2]= t->center[2]- 6.0f*axis[2];
751                         
752                         projectIntView(t, axis, t->center2d);
753                         
754                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
755                         if(t->mode==TFM_TRANSLATION) VECCOPY(t->center, axis);
756                 }
757         }       
758
759         if(t->spacetype==SPACE_VIEW3D)
760                 initgrabz(t->center[0], t->center[1], t->center[2]);
761 }
762
763 void calculatePropRatio(TransInfo *t)
764 {
765         TransData *td = t->data;
766         int i;
767         float dist;
768         short connected = t->flag & T_PROP_CONNECTED;
769
770         if (t->flag & T_PROP_EDIT) {
771                 for(i = 0 ; i < t->total; i++, td++) {
772                         if (td->flag & TD_SELECTED) {
773                                 td->factor = 1.0f;
774                         }
775                         else if ((connected && 
776                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
777                                 ||
778                                         (connected == 0 &&
779                                                 td->rdist > t->propsize)) {
780                                 /* 
781                                    The elements are sorted according to their dist member in the array,
782                                    that means we can stop when it finds one element outside of the propsize.
783                                 */
784                                 td->flag |= TD_NOACTION;
785                                 td->factor = 0.0f;
786                                 restoreElement(td);
787                         }
788                         else {
789                                 /* Use rdist for falloff calculations, it is the real distance */
790                                 td->flag &= ~TD_NOACTION;
791                                 dist= (t->propsize-td->rdist)/t->propsize;
792                                 switch(G.scene->prop_mode) {
793                                 case PROP_SHARP:
794                                         td->factor= dist*dist;
795                                         break;
796                                 case PROP_SMOOTH:
797                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
798                                         break;
799                                 case PROP_ROOT:
800                                         td->factor = (float)sqrt(dist);
801                                         break;
802                                 case PROP_LIN:
803                                         td->factor = dist;
804                                         break;
805                                 case PROP_CONST:
806                                         td->factor = 1.0f;
807                                         break;
808                                 case PROP_SPHERE:
809                                         td->factor = (float)sqrt(2*dist - dist * dist);
810                                         break;
811                                 default:
812                                         td->factor = 1;
813                                 }
814                         }
815                 }
816                 switch(G.scene->prop_mode) {
817                 case PROP_SHARP:
818                         strcpy(t->proptext, "(Sharp)");
819                         break;
820                 case PROP_SMOOTH:
821                         strcpy(t->proptext, "(Smooth)");
822                         break;
823                 case PROP_ROOT:
824                         strcpy(t->proptext, "(Root)");
825                         break;
826                 case PROP_LIN:
827                         strcpy(t->proptext, "(Linear)");
828                         break;
829                 case PROP_CONST:
830                         strcpy(t->proptext, "(Constant)");
831                         break;
832                 case PROP_SPHERE:
833                         strcpy(t->proptext, "(Sphere)");
834                         break;
835                 default:
836                         strcpy(t->proptext, "");
837                 }
838         }
839         else {
840                 for(i = 0 ; i < t->total; i++, td++) {
841                         td->factor = 1.0;
842                 }
843                 strcpy(t->proptext, "");
844         }
845 }
846
847 TransInfo * BIF_GetTransInfo() {
848         return &Trans;
849 }
850