- added GCC warning -Wstrict-prototypes
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_armature_types.h"
39 #include "DNA_lattice_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_view3d_types.h"
46 #include "DNA_modifier_types.h"
47
48 #include "RNA_access.h"
49
50 //#include "BIF_screen.h"
51 //#include "BIF_mywindow.h"
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54 //#include "BIF_editmesh.h"
55 //#include "BIF_editsima.h"
56 //#include "BIF_editparticle.h"
57 //#include "BIF_meshtools.h"
58
59 #include "BKE_animsys.h"
60 #include "BKE_action.h"
61 #include "BKE_armature.h"
62 #include "BKE_curve.h"
63 #include "BKE_depsgraph.h"
64 #include "BKE_displist.h"
65 #include "BKE_fcurve.h"
66 #include "BKE_lattice.h"
67 #include "BKE_mesh.h"
68 #include "BKE_nla.h"
69 #include "BKE_context.h"
70
71 #include "ED_anim_api.h"
72 #include "ED_armature.h"
73 #include "ED_image.h"
74 #include "ED_keyframing.h"
75 #include "ED_markers.h"
76 #include "ED_mesh.h"
77 #include "ED_particle.h"
78 #include "ED_screen_types.h"
79 #include "ED_space_api.h"
80 #include "ED_uvedit.h"
81 #include "ED_view3d.h"
82 #include "ED_curve.h" /* for ED_curve_editnurbs */
83
84 //#include "BDR_unwrapper.h"
85
86 #include "BLI_math.h"
87 #include "BLI_blenlib.h"
88 #include "BLI_editVert.h"
89 #include "BLI_rand.h"
90
91
92 #include "WM_types.h"
93 #include "WM_api.h"
94
95 #include "UI_resources.h"
96
97 //#include "blendef.h"
98 //
99 //#include "mydevice.h"
100
101 #include "transform.h"
102
103 extern ListBase editelems;
104
105 /* ************************** Functions *************************** */
106
107 void getViewVector(TransInfo *t, float coord[3], float vec[3])
108 {
109         if (t->persp != RV3D_ORTHO)
110         {
111                 float p1[4], p2[4];
112                 
113                 VECCOPY(p1, coord);
114                 p1[3] = 1.0f;
115                 VECCOPY(p2, p1);
116                 p2[3] = 1.0f;
117                 mul_m4_v4(t->viewmat, p2);
118                 
119                 p2[0] = 2.0f * p2[0];
120                 p2[1] = 2.0f * p2[1];
121                 p2[2] = 2.0f * p2[2];
122                 
123                 mul_m4_v4(t->viewinv, p2);
124                 
125                 sub_v3_v3v3(vec, p1, p2);
126         }
127         else {
128                 VECCOPY(vec, t->viewinv[2]);
129         }
130         normalize_v3(vec);
131 }
132
133 /* ************************** GENERICS **************************** */
134
135 static void clipMirrorModifier(TransInfo *t, Object *ob)
136 {
137         ModifierData *md= ob->modifiers.first;
138         float tolerance[3] = {0.0f, 0.0f, 0.0f};
139         int axis = 0;
140         
141         for (; md; md=md->next) {
142                 if (md->type==eModifierType_Mirror) {
143                         MirrorModifierData *mmd = (MirrorModifierData*) md;
144                         
145                         if(mmd->flag & MOD_MIR_CLIPPING) {
146                                 axis = 0;
147                                 if(mmd->flag & MOD_MIR_AXIS_X) {
148                                         axis |= 1;
149                                         tolerance[0] = mmd->tolerance;
150                                 }
151                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
152                                         axis |= 2;
153                                         tolerance[1] = mmd->tolerance;
154                                 }
155                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
156                                         axis |= 4;
157                                         tolerance[2] = mmd->tolerance;
158                                 }
159                                 if (axis) {
160                                         float mtx[4][4], imtx[4][4];
161                                         int i;
162                                         TransData *td = t->data;
163                                         
164                                         if (mmd->mirror_ob) {
165                                                 float obinv[4][4];
166                                                 
167                                                 invert_m4_m4(obinv, mmd->mirror_ob->obmat);
168                                                 mul_m4_m4m4(mtx, ob->obmat, obinv);
169                                                 invert_m4_m4(imtx, mtx);
170                                         }
171                                         
172                                         for(i = 0 ; i < t->total; i++, td++) {
173                                                 int clip;
174                                                 float loc[3], iloc[3];
175                                                 
176                                                 if (td->flag & TD_NOACTION)
177                                                         break;
178                                                 if (td->loc==NULL)
179                                                         break;
180                                                 
181                                                 if (td->flag & TD_SKIP)
182                                                         continue;
183                                                 
184                                                 copy_v3_v3(loc,  td->loc);
185                                                 copy_v3_v3(iloc, td->iloc);
186                                                 
187                                                 if (mmd->mirror_ob) {
188                                                         mul_m4_v3(mtx, loc);
189                                                         mul_m4_v3(mtx, iloc);
190                                                 }
191                                                 
192                                                 clip = 0;
193                                                 if(axis & 1) {
194                                                         if(fabs(iloc[0])<=tolerance[0] ||
195                                                            loc[0]*iloc[0]<0.0f) {
196                                                                 loc[0]= 0.0f;
197                                                                 clip = 1;
198                                                         }
199                                                 }
200                                                 
201                                                 if(axis & 2) {
202                                                         if(fabs(iloc[1])<=tolerance[1] ||
203                                                            loc[1]*iloc[1]<0.0f) {
204                                                                 loc[1]= 0.0f;
205                                                                 clip = 1;
206                                                         }
207                                                 }
208                                                 if(axis & 4) {
209                                                         if(fabs(iloc[2])<=tolerance[2] ||
210                                                            loc[2]*iloc[2]<0.0f) {
211                                                                 loc[2]= 0.0f;
212                                                                 clip = 1;
213                                                         }
214                                                 }
215                                                 if (clip) {
216                                                         if (mmd->mirror_ob) {
217                                                                 mul_m4_v3(imtx, loc);
218                                                         }
219                                                         copy_v3_v3(td->loc, loc);
220                                                 }
221                                         }
222                                 }
223                                 
224                         }
225                 }
226         }
227 }
228
229 /* assumes obedit set to mesh object */
230 static void editmesh_apply_to_mirror(TransInfo *t)
231 {
232         TransData *td = t->data;
233         EditVert *eve;
234         int i;
235         
236         for(i = 0 ; i < t->total; i++, td++) {
237                 if (td->flag & TD_NOACTION)
238                         break;
239                 if (td->loc==NULL)
240                         break;
241                 if (td->flag & TD_SKIP)
242                         continue;
243                 
244                 eve = td->extra;
245                 if (eve) {
246                         eve->co[0]= -td->loc[0];
247                         eve->co[1]= td->loc[1];
248                         eve->co[2]= td->loc[2];
249                 }
250                 
251                 if (td->flag & TD_MIRROR_EDGE)
252                 {
253                         td->loc[0] = 0;
254                 }
255         }
256 }
257
258 /* for the realtime animation recording feature, handle overlapping data */
259 static void animrecord_check_state (Scene *scene, ID *id, wmTimer *animtimer)
260 {
261         ScreenAnimData *sad= (animtimer) ? animtimer->customdata : NULL;
262         
263         /* sanity checks */
264         if ELEM3(NULL, scene, id, sad)
265                 return;
266         
267         /* check if we need a new strip if:
268          *      - if animtimer is running 
269          *      - we're not only keying for available channels
270          *      - the option to add new actions for each round is not enabled
271          */
272         if (IS_AUTOKEY_FLAG(INSERTAVAIL)==0 && (scene->toolsettings->autokey_flag & ANIMRECORD_FLAG_WITHNLA)) {
273                 /* if playback has just looped around, we need to add a new NLA track+strip to allow a clean pass to occur */
274                 if ((sad) && (sad->flag & ANIMPLAY_FLAG_JUMPED)) {
275                         AnimData *adt= BKE_animdata_from_id(id);
276                         
277                         /* perform push-down manually with some differences 
278                          * NOTE: BKE_nla_action_pushdown() sync warning...
279                          */
280                         if ((adt->action) && !(adt->flag & ADT_NLA_EDIT_ON)) {
281                                 float astart, aend;
282                                 
283                                 /* only push down if action is more than 1-2 frames long */
284                                 calc_action_range(adt->action, &astart, &aend, 1);
285                                 if (aend > astart+2.0f) {
286                                         NlaStrip *strip= add_nlastrip_to_stack(adt, adt->action);
287                                         
288                                         /* clear reference to action now that we've pushed it onto the stack */
289                                         adt->action->id.us--;
290                                         adt->action= NULL;
291                                         
292                                         /* adjust blending + extend so that they will behave correctly */
293                                         strip->extendmode= NLASTRIP_EXTEND_NOTHING;
294                                         strip->flag &= ~(NLASTRIP_FLAG_AUTO_BLENDS|NLASTRIP_FLAG_SELECT|NLASTRIP_FLAG_ACTIVE);
295                                         
296                                         /* also, adjust the AnimData's action extend mode to be on 
297                                          * 'nothing' so that previous result still play 
298                                          */
299                                         adt->act_extendmode= NLASTRIP_EXTEND_NOTHING;
300                                 }
301                         }
302                 }
303         }
304 }
305
306 static int fcu_test_selected(FCurve *fcu)
307 {
308         BezTriple *bezt= fcu->bezt;
309         unsigned int i;
310
311         if (bezt==NULL) /* ignore baked */
312                 return 0;
313
314         for (i=0; i < fcu->totvert; i++, bezt++) {
315                 if (BEZSELECTED(bezt)) return 1;
316         }
317
318         return 0;
319 }
320
321 /* called for updating while transform acts, once per redraw */
322 void recalcData(TransInfo *t)
323 {
324         Base *base = t->scene->basact;
325
326         if (t->spacetype==SPACE_NODE) {
327                 flushTransNodes(t);
328         }
329         else if (t->spacetype==SPACE_SEQ) {
330                 flushTransSeq(t);
331         }
332         else if (t->spacetype == SPACE_ACTION) {
333                 Scene *scene= t->scene;
334                 SpaceAction *saction= (SpaceAction *)t->sa->spacedata.first;
335                 
336                 bAnimContext ac= {0};
337                 ListBase anim_data = {NULL, NULL};
338                 bAnimListElem *ale;
339                 int filter;
340                 
341                 /* initialise relevant anim-context 'context' data from TransInfo data */
342                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
343                 ac.scene= t->scene;
344                 ac.obact= OBACT;
345                 ac.sa= t->sa;
346                 ac.ar= t->ar;
347                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
348                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
349                 
350                 ANIM_animdata_context_getdata(&ac);
351                 
352                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
353                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_ANIMDATA);
354                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
355                 
356                 /* just tag these animdata-blocks to recalc, assuming that some data there changed 
357                  * BUT only do this if realtime updates are enabled
358                  */
359                 if ((saction->flag & SACTION_NOREALTIMEUPDATES) == 0) {
360                         for (ale= anim_data.first; ale; ale= ale->next) {
361                                 /* set refresh tags for objects using this animation */
362                                 ANIM_list_elem_update(t->scene, ale);
363                         }
364                 }
365                 
366                 /* now free temp channels */
367                 BLI_freelistN(&anim_data);
368         }
369         else if (t->spacetype == SPACE_IPO) {
370                 Scene *scene;
371                 SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
372                 
373                 ListBase anim_data = {NULL, NULL};
374                 bAnimContext ac;
375                 int filter;
376                 
377                 bAnimListElem *ale;
378                 int dosort = 0;
379                 
380                 
381                 /* initialise relevant anim-context 'context' data from TransInfo data */
382                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
383                 memset(&ac, 0, sizeof(bAnimContext));
384                 
385                 scene= ac.scene= t->scene;
386                 ac.obact= OBACT;
387                 ac.sa= t->sa;
388                 ac.ar= t->ar;
389                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
390                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
391                 
392                 ANIM_animdata_context_getdata(&ac);
393                 
394                 /* do the flush first */
395                 flushTransGraphData(t);
396                 
397                 /* get curves to check if a re-sort is needed */
398                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY | ANIMFILTER_CURVEVISIBLE);
399                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
400                 
401                 /* now test if there is a need to re-sort */
402                 for (ale= anim_data.first; ale; ale= ale->next) {
403                         FCurve *fcu= (FCurve *)ale->key_data;
404                         
405                         /* ignore unselected fcurves */
406                         if (!fcu_test_selected(fcu))
407                                 continue;
408                         
409                         // fixme: only do this for selected verts...
410                         ANIM_unit_mapping_apply_fcurve(ac.scene, ale->id, ale->key_data, ANIM_UNITCONV_ONLYSEL|ANIM_UNITCONV_SELVERTS|ANIM_UNITCONV_RESTORE);
411                         
412                         
413                         /* watch it: if the time is wrong: do not correct handles yet */
414                         if (test_time_fcurve(fcu))
415                                 dosort++;
416                         else
417                                 calchandles_fcurve(fcu);
418                         
419                         /* set refresh tags for objects using this animation,
420                          * BUT only if realtime updates are enabled  
421                          */
422                         if ((sipo->flag & SIPO_NOREALTIMEUPDATES) == 0)
423                                 ANIM_list_elem_update(t->scene, ale);
424                 }
425                 
426                 /* do resort and other updates? */
427                 if (dosort) remake_graph_transdata(t, &anim_data);
428                 
429                 /* now free temp channels */
430                 BLI_freelistN(&anim_data);
431         }
432         else if (t->spacetype == SPACE_NLA) {
433                 TransDataNla *tdn= (TransDataNla *)t->customData;
434                 SpaceNla *snla= (SpaceNla *)t->sa->spacedata.first;
435                 Scene *scene= t->scene;
436                 double secf= FPS;
437                 int i;
438                 
439                 /* for each strip we've got, perform some additional validation of the values that got set before
440                  * using RNA to set the value (which does some special operations when setting these values to make
441                  * sure that everything works ok)
442                  */
443                 for (i = 0; i < t->total; i++, tdn++) {
444                         NlaStrip *strip= tdn->strip;
445                         PointerRNA strip_ptr;
446                         short pExceeded, nExceeded, iter;
447                         int delta_y1, delta_y2;
448                         
449                         /* if this tdn has no handles, that means it is just a dummy that should be skipped */
450                         if (tdn->handle == 0)
451                                 continue;
452                         
453                         /* set refresh tags for objects using this animation,
454                          * BUT only if realtime updates are enabled  
455                          */
456                         if ((snla->flag & SNLA_NOREALTIMEUPDATES) == 0)
457                                 ANIM_id_update(t->scene, tdn->id);
458                         
459                         /* if cancelling transform, just write the values without validating, then move on */
460                         if (t->state == TRANS_CANCEL) {
461                                 /* clear the values by directly overwriting the originals, but also need to restore
462                                  * endpoints of neighboring transition-strips
463                                  */
464                                 
465                                 /* start */
466                                 strip->start= tdn->h1[0];
467                                 
468                                 if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION))
469                                         strip->prev->end= tdn->h1[0];
470                                 
471                                 /* end */
472                                 strip->end= tdn->h2[0];
473                                 
474                                 if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION))
475                                         strip->next->start= tdn->h2[0];
476                                 
477                                 /* flush transforms to child strips (since this should be a meta) */
478                                 BKE_nlameta_flush_transforms(strip);
479                                 
480                                 /* restore to original track (if needed) */
481                                 if (tdn->oldTrack != tdn->nlt) {
482                                         /* just append to end of list for now, since strips get sorted in special_aftertrans_update() */
483                                         BLI_remlink(&tdn->nlt->strips, strip);
484                                         BLI_addtail(&tdn->oldTrack->strips, strip);
485                                 }
486                                 
487                                 continue;
488                         }
489                         
490                         /* firstly, check if the proposed transform locations would overlap with any neighbouring strips
491                          * (barring transitions) which are absolute barriers since they are not being moved
492                          *
493                          * this is done as a iterative procedure (done 5 times max for now)
494                          */
495                         for (iter=0; iter < 5; iter++) {
496                                 pExceeded= ((strip->prev) && (strip->prev->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h1[0] < strip->prev->end));
497                                 nExceeded= ((strip->next) && (strip->next->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h2[0] > strip->next->start));
498                                 
499                                 if ((pExceeded && nExceeded) || (iter == 4) ) {
500                                         /* both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise)
501                                          *      - simply crop strip to fit within the bounds of the strips bounding it
502                                          *      - if there were no neighbours, clear the transforms (make it default to the strip's current values)
503                                          */
504                                         if (strip->prev && strip->next) {
505                                                 tdn->h1[0]= strip->prev->end;
506                                                 tdn->h2[0]= strip->next->start;
507                                         }
508                                         else {
509                                                 tdn->h1[0]= strip->start;
510                                                 tdn->h2[0]= strip->end;
511                                         }
512                                 }
513                                 else if (nExceeded) {
514                                         /* move backwards */
515                                         float offset= tdn->h2[0] - strip->next->start;
516                                         
517                                         tdn->h1[0] -= offset;
518                                         tdn->h2[0] -= offset;
519                                 }
520                                 else if (pExceeded) {
521                                         /* more forwards */
522                                         float offset= strip->prev->end - tdn->h1[0];
523                                         
524                                         tdn->h1[0] += offset;
525                                         tdn->h2[0] += offset;
526                                 }
527                                 else /* all is fine and well */
528                                         break;
529                         }
530                         
531                         /* handle auto-snapping */
532                         switch (snla->autosnap) {
533                                 case SACTSNAP_FRAME: /* snap to nearest frame/time  */
534                                         if (snla->flag & SNLA_DRAWTIME) {
535                                                 tdn->h1[0]= (float)( floor((tdn->h1[0]/secf) + 0.5f) * secf );
536                                                 tdn->h2[0]= (float)( floor((tdn->h2[0]/secf) + 0.5f) * secf );
537                                         }
538                                         else {
539                                                 tdn->h1[0]= (float)( floor(tdn->h1[0]+0.5f) );
540                                                 tdn->h2[0]= (float)( floor(tdn->h2[0]+0.5f) );
541                                         }
542                                         break;
543                                 
544                                 case SACTSNAP_MARKER: /* snap to nearest marker */
545                                         tdn->h1[0]= (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h1[0]);
546                                         tdn->h2[0]= (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h2[0]);
547                                         break;
548                         }
549                         
550                         /* use RNA to write the values... */
551                         // TODO: do we need to write in 2 passes to make sure that no truncation goes on?
552                         RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
553                         
554                         RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
555                         RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
556                         
557                         /* flush transforms to child strips (since this should be a meta) */
558                         BKE_nlameta_flush_transforms(strip);
559                         
560                         
561                         /* now, check if we need to try and move track
562                          *      - we need to calculate both, as only one may have been altered by transform if only 1 handle moved
563                          */
564                         delta_y1= ((int)tdn->h1[1] / NLACHANNEL_STEP - tdn->trackIndex);
565                         delta_y2= ((int)tdn->h2[1] / NLACHANNEL_STEP - tdn->trackIndex);
566                         
567                         if (delta_y1 || delta_y2) {
568                                 NlaTrack *track;
569                                 int delta = (delta_y2) ? delta_y2 : delta_y1;
570                                 int n;
571                                 
572                                 /* move in the requested direction, checking at each layer if there's space for strip to pass through,
573                                  * stopping on the last track available or that we're able to fit in
574                                  */
575                                 if (delta > 0) {
576                                         for (track=tdn->nlt->next, n=0; (track) && (n < delta); track=track->next, n++) {
577                                                 /* check if space in this track for the strip */
578                                                 if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
579                                                         /* move strip to this track */
580                                                         BLI_remlink(&tdn->nlt->strips, strip);
581                                                         BKE_nlatrack_add_strip(track, strip);
582                                                         
583                                                         tdn->nlt= track;
584                                                         tdn->trackIndex++;
585                                                 }
586                                                 else /* can't move any further */
587                                                         break;
588                                         }
589                                 }
590                                 else {
591                                         /* make delta 'positive' before using it, since we now know to go backwards */
592                                         delta= -delta;
593                                         
594                                         for (track=tdn->nlt->prev, n=0; (track) && (n < delta); track=track->prev, n++) {
595                                                 /* check if space in this track for the strip */
596                                                 if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
597                                                         /* move strip to this track */
598                                                         BLI_remlink(&tdn->nlt->strips, strip);
599                                                         BKE_nlatrack_add_strip(track, strip);
600                                                         
601                                                         tdn->nlt= track;
602                                                         tdn->trackIndex--;
603                                                 }
604                                                 else /* can't move any further */
605                                                         break;
606                                         }
607                                 }
608                         }
609                 }
610         }
611         else if (t->spacetype == SPACE_IMAGE) {
612                 if (t->obedit && t->obedit->type == OB_MESH) {
613                         SpaceImage *sima= t->sa->spacedata.first;
614                         
615                         flushTransUVs(t);
616                         if(sima->flag & SI_LIVE_UNWRAP)
617                                 ED_uvedit_live_unwrap_re_solve();
618                         
619                         DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);
620                 }
621         }
622         else if (t->spacetype == SPACE_VIEW3D) {
623                 
624                 if (t->obedit) {
625                         if ELEM(t->obedit->type, OB_CURVE, OB_SURF) {
626                                 Curve *cu= t->obedit->data;
627                                 ListBase *nurbs= ED_curve_editnurbs(cu);
628                                 Nurb *nu= nurbs->first;
629
630                                 if(t->state != TRANS_CANCEL) {
631                                         clipMirrorModifier(t, t->obedit);
632                                         applyProject(t);
633                                 }
634
635                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
636
637                                 if (t->state == TRANS_CANCEL) {
638                                         while(nu) {
639                                                 calchandlesNurb(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
640                                                 nu= nu->next;
641                                         }
642                                 } else {
643                                         /* Normal updating */
644                                         while(nu) {
645                                                 test2DNurb(nu);
646                                                 calchandlesNurb(nu);
647                                                 nu= nu->next;
648                                         }
649                                 }
650                         }
651                         else if(t->obedit->type==OB_LATTICE) {
652                                 Lattice *la= t->obedit->data;
653
654                                 if(t->state != TRANS_CANCEL) {
655                                         applyProject(t);
656                                 }
657
658                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
659         
660                                 if(la->editlatt->latt->flag & LT_OUTSIDE) outside_lattice(la->editlatt->latt);
661                         }
662                         else if (t->obedit->type == OB_MESH) {
663                                 EditMesh *em = ((Mesh*)t->obedit->data)->edit_mesh;
664                                 /* mirror modifier clipping? */
665                                 if(t->state != TRANS_CANCEL) {
666                                         clipMirrorModifier(t, t->obedit);
667                                         applyProject(t);
668                                 }
669                                 if((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
670                                         editmesh_apply_to_mirror(t);
671                                         
672                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
673                                 
674                                 recalc_editnormals(em);
675                         }
676                         else if(t->obedit->type==OB_ARMATURE) { /* no recalc flag, does pose */
677                                 bArmature *arm= t->obedit->data;
678                                 ListBase *edbo = arm->edbo;
679                                 EditBone *ebo;
680                                 TransData *td = t->data;
681                                 int i;
682                                 
683                                 if(t->state != TRANS_CANCEL) {
684                                         applyProject(t);
685                                 }
686
687                                 /* Ensure all bones are correctly adjusted */
688                                 for (ebo = edbo->first; ebo; ebo = ebo->next){
689                                         
690                                         if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
691                                                 /* If this bone has a parent tip that has been moved */
692                                                 if (ebo->parent->flag & BONE_TIPSEL){
693                                                         VECCOPY (ebo->head, ebo->parent->tail);
694                                                         if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
695                                                 }
696                                                 /* If this bone has a parent tip that has NOT been moved */
697                                                 else{
698                                                         VECCOPY (ebo->parent->tail, ebo->head);
699                                                         if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
700                                                 }
701                                         }
702                                         
703                                         /* on extrude bones, oldlength==0.0f, so we scale radius of points */
704                                         ebo->length= len_v3v3(ebo->head, ebo->tail);
705                                         if(ebo->oldlength==0.0f) {
706                                                 ebo->rad_head= 0.25f*ebo->length;
707                                                 ebo->rad_tail= 0.10f*ebo->length;
708                                                 ebo->dist= 0.25f*ebo->length;
709                                                 if(ebo->parent) {
710                                                         if(ebo->rad_head > ebo->parent->rad_tail)
711                                                                 ebo->rad_head= ebo->parent->rad_tail;
712                                                 }
713                                         }
714                                         else if(t->mode!=TFM_BONE_ENVELOPE) {
715                                                 /* if bones change length, lets do that for the deform distance as well */
716                                                 ebo->dist*= ebo->length/ebo->oldlength;
717                                                 ebo->rad_head*= ebo->length/ebo->oldlength;
718                                                 ebo->rad_tail*= ebo->length/ebo->oldlength;
719                                                 ebo->oldlength= ebo->length;
720                                         }
721                                 }
722                                 
723                                 
724                                 if (t->mode != TFM_BONE_ROLL)
725                                 {
726                                         /* fix roll */
727                                         for(i = 0; i < t->total; i++, td++)
728                                         {
729                                                 if (td->extra)
730                                                 {
731                                                         float vec[3], up_axis[3];
732                                                         float qrot[4];
733                                                         
734                                                         ebo = td->extra;
735                                                         VECCOPY(up_axis, td->axismtx[2]);
736                                                         
737                                                         if (t->mode != TFM_ROTATION)
738                                                         {
739                                                                 sub_v3_v3v3(vec, ebo->tail, ebo->head);
740                                                                 normalize_v3(vec);
741                                                                 rotation_between_vecs_to_quat(qrot, td->axismtx[1], vec);
742                                                                 mul_qt_v3(qrot, up_axis);
743                                                         }
744                                                         else
745                                                         {
746                                                                 mul_m3_v3(t->mat, up_axis);
747                                                         }
748                                                         
749                                                         ebo->roll = ED_rollBoneToVector(ebo, up_axis, FALSE);
750                                                 }
751                                         }
752                                 }
753                                 
754                                 if(arm->flag & ARM_MIRROR_EDIT)
755                                         transform_armature_mirror_update(t->obedit);
756                                 
757                         }
758                         else
759                         {
760                                 if(t->state != TRANS_CANCEL) {
761                                         applyProject(t);
762                                 }
763                                 DAG_id_flush_update(t->obedit->data, OB_RECALC_DATA);  /* sets recalc flags */
764                         }
765                 }
766                 else if( (t->flag & T_POSE) && t->poseobj) {
767                         Object *ob= t->poseobj;
768                         bArmature *arm= ob->data;
769                         
770                         /* if animtimer is running, and the object already has animation data,
771                          * check if the auto-record feature means that we should record 'samples'
772                          * (i.e. uneditable animation values)
773                          */
774                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
775                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
776                                 int targetless_ik= (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
777                                 
778                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
779                                 autokeyframe_pose_cb_func(NULL, t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
780                         }
781                         
782                         /* old optimize trick... this enforces to bypass the depgraph */
783                         if (!(arm->flag & ARM_DELAYDEFORM)) {
784                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
785                         }
786                         else
787                                 where_is_pose(t->scene, ob);
788                 }
789                 else if(base && (base->object->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(t->scene, base->object)) {
790                         if(t->state != TRANS_CANCEL) {
791                                 applyProject(t);
792                         }
793                         flushTransParticles(t);
794                 }
795                 else {
796                         int i;
797                         
798                         if(t->state != TRANS_CANCEL) {
799                                 applyProject(t);
800                         }
801
802                         for (i = 0; i < t->total; i++) {
803                                 TransData *td = t->data + i;
804                                 Object *ob = td->ob;
805                                 
806                                 if (td->flag & TD_NOACTION)
807                                         break;
808                                 
809                                 if (td->flag & TD_SKIP)
810                                         continue;
811                                 
812                                 /* if animtimer is running, and the object already has animation data,
813                                  * check if the auto-record feature means that we should record 'samples'
814                                  * (i.e. uneditable animation values)
815                                  */
816                                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
817                                 if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
818                                         animrecord_check_state(t->scene, &ob->id, t->animtimer);
819                                         autokeyframe_ob_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode);
820                                 }
821                                 
822                                 /* sets recalc flags fully, instead of flushing existing ones 
823                                  * otherwise proxies don't function correctly
824                                  */
825                                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
826                         }
827                 }
828                 
829                 if(((View3D*)t->view)->drawtype == OB_SHADED)
830                         reshadeall_displist(t->scene);
831         }
832 }
833
834 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options)
835 {
836         float v1[3], v2[3], v3[3];
837         char col[3], col2[3];
838
839         if (t->spacetype == SPACE_VIEW3D)
840         {
841                 View3D *v3d = t->view;
842                 
843                 glPushMatrix();
844                 
845                 //if(t->obedit) glLoadMatrixf(t->obedit->obmat);        // sets opengl viewing
846                 
847                 
848                 copy_v3_v3(v3, dir);
849                 mul_v3_fl(v3, v3d->far);
850                 
851                 sub_v3_v3v3(v2, center, v3);
852                 add_v3_v3v3(v1, center, v3);
853                 
854                 if (options & DRAWLIGHT) {
855                         col[0] = col[1] = col[2] = 220;
856                 }
857                 else {
858                         UI_GetThemeColor3ubv(TH_GRID, col);
859                 }
860                 UI_make_axis_color(col, col2, axis);
861                 glColor3ubv((GLubyte *)col2);
862                 
863                 setlinestyle(0);
864                 glBegin(GL_LINE_STRIP);
865                         glVertex3fv(v1);
866                         glVertex3fv(v2);
867                 glEnd();
868                 
869                 glPopMatrix();
870         }
871 }
872
873 void resetTransRestrictions(TransInfo *t)
874 {
875         t->flag &= ~T_ALL_RESTRICTIONS;
876 }
877
878 int initTransInfo (bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
879 {
880         Scene *sce = CTX_data_scene(C);
881         ToolSettings *ts = CTX_data_tool_settings(C);
882         ARegion *ar = CTX_wm_region(C);
883         ScrArea *sa = CTX_wm_area(C);
884         Object *obedit = CTX_data_edit_object(C);
885         
886         /* moving: is shown in drawobject() (transform color) */
887 //  TRANSFORM_FIX_ME
888 //      if(obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
889 //      else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
890 //      else G.moving= G_TRANSFORM_OBJ;
891         
892         t->scene = sce;
893         t->sa = sa;
894         t->ar = ar;
895         t->obedit = obedit;
896         t->settings = ts;
897         
898         t->data = NULL;
899         t->ext = NULL;
900         
901         t->helpline = HLP_NONE;
902         
903         t->flag = 0;
904         
905         t->redraw = 1; /* redraw first time */
906         
907         if (event)
908         {
909                 t->imval[0] = event->x - t->ar->winrct.xmin;
910                 t->imval[1] = event->y - t->ar->winrct.ymin;
911                 
912                 t->event_type = event->type;
913         }
914         else
915         {
916                 t->imval[0] = 0;
917                 t->imval[1] = 0;
918         }
919         
920         t->con.imval[0] = t->imval[0];
921         t->con.imval[1] = t->imval[1];
922         
923         t->mval[0] = t->imval[0];
924         t->mval[1] = t->imval[1];
925         
926         t->transform            = NULL;
927         t->handleEvent          = NULL;
928         
929         t->total                        = 0;
930         
931         t->val = 0.0f;
932         
933         t->vec[0]                       =
934                 t->vec[1]               =
935                 t->vec[2]               = 0.0f;
936         
937         t->center[0]            =
938                 t->center[1]    =
939                 t->center[2]    = 0.0f;
940         
941         unit_m3(t->mat);
942         
943         /* if there's an event, we're modal */
944         if (event) {
945                 t->flag |= T_MODAL;
946         }
947
948         /* Crease needs edge flag */
949         if (t->mode == TFM_CREASE) {
950                 t->options |= CTX_EDGE;
951         }
952
953         t->spacetype = sa->spacetype;
954         if(t->spacetype == SPACE_VIEW3D)
955         {
956                 View3D *v3d = sa->spacedata.first;
957                 
958                 t->view = v3d;
959                 t->animtimer= CTX_wm_screen(C)->animtimer;
960                 
961                 /* turn manipulator off during transform */
962                 // FIXME: but don't do this when USING the manipulator...
963                 if (t->flag & T_MODAL) {
964                         t->twtype = v3d->twtype;
965                         v3d->twtype = 0;
966                 }
967
968                 if(v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
969                 t->around = v3d->around;
970                 
971                 if (op && RNA_struct_find_property(op->ptr, "constraint_orientation") && RNA_property_is_set(op->ptr, "constraint_orientation"))
972                 {
973                         t->current_orientation = RNA_enum_get(op->ptr, "constraint_orientation");
974                         
975                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C))
976                         {
977                                 t->current_orientation = V3D_MANIP_GLOBAL;
978                         }
979                 }
980                 else
981                 {
982                         t->current_orientation = v3d->twmode;
983                 }
984         }
985         else if(t->spacetype==SPACE_IMAGE || t->spacetype==SPACE_NODE)
986         {
987                 SpaceImage *sima = sa->spacedata.first;
988                 // XXX for now, get View2D  from the active region
989                 t->view = &ar->v2d;
990                 t->around = sima->around;
991         }
992         else if(t->spacetype==SPACE_IPO) 
993         {
994                 SpaceIpo *sipo= sa->spacedata.first;
995                 t->view = &ar->v2d;
996                 t->around = sipo->around;
997         }
998         else
999         {
1000                 // XXX for now, get View2D  from the active region
1001                 t->view = &ar->v2d;
1002                 // XXX for now, the center point is the midpoint of the data
1003                 t->around = V3D_CENTER;
1004         }
1005         
1006         if (op && RNA_property_is_set(op->ptr, "release_confirm"))
1007         {
1008                 if (RNA_boolean_get(op->ptr, "release_confirm"))
1009                 {
1010                         t->flag |= T_RELEASE_CONFIRM;
1011                 }
1012         }
1013         else
1014         {
1015                 if (U.flag & USER_RELEASECONFIRM)
1016                 {
1017                         t->flag |= T_RELEASE_CONFIRM;
1018                 }
1019         }
1020
1021         if (op && RNA_struct_find_property(op->ptr, "mirror") && RNA_property_is_set(op->ptr, "mirror"))
1022         {
1023                 if (RNA_boolean_get(op->ptr, "mirror"))
1024                 {
1025                         t->flag |= T_MIRROR;
1026                         t->mirror = 1;
1027                 }
1028         }
1029         // Need stuff to take it from edit mesh or whatnot here
1030         else
1031         {
1032                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X))
1033                 {
1034                         t->flag |= T_MIRROR;
1035                         t->mirror = 1;
1036                 }
1037         }
1038         
1039         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1040         if (op && RNA_struct_find_property(op->ptr, "proportional"))
1041         {
1042                 if (RNA_property_is_set(op->ptr, "proportional"))
1043                 {
1044                         switch(RNA_enum_get(op->ptr, "proportional"))
1045                         {
1046                         case PROP_EDIT_CONNECTED:
1047                                 t->flag |= T_PROP_CONNECTED;
1048                         case PROP_EDIT_ON:
1049                                 t->flag |= T_PROP_EDIT;
1050                                 break;
1051                         }
1052                 }
1053                 else
1054                 {
1055                         /* use settings from scene only if modal */
1056                         if (t->flag & T_MODAL)
1057                         {
1058                                 if ((t->options & CTX_NO_PET) == 0)
1059                                 {
1060                                         if (t->obedit && ts->proportional != PROP_EDIT_OFF)
1061                                         {
1062                                                 t->flag |= T_PROP_EDIT;
1063
1064                                                 if(ts->proportional == PROP_EDIT_CONNECTED)
1065                                                         t->flag |= T_PROP_CONNECTED;
1066                                         }
1067                                         else if (t->obedit == NULL && ts->proportional_objects)
1068                                         {
1069                                                 t->flag |= T_PROP_EDIT;
1070                                         }
1071                                 }
1072                         }
1073                 }
1074                 
1075                 if (op && RNA_struct_find_property(op->ptr, "proportional_size") && RNA_property_is_set(op->ptr, "proportional_size"))
1076                 {
1077                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1078                 }
1079                 else
1080                 {
1081                         t->prop_size = ts->proportional_size;
1082                 }
1083                 
1084                 
1085                 /* TRANSFORM_FIX_ME rna restrictions */
1086                 if (t->prop_size <= 0.00001f)
1087                 {
1088                         printf("Proportional size (%f) under 0.00001, reseting to 1!\n", t->prop_size);
1089                         t->prop_size = 1.0f;
1090                 }
1091                 
1092                 if (op && RNA_struct_find_property(op->ptr, "proportional_edit_falloff") && RNA_property_is_set(op->ptr, "proportional_edit_falloff"))
1093                 {
1094                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_edit_falloff");
1095                 }
1096                 else
1097                 {
1098                         t->prop_mode = ts->prop_mode;
1099                 }
1100         }
1101         else /* add not pet option to context when not available */
1102         {
1103                 t->options |= CTX_NO_PET;
1104         }
1105         
1106         setTransformViewMatrices(t);
1107         initNumInput(&t->num);
1108         initNDofInput(&t->ndof);
1109         
1110         return 1;
1111 }
1112
1113 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1114 void postTrans (bContext *C, TransInfo *t)
1115 {
1116         TransData *td;
1117         
1118         if (t->draw_handle_view)
1119                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1120         if (t->draw_handle_apply)
1121                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1122         if (t->draw_handle_pixel)
1123                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1124         if (t->draw_handle_cursor)
1125                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1126
1127         if (t->customFree) {
1128                 /* Can take over freeing t->data and data2d etc... */
1129                 t->customFree(t);
1130         }
1131         else if (t->customData) {
1132                 MEM_freeN(t->customData);
1133         }
1134
1135         /* postTrans can be called when nothing is selected, so data is NULL already */
1136         if (t->data) {
1137                 int a;
1138                 
1139                 /* free data malloced per trans-data */
1140                 for(a=0, td= t->data; a<t->total; a++, td++) {
1141                         if (td->flag & TD_BEZTRIPLE) 
1142                                 MEM_freeN(td->hdata);
1143                 }
1144                 MEM_freeN(t->data);
1145         }
1146         
1147         BLI_freelistN(&t->tsnap.points);
1148
1149         if (t->ext) MEM_freeN(t->ext);
1150         if (t->data2d) {
1151                 MEM_freeN(t->data2d);
1152                 t->data2d= NULL;
1153         }
1154         
1155         if(t->spacetype==SPACE_IMAGE) {
1156                 SpaceImage *sima= t->sa->spacedata.first;
1157                 if(sima->flag & SI_LIVE_UNWRAP)
1158                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1159         }
1160         
1161         if (t->mouse.data)
1162         {
1163                 MEM_freeN(t->mouse.data);
1164         }
1165 }
1166
1167 void applyTransObjects(TransInfo *t)
1168 {
1169         TransData *td;
1170         
1171         for (td = t->data; td < t->data + t->total; td++) {
1172                 VECCOPY(td->iloc, td->loc);
1173                 if (td->ext->rot) {
1174                         VECCOPY(td->ext->irot, td->ext->rot);
1175                 }
1176                 if (td->ext->size) {
1177                         VECCOPY(td->ext->isize, td->ext->size);
1178                 }
1179         }
1180         recalcData(t);
1181 }
1182
1183 static void restoreElement(TransData *td) {
1184         /* TransData for crease has no loc */
1185         if (td->loc) {
1186                 VECCOPY(td->loc, td->iloc);
1187         }
1188         if (td->val) {
1189                 *td->val = td->ival;
1190         }
1191
1192         if (td->ext && (td->flag&TD_NO_EXT)==0) {
1193                 if (td->ext->rot) {
1194                         VECCOPY(td->ext->rot, td->ext->irot);
1195                 }
1196                 if(td->ext->rotAngle) {
1197                         *td->ext->rotAngle= td->ext->irotAngle;
1198                 }
1199                 if(td->ext->rotAxis) {
1200                         VECCOPY(td->ext->rotAxis, td->ext->irotAxis);
1201                 }
1202                 /* XXX, drotAngle & drotAxis not used yet */
1203                 if (td->ext->size) {
1204                         VECCOPY(td->ext->size, td->ext->isize);
1205                 }
1206                 if (td->ext->quat) {
1207                         QUATCOPY(td->ext->quat, td->ext->iquat);
1208                 }
1209         }
1210         
1211         if (td->flag & TD_BEZTRIPLE) {
1212                 *(td->hdata->h1) = td->hdata->ih1;
1213                 *(td->hdata->h2) = td->hdata->ih2;
1214         }
1215 }
1216
1217 void restoreTransObjects(TransInfo *t)
1218 {
1219         TransData *td;
1220         TransData2D *td2d;
1221
1222         for (td = t->data; td < t->data + t->total; td++) {
1223                 restoreElement(td);
1224         }
1225         
1226         for (td2d=t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1227                 if (td2d->h1) {
1228                         td2d->h1[0] = td2d->ih1[0];
1229                         td2d->h1[1] = td2d->ih1[1];
1230                 }
1231                 if (td2d->h2) {
1232                         td2d->h2[0] = td2d->ih2[0];
1233                         td2d->h2[1] = td2d->ih2[1];
1234                 }
1235         }
1236
1237         unit_m3(t->mat);
1238         
1239         recalcData(t);
1240 }
1241
1242 void calculateCenter2D(TransInfo *t)
1243 {
1244         if (t->flag & (T_EDIT|T_POSE)) {
1245                 Object *ob= t->obedit?t->obedit:t->poseobj;
1246                 float vec[3];
1247                 
1248                 VECCOPY(vec, t->center);
1249                 mul_m4_v3(ob->obmat, vec);
1250                 projectIntView(t, vec, t->center2d);
1251         }
1252         else {
1253                 projectIntView(t, t->center, t->center2d);
1254         }
1255 }
1256
1257 void calculateCenterCursor(TransInfo *t)
1258 {
1259         float *cursor;
1260         
1261         cursor = give_cursor(t->scene, t->view);
1262         VECCOPY(t->center, cursor);
1263         
1264         /* If edit or pose mode, move cursor in local space */
1265         if (t->flag & (T_EDIT|T_POSE)) {
1266                 Object *ob = t->obedit?t->obedit:t->poseobj;
1267                 float mat[3][3], imat[3][3];
1268                 
1269                 sub_v3_v3v3(t->center, t->center, ob->obmat[3]);
1270                 copy_m3_m4(mat, ob->obmat);
1271                 invert_m3_m3(imat, mat);
1272                 mul_m3_v3(imat, t->center);
1273         }
1274         
1275         calculateCenter2D(t);
1276 }
1277
1278 void calculateCenterCursor2D(TransInfo *t)
1279 {
1280         float aspx=1.0, aspy=1.0;
1281         float *cursor= NULL;
1282         
1283         if(t->spacetype==SPACE_IMAGE) {
1284                 SpaceImage *sima= (SpaceImage *)t->sa->spacedata.first;
1285                 /* only space supported right now but may change */
1286                 ED_space_image_uv_aspect(sima, &aspx, &aspy);
1287                 cursor = sima->cursor;
1288         }
1289         
1290         if (cursor) {
1291                 t->center[0] = cursor[0] * aspx;
1292                 t->center[1] = cursor[1] * aspy;
1293         }
1294         
1295         calculateCenter2D(t);
1296 }
1297
1298 void calculateCenterCursorGraph2D(TransInfo *t)
1299 {
1300         SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
1301         Scene *scene= t->scene;
1302         
1303         /* cursor is combination of current frame, and graph-editor cursor value */
1304         t->center[0]= (float)(scene->r.cfra);
1305         t->center[1]= sipo->cursorVal;
1306         
1307         calculateCenter2D(t);
1308 }
1309
1310 void calculateCenterMedian(TransInfo *t)
1311 {
1312         float partial[3] = {0.0f, 0.0f, 0.0f};
1313         int total = 0;
1314         int i;
1315         
1316         for(i = 0; i < t->total; i++) {
1317                 if (t->data[i].flag & TD_SELECTED) {
1318                         if (!(t->data[i].flag & TD_NOCENTER))
1319                         {
1320                                 add_v3_v3(partial, t->data[i].center);
1321                                 total++;
1322                         }
1323                 }
1324                 else {
1325                         /*
1326                            All the selected elements are at the head of the array
1327                            which means we can stop when it finds unselected data
1328                         */
1329                         break;
1330                 }
1331         }
1332         if(i)
1333                 mul_v3_fl(partial, 1.0f / total);
1334         VECCOPY(t->center, partial);
1335         
1336         calculateCenter2D(t);
1337 }
1338
1339 void calculateCenterBound(TransInfo *t)
1340 {
1341         float max[3];
1342         float min[3];
1343         int i;
1344         for(i = 0; i < t->total; i++) {
1345                 if (i) {
1346                         if (t->data[i].flag & TD_SELECTED) {
1347                                 if (!(t->data[i].flag & TD_NOCENTER))
1348                                         minmax_v3v3_v3(min, max, t->data[i].center);
1349                         }
1350                         else {
1351                                 /*
1352                                    All the selected elements are at the head of the array
1353                                    which means we can stop when it finds unselected data
1354                                 */
1355                                 break;
1356                         }
1357                 }
1358                 else {
1359                         VECCOPY(max, t->data[i].center);
1360                         VECCOPY(min, t->data[i].center);
1361                 }
1362         }
1363         add_v3_v3v3(t->center, min, max);
1364         mul_v3_fl(t->center, 0.5);
1365         
1366         calculateCenter2D(t);
1367 }
1368
1369 void calculateCenter(TransInfo *t)
1370 {
1371         switch(t->around) {
1372         case V3D_CENTER:
1373                 calculateCenterBound(t);
1374                 break;
1375         case V3D_CENTROID:
1376                 calculateCenterMedian(t);
1377                 break;
1378         case V3D_CURSOR:
1379                 if(t->spacetype==SPACE_IMAGE)
1380                         calculateCenterCursor2D(t);
1381                 else if(t->spacetype==SPACE_IPO)
1382                         calculateCenterCursorGraph2D(t);
1383                 else
1384                         calculateCenterCursor(t);
1385                 break;
1386         case V3D_LOCAL:
1387                 /* Individual element center uses median center for helpline and such */
1388                 calculateCenterMedian(t);
1389                 break;
1390         case V3D_ACTIVE:
1391                 {
1392                 /* set median, and if if if... do object center */
1393                 
1394                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1395
1396                 if (t->obedit && t->obedit->type == OB_MESH) {
1397                         EditSelection ese;
1398                         EditMesh *em = BKE_mesh_get_editmesh(t->obedit->data);
1399                         
1400                         if (EM_get_actSelection(em, &ese)) {
1401                                 EM_editselection_center(t->center, &ese);
1402                                 calculateCenter2D(t);
1403                                 break;
1404                         }
1405                 } /* END EDIT MODE ACTIVE ELEMENT */
1406                 
1407                 calculateCenterMedian(t);
1408                 if((t->flag & (T_EDIT|T_POSE))==0)
1409                 {
1410                         Scene *scene = t->scene;
1411                         Object *ob= OBACT;
1412                         if(ob)
1413                         {
1414                                 VECCOPY(t->center, ob->obmat[3]);
1415                                 projectIntView(t, t->center, t->center2d);
1416                         }
1417                 }
1418                 
1419                 }
1420         }
1421         
1422         /* setting constraint center */
1423         VECCOPY(t->con.center, t->center);
1424         if(t->flag & (T_EDIT|T_POSE))
1425         {
1426                 Object *ob= t->obedit?t->obedit:t->poseobj;
1427                 mul_m4_v3(ob->obmat, t->con.center);
1428         }
1429         
1430         /* for panning from cameraview */
1431         if(t->flag & T_OBJECT)
1432         {
1433                 if(t->spacetype==SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW)
1434                 {
1435                         View3D *v3d = t->view;
1436                         Scene *scene = t->scene;
1437                         RegionView3D *rv3d = t->ar->regiondata;
1438                         
1439                         if(v3d->camera == OBACT && rv3d->persp==RV3D_CAMOB)
1440                         {
1441                                 float axis[3];
1442                                 /* persinv is nasty, use viewinv instead, always right */
1443                                 VECCOPY(axis, t->viewinv[2]);
1444                                 normalize_v3(axis);
1445                                 
1446                                 /* 6.0 = 6 grid units */
1447                                 axis[0]= t->center[0]- 6.0f*axis[0];
1448                                 axis[1]= t->center[1]- 6.0f*axis[1];
1449                                 axis[2]= t->center[2]- 6.0f*axis[2];
1450                                 
1451                                 projectIntView(t, axis, t->center2d);
1452                                 
1453                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1454                                 if(t->mode==TFM_TRANSLATION)
1455                                 {
1456                                         VECCOPY(t->center, axis);
1457                                         VECCOPY(t->con.center, t->center);
1458                                 }
1459                         }
1460                 }
1461         }
1462         
1463         if(t->spacetype==SPACE_VIEW3D)
1464         {
1465                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1466                 if(t->flag & (T_EDIT|T_POSE)) {
1467                         Object *ob= t->obedit?t->obedit:t->poseobj;
1468                         float vec[3];
1469                         
1470                         VECCOPY(vec, t->center);
1471                         mul_m4_v3(ob->obmat, vec);
1472                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1473                 }
1474                 else {
1475                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1476                 }
1477         }
1478 }
1479
1480 void calculatePropRatio(TransInfo *t)
1481 {
1482         TransData *td = t->data;
1483         int i;
1484         float dist;
1485         short connected = t->flag & T_PROP_CONNECTED;
1486
1487         if (t->flag & T_PROP_EDIT) {
1488                 for(i = 0 ; i < t->total; i++, td++) {
1489                         if (td->flag & TD_SELECTED) {
1490                                 td->factor = 1.0f;
1491                         }
1492                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f)
1493                         {
1494                                 td->flag |= TD_SKIP;
1495                                 td->factor = 0.0f;
1496                                 restoreElement(td);
1497                         }
1498                         else if ((connected &&
1499                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size))
1500                                 ||
1501                                         (connected == 0 &&
1502                                                 td->rdist > t->prop_size)) {
1503                                 /*
1504                                    The elements are sorted according to their dist member in the array,
1505                                    that means we can stop when it finds one element outside of the propsize.
1506                                 */
1507                                 td->flag |= TD_NOACTION;
1508                                 td->factor = 0.0f;
1509                                 restoreElement(td);
1510                         }
1511                         else {
1512                                 /* Use rdist for falloff calculations, it is the real distance */
1513                                 td->flag &= ~TD_NOACTION;
1514                                 
1515                                 if (connected)
1516                                         dist= (t->prop_size-td->dist)/t->prop_size;
1517                                 else
1518                                         dist= (t->prop_size-td->rdist)/t->prop_size;
1519
1520                                 /*
1521                                  * Clamp to positive numbers.
1522                                  * Certain corner cases with connectivity and individual centers
1523                                  * can give values of rdist larger than propsize.
1524                                  */
1525                                 if (dist < 0.0f)
1526                                         dist = 0.0f;
1527                                 
1528                                 switch(t->prop_mode) {
1529                                 case PROP_SHARP:
1530                                         td->factor= dist*dist;
1531                                         break;
1532                                 case PROP_SMOOTH:
1533                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1534                                         break;
1535                                 case PROP_ROOT:
1536                                         td->factor = (float)sqrt(dist);
1537                                         break;
1538                                 case PROP_LIN:
1539                                         td->factor = dist;
1540                                         break;
1541                                 case PROP_CONST:
1542                                         td->factor = 1.0f;
1543                                         break;
1544                                 case PROP_SPHERE:
1545                                         td->factor = (float)sqrt(2*dist - dist * dist);
1546                                         break;
1547                                 case PROP_RANDOM:
1548                                         BLI_srand( BLI_rand() ); /* random seed */
1549                                         td->factor = BLI_frand()*dist;
1550                                         break;
1551                                 default:
1552                                         td->factor = 1;
1553                                 }
1554                         }
1555                 }
1556                 switch(t->prop_mode) {
1557                 case PROP_SHARP:
1558                         strcpy(t->proptext, "(Sharp)");
1559                         break;
1560                 case PROP_SMOOTH:
1561                         strcpy(t->proptext, "(Smooth)");
1562                         break;
1563                 case PROP_ROOT:
1564                         strcpy(t->proptext, "(Root)");
1565                         break;
1566                 case PROP_LIN:
1567                         strcpy(t->proptext, "(Linear)");
1568                         break;
1569                 case PROP_CONST:
1570                         strcpy(t->proptext, "(Constant)");
1571                         break;
1572                 case PROP_SPHERE:
1573                         strcpy(t->proptext, "(Sphere)");
1574                         break;
1575                 case PROP_RANDOM:
1576                         strcpy(t->proptext, "(Random)");
1577                         break;
1578                 default:
1579                         strcpy(t->proptext, "");
1580                 }
1581         }
1582         else {
1583                 for(i = 0 ; i < t->total; i++, td++) {
1584                         td->factor = 1.0;
1585                 }
1586                 strcpy(t->proptext, "");
1587         }
1588 }