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