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
This commit is contained in:
2022-02-15 10:27:03 -06:00
parent 3ed3ee253b
commit a9f023e226
22 changed files with 64 additions and 49 deletions

View File

@@ -4,7 +4,8 @@
#include "BLI_array.hh" #include "BLI_array.hh"
#include "BLI_color.hh" #include "BLI_color.hh"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.h"
#include "BLI_math_vector.hh"
#include "DNA_customdata_types.h" #include "DNA_customdata_types.h"

View File

@@ -17,7 +17,7 @@
#include "BLI_index_range.hh" #include "BLI_index_range.hh"
#include "BLI_listbase.h" #include "BLI_listbase.h"
#include "BLI_math_base.h" #include "BLI_math_base.h"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
#include "BLI_rand.hh" #include "BLI_rand.hh"
#include "BLI_string.h" #include "BLI_string.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"

View File

@@ -26,7 +26,7 @@
#include "BLI_linklist.h" #include "BLI_linklist.h"
#include "BLI_listbase.h" #include "BLI_listbase.h"
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
#include "BLI_memarena.h" #include "BLI_memarena.h"
#include "BLI_string.h" #include "BLI_string.h"
#include "BLI_task.hh" #include "BLI_task.hh"

View File

@@ -17,6 +17,7 @@
#include "BLI_array.hh" #include "BLI_array.hh"
#include "BLI_index_range.hh" #include "BLI_index_range.hh"
#include "BLI_math_vec_types.hh" #include "BLI_math_vec_types.hh"
#include "BLI_math_vector.h"
#include "BLI_span.hh" #include "BLI_span.hh"
#include "DNA_mesh_types.h" #include "DNA_mesh_types.h"

View File

@@ -6,6 +6,7 @@
#include "BKE_tracking.h" #include "BKE_tracking.h"
#include "BLI_math_vec_types.hh" #include "BLI_math_vec_types.hh"
#include "BLI_math_vector.h"
namespace blender { namespace blender {

View File

@@ -5,7 +5,7 @@
#include "FN_multi_function_builder.hh" #include "FN_multi_function_builder.hh"
#include "BLI_color.hh" #include "BLI_color.hh"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
namespace blender::bke { namespace blender::bke {
@@ -72,7 +72,7 @@ static int float2_to_int(const float2 &a)
} }
static bool float2_to_bool(const float2 &a) static bool float2_to_bool(const float2 &a)
{ {
return !is_zero_v2(a); return !math::is_zero(a);
} }
static int8_t float2_to_int8(const float2 &a) static int8_t float2_to_int8(const float2 &a)
{ {
@@ -85,7 +85,7 @@ static ColorGeometry4f float2_to_color(const float2 &a)
static bool float3_to_bool(const float3 &a) static bool float3_to_bool(const float3 &a)
{ {
return !is_zero_v3(a); return !math::is_zero(a);
} }
static int8_t float3_to_int8(const float3 &a) static int8_t float3_to_int8(const float3 &a)
{ {

View File

@@ -5,6 +5,7 @@
#include "BLI_math_matrix.h" #include "BLI_math_matrix.h"
#include "BLI_math_vec_types.hh" #include "BLI_math_vec_types.hh"
#include "BLI_math_vector.h" #include "BLI_math_vector.h"
#include "BLI_math_vector.hh"
namespace blender { namespace blender {

View File

@@ -6,7 +6,7 @@
* \ingroup bli * \ingroup bli
*/ */
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
#ifdef WITH_GMP #ifdef WITH_GMP

View File

@@ -12,7 +12,6 @@
#include <iostream> #include <iostream>
#include <type_traits> #include <type_traits>
#include "BLI_math_vector.hh"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
namespace blender { namespace blender {
@@ -41,6 +40,38 @@ template<typename T> struct vec_struct_base<T, 4> {
T x, y, z, w; 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> { template<typename T, int Size> struct vec_base : public vec_struct_base<T, Size> {
static constexpr int type_length = 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) 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]); 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) 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]); 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) 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]); 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_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]); 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_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]); BLI_VEC_OP_IMPL(ret, i, ret[i] = a % b[i]);
} }

View File

