1a803bf00a81d2e7ab3f9f730067303d51f2b0df
[blender.git] / source / blender / src / 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 "DNA_action_types.h"
36 #include "DNA_armature_types.h"
37 #include "DNA_constraint_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_lattice_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_modifier_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_object_force.h"
44 #include "DNA_particle_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_userdef_types.h"
49 #include "DNA_view3d_types.h"
50
51 #include "BIF_screen.h"
52 #include "BIF_resources.h"
53 #include "BIF_mywindow.h"
54 #include "BIF_gl.h"
55 #include "BIF_editaction.h"
56 #include "BIF_editarmature.h"
57 #include "BIF_editmesh.h"
58 #include "BIF_editnla.h"
59 #include "BIF_editsima.h"
60 #include "BIF_editparticle.h"
61 #include "BIF_meshtools.h"
62 #include "BIF_retopo.h"
63
64 #include "BSE_editipo.h"
65 #include "BSE_editipo_types.h"
66
67 #ifdef WITH_VERSE
68 #include "BIF_verse.h"
69 #endif
70
71 #include "BKE_action.h"
72 #include "BKE_anim.h"
73 #include "BKE_armature.h"
74 #include "BKE_cloth.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 != V3D_ORTHO)
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                                                 if (td->flag & TD_SKIP)
193                                                         continue;
194                         
195                                                 VecCopyf(loc,  td->loc);
196                                                 VecCopyf(iloc, td->iloc);
197
198                                                 if (mmd->mirror_ob) {
199                                                         VecMat4MulVecfl(loc, mtx, loc);
200                                                         VecMat4MulVecfl(iloc, mtx, iloc);
201                                                 }
202
203                                                 clip = 0;
204                                                 if(axis & 1) {
205                                                         if(fabs(iloc[0])<=tolerance[0] || 
206                                                            loc[0]*iloc[0]<0.0f) {
207                                                                 loc[0]= 0.0f;
208                                                                 clip = 1;
209                                                         }
210                                                 }
211                         
212                                                 if(axis & 2) {
213                                                         if(fabs(iloc[1])<=tolerance[1] || 
214                                                            loc[1]*iloc[1]<0.0f) {
215                                                                 loc[1]= 0.0f;
216                                                                 clip = 1;
217                                                         }
218                                                 }
219                                                 if(axis & 4) {
220                                                         if(fabs(iloc[2])<=tolerance[2] || 
221                                                            loc[2]*iloc[2]<0.0f) {
222                                                                 loc[2]= 0.0f;
223                                                                 clip = 1;
224                                                         }
225                                                 }
226                                                 if (clip) {
227                                                         if (mmd->mirror_ob) {
228                                                                 VecMat4MulVecfl(loc, imtx, loc);
229                                                         }
230                                                         VecCopyf(td->loc, loc);
231                                                 }
232                                         }
233                                 }
234
235                         }
236                 }
237         }
238 }
239
240 /* assumes G.obedit set to mesh object */
241 static void editmesh_apply_to_mirror(TransInfo *t)
242 {
243         TransData *td = t->data;
244         EditVert *eve;
245         int i;
246         
247         for(i = 0 ; i < t->total; i++, td++) {
248                 if (td->flag & TD_NOACTION)
249                         break;
250                 if (td->loc==NULL)
251                         break;
252                 if (td->flag & TD_SKIP)
253                         continue;
254                 
255                 eve = td->tdmir;
256                 if(eve) {
257                         eve->co[0]= -td->loc[0];
258                         eve->co[1]= td->loc[1];
259                         eve->co[2]= td->loc[2];
260                 }               
261         }               
262 }
263
264 /* called for updating while transform acts, once per redraw */
265 void recalcData(TransInfo *t)
266 {
267         Base *base;
268 #ifdef WITH_VERSE
269         struct TransData *td;
270 #endif
271         
272         if (t->spacetype == SPACE_ACTION) {
273                 Object *ob= OBACT;
274                 void *data;
275                 short context;
276                 
277                 /* determine what type of data we are operating on */
278                 data = get_action_context(&context);
279                 if (data == NULL) return;
280                 
281                 if (G.saction->lock) {
282                         if (context == ACTCONT_ACTION) {
283                                 if(ob) {
284                                         ob->ctime= -1234567.0f;
285                                         if(ob->pose || ob_get_key(ob))
286                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC);
287                                         else
288                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
289                                 }
290                         }
291                         else if (context == ACTCONT_SHAPEKEY) {
292                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
293                         }
294                 }
295         }       
296         else if (t->spacetype == SPACE_NLA) {
297                 if (G.snla->lock) {
298                         for (base=G.scene->base.first; base; base=base->next) {
299                                 if (base->flag & BA_HAS_RECALC_OB)
300                                         base->object->recalc |= OB_RECALC_OB;
301                                 if (base->flag & BA_HAS_RECALC_DATA)
302                                         base->object->recalc |= OB_RECALC_DATA;
303                                 
304                                 if (base->object->recalc) 
305                                         base->object->ctime= -1234567.0f;       // eveil! 
306                         }
307                         
308                         DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
309                 }
310         }
311         else if (t->spacetype == SPACE_IPO) {
312                 EditIpo *ei;
313                 int dosort = 0;
314                 int a;
315                 
316                 /* do the flush first */
317                 flushTransIpoData(t);
318                 
319                 /* now test if there is a need to re-sort */
320                 ei= G.sipo->editipo;
321                 for (a=0; a<G.sipo->totipo; a++, ei++) {
322                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
323                                 
324                                 /* watch it: if the time is wrong: do not correct handles */
325                                 if (test_time_ipocurve(ei->icu)) {
326                                         dosort++;
327                                 } else {
328                                         calchandles_ipocurve(ei->icu);
329                                 }
330                         }
331                 }
332                 
333                 /* do resort and other updates? */
334                 if (dosort) remake_ipo_transdata(t);
335                 if (G.sipo->showkey) update_ipokey_val();
336                 
337                 calc_ipo(G.sipo->ipo, (float)CFRA);
338                 
339                 /* update realtime - not working? */
340                 if (G.sipo->lock) {
341                         if (G.sipo->blocktype==ID_MA || G.sipo->blocktype==ID_TE) {
342                                 do_ipo(G.sipo->ipo);
343                         }
344                         else if(G.sipo->blocktype==ID_CA) {
345                                 do_ipo(G.sipo->ipo);
346                         }
347                         else if(G.sipo->blocktype==ID_KE) {
348                                 Object *ob= OBACT;
349                                 if(ob) {
350                                         ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
351                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
352                                 }
353                         }
354                         else if(G.sipo->blocktype==ID_PO) {
355                                 Object *ob= OBACT;
356                                 if(ob && ob->pose) {
357                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
358                                 }
359                         }
360                         else if(G.sipo->blocktype==ID_OB) {
361                                 Base *base= FIRSTBASE;
362                                 
363                                 while(base) {
364                                         if(base->object->ipo==G.sipo->ipo) {
365                                                 do_ob_ipo(base->object);
366                                                 base->object->recalc |= OB_RECALC_OB;
367                                         }
368                                         base= base->next;
369                                 }
370                                 DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
371                         }
372                 }
373         }
374         else if (G.obedit) {
375                 if (G.obedit->type == OB_MESH) {
376                         if(t->spacetype==SPACE_IMAGE) {
377                                 flushTransUVs(t);
378                                 if (G.sima->flag & SI_LIVE_UNWRAP)
379                                         unwrap_lscm_live_re_solve();
380                         } else {
381                                 /* mirror modifier clipping? */
382                                 if(t->state != TRANS_CANCEL) {
383                                         if ((G.qual & LR_CTRLKEY)==0) {
384                                                 /* Only retopo if not snapping, Note, this is the only case of G.qual being used, but we have no T_SHIFT_MOD - Campbell */
385                                                 retopo_do_all();
386                                         }
387                                         clipMirrorModifier(t, G.obedit);
388                                 }
389                                 if((t->context & CTX_NO_MIRROR) == 0 && (G.scene->toolsettings->editbutflag & B_MESH_X_MIRROR))
390                                         editmesh_apply_to_mirror(t);
391                                 
392                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
393                                 
394                                 recalc_editnormals();
395                         }
396                 }
397                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
398                         Nurb *nu= editNurb.first;
399                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
400                         
401                         if (t->state == TRANS_CANCEL) {
402                                 while(nu) {
403                                         calchandlesNurb(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
404                                         nu= nu->next;
405                                 }
406                         } else {
407                                 /* Normal updating */
408                                 while(nu) {
409                                         test2DNurb(nu);
410                                         calchandlesNurb(nu);
411                                         nu= nu->next;
412                                 }
413                                 retopo_do_all();
414                         }
415                 }
416                 else if(G.obedit->type==OB_ARMATURE){   /* no recalc flag, does pose */
417                         bArmature *arm= G.obedit->data;
418                         EditBone *ebo;
419                         
420                         /* Ensure all bones are correctly adjusted */
421                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
422                                 
423                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
424                                         /* If this bone has a parent tip that has been moved */
425                                         if (ebo->parent->flag & BONE_TIPSEL){
426                                                 VECCOPY (ebo->head, ebo->parent->tail);
427                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
428                                         }
429                                         /* If this bone has a parent tip that has NOT been moved */
430                                         else{
431                                                 VECCOPY (ebo->parent->tail, ebo->head);
432                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
433                                         }
434                                 }
435                                 
436                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
437                                 ebo->length= VecLenf(ebo->head, ebo->tail);
438                                 if(ebo->oldlength==0.0f) {
439                                         ebo->rad_head= 0.25f*ebo->length;
440                                         ebo->rad_tail= 0.10f*ebo->length;
441                                         ebo->dist= 0.25f*ebo->length;
442                                         if(ebo->parent) {
443                                                 if(ebo->rad_head > ebo->parent->rad_tail)
444                                                         ebo->rad_head= ebo->parent->rad_tail;
445                                         }
446                                 }
447                                 else if(t->mode!=TFM_BONE_ENVELOPE) {
448                                         /* if bones change length, lets do that for the deform distance as well */
449                                         ebo->dist*= ebo->length/ebo->oldlength;
450                                         ebo->rad_head*= ebo->length/ebo->oldlength;
451                                         ebo->rad_tail*= ebo->length/ebo->oldlength;
452                                         ebo->oldlength= ebo->length;
453                                 }
454                         }
455                         if(arm->flag & ARM_MIRROR_EDIT) 
456                                 transform_armature_mirror_update();
457                         
458                 }
459                 else if(G.obedit->type==OB_LATTICE) {
460                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
461                         
462                         if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
463                 }
464                 else {
465                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
466                 }
467         }
468         else if( (t->flag & T_POSE) && t->poseobj) {
469                 Object *ob= t->poseobj;
470                 bArmature *arm= ob->data;
471                 
472                 /* old optimize trick... this enforces to bypass the depgraph */
473                 if (!(arm->flag & ARM_DELAYDEFORM)) {
474                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
475                 }
476                 else
477                         where_is_pose(ob);
478         }
479         else if(G.f & G_PARTICLEEDIT) {
480                 flushTransParticles(t);
481         }
482         else {
483                 for(base= FIRSTBASE; base; base= base->next) {
484                         Object *ob= base->object;
485                         
486                         /* this flag is from depgraph, was stored in initialize phase, handled in drawview.c */
487                         if(base->flag & BA_HAS_RECALC_OB)
488                                 ob->recalc |= OB_RECALC_OB;
489                         if(base->flag & BA_HAS_RECALC_DATA)
490                                 ob->recalc |= OB_RECALC_DATA;
491
492                         /* thanks to ob->ctime usage, ipos are not called in where_is_object,
493                            unless we edit ipokeys */
494                         if(base->flag & BA_DO_IPO) {
495                                 if(ob->ipo) {
496                                         IpoCurve *icu;
497                                         
498                                         ob->ctime= -1234567.0;
499                                         
500                                         icu= ob->ipo->curve.first;
501                                         while(icu) {
502                                                 calchandles_ipocurve(icu);
503                                                 icu= icu->next;
504                                         }
505                                 }                               
506                         }
507                         
508                         /* proxy exception */
509                         if(ob->proxy)
510                                 ob->proxy->recalc |= ob->recalc;
511                         if(ob->proxy_group)
512                                 group_tag_recalc(ob->proxy_group->dup_group);
513                 } 
514         }
515
516 #ifdef WITH_VERSE
517         for (td = t->data; td < t->data + t->total; td++) {
518                 if(td->flag & TD_VERSE_VERT) {
519                         if(td->verse)
520                                 send_versevert_pos((VerseVert*)td->verse);
521                 }
522                 else if(td->flag & TD_VERSE_OBJECT)
523                         if(td->verse) b_verse_send_transformation((Object*)td->verse);
524         }
525 #endif
526         
527         /* update shaded drawmode while transform */
528         if(t->spacetype==SPACE_VIEW3D && G.vd->drawtype == OB_SHADED)
529                 reshadeall_displist();
530 }
531
532 void initTransModeFlags(TransInfo *t, int mode) 
533 {
534         t->mode = mode;
535         t->num.flag = 0;
536
537         /* REMOVING RESTRICTIONS FLAGS */
538         t->flag &= ~T_ALL_RESTRICTIONS;
539         
540         switch (mode) {
541         case TFM_RESIZE:
542                 t->flag |= T_NULL_ONE;
543                 t->num.flag |= NUM_NULL_ONE;
544                 t->num.flag |= NUM_AFFECT_ALL;
545                 if (!G.obedit) {
546                         t->flag |= T_NO_ZERO;
547                         t->num.flag |= NUM_NO_ZERO;
548                 }
549                 break;
550         case TFM_TOSPHERE:
551                 t->num.flag |= NUM_NULL_ONE;
552                 t->num.flag |= NUM_NO_NEGATIVE;
553                 t->flag |= T_NO_CONSTRAINT;
554                 break;
555         case TFM_SHEAR:
556         case TFM_CREASE:
557         case TFM_BONE_ENVELOPE:
558         case TFM_CURVE_SHRINKFATTEN:
559         case TFM_BONE_ROLL:
560                 t->flag |= T_NO_CONSTRAINT;
561                 break;
562         }
563 }
564
565 void drawLine(float *center, float *dir, char axis, short options)
566 {
567         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
568         float v1[3], v2[3], v3[3];
569         char col[3], col2[3];
570         
571         //if(G.obedit) mymultmatrix(G.obedit->obmat);   // sets opengl viewing
572
573         VecCopyf(v3, dir);
574         VecMulf(v3, G.vd->far);
575         
576         VecSubf(v2, center, v3);
577         VecAddf(v1, center, v3);
578
579         if (options & DRAWLIGHT) {
580                 col[0] = col[1] = col[2] = 220;
581         }
582         else {
583                 BIF_GetThemeColor3ubv(TH_GRID, col);
584         }
585         make_axis_color(col, col2, axis);
586         glColor3ubv((GLubyte *)col2);
587
588         setlinestyle(0);
589         glBegin(GL_LINE_STRIP); 
590                 glVertex3fv(v1); 
591                 glVertex3fv(v2); 
592         glEnd();
593         
594         myloadmatrix(G.vd->viewmat);
595 }
596
597 void initTrans (TransInfo *t)
598 {
599         
600         /* moving: is shown in drawobject() (transform color) */
601         if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
602         else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
603         else G.moving= G_TRANSFORM_OBJ;
604
605         t->data = NULL;
606         t->ext = NULL;
607
608         t->flag = 0;
609
610         /* setting PET flag */
611         if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
612                 t->flag |= T_PROP_EDIT;
613                 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED;       // yes i know, has to become define
614         }
615
616         getmouseco_areawin(t->imval);
617         t->con.imval[0] = t->imval[0];
618         t->con.imval[1] = t->imval[1];
619
620         t->transform            = NULL;
621         t->handleEvent          = NULL;
622
623         t->total                        =
624                 t->num.idx              =
625                 t->num.idx_max  =
626                 t->num.ctrl[0]  = 
627                 t->num.ctrl[1]  = 
628                 t->num.ctrl[2]  = 0;
629
630         t->val = 0.0f;
631
632         t->num.val[0]           = 
633                 t->num.val[1]   = 
634                 t->num.val[2]   = 0.0f;
635
636         t->vec[0]                       =
637                 t->vec[1]               =
638                 t->vec[2]               = 0.0f;
639         
640         t->center[0]            =
641                 t->center[1]    =
642                 t->center[2]    = 0.0f;
643         
644         Mat3One(t->mat);
645         
646         t->spacetype = curarea->spacetype;
647         if(t->spacetype==SPACE_VIEW3D) {
648                 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
649                 t->around = G.vd->around;
650         } else if(t->spacetype==SPACE_IMAGE) {
651                 t->around = G.v2d->around;
652         }
653         else
654                 t->around = V3D_CENTER;
655
656         setTransformViewMatrices(t);
657         initNDofInput(&(t->ndof));
658 }
659
660 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
661 void postTrans (TransInfo *t) 
662 {
663         TransData *td;
664
665         G.moving = 0; // Set moving flag off (display as usual)
666 #ifdef WITH_VERSE
667
668         for (td = t->data; td < t->data + t->total; td++) {
669                 if(td->flag & TD_VERSE_VERT) {
670                         if(td->verse) send_versevert_pos((VerseVert*)td->verse);
671                 }
672                 else if(td->flag & TD_VERSE_OBJECT) {
673                         if(td->verse) {
674                                 struct VNode *vnode;
675                                 vnode = (VNode*)((Object*)td->verse)->vnode;
676                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
677                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
678                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
679                                 b_verse_send_transformation((Object*)td->verse);
680                         }
681                 }
682         }
683 #endif
684
685         stopConstraint(t);
686         
687         /* postTrans can be called when nothing is selected, so data is NULL already */
688         if (t->data) {
689                 int a;
690
691                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
692                 for(a=0, td= t->data; a<t->total; a++, td++) {
693                         if(td->tdi) MEM_freeN(td->tdi);
694                         if (td->flag & TD_BEZTRIPLE) MEM_freeN(td->hdata); 
695                 }
696                 MEM_freeN(t->data);
697         }
698
699         if (t->ext) MEM_freeN(t->ext);
700         if (t->data2d) {
701                 MEM_freeN(t->data2d);
702                 t->data2d= NULL;
703         }
704
705         if(t->spacetype==SPACE_IMAGE) {
706                 if (G.sima->flag & SI_LIVE_UNWRAP)
707                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
708         }
709 }
710
711 void applyTransObjects(TransInfo *t)
712 {
713         TransData *td;
714         
715         for (td = t->data; td < t->data + t->total; td++) {
716                 VECCOPY(td->iloc, td->loc);
717                 if (td->ext->rot) {
718                         VECCOPY(td->ext->irot, td->ext->rot);
719                 }
720                 if (td->ext->size) {
721                         VECCOPY(td->ext->isize, td->ext->size);
722                 }
723         }       
724         recalcData(t);
725
726
727 /* helper for below */
728 static void restore_ipokey(float *poin, float *old)
729 {
730         if(poin) {
731                 poin[0]= old[0];
732                 poin[-3]= old[3];
733                 poin[3]= old[6];
734         }
735 }
736
737 static void restoreElement(TransData *td) {
738         /* TransData for crease has no loc */
739         if (td->loc) {
740                 VECCOPY(td->loc, td->iloc);
741         }
742         if (td->val) {
743                 *td->val = td->ival;
744         }
745         if (td->ext && (td->flag&TD_NO_EXT)==0) {
746                 if (td->ext->rot) {
747                         VECCOPY(td->ext->rot, td->ext->irot);
748                 }
749                 if (td->ext->size) {
750                         VECCOPY(td->ext->size, td->ext->isize);
751                 }
752                 if(td->flag & TD_USEQUAT) {
753                         if (td->ext->quat) {
754                                 QUATCOPY(td->ext->quat, td->ext->iquat);
755                         }
756                 }
757         }
758         
759         if (td->flag & TD_BEZTRIPLE) {
760                 *(td->hdata->h1) = td->hdata->ih1;
761                 *(td->hdata->h2) = td->hdata->ih2;
762         }
763         
764         if(td->tdi) {
765                 TransDataIpokey *tdi= td->tdi;
766                 
767                 restore_ipokey(tdi->locx, tdi->oldloc);
768                 restore_ipokey(tdi->locy, tdi->oldloc+1);
769                 restore_ipokey(tdi->locz, tdi->oldloc+2);
770
771                 restore_ipokey(tdi->rotx, tdi->oldrot);
772                 restore_ipokey(tdi->roty, tdi->oldrot+1);
773                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
774                 
775                 restore_ipokey(tdi->sizex, tdi->oldsize);
776                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
777                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
778         }
779 }
780
781 void restoreTransObjects(TransInfo *t)
782 {
783         TransData *td;
784         
785         for (td = t->data; td < t->data + t->total; td++) {
786                 restoreElement(td);
787 #ifdef WITH_VERSE
788                 /* position of vertexes and object transformation matrix is sent
789                  * extra, becuase blender uses synchronous sending of vertexes
790                  * position as well object trans. matrix and it isn't possible to
791                  * send it in recalcData sometimes */
792                 if(td->flag & TD_VERSE_VERT) {
793                         if(td->verse) {
794                                 ((VerseVert*)td->verse)->flag |= VERT_POS_OBSOLETE;
795                         }
796                 }
797                 else if(td->flag & TD_VERSE_OBJECT)
798                         if(td->verse) {
799                                 struct VNode *vnode;
800                                 vnode = (VNode*)((Object*)td->verse)->vnode;
801                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
802                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
803                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
804                         }
805 #endif
806         }       
807         recalcData(t);
808 }
809
810 void calculateCenter2D(TransInfo *t)
811 {
812         if (t->flag & (T_EDIT|T_POSE)) {
813                 Object *ob= G.obedit?G.obedit:t->poseobj;
814                 float vec[3];
815                 
816                 VECCOPY(vec, t->center);
817                 Mat4MulVecfl(ob->obmat, vec);
818                 projectIntView(t, vec, t->center2d);
819         }
820         else {
821                 projectIntView(t, t->center, t->center2d);
822         }
823 }
824
825 void calculateCenterCursor(TransInfo *t)
826 {
827         float *cursor;
828
829         cursor = give_cursor();
830         VECCOPY(t->center, cursor);
831
832         /* If edit or pose mode, move cursor in local space */
833         if (t->flag & (T_EDIT|T_POSE)) {
834                 Object *ob = G.obedit?G.obedit:t->poseobj;
835                 float mat[3][3], imat[3][3];
836                 
837                 VecSubf(t->center, t->center, ob->obmat[3]);
838                 Mat3CpyMat4(mat, ob->obmat);
839                 Mat3Inv(imat, mat);
840                 Mat3MulVecfl(imat, t->center);
841         }
842         
843         calculateCenter2D(t);
844 }
845
846 void calculateCenterCursor2D(TransInfo *t)
847 {
848         float aspx=1.0, aspy=1.0;
849         
850         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
851                 transform_aspect_ratio_tface_uv(&aspx, &aspy);
852         if (G.v2d) {
853                 t->center[0] = G.v2d->cursor[0] * aspx; 
854                 t->center[1] = G.v2d->cursor[1] * aspy; 
855         }
856         calculateCenter2D(t);
857 }
858
859 void calculateCenterMedian(TransInfo *t)
860 {
861         float partial[3] = {0.0f, 0.0f, 0.0f};
862         int i;
863         
864         for(i = 0; i < t->total; i++) {
865                 if (t->data[i].flag & TD_SELECTED) {
866                         if (!(t->data[i].flag & TD_NOCENTER))
867                                 VecAddf(partial, partial, t->data[i].center);
868                 }
869                 else {
870                         /* 
871                            All the selected elements are at the head of the array 
872                            which means we can stop when it finds unselected data
873                         */
874                         break;
875                 }
876         }
877         if(i)
878                 VecMulf(partial, 1.0f / i);
879         VECCOPY(t->center, partial);
880
881         calculateCenter2D(t);
882 }
883
884 void calculateCenterBound(TransInfo *t)
885 {
886         float max[3];
887         float min[3];
888         int i;
889         for(i = 0; i < t->total; i++) {
890                 if (i) {
891                         if (t->data[i].flag & TD_SELECTED) {
892                                 if (!(t->data[i].flag & TD_NOCENTER))
893                                         MinMax3(min, max, t->data[i].center);
894                         }
895                         else {
896                                 /* 
897                                    All the selected elements are at the head of the array 
898                                    which means we can stop when it finds unselected data
899                                 */
900                                 break;
901                         }
902                 }
903                 else {
904                         VECCOPY(max, t->data[i].center);
905                         VECCOPY(min, t->data[i].center);
906                 }
907         }
908         VecAddf(t->center, min, max);
909         VecMulf(t->center, 0.5);
910
911         calculateCenter2D(t);
912 }
913
914 void calculateCenter(TransInfo *t) 
915 {
916         switch(t->around) {
917         case V3D_CENTER:
918                 calculateCenterBound(t);
919                 break;
920         case V3D_CENTROID:
921                 calculateCenterMedian(t);
922                 break;
923         case V3D_CURSOR:
924                 if(t->spacetype==SPACE_IMAGE)
925                         calculateCenterCursor2D(t);
926                 else
927                         calculateCenterCursor(t);
928                 break;
929         case V3D_LOCAL:
930                 /* Individual element center uses median center for helpline and such */
931                 calculateCenterMedian(t);
932                 break;
933         case V3D_ACTIVE:
934                 /* set median, and if if if... do object center */
935                 
936                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
937                 if (G.obedit && G.obedit->type == OB_MESH && G.editMesh->selected.last) {
938                         EM_editselection_center(t->center, G.editMesh->selected.last);
939                         calculateCenter2D(t);
940                         break;
941                 } /* END EDIT MODE ACTIVE ELEMENT */
942                 
943                 calculateCenterMedian(t);
944                 if((t->flag & (T_EDIT|T_POSE))==0) {
945                         Object *ob= OBACT;
946                         if(ob) {
947                                 VECCOPY(t->center, ob->obmat[3]);
948                                 projectIntView(t, t->center, t->center2d);
949                         }
950                 }
951         }
952
953         /* setting constraint center */
954         VECCOPY(t->con.center, t->center);
955         if(t->flag & (T_EDIT|T_POSE)) {
956                 Object *ob= G.obedit?G.obedit:t->poseobj;
957                 Mat4MulVecfl(ob->obmat, t->con.center);
958         }
959
960         /* voor panning from cameraview */
961         if(t->flag & T_OBJECT) {
962                 if( G.vd->camera==OBACT && G.vd->persp==V3D_CAMOB) {
963                         float axis[3];
964                         /* persinv is nasty, use viewinv instead, always right */
965                         VECCOPY(axis, t->viewinv[2]);
966                         Normalize(axis);
967
968                         /* 6.0 = 6 grid units */
969                         axis[0]= t->center[0]- 6.0f*axis[0];
970                         axis[1]= t->center[1]- 6.0f*axis[1];
971                         axis[2]= t->center[2]- 6.0f*axis[2];
972                         
973                         projectIntView(t, axis, t->center2d);
974                         
975                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
976                         if(t->mode==TFM_TRANSLATION) {
977                                 VECCOPY(t->center, axis);
978                                 VECCOPY(t->con.center, t->center);
979                         }
980                 }
981         }       
982
983         if(t->spacetype==SPACE_VIEW3D)
984                 initgrabz(t->center[0], t->center[1], t->center[2]);
985 }
986
987 void calculatePropRatio(TransInfo *t)
988 {
989         TransData *td = t->data;
990         int i;
991         float dist;
992         short connected = t->flag & T_PROP_CONNECTED;
993
994         if (t->flag & T_PROP_EDIT) {
995                 for(i = 0 ; i < t->total; i++, td++) {
996                         if (td->flag & TD_SELECTED) {
997                                 td->factor = 1.0f;
998                         }
999                         else if ((connected && 
1000                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
1001                                 ||
1002                                         (connected == 0 &&
1003                                                 td->rdist > t->propsize)) {
1004                                 /* 
1005                                    The elements are sorted according to their dist member in the array,
1006                                    that means we can stop when it finds one element outside of the propsize.
1007                                 */
1008                                 td->flag |= TD_NOACTION;
1009                                 td->factor = 0.0f;
1010                                 restoreElement(td);
1011                         }
1012                         else {
1013                                 /* Use rdist for falloff calculations, it is the real distance */
1014                                 td->flag &= ~TD_NOACTION;
1015                                 dist= (t->propsize-td->rdist)/t->propsize;
1016                                 
1017                                 /*
1018                                  * Clamp to positive numbers.
1019                                  * Certain corner cases with connectivity and individual centers
1020                                  * can give values of rdist larger than propsize.
1021                                  */
1022                                 if (dist < 0.0f)
1023                                         dist = 0.0f;
1024                                 
1025                                 switch(G.scene->prop_mode) {
1026                                 case PROP_SHARP:
1027                                         td->factor= dist*dist;
1028                                         break;
1029                                 case PROP_SMOOTH:
1030                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1031                                         break;
1032                                 case PROP_ROOT:
1033                                         td->factor = (float)sqrt(dist);
1034                                         break;
1035                                 case PROP_LIN:
1036                                         td->factor = dist;
1037                                         break;
1038                                 case PROP_CONST:
1039                                         td->factor = 1.0f;
1040                                         break;
1041                                 case PROP_SPHERE:
1042                                         td->factor = (float)sqrt(2*dist - dist * dist);
1043                                         break;
1044                                 case PROP_RANDOM:
1045                                         BLI_srand( BLI_rand() ); /* random seed */
1046                                         td->factor = BLI_frand()*dist;
1047                                         break;
1048                                 default:
1049                                         td->factor = 1;
1050                                 }
1051                         }
1052                 }
1053                 switch(G.scene->prop_mode) {
1054                 case PROP_SHARP:
1055                         strcpy(t->proptext, "(Sharp)");
1056                         break;
1057                 case PROP_SMOOTH:
1058                         strcpy(t->proptext, "(Smooth)");
1059                         break;
1060                 case PROP_ROOT:
1061                         strcpy(t->proptext, "(Root)");
1062                         break;
1063                 case PROP_LIN:
1064                         strcpy(t->proptext, "(Linear)");
1065                         break;
1066                 case PROP_CONST:
1067                         strcpy(t->proptext, "(Constant)");
1068                         break;
1069                 case PROP_SPHERE:
1070                         strcpy(t->proptext, "(Sphere)");
1071                         break;
1072                 case PROP_RANDOM:
1073                         strcpy(t->proptext, "(Random)");
1074                         break;
1075                 default:
1076                         strcpy(t->proptext, "");
1077                 }
1078         }
1079         else {
1080                 for(i = 0 ; i < t->total; i++, td++) {
1081                         td->factor = 1.0;
1082                 }
1083                 strcpy(t->proptext, "");
1084         }
1085 }
1086
1087 TransInfo * BIF_GetTransInfo() {
1088         return &Trans;
1089 }
1090