diff options
author | Hans Goudey <h.goudey@me.com> | 2022-02-15 19:27:03 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-02-15 19:27:03 +0300 |
commit | a9f023e226389461b1140f80e62f650d009a06a5 (patch) | |
tree | 2d3b860bf5d5996e77b102673bd0cbaef4d15f26 /source/blender/blenlib | |
parent | 3ed3ee253b12fe4d481357386650a13de244caf9 (diff) |
BLI: Change dependencies in vector math files
This patch reverses the dependency between `BLI_math_vec_types.hh` and
`BLI_math_vector.hh`. Now the higher level `blender::math` functions
depend on the header that defines the types they work with, rather than
the other way around.
The initial goal was to allow defining an `enable_if` in the types header
and using it in the math header. But I also think this operations to types
dependency is more natural anyway.
This required changing the includes some files used from the type
header to the math implementation header. I took that change a bit
further removing the C vector math header from the C++ header;
I think that helps to make the transition between the two systems
clearer.
Differential Revision: https://developer.blender.org/D14112
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_float4x4.hh | 1 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vec_mpq_types.hh | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vec_types.hh | 43 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vector.hh | 30 | ||||
-rw-r--r-- | source/blender/blenlib/intern/mesh_boolean.cc | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/mesh_intersect.cc | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/noise.cc | 2 |
7 files changed, 43 insertions, 38 deletions
diff --git a/source/blender/blenlib/BLI_float4x4.hh b/source/blender/blenlib/BLI_float4x4.hh index 60765aaec09..9f7fbffc692 100644 --- a/source/blender/blenlib/BLI_float4x4.hh +++ b/source/blender/blenlib/BLI_float4x4.hh @@ -5,6 +5,7 @@ #include "BLI_math_matrix.h" #include "BLI_math_vec_types.hh" #include "BLI_math_vector.h" +#include "BLI_math_vector.hh" namespace blender { diff --git a/source/blender/blenlib/BLI_math_vec_mpq_types.hh b/source/blender/blenlib/BLI_math_vec_mpq_types.hh index a7dad2921dd..45c416a5734 100644 --- a/source/blender/blenlib/BLI_math_vec_mpq_types.hh +++ b/source/blender/blenlib/BLI_math_vec_mpq_types.hh @@ -6,7 +6,7 @@ * \ingroup bli */ -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" #ifdef WITH_GMP diff --git a/source/blender/blenlib/BLI_math_vec_types.hh b/source/blender/blenlib/BLI_math_vec_types.hh index 192284cb8fc..ad885bde27d 100644 --- a/source/blender/blenlib/BLI_math_vec_types.hh +++ b/source/blender/blenlib/BLI_math_vec_types.hh @@ -12,7 +12,6 @@ #include <iostream> #include <type_traits> -#include "BLI_math_vector.hh" #include "BLI_utildefines.h" namespace blender { @@ -41,6 +40,38 @@ template<typename T> struct vec_struct_base<T, 4> { T x, y, z, w; }; +namespace math { + +template<typename T> uint64_t vector_hash(const T &vec) +{ + BLI_STATIC_ASSERT(T::type_length <= 4, "Longer types need to implement vector_hash themself."); + const typename T::uint_type &uvec = *reinterpret_cast<const typename T::uint_type *>(&vec); + uint64_t result; + result = uvec[0] * uint64_t(435109); + if constexpr (T::type_length > 1) { + result ^= uvec[1] * uint64_t(380867); + } + if constexpr (T::type_length > 2) { + result ^= uvec[2] * uint64_t(1059217); + } + if constexpr (T::type_length > 3) { + result ^= uvec[3] * uint64_t(2002613); + } + return result; +} + +template<typename T> inline bool is_any_zero(const T &a) +{ + for (int i = 0; i < T::type_length; i++) { + if (a[i] == T::base_type(0)) { + return true; + } + } + return false; +} + +} // namespace math + template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> { static constexpr int type_length = Size; @@ -318,7 +349,7 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> friend vec_base operator/(const vec_base &a, const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + BLI_assert(!math::is_any_zero()); BLI_VEC_OP_IMPL(ret, i, ret[i] = a[i] / b[i]); } @@ -330,7 +361,7 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> friend vec_base operator/(T a, const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + BLI_assert(!math::is_any_zero()); BLI_VEC_OP_IMPL(ret, i, ret[i] = a / b[i]); } @@ -342,7 +373,7 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> vec_base &operator/=(const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + BLI_assert(!b != T(0)); BLI_VEC_OP_IMPL_SELF(i, (*this)[i] /= b[i]); } @@ -474,7 +505,7 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> BLI_INT_OP(T) friend vec_base operator%(const vec_base &a, const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + BLI_assert(!math::is_any_zero()); BLI_VEC_OP_IMPL(ret, i, ret[i] = a[i] % b[i]); } @@ -486,7 +517,7 @@ template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> BLI_INT_OP(T) friend vec_base operator%(T a, const vec_base &b) { - BLI_assert(!math::is_any_zero(b)); + BLI_assert(b != T(0)); BLI_VEC_OP_IMPL(ret, i, ret[i] = a % b[i]); } diff --git a/source/blender/blenlib/BLI_math_vector.hh b/source/blender/blenlib/BLI_math_vector.hh index 930f3b62fa5..d2ef2a1c5c8 100644 --- a/source/blender/blenlib/BLI_math_vector.hh +++ b/source/blender/blenlib/BLI_math_vector.hh @@ -11,7 +11,7 @@ #include <type_traits> #include "BLI_math_base_safe.h" -#include "BLI_math_vector.h" +#include "BLI_math_vec_types.hh" #include "BLI_span.hh" #include "BLI_utildefines.h" @@ -55,16 +55,6 @@ template<typename T> inline bool is_zero(const T &a) return true; } -template<typename T> inline bool is_any_zero(const T &a) -{ - for (int i = 0; i < T::type_length; i++) { - if (a[i] == bT(0)) { - return true; - } - } - return false; -} - template<typename T> inline T abs(const T &a) { T result; @@ -241,24 +231,6 @@ template<typename T, BLI_ENABLE_IF_FLT_VEC(T)> inline bT distance(const T &a, co return length(a - b); } -template<typename T> uint64_t vector_hash(const T &vec) -{ - BLI_STATIC_ASSERT(T::type_length <= 4, "Longer types need to implement vector_hash themself."); - const typename T::uint_type &uvec = *reinterpret_cast<const typename T::uint_type *>(&vec); - uint64_t result; - result = uvec[0] * uint64_t(435109); - if constexpr (T::type_length > 1) { - result ^= uvec[1] * uint64_t(380867); - } - if constexpr (T::type_length > 2) { - result ^= uvec[2] * uint64_t(1059217); - } - if constexpr (T::type_length > 3) { - result ^= uvec[3] * uint64_t(2002613); - } - return result; -} - template<typename T, BLI_ENABLE_IF_FLT_VEC(T)> inline T reflect(const T &incident, const T &normal) { BLI_ASSERT_UNIT(normal); diff --git a/source/blender/blenlib/intern/mesh_boolean.cc b/source/blender/blenlib/intern/mesh_boolean.cc index 3cdc047f5fe..6e2e9787ebe 100644 --- a/source/blender/blenlib/intern/mesh_boolean.cc +++ b/source/blender/blenlib/intern/mesh_boolean.cc @@ -22,7 +22,7 @@ # include "BLI_math_geom.h" # include "BLI_math_mpq.hh" # include "BLI_math_vec_mpq_types.hh" -# include "BLI_math_vec_types.hh" +# include "BLI_math_vector.hh" # include "BLI_mesh_intersect.hh" # include "BLI_set.hh" # include "BLI_span.hh" diff --git a/source/blender/blenlib/intern/mesh_intersect.cc b/source/blender/blenlib/intern/mesh_intersect.cc index 5295c44c991..8f2c86556aa 100644 --- a/source/blender/blenlib/intern/mesh_intersect.cc +++ b/source/blender/blenlib/intern/mesh_intersect.cc @@ -23,6 +23,7 @@ # include "BLI_math_mpq.hh" # include "BLI_math_vec_mpq_types.hh" # include "BLI_math_vec_types.hh" +# include "BLI_math_vector.h" # include "BLI_polyfill_2d.h" # include "BLI_set.hh" # include "BLI_span.hh" diff --git a/source/blender/blenlib/intern/noise.cc b/source/blender/blenlib/intern/noise.cc index 24af186c1d5..a514c9e5183 100644 --- a/source/blender/blenlib/intern/noise.cc +++ b/source/blender/blenlib/intern/noise.cc @@ -7,7 +7,7 @@ #include <cstdint> #include "BLI_math_base_safe.h" -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" #include "BLI_noise.hh" #include "BLI_utildefines.h" |