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