72937fb10b69a3a35c6ba6bef62910ff0feda102
[blender.git] / source / blender / src / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
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_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51 #include "DNA_view3d_types.h"
52
53 #include "BIF_screen.h"
54 #include "BIF_resources.h"
55 #include "BIF_mywindow.h"
56 #include "BIF_gl.h"
57 #include "BIF_editaction.h"
58 #include "BIF_editarmature.h"
59 #include "BIF_editmesh.h"
60 #include "BIF_editnla.h"
61 #include "BIF_editsima.h"
62 #include "BIF_meshtools.h"
63 #include "BIF_retopo.h"
64
65 #include "BSE_editipo.h"
66 #include "BSE_editipo_types.h"
67
68 #ifdef WITH_VERSE
69 #include "BIF_verse.h"
70 #endif
71
72 #include "BKE_action.h"
73 #include "BKE_anim.h"
74 #include "BKE_armature.h"
75 #include "BKE_curve.h"
76 #include "BKE_depsgraph.h"
77 #include "BKE_displist.h"
78 #include "BKE_depsgraph.h"
79 #include "BKE_global.h"
80 #include "BKE_group.h"
81 #include "BKE_ipo.h"
82 #include "BKE_lattice.h"
83 #include "BKE_key.h"
84 #include "BKE_mesh.h"
85 #include "BKE_modifier.h"
86 #include "BKE_object.h"
87 #include "BKE_utildefines.h"
88
89 #ifdef WITH_VERSE
90 #include "BKE_verse.h"
91 #endif
92
93 #include "BSE_view.h"
94 #include "BSE_editaction_types.h"
95 #include "BDR_unwrapper.h"
96
97 #include "BLI_arithb.h"
98 #include "BLI_blenlib.h"
99 #include "BLI_editVert.h"
100 #include "BLI_rand.h"
101
102 #include "blendef.h"
103
104 #include "mydevice.h"
105
106 #include "transform.h"
107
108 extern ListBase editNurb;
109 extern ListBase editelems;
110
111 extern TransInfo Trans; /* From transform.c */
112
113 /* ************************** Functions *************************** */
114
115
116 void getViewVector(float coord[3], float vec[3])
117 {
118         TransInfo *t = BIF_GetTransInfo();
119
120         if (t->persp)
121         {
122                 float p1[4], p2[4];
123
124                 VECCOPY(p1, coord);
125                 p1[3] = 1.0f;
126                 VECCOPY(p2, p1);
127                 p2[3] = 1.0f;
128                 Mat4MulVec4fl(t->viewmat, p2);
129
130                 p2[0] = 2.0f * p2[0];
131                 p2[1] = 2.0f * p2[1];
132                 p2[2] = 2.0f * p2[2];
133
134                 Mat4MulVec4fl(t->viewinv, p2);
135
136                 VecSubf(vec, p1, p2);
137         }
138         else {
139                 VECCOPY(vec, t->viewinv[2]);
140         }
141         Normalize(vec);
142 }
143
144 /* ************************** GENERICS **************************** */
145
146 static void clipMirrorModifier(TransInfo *t, Object *ob)
147 {
148         ModifierData *md= ob->modifiers.first;
149         float tolerance[3] = {0.0f, 0.0f, 0.0f};
150         int axis = 0;
151
152         for (; md; md=md->next) {
153                 if (md->type==eModifierType_Mirror) {
154                         MirrorModifierData *mmd = (MirrorModifierData*) md;     
155                 
156                         if(mmd->flag & MOD_MIR_CLIPPING) {
157                                 axis = 0;
158                                 if(mmd->flag & MOD_MIR_AXIS_X) {
159                                         axis |= 1;
160                                         tolerance[0] = mmd->tolerance;
161                                 }
162                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
163                                         axis |= 2;
164                                         tolerance[1] = mmd->tolerance;
165                                 }
166                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
167                                         axis |= 4;
168                                         tolerance[2] = mmd->tolerance;
169                                 }
170                                 if (axis) {
171                                         float mtx[4][4], imtx[4][4];
172                                         int i;
173                                         TransData *td = t->data;
174                 
175                                         if (mmd->mirror_ob) {
176                                                 float obinv[4][4];
177
178                                                 Mat4Invert(obinv, mmd->mirror_ob->obmat);
179                                                 Mat4MulMat4(mtx, ob->obmat, obinv);
180                                                 Mat4Invert(imtx, mtx);
181                                         }
182
183                                         for(i = 0 ; i < t->total; i++, td++) {
184                                                 int clip;
185                                                 float loc[3], iloc[3];
186
187                                                 if (td->flag & TD_NOACTION)
188                                                         break;
189                                                 if (td->loc==NULL)
190                                                         break;
191                         
192                                                 VecCopyf(loc,  td->loc);
193                                                 VecCopyf(iloc, td->iloc);
194
195                                                 if (mmd->mirror_ob) {
196                                                         VecMat4MulVecfl(loc, mtx, loc);
197                                                         VecMat4MulVecfl(iloc, mtx, iloc);
198                                                 }
199
200                                                 clip = 0;
201                                                 if(axis & 1) {
202                                                         if(fabs(iloc[0])<=tolerance[0] || 
203                                                            loc[0]*iloc[0]<0.0f) {
204                                                                 loc[0]= 0.0f;
205                                                                 clip = 1;
206                                                         }
207                                                 }
208                         
209                                                 if(axis & 2) {
210                                                         if(fabs(iloc[1])<=tolerance[1] || 
211                                                            loc[1]*iloc[1]<0.0f) {
212                                                                 loc[1]= 0.0f;
213                                                                 clip = 1;
214                                                         }
215                                                 }
216                                                 if(axis & 4) {
217                                                         if(fabs(iloc[2])<=tolerance[2] || 
218                                                            loc[2]*iloc[2]<0.0f) {
219                                                                 loc[2]= 0.0f;
220                                                                 clip = 1;
221                                                         }
222                                                 }
223                                                 if (clip) {
224                                                         if (mmd->mirror_ob) {
225                                                                 VecMat4MulVecfl(loc, imtx, loc);
226                                                         }
227                                                         VecCopyf(td->loc, loc);
228                                                 }
229                                         }
230                                 }
231
232                         }
233                 }
234         }
235 }
236
237 /* assumes G.obedit set to mesh object */
238 static void editmesh_apply_to_mirror(TransInfo *t)
239 {
240         TransData *td = t->data;
241         EditVert *eve;
242         int i;
243         
244         for(i = 0 ; i < t->total; i++, td++) {
245                 if (td->flag & TD_NOACTION)
246                         break;
247                 if (td->loc==NULL)
248                         break;
249                 
250                 eve= td->tdmir;
251                 if(eve) {
252                         eve->co[0]= -td->loc[0];
253                         eve->co[1]= td->loc[1];
254                         eve->co[2]= td->loc[2];
255                 }               
256         }               
257 }
258
259 /* called for updating while transform acts, once per redraw */
260 void recalcData(TransInfo *t)
261 {
262         Base *base;
263 #ifdef WITH_VERSE
264         struct TransData *td;
265 #endif
266         
267         if (t->spacetype == SPACE_ACTION) {
268                 Object *ob= OBACT;
269                 void *data;
270                 short context;
271                 
272                 /* determine what type of data we are operating on */
273                 data = get_action_context(&context);
274                 if (data == NULL) return;
275                 
276                 if (G.saction->lock) {
277                         if (context == ACTCONT_ACTION) {
278                                 if(ob) {
279                                         ob->ctime= -1234567.0f;
280                                         if(ob->pose || ob_get_key(ob))
281                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC);
282                                         else
283                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
284                                 }
285                         }
286                         else if (context == ACTCONT_SHAPEKEY) {
287                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
288                         }
289                 }
290         }       
291         else if (t->spacetype == SPACE_NLA) {
292                 if (G.snla->lock) {
293                         for (base=G.scene->base.first; base; base=base->next) {
294                                 if (base->flag & BA_HAS_RECALC_OB)
295                                         base->object->recalc |= OB_RECALC_OB;
296                                 if (base->flag & BA_HAS_RECALC_DATA)
297                                         base->object->recalc |= OB_RECALC_DATA;
298                                 
299                                 if (base->object->recalc) 
300                                         base->object->ctime= -1234567.0f;       // eveil! 
301                         }
302                         
303                         DAG_scene_flush_update(G.scene, screen_view3d_layers());
304                 }
305         }
306         else if (t->spacetype == SPACE_IPO) {
307                 EditIpo *ei;
308                 int dosort = 0;
309                 int a;
310                 
311                 /* do the flush first */
312                 flushTransIpoData(t);
313                 
314                 /* now test if there is a need to re-sort */
315                 ei= G.sipo->editipo;
316                 for (a=0; a<G.sipo->totipo; a++, ei++) {
317                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
318                                 
319                                 /* watch it: if the time is wrong: do not correct handles */
320                                 if (test_time_ipocurve(ei->icu)) dosort++;
321                                 else testhandles_ipocurve(ei->icu);
322                         }
323                 }
324                 
325                 /* do resort and other updates? */
326                 if (dosort) remake_ipo_transdata(t);
327                 if (G.sipo->showkey) update_ipokey_val();
328                 
329                 calc_ipo(G.sipo->ipo, (float)CFRA);
330                 
331                 /* update realtime - not working? */
332                 if (G.sipo->lock) {
333                         if (G.sipo->blocktype==ID_MA || G.sipo->blocktype==ID_TE) {
334                                 do_ipo(G.sipo->ipo);
335                         }
336                         else if(G.sipo->blocktype==ID_CA) {
337                                 do_ipo(G.sipo->ipo);
338                         }
339                         else if(G.sipo->blocktype==ID_KE) {
340                                 Object *ob= OBACT;
341                                 if(ob) {
342                                         ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
343                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
344                                 }
345                         }
346                         else if(G.sipo->blocktype==ID_PO) {
347                                 Object *ob= OBACT;
348                                 if(ob && ob->pose) {
349                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
350                                 }
351                         }
352                         else if(G.sipo->blocktype==ID_OB) {
353                                 Base *base= FIRSTBASE;
354                                 
355                                 while(base) {
356                                         if(base->object->ipo==G.sipo->ipo) {
357                                                 do_ob_ipo(base->object);
358                                                 base->object->recalc |= OB_RECALC_OB;
359                                         }
360                                         base= base->next;
361                                 }
362                                 DAG_scene_flush_update(G.scene, screen_view3d_layers());
363                         }
364                 }
365         }
366         else if (G.obedit) {
367                 if (G.obedit->type == OB_MESH) {
368                         if(t->spacetype==SPACE_IMAGE) {
369                                 flushTransUVs(t);
370                                 if (G.sima->flag & SI_LIVE_UNWRAP)
371                                         unwrap_lscm_live_re_solve();
372                         } else {
373                         
374                                 retopo_do_all();
375         
376                                 /* mirror modifier clipping? */
377                                 if(t->state != TRANS_CANCEL)
378                                         clipMirrorModifier(t, G.obedit);
379                                 
380                                 if(G.scene->toolsettings->editbutflag & B_MESH_X_MIRROR)
381                                         editmesh_apply_to_mirror(t);
382                                 
383                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
384                                 
385                                 recalc_editnormals();
386                         }
387                 }
388                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
389                         Nurb *nu= editNurb.first;
390                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
391                         
392                         while(nu) {
393                                 test2DNurb(nu);
394                                 testhandlesNurb(nu); /* test for bezier too */
395                                 nu= nu->next;
396                         }
397
398                         retopo_do_all();
399                 }
400                 else if(G.obedit->type==OB_ARMATURE){   /* no recalc flag, does pose */
401                         bArmature *arm= G.obedit->data;
402                         EditBone *ebo;
403                         
404                         /* Ensure all bones are correctly adjusted */
405                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
406                                 
407                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
408                                         /* If this bone has a parent tip that has been moved */
409                                         if (ebo->parent->flag & BONE_TIPSEL){
410                                                 VECCOPY (ebo->head, ebo->parent->tail);
411                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
412                                         }
413                                         /* If this bone has a parent tip that has NOT been moved */
414                                         else{
415                                                 VECCOPY (ebo->parent->tail, ebo->head);
416                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
417                                         }
418                                 }
419                                 
420                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
421                                 ebo->length= VecLenf(ebo->head, ebo->tail);
422                                 if(ebo->oldlength==0.0f) {
423                                         ebo->rad_head= 0.25f*ebo->length;
424                                         ebo->rad_tail= 0.10f*ebo->length;
425                                         ebo->dist= 0.25f*ebo->length;
426                                         if(ebo->parent) {
427                                                 if(ebo->rad_head > ebo->parent->rad_tail)
428                                                         ebo->rad_head= ebo->parent->rad_tail;
429                                         }
430                                 }
431                                 else if(t->mode!=TFM_BONE_ENVELOPE) {
432                                         /* if bones change length, lets do that for the deform distance as well */
433                                         ebo->dist*= ebo->length/ebo->oldlength;
434                                         ebo->rad_head*= ebo->length/ebo->oldlength;
435                                         ebo->rad_tail*= ebo->length/ebo->oldlength;
436                                         ebo->oldlength= ebo->length;
437                                 }
438                         }
439                         if(arm->flag & ARM_MIRROR_EDIT) 
440                                 transform_armature_mirror_update();
441                         
442                 }
443                 else if(G.obedit->type==OB_LATTICE) {
444                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
445                         
446                         if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
447                 }
448                 else {
449                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
450                 }
451         }
452         else if( (t->flag & T_POSE) && t->poseobj) {
453                 Object *ob= t->poseobj;
454                 bArmature *arm= ob->data;
455                 
456                 /* old optimize trick... this enforces to bypass the depgraph */
457                 if (!(arm->flag & ARM_DELAYDEFORM)) {
458                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
459                         
460                         /* bah, softbody exception... recalcdata doesnt reset */
461                         for(base= FIRSTBASE; base; base= base->next) {
462                                 if(base->object->recalc & OB_RECALC_DATA)
463                                         if(modifiers_isSoftbodyEnabled(base->object)) {
464                                                 base->object->softflag |= OB_SB_REDO;
465                                 }
466                         }
467                 }
468                 else
469                         where_is_pose(ob);
470         }
471         else {
472                 for(base= FIRSTBASE; base; base= base->next) {
473                         Object *ob= base->object;
474                         
475                         /* this flag is from depgraph, was stored in nitialize phase, handled in drawview.c */
476                         if(base->flag & BA_HAS_RECALC_OB)
477                                 ob->recalc |= OB_RECALC_OB;
478                         if(base->flag & BA_HAS_RECALC_DATA)
479                                 ob->recalc |= OB_RECALC_DATA;
480                         
481                         /* thanks to ob->ctime usage, ipos are not called in where_is_object,
482                            unless we edit ipokeys */
483                         if(base->flag & BA_DO_IPO) {
484                                 if(ob->ipo) {
485                                         IpoCurve *icu;
486                                         
487                                         ob->ctime= -1234567.0;
488                                         
489                                         icu= ob->ipo->curve.first;
490                                         while(icu) {
491                                                 calchandles_ipocurve(icu);
492                                                 icu= icu->next;
493                                         }
494                                 }                               
495                         }
496                         
497                         /* softbody exception */
498                         if(modifiers_isSoftbodyEnabled(ob)) {
499                                 if(ob->recalc & OB_RECALC_DATA)
500                                         ob->softflag |= OB_SB_REDO;
501                         }
502                         
503                         /* proxy exception */
504                         if(ob->proxy)
505                                 ob->proxy->recalc |= ob->recalc;
506                         if(ob->proxy_group)
507                                 group_tag_recalc(ob->proxy_group->dup_group);
508                 } 
509         }
510
511 #ifdef WITH_VERSE
512         for (td = t->data; td < t->data + t->total; td++) {
513                 if(td->flag & TD_VERSE_VERT) {
514                         if(td->verse)
515                                 send_versevert_pos((VerseVert*)td->verse);
516                 }
517                 else if(td->flag & TD_VERSE_OBJECT)
518                         if(td->verse) b_verse_send_transformation((Object*)td->verse);
519         }
520 #endif
521         
522         /* update shaded drawmode while transform */
523         if(t->spacetype==SPACE_VIEW3D && G.vd->drawtype == OB_SHADED)
524                 reshadeall_displist();
525         
526 }
527
528 void drawLine(float *center, float *dir, char axis, short options)
529 {
530         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
531         float v1[3], v2[3], v3[3];
532         char col[3], col2[3];
533         
534         //if(G.obedit) mymultmatrix(G.obedit->obmat);   // sets opengl viewing
535
536         VecCopyf(v3, dir);
537         VecMulf(v3, G.vd->far);
538         
539         VecSubf(v2, center, v3);
540         VecAddf(v1, center, v3);
541
542         if (options & DRAWLIGHT) {
543                 col[0] = col[1] = col[2] = 220;
544         }
545         else {
546                 BIF_GetThemeColor3ubv(TH_GRID, col);
547         }
548         make_axis_color(col, col2, axis);
549         glColor3ubv((GLubyte *)col2);
550
551         setlinestyle(0);
552         glBegin(GL_LINE_STRIP); 
553                 glVertex3fv(v1); 
554                 glVertex3fv(v2); 
555         glEnd();
556         
557         myloadmatrix(G.vd->viewmat);
558 }
559
560 void initTrans (TransInfo *t)
561 {
562         /* moving: is shown in drawobject() (transform color) */
563         if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
564         else G.moving= G_TRANSFORM_OBJ;
565
566         t->data = NULL;
567         t->ext = NULL;
568
569         t->flag = 0;
570         t->num.flag = 0;
571
572
573         /* setting PET flag */
574         if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
575                 t->flag |= T_PROP_EDIT;
576                 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED;       // yes i know, has to become define
577         }
578
579         getmouseco_areawin(t->imval);
580         t->con.imval[0] = t->imval[0];
581         t->con.imval[1] = t->imval[1];
582
583         t->transform            = NULL;
584         t->handleEvent          = NULL;
585         t->customData           = NULL;
586
587         t->total                        =
588                 t->num.idx              =
589                 t->num.idx_max  =
590                 t->num.ctrl[0]  = 
591                 t->num.ctrl[1]  = 
592                 t->num.ctrl[2]  = 0;
593
594         t->val = 0.0f;
595
596         t->num.val[0]           = 
597                 t->num.val[1]   = 
598                 t->num.val[2]   = 0.0f;
599
600         t->vec[0]                       =
601                 t->vec[1]               =
602                 t->vec[2]               = 0.0f;
603         
604         t->center[0]            =
605                 t->center[1]    =
606                 t->center[2]    = 0.0f;
607         
608         Mat3One(t->mat);
609         
610         t->spacetype = curarea->spacetype;
611         if(t->spacetype==SPACE_VIEW3D) {
612                 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
613                 t->around = G.vd->around;
614         } else if(t->spacetype==SPACE_IMAGE) {
615                 t->around = G.v2d->around;
616         }
617         else
618                 t->around = V3D_CENTER;
619
620         setTransformViewMatrices(t);
621 }
622
623 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
624 void postTrans (TransInfo *t) 
625 {
626         TransData *td;
627
628         G.moving = 0; // Set moving flag off (display as usual)
629 #ifdef WITH_VERSE
630
631         for (td = t->data; td < t->data + t->total; td++) {
632                 if(td->flag & TD_VERSE_VERT) {
633                         if(td->verse) send_versevert_pos((VerseVert*)td->verse);
634                 }
635                 else if(td->flag & TD_VERSE_OBJECT) {
636                         if(td->verse) {
637                                 struct VNode *vnode;
638                                 vnode = (VNode*)((Object*)td->verse)->vnode;
639                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
640                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
641                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
642                                 b_verse_send_transformation((Object*)td->verse);
643                         }
644                 }
645         }
646 #endif
647
648         stopConstraint(t);
649         
650         /* postTrans can be called when nothing is selected, so data is NULL already */
651         if (t->data) {
652                 int a;
653
654                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
655                 for(a=0, td= t->data; a<t->total; a++, td++) {
656                         if(td->tdi) MEM_freeN(td->tdi);
657                 }
658                 MEM_freeN(t->data);
659         }
660
661         if (t->ext) MEM_freeN(t->ext);
662         if (t->data2d) {
663                 MEM_freeN(t->data2d);
664                 t->data2d= NULL;
665         }
666         
667         if ((t->flag & T_FREE_CUSTOMDATA) && t->customData != NULL) {
668                 MEM_freeN(t->customData);
669         }
670
671         if(t->spacetype==SPACE_IMAGE) {
672                 if (G.sima->flag & SI_LIVE_UNWRAP)
673                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
674         }
675 }
676
677 void applyTransObjects(TransInfo *t)
678 {
679         TransData *td;
680         
681         for (td = t->data; td < t->data + t->total; td++) {
682                 VECCOPY(td->iloc, td->loc);
683                 if (td->ext->rot) {
684                         VECCOPY(td->ext->irot, td->ext->rot);
685                 }
686                 if (td->ext->size) {
687                         VECCOPY(td->ext->isize, td->ext->size);
688                 }
689         }       
690         recalcData(t);
691
692
693 /* helper for below */
694 static void restore_ipokey(float *poin, float *old)
695 {
696         if(poin) {
697                 poin[0]= old[0];
698                 poin[-3]= old[3];
699                 poin[3]= old[6];
700         }
701 }
702
703 static void restoreElement(TransData *td) {
704         /* TransData for crease has no loc */
705         if (td->loc) {
706                 VECCOPY(td->loc, td->iloc);
707         }
708         if (td->val) {
709                 *td->val = td->ival;
710         }
711         if (td->ext) {
712                 if (td->ext->rot) {
713                         VECCOPY(td->ext->rot, td->ext->irot);
714                 }
715                 if (td->ext->size) {
716                         VECCOPY(td->ext->size, td->ext->isize);
717                 }
718                 if(td->flag & TD_USEQUAT) {
719                         if (td->ext->quat) {
720                                 QUATCOPY(td->ext->quat, td->ext->iquat);
721                         }
722                 }
723         }
724         if(td->tdi) {
725                 TransDataIpokey *tdi= td->tdi;
726                 
727                 restore_ipokey(tdi->locx, tdi->oldloc);
728                 restore_ipokey(tdi->locy, tdi->oldloc+1);
729                 restore_ipokey(tdi->locz, tdi->oldloc+2);
730
731                 restore_ipokey(tdi->rotx, tdi->oldrot);
732                 restore_ipokey(tdi->roty, tdi->oldrot+1);
733                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
734                 
735                 restore_ipokey(tdi->sizex, tdi->oldsize);
736                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
737                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
738         }
739 }
740
741 void restoreTransObjects(TransInfo *t)
742 {
743         TransData *td;
744         
745         for (td = t->data; td < t->data + t->total; td++) {
746                 restoreElement(td);
747 #ifdef WITH_VERSE
748                 /* position of vertexes and object transformation matrix is sent
749                  * extra, becuase blender uses synchronous sending of vertexes
750                  * position as well object trans. matrix and it isn't possible to
751                  * send it in recalcData sometimes */
752                 if(td->flag & TD_VERSE_VERT) {
753                         if(td->verse) {
754                                 ((VerseVert*)td->verse)->flag |= VERT_POS_OBSOLETE;
755                         }
756                 }
757                 else if(td->flag & TD_VERSE_OBJECT)
758                         if(td->verse) {
759                                 struct VNode *vnode;
760                                 vnode = (VNode*)((Object*)td->verse)->vnode;
761                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
762                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
763                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
764                         }
765 #endif
766         }       
767         recalcData(t);
768 }
769
770 void calculateCenter2D(TransInfo *t)
771 {
772         if (t->flag & (T_EDIT|T_POSE)) {
773                 Object *ob= G.obedit?G.obedit:t->poseobj;
774                 float vec[3];
775                 
776                 VECCOPY(vec, t->center);
777                 Mat4MulVecfl(ob->obmat, vec);
778                 projectIntView(t, vec, t->center2d);
779         }
780         else {
781                 projectIntView(t, t->center, t->center2d);
782         }
783 }
784
785 void calculateCenterCursor(TransInfo *t)
786 {
787         float *cursor;
788
789         cursor = give_cursor();
790         VECCOPY(t->center, cursor);
791
792         /* If edit or pose mode, move cursor in local space */
793         if (t->flag & (T_EDIT|T_POSE)) {
794                 Object *ob = G.obedit?G.obedit:t->poseobj;
795                 float mat[3][3], imat[3][3];
796                 
797                 VecSubf(t->center, t->center, ob->obmat[3]);
798                 Mat3CpyMat4(mat, ob->obmat);
799                 Mat3Inv(imat, mat);
800                 Mat3MulVecfl(imat, t->center);
801         }
802         
803         calculateCenter2D(t);
804 }
805
806 void calculateCenterCursor2D(TransInfo *t)
807 {
808         float aspx=1.0, aspy=1.0;
809         
810         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
811                 transform_aspect_ratio_tface_uv(&aspx, &aspy);
812         if (G.v2d) {
813                 t->center[0] = G.v2d->cursor[0] * aspx; 
814                 t->center[1] = G.v2d->cursor[1] * aspy; 
815         }
816         calculateCenter2D(t);
817 }
818
819 void calculateCenterMedian(TransInfo *t)
820 {
821         float partial[3] = {0.0f, 0.0f, 0.0f};
822         int i;
823         
824         for(i = 0; i < t->total; i++) {
825                 if (t->data[i].flag & TD_SELECTED) {
826                         if (!(t->data[i].flag & TD_NOCENTER))
827                                 VecAddf(partial, partial, t->data[i].center);
828                 }
829                 else {
830                         /* 
831                            All the selected elements are at the head of the array 
832                            which means we can stop when it finds unselected data
833                         */
834                         break;
835                 }
836         }
837         if(i)
838                 VecMulf(partial, 1.0f / i);
839         VECCOPY(t->center, partial);
840
841         calculateCenter2D(t);
842 }
843
844 void calculateCenterBound(TransInfo *t)
845 {
846         float max[3];
847         float min[3];
848         int i;
849         for(i = 0; i < t->total; i++) {
850                 if (i) {
851                         if (t->data[i].flag & TD_SELECTED) {
852                                 if (!(t->data[i].flag & TD_NOCENTER))
853                                         MinMax3(min, max, t->data[i].center);
854                         }
855                         else {
856                                 /* 
857                                    All the selected elements are at the head of the array 
858                                    which means we can stop when it finds unselected data
859                                 */
860                                 break;
861                         }
862                 }
863                 else {
864                         VECCOPY(max, t->data[i].center);
865                         VECCOPY(min, t->data[i].center);
866                 }
867         }
868         VecAddf(t->center, min, max);
869         VecMulf(t->center, 0.5);
870
871         calculateCenter2D(t);
872 }
873
874 void calculateCenter(TransInfo *t) 
875 {
876         switch(t->around) {
877         case V3D_CENTER:
878                 calculateCenterBound(t);
879                 break;
880         case V3D_CENTROID:
881                 calculateCenterMedian(t);
882                 break;
883         case V3D_CURSOR:
884                 if(t->spacetype==SPACE_IMAGE)
885                         calculateCenterCursor2D(t);
886                 else
887                         calculateCenterCursor(t);
888                 break;
889         case V3D_LOCAL:
890                 /* Individual element center uses median center for helpline and such */
891                 calculateCenterMedian(t);
892                 break;
893         case V3D_ACTIVE:
894                 /* set median, and if if if... do object center */
895                 
896                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
897                 if (G.obedit && G.obedit->type == OB_MESH && G.editMesh->selected.last) {
898                         EM_editselection_center(t->center, G.editMesh->selected.last);
899                         calculateCenter2D(t);
900                         break;
901                 } /* END EDIT MODE ACTIVE ELEMENT */
902                 
903                 calculateCenterMedian(t);
904                 if((t->flag & (T_EDIT|T_POSE))==0) {
905                         Object *ob= OBACT;
906                         if(ob) {
907                                 VECCOPY(t->center, ob->obmat[3]);
908                                 projectIntView(t, t->center, t->center2d);
909                         }
910                 }
911         }
912
913         /* setting constraint center */
914         VECCOPY(t->con.center, t->center);
915         if(t->flag & (T_EDIT|T_POSE)) {
916                 Object *ob= G.obedit?G.obedit:t->poseobj;
917                 Mat4MulVecfl(ob->obmat, t->con.center);
918         }
919
920         /* voor panning from cameraview */
921         if(t->flag & T_OBJECT) {
922                 if( G.vd->camera==OBACT && G.vd->persp>1) {
923                         float axis[3];
924                         /* persinv is nasty, use viewinv instead, always right */
925                         VECCOPY(axis, t->viewinv[2]);
926                         Normalize(axis);
927
928                         /* 6.0 = 6 grid units */
929                         axis[0]= t->center[0]- 6.0f*axis[0];
930                         axis[1]= t->center[1]- 6.0f*axis[1];
931                         axis[2]= t->center[2]- 6.0f*axis[2];
932                         
933                         projectIntView(t, axis, t->center2d);
934                         
935                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
936                         if(t->mode==TFM_TRANSLATION) {
937                                 VECCOPY(t->center, axis);
938                                 VECCOPY(t->con.center, t->center);
939                         }
940                 }
941         }       
942
943         if(t->spacetype==SPACE_VIEW3D)
944                 initgrabz(t->center[0], t->center[1], t->center[2]);
945 }
946
947 void calculatePropRatio(TransInfo *t)
948 {
949         TransData *td = t->data;
950         int i;
951         float dist;
952         short connected = t->flag & T_PROP_CONNECTED;
953
954         if (t->flag & T_PROP_EDIT) {
955                 for(i = 0 ; i < t->total; i++, td++) {
956                         if (td->flag & TD_SELECTED) {
957                                 td->factor = 1.0f;
958                         }
959                         else if ((connected && 
960                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
961                                 ||
962                                         (connected == 0 &&
963                                                 td->rdist > t->propsize)) {
964                                 /* 
965                                    The elements are sorted according to their dist member in the array,
966                                    that means we can stop when it finds one element outside of the propsize.
967                                 */
968                                 td->flag |= TD_NOACTION;
969                                 td->factor = 0.0f;
970                                 restoreElement(td);
971                         }
972                         else {
973                                 /* Use rdist for falloff calculations, it is the real distance */
974                                 td->flag &= ~TD_NOACTION;
975                                 dist= (t->propsize-td->rdist)/t->propsize;
976                                 
977                                 /*
978                                  * Clamp to positive numbers.
979                                  * Certain corner cases with connectivity and individual centers
980                                  * can give values of rdist larger than propsize.
981                                  */
982                                 if (dist < 0.0f)
983                                         dist = 0.0f;
984                                 
985                                 switch(G.scene->prop_mode) {
986                                 case PROP_SHARP:
987                                         td->factor= dist*dist;
988                                         break;
989                                 case PROP_SMOOTH:
990                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
991                                         break;
992                                 case PROP_ROOT:
993                                         td->factor = (float)sqrt(dist);
994                                         break;
995                                 case PROP_LIN:
996                                         td->factor = dist;
997                                         break;
998                                 case PROP_CONST:
999                                         td->factor = 1.0f;
1000                                         break;
1001                                 case PROP_SPHERE:
1002                                         td->factor = (float)sqrt(2*dist - dist * dist);
1003                                         break;
1004                                 case PROP_RANDOM:
1005                                         BLI_srand( BLI_rand() ); /* random seed */
1006                                         td->factor = BLI_frand()*dist;
1007                                         break;
1008                                 default:
1009                                         td->factor = 1;
1010                                 }
1011                         }
1012                 }
1013                 switch(G.scene->prop_mode) {
1014                 case PROP_SHARP:
1015                         strcpy(t->proptext, "(Sharp)");
1016                         break;
1017                 case PROP_SMOOTH:
1018                         strcpy(t->proptext, "(Smooth)");
1019                         break;
1020                 case PROP_ROOT:
1021                         strcpy(t->proptext, "(Root)");
1022                         break;
1023                 case PROP_LIN:
1024                         strcpy(t->proptext, "(Linear)");
1025                         break;
1026                 case PROP_CONST:
1027                         strcpy(t->proptext, "(Constant)");
1028                         break;
1029                 case PROP_SPHERE:
1030                         strcpy(t->proptext, "(Sphere)");
1031                         break;
1032                 case PROP_RANDOM:
1033                         strcpy(t->proptext, "(Random)");
1034                         break;
1035                 default:
1036                         strcpy(t->proptext, "");
1037                 }
1038         }
1039         else {
1040                 for(i = 0 ; i < t->total; i++, td++) {
1041                         td->factor = 1.0;
1042                 }
1043                 strcpy(t->proptext, "");
1044         }
1045 }
1046
1047 TransInfo * BIF_GetTransInfo() {
1048         return &Trans;
1049 }
1050