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