[Bf-blender-cvs] [7bd8a518a8d] override-recursive-resync: BLI_math: Cleanup: Use `mul_`/`madd_` functions.

Bastien Montagne noreply at git.blender.org
Mon Jun 14 16:35:04 CEST 2021


Commit: 7bd8a518a8d1b8929c3f89b7d8b0c855e81134d3
Author: Bastien Montagne
Date:   Mon Jun 14 12:30:11 2021 +0200
Branches: override-recursive-resync
https://developer.blender.org/rB7bd8a518a8d1b8929c3f89b7d8b0c855e81134d3

BLI_math: Cleanup: Use `mul_`/`madd_` functions.

Better to avoid explicit vectors components direct manipulation when a
generic operation for whole vector exists, if nothing else it avoids
potential mistakes in indices.

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

M	source/blender/blenlib/BLI_math_base.h
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_base.h b/source/blender/blenlib/BLI_math_base.h
index 46219ad5493..5a3482db046 100644
--- a/source/blender/blenlib/BLI_math_base.h
+++ b/source/blender/blenlib/BLI_math_base.h
@@ -237,6 +237,7 @@ float ceil_power_of_10(float f);
 #ifndef NDEBUG
 /** \note 0.0001 is too small because normals may be converted from short's: see T34322. */
 #  define BLI_ASSERT_UNIT_EPSILON 0.0002f
+#  define BLI_ASSERT_UNIT_EPSILON_DB 0.0002
 /**
  * \note Checks are flipped so NAN doesn't assert.
  * This is done because we're making sure the value was normalized and in the case we
@@ -253,8 +254,8 @@ float ceil_power_of_10(float f);
 #  define BLI_ASSERT_UNIT_V3_DB(v) \
     { \
       const double _test_unit = len_squared_v3_db(v); \
-      BLI_assert(!(fabs(_test_unit - 1.0) >= BLI_ASSERT_UNIT_EPSILON) || \
-                 !(fabs(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
+      BLI_assert(!(fabs(_test_unit - 1.0) >= BLI_ASSERT_UNIT_EPSILON_DB) || \
+                 !(fabs(_test_unit) >= BLI_ASSERT_UNIT_EPSILON_DB)); \
     } \
     (void)0
 
diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h
index caedf83666f..556a216bb89 100644
--- a/source/blender/blenlib/BLI_math_vector.h
+++ b/source/blender/blenlib/BLI_math_vector.h
@@ -163,6 +163,7 @@ MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f);
 MINLINE void madd_v3_v3v3(float r[3], const float a[3], const float b[3]);
 MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f);
 MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f);
+MINLINE void madd_v3_v3v3db_db(double r[3], const double a[3], const double b[3], double f);
 MINLINE void madd_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3]);
 MINLINE void madd_v4_v4fl(float r[4], const float a[4], float f);
 MINLINE void madd_v4_v4v4(float r[4], const float a[4], const float b[4]);
diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c
index 05c5f672baa..35dfe421cf0 100644
--- a/source/blender/blenlib/intern/math_vector.c
+++ b/source/blender/blenlib/intern/math_vector.c
@@ -663,9 +663,7 @@ void project_v2_v2v2(float out[2], const float p[2], const float v_proj[2])
   }
 
   const float mul = dot_v2v2(p, v_proj) / dot_v2v2(v_proj, v_proj);
-
-  out[0] = mul * v_proj[0];
-  out[1] = mul * v_proj[1];
+  mul_v2_v2fl(out, v_proj, mul);
 }
 
 /**
@@ -679,10 +677,7 @@ void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
   }
 
   const float mul = dot_v3v3(p, v_proj) / dot_v3v3(v_proj, v_proj);
-
-  out[0] = mul * v_proj[0];
-  out[1] = mul * v_proj[1];
-  out[2] = mul * v_proj[2];
+  mul_v3_v3fl(out, v_proj, mul);
 }
 
 void project_v3_v3v3_db(double out[3], const double p[3], const double v_proj[3])
@@ -693,10 +688,7 @@ void project_v3_v3v3_db(double out[3], const double p[3], const double v_proj[3]
   }
 
   const double mul = dot_v3v3_db(p, v_proj) / dot_v3v3_db(v_proj, v_proj);
-
-  out[0] = mul * v_proj[0];
-  out[1] = mul * v_proj[1];
-  out[2] = mul * v_proj[2];
+  mul_v3_v3db_db(out, v_proj, mul);
 }
 
 /**
@@ -705,10 +697,9 @@ void project_v3_v3v3_db(double out[3], const double p[3], const double v_proj[3]
 void project_v2_v2v2_normalized(float out[2], const float p[2], const float v_proj[2])
 {
   BLI_ASSERT_UNIT_V2(v_proj);
-  const float mul = dot_v2v2(p, v_proj);
 
-  out[0] = mul * v_proj[0];
-  out[1] = mul * v_proj[1];
+  const float mul = dot_v2v2(p, v_proj);
+  mul_v2_v2fl(out, v_proj, mul);
 }
 
 /**
@@ -717,11 +708,9 @@ void project_v2_v2v2_normalized(float out[2], const float p[2], const float v_pr
 void project_v3_v3v3_normalized(float out[3], const float p[3], const float v_proj[3])
 {
   BLI_ASSERT_UNIT_V3(v_proj);
-  const float mul = dot_v3v3(p, v_proj);
 
-  out[0] = mul * v_proj[0];
-  out[1] = mul * v_proj[1];
-  out[2] = mul * v_proj[2];
+  const float mul = dot_v3v3(p, v_proj);
+  mul_v3_v3fl(out, v_proj, mul);
 }
 
 /**
@@ -740,37 +729,31 @@ void project_v3_v3v3_normalized(float out[3], const float p[3], const float v_pr
 void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
 {
   const float mul = dot_v3v3(p, v_plane) / dot_v3v3(v_plane, v_plane);
-
-  out[0] = p[0] - (mul * v_plane[0]);
-  out[1] = p[1] - (mul * v_plane[1]);
-  out[2] = p[2] - (mul * v_plane[2]);
+  /* out[x] = p[x] - (mul * v_plane[x]) */
+  madd_v3_v3v3fl(out, p, v_plane, -mul);
 }
 
 void project_plane_v2_v2v2(float out[2], const float p[2], const float v_plane[2])
 {
   const float mul = dot_v2v2(p, v_plane) / dot_v2v2(v_plane, v_plane);
-
-  out[0] = p[0] - (mul * v_plane[0]);
-  out[1] = p[1] - (mul * v_plane[1]);
+  /* out[x] = p[x] - (mul * v_plane[x]) */
+  madd_v2_v2v2fl(out, p, v_plane, -mul);
 }
 
 void project_plane_normalized_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
 {
   BLI_ASSERT_UNIT_V3(v_plane);
   const float mul = dot_v3v3(p, v_plane);
-
-  out[0] = p[0] - (mul * v_plane[0]);
-  out[1] = p[1] - (mul * v_plane[1]);
-  out[2] = p[2] - (mul * v_plane[2]);
+  /* out[x] = p[x] - (mul * v_plane[x]) */
+  madd_v3_v3v3fl(out, p, v_plane, -mul);
 }
 
 void project_plane_normalized_v2_v2v2(float out[2], const float p[2], const float v_plane[2])
 {
   BLI_ASSERT_UNIT_V2(v_plane);
   const float mul = dot_v2v2(p, v_plane);
-
-  out[0] = p[0] - (mul * v_plane[0]);
-  out[1] = p[1] - (mul * v_plane[1]);
+  /* out[x] = p[x] - (mul * v_plane[x]) */
+  madd_v2_v2v2fl(out, p, v_plane, -mul);
 }
 
 /* project a vector on a plane defined by normal and a plane point p */
