Commit: b014f0307772143694ab05098fd22619c6b31080
Author: Germano Cavalcante
Date:   Thu Jul 14 00:02:21 2016 -0300
Branches: master
https://developer.blender.org/rBb014f0307772143694ab05098fd22619c6b31080

Simplify snapping functions

from D2104: reference all repeated (and strange) equations (example: 
mul_m4_m4m4(pmat_local, pmat, obmat)) in the function `precalc_project.

This is useful for maintenance.

===================================================================

M       source/blender/editors/transform/transform_snap_object.c

===================================================================

diff --git a/source/blender/editors/transform/transform_snap_object.c 
b/source/blender/editors/transform/transform_snap_object.c
index 7aed7f9..dc7f5e9 100644
--- a/source/blender/editors/transform/transform_snap_object.c
+++ b/source/blender/editors/transform/transform_snap_object.c
@@ -230,6 +230,27 @@ typedef struct BVHTreeFromMeshType {
        char type;
 } BVHTreeFromMeshType;
 
+typedef struct PreDefProject {
+       float pmat[4][4]; /* perspective matrix multiplied by object matrix */
+       float win_half[2];
+       float dist_px_sq;
+} PreDefProject;
+
+static void precalc_project(PreDefProject *projectdefs, const ARegion *ar,
+        const float dist_px, float obmat[4][4])
+{
+       float (*pmat)[4] = ((RegionView3D *)ar->regiondata)->persmat;
+       if (obmat) {
+               mul_m4_m4m4(projectdefs->pmat, pmat, obmat);
+       }
+       else {
+               copy_m4_m4(projectdefs->pmat, pmat);
+       }
+       projectdefs->win_half[0] = ar->winx / 2;
+       projectdefs->win_half[1] = ar->winy / 2;
+       projectdefs->dist_px_sq = SQUARE(dist_px);
+}
+
 /**
 * From a threshold (maximum distance to snap in pixels) returns:
 *
@@ -391,21 +412,23 @@ static bool test_edge_dist(
 #undef V3_MUL_ELEM
 
 static bool test_projected_vert_dist(
-        float pmat_local[4][4], const float co[3], const bool is_persp,
-        const float mval[2], const float depth_range[2], const float 
win_half[2], float *dist_px_sq,
+        PreDefProject *projectdefs,
+        const float co[3], const bool is_persp,
+        const float mval[2], const float depth_range[2],
         float r_co[3])
 {
        float depth;
+       float(*pmat)[4] = projectdefs->pmat;
        if (is_persp) {
-               depth = mul_project_m4_v3_zfac(pmat_local, co);
+               depth = mul_project_m4_v3_zfac(pmat, co);
                if (depth < depth_range[0] || depth > depth_range[1]) {
                        return false;
                }
        }
 
        float co2d[2] = {
-               (dot_m4_v3_row_x(pmat_local, co) + pmat_local[3][0]),
-               (dot_m4_v3_row_y(pmat_local, co) + pmat_local[3][1]),
+               (dot_m4_v3_row_x(pmat, co) + pmat[3][0]),
+               (dot_m4_v3_row_y(pmat, co) + pmat[3][1]),
        };
 
        if (is_persp) {
@@ -414,28 +437,30 @@ static bool test_projected_vert_dist(
 
        co2d[0] += 1.0f;
        co2d[1] += 1.0f;
-       co2d[0] *= win_half[0];
-       co2d[1] *= win_half[1];
+       co2d[0] *= projectdefs->win_half[0];
+       co2d[1] *= projectdefs->win_half[1];
 
        const float dist_sq = len_squared_v2v2(mval, co2d);
-       if (dist_sq < *dist_px_sq) {
+       if (dist_sq < projectdefs->dist_px_sq) {
                copy_v3_v3(r_co, co);
-               *dist_px_sq = dist_sq;
+               projectdefs->dist_px_sq = dist_sq;
                return true;
        }
        return false;
 }
 
 static bool test_projected_edge_dist(
-        float pmat_local[4][4], const float va[3], const float vb[3], const 
bool is_persp,
-        const float mval[2], const float depth_range[2], const float 
win_half[2], float *dist_px_sq,
+        PreDefProject *projectdefs,
+        const float va[3], const float vb[3], const bool is_persp,
+        const float mval[2], const float depth_range[2],
         float r_co[3])
 {
        float depth_a, depth_b;
+       float(*pmat)[4] = projectdefs->pmat;
 
        if (is_persp) {
-               depth_a = mul_project_m4_v3_zfac(pmat_local, va);
-               depth_b = mul_project_m4_v3_zfac(pmat_local, vb);
+               depth_a = mul_project_m4_v3_zfac(pmat, va);
+               depth_b = mul_project_m4_v3_zfac(pmat, vb);
 
                if (depth_a < depth_range[0] && depth_b < depth_range[0]) {
                        return false;
@@ -443,12 +468,12 @@ static bool test_projected_edge_dist(
        }
 
        float va2d[2] = {
-               (dot_m4_v3_row_x(pmat_local, va) + pmat_local[3][0]),
-               (dot_m4_v3_row_y(pmat_local, va) + pmat_local[3][1]),
+               (dot_m4_v3_row_x(pmat, va) + pmat[3][0]),
+               (dot_m4_v3_row_y(pmat, va) + pmat[3][1]),
        };
        float vb2d[2] = {
-               (dot_m4_v3_row_x(pmat_local, vb) + pmat_local[3][0]),
-               (dot_m4_v3_row_y(pmat_local, vb) + pmat_local[3][1]),
+               (dot_m4_v3_row_x(pmat, vb) + pmat[3][0]),
+               (dot_m4_v3_row_y(pmat, vb) + pmat[3][1]),
        };
 
        if (is_persp) {
@@ -461,10 +486,10 @@ static bool test_projected_edge_dist(
        vb2d[0] += 1.0f;
        vb2d[1] += 1.0f;
 
-       va2d[0] *= win_half[0];
-       va2d[1] *= win_half[1];
-       vb2d[0] *= win_half[0];
-       vb2d[1] *= win_half[1];
+       va2d[0] *= projectdefs->win_half[0];
+       va2d[1] *= projectdefs->win_half[1];
+       vb2d[0] *= projectdefs->win_half[0];
+       vb2d[1] *= projectdefs->win_half[1];
 
        float tmp_point[2], edge[2], rdist;
        sub_v3_v3v3(tmp_point, mval, va2d);
@@ -485,7 +510,7 @@ static bool test_projected_edge_dist(
                //madd_v2_v2fl(va2d, edge, lambda);
                rdist = len_squared_v2v2(mval, tmp_point);
        }
-       if (rdist < *dist_px_sq) {
+       if (rdist < projectdefs->dist_px_sq) {
                if (r_co) {
                        if (is_persp) {
                                const float fac = depth_a / (depth_a + depth_b);
@@ -501,7 +526,7 @@ static bool test_projected_edge_dist(
                        madd_v3_v3v3fl(r_co, va, seg, lambda);
                }
 
-               *dist_px_sq = rdist;
+               projectdefs->dist_px_sq = rdist;
                return true;
        }
        return false;
@@ -518,12 +543,11 @@ typedef struct Object_Nearest2dPrecalc {
        float ray_origin_local[3];
        float ray_direction_local[3];
        float ray_inv_dir[3];
-       float pmat_local[4][4]; /* perspective matrix multiplied by object 
matrix */
+
+       PreDefProject projectdefs;
        float mval[2];
-       float win_half[2];
        bool sign[3];
        bool r_axis_closest[3];
-       float dist_px_sq; /* squared */
        float depth_range[2];
 
        void *userdata;
@@ -534,21 +558,17 @@ typedef struct Object_Nearest2dPrecalc {
 
 
 static void nearest2d_precalc(
-        struct Object_Nearest2dPrecalc *neasrest_precalc,
+        struct Object_Nearest2dPrecalc *neasrest_precalc, const ARegion *ar,
+        const float dist_px, float obmat[4][4],
         const float ray_origin_local[3], const float ray_direction_local[3],
-        const float mval[2], const float depth_range[2],
-        float pmat_local[4][4], const float region_win[2],
-        const float dist_px_sq)
+        const float mval[2], const float depth_range[2])
 {
+       precalc_project(&neasrest_precalc->projectdefs, ar, dist_px, obmat);
        copy_v3_v3(neasrest_precalc->ray_origin_local, ray_origin_local);
        copy_v3_v3(neasrest_precalc->ray_direction_local, ray_direction_local);
-       copy_m4_m4(neasrest_precalc->pmat_local, pmat_local);
-       mul_v2_v2fl(neasrest_precalc->win_half, region_win, 0.5f);
        copy_v2_v2(neasrest_precalc->mval, mval);
        copy_v2_v2(neasrest_precalc->depth_range, depth_range);
 
-       neasrest_precalc->dist_px_sq = dist_px_sq;
-
        for (int i = 0; i < 3; i++) {
                neasrest_precalc->ray_inv_dir[i] = 
                        (neasrest_precalc->ray_direction_local[i] != 0.0f) ?
@@ -670,16 +690,17 @@ static bool walk_parent_snap_project_cb(const 
BVHTreeAxisRange *bounds, void *us
        }
        float scale = fabsf(local_bvmax[main_axis] - local_bvmin[main_axis]);
 
-       float depth_a = mul_project_m4_v3_zfac(data->pmat_local, va);
-       float depth_b = depth_a + data->pmat_local[main_axis][3] * scale;
+       float (*pmat)[4] = data->projectdefs.pmat;
+       float depth_a = mul_project_m4_v3_zfac(pmat, va);
+       float depth_b = depth_a + pmat[main_axis][3] * scale;
 
        float va2d[2] = {
-               (dot_m4_v3_row_x(data->pmat_local, va) + 
data->pmat_local[3][0]),
-               (dot_m4_v3_row_y(data->pmat_local, va) + 
data->pmat_local[3][1]),
+               (dot_m4_v3_row_x(pmat, va) + pmat[3][0]),
+               (dot_m4_v3_row_y(pmat, va) + pmat[3][1]),
        };
        float vb2d[2] = {
-               (va2d[0] + data->pmat_local[main_axis][0] * scale) / depth_b,
-               (va2d[1] + data->pmat_local[main_axis][1] * scale) / depth_b,
+               (va2d[0] + pmat[main_axis][0] * scale) / depth_b,
+               (va2d[1] + pmat[main_axis][1] * scale) / depth_b,
        };
 
        va2d[0] /= depth_a;
@@ -690,10 +711,10 @@ static bool walk_parent_snap_project_cb(const 
BVHTreeAxisRange *bounds, void *us
        vb2d[0] += 1.0f;
        vb2d[1] += 1.0f;
 
-       va2d[0] *= data->win_half[0];
-       va2d[1] *= data->win_half[1];
-       vb2d[0] *= data->win_half[0];
-       vb2d[1] *= data->win_half[1];
+       va2d[0] *= data->projectdefs.win_half[0];
+       va2d[1] *= data->projectdefs.win_half[1];
+       vb2d[0] *= data->projectdefs.win_half[0];
+       vb2d[1] *= data->projectdefs.win_half[1];
 
        //float dvec[2], edge[2], rdist;
        //sub_v2_v2v2(dvec, data->mval, va2d);
@@ -722,7 +743,7 @@ static bool walk_parent_snap_project_cb(const 
BVHTreeAxisRange *bounds, void *us
        else {
                rdist = len_squared_v2v2(data->mval, va2d);
        }
-       return rdist < data->dist_px_sq;
+       return rdist < data->projectdefs.dist_px_sq;
 }
 
 static bool cb_leaf_snap_vert(const BVHTreeAxisRange *bounds, int index, void 
*userdata)
@@ -734,9 +755,8 @@ static bool cb_leaf_snap_vert(const BVHTreeAxisRange 
*bounds, int index, void *u
                (bounds[2].min + bounds[2].max) / 2,
        };
        if (test_projected_vert_dist(
-               neasrest_precalc->pmat_local, co, true,
+               &neasrest_precalc->projectdefs, co, true,
                neasrest_precalc->mval, neasrest_precalc->depth_range,
-               neasrest_precalc->win_half, &neasrest_precalc->dist_px_sq,
                neasrest_precalc->co))
        {
                copy_vert_no(neasrest_precalc->userdata, index, 
neasrest_precalc->no);
@@ -753,9 +773,8 @@ static bool cb_leaf_snap_edge(const BVHTreeAxisRange 
*UNUSED(bounds), int index,
        get_edge_verts(neasrest_precalc->userdata, index, v_pair);
 
        if (test_projected_edge_dist(
-               neasrest_precalc->pmat_local, v_pair[0], v_pair[1], true,
+               &neasrest_precalc->projectdefs, v_pair[0], v_pair[1], true,
                neasrest_precalc->mval, neasrest_precalc->depth_range,
-               neasrest_precalc->win_half, &neasrest_precalc->dist_px_sq,
                neasrest_precalc->co))
        {
                sub_v3_v3v3(neasrest_precalc->no, v_pair[0], v_pair[1]);
@@ -788,13 +807,8 @@ static bool snapArmature(
 {
        bool retval = false;
 
-       const float win_half[2] = {ar->winx / 2, ar->winy / 2};
-       float (*pmat)[4] = ((RegionView3D *)ar->regiondata)->persmat;
-
-       float pmat_local[4][4];
-       mul_m4_m4m4(pmat_local, pmat, obmat);
-
-       float dist_px_sq = SQUARE(*dist_px);
+       PreDefProject projectdefs;
+       precalc_project(&projectdefs, ar, *dist_px, obmat);
 
        if (arm->edbo) {
                for (EditBone *eBone = arm->edbo->first; eBone; eBone = 
eBone->next) {
@@ -804,16 +818,14 @@ static bool snapArmature(
                                        switch (snap_to) {
                                                case SCE_SNAP_MODE_VERTEX:
                                                        retval |= 
test_projected_vert_dist(
-                                                               pmat_local, 
eBone->head, is_persp, mval, depth_range, win_half,
-                                                               &dist_px_sq, 
r_loc);
+                                                               &projectdefs, 
eBone->head, is_persp, mval, depth_range, r_loc);
                                                        retval |= 
test_projected_vert

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to