Commit: cd596fa1c7ece1e8129a5766c7cdcd5be22245cd Author: Germano Cavalcante Date: Sun Jan 29 12:07:14 2017 -0300 Branches: master https://developer.blender.org/rBcd596fa1c7ece1e8129a5766c7cdcd5be22245cd
Remove struct `PreDefProject` and store all immutable parameters within the new struct `SnapData` In order to simplify the reading of these functions, the parameters: `snap_to`, `mval`, `ray_start`, `ray_dir`, `view_proj` and `depth_range` are now stored in the struct `SnapData` =================================================================== 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 fba965f69a..f0177edf20 100644 --- a/source/blender/editors/transform/transform_snap_object.c +++ b/source/blender/editors/transform/transform_snap_object.c @@ -59,6 +59,18 @@ #include "transform.h" +typedef struct SnapData { + short snap_to; + float mval[2]; + float ray_origin[3]; + float ray_start[3]; + float ray_dir[3]; + float pmat[4][4]; /* perspective matrix */ + float win_half[2];/* win x and y */ + enum eViewProj view_proj; + float depth_range[2]; +} SnapData; + typedef struct SnapObjectData { enum { SNAP_MESH = 1, @@ -232,26 +244,40 @@ 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]) +/* + * Generates a struct with the immutable parameters that will be used on all objects. + * + * \param snap_to: Element to snap, Vertice, Edge or Face. + * \param view_proj: ORTHO or PERSP. + * Currently only works one at a time, but can eventually operate as flag. + * + * \param mval: Mouse coords. + * (When NULL, ray-casting is handled without any projection matrix correction.) + * \param ray_origin: ray_start before being moved toward the ray_normal at the distance from vew3d clip_min. + * \param ray_start: ray_origin moved for the start clipping plane (clip_min). + * \param ray_direction: Unit length direction of the ray. + * \param depth_range: distances of clipe plane min and clip plane max; + */ +static void set_SnapData( + SnapData *snapdata, + const ARegion *ar, const unsigned short snap_to, const enum eViewProj view_proj, + const float mval[2], const float ray_origin[3], const float ray_start[3], + const float ray_direction[3], const float depth_range[2]) { - 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); + if (ar) { + copy_m4_m4(snapdata->pmat, ((RegionView3D *)ar->regiondata)->persmat); + snapdata->win_half[0] = ar->winx / 2; + snapdata->win_half[1] = ar->winy / 2; + } + if (mval) { + copy_v2_v2(snapdata->mval, mval); + } + snapdata->snap_to = snap_to; + copy_v3_v3(snapdata->ray_origin, ray_origin); + copy_v3_v3(snapdata->ray_start, ray_start); + copy_v3_v3(snapdata->ray_dir, ray_direction); + snapdata->view_proj = view_proj; + copy_v2_v2(snapdata->depth_range, depth_range); } static const float *get_vert_co(const BVHTreeFromMeshType *meshdata, const int index) @@ -322,14 +348,12 @@ static void get_edge_verts( } static bool test_projected_vert_dist( - PreDefProject *projectdefs, - const float co[3], const enum eViewProj view_proj, - const float mval[2], const float depth_range[2], - float r_co[3]) + const float depth_range[2], const float mval[2], const float co[3], + float pmat[4][4], const float win_half[2], const bool is_persp, + float *dist_px_sq, float r_co[3]) { float depth; - float(*pmat)[4] = projectdefs->pmat; - if (view_proj == VIEW_PROJ_PERSP) { + if (is_persp) { depth = mul_project_m4_v3_zfac(pmat, co); if (depth < depth_range[0] || depth > depth_range[1]) { return false; @@ -341,34 +365,35 @@ static bool test_projected_vert_dist( (dot_m4_v3_row_y(pmat, co) + pmat[3][1]), }; - if (view_proj == VIEW_PROJ_PERSP) { + if (is_persp) { mul_v2_fl(co2d, 1 / depth); } co2d[0] += 1.0f; co2d[1] += 1.0f; - co2d[0] *= projectdefs->win_half[0]; - co2d[1] *= projectdefs->win_half[1]; + co2d[0] *= win_half[0]; + co2d[1] *= win_half[1]; const float dist_sq = len_squared_v2v2(mval, co2d); - if (dist_sq < projectdefs->dist_px_sq) { + if (dist_sq < *dist_px_sq) { copy_v3_v3(r_co, co); - projectdefs->dist_px_sq = dist_sq; + *dist_px_sq = dist_sq; return true; } return false; } static bool test_projected_edge_dist( - PreDefProject *projectdefs, - const float va[3], const float vb[3], const float ray_start[3], const float ray_normal[3], - const enum eViewProj view_proj, const float mval[2], const float depth_range[2], - float r_co[3]) + const float depth_range[2], const float mval[2], + float pmat[4][4], const float win_half[2], const bool is_persp, + const float ray_start[3], const float ray_dir[3], + const float va[3], const float vb[3], + float *dist_px_sq, float r_co[3]) { float tmp_co[3], depth; - dist_squared_ray_to_seg_v3(ray_start, ray_normal, va, vb, tmp_co, &depth); - return test_projected_vert_dist(projectdefs, tmp_co, view_proj, mval, depth_range, r_co); + dist_squared_ray_to_seg_v3(ray_start, ray_dir, va, vb, tmp_co, &depth); + return test_projected_vert_dist(depth_range, mval, tmp_co, pmat, win_half, is_persp, dist_px_sq, r_co); } /** \} */ @@ -383,11 +408,15 @@ typedef struct Object_Nearest2dPrecalc { float ray_direction_local[3]; float ray_inv_dir[3]; - PreDefProject projectdefs; + float depth_range[2]; + float pmat[4][4]; /* perspective matrix multiplied by object matrix */ + float win_half[2]; + bool is_persp; + float dist_px_sq; + float mval[2]; bool sign[3]; bool r_axis_closest[3]; - float depth_range[2]; void *userdata; int index; @@ -397,16 +426,24 @@ typedef struct Object_Nearest2dPrecalc { static void nearest2d_precalc( - Object_Nearest2dPrecalc *neasrest_precalc, const ARegion *ar, + Object_Nearest2dPrecalc *neasrest_precalc, SnapData *snapdata, 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]) + const float ray_origin_local[3], const float ray_direction_local[3]) { - precalc_project(&neasrest_precalc->projectdefs, ar, dist_px, obmat); + if (obmat) { + mul_m4_m4m4(neasrest_precalc->pmat, snapdata->pmat, obmat); + } + else { + copy_m4_m4(neasrest_precalc->pmat, snapdata->pmat); + } + copy_v2_v2(neasrest_precalc->win_half, snapdata->win_half); + neasrest_precalc->dist_px_sq = SQUARE(dist_px); + neasrest_precalc->is_persp = snapdata->view_proj == VIEW_PROJ_PERSP; + copy_v2_v2(neasrest_precalc->depth_range, snapdata->depth_range); + copy_v3_v3(neasrest_precalc->ray_origin_local, ray_origin_local); copy_v3_v3(neasrest_precalc->ray_direction_local, ray_direction_local); - copy_v2_v2(neasrest_precalc->mval, mval); - copy_v2_v2(neasrest_precalc->depth_range, depth_range); + copy_v2_v2(neasrest_precalc->mval, snapdata->mval); for (int i = 0; i < 3; i++) { neasrest_precalc->ray_inv_dir[i] = @@ -456,9 +493,9 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us (local_bvmax[1] - data->ray_origin_local[1]) * data->ray_inv_dir[1], (local_bvmax[2] - data->ray_origin_local[2]) * data->ray_inv_dir[2], }; - /* va[3] and vb[3] are the coordinates of the AABB edge closest to the ray */ + /* `va` and `vb` are the coordinates of the AABB edge closest to the ray */ float va[3], vb[3]; - /* rtmin and rtmax are the distances of the minimum and maximum ray hits on the AABB */ + /* `rtmin` and `rtmax` are the minimum and maximum distances of the ray hits on the AABB */ float rtmin, rtmax; int main_axis; @@ -506,10 +543,10 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us /* if rtmin < rtmax, ray intersect `AABB` */ if (rtmin <= rtmax) { #ifdef IGNORE_BEHIND_RAY - /* `if rtmax < depth_min`, the hit is behind us - * TODO: Check if the entire AABB is behind ray - * this will prevent unnecessary leaf testing*/ - if (rtmax < depth_range[0]) { + /* `if rtmax < depth_min`, the hit is behind us */ + if (rtmax < data->depth_range[0]) { + /* TODO: TODO: Check if the entire AABB is behind ray + * this will prevent unnecessary leaf testing */ return false; } #endif @@ -518,9 +555,9 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us return true; } #ifdef IGNORE_BEHIND_RAY - /* `if rtmin < depth_min`, the hit is behing us - * TODO: Check if the entire AABB is behind ray */ - else if (rtmin < depth_range[0]) { + /* `if rtmin < depth_min`, the hit is behing us */ + else if (rtmin < data->depth_range[0]) { + /* TODO: Test if the AABB is totally behind ray */ return false; } #endif @@ -534,7 +571,7 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us } float scale = fabsf(local_bvmax[main_axis] - local_bvmin[main_axis]); - float (*pmat)[4] = data->projectdefs.pmat; + float (*pmat)[4] = data->pmat; float depth_a = mul_project_m4_v3_zfac(pmat, va); float depth_b = depth_a + pmat[main_axis][3] * scale; @@ -555,10 +592,10 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us vb2d[0] += 1.0f; vb2d[1] += 1.0f; - 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]; + va2d[0] *= data->win_half[0]; + va2d[1] *= data->win_half[1]; + vb2d[0] *= data->win_half[0]; + vb2d[1] *= data->win_half[1]; //float dvec[2], edge[2], rdist; //sub_v2_v2v2(dvec, data->mval, va2d); @@ -587,7 +624,7 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us else { rdist = len_squared_v2v2(data->mval, va2d); } - return rdist < data->projectdefs.dist_px_sq; + return rdist < data->dist_px_sq; } static bool cb_walk_leaf_snap_vert(const BVHTreeAxisRange *bounds, int index, void *userdata) @@ -599,12 +636,13 @@ static bool cb_walk_leaf_snap_vert(const BVHTreeAxisRange *bounds, int index, vo (bounds[2].min + bounds[2].max) / 2, }; - /* Although this function is also used in the Othogonal @@ 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