2.5 / Nodes / Transformations
[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->spacetype==SPACE_NODE) {
579                 flushTransNodes(t);
580         }
581         else if (t->obedit) {
582                 if (t->obedit->type == OB_MESH) {
583                         if(t->spacetype==SPACE_IMAGE) {
584                                 flushTransUVs(t);
585                                 /* TRANSFORM_FIX_ME */
586 //                              if (G.sima->flag & SI_LIVE_UNWRAP)
587 //                                      unwrap_lscm_live_re_solve();
588                         } else {
589                                 EditMesh *em = ((Mesh*)t->obedit->data)->edit_mesh;
590                                 /* mirror modifier clipping? */
591                                 if(t->state != TRANS_CANCEL) {
592                                         /* TRANSFORM_FIX_ME */
593 //                                      if ((G.qual & LR_CTRLKEY)==0) {
594 //                                              /* Only retopo if not snapping, Note, this is the only case of G.qual being used, but we have no T_SHIFT_MOD - Campbell */
595 //                                              retopo_do_all();
596 //                                      }
597                                         clipMirrorModifier(t, t->obedit);
598                                 }
599                                 if((t->options & CTX_NO_MIRROR) == 0 && (t->scene->toolsettings->editbutflag & B_MESH_X_MIRROR))
600                                         editmesh_apply_to_mirror(t);
601                                 
602                                 DAG_object_flush_update(t->scene, t->obedit, OB_RECALC_DATA);  /* sets recalc flags */
603                                 
604                                 recalc_editnormals(em);
605                         }
606                 }
607         }
608         else {
609                 for(base= FIRSTBASE; base; base= base->next) {
610                         Object *ob= base->object;
611                         
612                         /* this flag is from depgraph, was stored in initialize phase, handled in drawview.c */
613                         if(base->flag & BA_HAS_RECALC_OB)
614                                 ob->recalc |= OB_RECALC_OB;
615                         if(base->flag & BA_HAS_RECALC_DATA)
616                                 ob->recalc |= OB_RECALC_DATA;
617
618                         /* thanks to ob->ctime usage, ipos are not called in where_is_object,
619                            unless we edit ipokeys */
620                         if(base->flag & BA_DO_IPO) {
621                                 if(ob->ipo) {
622                                         IpoCurve *icu;
623                                         
624                                         ob->ctime= -1234567.0;
625                                         
626                                         icu= ob->ipo->curve.first;
627                                         while(icu) {
628                                                 calchandles_ipocurve(icu);
629                                                 icu= icu->next;
630                                         }
631                                 }                               
632                         }
633                         
634                         /* proxy exception */
635                         if(ob->proxy)
636                                 ob->proxy->recalc |= ob->recalc;
637                         if(ob->proxy_group)
638                                 group_tag_recalc(ob->proxy_group->dup_group);
639                 } 
640         }
641
642 #ifdef WITH_VERSE
643         for (td = t->data; td < t->data + t->total; td++) {
644                 if(td->flag & TD_VERSE_VERT) {
645                         if(td->verse)
646                                 send_versevert_pos((VerseVert*)td->verse);
647                 }
648                 else if(td->flag & TD_VERSE_OBJECT)
649                         if(td->verse) b_verse_send_transformation((Object*)td->verse);
650         }
651 #endif
652         
653         /* update shaded drawmode while transform */
654         if(t->spacetype==SPACE_VIEW3D && ((View3D*)t->view)->drawtype == OB_SHADED)
655                 reshadeall_displist();
656 }
657
658 void drawLine(float *center, float *dir, char axis, short options)
659 {
660 #if 0 // TRANSFORM_FIX_ME
661         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
662         float v1[3], v2[3], v3[3];
663         char col[3], col2[3];
664         
665         //if(t->obedit) mymultmatrix(t->obedit->obmat); // sets opengl viewing
666
667         VecCopyf(v3, dir);
668         VecMulf(v3, G.vd->far);
669         
670         VecSubf(v2, center, v3);
671         VecAddf(v1, center, v3);
672
673         if (options & DRAWLIGHT) {
674                 col[0] = col[1] = col[2] = 220;
675         }
676         else {
677                 BIF_GetThemeColor3ubv(TH_GRID, col);
678         }
679         make_axis_color(col, col2, axis);
680         glColor3ubv((GLubyte *)col2);
681
682         setlinestyle(0);
683         glBegin(GL_LINE_STRIP); 
684                 glVertex3fv(v1); 
685                 glVertex3fv(v2); 
686         glEnd();
687         
688         myloadmatrix(G.vd->viewmat);
689 #endif
690 }
691
692 void resetTransRestrictions(TransInfo *t)
693 {
694         t->flag &= ~T_ALL_RESTRICTIONS;
695 }
696
697 void initTransInfo (bContext *C, TransInfo *t, wmEvent *event)
698 {
699         Scene *sce = CTX_data_scene(C);
700         ARegion *ar = CTX_wm_region(C);
701         ScrArea *sa = CTX_wm_area(C);
702         Object *obedit = CTX_data_edit_object(C);
703         
704         /* moving: is shown in drawobject() (transform color) */
705 //  TRANSFORM_FIX_ME    
706 //      if(obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
707 //      else if(G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
708 //      else G.moving= G_TRANSFORM_OBJ;
709         
710         t->scene = sce;
711         t->sa = sa;
712         t->ar = ar;
713         t->obedit = obedit;
714
715         t->data = NULL;
716         t->ext = NULL;
717
718         t->flag = 0;
719         
720         t->redraw = 1; /* redraw first time */
721         
722         t->propsize = 1.0f; /* TRANSFORM_FIX_ME this needs to be saved in scene or something */
723
724         /* setting PET flag */
725         if ((t->options & CTX_NO_PET) == 0 && (sce->proportional)) {
726                 t->flag |= T_PROP_EDIT;
727                 
728                 if(sce->proportional == 2)
729                         t->flag |= T_PROP_CONNECTED;    // yes i know, has to become define
730         }
731
732         if (event)
733         {
734                 t->imval[0] = event->x - t->ar->winrct.xmin;
735                 t->imval[1] = event->y - t->ar->winrct.ymin;
736         }
737         else
738         {
739                 t->imval[0] = 0;
740                 t->imval[1] = 0;
741         }
742         
743         t->con.imval[0] = t->imval[0];
744         t->con.imval[1] = t->imval[1];
745
746         t->mval[0] = t->imval[0];
747         t->mval[1] = t->imval[1];
748
749         t->transform            = NULL;
750         t->handleEvent          = NULL;
751
752         t->total                        = 0;
753
754         t->val = 0.0f;
755
756         t->vec[0]                       =
757                 t->vec[1]               =
758                 t->vec[2]               = 0.0f;
759         
760         t->center[0]            =
761                 t->center[1]    =
762                 t->center[2]    = 0.0f;
763         
764         Mat3One(t->mat);
765         
766         t->spacetype = sa->spacetype;
767         if(t->spacetype == SPACE_VIEW3D)
768         {
769                 View3D *v3d = sa->spacedata.first;
770                 
771                 t->view = v3d;
772                 
773                 if(v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
774                 t->around = v3d->around;
775         }
776         else if(t->spacetype==SPACE_IMAGE || t->spacetype==SPACE_NODE)
777         {
778                 View2D *v2d = sa->spacedata.first; // XXX no!
779
780                 t->view = v2d;
781
782                 t->around = v2d->around;
783         }
784         else
785         {
786                 // XXX for now, get View2D  from the active region
787                 t->view = &ar->v2d;
788                 
789                 t->around = V3D_CENTER;
790         }
791
792         setTransformViewMatrices(t);
793         initNumInput(&t->num);
794         initNDofInput(&t->ndof);
795 }
796
797 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
798 void postTrans (TransInfo *t) 
799 {
800         TransData *td;
801
802         G.moving = 0; // Set moving flag off (display as usual)
803 #ifdef WITH_VERSE
804
805         for (td = t->data; td < t->data + t->total; td++) {
806                 if(td->flag & TD_VERSE_VERT) {
807                         if(td->verse) send_versevert_pos((VerseVert*)td->verse);
808                 }
809                 else if(td->flag & TD_VERSE_OBJECT) {
810                         if(td->verse) {
811                                 struct VNode *vnode;
812                                 vnode = (VNode*)((Object*)td->verse)->vnode;
813                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
814                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
815                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
816                                 b_verse_send_transformation((Object*)td->verse);
817                         }
818                 }
819         }
820 #endif
821
822         stopConstraint(t);
823         
824         /* postTrans can be called when nothing is selected, so data is NULL already */
825         if (t->data) {
826                 int a;
827
828                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
829                 for(a=0, td= t->data; a<t->total; a++, td++) {
830                         if(td->tdi) MEM_freeN(td->tdi);
831                         if (td->flag & TD_BEZTRIPLE) MEM_freeN(td->hdata); 
832                 }
833                 MEM_freeN(t->data);
834         }
835
836         if (t->ext) MEM_freeN(t->ext);
837         if (t->data2d) {
838                 MEM_freeN(t->data2d);
839                 t->data2d= NULL;
840         }
841
842         if(t->spacetype==SPACE_IMAGE) {
843 #if 0 // TRANSFORM_FIX_ME
844                 if (G.sima->flag & SI_LIVE_UNWRAP)
845                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
846 #endif
847         }
848         else if(t->spacetype==SPACE_ACTION) {
849                 if (t->customData)
850                         MEM_freeN(t->customData);
851         }
852 }
853
854 void applyTransObjects(TransInfo *t)
855 {
856         TransData *td;
857         
858         for (td = t->data; td < t->data + t->total; td++) {
859                 VECCOPY(td->iloc, td->loc);
860                 if (td->ext->rot) {
861                         VECCOPY(td->ext->irot, td->ext->rot);
862                 }
863                 if (td->ext->size) {
864                         VECCOPY(td->ext->isize, td->ext->size);
865                 }
866         }       
867         recalcData(t);
868
869
870 /* helper for below */
871 static void restore_ipokey(float *poin, float *old)
872 {
873         if(poin) {
874                 poin[0]= old[0];
875                 poin[-3]= old[3];
876                 poin[3]= old[6];
877         }
878 }
879
880 static void restoreElement(TransData *td) {
881         /* TransData for crease has no loc */
882         if (td->loc) {
883                 VECCOPY(td->loc, td->iloc);
884         }
885         if (td->val) {
886                 *td->val = td->ival;
887         }
888         if (td->ext && (td->flag&TD_NO_EXT)==0) {
889                 if (td->ext->rot) {
890                         VECCOPY(td->ext->rot, td->ext->irot);
891                 }
892                 if (td->ext->size) {
893                         VECCOPY(td->ext->size, td->ext->isize);
894                 }
895                 if(td->flag & TD_USEQUAT) {
896                         if (td->ext->quat) {
897                                 QUATCOPY(td->ext->quat, td->ext->iquat);
898                         }
899                 }
900         }
901         
902         if (td->flag & TD_BEZTRIPLE) {
903                 *(td->hdata->h1) = td->hdata->ih1;
904                 *(td->hdata->h2) = td->hdata->ih2;
905         }
906         
907         if(td->tdi) {
908                 TransDataIpokey *tdi= td->tdi;
909                 
910                 restore_ipokey(tdi->locx, tdi->oldloc);
911                 restore_ipokey(tdi->locy, tdi->oldloc+1);
912                 restore_ipokey(tdi->locz, tdi->oldloc+2);
913
914                 restore_ipokey(tdi->rotx, tdi->oldrot);
915                 restore_ipokey(tdi->roty, tdi->oldrot+1);
916                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
917                 
918                 restore_ipokey(tdi->sizex, tdi->oldsize);
919                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
920                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
921         }
922 }
923
924 void restoreTransObjects(TransInfo *t)
925 {
926         TransData *td;
927         
928         for (td = t->data; td < t->data + t->total; td++) {
929                 restoreElement(td);
930 #ifdef WITH_VERSE
931                 /* position of vertexes and object transformation matrix is sent
932                  * extra, becuase blender uses synchronous sending of vertexes
933                  * position as well object trans. matrix and it isn't possible to
934                  * send it in recalcData sometimes */
935                 if(td->flag & TD_VERSE_VERT) {
936                         if(td->verse) {
937                                 ((VerseVert*)td->verse)->flag |= VERT_POS_OBSOLETE;
938                         }
939                 }
940                 else if(td->flag & TD_VERSE_OBJECT)
941                         if(td->verse) {
942                                 struct VNode *vnode;
943                                 vnode = (VNode*)((Object*)td->verse)->vnode;
944                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
945                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
946                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
947                         }
948 #endif
949         }
950         
951         Mat3One(t->mat);
952         
953         recalcData(t);
954 }
955
956 void calculateCenter2D(TransInfo *t)
957 {
958         if (t->flag & (T_EDIT|T_POSE)) {
959                 Object *ob= t->obedit?t->obedit:t->poseobj;
960                 float vec[3];
961                 
962                 VECCOPY(vec, t->center);
963                 Mat4MulVecfl(ob->obmat, vec);
964                 projectIntView(t, vec, t->center2d);
965         }
966         else {
967                 projectIntView(t, t->center, t->center2d);
968         }
969 }
970
971 void calculateCenterCursor(TransInfo *t)
972 {
973         float *cursor;
974
975         cursor = give_cursor(t->scene, t->view);
976         VECCOPY(t->center, cursor);
977
978         /* If edit or pose mode, move cursor in local space */
979         if (t->flag & (T_EDIT|T_POSE)) {
980                 Object *ob = t->obedit?t->obedit:t->poseobj;
981                 float mat[3][3], imat[3][3];
982                 
983                 VecSubf(t->center, t->center, ob->obmat[3]);
984                 Mat3CpyMat4(mat, ob->obmat);
985                 Mat3Inv(imat, mat);
986                 Mat3MulVecfl(imat, t->center);
987         }
988         
989         calculateCenter2D(t);
990 }
991
992 void calculateCenterCursor2D(TransInfo *t)
993 {
994 #if 0 // TRANSFORM_FIX_ME
995         float aspx=1.0, aspy=1.0;
996         
997         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
998                 transform_aspect_ratio_tface_uv(&aspx, &aspy);
999         if (G.v2d) {
1000                 t->center[0] = G.v2d->cursor[0] * aspx; 
1001                 t->center[1] = G.v2d->cursor[1] * aspy; 
1002         }
1003 #endif
1004         calculateCenter2D(t);
1005 }
1006
1007 void calculateCenterMedian(TransInfo *t)
1008 {
1009         float partial[3] = {0.0f, 0.0f, 0.0f};
1010         int total = 0;
1011         int i;
1012         
1013         for(i = 0; i < t->total; i++) {
1014                 if (t->data[i].flag & TD_SELECTED) {
1015                         if (!(t->data[i].flag & TD_NOCENTER))
1016                         {
1017                                 VecAddf(partial, partial, t->data[i].center);
1018                                 total++;
1019                         }
1020                 }
1021                 else {
1022                         /* 
1023                            All the selected elements are at the head of the array 
1024                            which means we can stop when it finds unselected data
1025                         */
1026                         break;
1027                 }
1028         }
1029         if(i)
1030                 VecMulf(partial, 1.0f / total);
1031         VECCOPY(t->center, partial);
1032
1033         calculateCenter2D(t);
1034 }
1035
1036 void calculateCenterBound(TransInfo *t)
1037 {
1038         float max[3];
1039         float min[3];
1040         int i;
1041         for(i = 0; i < t->total; i++) {
1042                 if (i) {
1043                         if (t->data[i].flag & TD_SELECTED) {
1044                                 if (!(t->data[i].flag & TD_NOCENTER))
1045                                         MinMax3(min, max, t->data[i].center);
1046                         }
1047                         else {
1048                                 /* 
1049                                    All the selected elements are at the head of the array 
1050                                    which means we can stop when it finds unselected data
1051                                 */
1052                                 break;
1053                         }
1054                 }
1055                 else {
1056                         VECCOPY(max, t->data[i].center);
1057                         VECCOPY(min, t->data[i].center);
1058                 }
1059         }
1060         VecAddf(t->center, min, max);
1061         VecMulf(t->center, 0.5);
1062
1063         calculateCenter2D(t);
1064 }
1065
1066 void calculateCenter(TransInfo *t) 
1067 {
1068         switch(t->around) {
1069         case V3D_CENTER:
1070                 calculateCenterBound(t);
1071                 break;
1072         case V3D_CENTROID:
1073                 calculateCenterMedian(t);
1074                 break;
1075         case V3D_CURSOR:
1076                 if(t->spacetype==SPACE_IMAGE)
1077                         calculateCenterCursor2D(t);
1078                 else
1079                         calculateCenterCursor(t);
1080                 break;
1081         case V3D_LOCAL:
1082                 /* Individual element center uses median center for helpline and such */
1083                 calculateCenterMedian(t);
1084                 break;
1085         case V3D_ACTIVE:
1086                 {
1087                 /* set median, and if if if... do object center */
1088                 EditSelection ese;
1089                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1090
1091 #if 0 // TRANSFORM_FIX_ME
1092                 if (t->obedit && t->obedit->type == OB_MESH && EM_get_actSelection(&ese)) {
1093                         EM_editselection_center(t->center, &ese);
1094                         calculateCenter2D(t);
1095                         break;
1096                 } /* END EDIT MODE ACTIVE ELEMENT */
1097 #endif
1098                 
1099                 calculateCenterMedian(t);
1100                 if((t->flag & (T_EDIT|T_POSE))==0)
1101                 {
1102                         Scene *scene = t->scene;
1103                         Object *ob= OBACT;
1104                         if(ob)
1105                         {
1106                                 VECCOPY(t->center, ob->obmat[3]);
1107                                 projectIntView(t, t->center, t->center2d);
1108                         }
1109                 }
1110                 
1111                 }
1112         }
1113
1114         /* setting constraint center */
1115         VECCOPY(t->con.center, t->center);
1116         if(t->flag & (T_EDIT|T_POSE))
1117         {
1118                 Object *ob= t->obedit?t->obedit:t->poseobj;
1119                 Mat4MulVecfl(ob->obmat, t->con.center);
1120         }
1121
1122         /* voor panning from cameraview */
1123         if(t->flag & T_OBJECT)
1124         {
1125                 if(t->spacetype==SPACE_VIEW3D)
1126                 {
1127                         View3D *v3d = t->view;
1128                         Scene *scene = t->scene;
1129                         
1130                         if(v3d->camera == OBACT && v3d->persp==V3D_CAMOB)
1131                         {
1132                                 float axis[3];
1133                                 /* persinv is nasty, use viewinv instead, always right */
1134                                 VECCOPY(axis, t->viewinv[2]);
1135                                 Normalize(axis);
1136         
1137                                 /* 6.0 = 6 grid units */
1138                                 axis[0]= t->center[0]- 6.0f*axis[0];
1139                                 axis[1]= t->center[1]- 6.0f*axis[1];
1140                                 axis[2]= t->center[2]- 6.0f*axis[2];
1141                                 
1142                                 projectIntView(t, axis, t->center2d);
1143                                 
1144                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1145                                 if(t->mode==TFM_TRANSLATION)
1146                                 {
1147                                         VECCOPY(t->center, axis);
1148                                         VECCOPY(t->con.center, t->center);
1149                                 }
1150                         }
1151                 }
1152         }       
1153
1154         if(t->spacetype==SPACE_VIEW3D)
1155         {
1156                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d() */
1157                 if(t->flag & (T_EDIT|T_POSE)) {
1158                         Object *ob= t->obedit?t->obedit:t->poseobj;
1159                         float vec[3];
1160                         
1161                         VECCOPY(vec, t->center);
1162                         Mat4MulVecfl(ob->obmat, vec);
1163                         initgrabz(t->view, vec[0], vec[1], vec[2]);
1164                 }
1165                 else {
1166                         initgrabz(t->view, t->center[0], t->center[1], t->center[2]);
1167                 } 
1168         }
1169 }
1170
1171 void calculatePropRatio(TransInfo *t)
1172 {
1173         TransData *td = t->data;
1174         int i;
1175         float dist;
1176         short connected = t->flag & T_PROP_CONNECTED;
1177         
1178         if (t->flag & T_PROP_EDIT) {
1179                 for(i = 0 ; i < t->total; i++, td++) {
1180                         if (td->flag & TD_SELECTED) {
1181                                 td->factor = 1.0f;
1182                         }
1183                         else if ((connected && 
1184                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
1185                                 ||
1186                                         (connected == 0 &&
1187                                                 td->rdist > t->propsize)) {
1188                                 /* 
1189                                    The elements are sorted according to their dist member in the array,
1190                                    that means we can stop when it finds one element outside of the propsize.
1191                                 */
1192                                 td->flag |= TD_NOACTION;
1193                                 td->factor = 0.0f;
1194                                 restoreElement(td);
1195                         }
1196                         else {
1197                                 /* Use rdist for falloff calculations, it is the real distance */
1198                                 td->flag &= ~TD_NOACTION;
1199                                 dist= (t->propsize-td->rdist)/t->propsize;
1200                                 
1201                                 /*
1202                                  * Clamp to positive numbers.
1203                                  * Certain corner cases with connectivity and individual centers
1204                                  * can give values of rdist larger than propsize.
1205                                  */
1206                                 if (dist < 0.0f)
1207                                         dist = 0.0f;
1208                                 
1209                                 switch(G.scene->prop_mode) {
1210                                 case PROP_SHARP:
1211                                         td->factor= dist*dist;
1212                                         break;
1213                                 case PROP_SMOOTH:
1214                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
1215                                         break;
1216                                 case PROP_ROOT:
1217                                         td->factor = (float)sqrt(dist);
1218                                         break;
1219                                 case PROP_LIN:
1220                                         td->factor = dist;
1221                                         break;
1222                                 case PROP_CONST:
1223                                         td->factor = 1.0f;
1224                                         break;
1225                                 case PROP_SPHERE:
1226                                         td->factor = (float)sqrt(2*dist - dist * dist);
1227                                         break;
1228                                 case PROP_RANDOM:
1229                                         BLI_srand( BLI_rand() ); /* random seed */
1230                                         td->factor = BLI_frand()*dist;
1231                                         break;
1232                                 default:
1233                                         td->factor = 1;
1234                                 }
1235                         }
1236                 }
1237                 switch(G.scene->prop_mode) {
1238                 case PROP_SHARP:
1239                         strcpy(t->proptext, "(Sharp)");
1240                         break;
1241                 case PROP_SMOOTH:
1242                         strcpy(t->proptext, "(Smooth)");
1243                         break;
1244                 case PROP_ROOT:
1245                         strcpy(t->proptext, "(Root)");
1246                         break;
1247                 case PROP_LIN:
1248                         strcpy(t->proptext, "(Linear)");
1249                         break;
1250                 case PROP_CONST:
1251                         strcpy(t->proptext, "(Constant)");
1252                         break;
1253                 case PROP_SPHERE:
1254                         strcpy(t->proptext, "(Sphere)");
1255                         break;
1256                 case PROP_RANDOM:
1257                         strcpy(t->proptext, "(Random)");
1258                         break;
1259                 default:
1260                         strcpy(t->proptext, "");
1261                 }
1262         }
1263         else {
1264                 for(i = 0 ; i < t->total; i++, td++) {
1265                         td->factor = 1.0;
1266                 }
1267                 strcpy(t->proptext, "");
1268         }
1269 }
1270
1271 TransInfo *BIF_GetTransInfo()
1272 {
1273         return NULL;
1274 }
1275
1276 float get_drawsize(View3D *v3d, ScrArea *sa, float *co)
1277 {
1278         float size, vec[3], len1, len2;
1279         
1280         /* size calculus, depending ortho/persp settings, like initgrabz() */
1281         size= v3d->persmat[0][3]*co[0]+ v3d->persmat[1][3]*co[1]+ v3d->persmat[2][3]*co[2]+ v3d->persmat[3][3];
1282         
1283         VECCOPY(vec, v3d->persinv[0]);
1284         len1= Normalize(vec);
1285         VECCOPY(vec, v3d->persinv[1]);
1286         len2= Normalize(vec);
1287         
1288         size*= 0.01f*(len1>len2?len1:len2);
1289
1290         /* correct for window size to make widgets appear fixed size */
1291         if(sa->winx > sa->winy) size*= 1000.0f/(float)sa->winx;
1292         else size*= 1000.0f/(float)sa->winy;
1293
1294         return size;
1295 }