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