diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-08-23 12:10:37 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-08-23 12:10:37 +0300 |
commit | e7775833a78019a0e2e87feedf4ff3ec2620d4a8 (patch) | |
tree | 4cf535d65ffa1bc54bfa2d5a27b98c8193230af6 /source/blender | |
parent | 0cb23fafe7c93f4cbf010a3f2fdd1db96d2d4e1d (diff) |
Math Lib: Use same logic for 2d/3d functions
dist_squared_to_line_v2, dist_to_line_v2
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 71 |
1 files changed, 12 insertions, 59 deletions
diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 6e54d4b5865..ee1b74ce6fc 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -308,72 +308,25 @@ float volume_tetrahedron_signed_v3(const float v1[3], const float v2[3], const f * using Hesse formula, NO LINE PIECE! */ float dist_squared_to_line_v2(const float p[2], const float l1[2], const float l2[2]) { - float a[2], deler; + float closest[2]; - a[0] = l1[1] - l2[1]; - a[1] = l2[0] - l1[0]; + closest_to_line_v2(closest, p, l1, l2); - deler = len_squared_v2(a); - - if (deler != 0.0f) { - float f = ((p[0] - l1[0]) * a[0] + - (p[1] - l1[1]) * a[1]); - return (f * f) / deler; - } - else { - return 0.0f; - } + return len_squared_v2v2(closest, p); } float dist_to_line_v2(const float p[2], const float l1[2], const float l2[2]) { - float a[2], deler; - - a[0] = l1[1] - l2[1]; - a[1] = l2[0] - l1[0]; - - deler = len_squared_v2(a); - - if (deler != 0.0f) { - float f = ((p[0] - l1[0]) * a[0] + - (p[1] - l1[1]) * a[1]); - return fabsf(f) / sqrtf(deler); - } - else { - return 0.0f; - } + return sqrtf(dist_squared_to_line_v2(p, l1, l2)); } /* distance p to line-piece v1-v2 */ float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2]) { - float lambda, rc[2], pt[2], len; + float closest[2]; - rc[0] = l2[0] - l1[0]; - rc[1] = l2[1] - l1[1]; - len = rc[0] * rc[0] + rc[1] * rc[1]; - if (len == 0.0f) { - rc[0] = p[0] - l1[0]; - rc[1] = p[1] - l1[1]; - return (rc[0] * rc[0] + rc[1] * rc[1]); - } + closest_to_line_segment_v2(closest, p, l1, l2); - lambda = (rc[0] * (p[0] - l1[0]) + rc[1] * (p[1] - l1[1])) / len; - if (lambda <= 0.0f) { - pt[0] = l1[0]; - pt[1] = l1[1]; - } - else if (lambda >= 1.0f) { - pt[0] = l2[0]; - pt[1] = l2[1]; - } - else { - pt[0] = lambda * rc[0] + l1[0]; - pt[1] = lambda * rc[1] + l1[1]; - } - - rc[0] = pt[0] - p[0]; - rc[1] = pt[1] - p[1]; - return (rc[0] * rc[0] + rc[1] * rc[1]); + return len_squared_v2v2(closest, p); } float dist_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2]) @@ -501,17 +454,17 @@ float dist_to_line_segment_v3(const float p[3], const float l1[3], const float l return sqrtf(dist_squared_to_line_segment_v3(p, l1, l2)); } -float dist_squared_to_line_v3(const float v1[3], const float l1[3], const float l2[3]) +float dist_squared_to_line_v3(const float p[3], const float l1[3], const float l2[3]) { float closest[3]; - closest_to_line_v3(closest, v1, l1, l2); + closest_to_line_v3(closest, p, l1, l2); - return len_squared_v3v3(closest, v1); + return len_squared_v3v3(closest, p); } -float dist_to_line_v3(const float v1[3], const float l1[3], const float l2[3]) +float dist_to_line_v3(const float p[3], const float l1[3], const float l2[3]) { - return sqrtf(dist_squared_to_line_v3(v1, l1, l2)); + return sqrtf(dist_squared_to_line_v3(p, l1, l2)); } /** |