Merged 15170:15635 from trunk (no conflicts or even merges)
[blender.git] / source / blender / src / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_action_types.h"
36 #include "DNA_armature_types.h"
37 #include "DNA_constraint_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_lattice_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_modifier_types.h"
42 #include "DNA_nla_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_object_force.h"
45 #include "DNA_particle_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_userdef_types.h"
50 #include "DNA_view3d_types.h"
51
52 #include "BIF_screen.h"
53 #include "BIF_resources.h"
54 #include "BIF_mywindow.h"
55 #include "BIF_gl.h"
56 #include "BIF_editaction.h"
57 #include "BIF_editarmature.h"
58 #include "BIF_editmesh.h"
59 #include "BIF_editnla.h"
60 #include "BIF_editsima.h"
61 #include "BIF_editparticle.h"
62 #include "BIF_meshtools.h"
63 #include "BIF_retopo.h"
64
65 #include "BSE_editipo.h"
66 #include "BSE_editipo_types.h"
67
68 #ifdef WITH_VERSE
69 #include "BIF_verse.h"
70 #endif
71
72 #include "BKE_action.h"
73 #include "BKE_anim.h"
74 #include "BKE_armature.h"
75 #include "BKE_cloth.h"
76 #include "BKE_curve.h"
77 #include "BKE_depsgraph.h"
78 #include "BKE_displist.h"
79 #include "BKE_depsgraph.h"
80 #include "BKE_global.h"
81 #include "BKE_group.h"
82 #include "BKE_ipo.h"
83 #include "BKE_lattice.h"
84 #include "BKE_key.h"
85 #include "BKE_mesh.h"
86 #include "BKE_modifier.h"
87 #include "BKE_object.h"
88 #include "BKE_utildefines.h"
89
90 #ifdef WITH_VERSE
91 #include "BKE_verse.h"
92 #endif
93
94 #include "BSE_view.h"
95 #include "BSE_editaction_types.h"
96 #include "BDR_unwrapper.h"
97
98 #include "BLI_arithb.h"
99 #include "BLI_blenlib.h"
100 #include "BLI_editVert.h"
101 #include "BLI_rand.h"
102
103 #include "blendef.h"
104
105 #include "mydevice.h"
106
107 #include "transform.h"
108
109 extern ListBase editNurb;
110 extern ListBase editelems;
111
112 extern TransInfo Trans; /* From transform.c */
113
114 /* ************************** Functions *************************** */
115
116 void getViewVector(float coord[3], float vec[3])
117 {
118         TransInfo *t = BIF_GetTransInfo();
119
120         if (t->persp != V3D_ORTHO)
121         {
122                 float p1[4], p2[4];
123
124                 VECCOPY(p1, coord);
125                 p1[3] = 1.0f;
126                 VECCOPY(p2, p1);
127                 p2[3] = 1.0f;
128                 Mat4MulVec4fl(t->viewmat, p2);
129
130                 p2[0] = 2.0f * p2[0];
131                 p2[1] = 2.0f * p2[1];
132                 p2[2] = 2.0f * p2[2];
133
134                 Mat4MulVec4fl(t->viewinv, p2);
135
136                 VecSubf(vec, p1, p2);
137         }
138         else {
139                 VECCOPY(vec, t->viewinv[2]);
140         }
141         Normalize(vec);
142 }
143
144 /* ************************** GENERICS **************************** */
145
146 static void clipMirrorModifier(TransInfo *t, Object *ob)
147 {
148         ModifierData *md= ob->modifiers.first;
149         float tolerance[3] = {0.0f, 0.0f, 0.0f};
150         int axis = 0;
151
152         for (; md; md=md->next) {
153                 if (md->type==eModifierType_Mirror) {
154                         MirrorModifierData *mmd = (MirrorModifierData*) md;     
155                 
156                         if(mmd->flag & MOD_MIR_CLIPPING) {
157                                 axis = 0;
158                                 if(mmd->flag & MOD_MIR_AXIS_X) {
159                                         axis |= 1;
160                                         tolerance[0] = mmd->tolerance;
161                                 }
162                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
163                                         axis |= 2;
164                                         tolerance[1] = mmd->tolerance;
165                                 }
166                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
167                                         axis |= 4;
168                                         tolerance[2] = mmd->tolerance;
169                                 }
170                                 if (axis) {
171                                         float mtx[4][4], imtx[4][4];
172                                         int i;
173                                         TransData *td = t->data;
174                 
175                                         if (mmd->mirror_ob) {
176                                                 float obinv[4][4];
177
178                                                 Mat4Invert(obinv, mmd->mirror_ob->obmat);
179                                                 Mat4MulMat4(mtx, ob->obmat, obinv);
180                                                 Mat4Invert(imtx, mtx);
181                                         }
182
183                                         for(i = 0 ; i < t->total; i++, td++) {
184                                                 int clip;
185                                                 float loc[3], iloc[3];
186
187                                                 if (td->flag & TD_NOACTION)
188                                                         break;
189                                                 if (td->loc==NULL)
190                                                         break;
191                                                         
192                                                 if (td->flag & TD_SKIP)
193                                                         continue;
194                         
195                                                 VecCopyf(loc,  td->loc);
196                                                 VecCopyf(iloc, td->iloc);
197
198                                                 if (mmd->mirror_ob) {
199                                                         VecMat4MulVecfl(loc, mtx, loc);
200                                                         VecMat4MulVecfl(iloc, mtx, iloc);
201                                                 }
202
203                                                 clip = 0;
204                                                 if(axis & 1) {
205                                                         if(fabs(iloc[0])<=tolerance[0] || 
206                                                            loc[0]*iloc[0]<0.0f) {
207                                                                 loc[0]= 0.0f;
208                                                                 clip = 1;
209                                                         }
210                                                 }
211                         
212                                                 if(axis & 2) {
213                                                         if(fabs(iloc[1])<=tolerance[1] || 
214                                                            loc[1]*iloc[1]<0.0f) {
215                                                                 loc[1]= 0.0f;
216                                                                 clip = 1;
217                                                         }
218                                                 }
219                                                 if(axis & 4) {
220                                                         if(fabs(iloc[2])<=tolerance[2] || 
221                                                            loc[2]*iloc[2]<0.0f) {
222                                                                 loc[2]= 0.0f;
223                                                                 clip = 1;
224                                                         }
225                                                 }
226                                                 if (clip) {
227                                                         if (mmd->mirror_ob) {
228                                                                 VecMat4MulVecfl(loc, imtx, loc);
229                                                         }
230                                                         VecCopyf(td->loc, loc);
231                                                 }
232                                         }
233                                 }
234
235                         }
236                 }
237         }
238 }
239
240 /* assumes G.obedit set to mesh object */
241 static void editmesh_apply_to_mirror(TransInfo *t)
242 {
243         TransData *td = t->data;
244         EditVert *eve;
245         int i;
246         
247         for(i = 0 ; i < t->total; i++, td++) {
248                 if (td->flag & TD_NOACTION)
249                         break;
250                 if (td->loc==NULL)
251                         break;
252                 if (td->flag & TD_SKIP)
253                         continue;
254                 
255                 eve = td->tdmir;
256                 if(eve) {
257                         eve->co[0]= -td->loc[0];
258                         eve->co[1]= td->loc[1];
259                         eve->co[2]= td->loc[2];
260                 }               
261         }               
262 }
263
264 /* called for updating while transform acts, once per redraw */
265 void recalcData(TransInfo *t)
266 {
267         Base *base;
268 #ifdef WITH_VERSE
269         struct TransData *td;
270 #endif
271         
272         if (t->spacetype == SPACE_ACTION) {
273                 Object *ob= OBACT;
274                 void *data;
275                 short context;
276                 
277                 /* determine what type of data we are operating on */
278                 data = get_action_context(&context);
279                 if (data == NULL) return;
280                 
281                 if (G.saction->lock) {
282                         if (context == ACTCONT_ACTION) {
283                                 if(ob) {
284                                         ob->ctime= -1234567.0f;
285                                         if(ob->pose || ob_get_key(ob))
286                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC);
287                                         else
288                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
289                                 }
290                         }
291                         else if (context == ACTCONT_SHAPEKEY) {
292                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
293                         }
294                 }
295         }       
296         else if (t->spacetype == SPACE_NLA) {
297                 if (G.snla->lock) {
298                         for (base=G.scene->base.first; base; base=base->next) {
299                                 if (base->flag & BA_HAS_RECALC_OB)
300                                         base->object->recalc |= OB_RECALC_OB;
301                                 if (base->flag & BA_HAS_RECALC_DATA)
302                                         base->object->recalc |= OB_RECALC_DATA;
303                                 
304                                 if (base->object->recalc) 
305                                         base->object->ctime= -1234567.0f;       // eveil! 
306                                 
307                                 /* recalculate scale of selected nla-strips */
308                                 if (base->object->nlastrips.first) {
309                                         Object *bob= base->object;
310                                         bActionStrip *strip;
311                                         
312                                         for (strip= bob->nlastrips.first; strip; strip= strip->next) {
313                                                 if (strip->flag & ACTSTRIP_SELECT) {
314                                                         float actlen= strip->actend - strip->actstart;
315                                                         float len= strip->end - strip->start;
316                                                         
317                                                         strip->scale= len / (actlen * strip->repeat);
318                                                 }
319                                         }
320                                 }
321                         }
322                         
323                         DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
324                 }
325                 else {
326                         for (base=G.scene->base.first; base; base=base->next) {
327                                 /* recalculate scale of selected nla-strips */
328                                 if (base->object && base->object->nlastrips.first) {
329                                         Object *bob= base->object;
330                                         bActionStrip *strip;
331                                         
332                                         for (strip= bob->nlastrips.first; strip; strip= strip->next) {
333                                                 if (strip->flag & ACTSTRIP_SELECT) {
334                                                         float actlen= strip->actend - strip->actstart;
335                                                         float len= strip->end - strip->start;
336                                                         
337                                                         /* prevent 'negative' scaling */
338                                                         if (len < 0) {
339                                                                 SWAP(float, strip->start, strip->end);
340                                                                 len= fabs(len);
341                                                         }
342                                                         
343                                                         /* calculate new scale */
344                                                         strip->scale= len / (actlen * strip->repeat);
345                                                 }
346                                         }
347                                 }
348                         }
349                 }
350         }
351         else if (t->spacetype == SPACE_IPO) {
352                 EditIpo *ei;
353                 int dosort = 0;
354                 int a;
355                 
356                 /* do the flush first */
357                 flushTransIpoData(t);
358                 
359                 /* now test if there is a need to re-sort */
360                 ei= G.sipo->editipo;
361                 for (a=0; a<G.sipo->totipo; a++, ei++) {
362                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
363                                 
364                                 /* watch it: if the time is wrong: do not correct handles */
365                                 if (test_time_ipocurve(ei->icu)) {
366                                         dosort++;
367                                 } else {
368                                         calchandles_ipocurve(ei->icu);
369                                 }
370                         }
371                 }
372                 
373                 /* do resort and other updates? */
374                 if (dosort) remake_ipo_transdata(t);
375                 if (G.sipo->showkey) update_ipokey_val();
376                 
377                 calc_ipo(G.sipo->ipo, (float)CFRA);
378                 
379                 /* update realtime - not working? */
380                 if (G.sipo->lock) {
381                         if (G.sipo->blocktype==ID_MA || G.sipo->blocktype==ID_TE) {
382                                 do_ipo(G.sipo->ipo);
383                         }
384                         else if(G.sipo->blocktype==ID_CA) {
385                                 do_ipo(G.sipo->ipo);
386                         }
387                         else if(G.sipo->blocktype==ID_KE) {
388                                 Object *ob= OBACT;
389                                 if(ob) {
390                                         ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
391                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
392                                 }
393                         }
394                         else if(G.sipo->blocktype==ID_PO) {
395                                 Object *ob= OBACT;
396                                 if(ob && ob->pose) {
397                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
398                                 }
399                         }
400                         else if(G.sipo->blocktype==ID_OB) {
401                                 Object *ob= OBACT;
402                                 Base *base= FIRSTBASE;
403                                 
404                                 /* only if this if active object has this ipo in an action (assumes that current ipo is in action) */
405                                 if ((ob) && (ob->ipoflag & OB_ACTION_OB) && (G.sipo->pin==0)) {
406                                         ob->ctime= -1234567.0f;
407                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
408                                 }
409                                 
410                                 while(base) {
411                                         if(base->object->ipo==G.sipo->ipo) {
412                                                 do_ob_ipo(base->object);
413                                                 base->object->recalc |= OB_RECALC_OB;
414                                         }
415                                         base= base->next;
416                                 }
417                                 DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
418                         }
419                 }
420         }
421         else if (G.obedit) {
422                 if (G.obedit->type == OB_MESH) {
423                         if(t->spacetype==SPACE_IMAGE) {
424                                 flushTransUVs(t);
425                                 if (G.sima->flag & SI_LIVE_UNWRAP)
426                                         unwrap_lscm_live_re_solve();
427                         } else {
428                                 /* mirror modifier clipping? */
429                                 if(t->state != TRANS_CANCEL) {
430                                         if ((G.qual & LR_CTRLKEY)==0) {
431                                                 /* Only retopo if not snapping, Note, this is the only case of G.qual being used, but we have no T_SHIFT_MOD - Campbell */
432                                                 retopo_do_all();
433                                         }
434                                         clipMirrorModifier(t, G.obedit);
435                                 }
436                                 if((t->context & CTX_NO_MIRROR) == 0 && (G.scene->toolsettings->editbutflag & B_MESH_X_MIRROR))
437                                         editmesh_apply_to_mirror(t);
438                                 
439                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
440                                 
441                                 recalc_editnormals();
442                         }
443                 }
444                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
445                         Nurb *nu= editNurb.first;
446                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
447                         
448                         if (t->state == TRANS_CANCEL) {
449                                 while(nu) {
450                                         calchandlesNurb(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
451                                         nu= nu->next;
452                                 }
453                         } else {
454                                 /* Normal updating */
455                                 while(nu) {
456                                         test2DNurb(nu);
457                                         calchandlesNurb(nu);
458                                         nu= nu->next;
459                                 }
460                                 retopo_do_all();
461                         }
462                 }
463                 else if(G.obedit->type==OB_ARMATURE){   /* no recalc flag, does pose */
464                         bArmature *arm= G.obedit->data;
465                         EditBone *ebo;
466                         
467                         /* Ensure all bones are correctly adjusted */
468                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
469                                 
470                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
471                                         /* If this bone has a parent tip that has been moved */
472                                         if (ebo->parent->flag & BONE_TIPSEL){
473                                                 VECCOPY (ebo->head, ebo->parent->tail);
474                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
475                                         }
476                                         /* If this bone has a parent tip that has NOT been moved */
477                                         else{
478                                                 VECCOPY (ebo->parent->tail, ebo->head);
479                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
480                                         }
481                                 }
482                                 
483                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
484                                 ebo->length= VecLenf(ebo->head, ebo->tail);
485                                 if(ebo->oldlength==0.0f) {
486                                         ebo->rad_head= 0.25f*ebo->length;
487                                         ebo->rad_tail= 0.10f*ebo->length;
488                                         ebo->dist= 0.25f*ebo->length;
489                                         if(ebo->parent) {
490                                                 if(ebo->rad_head > ebo->parent->rad_tail)
491                                                         ebo->rad_head= ebo->parent->rad_tail;
492                                         }
493                                 }
494                                 else if(t->mode!=TFM_BONE_ENVELOPE) {
495                                         /* if bones change length, lets do that for the deform distance as well */
496                                         ebo->dist*= ebo->length/ebo->oldlength;
497                                         ebo->rad_head*= ebo->length/ebo->oldlength;
498                                         ebo->rad_tail*= ebo->length/ebo->oldlength;
499                                         ebo->oldlength= ebo->length;
500                                 }
501                         }
502                         if(arm->flag & ARM_MIRROR_EDIT) 
503                                 transform_armature_mirror_update();
504                         
505                 }
506                 else if(G.obedit->type==OB_LATTICE) {
507                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
508                         
509                         if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
510                 }
511                 else {
512                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
513                 }
514         }
515         else if( (t->flag & T_POSE) && t->poseobj) {
516                 Object *ob= t->poseobj;
517                 bArmature *arm= ob->data;
518                 
519                 /* old optimize trick... this enforces to bypass the depgraph */
520                 if (!(arm->flag & ARM_DELAYDEFORM)) {
521                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
522                 }
523                 else
524                         where_is_pose(ob);
525         }
526         else if(G.f & G_PARTICLEEDIT) {
527                 flushTransParticles(t);
528         }
529         else {
530                 for(base= FIRSTBASE; base; base= base->next) {
531                         Object *ob= base->object;
532                         
533                         /* this flag is from depgraph, was stored in initialize phase, handled in drawview.c */
534                         if(base->flag & BA_HAS_RECALC_OB)
535                                 ob->recalc |= OB_RECALC_OB;
536                         if(base->flag & BA_HAS_RECALC_DATA)
537                                 ob->recalc |= OB_RECALC_DATA;
538
539                         /* thanks to ob->ctime usage, ipos are not called in where_is_object,
540                            unless we edit ipokeys */
541                         if(base->flag & BA_DO_IPO) {
542                                 if(ob->ipo) {
543                                         IpoCurve *icu;
544                                         
545                                         ob->ctime= -1234567.0;
546                                         
547                                         icu= ob->ipo->curve.first;
548                                         while(icu) {
549                                                 calchandles_ipocurve(icu);
550                                                 icu= icu->next;
551                                         }
552                                 }                               
553                         }
554                         
555                         /* proxy exception */
556                         if(ob->proxy)
557                                 ob->proxy->recalc |= ob->recalc;
558                         if(ob->proxy_group)
559                                 group_tag_recalc(ob->proxy_group->dup_group);
560                 } 
561         }
562
563 #ifdef WITH_VERSE
564         for (td = t->data; td < t->data + t->total; td++) {
565                 if(td->flag & TD_VERSE_VERT) {
566                         if(td->verse)
567                                 send_versevert_pos((VerseVert*)td->verse);
568                 }
569                 else if(td->flag & TD_VERSE_OBJECT)
570                         if(td->verse) b_verse_send_transformation((Object*)td->verse);
571         }
572 #endif
573         
574         /* update shaded drawmode while transform */
575         if(t->spacetype==SPACE_VIEW3D && G.vd->drawtype == OB_SHADED)
576                 reshadeall_displist();
577 }
578
579 void initTransModeFlags(TransInfo *t, int mode) 
580 {
581         t->mode = mode;
582         t->num.flag = 0;
583
584         /* REMOVING RESTRICTIONS FLAGS */
585         t->flag &= ~T_ALL_RESTRICTIONS;
586         
587         switch (mode) {
588         case TFM_RESIZE:
589                 t->flag |= T_NULL_ONE;
590                 t->num.flag |= NUM_NULL_ONE;
591                 t->num.flag |= NUM_AFFECT_ALL;
592                 if (!G.obedit) {
593                         t->flag |= T_NO_ZERO;
594                         t->num.flag |= NUM_NO_ZERO;
595                 }
596                 break;
597         case TFM_TOSPHERE:
598                 t->num.flag |= NUM_NULL_ONE;
599                 t->num.flag |= NUM_NO_NEGATIVE;
600                 t->flag |= T_NO_CONSTRAINT;
601                 break;
602         case TFM_SHEAR:
603         case TFM_CREASE:
604         case TFM_BONE_ENVELOPE:
605         case TFM_CURVE_SHRINKFATTEN:
606         case TFM_BONE_ROLL:
607                 t->flag |= T_NO_CONSTRAINT;
608                 break;
609         }
610 }
611
612 void drawLine(float *center, float *dir, char axis, short options)
613 {
614         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
615         float v1[3], v2[3], v3[3];
616         char col[3], col2[3];
617         
618         //if(G.obedit) mymultmatrix(G.obedit->obmat);   // sets opengl viewing
619
620         VecCopyf(v3, dir);
621         VecMulf(v3, G.vd->far);
622         
623         VecSubf(v2, center, v3);
624         VecAddf(v1, center, v3);
625
626         if (options & DRAWLIGHT) {
627                 col[0] = col[1] = col[2] = 220;
628         }
629         else {
630                 BIF_GetThemeColor3ubv(TH_GRID, col);
631         }
632         make_axis_color(col, col2, axis);
633         glColor3ubv((GLubyte *)col2);
634
635         setlinestyle(0);
636         glBegin(GL_LINE_STRIP); 
637                 glVertex3fv(v1); 
638                 glVertex3fv(v2); 
639         glEnd();
640         
641         myloadmatrix(G.vd->viewmat);
642 }
643
644 void initTrans (TransInfo *t)
645 {
646         
647         /* moving: is shown in drawobject() (transform color) */
648         if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
649         else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
650         else G.moving= G_TRANSFORM_OBJ;
651
652         t->data = NULL;
653         t->ext = NULL;
654
655         t->flag = 0;
656
657         /* setting PET flag */
658         if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
659                 t->flag |= T_PROP_EDIT;
660                 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED;       // yes i know, has to become define
661         }
662
663         getmouseco_areawin(t->imval);
664         t->con.imval[0] = t->imval[0];
665         t->con.imval[1] = t->imval[1];
666
667         t->transform            = NULL;
668         t->handleEvent          = NULL;
669
670         t->total                        =
671                 t->num.idx              =
672                 t->num.idx_max  =
673                 t->num.ctrl[0]  = 
674                 t->num.ctrl[1]  = 
675                 t->num.ctrl[2]  = 0;
676
677         t->val = 0.0f;
678
679         t->num.val[0]           = 
680                 t->num.val[1]   = 
681                 t->num.val[2]   = 0.0f;
682
683         t->vec[0]                       =
684                 t->vec[1]               =
685                 t->vec[2]               = 0.0f;
686         
687         t->center[0]            =
688                 t->center[1]    =
689                 t->center[2]    = 0.0f;
690         
691         Mat3One(t->mat);
692         
693         t->spacetype = curarea->spacetype;
694         if(t->spacetype==SPACE_VIEW3D) {
695                 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
696                 t->around = G.vd->around;
697         } else if(t->spacetype==SPACE_IMAGE) {
698                 t->around = G.v2d->around;
699         }
700         else
701                 t->around = V3D_CENTER;
702
703         setTransformViewMatrices(t);
704         initNDofInput(&(t->ndof));
705 }
706
707 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
708 void postTrans (TransInfo *t) 
709 {
710         TransData *td;
711
712         G.moving = 0; // Set moving flag off (display as usual)
713 #ifdef WITH_VERSE
714
715         for (td = t->data; td < t->data + t->total; td++) {
716                 if(td->flag & TD_VERSE_VERT) {
717                         if(td->verse) send_versevert_pos((VerseVert*)td->verse);
718                 }
719                 else if(td->flag & TD_VERSE_OBJECT) {
720                         if(td->verse) {
721                                 struct VNode *vnode;
722                                 vnode = (VNode*)((Object*)td->verse)->vnode;
723                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
724                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
725                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
726                                 b_verse_send_transformation((Object*)td->verse);
727                         }
728                 }
729         }
730 #endif
731
732         stopConstraint(t);
733         
734         /* postTrans can be called when nothing is selected, so data is NULL already */
735         if (t->data) {
736                 int a;
737
738                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
739                 for(a=0, td= t->data; a<t->total; a++, td++) {
740                         if(td->tdi) MEM_freeN(td->tdi);
741                         if (td->flag & TD_BEZTRIPLE) MEM_freeN(td->hdata); 
742                 }
743                 MEM_freeN(t->data);
744         }
745
746         if (t->ext) MEM_freeN(t->ext);
747         if (t->data2d) {
748                 MEM_freeN(t->data2d);
749                 t->data2d= NULL;
750         }
751
752         if(t->spacetype==SPACE_IMAGE) {
753                 if (G.sima->flag & SI_LIVE_UNWRAP)
754                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
755         }
756 }
757
758 void applyTransObjects(TransInfo *t)
759 {
760         TransData *td;
761         
762         for (td = t->data; td < t->data + t->total; td++) {
763                 VECCOPY(td->iloc, td->loc);
764                 if (td->ext->rot) {
765                         VECCOPY(td->ext->irot, td->ext->rot);
766                 }
767                 if (td->ext->size) {
768                         VECCOPY(td->ext->isize, td->ext->size);
769                 }
770         }       
771         recalcData(t);
772
773
774 /* helper for below */
775 static void restore_ipokey(float *poin, float *old)
776 {
777         if(poin) {
778                 poin[0]= old[0];
779                 poin[-3]= old[3];
780                 poin[3]= old[6];
781         }
782 }
783
784 static void restoreElement(TransData *td) {
785         /* TransData for crease has no loc */
786         if (td->loc) {
787                 VECCOPY(td->loc, td->iloc);
788         }
789         if (td->val) {
790                 *td->val = td->ival;
791         }
792         if (td->ext && (td->flag&TD_NO_EXT)==0) {
793                 if (td->ext->rot) {
794                         VECCOPY(td->ext->rot, td->ext->irot);
795                 }
796                 if (td->ext->size) {
797                         VECCOPY(td->ext->size, td->ext->isize);
798                 }
799                 if(td->flag & TD_USEQUAT) {
800                         if (td->ext->quat) {
801                                 QUATCOPY(td->ext->quat, td->ext->iquat);
802                         }
803                 }
804         }
805         
806         if (td->flag & TD_BEZTRIPLE) {
807                 *(td->hdata->h1) = td->hdata->ih1;
808                 *(td->hdata->h2) = td->hdata->ih2;
809         }
810         
811         if(td->tdi) {
812                 TransDataIpokey *tdi= td->tdi;
813                 
814                 restore_ipokey(tdi->locx, tdi->oldloc);
815                 restore_ipokey(tdi->locy, tdi->oldloc+1);
816                 restore_ipokey(tdi->locz, tdi->oldloc+2);
817
818                 restore_ipokey(tdi->rotx, tdi->oldrot);
819                 restore_ipokey(tdi->roty, tdi->oldrot+1);
820                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
821                 
822                 restore_ipokey(tdi->sizex, tdi->oldsize);
823                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
824                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
825         }
826 }
827
828 void restoreTransObjects(TransInfo *t)
829 {
830         TransData *td;
831         
832         for (td = t->data; td < t->data + t->total; td++) {
833                 restoreElement(td);
834 #ifdef WITH_VERSE
835                 /* position of vertexes and object transformation matrix is sent
836                  * extra, becuase blender uses synchronous sending of vertexes
837                  * position as well object trans. matrix and it isn't possible to
838                  * send it in recalcData sometimes */
839                 if(td->flag & TD_VERSE_VERT) {
840                         if(td->verse) {
841                                 ((VerseVert*)td->verse)->flag |= VERT_POS_OBSOLETE;
842                         }
843                 }
844                 else if(td->flag & TD_VERSE_OBJECT)
845                         if(td->verse) {
846                                 struct VNode *vnode;
847                                 vnode = (VNode*)((Object*)td->verse)->vnode;
848                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
849                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
850                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
851                         }
852 #endif
853         }       
854         recalcData(t);
855 }
856
857 void calculateCenter2D(TransInfo *t)
858 {
859         if (t->flag & (T_EDIT|T_POSE)) {
860                 Object *ob= G.obedit?G.obedit:t->poseobj;
861                 float vec[3];
862                 
863                 VECCOPY(vec, t->center);
864                 Mat4MulVecfl(ob->obmat, vec);
865                 projectIntView(t, vec, t->center2d);
866         }
867         else {
868                 projectIntView(t, t->center, t->center2d);
869         }
870 }
871
872 void calculateCenterCursor(TransInfo *t)
873 {
874         float *cursor;
875
876         cursor = give_cursor();
877         VECCOPY(t->center, cursor);
878
879         /* If edit or pose mode, move cursor in local space */
880         if (t->flag & (T_EDIT|T_POSE)) {
881                 Object *ob = G.obedit?G.obedit:t->poseobj;
882                 float mat[3][3], imat[3][3];
883                 
884                 VecSubf(t->center, t->center, ob->obmat[3]);
885                 Mat3CpyMat4(mat, ob->obmat);
886                 Mat3Inv(imat, mat);
887                 Mat3MulVecfl(imat, t->center);
888         }
889         
890         calculateCenter2D(t);
891 }
892
893 void calculateCenterCursor2D(TransInfo *t)
894 {
895         float aspx=1.0, aspy=1.0;
896         
897         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
898                 transform_aspect_ratio_tface_uv(&aspx, &aspy);
899         if (G.v2d) {
900                 t->center[0] = G.v2d->cursor[0] * aspx; 
901                 t->center[1] = G.v2d->cursor[1] * aspy; 
902         }
903         calculateCenter2D(t);
904 }
905
906 void calculateCenterMedian(TransInfo *t)
907 {
908         float partial[3] = {0.0f, 0.0f, 0.0f};
909         int i;
910         
911         for(i = 0; i < t->total; i++) {
912                 if (t->data[i].flag & TD_SELECTED) {
913                         if (!(t->data[i].flag & TD_NOCENTER))
914                                 VecAddf(partial, partial, t->data[i].center);
915                 }
916                 else {
917                         /* 
918                            All the selected elements are at the head of the array 
919                            which means we can stop when it finds unselected data
920                         */
921                         break;
922                 }
923         }
924         if(i)
925                 VecMulf(partial, 1.0f / i);
926         VECCOPY(t->center, partial);
927
928         calculateCenter2D(t);
929 }
930
931 void calculateCenterBound(TransInfo *t)
932 {
933         float max[3];
934         float min[3];
935         int i;
936         for(i = 0; i < t->total; i++) {
937                 if (i) {
938                         if (t->data[i].flag & TD_SELECTED) {
939                                 if (!(t->data[i].flag & TD_NOCENTER))
940                                         MinMax3(min, max, t->data[i].center);
941                         }
942                         else {
943                                 /* 
944                                    All the selected elements are at the head of the array 
945                                    which means we can stop when it finds unselected data
946                                 */
947                                 break;
948                         }
949                 }
950                 else {
951                         VECCOPY(max, t->data[i].center);
952                         VECCOPY(min, t->data[i].center);
953                 }
954         }
955         VecAddf(t->center, min, max);
956         VecMulf(t->center, 0.5);
957
958         calculateCenter2D(t);
959 }
960
961 void calculateCenter(TransInfo *t) 
962 {
963         switch(t->around) {
964         case V3D_CENTER:
965                 calculateCenterBound(t);
966                 break;
967         case V3D_CENTROID:
968                 calculateCenterMedian(t);
969                 break;
970         case V3D_CURSOR:
971                 if(t->spacetype==SPACE_IMAGE)
972                         calculateCenterCursor2D(t);
973                 else
974                         calculateCenterCursor(t);
975                 break;
976         case V3D_LOCAL:
977                 /* Individual element center uses median center for helpline and such */
978                 calculateCenterMedian(t);
979                 break;
980         case V3D_ACTIVE:
981                 {
982                 /* set median, and if if if... do object center */
983                 EditSelection ese;
984                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
985                 if (G.obedit && G.obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
986                         EM_editselection_center(t->center, &ese);
987                         calculateCenter2D(t);
988                         break;
989                 } /* END EDIT MODE ACTIVE ELEMENT */
990                 
991                 calculateCenterMedian(t);
992                 if((t->flag & (T_EDIT|T_POSE))==0) {
993                         Object *ob= OBACT;
994                         if(ob) {
995                                 VECCOPY(t->center, ob->obmat[3]);
996                                 projectIntView(t, t->center, t->center2d);
997                         }
998                 }
999                 
1000                 }
1001         }
1002
1003         /* setting constraint center */
1004         VECCOPY(t->con.center, t->center);
1005         if(t->flag & (T_EDIT|T_POSE)) {
1006                 Object *ob= G.obedit?G.obedit:t->poseobj;
1007                 Mat4MulVecfl(ob->obmat, t->con.center);
1008         }
1009
1010         /* voor panning from cameraview */
1011         if(t->flag & T_OBJECT) {
1012                 if( G.vd->camera==OBACT && G.vd->persp==V3D_CAMOB) {
1013                         float axis[3];
1014                         /* persinv is nasty, use viewinv instead, always right */
1015                         VECCOPY(axis, t->viewinv[2]);
1016                         Normalize(axis);
1017
1018                         /* 6.0 = 6 grid units */
1019                         axis[0]= t->center[0]- 6.0f*axis[0];
1020                         axis[1]= t->center[1]- 6.0f*axis[1];
1021                         axis[2]= t->center[2]- 6.0f*axis[2];
1022                         
1023                         projectIntView(t, axis, t->center2d);
1024                         
1025                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
1026                         if(t->mode==TFM_TRANSLATION) {
1027                                 VECCOPY(t->center, axis);
1028                                 VECCOPY(t->con.center, t->center);
1029                         }
1030                 }
1031         }       
1032
1033         if(t->spacetype==SPACE_VIEW3D)
1034                 initgrabz(t->center[0], t->center[1], t->center[2]);
1035 }
1036
1037 void calculatePropRatio(TransInfo *t)
1038 {
1039         TransData *td = t->data;
1040         int i;
1041         float dist;
1042         short connected = t->flag & T_PROP_CONNECTED;
1043
1044         if (t->flag & T_PROP_EDIT) {
1045                 for(i = 0 ; i < t->total; i++, td++) {
1046                         if (td->flag & TD_SELECTED) {
1047                                 td->factor = 1.0f;
1048                         }
1049                         else if ((connected && 
1050                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
1051                                 ||
1052                                         (connected == 0 &&
1053                                                 td->rdist > t->propsize)) {
1054                                 /* 
1055                                    The elements are sorted according to their dist member in the array,
1056                                    that means we can stop when it finds one element outside of the propsize.
1057                                 */
1058                                 td->flag |= TD_NOACTION;
1059                                 td->factor = 0.0f;
1060                                 restoreElement(td);
1061                         }
1062                         else {
1063                                 /* Use rdist for falloff calculations, it is the real distance */
1064                                 td->flag &= ~TD_NOACTION;
1065                                 dist= (t->propsize-td->rdist)/t->propsize;
1066                                 
1067                                 /*
1068                                  * Clamp to positive numbers.
1069                                  * Certain corner cases with connectivity and individual centers
1070                                  * can give values of rdist larger than propsize.
1071                                  */
1072                                 if (dist < 0.0f)
1073                                         dist = 0.0f;
1074                                 
1075                                 switch(G.scene->prop_mode) {
1076                                 case PROP_SHARP:
1077                                         td->factor= dist*dist;
1078                                         break;
1079                                 case PROP_SMOOTH:
1080                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1081                                         break;
1082                                 case PROP_ROOT:
1083                                         td->factor = (float)sqrt(dist);
1084                                         break;
1085                                 case PROP_LIN:
1086                                         td->factor = dist;
1087                                         break;
1088                                 case PROP_CONST:
1089                                         td->factor = 1.0f;
1090                                         break;
1091                                 case PROP_SPHERE:
1092                                         td->factor = (float)sqrt(2*dist - dist * dist);
1093                                         break;
1094                                 case PROP_RANDOM:
1095                                         BLI_srand( BLI_rand() ); /* random seed */
1096                                         td->factor = BLI_frand()*dist;
1097                                         break;
1098                                 default:
1099                                         td->factor = 1;
1100                                 }
1101                         }
1102                 }
1103                 switch(G.scene->prop_mode) {
1104                 case PROP_SHARP:
1105                         strcpy(t->proptext, "(Sharp)");
1106                         break;
1107                 case PROP_SMOOTH:
1108                         strcpy(t->proptext, "(Smooth)");
1109                         break;
1110                 case PROP_ROOT:
1111                         strcpy(t->proptext, "(Root)");
1112                         break;
1113                 case PROP_LIN:
1114                         strcpy(t->proptext, "(Linear)");
1115                         break;
1116                 case PROP_CONST:
1117                         strcpy(t->proptext, "(Constant)");
1118                         break;
1119                 case PROP_SPHERE:
1120                         strcpy(t->proptext, "(Sphere)");
1121                         break;
1122                 case PROP_RANDOM:
1123                         strcpy(t->proptext, "(Random)");
1124                         break;
1125                 default:
1126                         strcpy(t->proptext, "");
1127                 }
1128         }
1129         else {
1130                 for(i = 0 ; i < t->total; i++, td++) {
1131                         td->factor = 1.0;
1132                 }
1133                 strcpy(t->proptext, "");
1134         }
1135 }
1136
1137 TransInfo * BIF_GetTransInfo() {
1138         return &Trans;
1139 }
1140