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