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:
authorBastien Montagne <bastien@blender.org>2021-06-14 12:06:35 +0300
committerJeroen Bakker <jeroen@blender.org>2021-08-16 11:01:13 +0300
commitd3856b7e9715c7e83d377ab32dd914ec9deee7d7 (patch)
treeef81f47a6778dfee71b1ef9352bc3394d278d5a4
parentdbdf22786da0f4d1a1d21c2c1454c9c4728b3eba (diff)
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.
-rw-r--r--source/blender/blenlib/BLI_math_vector.h4
-rw-r--r--source/blender/blenlib/intern/math_vector.c10
-rw-r--r--source/blender/blenlib/intern/math_vector_inline.c15
3 files changed, 29 insertions, 0 deletions
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);