Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGermano Cavalcante <germano.costa@ig.com.br>2016-07-14 06:02:21 +0300
committerGermano Cavalcante <germano.costa@ig.com.br>2016-07-14 06:02:21 +0300
commitb014f0307772143694ab05098fd22619c6b31080 (patch)
tree1c72ef5931ec04ce8d4b03a7418f423c72adbd7f /source/blender/editors/transform
parent743f9315842489e94e778607742322a01995d365 (diff)
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.
Diffstat (limited to 'source/blender/editors/transform')
-rw-r--r--source/blender/editors/transform/transform_snap_object.c223
1 files changed, 102 insertions, 121 deletions
diff --git a/source/blender/editors/transform/transform_snap_object.c b/source/blender/editors/transform/transform_snap_object.c
index 7aed7f9fdae..dc7f5e9ad58 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_dist(
- pmat_local, eBone->tail, is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, eBone->tail, is_persp, mval, depth_range, r_loc);
break;
case SCE_SNAP_MODE_EDGE:
retval |= test_projected_edge_dist(
- pmat_local, eBone->head, eBone->tail, is_persp,
- mval, depth_range, win_half, &dist_px_sq, r_loc);
+ &projectdefs, eBone->head, eBone->tail, is_persp,
+ mval, depth_range, r_loc);
break;
}
}
@@ -831,23 +843,21 @@ static bool snapArmature(
switch (snap_to) {
case SCE_SNAP_MODE_VERTEX:
retval |= test_projected_vert_dist(
- pmat_local, head_vec, is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, head_vec, is_persp, mval, depth_range, r_loc);
retval |= test_projected_vert_dist(
- pmat_local, tail_vec, is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, tail_vec, is_persp, mval, depth_range, r_loc);
break;
case SCE_SNAP_MODE_EDGE:
retval |= test_projected_edge_dist(
- pmat_local, head_vec, tail_vec, is_persp,
- mval, depth_range, win_half, &dist_px_sq, r_loc);
+ &projectdefs, head_vec, tail_vec, is_persp,
+ mval, depth_range, r_loc);
break;
}
}
}
}
if (retval) {
- *dist_px = sqrtf(dist_px_sq);
+ *dist_px = sqrtf(projectdefs.dist_px_sq);
mul_m4_v3(obmat, r_loc);
return true;
}
@@ -870,13 +880,8 @@ static bool snapCurve(
return retval;
}
- 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);
for (Nurb *nu = (ob->mode == OB_MODE_EDIT ? cu->editnurb->nurbs.first : cu->nurb.first); nu; nu = nu->next) {
for (int u = 0; u < nu->pntsu; u++) {
@@ -890,22 +895,19 @@ static bool snapCurve(
break;
}
retval |= test_projected_vert_dist(
- pmat_local, nu->bezt[u].vec[1], is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, nu->bezt[u].vec[1], is_persp, mval, depth_range, r_loc);
/* don't snap if handle is selected (moving), or if it is aligning to a moving handle */
if (!(nu->bezt[u].f1 & SELECT) &&
!(nu->bezt[u].h1 & HD_ALIGN && nu->bezt[u].f3 & SELECT))
{
retval |= test_projected_vert_dist(
- pmat_local, nu->bezt[u].vec[0], is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, nu->bezt[u].vec[0], is_persp, mval, depth_range, r_loc);
}
if (!(nu->bezt[u].f3 & SELECT) &&
!(nu->bezt[u].h2 & HD_ALIGN && nu->bezt[u].f1 & SELECT))
{
retval |= test_projected_vert_dist(
- pmat_local, nu->bezt[u].vec[2], is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, nu->bezt[u].vec[2], is_persp, mval, depth_range, r_loc);
}
}
else {
@@ -914,8 +916,7 @@ static bool snapCurve(
break;
}
retval |= test_projected_vert_dist(
- pmat_local, nu->bp[u].vec, is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, nu->bp[u].vec, is_persp, mval, depth_range, r_loc);
}
}
else {
@@ -923,13 +924,11 @@ static bool snapCurve(
if (nu->pntsu > 1) {
if (nu->bezt) {
retval |= test_projected_vert_dist(
- pmat_local, nu->bezt[u].vec[1], is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, nu->bezt[u].vec[1], is_persp, mval, depth_range, r_loc);
}
else {
retval |= test_projected_vert_dist(
- pmat_local, nu->bp[u].vec, is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, nu->bp[u].vec, is_persp, mval, depth_range, r_loc);
}
}
}
@@ -941,7 +940,7 @@ static bool snapCurve(
}
}
if (retval) {
- *dist_px = sqrtf(dist_px_sq);
+ *dist_px = sqrtf(projectdefs.dist_px_sq);
mul_m4_v3(obmat, r_loc);
return true;
}
@@ -968,16 +967,12 @@ static bool snapEmpty(
switch (snap_to) {
case SCE_SNAP_MODE_VERTEX:
{
- const float win_half[2] = { ar->winx / 2, ar->winy / 2 };
- float (*pmat)[4] = ((RegionView3D *)ar->regiondata)->persmat;
+ PreDefProject projectdefs;
+ precalc_project(&projectdefs, ar, *dist_px, NULL);
float tmp_co[3];
copy_v3_v3(tmp_co, obmat[3]);
- float dist_px_sq = SQUARE(*dist_px);
- if (test_projected_vert_dist(
- pmat, tmp_co, is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc))
- {
- *dist_px = sqrtf(dist_px_sq);
+ if (test_projected_vert_dist(&projectdefs, tmp_co, is_persp, mval, depth_range, r_loc)) {
+ *dist_px = sqrtf(projectdefs.dist_px_sq);
retval = true;
}
break;
@@ -999,12 +994,9 @@ static bool snapCamera(
float r_loc[3], float *UNUSED(r_no))
{
Scene *scene = sctx->scene;
- const ARegion *ar = sctx->v3d_data.ar;
-
- const float win_half[2] = {ar->winx / 2, ar->winy / 2};
- float (*pmat)[4] = ((RegionView3D *)ar->regiondata)->persmat;
- float dist_px_sq = SQUARE(*dist_px);
+ PreDefProject projectdefs;
+ precalc_project(&projectdefs, sctx->v3d_data.ar, *dist_px, NULL);
float orig_camera_mat[4][4], orig_camera_imat[4][4], imat[4][4];
bool retval = false;
@@ -1065,8 +1057,7 @@ static bool snapCamera(
mul_m4_v3(vertex_obmat, bundle_pos);
retval |= test_projected_vert_dist(
- pmat, bundle_pos, is_persp, mval, depth_range, win_half,
- &dist_px_sq, r_loc);
+ &projectdefs, bundle_pos, is_persp, mval, depth_range, r_loc);
}
}
@@ -1077,7 +1068,7 @@ static bool snapCamera(
}
if (retval) {
- *dist_px = sqrtf(dist_px_sq);
+ *dist_px = sqrtf(projectdefs.dist_px_sq);
return true;
}
return false;
@@ -1379,13 +1370,8 @@ static bool snapDerivedMesh(
neasrest_precalc.userdata = &treedata_type;
neasrest_precalc.index = -1;
- float (*pmat)[4] = ((RegionView3D *)ar->regiondata)->persmat;
- const float region_win[2] = {ar->winx, ar->winy};
-
- float pmat_local[4][4];
- mul_m4_m4m4(pmat_local, pmat, obmat);
- nearest2d_precalc(&neasrest_precalc, ray_org_local, ray_normal_local,
- mval, depth_range, pmat_local, region_win, SQUARE(*dist_px));
+ nearest2d_precalc(&neasrest_precalc, ar, *dist_px, obmat,
+ ray_org_local, ray_normal_local, mval, depth_range);
BVHTree_WalkLeafCallback callback = (snap_to == SCE_SNAP_MODE_VERTEX) ?
cb_leaf_snap_vert : cb_leaf_snap_edge;
@@ -1403,7 +1389,7 @@ static bool snapDerivedMesh(
mul_m3_v3(timat, r_no);
normalize_v3(r_no);
}
- *dist_px = sqrtf(neasrest_precalc.dist_px_sq);
+ *dist_px = sqrtf(neasrest_precalc.projectdefs.dist_px_sq);
retval = true;
}
@@ -1706,13 +1692,8 @@ static bool snapEditMesh(
neasrest_precalc.userdata = &treedata_type;
neasrest_precalc.index = -1;
- float (*pmat)[4] = ((RegionView3D *)ar->regiondata)->persmat;
- const float region_win[2] = {ar->winx, ar->winy};
-
- float pmat_local[4][4];
- mul_m4_m4m4(pmat_local, pmat, obmat);
- nearest2d_precalc(&neasrest_precalc, ray_org_local, ray_normal_local,
- mval, depth_range, pmat_local, region_win, SQUARE(*dist_px));
+ nearest2d_precalc(&neasrest_precalc, ar, *dist_px, obmat,
+ ray_org_local, ray_normal_local, mval, depth_range);
BVHTree_WalkLeafCallback callback = (snap_to == SCE_SNAP_MODE_VERTEX) ?
cb_leaf_snap_vert : cb_leaf_snap_edge;
@@ -1730,7 +1711,7 @@ static bool snapEditMesh(
mul_m3_v3(timat, r_no);
normalize_v3(r_no);
}
- *dist_px = sqrtf(neasrest_precalc.dist_px_sq);
+ *dist_px = sqrtf(neasrest_precalc.projectdefs.dist_px_sq);
retval = true;
}