@@ -782,9 +765,8 @@ void project_v3_plane(float out[3], const float plane_no[3], const float plane_c
   sub_v3_v3v3(vector, out, plane_co);
   mul = dot_v3v3(vector, plane_no) / len_squared_v3(plane_no);
 
-  mul_v3_v3fl(vector, plane_no, mul);
-
-  sub_v3_v3(out, vector);
+  /* out[x] = out[x] - (mul * plane_no[x]) */
+  madd_v3_v3fl(out, plane_no, -mul);
 }
 
 /* Returns a vector bisecting the angle at b formed by a, b and c */
@@ -817,24 +799,18 @@ void bisect_v3_v3v3v3(float r[3], const float a[3], const float b[3], const floa
  */
 void reflect_v3_v3v3(float out[3], const float v[3], const float normal[3])
 {
-  const float dot2 = 2.0f * dot_v3v3(v, normal);
-
   BLI_ASSERT_UNIT_V3(normal);
-
-  out[0] = v[0] - (dot2 * normal[0]);
-  out[1] = v[1] - (dot2 * normal[1]);
-  out[2] = v[2] - (dot2 * normal[2]);
+  const float dot2 = 2.0f * dot_v3v3(v, normal);
+  /* out[x] = v[x] - (dot2 * normal[x]) */
+  madd_v3_v3v3fl(out, v, normal, -dot2);
 }
 
 void reflect_v3_v3v3_db(double out[3], const double v[3], const double normal[3])
 {
+  BLI_ASSERT_UNIT_V3_DB(normal);
   const double dot2 = 2.0 * dot_v3v3_db(v, normal);
-
-  /* BLI_ASSERT_UNIT_V3_DB(normal); this assert is not known? */
-
-  out[0] = v[0] - (dot2 * normal[0]);
-  out[1] = v[1] - (dot2 * normal[1]);
-  out[2] = v[2] - (dot2 * normal[2]);
+  /* out[x] = v[x] - (dot2 * normal[x]) */
+  madd_v3_v3v3db_db(out, v, normal, -dot2);
 }
 
 /**
diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c
index e0df9665a7e..db9ece81c59 100644
--- a/source/blender/blenlib/intern/math_vector_inline.c
+++ b/source/blender/blenlib/intern/math_vector_inline.c
@@ -722,6 +722,13 @@ MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], floa
   r[2] = a[2] + b[2] * f;
 }
 
+MINLINE void madd_v3_v3v3db_db(double r[3], const double a[3], const double b[3], double f)
+{
+  r[0] = a[0] + b[0] * f;
+  r[1] = a[1] + b[1] * f;
+  r[2] = a[2] + b[2] * f;
+}
+
 MINLINE void madd_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3])
 {
   r[0] = a[0] + b[0] * c[0];



More information about the Bf-blender-cvs mailing list