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