diff options
author | Bastien Montagne <bastien@blender.org> | 2021-06-14 13:30:11 +0300 |
---|---|---|
committer | Bastien Montagne <bastien@blender.org> | 2021-06-14 13:32:38 +0300 |
commit | 748475b9431c5b09514b69b042f0daaea1231dc1 (patch) | |
tree | 935b5661487a1603f2811ed8a5b38fe2747c4099 /source | |
parent | b21db5e6988566dc3344b3293eb7e33234ec2597 (diff) |
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.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_math_base.h | 5 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vector.h | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_vector.c | 70 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_vector_inline.c | 7 |
4 files changed, 34 insertions, 49 deletions
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]; |