diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-08-23 19:19:20 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-08-23 19:19:20 +0400 |
commit | 6f26acb00934594859cca09e9774b41a6c1a9e7c (patch) | |
tree | 5ce857f0dbf974ad9a1b1e1a549d35c592f17022 | |
parent | 25ca7622386a1cb74b1745f1038fd71cd498a17c (diff) |
simplify dist_to_plane_v3 and add dist_squared_to_plane_v3
-rw-r--r-- | source/blender/blenkernel/intern/camera.c | 20 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 20 |
3 files changed, 25 insertions, 16 deletions
diff --git a/source/blender/blenkernel/intern/camera.c b/source/blender/blenkernel/intern/camera.c index d92a9220b4f..536ec95e3d1 100644 --- a/source/blender/blenkernel/intern/camera.c +++ b/source/blender/blenkernel/intern/camera.c @@ -459,7 +459,7 @@ typedef struct CameraViewFrameData { float plane_tx[4][4]; /* 4 planes (not 4x4 matrix)*/ float frame_tx[4][3]; float normal_tx[4][3]; - float dist_vals[4]; + float dist_vals_sq[4]; /* distance squared (signed) */ unsigned int tot; } CameraViewFrameData; @@ -469,9 +469,9 @@ static void camera_to_frame_view_cb(const float co[3], void *user_data) unsigned int i; for (i = 0; i < 4; i++) { - float nd = dist_to_plane_v3(co, data->plane_tx[i]); - if (nd < data->dist_vals[i]) { - data->dist_vals[i] = nd; + float nd = dist_squared_to_plane_v3(co, data->plane_tx[i]); + if (nd < data->dist_vals_sq[i]) { + data->dist_vals_sq[i] = nd; } } @@ -520,10 +520,7 @@ int BKE_camera_view_frame_fit_to_scene(Scene *scene, struct View3D *v3d, Object } /* initialize callback data */ - data_cb.dist_vals[0] = - data_cb.dist_vals[1] = - data_cb.dist_vals[2] = - data_cb.dist_vals[3] = FLT_MAX; + copy_v4_fl(data_cb.dist_vals_sq, FLT_MAX); data_cb.tot = 0; /* run callback on all visible points */ BKE_scene_foreach_display_point(scene, v3d, BA_SELECT, @@ -538,11 +535,16 @@ int BKE_camera_view_frame_fit_to_scene(Scene *scene, struct View3D *v3d, Object float plane_isect_pt_1[3], plane_isect_pt_2[3]; + /* could make a generic macro */ +#define SQRT_SIGNED(f) copysign(sqrtf(fabsf(f)), f) + /* apply the dist-from-plane's to the transformed plane points */ for (i = 0; i < 4; i++) { - mul_v3_v3fl(plane_tx[i], data_cb.normal_tx[i], data_cb.dist_vals[i]); + mul_v3_v3fl(plane_tx[i], data_cb.normal_tx[i], SQRT_SIGNED(data_cb.dist_vals_sq[i])); } +#undef SQRT_SIGNED + isect_plane_plane_v3(plane_isect_1, plane_isect_1_no, plane_tx[0], data_cb.normal_tx[0], plane_tx[2], data_cb.normal_tx[2]); diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index 383ab6b2533..02db0885163 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -81,6 +81,7 @@ float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float dist_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2]); void closest_to_line_segment_v2(float closest[2], const float p[2], const float l1[2], const float l2[2]); +float dist_squared_to_plane_v3(const float p[3], const float plane[4]); float dist_to_plane_v3(const float p[3], const float plane[4]); float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3]); float dist_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3]); diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 1bf7b1f6a9a..6ca4b4b6095 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -321,17 +321,23 @@ void closest_to_plane_v3(float close_r[3], const float plane[4], const float pt[ madd_v3_v3v3fl(close_r, pt, plane, -side / length); } -float dist_to_plane_v3(const float pt[3], const float plane[4]) +float dist_squared_to_plane_v3(const float pt[3], const float plane[4]) { - float close[3]; - - /* same logic as closest_to_plane_v3() */ const float length = len_squared_v3(plane); const float side = plane_point_side_v3(plane, pt); - madd_v3_v3v3fl(close, pt, plane, -side / length); - /* end same logic */ + const float fac = side / length; + return copysign(length * (fac * fac), side); +} - return copysign(len_v3v3(pt, close), side); +/** + * Return the signed distance from the point to the plane. + */ +float dist_to_plane_v3(const float pt[3], const float plane[4]) +{ + const float length = len_squared_v3(plane); + const float side = plane_point_side_v3(plane, pt); + const float fac = side / length; + return sqrtf(length) * fac; } /* distance v1 to line-piece l1-l2 in 3D */ |