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