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