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