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:
authorHans Goudey <h.goudey@me.com>2022-02-15 19:27:03 +0300
committerHans Goudey <h.goudey@me.com>2022-02-15 19:27:03 +0300
commita9f023e226389461b1140f80e62f650d009a06a5 (patch)
tree2d3b860bf5d5996e77b102673bd0cbaef4d15f26 /source/blender/blenlib/BLI_math_vec_types.hh
parent3ed3ee253b12fe4d481357386650a13de244caf9 (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/BLI_math_vec_types.hh')
-rw-r--r--source/blender/blenlib/BLI_math_vec_types.hh43
1 files changed, 37 insertions, 6 deletions
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]);
}