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