Initial commit of cloth modifier from branch rev 13453
[blender.git] / source / blender / src / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_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                                         if(modifiers_isSoftbodyEnabled(base->object)) {
473                                                 base->object->softflag |= OB_SB_REDO;
474                                         }
475                                         else if(modifiers_isClothEnabled(base->object)) {
476                                                 ClothModifierData *clmd = (ClothModifierData *) modifiers_findByType(base->object, eModifierType_Cloth);
477                                                 clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
478                                         }
479                                         
480                                 }
481                         }
482                 }
483                 else
484                         where_is_pose(ob);
485         }
486         else if(G.f & G_PARTICLEEDIT) {
487                 flushTransParticles(t);
488         }
489         else {
490                 for(base= FIRSTBASE; base; base= base->next) {
491                         Object *ob= base->object;
492                         
493                         /* this flag is from depgraph, was stored in nitialize phase, handled in drawview.c */
494                         if(base->flag & BA_HAS_RECALC_OB)
495                                 ob->recalc |= OB_RECALC_OB;
496                         if(base->flag & BA_HAS_RECALC_DATA)
497                                 ob->recalc |= OB_RECALC_DATA;
498                         
499                         /* thanks to ob->ctime usage, ipos are not called in where_is_object,
500                            unless we edit ipokeys */
501                         if(base->flag & BA_DO_IPO) {
502                                 if(ob->ipo) {
503                                         IpoCurve *icu;
504                                         
505                                         ob->ctime= -1234567.0;
506                                         
507                                         icu= ob->ipo->curve.first;
508                                         while(icu) {
509                                                 calchandles_ipocurve(icu);
510                                                 icu= icu->next;
511                                         }
512                                 }                               
513                         }
514                         
515                         /* softbody & cloth exception */
516                         if(ob->recalc & OB_RECALC_DATA)
517                         {
518                                 if(modifiers_isSoftbodyEnabled(ob)) {
519                                                 ob->softflag |= OB_SB_REDO;
520                                 }
521                                 else if(modifiers_isClothEnabled(ob)) {
522                                         ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
523                                         clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
524                                 }
525                         }
526                         
527                         /* proxy exception */
528                         if(ob->proxy)
529                                 ob->proxy->recalc |= ob->recalc;
530                         if(ob->proxy_group)
531                                 group_tag_recalc(ob->proxy_group->dup_group);
532                 } 
533         }
534
535 #ifdef WITH_VERSE
536         for (td = t->data; td < t->data + t->total; td++) {
537                 if(td->flag & TD_VERSE_VERT) {
538                         if(td->verse)
539                                 send_versevert_pos((VerseVert*)td->verse);
540                 }
541                 else if(td->flag & TD_VERSE_OBJECT)
542                         if(td->verse) b_verse_send_transformation((Object*)td->verse);
543         }
544 #endif
545         
546         /* update shaded drawmode while transform */
547         if(t->spacetype==SPACE_VIEW3D && G.vd->drawtype == OB_SHADED)
548                 reshadeall_displist();
549         
550 }
551
552 void drawLine(float *center, float *dir, char axis, short options)
553 {
554         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
555         float v1[3], v2[3], v3[3];
556         char col[3], col2[3];
557         
558         //if(G.obedit) mymultmatrix(G.obedit->obmat);   // sets opengl viewing
559
560         VecCopyf(v3, dir);
561         VecMulf(v3, G.vd->far);
562         
563         VecSubf(v2, center, v3);
564         VecAddf(v1, center, v3);
565
566         if (options & DRAWLIGHT) {
567                 col[0] = col[1] = col[2] = 220;
568         }
569         else {
570                 BIF_GetThemeColor3ubv(TH_GRID, col);
571         }
572         make_axis_color(col, col2, axis);
573         glColor3ubv((GLubyte *)col2);
574
575         setlinestyle(0);
576         glBegin(GL_LINE_STRIP); 
577                 glVertex3fv(v1); 
578                 glVertex3fv(v2); 
579         glEnd();
580         
581         myloadmatrix(G.vd->viewmat);
582 }
583
584 void initTrans (TransInfo *t)
585 {
586         /* moving: is shown in drawobject() (transform color) */
587         if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
588         else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
589         else G.moving= G_TRANSFORM_OBJ;
590
591         t->data = NULL;
592         t->ext = NULL;
593
594         t->flag = 0;
595         t->num.flag = 0;
596
597
598         /* setting PET flag */
599         if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
600                 t->flag |= T_PROP_EDIT;
601                 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED;       // yes i know, has to become define
602         }
603
604         getmouseco_areawin(t->imval);
605         t->con.imval[0] = t->imval[0];
606         t->con.imval[1] = t->imval[1];
607
608         t->transform            = NULL;
609         t->handleEvent          = NULL;
610         t->customData           = NULL;
611
612         t->total                        =
613                 t->num.idx              =
614                 t->num.idx_max  =
615                 t->num.ctrl[0]  = 
616                 t->num.ctrl[1]  = 
617                 t->num.ctrl[2]  = 0;
618
619         t->val = 0.0f;
620
621         t->num.val[0]           = 
622                 t->num.val[1]   = 
623                 t->num.val[2]   = 0.0f;
624
625         t->vec[0]                       =
626                 t->vec[1]               =
627                 t->vec[2]               = 0.0f;
628         
629         t->center[0]            =
630                 t->center[1]    =
631                 t->center[2]    = 0.0f;
632         
633         Mat3One(t->mat);
634         
635         t->spacetype = curarea->spacetype;
636         if(t->spacetype==SPACE_VIEW3D) {
637                 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
638                 t->around = G.vd->around;
639         } else if(t->spacetype==SPACE_IMAGE) {
640                 t->around = G.v2d->around;
641         }
642         else
643                 t->around = V3D_CENTER;
644
645         setTransformViewMatrices(t);
646 }
647
648 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
649 void postTrans (TransInfo *t) 
650 {
651         TransData *td;
652
653         G.moving = 0; // Set moving flag off (display as usual)
654 #ifdef WITH_VERSE
655
656         for (td = t->data; td < t->data + t->total; td++) {
657                 if(td->flag & TD_VERSE_VERT) {
658                         if(td->verse) send_versevert_pos((VerseVert*)td->verse);
659                 }
660                 else if(td->flag & TD_VERSE_OBJECT) {
661                         if(td->verse) {
662                                 struct VNode *vnode;
663                                 vnode = (VNode*)((Object*)td->verse)->vnode;
664                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
665                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
666                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
667                                 b_verse_send_transformation((Object*)td->verse);
668                         }
669                 }
670         }
671 #endif
672
673         stopConstraint(t);
674         
675         /* postTrans can be called when nothing is selected, so data is NULL already */
676         if (t->data) {
677                 int a;
678
679                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
680                 for(a=0, td= t->data; a<t->total; a++, td++) {
681                         if(td->tdi) MEM_freeN(td->tdi);
682                 }
683                 MEM_freeN(t->data);
684         }
685
686         if (t->ext) MEM_freeN(t->ext);
687         if (t->data2d) {
688                 MEM_freeN(t->data2d);
689                 t->data2d= NULL;
690         }
691         
692         if ((t->flag & T_FREE_CUSTOMDATA) && t->customData != NULL) {
693                 MEM_freeN(t->customData);
694         }
695
696         if(t->spacetype==SPACE_IMAGE) {
697                 if (G.sima->flag & SI_LIVE_UNWRAP)
698                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
699         }
700 }
701
702 void applyTransObjects(TransInfo *t)
703 {
704         TransData *td;
705         
706         for (td = t->data; td < t->data + t->total; td++) {
707                 VECCOPY(td->iloc, td->loc);
708                 if (td->ext->rot) {
709                         VECCOPY(td->ext->irot, td->ext->rot);
710                 }
711                 if (td->ext->size) {
712                         VECCOPY(td->ext->isize, td->ext->size);
713                 }
714         }       
715         recalcData(t);
716
717
718 /* helper for below */
719 static void restore_ipokey(float *poin, float *old)
720 {
721         if(poin) {
722                 poin[0]= old[0];
723                 poin[-3]= old[3];
724                 poin[3]= old[6];
725         }
726 }
727
728 static void restoreElement(TransData *td) {
729         /* TransData for crease has no loc */
730         if (td->loc) {
731                 VECCOPY(td->loc, td->iloc);
732         }
733         if (td->val) {
734                 *td->val = td->ival;
735         }
736         if (td->ext && (td->flag&TD_NO_EXT)==0) {
737                 if (td->ext->rot) {
738                         VECCOPY(td->ext->rot, td->ext->irot);
739                 }
740                 if (td->ext->size) {
741                         VECCOPY(td->ext->size, td->ext->isize);
742                 }
743                 if(td->flag & TD_USEQUAT) {
744                         if (td->ext->quat) {
745                                 QUATCOPY(td->ext->quat, td->ext->iquat);
746                         }
747                 }
748         }
749         if(td->tdi) {
750                 TransDataIpokey *tdi= td->tdi;
751                 
752                 restore_ipokey(tdi->locx, tdi->oldloc);
753                 restore_ipokey(tdi->locy, tdi->oldloc+1);
754                 restore_ipokey(tdi->locz, tdi->oldloc+2);
755
756                 restore_ipokey(tdi->rotx, tdi->oldrot);
757                 restore_ipokey(tdi->roty, tdi->oldrot+1);
758                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
759                 
760                 restore_ipokey(tdi->sizex, tdi->oldsize);
761                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
762                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
763         }
764 }
765
766 void restoreTransObjects(TransInfo *t)
767 {
768         TransData *td;
769         
770         for (td = t->data; td < t->data + t->total; td++) {
771                 restoreElement(td);
772 #ifdef WITH_VERSE
773                 /* position of vertexes and object transformation matrix is sent
774                  * extra, becuase blender uses synchronous sending of vertexes
775                  * position as well object trans. matrix and it isn't possible to
776                  * send it in recalcData sometimes */
777                 if(td->flag & TD_VERSE_VERT) {
778                         if(td->verse) {
779                                 ((VerseVert*)td->verse)->flag |= VERT_POS_OBSOLETE;
780                         }
781                 }
782                 else if(td->flag & TD_VERSE_OBJECT)
783                         if(td->verse) {
784                                 struct VNode *vnode;
785                                 vnode = (VNode*)((Object*)td->verse)->vnode;
786                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
787                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
788                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
789                         }
790 #endif
791         }       
792         recalcData(t);
793 }
794
795 void calculateCenter2D(TransInfo *t)
796 {
797         if (t->flag & (T_EDIT|T_POSE)) {
798                 Object *ob= G.obedit?G.obedit:t->poseobj;
799                 float vec[3];
800                 
801                 VECCOPY(vec, t->center);
802                 Mat4MulVecfl(ob->obmat, vec);
803                 projectIntView(t, vec, t->center2d);
804         }
805         else {
806                 projectIntView(t, t->center, t->center2d);
807         }
808 }
809
810 void calculateCenterCursor(TransInfo *t)
811 {
812         float *cursor;
813
814         cursor = give_cursor();
815         VECCOPY(t->center, cursor);
816
817         /* If edit or pose mode, move cursor in local space */
818         if (t->flag & (T_EDIT|T_POSE)) {
819                 Object *ob = G.obedit?G.obedit:t->poseobj;
820                 float mat[3][3], imat[3][3];
821                 
822                 VecSubf(t->center, t->center, ob->obmat[3]);
823                 Mat3CpyMat4(mat, ob->obmat);
824                 Mat3Inv(imat, mat);
825                 Mat3MulVecfl(imat, t->center);
826         }
827         
828         calculateCenter2D(t);
829 }
830
831 void calculateCenterCursor2D(TransInfo *t)
832 {
833         float aspx=1.0, aspy=1.0;
834         
835         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
836                 transform_aspect_ratio_tface_uv(&aspx, &aspy);
837         if (G.v2d) {
838                 t->center[0] = G.v2d->cursor[0] * aspx; 
839                 t->center[1] = G.v2d->cursor[1] * aspy; 
840         }
841         calculateCenter2D(t);
842 }
843
844 void calculateCenterMedian(TransInfo *t)
845 {
846         float partial[3] = {0.0f, 0.0f, 0.0f};
847         int i;
848         
849         for(i = 0; i < t->total; i++) {
850                 if (t->data[i].flag & TD_SELECTED) {
851                         if (!(t->data[i].flag & TD_NOCENTER))
852                                 VecAddf(partial, partial, t->data[i].center);
853                 }
854                 else {
855                         /* 
856                            All the selected elements are at the head of the array 
857                            which means we can stop when it finds unselected data
858                         */
859                         break;
860                 }
861         }
862         if(i)
863                 VecMulf(partial, 1.0f / i);
864         VECCOPY(t->center, partial);
865
866         calculateCenter2D(t);
867 }
868
869 void calculateCenterBound(TransInfo *t)
870 {
871         float max[3];
872         float min[3];
873         int i;
874         for(i = 0; i < t->total; i++) {
875                 if (i) {
876                         if (t->data[i].flag & TD_SELECTED) {
877                                 if (!(t->data[i].flag & TD_NOCENTER))
878                                         MinMax3(min, max, t->data[i].center);
879                         }
880                         else {
881                                 /* 
882                                    All the selected elements are at the head of the array 
883                                    which means we can stop when it finds unselected data
884                                 */
885                                 break;
886                         }
887                 }
888                 else {
889                         VECCOPY(max, t->data[i].center);
890                         VECCOPY(min, t->data[i].center);
891                 }
892         }
893         VecAddf(t->center, min, max);
894         VecMulf(t->center, 0.5);
895
896         calculateCenter2D(t);
897 }
898
899 void calculateCenter(TransInfo *t) 
900 {
901         switch(t->around) {
902         case V3D_CENTER:
903                 calculateCenterBound(t);
904                 break;
905         case V3D_CENTROID:
906                 calculateCenterMedian(t);
907                 break;
908         case V3D_CURSOR:
909                 if(t->spacetype==SPACE_IMAGE)
910                         calculateCenterCursor2D(t);
911                 else
912                         calculateCenterCursor(t);
913                 break;
914         case V3D_LOCAL:
915                 /* Individual element center uses median center for helpline and such */
916                 calculateCenterMedian(t);
917                 break;
918         case V3D_ACTIVE:
919                 /* set median, and if if if... do object center */
920                 
921                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
922                 if (G.obedit && G.obedit->type == OB_MESH && G.editMesh->selected.last) {
923                         EM_editselection_center(t->center, G.editMesh->selected.last);
924                         calculateCenter2D(t);
925                         break;
926                 } /* END EDIT MODE ACTIVE ELEMENT */
927                 
928                 calculateCenterMedian(t);
929                 if((t->flag & (T_EDIT|T_POSE))==0) {
930                         Object *ob= OBACT;
931                         if(ob) {
932                                 VECCOPY(t->center, ob->obmat[3]);
933                                 projectIntView(t, t->center, t->center2d);
934                         }
935                 }
936         }
937
938         /* setting constraint center */
939         VECCOPY(t->con.center, t->center);
940         if(t->flag & (T_EDIT|T_POSE)) {
941                 Object *ob= G.obedit?G.obedit:t->poseobj;
942                 Mat4MulVecfl(ob->obmat, t->con.center);
943         }
944
945         /* voor panning from cameraview */
946         if(t->flag & T_OBJECT) {
947                 if( G.vd->camera==OBACT && G.vd->persp>1) {
948                         float axis[3];
949                         /* persinv is nasty, use viewinv instead, always right */
950                         VECCOPY(axis, t->viewinv[2]);
951                         Normalize(axis);
952
953                         /* 6.0 = 6 grid units */
954                         axis[0]= t->center[0]- 6.0f*axis[0];
955                         axis[1]= t->center[1]- 6.0f*axis[1];
956                         axis[2]= t->center[2]- 6.0f*axis[2];
957                         
958                         projectIntView(t, axis, t->center2d);
959                         
960                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
961                         if(t->mode==TFM_TRANSLATION) {
962                                 VECCOPY(t->center, axis);
963                                 VECCOPY(t->con.center, t->center);
964                         }
965                 }
966         }       
967
968         if(t->spacetype==SPACE_VIEW3D)
969                 initgrabz(t->center[0], t->center[1], t->center[2]);
970 }
971
972 void calculatePropRatio(TransInfo *t)
973 {
974         TransData *td = t->data;
975         int i;
976         float dist;
977         short connected = t->flag & T_PROP_CONNECTED;
978
979         if (t->flag & T_PROP_EDIT) {
980                 for(i = 0 ; i < t->total; i++, td++) {
981                         if (td->flag & TD_SELECTED) {
982                                 td->factor = 1.0f;
983                         }
984                         else if ((connected && 
985                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
986                                 ||
987                                         (connected == 0 &&
988                                                 td->rdist > t->propsize)) {
989                                 /* 
990                                    The elements are sorted according to their dist member in the array,
991                                    that means we can stop when it finds one element outside of the propsize.
992                                 */
993                                 td->flag |= TD_NOACTION;
994                                 td->factor = 0.0f;
995                                 restoreElement(td);
996                         }
997                         else {
998                                 /* Use rdist for falloff calculations, it is the real distance */
999                                 td->flag &= ~TD_NOACTION;
1000                                 dist= (t->propsize-td->rdist)/t->propsize;
1001                                 
1002                                 /*
1003                                  * Clamp to positive numbers.
1004                                  * Certain corner cases with connectivity and individual centers
1005                                  * can give values of rdist larger than propsize.
1006                                  */
1007                                 if (dist < 0.0f)
1008                                         dist = 0.0f;
1009                                 
1010                                 switch(G.scene->prop_mode) {
1011                                 case PROP_SHARP:
1012                                         td->factor= dist*dist;
1013                                         break;
1014                                 case PROP_SMOOTH:
1015                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1016                                         break;
1017                                 case PROP_ROOT:
1018                                         td->factor = (float)sqrt(dist);
1019                                         break;
1020                                 case PROP_LIN:
1021                                         td->factor = dist;
1022                                         break;
1023                                 case PROP_CONST:
1024                                         td->factor = 1.0f;
1025                                         break;
1026                                 case PROP_SPHERE:
1027                                         td->factor = (float)sqrt(2*dist - dist * dist);
1028                                         break;
1029                                 case PROP_RANDOM:
1030                                         BLI_srand( BLI_rand() ); /* random seed */
1031                                         td->factor = BLI_frand()*dist;
1032                                         break;
1033                                 default:
1034                                         td->factor = 1;
1035                                 }
1036                         }
1037                 }
1038                 switch(G.scene->prop_mode) {
1039                 case PROP_SHARP:
1040                         strcpy(t->proptext, "(Sharp)");
1041                         break;
1042                 case PROP_SMOOTH:
1043                         strcpy(t->proptext, "(Smooth)");
1044                         break;
1045                 case PROP_ROOT:
1046                         strcpy(t->proptext, "(Root)");
1047                         break;
1048                 case PROP_LIN:
1049                         strcpy(t->proptext, "(Linear)");
1050                         break;
1051                 case PROP_CONST:
1052                         strcpy(t->proptext, "(Constant)");
1053                         break;
1054                 case PROP_SPHERE:
1055                         strcpy(t->proptext, "(Sphere)");
1056                         break;
1057                 case PROP_RANDOM:
1058                         strcpy(t->proptext, "(Random)");
1059                         break;
1060                 default:
1061                         strcpy(t->proptext, "");
1062                 }
1063         }
1064         else {
1065                 for(i = 0 ; i < t->total; i++, td++) {
1066                         td->factor = 1.0;
1067                 }
1068                 strcpy(t->proptext, "");
1069         }
1070 }
1071
1072 TransInfo * BIF_GetTransInfo() {
1073         return &Trans;
1074 }
1075