Fix T42444: Neg scale rotated object flips 2x axis
[blender.git] / source / blender / editors / transform / transform.c
index c7ff9f0089fef24bee805c903cc33d22a41161da..d704f62654baaaab0591de3328ef63213863be63 100644 (file)
@@ -271,7 +271,8 @@ void convertViewVec(TransInfo *t, float r_vec[3], int dx, int dy)
                        r_vec[0] = dx;
                        r_vec[1] = dy;
                }
-               else {  const float mval_f[2] = {(float)dx, (float)dy};
+               else {
+                       const float mval_f[2] = {(float)dx, (float)dy};
                        ED_view3d_win_to_delta(t->ar, mval_f, r_vec, t->zfac);
                }
        }
@@ -1124,47 +1125,22 @@ int transformEvent(TransInfo *t, const wmEvent *event)
                                handled = true;
                                break;
                        case TFM_MODAL_AXIS_X:
-                               if ((t->flag & T_NO_CONSTRAINT) == 0) {
-                                       if (cmode == 'X') {
-                                               stopConstraint(t);
-                                       }
-                                       else {
-                                               if (t->flag & T_2D_EDIT) {
-                                                       setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), IFACE_("along X"));
-                                               }
-                                               else {
-                                                       setUserConstraint(t, t->current_orientation, (CON_AXIS0), IFACE_("along %s X"));
-                                               }
-                                       }
+                               if (!(t->flag & T_NO_CONSTRAINT)) {
+                                       transform_event_xyz_constraint(t, XKEY, cmode);
                                        t->redraw |= TREDRAW_HARD;
                                        handled = true;
                                }
                                break;
                        case TFM_MODAL_AXIS_Y:
                                if ((t->flag & T_NO_CONSTRAINT) == 0) {
-                                       if (cmode == 'Y') {
-                                               stopConstraint(t);
-                                       }
-                                       else {
-                                               if (t->flag & T_2D_EDIT) {
-                                                       setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), IFACE_("along Y"));
-                                               }
-                                               else {
-                                                       setUserConstraint(t, t->current_orientation, (CON_AXIS1), IFACE_("along %s Y"));
-                                               }
-                                       }
+                                       transform_event_xyz_constraint(t, YKEY, cmode);
                                        t->redraw |= TREDRAW_HARD;
                                        handled = true;
                                }
                                break;
                        case TFM_MODAL_AXIS_Z:
-                               if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
-                                       if (cmode == 'Z') {
-                                               stopConstraint(t);
-                                       }
-                                       else {
-                                               setUserConstraint(t, t->current_orientation, (CON_AXIS2), IFACE_("along %s Z"));
-                                       }
+                               if ((t->flag & (T_NO_CONSTRAINT)) == 0) {
+                                       transform_event_xyz_constraint(t, ZKEY, cmode);
                                        t->redraw |= TREDRAW_HARD;
                                        handled = true;
                                }
@@ -3183,38 +3159,23 @@ static void headerResize(TransInfo *t, float vec[3], char str[MAX_INFO_LEN])
        }
 }
 
-/* FLT_EPSILON is too small [#29633], 0.0000001f starts to flip */
-#define TX_FLIP_EPS 0.00001f
-BLI_INLINE int tx_sign(const float a)
-{
-       return (a < -TX_FLIP_EPS ? 1 : a > TX_FLIP_EPS ? 2 : 3);
-}
-BLI_INLINE int tx_vec_sign_flip(const float a[3], const float b[3])
-{
-       return ((tx_sign(a[0]) & tx_sign(b[0])) == 0 ||
-               (tx_sign(a[1]) & tx_sign(b[1])) == 0 ||
-               (tx_sign(a[2]) & tx_sign(b[2])) == 0);
-}
-
-/* smat is reference matrix, only scaled */
+/**
+ * \a smat is reference matrix only.
+ *
+ * \note this is a tricky area, before making changes see: T29633, T42444
+ */
 static void TransMat3ToSize(float mat[3][3], float smat[3][3], float size[3])
 {
-       float vec[3];
-       
-       copy_v3_v3(vec, mat[0]);
-       size[0] = normalize_v3(vec);
-       copy_v3_v3(vec, mat[1]);
-       size[1] = normalize_v3(vec);
-       copy_v3_v3(vec, mat[2]);
-       size[2] = normalize_v3(vec);
-       
+       float rmat[3][3];
+
+       mat3_to_rot_size(rmat, size, mat);
+
        /* first tried with dotproduct... but the sign flip is crucial */
-       if (tx_vec_sign_flip(mat[0], smat[0]) ) size[0] = -size[0];
-       if (tx_vec_sign_flip(mat[1], smat[1]) ) size[1] = -size[1];
-       if (tx_vec_sign_flip(mat[2], smat[2]) ) size[2] = -size[2];
+       if (dot_v3v3(rmat[0], smat[0]) < 0.0f) size[0] = -size[0];
+       if (dot_v3v3(rmat[1], smat[1]) < 0.0f) size[1] = -size[1];
+       if (dot_v3v3(rmat[2], smat[2]) < 0.0f) size[2] = -size[2];
 }
 
-
 static void ElementResize(TransInfo *t, TransData *td, float mat[3][3])
 {
        float tmat[3][3], smat[3][3], center[3];