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