[Bf-blender-cvs] [d3856b7e971] blender-v2.83-release: BLI_math: Fix several division-by-zero cases.

Bastien Montagne noreply at git.blender.org
Mon Aug 16 10:02:40 CEST 2021


Commit: d3856b7e9715c7e83d377ab32dd914ec9deee7d7
Author: Bastien Montagne
Date:   Mon Jun 14 11:06:35 2021 +0200
Branches: blender-v2.83-release
https://developer.blender.org/rBd3856b7e9715c7e83d377ab32dd914ec9deee7d7

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.

===================================================================

M	source/blender/blenlib/BLI_math_vector.h
M	source/blender/blenlib/intern/math_vector.c
M	source/blender/blenlib/intern/math_vector_inline.c

===================================================================

diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h
index 6cfa2d2ced6..74346f15039 100644
--- a/source/blender/blenlib/BLI_math_vector.h
+++ b/source/blender/blenlib/BLI_math_vector.h
@@ -315,6 +315,10 @@ 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;
 
+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[3]) 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_vector.c b/source/blender/blenlib/intern/math_vector.c
index 9009f73a62f..26dac566095 100644
--- a/source/blender/blenlib/intern/math_vector.c
+++ b/source/blender/blenlib/intern/math_vector.c
@@ -653,6 +653,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];
@@ -664,6 +669,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];
diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c
index d2c55233653..ad9cbbbadd4 100644
--- a/source/blender/blenlib/intern/math_vector_inline.c
+++ b/source/blender/blenlib/intern/math_vector_inline.c
@@ -1207,6 +1207,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);



More information about the Bf-blender-cvs mailing list