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