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