From c2f74d62435a25dc52d1a640db1c417ad7ec75d3 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Mon, 14 Jun 2021 11:06:35 +0200 Subject: BLI_math: Fix several division-by-zero cases. Those were caused by various tools used on degenerate geometry, see T79775. Note that fixes are as low-level as possible, to ensure they cover as much as possible of unreported issues too. We still probably have many more of those hidden in BLI_math though. --- source/blender/blenlib/BLI_math_vector.h | 6 +++++- source/blender/blenlib/intern/math_geom.c | 9 ++++++++- source/blender/blenlib/intern/math_vector.c | 15 +++++++++++++++ source/blender/blenlib/intern/math_vector_inline.c | 15 +++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) (limited to 'source') diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h index bb1e1a1c38d..7e734a18e8c 100644 --- a/source/blender/blenlib/BLI_math_vector.h +++ b/source/blender/blenlib/BLI_math_vector.h @@ -327,7 +327,11 @@ MINLINE bool is_zero_v2(const float a[3]) ATTR_WARN_UNUSED_RESULT; MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT; MINLINE bool is_zero_v4(const float a[4]) ATTR_WARN_UNUSED_RESULT; -bool is_finite_v2(const float a[3]) ATTR_WARN_UNUSED_RESULT; +MINLINE bool is_zero_v2_db(const double a[2]) ATTR_WARN_UNUSED_RESULT; +MINLINE bool is_zero_v3_db(const double a[3]) ATTR_WARN_UNUSED_RESULT; +MINLINE bool is_zero_v4_db(const double a[4]) ATTR_WARN_UNUSED_RESULT; + +bool is_finite_v2(const float a[2]) ATTR_WARN_UNUSED_RESULT; bool is_finite_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT; bool is_finite_v4(const float a[4]) ATTR_WARN_UNUSED_RESULT; diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 01cda6c9e4a..897c18adba5 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -3344,6 +3344,13 @@ float closest_to_ray_v3(float r_close[3], const float ray_dir[3]) { float h[3], lambda; + + if (UNLIKELY(is_zero_v3(ray_dir))) { + lambda = 0.0f; + copy_v3_v3(r_close, ray_orig); + return lambda; + } + sub_v3_v3v3(h, p, ray_orig); lambda = dot_v3v3(ray_dir, h) / dot_v3v3(ray_dir, ray_dir); madd_v3_v3v3fl(r_close, ray_orig, ray_dir, lambda); @@ -4467,7 +4474,7 @@ void interp_weights_poly_v2(float *w, float v[][2], const int n, const float co[ d_curr = d_next; DIR_V2_SET(&d_next, v_next, co); ht = mean_value_half_tan_v2_db(&d_curr, &d_next); - w[i_curr] = (float)((ht_prev + ht) / d_curr.len); + w[i_curr] = (d_curr.len == 0.0) ? 0.0f : (float)((ht_prev + ht) / d_curr.len); totweight += w[i_curr]; /* step */ diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c index a21e0c8f092..521171cda95 100644 --- a/source/blender/blenlib/intern/math_vector.c +++ b/source/blender/blenlib/intern/math_vector.c @@ -657,6 +657,11 @@ void angle_poly_v3(float *angles, const float *verts[3], int len) */ void project_v2_v2v2(float out[2], const float p[2], const float v_proj[2]) { + if (UNLIKELY(is_zero_v2(v_proj))) { + zero_v2(out); + return; + } + const float mul = dot_v2v2(p, v_proj) / dot_v2v2(v_proj, v_proj); out[0] = mul * v_proj[0]; @@ -668,6 +673,11 @@ void project_v2_v2v2(float out[2], const float p[2], const float v_proj[2]) */ void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3]) { + if (UNLIKELY(is_zero_v3(v_proj))) { + zero_v3(out); + return; + } + const float mul = dot_v3v3(p, v_proj) / dot_v3v3(v_proj, v_proj); out[0] = mul * v_proj[0]; @@ -677,6 +687,11 @@ void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3]) void project_v3_v3v3_db(double out[3], const double p[3], const double v_proj[3]) { + if (UNLIKELY(is_zero_v3_db(v_proj))) { + zero_v3_db(out); + return; + } + const double mul = dot_v3v3_db(p, v_proj) / dot_v3v3_db(v_proj, v_proj); out[0] = mul * v_proj[0]; diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c index ead354c2d87..e0df9665a7e 100644 --- a/source/blender/blenlib/intern/math_vector_inline.c +++ b/source/blender/blenlib/intern/math_vector_inline.c @@ -1282,6 +1282,21 @@ MINLINE bool is_zero_v4(const float v[4]) return (v[0] == 0.0f && v[1] == 0.0f && v[2] == 0.0f && v[3] == 0.0f); } +MINLINE bool is_zero_v2_db(const double v[2]) +{ + return (v[0] == 0.0 && v[1] == 0.0); +} + +MINLINE bool is_zero_v3_db(const double v[3]) +{ + return (v[0] == 0.0 && v[1] == 0.0 && v[2] == 0.0); +} + +MINLINE bool is_zero_v4_db(const double v[4]) +{ + return (v[0] == 0.0 && v[1] == 0.0 && v[2] == 0.0 && v[3] == 0.0); +} + MINLINE bool is_one_v3(const float v[3]) { return (v[0] == 1.0f && v[1] == 1.0f && v[2] == 1.0f); -- cgit v1.2.3