4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 #include "MEM_guardedalloc.h"
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51 #include "DNA_view3d_types.h"
53 #include "BIF_screen.h"
54 #include "BIF_resources.h"
55 #include "BIF_mywindow.h"
57 #include "BIF_editarmature.h"
58 #include "BIF_editmesh.h"
59 #include "BIF_editsima.h"
60 #include "BIF_meshtools.h"
62 #include "BKE_action.h"
64 #include "BKE_armature.h"
65 #include "BKE_curve.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_displist.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_global.h"
71 #include "BKE_lattice.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_utildefines.h"
78 #include "BDR_unwrapper.h"
80 #include "BLI_arithb.h"
81 #include "BLI_blenlib.h"
82 #include "BLI_editVert.h"
88 #include "transform.h"
90 extern ListBase editNurb;
91 extern ListBase editelems;
93 extern TransInfo Trans; /* From transform.c */
95 /* ************************** Functions *************************** */
98 void getViewVector(float coord[3], float vec[3])
100 TransInfo *t = BIF_GetTransInfo();
110 Mat4MulVec4fl(t->viewmat, p2);
112 p2[0] = 2.0f * p2[0];
113 p2[1] = 2.0f * p2[1];
114 p2[2] = 2.0f * p2[2];
116 Mat4MulVec4fl(t->viewinv, p2);
118 VecSubf(vec, p1, p2);
121 VECCOPY(vec, t->viewinv[2]);
126 /* ************************** GENERICS **************************** */
128 static void clipMirrorModifier(TransInfo *t, Object *ob)
130 ModifierData *md= ob->modifiers.first;
134 for (; md; md=md->next) {
135 if (md->type==eModifierType_Mirror) {
136 MirrorModifierData *mmd = (MirrorModifierData*) md;
138 if(mmd->flag & MOD_MIR_CLIPPING) {
142 tolerance[0] = mmd->tolerance;
146 tolerance[1] = mmd->tolerance;
150 tolerance[2] = mmd->tolerance;
156 TransData *td = t->data;
159 for(i = 0 ; i < t->total; i++, td++) {
160 if (td->flag & TD_NOACTION)
166 if(fabs(td->iloc[0])<=tolerance[0] || td->loc[0]*td->iloc[0]<0.0f) td->loc[0]= 0.0f;
170 if(fabs(td->iloc[1])<=tolerance[1] || td->loc[1]*td->iloc[1]<0.0f) td->loc[1]= 0.0f;
173 if(fabs(td->iloc[2])<=tolerance[2] || td->loc[2]*td->iloc[2]<0.0f) td->loc[2]= 0.0f;
179 /* assumes G.obedit set to mesh object */
180 static void editmesh_apply_to_mirror(TransInfo *t)
182 TransData *td = t->data;
186 for(i = 0 ; i < t->total; i++, td++) {
187 if (td->flag & TD_NOACTION)
194 eve->co[0]= -td->loc[0];
195 eve->co[1]= td->loc[1];
196 eve->co[2]= td->loc[2];
201 /* called for updating while transform acts, once per redraw */
202 void recalcData(TransInfo *t)
207 if (G.obedit->type == OB_MESH) {
209 /* mirror modifier clipping? */
210 if(t->state != TRANS_CANCEL)
211 clipMirrorModifier(t, G.obedit);
213 if(G.scene->toolsettings->editbutflag & B_MESH_X_MIRROR)
214 editmesh_apply_to_mirror(t);
216 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
218 recalc_editnormals();
220 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
221 Nurb *nu= editNurb.first;
222 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
226 testhandlesNurb(nu); /* test for bezier too */
230 else if(G.obedit->type==OB_ARMATURE){ /* no recalc flag, does pose */
231 bArmature *arm= G.obedit->data;
234 /* Ensure all bones are correctly adjusted */
235 for (ebo=G.edbo.first; ebo; ebo=ebo->next){
237 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
238 /* If this bone has a parent tip that has been moved */
239 if (ebo->parent->flag & BONE_TIPSEL){
240 VECCOPY (ebo->head, ebo->parent->tail);
241 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
243 /* If this bone has a parent tip that has NOT been moved */
245 VECCOPY (ebo->parent->tail, ebo->head);
246 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
250 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
251 ebo->length= VecLenf(ebo->head, ebo->tail);
252 if(ebo->oldlength==0.0f) {
253 ebo->rad_head= 0.25f*ebo->length;
254 ebo->rad_tail= 0.10f*ebo->length;
255 ebo->dist= 0.25f*ebo->length;
257 if(ebo->rad_head > ebo->parent->rad_tail)
258 ebo->rad_head= ebo->parent->rad_tail;
261 else if(t->mode!=TFM_BONE_ENVELOPE) {
262 /* if bones change length, lets do that for the deform distance as well */
263 ebo->dist*= ebo->length/ebo->oldlength;
264 ebo->rad_head*= ebo->length/ebo->oldlength;
265 ebo->rad_tail*= ebo->length/ebo->oldlength;
266 ebo->oldlength= ebo->length;
269 if(arm->flag & ARM_MIRROR_EDIT)
270 transform_armature_mirror_update();
273 else if(G.obedit->type==OB_LATTICE) {
274 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
276 if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
279 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); /* sets recalc flags */
282 else if( (t->flag & T_POSE) && t->poseobj) {
283 Object *ob= t->poseobj;
284 bArmature *arm= ob->data;
286 /* old optimize trick... this enforces to bypass the depgraph */
287 if (!(arm->flag & ARM_DELAYDEFORM)) {
288 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA); /* sets recalc flags */
290 /* bah, softbody exception... recalcdata doesnt reset */
291 for(base= FIRSTBASE; base; base= base->next) {
292 if(base->object->recalc & OB_RECALC_DATA)
293 if(modifiers_isSoftbodyEnabled(base->object)) {
294 base->object->softflag |= OB_SB_REDO;
301 else if(t->spacetype==SPACE_IMAGE) {
303 if (G.sima->flag & SI_LIVE_UNWRAP)
304 unwrap_lscm_live_re_solve();
307 for(base= FIRSTBASE; base; base= base->next) {
308 /* this flag is from depgraph, was stored in nitialize phase, handled in drawview.c */
309 if(base->flag & BA_HAS_RECALC_OB)
310 base->object->recalc |= OB_RECALC_OB;
311 if(base->flag & BA_HAS_RECALC_DATA)
312 base->object->recalc |= OB_RECALC_DATA;
314 /* thanks to ob->ctime usage, ipos are not called in where_is_object,
315 unless we edit ipokeys */
316 if(base->flag & BA_DO_IPO) {
317 if(base->object->ipo) {
320 base->object->ctime= -1234567.0;
322 icu= base->object->ipo->curve.first;
324 calchandles_ipocurve(icu);
330 /* softbody exception */
331 if(modifiers_isSoftbodyEnabled(base->object)) {
332 if(base->object->recalc & OB_RECALC_DATA)
333 base->object->softflag |= OB_SB_REDO;
338 /* update shaded drawmode while transform */
339 if(t->spacetype==SPACE_VIEW3D && G.vd->drawtype == OB_SHADED)
340 reshadeall_displist();
344 void initTransModeFlags(TransInfo *t, int mode)
349 /* REMOVING RESTRICTIONS FLAGS */
350 t->flag &= ~T_ALL_RESTRICTIONS;
354 t->flag |= T_NULL_ONE;
355 t->num.flag |= NUM_NULL_ONE;
356 t->num.flag |= NUM_AFFECT_ALL;
358 t->flag |= T_NO_ZERO;
359 t->num.flag |= NUM_NO_ZERO;
363 t->num.flag |= NUM_NULL_ONE;
364 t->num.flag |= NUM_NO_NEGATIVE;
365 t->flag |= T_NO_CONSTRAINT;
369 case TFM_BONE_ENVELOPE:
370 t->flag |= T_NO_CONSTRAINT;
375 void drawLine(float *center, float *dir, char axis, short options)
377 extern void make_axis_color(char *col, char *col2, char axis); // drawview.c
378 float v1[3], v2[3], v3[3];
379 char col[3], col2[3];
381 //if(G.obedit) mymultmatrix(G.obedit->obmat); // sets opengl viewing
384 VecMulf(v3, G.vd->far);
386 VecSubf(v2, center, v3);
387 VecAddf(v1, center, v3);
389 if (options & DRAWLIGHT) {
390 col[0] = col[1] = col[2] = 220;
393 BIF_GetThemeColor3ubv(TH_GRID, col);
395 make_axis_color(col, col2, axis);
399 glBegin(GL_LINE_STRIP);
404 myloadmatrix(G.vd->viewmat);
407 void initTrans (TransInfo *t)
410 /* moving: is shown in drawobject() (transform color) */
411 if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
412 else G.moving= G_TRANSFORM_OBJ;
419 /* setting PET flag */
420 if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
421 t->flag |= T_PROP_EDIT;
422 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED; // yes i know, has to become define
425 getmouseco_areawin(t->imval);
426 t->con.imval[0] = t->imval[0];
427 t->con.imval[1] = t->imval[1];
442 t->num.val[2] = 0.0f;
450 t->spacetype = curarea->spacetype;
451 if(t->spacetype==SPACE_VIEW3D) {
452 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
453 t->around = G.vd->around;
456 t->around = V3D_CENTRE;
458 setTransformViewMatrices(t);
461 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
462 void postTrans (TransInfo *t)
464 G.moving = 0; // Set moving flag off (display as usual)
468 /* postTrans can be called when nothing is selected, so data is NULL already */
473 /* since ipokeys are optional on objects, we mallocced them per trans-data */
474 for(a=0, td= t->data; a<t->total; a++, td++) {
475 if(td->tdi) MEM_freeN(td->tdi);
480 if (t->ext) MEM_freeN(t->ext);
482 MEM_freeN(t->data2d);
486 if(t->spacetype==SPACE_IMAGE) {
487 if (G.sima->flag & SI_LIVE_UNWRAP)
488 unwrap_lscm_live_end(t->state == TRANS_CANCEL);
492 static void apply_grid3(TransInfo *t, float *val, int max_index, float fac1, float fac2, float fac3)
494 /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
500 if(t->mode==TFM_ROTATION || t->mode==TFM_WARP || t->mode==TFM_TILT || t->mode==TFM_TRACKBALL)
501 invert = U.flag & USER_AUTOROTGRID;
502 else if(t->mode==TFM_RESIZE || t->mode==TFM_SHEAR || t->mode==TFM_BONESIZE || t->mode==TFM_SHRINKFATTEN)
503 invert = U.flag & USER_AUTOSIZEGRID;
505 invert = U.flag & USER_AUTOGRABGRID;
507 for (i=0; i<=max_index; i++) {
509 /* evil hack - snapping needs to be adapted for image aspect ratio */
510 if((t->spacetype==SPACE_IMAGE) && (t->mode==TFM_TRANSLATION)) {
512 transform_aspect_ratio_tface_uv(&aspx, &aspy);
518 if(G.qual & LR_CTRLKEY) ctrl= 0;
521 else ctrl= (G.qual & LR_CTRLKEY);
523 if(ctrl && (G.qual & LR_SHIFTKEY)) {
525 for (i=0; i<=max_index; i++) {
526 val[i]= fac3*asp*(float)floor(val[i]/(fac3*asp) +.5);
532 for (i=0; i<=max_index; i++) {
533 val[i]= fac2*asp*(float)floor(val[i]/(fac2*asp) +.5);
539 for (i=0; i<=max_index; i++) {
540 val[i]= fac1*asp*(float)floor(val[i]/(fac1*asp) +.5);
547 void snapGrid(TransInfo *t, float *val) {
548 apply_grid3(t, val, t->idx_max, t->snap[0], t->snap[1], t->snap[2]);
551 void applyTransObjects(TransInfo *t)
555 for (td = t->data; td < t->data + t->total; td++) {
556 VECCOPY(td->iloc, td->loc);
558 VECCOPY(td->ext->irot, td->ext->rot);
561 VECCOPY(td->ext->isize, td->ext->size);
567 /* helper for below */
568 static void restore_ipokey(float *poin, float *old)
577 static void restoreElement(TransData *td) {
578 /* TransData for crease has no loc */
580 VECCOPY(td->loc, td->iloc);
587 VECCOPY(td->ext->rot, td->ext->irot);
590 VECCOPY(td->ext->size, td->ext->isize);
592 if(td->flag & TD_USEQUAT) {
594 QUATCOPY(td->ext->quat, td->ext->iquat);
599 TransDataIpokey *tdi= td->tdi;
601 restore_ipokey(tdi->locx, tdi->oldloc);
602 restore_ipokey(tdi->locy, tdi->oldloc+1);
603 restore_ipokey(tdi->locz, tdi->oldloc+2);
605 restore_ipokey(tdi->rotx, tdi->oldrot);
606 restore_ipokey(tdi->roty, tdi->oldrot+1);
607 restore_ipokey(tdi->rotz, tdi->oldrot+2);
609 restore_ipokey(tdi->sizex, tdi->oldsize);
610 restore_ipokey(tdi->sizey, tdi->oldsize+1);
611 restore_ipokey(tdi->sizez, tdi->oldsize+2);
615 void restoreTransObjects(TransInfo *t)
619 for (td = t->data; td < t->data + t->total; td++) {
625 void calculateCenter2D(TransInfo *t)
627 if (t->flag & (T_EDIT|T_POSE)) {
628 Object *ob= G.obedit?G.obedit:t->poseobj;
631 VECCOPY(vec, t->center);
632 Mat4MulVecfl(ob->obmat, vec);
633 projectIntView(t, vec, t->center2d);
636 projectIntView(t, t->center, t->center2d);
640 void calculateCenterCursor(TransInfo *t)
644 cursor = give_cursor();
645 VECCOPY(t->center, cursor);
647 /* If edit or pose mode, move cursor in local space */
648 if(t->flag & (T_EDIT|T_POSE)) {
649 Object *ob= G.obedit?G.obedit:t->poseobj;
650 float mat[3][3], imat[3][3];
652 VecSubf(t->center, t->center, ob->obmat[3]);
653 Mat3CpyMat4(mat, ob->obmat);
655 Mat3MulVecfl(imat, t->center);
658 calculateCenter2D(t);
661 void calculateCenterMedian(TransInfo *t)
663 float partial[3] = {0.0f, 0.0f, 0.0f};
665 for(i = 0; i < t->total; i++) {
666 if (t->data[i].flag & TD_SELECTED) {
667 VecAddf(partial, partial, t->data[i].center);
671 All the selected elements are at the head of the array
672 which means we can stop when it finds unselected data
677 VecMulf(partial, 1.0f / i);
678 VECCOPY(t->center, partial);
680 calculateCenter2D(t);
683 void calculateCenterBound(TransInfo *t)
688 for(i = 0; i < t->total; i++) {
690 if (t->data[i].flag & TD_SELECTED) {
691 MinMax3(min, max, t->data[i].center);
695 All the selected elements are at the head of the array
696 which means we can stop when it finds unselected data
702 VECCOPY(max, t->data[i].center);
703 VECCOPY(min, t->data[i].center);
706 VecAddf(t->center, min, max);
707 VecMulf(t->center, 0.5);
709 calculateCenter2D(t);
712 void calculateCenter(TransInfo *t)
716 calculateCenterBound(t);
719 calculateCenterMedian(t);
722 calculateCenterCursor(t);
725 /* Individual element center uses median center for helpline and such */
726 calculateCenterMedian(t);
729 /* set median, and if if if... do object center */
731 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
732 if (G.obedit && G.obedit->type == OB_MESH && G.editMesh->selected.last) {
733 EM_editselection_center(t->center, G.editMesh->selected.last);
734 calculateCenter2D(t);
736 } /* END EDIT MODE ACTIVE ELEMENT */
738 calculateCenterMedian(t);
739 if((t->flag & (T_EDIT|T_POSE))==0) {
742 VECCOPY(t->center, ob->obmat[3]);
743 projectIntView(t, t->center, t->center2d);
748 /* setting constraint center */
749 VECCOPY(t->con.center, t->center);
750 if(t->flag & (T_EDIT|T_POSE)) {
751 Object *ob= G.obedit?G.obedit:t->poseobj;
752 Mat4MulVecfl(ob->obmat, t->con.center);
755 /* voor panning from cameraview */
756 if(t->flag & T_OBJECT) {
757 if( G.vd->camera==OBACT && G.vd->persp>1) {
759 /* persinv is nasty, use viewinv instead, always right */
760 VECCOPY(axis, t->viewinv[2]);
763 /* 6.0 = 6 grid units */
764 axis[0]= t->center[0]- 6.0f*axis[0];
765 axis[1]= t->center[1]- 6.0f*axis[1];
766 axis[2]= t->center[2]- 6.0f*axis[2];
768 projectIntView(t, axis, t->center2d);
770 /* rotate only needs correct 2d center, grab needs initgrabz() value */
771 if(t->mode==TFM_TRANSLATION) {
772 VECCOPY(t->center, axis);
773 VECCOPY(t->con.center, t->center);
778 if(t->spacetype==SPACE_VIEW3D)
779 initgrabz(t->center[0], t->center[1], t->center[2]);
782 void calculatePropRatio(TransInfo *t)
784 TransData *td = t->data;
787 short connected = t->flag & T_PROP_CONNECTED;
789 if (t->flag & T_PROP_EDIT) {
790 for(i = 0 ; i < t->total; i++, td++) {
791 if (td->flag & TD_SELECTED) {
794 else if ((connected &&
795 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
798 td->rdist > t->propsize)) {
800 The elements are sorted according to their dist member in the array,
801 that means we can stop when it finds one element outside of the propsize.
803 td->flag |= TD_NOACTION;
808 /* Use rdist for falloff calculations, it is the real distance */
809 td->flag &= ~TD_NOACTION;
810 dist= (t->propsize-td->rdist)/t->propsize;
811 switch(G.scene->prop_mode) {
813 td->factor= dist*dist;
816 td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
819 td->factor = (float)sqrt(dist);
828 td->factor = (float)sqrt(2*dist - dist * dist);
835 switch(G.scene->prop_mode) {
837 strcpy(t->proptext, "(Sharp)");
840 strcpy(t->proptext, "(Smooth)");
843 strcpy(t->proptext, "(Root)");
846 strcpy(t->proptext, "(Linear)");
849 strcpy(t->proptext, "(Constant)");
852 strcpy(t->proptext, "(Sphere)");
855 strcpy(t->proptext, "");
859 for(i = 0 ; i < t->total; i++, td++) {
862 strcpy(t->proptext, "");
866 TransInfo * BIF_GetTransInfo() {