Transform: internal change, simplify view angle checks
authorCampbell Barton <ideasman42@gmail.com>
Wed, 12 Mar 2014 06:52:07 +0000 (17:52 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 12 Mar 2014 07:30:38 +0000 (18:30 +1100)
source/blender/editors/transform/transform_manipulator.c
source/blender/makesdna/DNA_view3d_types.h

index 861a35c2252ceef85d3eb233057d900474649d80..845e3cfe349acdb18ae5c73730c501be3bab3421 100644 (file)
 #define MAN_GHOST   1
 #define MAN_MOVECOL 2
 
+/* threshold for testing view aligned manipulator axis */
+#define TW_AXIS_DOT_MIN 0.02f
+#define TW_AXIS_DOT_MAX 0.1f
+
 /* transform widget center calc helper for below */
 static void calc_tw_center(Scene *scene, const float co[3])
 {
@@ -658,36 +662,23 @@ int calc_manipulator_stats(const bContext *C)
 static void test_manipulator_axis(const bContext *C)
 {
        RegionView3D *rv3d = CTX_wm_region_view3d(C);
-       float angle;
-       float vec[3];
+       float view_vec[3], axis_vec[3];
+       float idot;
+       int i;
 
-       ED_view3d_global_to_vector(rv3d, rv3d->twmat[3], vec);
+       const int twdrawflag_axis[3] = {
+           (MAN_TRANS_X | MAN_SCALE_X),
+           (MAN_TRANS_Y | MAN_SCALE_Y),
+           (MAN_TRANS_Z | MAN_SCALE_Z)};
 
-       angle = fabsf(angle_v3v3(rv3d->twmat[0], vec));
-       if (angle > (float)M_PI / 2.0f) {
-               angle = (float)M_PI - angle;
-       }
-       angle = rv3d->twangle[0] = RAD2DEGF(angle);
-       if (angle < 5.0f) {
-               rv3d->twdrawflag &= ~(MAN_TRANS_X | MAN_SCALE_X);
-       }
-
-       angle = fabsf(angle_v3v3(rv3d->twmat[1], vec));
-       if (angle > (float)M_PI / 2.0f) {
-               angle = (float)M_PI - angle;
-       }
-       angle = rv3d->twangle[1] = RAD2DEGF(angle);
-       if (angle < 5.0f) {
-               rv3d->twdrawflag &= ~(MAN_TRANS_Y | MAN_SCALE_Y);
-       }
+       ED_view3d_global_to_vector(rv3d, rv3d->twmat[3], view_vec);
 
-       angle = fabsf(angle_v3v3(rv3d->twmat[2], vec));
-       if (angle > (float)M_PI / 2.0f) {
-               angle = (float)M_PI - angle;
-       }
-       angle = rv3d->twangle[2] = RAD2DEGF(angle);
-       if (angle < 5.0f) {
-               rv3d->twdrawflag &= ~(MAN_TRANS_Z | MAN_SCALE_Z);
+       for (i = 0; i < 3; i++) {
+               normalize_v3_v3(axis_vec, rv3d->twmat[i]);
+               rv3d->tw_idot[i] = idot = 1.0f - fabsf(dot_v3v3(view_vec, axis_vec));
+               if (idot < TW_AXIS_DOT_MIN) {
+                       rv3d->twdrawflag &= ~twdrawflag_axis[i];
+               }
        }
 }
 
@@ -789,15 +780,17 @@ static void partial_doughnut(float radring, float radhole, int start, int end, i
        }
 }
 
-static char axisBlendAngle(float angle)
+static char axisBlendAngle(float idot)
 {
-       if (angle > 20)
+       if (idot > TW_AXIS_DOT_MAX) {
                return 255;
-
-       if (angle < 5)
+       }
+       else if (idot < TW_AXIS_DOT_MIN) {
                return 0;
-
-       return (char)(255.0f * (angle - 5) / 15.0f);
+       }
+       else {
+               return (char)(255.0f * (idot - TW_AXIS_DOT_MIN) / (TW_AXIS_DOT_MAX - TW_AXIS_DOT_MIN));
+       }
 }
 
 /* three colors can be set:
@@ -877,7 +870,7 @@ static void draw_manipulator_axes_single(View3D *v3d, RegionView3D *rv3d, int co
                                        else if (flagx & MAN_TRANS_X) glLoadName(MAN_TRANS_X);
                                }
                                else {
-                                       manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->twangle[0]));
+                                       manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->tw_idot[0]));
                                }
                                glBegin(GL_LINES);
                                glVertex3f(0.2f, 0.0f, 0.0f);
@@ -892,7 +885,7 @@ static void draw_manipulator_axes_single(View3D *v3d, RegionView3D *rv3d, int co
                                        else if (flagy & MAN_TRANS_Y) glLoadName(MAN_TRANS_Y);
                                }
                                else {
-                                       manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->twangle[1]));
+                                       manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->tw_idot[1]));
                                }
                                glBegin(GL_LINES);
                                glVertex3f(0.0f, 0.2f, 0.0f);
@@ -907,7 +900,7 @@ static void draw_manipulator_axes_single(View3D *v3d, RegionView3D *rv3d, int co
                                        else if (flagz & MAN_TRANS_Z) glLoadName(MAN_TRANS_Z);
                                }
                                else {
-                                       manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->twangle[2]));
+                                       manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->tw_idot[2]));
                                }
                                glBegin(GL_LINES);
                                glVertex3f(0.0f, 0.0f, 0.2f);
@@ -1320,7 +1313,7 @@ static void draw_manipulator_scale(
                                if (drawflags & MAN_SCALE_X) {
                                        glTranslatef(dz, 0.0, 0.0);
                                        if (is_picksel) glLoadName(MAN_SCALE_X);
-                                       else manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->twangle[0]));
+                                       else manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->tw_idot[0]));
                                        drawsolidcube(cusize);
                                        glTranslatef(-dz, 0.0, 0.0);
                                }
@@ -1329,7 +1322,7 @@ static void draw_manipulator_scale(
                                if (drawflags & MAN_SCALE_Y) {
                                        glTranslatef(0.0, dz, 0.0);
                                        if (is_picksel) glLoadName(MAN_SCALE_Y);
-                                       else manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->twangle[1]));
+                                       else manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->tw_idot[1]));
                                        drawsolidcube(cusize);
                                        glTranslatef(0.0, -dz, 0.0);
                                }
@@ -1338,7 +1331,7 @@ static void draw_manipulator_scale(
                                if (drawflags & MAN_SCALE_Z) {
                                        glTranslatef(0.0, 0.0, dz);
                                        if (is_picksel) glLoadName(MAN_SCALE_Z);
-                                       else manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->twangle[2]));
+                                       else manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->tw_idot[2]));
                                        drawsolidcube(cusize);
                                        glTranslatef(0.0, 0.0, -dz);
                                }
@@ -1451,7 +1444,7 @@ static void draw_manipulator_translate(
                                if (drawflags & MAN_TRANS_Z) {
                                        glTranslatef(0.0, 0.0, dz);
                                        if (is_picksel) glLoadName(MAN_TRANS_Z);
-                                       else manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->twangle[2]));
+                                       else manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->tw_idot[2]));
                                        draw_cone(qobj, cylen, cywid);
                                        glTranslatef(0.0, 0.0, -dz);
                                }
@@ -1460,7 +1453,7 @@ static void draw_manipulator_translate(
                                if (drawflags & MAN_TRANS_X) {
                                        glTranslatef(dz, 0.0, 0.0);
                                        if (is_picksel) glLoadName(MAN_TRANS_X);
-                                       else manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->twangle[0]));
+                                       else manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->tw_idot[0]));
                                        glRotatef(90.0, 0.0, 1.0, 0.0);
                                        draw_cone(qobj, cylen, cywid);
                                        glRotatef(-90.0, 0.0, 1.0, 0.0);
@@ -1471,7 +1464,7 @@ static void draw_manipulator_translate(
                                if (drawflags & MAN_TRANS_Y) {
                                        glTranslatef(0.0, dz, 0.0);
                                        if (is_picksel) glLoadName(MAN_TRANS_Y);
-                                       else manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->twangle[1]));
+                                       else manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->tw_idot[1]));
                                        glRotatef(-90.0, 1.0, 0.0, 0.0);
                                        draw_cone(qobj, cylen, cywid);
                                        glRotatef(90.0, 1.0, 0.0, 0.0);
index a154f6fed15eeca56d79e831140b08cbcaa2a011..f434a5ac75494d4475c0fe5db46281954c089652 100644 (file)
@@ -142,7 +142,7 @@ typedef struct RegionView3D {
        short lpersp, lview; /* lpersp can never be set to 'RV3D_CAMOB' */
 
        float gridview;
-       float twangle[3];
+       float tw_idot[3];  /* manipulator runtime: (1 - dot) product with view vector (used to check view alignment) */
 
 
        /* active rotation from NDOF or elsewhere */