2.5 - Silencing various compiler warnings (mingw)
[blender-staging.git] / source / blender / editors / transform / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLO_sys_types.h" // for intptr_t support
36
37 #include "DNA_anim_types.h"
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_nla_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_object_force.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_windowmanager_types.h"
56
57 #include "RNA_access.h"
58
59 //#include "BIF_screen.h"
60 //#include "BIF_mywindow.h"
61 #include "BIF_gl.h"
62 //#include "BIF_editmesh.h"
63 //#include "BIF_editsima.h"
64 //#include "BIF_editparticle.h"
65 //#include "BIF_meshtools.h"
66
67 #include "BKE_animsys.h"
68 #include "BKE_action.h"
69 #include "BKE_anim.h"
70 #include "BKE_armature.h"
71 #include "BKE_cloth.h"
72 #include "BKE_curve.h"
73 #include "BKE_depsgraph.h"
74 #include "BKE_displist.h"
75 #include "BKE_depsgraph.h"
76 #include "BKE_fcurve.h"
77 #include "BKE_global.h"
78 #include "BKE_group.h"
79 #include "BKE_lattice.h"
80 #include "BKE_key.h"
81 #include "BKE_mesh.h"
82 #include "BKE_modifier.h"
83 #include "BKE_nla.h"
84 #include "BKE_object.h"
85 #include "BKE_utildefines.h"
86 #include "BKE_context.h"
87
88 #include "ED_anim_api.h"
89 #include "ED_armature.h"
90 #include "ED_image.h"
91 #include "ED_keyframing.h"
92 #include "ED_markers.h"
93 #include "ED_mesh.h"
94 #include "ED_retopo.h"
95 #include "ED_space_api.h"
96 #include "ED_uvedit.h"
97 #include "ED_view3d.h"
98
99 //#include "BDR_unwrapper.h"
100
101 #include "BLI_arithb.h"
102 #include "BLI_blenlib.h"
103 #include "BLI_editVert.h"
104 #include "BLI_rand.h"
105
106 #include "RNA_access.h"
107
108 #include "WM_types.h"
109
110 #include "UI_resources.h"
111
112 //#include "blendef.h"
113 //
114 //#include "mydevice.h"
115
116 #include "transform.h"
117
118 extern ListBase editelems;
119
120 /* ************************** Functions *************************** */
121
122 void getViewVector(TransInfo *t, float coord[3], float vec[3])
123 {
124         if (t->persp != V3D_ORTHO)
125         {
126                 float p1[4], p2[4];
127
128                 VECCOPY(p1, coord);
129                 p1[3] = 1.0f;
130                 VECCOPY(p2, p1);
131                 p2[3] = 1.0f;
132                 Mat4MulVec4fl(t->viewmat, p2);
133
134                 p2[0] = 2.0f * p2[0];
135                 p2[1] = 2.0f * p2[1];
136                 p2[2] = 2.0f * p2[2];
137
138                 Mat4MulVec4fl(t->viewinv, p2);
139
140                 VecSubf(vec, p1, p2);
141         }
142         else {
143                 VECCOPY(vec, t->viewinv[2]);
144         }
145         Normalize(vec);
146 }
147
148 /* ************************** GENERICS **************************** */
149
150 static void clipMirrorModifier(TransInfo *t, Object *ob)
151 {
152         ModifierData *md= ob->modifiers.first;
153         float tolerance[3] = {0.0f, 0.0f, 0.0f};
154         int axis = 0;
155
156         for (; md; md=md->next) {
157                 if (md->type==eModifierType_Mirror) {
158                         MirrorModifierData *mmd = (MirrorModifierData*) md;
159
160                         if(mmd->flag & MOD_MIR_CLIPPING) {
161                                 axis = 0;
162                                 if(mmd->flag & MOD_MIR_AXIS_X) {
163                                         axis |= 1;
164                                         tolerance[0] = mmd->tolerance;
165                                 }
166                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
167                                         axis |= 2;
168                                         tolerance[1] = mmd->tolerance;
169                                 }
170                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
171                                         axis |= 4;
172                                         tolerance[2] = mmd->tolerance;
173                                 }
174                                 if (axis) {
175                                         float mtx[4][4], imtx[4][4];
176                                         int i;
177                                         TransData *td = t->data;
178
179                                         if (mmd->mirror_ob) {
180                                                 float obinv[4][4];
181
182                                                 Mat4Invert(obinv, mmd->mirror_ob->obmat);
183                                                 Mat4MulMat4(mtx, ob->obmat, obinv);
184                                                 Mat4Invert(imtx, mtx);
185                                         }
186
187                                         for(i = 0 ; i < t->total; i++, td++) {
188                                                 int clip;
189                                                 float loc[3], iloc[3];
190
191                                                 if (td->flag & TD_NOACTION)
192                                                         break;
193                                                 if (td->loc==NULL)
194                                                         break;
195
196                                                 if (td->flag & TD_SKIP)
197                                                         continue;
198
199                                                 VecCopyf(loc,  td->loc);
200                                                 VecCopyf(iloc, td->iloc);
201
202                                                 if (mmd->mirror_ob) {
203                                                         VecMat4MulVecfl(loc, mtx, loc);
204                                                         VecMat4MulVecfl(iloc, mtx, iloc);
205                                                 }
206
207                                                 clip = 0;
208                                                 if(axis & 1) {
209                                                         if(fabs(iloc[0])<=tolerance[0] ||
210                                                            loc[0]*iloc[0]<0.0f) {
211                                                                 loc[0]= 0.0f;
212                                                                 clip = 1;
213                                                         }
214                                                 }
215
216                                                 if(axis & 2) {
217                                                         if(fabs(iloc[1])<=tolerance[1] ||
218                                                            loc[1]*iloc[1]<0.0f) {
219                                                                 loc[1]= 0.0f;
220                                                                 clip = 1;
221                                                         }
222                                                 }
223                                                 if(axis & 4) {
224                                                         if(fabs(iloc[2])<=tolerance[2] ||
225                                                            loc[2]*iloc[2]<0.0f) {
226                                                                 loc[2]= 0.0f;
227                                                                 clip = 1;
228                                                         }
229                                                 }
230                                                 if (clip) {
231                                                         if (mmd->mirror_ob) {
232                                                                 VecMat4MulVecfl(loc, imtx, loc);
233                                                         }
234                                                         VecCopyf(td->loc, loc);
235                                                 }
236                                         }
237                                 }
238
239                         }
240                 }
241         }
242 }
243
244 /* assumes obedit set to mesh object */
245 static void editmesh_apply_to_mirror(TransInfo *t)
246 {
247         TransData *td = t->data;
248         EditVert *eve;
249         int i;
250
251         for(i = 0 ; i < t->total; i++, td++) {
252                 if (td->flag & TD_NOACTION)
253                         break;
254                 if (td->loc==NULL)
255                         break;
256                 if (td->flag & TD_SKIP)
257                         continue;
258
259                 eve = td->extra;
260                 if(eve) {
261                         eve->co[0]= -td->loc[0];
262                         eve->co[1]= td->loc[1];
263                         eve->co[2]= td->loc[2];
264                 }
265         }
266 }
267
268 /* tags the given ID block for refreshes (if applicable) due to 
269  * Animation Editor editing
270  */
271 static void animedit_refresh_id_tags (ID *id)
272 {
273         AnimData *adt= BKE_animdata_from_id(id);
274         
275         /* tag AnimData for refresh so that other views will update in realtime with these changes */
276         if (adt)
277                 adt->recalc |= ADT_RECALC_ANIM;
278                 
279         /* if ID-block is Object, set recalc flags */
280         // TODO: this should probably go through the depsgraph instead... but for now, let's be lazy
281         switch (GS(id->name)) {
282                 case ID_OB:
283                 {
284                         Object *ob= (Object *)id;
285                         ob->recalc |= OB_RECALC;
286                 }
287                         break;
288         }
289 }
290
291 /* called for updating while transform acts, once per redraw */
292 void recalcData(TransInfo *t)
293 {
294         Scene *scene = t->scene;
295         Base *base;
296
297         if (t->obedit) {
298         }
299         else if(G.f & G_PARTICLEEDIT) {
300                 flushTransParticles(t);
301         }
302         if (t->spacetype==SPACE_NODE) {
303                 flushTransNodes(t);
304         }
305         else if (t->spacetype==SPACE_SEQ) {
306                 flushTransSeq(t);
307         }
308         else if (t->spacetype == SPACE_ACTION) {
309                 Scene *scene;
310                 
311                 bAnimContext ac;
312                 ListBase anim_data = {NULL, NULL};
313                 bAnimListElem *ale;
314                 int filter;
315                 
316                 /* initialise relevant anim-context 'context' data from TransInfo data */
317                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
318                 memset(&ac, 0, sizeof(bAnimContext));
319                 
320                 scene= ac.scene= t->scene;
321                 ac.obact= OBACT;
322                 ac.sa= t->sa;
323                 ac.ar= t->ar;
324                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
325                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
326                 
327                 ANIM_animdata_context_getdata(&ac);
328                 
329                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
330                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_ANIMDATA);
331                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
332                 
333                 /* just tag these animdata-blocks to recalc, assuming that some data there changed */
334                 for (ale= anim_data.first; ale; ale= ale->next) {
335                         /* set refresh tags for objects using this animation */
336                         animedit_refresh_id_tags(ale->id);
337                 }
338                 
339                 /* now free temp channels */
340                 BLI_freelistN(&anim_data);
341         }
342         else if (t->spacetype == SPACE_IPO) {
343                 Scene *scene;
344                 
345                 ListBase anim_data = {NULL, NULL};
346                 bAnimContext ac;
347                 int filter;
348                 
349                 bAnimListElem *ale;
350                 int dosort = 0;
351                 
352                 
353                 /* initialise relevant anim-context 'context' data from TransInfo data */
354                         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
355                 memset(&ac, 0, sizeof(bAnimContext));
356                 
357                 scene= ac.scene= t->scene;
358                 ac.obact= OBACT;
359                 ac.sa= t->sa;
360                 ac.ar= t->ar;
361                 ac.spacetype= (t->sa)? t->sa->spacetype : 0;
362                 ac.regiontype= (t->ar)? t->ar->regiontype : 0;
363                 
364                 ANIM_animdata_context_getdata(&ac);
365                 
366                 /* do the flush first */
367                 flushTransGraphData(t);
368                 
369                 /* get curves to check if a re-sort is needed */
370                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVESONLY | ANIMFILTER_CURVEVISIBLE);
371                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
372                 
373                 /* now test if there is a need to re-sort */
374                 for (ale= anim_data.first; ale; ale= ale->next) {
375                         FCurve *fcu= (FCurve *)ale->key_data;
376                         
377                         /* watch it: if the time is wrong: do not correct handles yet */
378                         if (test_time_fcurve(fcu))
379                                 dosort++;
380                         else
381                                 calchandles_fcurve(fcu);
382                                 
383                         /* set refresh tags for objects using this animation */
384                         animedit_refresh_id_tags(ale->id);
385                 }
386                 
387                 /* do resort and other updates? */
388                 if (dosort) remake_graph_transdata(t, &anim_data);
389                 
390                 /* now free temp channels */
391                 BLI_freelistN(&anim_data);
392         }
393         else if (t->spacetype == SPACE_NLA) {
394                 TransDataNla *tdn= (TransDataNla *)t->customData;
395                 SpaceNla *snla= (SpaceNla *)t->sa->spacedata.first;
396                 Scene *scene= t->scene;
397                 double secf= FPS;
398                 int i;
399                 
400                 /* for each strip we've got, perform some additional validation of the values that got set before
401                  * using RNA to set the value (which does some special operations when setting these values to make
402                  * sure that everything works ok)
403                  */
404                 for (i = 0; i < t->total; i++, tdn++) {
405                         NlaStrip *strip= tdn->strip;
406                         PointerRNA strip_ptr;
407                         short pExceeded, nExceeded, iter;
408                         int delta_y1, delta_y2;
409                         
410                         /* if this tdn has no handles, that means it is just a dummy that should be skipped */
411                         if (tdn->handle == 0)
412                                 continue;
413                         
414                         /* set refresh tags for objects using this animation */
415                         animedit_refresh_id_tags(tdn->id);
416                         
417                         /* if cancelling transform, just write the values without validating, then move on */
418                         if (t->state == TRANS_CANCEL) {
419                                 /* clear the values by directly overwriting the originals, but also need to restore
420                                  * endpoints of neighboring transition-strips
421                                  */
422                                 
423                                 /* start */
424                                 strip->start= tdn->h1[0];
425                                 
426                                 if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION))
427                                         strip->prev->end= tdn->h1[0];
428                                 
429                                 /* end */
430                                 strip->end= tdn->h2[0];
431                                 
432                                 if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION))
433                                         strip->next->start= tdn->h2[0];
434                                 
435                                 /* flush transforms to child strips (since this should be a meta) */
436                                 BKE_nlameta_flush_transforms(strip);
437                                 
438                                 /* restore to original track (if needed) */
439                                 if (tdn->oldTrack != tdn->nlt) {
440                                         /* just append to end of list for now, since strips get sorted in special_aftertrans_update() */
441                                         BLI_remlink(&tdn->nlt->strips, strip);
442                                         BLI_addtail(&tdn->oldTrack->strips, strip);
443                                 }
444                                 
445                                 continue;
446                         }
447                         
448                         /* firstly, check if the proposed transform locations would overlap with any neighbouring strips
449                          * (barring transitions) which are absolute barriers since they are not being moved
450                          *
451                          * this is done as a iterative procedure (done 5 times max for now)
452                          */
453                         for (iter=0; iter < 5; iter++) {
454                                 pExceeded= ((strip->prev) && (strip->prev->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h1[0] < strip->prev->end));
455                                 nExceeded= ((strip->next) && (strip->next->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h2[0] > strip->next->start));
456                                 
457                                 if ((pExceeded && nExceeded) || (iter == 4) ) {
458                                         /* both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise)
459                                          *      - simply crop strip to fit within the bounds of the strips bounding it
460                                          *      - if there were no neighbours, clear the transforms (make it default to the strip's current values)
461                                          */
462                                         if (strip->prev && strip->next) {
463                                                 tdn->h1[0]= strip->prev->end;
464                                                 tdn->h2[0]= strip->next->start;
465                                         }
466                                         else {
467                                                 tdn->h1[0]= strip->start;
468                                                 tdn->h2[0]= strip->end;
469                                         }
470                                 }
471                                 else if (nExceeded) {
472                                         /* move backwards */
473                                         float offset= tdn->h2[0] - strip->next->start;
474                                         
475                                         tdn->h1[0] -= offset;
476                                         tdn->h2[0] -= offset;
477                                 }
478                                 else if (pExceeded) {
479                                         /* more forwards */
480                                         float offset= strip->prev->end - tdn->h1[0];
481                                         
482                                         tdn->h1[0] += offset;
483                                         tdn->h2[0] += offset;
484                                 }
485                                 else /* all is fine and well */
486                                         break;
487                         }
488                         
489                         /* handle auto-snapping */
490                         switch (snla->autosnap) {
491                                 case SACTSNAP_FRAME: /* snap to nearest frame/time  */
492                                         if (snla->flag & SNLA_DRAWTIME) {
493                                                 tdn->h1[0]= (float)( floor((tdn->h1[0]/secf) + 0.5f) * secf );
494                                                 tdn->h2[0]= (float)( floor((tdn->h2[0]/secf) + 0.5f) * secf );
495                                         }
496                                         else {
497                                                 tdn->h1[0]= (float)( floor(tdn->h1[0]+0.5f) );
498                                                 tdn->h2[0]= (float)( floor(tdn->h2[0]+0.5f) );
499                                         }
500                                         break;
501                                 
502                                 case SACTSNAP_MARKER: /* snap to nearest marker */
503                                         tdn->h1[0]= (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h1[0]);
504                                         tdn->h2[0]= (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h2[0]);
505                                         break;
506                         }
507                         
508                         /* use RNA to write the values... */
509                         // TODO: do we need to write in 2 passes to make sure that no truncation goes on?
510                         RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
511                         
512                         RNA_float_set(&strip_ptr, "start_frame", tdn->h1[0]);
513                         RNA_float_set(&strip_ptr, "end_frame", tdn->h2[0]);
514                         
515                         /* flush transforms to child strips (since this should be a meta) */
516                         BKE_nlameta_flush_transforms(strip);
517                         
518                         
519                         /* now, check if we need to try and move track
520                          *      - we need to calculate both, as only one may have been altered by transform if only 1 handle moved
521                          */
522                         delta_y1= ((int)tdn->h1[1] / NLACHANNEL_STEP - tdn->trackIndex);
523                         delta_y2= ((int)tdn->h2[1] / NLACHANNEL_STEP - tdn->trackIndex);
524                         
525                         if (delta_y1 || delta_y2) {
526                                 NlaTrack *track;
527                                 int delta = (delta_y2) ? delta_y2 : delta_y1;
528                                 int n;
529                                 
530                                 /* move in the requested direction, checking at each layer if there's space for strip to pass through,
531                                  * stopping on the last track available or that we're able to fit in
532                                  */
533                                 if (delta > 0) {
534                                         for (track=tdn->nlt->next, n=0; (track) && (n < delta); track=track->next, n++) {
535                                                 /* check if space in this track for the strip */
536                                                 if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
537                                                         /* move strip to this track */
538                                                         BLI_remlink(&tdn->nlt->strips, strip);
539                                                         BKE_nlatrack_add_strip(track, strip);
540                                                         
541                                                         tdn->nlt= track;
542                                                         tdn->trackIndex += (n + 1); /* + 1, since n==0 would mean that we didn't change track */
543                                                 }
544                                                 else /* can't move any further */
545                                                         break;
546                                         }
547                                 }
548                                 else {
549                                         /* make delta 'positive' before using it, since we now know to go backwards */
550                                         delta= -delta;
551                                         
552                                         for (track=tdn->nlt->prev, n=0; (track) && (n < delta); track=track->prev, n++) {
553                                                 /* check if space in this track for the strip */
554                                                 if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
555                                                         /* move strip to this track */
556                                                         BLI_remlink(&tdn->nlt->strips, strip);
557                                                         BKE_nlatrack_add_strip(track, strip);
558                                                         
559                                                         tdn->nlt= track;
560                                                         tdn->trackIndex -= (n - 1); /* - 1, since n==0 would mean that we didn't change track */
561                                                 }
562                                                 else /* can't move any further */
563                                                         break;
564                                         }
565                                 }
566                         }
567                 }
568         }
569         else if (t->obedit) {
570                 if ELEM(t->obedit->type, OB_CURVE, OB_SURF) {
571                         Curve *cu= t->obedit->data;
572                         Nurb *nu= cu->editnurb->first;
573
574                         DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
575
576                         if (t->state == TRANS_CANCEL) {
577                                 while(nu) {
578                                         calchandlesNurb(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
579                                         nu= nu->next;
580                                 }
581                         } else {
582                                 /* Normal updating */
583                                 while(nu) {
584                                         test2DNurb(nu);
585                                         calchandlesNurb(nu);
586                                         nu= nu->next;
587                                 }
588                                 /* TRANSFORM_FIX_ME */
589                                 // retopo_do_all();
590                         }
591                 }
592                 else if(t->obedit->type==OB_LATTICE) {
593                         Lattice *la= t->obedit->data;
594                         DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
595
596                         if(la->editlatt->flag & LT_OUTSIDE) outside_lattice(la->editlatt);
597                 }
598                 else if (t->obedit->type == OB_MESH) {
599                         if(t->spacetype==SPACE_IMAGE) {
600                                 SpaceImage *sima= t->sa->spacedata.first;
601
602                                 flushTransUVs(t);
603                                 if(sima->flag & SI_LIVE_UNWRAP)
604                                         ED_uvedit_live_unwrap_re_solve();
605
606                                 DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);
607                         } else {
608                                 EditMesh *em = ((Mesh*)t->obedit->data)->edit_mesh;
609                                 /* mirror modifier clipping? */
610                                 if(t->state != TRANS_CANCEL) {
611                                         /* TRANSFORM_FIX_ME */
612 //                                      if ((G.qual & LR_CTRLKEY)==0) {
613 //                                              /* Only retopo if not snapping, Note, this is the only case of G.qual being used, but we have no T_SHIFT_MOD - Campbell */
614 //                                              retopo_do_all();
615 //                                      }
616                                         clipMirrorModifier(t, t->obedit);
617                                 }
618                                 if((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
619                                         editmesh_apply_to_mirror(t);
620
621                                 DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
622
623                                 recalc_editnormals(em);
624                         }
625                 }
626                 else if(t->obedit->type==OB_ARMATURE) { /* no recalc flag, does pose */
627                         bArmature *arm= t->obedit->data;
628                         ListBase *edbo = arm->edbo;
629                         EditBone *ebo;
630                         TransData *td = t->data;
631                         int i;
632
633                         /* Ensure all bones are correctly adjusted */
634                         for (ebo = edbo->first; ebo; ebo = ebo->next){
635
636                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
637                                         /* If this bone has a parent tip that has been moved */
638                                         if (ebo->parent->flag & BONE_TIPSEL){
639                                                 VECCOPY (ebo->head, ebo->parent->tail);
640                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
641                                         }
642                                         /* If this bone has a parent tip that has NOT been moved */
643                                         else{
644                                                 VECCOPY (ebo->parent->tail, ebo->head);
645                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
646                                         }
647                                 }
648
649                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
650                                 ebo->length= VecLenf(ebo->head, ebo->tail);
651                                 if(ebo->oldlength==0.0f) {
652                                         ebo->rad_head= 0.25f*ebo->length;
653                                         ebo->rad_tail= 0.10f*ebo->length;
654                                         ebo->dist= 0.25f*ebo->length;
655                                         if(ebo->parent) {
656                                                 if(ebo->rad_head > ebo->parent->rad_tail)
657                                                         ebo->rad_head= ebo->parent->rad_tail;
658                                         }
659                                 }
660                                 else if(t->mode!=TFM_BONE_ENVELOPE) {
661                                         /* if bones change length, lets do that for the deform distance as well */
662                                         ebo->dist*= ebo->length/ebo->oldlength;
663                                         ebo->rad_head*= ebo->length/ebo->oldlength;
664                                         ebo->rad_tail*= ebo->length/ebo->oldlength;
665                                         ebo->oldlength= ebo->length;
666                                 }
667                         }
668
669
670                         if (t->mode != TFM_BONE_ROLL)
671                         {
672                                 /* fix roll */
673                                 for(i = 0; i < t->total; i++, td++)
674                                 {
675                                         if (td->extra)
676                                         {
677                                                 float vec[3], up_axis[3];
678                                                 float qrot[4];
679
680                                                 ebo = td->extra;
681                                                 VECCOPY(up_axis, td->axismtx[2]);
682
683                                                 if (t->mode != TFM_ROTATION)
684                                                 {
685                                                         VecSubf(vec, ebo->tail, ebo->head);
686                                                         Normalize(vec);
687                                                         RotationBetweenVectorsToQuat(qrot, td->axismtx[1], vec);
688                                                         QuatMulVecf(qrot, up_axis);
689                                                 }
690                                                 else
691                                                 {
692                                                         Mat3MulVecfl(t->mat, up_axis);
693                                                 }
694
695                                                 ebo->roll = ED_rollBoneToVector(ebo, up_axis);
696                                         }
697                                 }
698                         }
699
700                         if(arm->flag & ARM_MIRROR_EDIT)
701                                 transform_armature_mirror_update(t->obedit);
702
703                 }
704                 else
705                         DAG_object_flush_update(scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
706         }
707         else if( (t->flag & T_POSE) && t->poseobj) {
708                 Object *ob= t->poseobj;
709                 bArmature *arm= ob->data;
710
711                 /* if animtimer is running, and the object already has animation data,
712                  * check if the auto-record feature means that we should record 'samples'
713                  * (i.e. uneditable animation values)
714                  */
715                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
716                 // TODO: maybe the ob->adt check isn't really needed? makes it too difficult to use...
717                 if (/*(ob->adt) && */(t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
718                         short targetless_ik= (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
719                         autokeyframe_pose_cb_func(t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
720                 }
721
722                 /* old optimize trick... this enforces to bypass the depgraph */
723                 if (!(arm->flag & ARM_DELAYDEFORM)) {
724                         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
725                 }
726                 else
727                         where_is_pose(scene, ob);
728         }
729         else {
730                 for(base= FIRSTBASE; base; base= base->next) {
731                         Object *ob= base->object;
732
733                         /* this flag is from depgraph, was stored in initialize phase, handled in drawview.c */
734                         if(base->flag & BA_HAS_RECALC_OB)
735                                 ob->recalc |= OB_RECALC_OB;
736                         if(base->flag & BA_HAS_RECALC_DATA)
737                                 ob->recalc |= OB_RECALC_DATA;
738
739                         /* if object/base is selected */
740                         if ((base->flag & SELECT) || (ob->flag & SELECT)) {
741                                 /* if animtimer is running, and the object already has animation data,
742                                  * check if the auto-record feature means that we should record 'samples'
743                                  * (i.e. uneditable animation values)
744                                  */
745                                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
746                                 // TODO: maybe the ob->adt check isn't really needed? makes it too difficult to use...
747                                 if (/*(ob->adt) && */(t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
748                                         autokeyframe_ob_cb_func(t->scene, (View3D *)t->view, ob, t->mode);
749                                 }
750                         }
751
752                         /* proxy exception */
753                         if(ob->proxy)
754                                 ob->proxy->recalc |= ob->recalc;
755                         if(ob->proxy_group)
756                                 group_tag_recalc(ob->proxy_group->dup_group);
757                 }
758         }
759
760         /* update shaded drawmode while transform */
761         if(t->spacetype==SPACE_VIEW3D && ((View3D*)t->view)->drawtype == OB_SHADED)
762                 reshadeall_displist(t->scene);
763 }
764
765 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options)
766 {
767         float v1[3], v2[3], v3[3];
768         char col[3], col2[3];
769
770         if (t->spacetype == SPACE_VIEW3D)
771         {
772                 View3D *v3d = t->view;
773
774                 glPushMatrix();
775
776                 //if(t->obedit) glLoadMatrixf(t->obedit->obmat);        // sets opengl viewing
777
778
779                 VecCopyf(v3, dir);
780                 VecMulf(v3, v3d->far);
781
782                 VecSubf(v2, center, v3);
783                 VecAddf(v1, center, v3);
784
785                 if (options & DRAWLIGHT) {
786                         col[0] = col[1] = col[2] = 220;
787                 }
788                 else {
789                         UI_GetThemeColor3ubv(TH_GRID, col);
790                 }
791                 UI_make_axis_color(col, col2, axis);
792                 glColor3ubv((GLubyte *)col2);
793
794                 setlinestyle(0);
795                 glBegin(GL_LINE_STRIP);
796                         glVertex3fv(v1);
797                         glVertex3fv(v2);
798                 glEnd();
799
800                 glPopMatrix();
801         }
802 }
803
804 void resetTransRestrictions(TransInfo *t)
805 {
806         t->flag &= ~T_ALL_RESTRICTIONS;
807 }
808
809 int initTransInfo (bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
810 {
811         Scene *sce = CTX_data_scene(C);
812         ToolSettings *ts = CTX_data_tool_settings(C);
813         ARegion *ar = CTX_wm_region(C);
814         ScrArea *sa = CTX_wm_area(C);
815         Object *obedit = CTX_data_edit_object(C);
816
817         /* moving: is shown in drawobject() (transform color) */
818 //  TRANSFORM_FIX_ME
819 //      if(obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
820 //      else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
821 //      else G.moving= G_TRANSFORM_OBJ;
822
823         t->scene = sce;
824         t->sa = sa;
825         t->ar = ar;
826         t->obedit = obedit;
827         t->settings = ts;
828
829         t->data = NULL;
830         t->ext = NULL;
831
832         t->helpline = HLP_NONE;
833
834         t->flag = 0;
835
836         t->redraw = 1; /* redraw first time */
837
838         if (event)
839         {
840                 t->imval[0] = event->x - t->ar->winrct.xmin;
841                 t->imval[1] = event->y - t->ar->winrct.ymin;
842
843                 t->event_type = event->type;
844         }
845         else
846         {
847                 t->imval[0] = 0;
848                 t->imval[1] = 0;
849         }
850
851         t->con.imval[0] = t->imval[0];
852         t->con.imval[1] = t->imval[1];
853
854         t->mval[0] = t->imval[0];
855         t->mval[1] = t->imval[1];
856
857         t->transform            = NULL;
858         t->handleEvent          = NULL;
859
860         t->total                        = 0;
861
862         t->val = 0.0f;
863
864         t->vec[0]                       =
865                 t->vec[1]               =
866                 t->vec[2]               = 0.0f;
867
868         t->center[0]            =
869                 t->center[1]    =
870                 t->center[2]    = 0.0f;
871
872         Mat3One(t->mat);
873
874         t->spacetype = sa->spacetype;
875         if(t->spacetype == SPACE_VIEW3D)
876         {
877                 View3D *v3d = sa->spacedata.first;
878
879                 t->view = v3d;
880                 t->animtimer= CTX_wm_screen(C)->animtimer;
881
882                 if(v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
883                 t->around = v3d->around;
884
885                 if (op && RNA_struct_find_property(op->ptr, "constraint_axis") && RNA_property_is_set(op->ptr, "constraint_orientation"))
886                 {
887                         t->current_orientation = RNA_enum_get(op->ptr, "constraint_orientation");
888
889                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C) - 1)
890                         {
891                                 t->current_orientation = V3D_MANIP_GLOBAL;
892                         }
893                 }
894                 else
895                 {
896                         t->current_orientation = v3d->twmode;
897                 }
898         }
899         else if(t->spacetype==SPACE_IMAGE || t->spacetype==SPACE_NODE)
900         {
901                 SpaceImage *sima = sa->spacedata.first;
902                 // XXX for now, get View2D  from the active region
903                 t->view = &ar->v2d;
904                 t->around = sima->around;
905         }
906         else
907         {
908                 // XXX for now, get View2D  from the active region
909                 t->view = &ar->v2d;
910
911                 t->around = V3D_CENTER;
912         }
913
914         if (op && RNA_struct_find_property(op->ptr, "mirror") && RNA_property_is_set(op->ptr, "mirror"))
915         {
916                 if (RNA_boolean_get(op->ptr, "mirror"))
917                 {
918                         t->flag |= T_MIRROR;
919                 }
920         }
921         // Need stuff to take it from edit mesh or whatnot here
922         else
923         {
924                 if (t->obedit && t->obedit->type == OB_MESH && ts->editbutflag & B_MESH_X_MIRROR)
925                 {
926                         t->flag |= T_MIRROR;
927                 }
928         }
929
930         /* setting PET flag */
931         if (op && RNA_struct_find_property(op->ptr, "proportional") && RNA_property_is_set(op->ptr, "proportional"))
932         {
933                 switch(RNA_enum_get(op->ptr, "proportional"))
934                 {
935                 case 2: /* XXX connected constant */
936                         t->flag |= T_PROP_CONNECTED;
937                 case 1: /* XXX prop on constant */
938                         t->flag |= T_PROP_EDIT;
939                         break;
940                 }
941         }
942         else
943         {
944                 if ((t->options & CTX_NO_PET) == 0 && (ts->proportional)) {
945                         t->flag |= T_PROP_EDIT;
946
947                         if(ts->proportional == 2)
948                                 t->flag |= T_PROP_CONNECTED;    // yes i know, has to become define
949                 }
950         }
951
952         if (op && RNA_struct_find_property(op->ptr, "proportional_size") && RNA_property_is_set(op->ptr, "proportional_size"))
953         {
954                 t->prop_size = RNA_float_get(op->ptr, "proportional_size");
955         }
956         else
957         {
958                 t->prop_size = ts->proportional_size;
959         }
960
961         if (op && RNA_struct_find_property(op->ptr, "proportional_editing_falloff") && RNA_property_is_set(op->ptr, "proportional_editing_falloff"))
962         {
963                 t->prop_mode = RNA_enum_get(op->ptr, "proportional_editing_falloff");
964         }
965         else
966         {
967                 t->prop_mode = ts->prop_mode;
968         }
969
970         /* TRANSFORM_FIX_ME rna restrictions */
971         if (t->prop_size <= 0)
972         {
973                 t->prop_size = 1.0f;
974         }
975
976         setTransformViewMatrices(t);
977         initNumInput(&t->num);
978         initNDofInput(&t->ndof);
979
980         return 1;
981 }
982
983 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
984 void postTrans (TransInfo *t)
985 {
986         TransData *td;
987
988         if (t->draw_handle)
989         {
990                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle);
991         }
992
993         /* postTrans can be called when nothing is selected, so data is NULL already */
994         if (t->data) {
995                 int a;
996
997                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
998                 for(a=0, td= t->data; a<t->total; a++, td++) {
999                         if(td->tdi) MEM_freeN(td->tdi);
1000                         if (td->flag & TD_BEZTRIPLE) MEM_freeN(td->hdata);
1001                 }
1002                 MEM_freeN(t->data);
1003         }
1004
1005         if (t->ext) MEM_freeN(t->ext);
1006         if (t->data2d) {
1007                 MEM_freeN(t->data2d);
1008                 t->data2d= NULL;
1009         }
1010
1011         if(t->spacetype==SPACE_IMAGE) {
1012                 SpaceImage *sima= t->sa->spacedata.first;
1013                 if(sima->flag & SI_LIVE_UNWRAP)
1014                         ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1015         }
1016         else if(ELEM(t->spacetype, SPACE_ACTION, SPACE_NLA)) {
1017                 if (t->customData)
1018                         MEM_freeN(t->customData);
1019         }
1020 }
1021
1022 void applyTransObjects(TransInfo *t)
1023 {
1024         TransData *td;
1025
1026         for (td = t->data; td < t->data + t->total; td++) {
1027                 VECCOPY(td->iloc, td->loc);
1028                 if (td->ext->rot) {
1029                         VECCOPY(td->ext->irot, td->ext->rot);
1030                 }
1031                 if (td->ext->size) {
1032                         VECCOPY(td->ext->isize, td->ext->size);
1033                 }
1034         }
1035         recalcData(t);
1036 }
1037
1038 /* helper for below */
1039 static void restore_ipokey(float *poin, float *old)
1040 {
1041         if(poin) {
1042                 poin[0]= old[0];
1043                 poin[-3]= old[3];
1044                 poin[3]= old[6];
1045         }
1046 }
1047
1048 static void restoreElement(TransData *td) {
1049         /* TransData for crease has no loc */
1050         if (td->loc) {
1051                 VECCOPY(td->loc, td->iloc);
1052         }
1053         if (td->val) {
1054                 *td->val = td->ival;
1055         }
1056         if (td->ext && (td->flag&TD_NO_EXT)==0) {
1057                 if (td->ext->rot) {
1058                         VECCOPY(td->ext->rot, td->ext->irot);
1059                 }
1060                 if (td->ext->size) {
1061                         VECCOPY(td->ext->size, td->ext->isize);
1062                 }
1063                 if(td->flag & TD_USEQUAT) {
1064                         if (td->ext->quat) {
1065                                 QUATCOPY(td->ext->quat, td->ext->iquat);
1066                         }
1067                 }
1068         }
1069
1070         if (td->flag & TD_BEZTRIPLE) {
1071                 *(td->hdata->h1) = td->hdata->ih1;
1072                 *(td->hdata->h2) = td->hdata->ih2;
1073         }
1074
1075         if(td->tdi) {
1076                 TransDataIpokey *tdi= td->tdi;
1077
1078                 restore_ipokey(tdi->locx, tdi->oldloc);
1079                 restore_ipokey(tdi->locy, tdi->oldloc+1);
1080                 restore_ipokey(tdi->locz, tdi->oldloc+2);
1081
1082                 restore_ipokey(tdi->rotx, tdi->oldrot);
1083                 restore_ipokey(tdi->roty, tdi->oldrot+1);
1084                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
1085
1086                 restore_ipokey(tdi->sizex, tdi->oldsize);
1087                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
1088                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
1089         }
1090 }
1091
1092 void restoreTransObjects(TransInfo *t)
1093 {
1094         TransData *td;
1095
1096         for (td = t->data; td < t->data + t->total; td++) {
1097                 restoreElement(td);
1098         }
1099
1100         Mat3One(t->mat);
1101
1102         recalcData(t);
1103 }
1104
1105 void calculateCenter2D(TransInfo *t)
1106 {
1107         if (t->flag & (T_EDIT|T_POSE)) {
1108                 Object *ob= t->obedit?t->obedit:t->poseobj;
1109                 float vec[3];
1110
1111                 VECCOPY(vec, t->center);
1112                 Mat4MulVecfl(ob->obmat, vec);
1113                 projectIntView(t, vec, t->center2d);
1114         }
1115         else {
1116                 projectIntView(t, t->center, t->center2d);
1117         }
1118 }
1119
1120 void calculateCenterCursor(TransInfo *t)
1121 {
1122         float *cursor;
1123
1124         cursor = give_cursor(t->scene, t->view);
1125         VECCOPY(t->center, cursor);
1126
1127         /* If edit or pose mode, move cursor in local space */
1128         if (t->flag & (T_EDIT|T_POSE)) {
1129                 Object *ob = t->obedit?t->obedit:t->poseobj;
1130                 float mat[3][3], imat[3][3];
1131
1132                 VecSubf(t->center, t->center, ob->obmat[3]);
1133                 Mat3CpyMat4(mat, ob->obmat);
1134                 Mat3Inv(imat, mat);
1135                 Mat3MulVecfl(imat, t->center);
1136         }
1137
1138         calculateCenter2D(t);
1139 }
1140
1141 void calculateCenterCursor2D(TransInfo *t)
1142 {
1143         View2D *v2d= t->view;
1144         float aspx=1.0, aspy=1.0;
1145
1146         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
1147                 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
1148
1149         if (v2d) {
1150                 t->center[0] = v2d->cursor[0] * aspx;
1151                 t->center[1] = v2d->cursor[1] * aspy;
1152         }
1153
1154         calculateCenter2D(t);
1155 }
1156
1157 void calculateCenterMedian(TransInfo *t)
1158 {
1159         float partial[3] = {0.0f, 0.0f, 0.0f};
1160         int total = 0;
1161         int i;
1162
1163         for(i = 0; i < t->total; i++) {
1164                 if (t->data[i].flag & TD_SELECTED) {
1165                         if (!(t->data[i].flag & TD_NOCENTER))
1166                         {
1167                                 VecAddf(partial, partial, t->data[i].center);
1168                                 total++;
1169                         }
1170                 }
1171                 else {
1172                         /*
1173                            All the selected elements are at the head of the array
1174                            which means we can stop when it finds unselected data
1175                         */
1176                         break;
1177                 }
1178         }
1179         if(i)
1180                 VecMulf(partial, 1.0f / total);
1181         VECCOPY(t->center, partial);
1182
1183         calculateCenter2D(t);
1184 }
1185
1186 void calculateCenterBound(TransInfo *t)
1187 {
1188         float max[3];
1189         float min[3];
1190         int i;
1191         for(i = 0; i < t->total; i++) {
1192                 if (i) {
1193                         if (t->data[i].flag & TD_SELECTED) {
1194                                 if (!(t->data[i].flag & TD_NOCENTER))
1195                                         MinMax3(min, max, t->data[i].center);
1196                         }
1197                         else {
1198                                 /*
1199                                    All the selected elements are at the head of the array
1200                                    which means we can stop when it finds unselected data
1201                                 */
1202                                 break;
1203                         }
1204                 }
1205                 else {
1206                         VECCOPY(max, t->data[i].center);
1207                         VECCOPY(min, t->data[i].center);
1208                 }
1209         }
1210         VecAddf(t->center, min, max);
1211         VecMulf(t->center, 0.5);
1212
1213         calculateCenter2D(t);
1214 }
1215
1216 void calculateCenter(TransInfo *t)
1217 {
1218         switch(t->around) {
1219         case V3D_CENTER:
1220                 calculateCenterBound(t);
1221                 break;
1222         case V3D_CENTROID:
1223                 calculateCenterMedian(t);
1224                 break;
1225         case V3D_CURSOR:
1226                 if(t->spacetype==SPACE_IMAGE)
1227                         calculateCenterCursor2D(t);
1228                 else
1229                         calculateCenterCursor(t);
1230                 break;
1231         case V3D_LOCAL:
1232                 /* Individual element center uses median center for helpline and such */
1233                 calculateCenterMedian(t);
1234                 break;
1235         case V3D_ACTIVE:
1236                 {
1237                 /* set median, and if if if... do object center */
1238 #if 0 // TRANSFORM_FIX_ME
1239                 EditSelection ese;
1240                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1241
1242                 if (t->obedit && t->obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
1243                         EM_editselection_center(t->center, &ese);
1244                         calculateCenter2D(t);
1245                         break;
1246                 } /* END EDIT MODE ACTIVE ELEMENT */
1247 #endif
1248
1249                 calculateCenterMedian(t);
1250                 if((t->flag & (T_EDIT|T_POSE))==0)
1251                 {
1252                         Scene *scene = t->scene;
1253                         Object *ob= OBACT;
1254                         if(ob)
1255                         {
1256                                 VECCOPY(t->center, ob->obmat[3]);
1257                                 projectIntView(t, t->center, t->center2d);
1258                         }
1259                 }
1260
1261                 }
1262         }
1263
1264         /* setting constraint center */
1265         VECCOPY(t->con.center, t->center);
1266         if(t->flag & (T_EDIT|T_POSE))
1267         {
1268                 Object *ob= t->obedit?t->obedit:t->poseobj;
1269                 Mat4MulVecfl(ob->obmat, t->con.center);
1270         }
1271
1272         /* voor panning from cameraview */
1273         if(t->flag & T_OBJECT)
1274         {
1275                 if(t->spacetype==SPACE_VIEW3D)
1276                 {
1277                         View3D *v3d = t->view;
1278                         Scene *scene = t->scene;
1279                         RegionView3D *rv3d = t->ar->regiondata;
1280
1281                         if(v3d->camera == OBACT && rv3d->persp==V3D_CAMOB)
1282                         {
1283                                 float axis[3];
1284                                 /* persinv is nasty, use viewinv instead, always right */
1285                                 VECCOPY(axis, t->viewinv[2]);
1286                                 Normalize(axis);
1287
1288                                 /* 6.0 = 6 grid units */
1289                                 axis[0]= t->center[0]- 6.0f*axis[0];
1290                                 axis[1]= t->center[1]- 6.0f*axis[1];
1291                                 axis[2]= t->center[2]- 6.0f*axis[2];
1292
1293                                 projectIntView(t, axis, t->center2d);
1294
1295                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1296                                 if(t->mode==TFM_TRANSLATION)
1297                                 {
1298                                         VECCOPY(t->center, axis);
1299                                         VECCOPY(t->con.center, t->center);
1300                                 }
1301                         }
1302                 }
1303         }
1304
1305         if(t->spacetype==SPACE_VIEW3D)
1306         {
1307                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1308                 if(t->flag & (T_EDIT|T_POSE)) {
1309                         Object *ob= t->obedit?t->obedit:t->poseobj;
1310                         float vec[3];
1311
1312                         VECCOPY(vec, t->center);
1313                         Mat4MulVecfl(ob->obmat, vec);
1314                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1315                 }
1316                 else {
1317                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1318                 }
1319         }
1320 }
1321
1322 void calculatePropRatio(TransInfo *t)
1323 {
1324         TransData *td = t->data;
1325         int i;
1326         float dist;
1327         short connected = t->flag & T_PROP_CONNECTED;
1328
1329         if (t->flag & T_PROP_EDIT) {
1330                 for(i = 0 ; i < t->total; i++, td++) {
1331                         if (td->flag & TD_SELECTED) {
1332                                 td->factor = 1.0f;
1333                         }
1334                         else if ((connected &&
1335                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size))
1336                                 ||
1337                                         (connected == 0 &&
1338                                                 td->rdist > t->prop_size)) {
1339                                 /*
1340                                    The elements are sorted according to their dist member in the array,
1341                                    that means we can stop when it finds one element outside of the propsize.
1342                                 */
1343                                 td->flag |= TD_NOACTION;
1344                                 td->factor = 0.0f;
1345                                 restoreElement(td);
1346                         }
1347                         else {
1348                                 /* Use rdist for falloff calculations, it is the real distance */
1349                                 td->flag &= ~TD_NOACTION;
1350                                 dist= (t->prop_size-td->rdist)/t->prop_size;
1351
1352                                 /*
1353                                  * Clamp to positive numbers.
1354                                  * Certain corner cases with connectivity and individual centers
1355                                  * can give values of rdist larger than propsize.
1356                                  */
1357                                 if (dist < 0.0f)
1358                                         dist = 0.0f;
1359
1360                                 switch(t->prop_mode) {
1361                                 case PROP_SHARP:
1362                                         td->factor= dist*dist;
1363                                         break;
1364                                 case PROP_SMOOTH:
1365                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1366                                         break;
1367                                 case PROP_ROOT:
1368                                         td->factor = (float)sqrt(dist);
1369                                         break;
1370                                 case PROP_LIN:
1371                                         td->factor = dist;
1372                                         break;
1373                                 case PROP_CONST:
1374                                         td->factor = 1.0f;
1375                                         break;
1376                                 case PROP_SPHERE:
1377                                         td->factor = (float)sqrt(2*dist - dist * dist);
1378                                         break;
1379                                 case PROP_RANDOM:
1380                                         BLI_srand( BLI_rand() ); /* random seed */
1381                                         td->factor = BLI_frand()*dist;
1382                                         break;
1383                                 default:
1384                                         td->factor = 1;
1385                                 }
1386                         }
1387                 }
1388                 switch(t->prop_mode) {
1389                 case PROP_SHARP:
1390                         strcpy(t->proptext, "(Sharp)");
1391                         break;
1392                 case PROP_SMOOTH:
1393                         strcpy(t->proptext, "(Smooth)");
1394                         break;
1395                 case PROP_ROOT:
1396                         strcpy(t->proptext, "(Root)");
1397                         break;
1398                 case PROP_LIN:
1399                         strcpy(t->proptext, "(Linear)");
1400                         break;
1401                 case PROP_CONST:
1402                         strcpy(t->proptext, "(Constant)");
1403                         break;
1404                 case PROP_SPHERE:
1405                         strcpy(t->proptext, "(Sphere)");
1406                         break;
1407                 case PROP_RANDOM:
1408                         strcpy(t->proptext, "(Random)");
1409                         break;
1410                 default:
1411                         strcpy(t->proptext, "");
1412                 }
1413         }
1414         else {
1415                 for(i = 0 ; i < t->total; i++, td++) {
1416                         td->factor = 1.0;
1417                 }
1418                 strcpy(t->proptext, "");
1419         }
1420 }
1421
1422 float get_drawsize(ARegion *ar, float *co)
1423 {
1424         RegionView3D *rv3d= ar->regiondata;
1425         float size, vec[3], len1, len2;
1426
1427         /* size calculus, depending ortho/persp settings, like initgrabz() */
1428         size= rv3d->persmat[0][3]*co[0]+ rv3d->persmat[1][3]*co[1]+ rv3d->persmat[2][3]*co[2]+ rv3d->persmat[3][3];
1429
1430         VECCOPY(vec, rv3d->persinv[0]);
1431         len1= Normalize(vec);
1432         VECCOPY(vec, rv3d->persinv[1]);
1433         len2= Normalize(vec);
1434
1435         size*= 0.01f*(len1>len2?len1:len2);
1436
1437         /* correct for window size to make widgets appear fixed size */
1438         if(ar->winx > ar->winy) size*= 1000.0f/(float)ar->winx;
1439         else size*= 1000.0f/(float)ar->winy;
1440
1441         return size;
1442 }