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