@@ -11,7 +11,7 @@
#include <type_traits> #include <type_traits>
#include "BLI_math_base_safe.h" #include "BLI_math_base_safe.h"
#include "BLI_math_vector.h" #include "BLI_math_vec_types.hh"
#include "BLI_span.hh" #include "BLI_span.hh"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
@@ -55,16 +55,6 @@ template<typename T> inline bool is_zero(const T &a)
return true; 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) template<typename T> inline T abs(const T &a)
{ {
T result; 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); 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) template<typename T, BLI_ENABLE_IF_FLT_VEC(T)> inline T reflect(const T &incident, const T &normal)
{ {
BLI_ASSERT_UNIT(normal); BLI_ASSERT_UNIT(normal);

View File

@@ -22,7 +22,7 @@
# include "BLI_math_geom.h" # include "BLI_math_geom.h"
# include "BLI_math_mpq.hh" # include "BLI_math_mpq.hh"
# include "BLI_math_vec_mpq_types.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_mesh_intersect.hh"
# include "BLI_set.hh" # include "BLI_set.hh"
# include "BLI_span.hh" # include "BLI_span.hh"

View File

@@ -23,6 +23,7 @@
# include "BLI_math_mpq.hh" # include "BLI_math_mpq.hh"
# include "BLI_math_vec_mpq_types.hh" # include "BLI_math_vec_mpq_types.hh"
# include "BLI_math_vec_types.hh" # include "BLI_math_vec_types.hh"
# include "BLI_math_vector.h"
# include "BLI_polyfill_2d.h" # include "BLI_polyfill_2d.h"
# include "BLI_set.hh" # include "BLI_set.hh"
# include "BLI_span.hh" # include "BLI_span.hh"

View File

@@ -7,7 +7,7 @@
#include <cstdint> #include <cstdint>
#include "BLI_math_base_safe.h" #include "BLI_math_base_safe.h"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
#include "BLI_noise.hh" #include "BLI_noise.hh"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"

View File

@@ -7,7 +7,8 @@
#pragma once #pragma once
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.h"
#include "BLI_math_vector.hh"
#include "BLI_vector.hh" #include "BLI_vector.hh"
#include "BKE_node.h" #include "BKE_node.h"

View File

@@ -3,6 +3,7 @@
#include "BLI_array.hh" #include "BLI_array.hh"
#include "BLI_index_mask.hh" #include "BLI_index_mask.hh"
#include "BLI_kdtree.h" #include "BLI_kdtree.h"
#include "BLI_math_vector.h"
#include "BLI_math_vector.hh" #include "BLI_math_vector.hh"
#include "BLI_vector.hh" #include "BLI_vector.hh"

View File

@@ -8,7 +8,8 @@
#include "BKE_node.h" #include "BKE_node.h"
#include "BLI_map.hh" #include "BLI_map.hh"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.h"
#include "BLI_math_vector.hh"
#include "BLI_path_util.h" #include "BLI_path_util.h"
#include "DNA_material_types.h" #include "DNA_material_types.h"

View File

@@ -6,7 +6,7 @@
#include "BLI_math_base_safe.h" #include "BLI_math_base_safe.h"
#include "BLI_math_rotation.h" #include "BLI_math_rotation.h"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
#include "BLI_string_ref.hh" #include "BLI_string_ref.hh"
namespace blender::nodes { namespace blender::nodes {

View File

@@ -5,6 +5,8 @@
* \ingroup cmpnodes * \ingroup cmpnodes
*/ */
#include "BLI_math_rotation.h"
#include "UI_interface.h" #include "UI_interface.h"
#include "UI_resources.h" #include "UI_resources.h"

View File

@@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "MEM_guardedalloc.h" #include "MEM_guardedalloc.h"

View File

@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */ /* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BLI_math_vector.h"
#include "BLI_task.hh" #include "BLI_task.hh"
#include "RNA_enum_types.h" #include "RNA_enum_types.h"

View File

@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */ /* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BLI_listbase.h" #include "BLI_listbase.h"
#include "BLI_math_vector.h"
#include "BLI_string.h" #include "BLI_string.h"
#include "RNA_enum_types.h" #include "RNA_enum_types.h"

View File

@@ -15,7 +15,7 @@
#include "BLI_color.hh" #include "BLI_color.hh"
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_math_base_safe.h" #include "BLI_math_base_safe.h"
#include "BLI_math_vec_types.hh" #include "BLI_math_vector.hh"
#include "BLI_rand.h" #include "BLI_rand.h"
#include "BLI_threads.h" #include "BLI_threads.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"