add missing ndofinput init
[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         initNDofInput(&(t->ndof));
468 }
469
470 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
471 void postTrans (TransInfo *t) 
472 {
473         TransData *td;
474
475         G.moving = 0; // Set moving flag off (display as usual)
476 #ifdef WITH_VERSE
477
478         for (td = t->data; td < t->data + t->total; td++) {
479                 if(td->flag & TD_VERSE_VERT) {
480                         if(td->verse) send_versevert_pos((VerseVert*)td->verse);
481                 }
482                 else if(td->flag & TD_VERSE_OBJECT) {
483                         if(td->verse) {
484                                 struct VNode *vnode;
485                                 vnode = (VNode*)((Object*)td->verse)->vnode;
486                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
487                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
488                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
489                                 b_verse_send_transformation((Object*)td->verse);
490                         }
491                 }
492         }
493 #endif
494
495         stopConstraint(t);
496         
497         /* postTrans can be called when nothing is selected, so data is NULL already */
498         if (t->data) {
499                 int a;
500
501                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
502                 for(a=0, td= t->data; a<t->total; a++, td++) {
503                         if(td->tdi) MEM_freeN(td->tdi);
504                 }
505                 MEM_freeN(t->data);
506         }
507
508         if (t->ext) MEM_freeN(t->ext);
509         if (t->data2d) {
510                 MEM_freeN(t->data2d);
511                 t->data2d= NULL;
512         }
513
514         if(t->spacetype==SPACE_IMAGE) {
515                 if (G.sima->flag & SI_LIVE_UNWRAP)
516                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
517         }
518 }
519
520 void applyTransObjects(TransInfo *t)
521 {
522         TransData *td;
523         
524         for (td = t->data; td < t->data + t->total; td++) {
525                 VECCOPY(td->iloc, td->loc);
526                 if (td->ext->rot) {
527                         VECCOPY(td->ext->irot, td->ext->rot);
528                 }
529                 if (td->ext->size) {
530                         VECCOPY(td->ext->isize, td->ext->size);
531                 }
532         }       
533         recalcData(t);
534
535
536 /* helper for below */
537 static void restore_ipokey(float *poin, float *old)
538 {
539         if(poin) {
540                 poin[0]= old[0];
541                 poin[-3]= old[3];
542                 poin[3]= old[6];
543         }
544 }
545
546 static void restoreElement(TransData *td) {
547         /* TransData for crease has no loc */
548         if (td->loc) {
549                 VECCOPY(td->loc, td->iloc);
550         }
551         if (td->val) {
552                 *td->val = td->ival;
553         }
554         if (td->ext) {
555                 if (td->ext->rot) {
556                         VECCOPY(td->ext->rot, td->ext->irot);
557                 }
558                 if (td->ext->size) {
559                         VECCOPY(td->ext->size, td->ext->isize);
560                 }
561                 if(td->flag & TD_USEQUAT) {
562                         if (td->ext->quat) {
563                                 QUATCOPY(td->ext->quat, td->ext->iquat);
564                         }
565                 }
566         }
567         if(td->tdi) {
568                 TransDataIpokey *tdi= td->tdi;
569                 
570                 restore_ipokey(tdi->locx, tdi->oldloc);
571                 restore_ipokey(tdi->locy, tdi->oldloc+1);
572                 restore_ipokey(tdi->locz, tdi->oldloc+2);
573
574                 restore_ipokey(tdi->rotx, tdi->oldrot);
575                 restore_ipokey(tdi->roty, tdi->oldrot+1);
576                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
577                 
578                 restore_ipokey(tdi->sizex, tdi->oldsize);
579                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
580                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
581         }
582 }
583
584 void restoreTransObjects(TransInfo *t)
585 {
586         TransData *td;
587         
588         for (td = t->data; td < t->data + t->total; td++) {
589                 restoreElement(td);
590 #ifdef WITH_VERSE
591                 /* position of vertexes and object transformation matrix is sent
592                  * extra, becuase blender uses synchronous sending of vertexes
593                  * position as well object trans. matrix and it isn't possible to
594                  * send it in recalcData sometimes */
595                 if(td->flag & TD_VERSE_VERT) {
596                         if(td->verse) {
597                                 ((VerseVert*)td->verse)->flag |= VERT_POS_OBSOLETE;
598                         }
599                 }
600                 else if(td->flag & TD_VERSE_OBJECT)
601                         if(td->verse) {
602                                 struct VNode *vnode;
603                                 vnode = (VNode*)((Object*)td->verse)->vnode;
604                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
605                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
606                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
607                         }
608 #endif
609         }       
610         recalcData(t);
611 }
612
613 void calculateCenter2D(TransInfo *t)
614 {
615         if (t->flag & (T_EDIT|T_POSE)) {
616                 Object *ob= G.obedit?G.obedit:t->poseobj;
617                 float vec[3];
618                 
619                 VECCOPY(vec, t->center);
620                 Mat4MulVecfl(ob->obmat, vec);
621                 projectIntView(t, vec, t->center2d);
622         }
623         else {
624                 projectIntView(t, t->center, t->center2d);
625         }
626 }
627
628 void calculateCenterCursor(TransInfo *t)
629 {
630         float *cursor;
631
632         cursor = give_cursor();
633         VECCOPY(t->center, cursor);
634
635         /* If edit or pose mode, move cursor in local space */
636         if (t->flag & (T_EDIT|T_POSE)) {
637                 Object *ob = G.obedit?G.obedit:t->poseobj;
638                 float mat[3][3], imat[3][3];
639                 
640                 VecSubf(t->center, t->center, ob->obmat[3]);
641                 Mat3CpyMat4(mat, ob->obmat);
642                 Mat3Inv(imat, mat);
643                 Mat3MulVecfl(imat, t->center);
644         }
645         
646         calculateCenter2D(t);
647 }
648
649 void calculateCenterMedian(TransInfo *t)
650 {
651         float partial[3] = {0.0f, 0.0f, 0.0f};
652         int i;
653         
654         for(i = 0; i < t->total; i++) {
655                 if (t->data[i].flag & TD_SELECTED) {
656                         VecAddf(partial, partial, t->data[i].center);
657                 }
658                 else {
659                         /* 
660                            All the selected elements are at the head of the array 
661                            which means we can stop when it finds unselected data
662                         */
663                         break;
664                 }
665         }
666         if(i)
667                 VecMulf(partial, 1.0f / i);
668         VECCOPY(t->center, partial);
669
670         calculateCenter2D(t);
671 }
672
673 void calculateCenterBound(TransInfo *t)
674 {
675         float max[3];
676         float min[3];
677         int i;
678         for(i = 0; i < t->total; i++) {
679                 if (i) {
680                         if (t->data[i].flag & TD_SELECTED) {
681                                 MinMax3(min, max, t->data[i].center);
682                         }
683                         else {
684                                 /* 
685                                    All the selected elements are at the head of the array 
686                                    which means we can stop when it finds unselected data
687                                 */
688                                 break;
689                         }
690                 }
691                 else {
692                         VECCOPY(max, t->data[i].center);
693                         VECCOPY(min, t->data[i].center);
694                 }
695         }
696         VecAddf(t->center, min, max);
697         VecMulf(t->center, 0.5);
698
699         calculateCenter2D(t);
700 }
701
702 void calculateCenter(TransInfo *t) 
703 {
704         switch(t->around) {
705         case V3D_CENTER:
706                 calculateCenterBound(t);
707                 break;
708         case V3D_CENTROID:
709                 calculateCenterMedian(t);
710                 break;
711         case V3D_CURSOR:
712                 calculateCenterCursor(t);
713                 break;
714         case V3D_LOCAL:
715                 /* Individual element center uses median center for helpline and such */
716                 calculateCenterMedian(t);
717                 break;
718         case V3D_ACTIVE:
719                 /* set median, and if if if... do object center */
720                 
721                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
722                 if (G.obedit && G.obedit->type == OB_MESH && G.editMesh->selected.last) {
723                         EM_editselection_center(t->center, G.editMesh->selected.last);
724                         calculateCenter2D(t);
725                         break;
726                 } /* END EDIT MODE ACTIVE ELEMENT */
727                 
728                 calculateCenterMedian(t);
729                 if((t->flag & (T_EDIT|T_POSE))==0) {
730                         Object *ob= OBACT;
731                         if(ob) {
732                                 VECCOPY(t->center, ob->obmat[3]);
733                                 projectIntView(t, t->center, t->center2d);
734                         }
735                 }
736         }
737
738         /* setting constraint center */
739         VECCOPY(t->con.center, t->center);
740         if(t->flag & (T_EDIT|T_POSE)) {
741                 Object *ob= G.obedit?G.obedit:t->poseobj;
742                 Mat4MulVecfl(ob->obmat, t->con.center);
743         }
744
745         /* voor panning from cameraview */
746         if(t->flag & T_OBJECT) {
747                 if( G.vd->camera==OBACT && G.vd->persp>1) {
748                         float axis[3];
749                         /* persinv is nasty, use viewinv instead, always right */
750                         VECCOPY(axis, t->viewinv[2]);
751                         Normalize(axis);
752
753                         /* 6.0 = 6 grid units */
754                         axis[0]= t->center[0]- 6.0f*axis[0];
755                         axis[1]= t->center[1]- 6.0f*axis[1];
756                         axis[2]= t->center[2]- 6.0f*axis[2];
757                         
758                         projectIntView(t, axis, t->center2d);
759                         
760                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
761                         if(t->mode==TFM_TRANSLATION) {
762                                 VECCOPY(t->center, axis);
763                                 VECCOPY(t->con.center, t->center);
764                         }
765                 }
766         }       
767
768         if(t->spacetype==SPACE_VIEW3D)
769                 initgrabz(t->center[0], t->center[1], t->center[2]);
770 }
771
772 void calculatePropRatio(TransInfo *t)
773 {
774         TransData *td = t->data;
775         int i;
776         float dist;
777         short connected = t->flag & T_PROP_CONNECTED;
778
779         if (t->flag & T_PROP_EDIT) {
780                 for(i = 0 ; i < t->total; i++, td++) {
781                         if (td->flag & TD_SELECTED) {
782                                 td->factor = 1.0f;
783                         }
784                         else if ((connected && 
785                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
786                                 ||
787                                         (connected == 0 &&
788                                                 td->rdist > t->propsize)) {
789                                 /* 
790                                    The elements are sorted according to their dist member in the array,
791                                    that means we can stop when it finds one element outside of the propsize.
792                                 */
793                                 td->flag |= TD_NOACTION;
794                                 td->factor = 0.0f;
795                                 restoreElement(td);
796                         }
797                         else {
798                                 /* Use rdist for falloff calculations, it is the real distance */
799                                 td->flag &= ~TD_NOACTION;
800                                 dist= (t->propsize-td->rdist)/t->propsize;
801                                 
802                                 /*
803                                  * Clamp to positive numbers.
804                                  * Certain corner cases with connectivity and individual centers
805                                  * can give values of rdist larger than propsize.
806                                  */
807                                 if (dist < 0.0f)
808                                         dist = 0.0f;
809                                 
810                                 switch(G.scene->prop_mode) {
811                                 case PROP_SHARP:
812                                         td->factor= dist*dist;
813                                         break;
814                                 case PROP_SMOOTH:
815                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
816                                         break;
817                                 case PROP_ROOT:
818                                         td->factor = (float)sqrt(dist);
819                                         break;
820                                 case PROP_LIN:
821                                         td->factor = dist;
822                                         break;
823                                 case PROP_CONST:
824                                         td->factor = 1.0f;
825                                         break;
826                                 case PROP_SPHERE:
827                                         td->factor = (float)sqrt(2*dist - dist * dist);
828                                         break;
829                                 case PROP_RANDOM:
830                                         BLI_srand( BLI_rand() ); /* random seed */
831                                         td->factor = BLI_frand()*dist;
832                                         break;
833                                 default:
834                                         td->factor = 1;
835                                 }
836                         }
837                 }
838                 switch(G.scene->prop_mode) {
839                 case PROP_SHARP:
840                         strcpy(t->proptext, "(Sharp)");
841                         break;
842                 case PROP_SMOOTH:
843                         strcpy(t->proptext, "(Smooth)");
844                         break;
845                 case PROP_ROOT:
846                         strcpy(t->proptext, "(Root)");
847                         break;
848                 case PROP_LIN:
849                         strcpy(t->proptext, "(Linear)");
850                         break;
851                 case PROP_CONST:
852                         strcpy(t->proptext, "(Constant)");
853                         break;
854                 case PROP_SPHERE:
855                         strcpy(t->proptext, "(Sphere)");
856                         break;
857                 case PROP_RANDOM:
858                         strcpy(t->proptext, "(Random)");
859                         break;
860                 default:
861                         strcpy(t->proptext, "");
862                 }
863         }
864         else {
865                 for(i = 0 ; i < t->total; i++, td++) {
866                         td->factor = 1.0;
867                 }
868                 strcpy(t->proptext, "");
869         }
870 }
871
872 TransInfo * BIF_GetTransInfo() {
873         return &Trans;
874 }
875