From a9f023e226389461b1140f80e62f650d009a06a5 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 15 Feb 2022 10:27:03 -0600 Subject: 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 --- source/blender/blenkernel/BKE_attribute_math.hh | 3 +- source/blender/blenkernel/intern/curves.cc | 2 +- source/blender/blenkernel/intern/mesh.cc | 2 +- .../blender/blenkernel/intern/mesh_remesh_voxel.cc | 1 + source/blender/blenkernel/intern/tracking_test.cc | 1 + .../blender/blenkernel/intern/type_conversions.cc | 6 +-- source/blender/blenlib/BLI_float4x4.hh | 1 + source/blender/blenlib/BLI_math_vec_mpq_types.hh | 2 +- source/blender/blenlib/BLI_math_vec_types.hh | 43 +++++++++++++++++++--- source/blender/blenlib/BLI_math_vector.hh | 30 +-------------- source/blender/blenlib/intern/mesh_boolean.cc | 2 +- source/blender/blenlib/intern/mesh_intersect.cc | 1 + source/blender/blenlib/intern/noise.cc | 2 +- source/blender/editors/space_node/node_intern.hh | 3 +- .../geometry/intern/mesh_merge_by_distance.cc | 1 + .../io/wavefront_obj/exporter/obj_export_mtl.cc | 3 +- source/blender/nodes/NOD_math_functions.hh | 2 +- .../composite/nodes/node_composite_chroma_matte.cc | 2 + .../blender/nodes/function/node_function_util.hh | 2 +- .../nodes/node_fn_align_euler_to_vector.cc | 1 + .../nodes/function/nodes/node_fn_rotate_euler.cc | 1 + source/blender/nodes/shader/node_shader_util.hh | 2 +- 22 files changed, 64 insertions(+), 49 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/BKE_attribute_math.hh b/source/blender/blenkernel/BKE_attribute_math.hh index a81ab78a621..42bff89922b 100644 --- a/source/blender/blenkernel/BKE_attribute_math.hh +++ b/source/blender/blenkernel/BKE_attribute_math.hh @@ -4,7 +4,8 @@ #include "BLI_array.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" diff --git a/source/blender/blenkernel/intern/curves.cc b/source/blender/blenkernel/intern/curves.cc index 097cfbf3101..ccc20d5118a 100644 --- a/source/blender/blenkernel/intern/curves.cc +++ b/source/blender/blenkernel/intern/curves.cc @@ -17,7 +17,7 @@ #include "BLI_index_range.hh" #include "BLI_listbase.h" #include "BLI_math_base.h" -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" #include "BLI_rand.hh" #include "BLI_string.h" #include "BLI_utildefines.h" diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc index 4e855b6dc2c..6ca5babbf13 100644 --- a/source/blender/blenkernel/intern/mesh.cc +++ b/source/blender/blenkernel/intern/mesh.cc @@ -26,7 +26,7 @@ #include "BLI_linklist.h" #include "BLI_listbase.h" #include "BLI_math.h" -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" #include "BLI_memarena.h" #include "BLI_string.h" #include "BLI_task.hh" diff --git a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc index 5067d67a6c8..b34a241bca8 100644 --- a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc +++ b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc @@ -17,6 +17,7 @@ #include "BLI_array.hh" #include "BLI_index_range.hh" #include "BLI_math_vec_types.hh" +#include "BLI_math_vector.h" #include "BLI_span.hh" #include "DNA_mesh_types.h" diff --git a/source/blender/blenkernel/intern/tracking_test.cc b/source/blender/blenkernel/intern/tracking_test.cc index 7c93e4b5406..42bbf23ea44 100644 --- a/source/blender/blenkernel/intern/tracking_test.cc +++ b/source/blender/blenkernel/intern/tracking_test.cc @@ -6,6 +6,7 @@ #include "BKE_tracking.h" #include "BLI_math_vec_types.hh" +#include "BLI_math_vector.h" namespace blender { diff --git a/source/blender/blenkernel/intern/type_conversions.cc b/source/blender/blenkernel/intern/type_conversions.cc index 4ab945d14c6..2e8b5b3433b 100644 --- a/source/blender/blenkernel/intern/type_conversions.cc +++ b/source/blender/blenkernel/intern/type_conversions.cc @@ -5,7 +5,7 @@ #include "FN_multi_function_builder.hh" #include "BLI_color.hh" -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" namespace blender::bke { @@ -72,7 +72,7 @@ static int float2_to_int(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) { @@ -85,7 +85,7 @@ static ColorGeometry4f float2_to_color(const float2 &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) { 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 #include -#include "BLI_math_vector.hh" #include "BLI_utildefines.h" namespace blender { @@ -41,6 +40,38 @@ template struct vec_struct_base { T x, y, z, w; }; +namespace math { + +template 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(&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 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 struct vec_base : public vec_struct_base { static constexpr int type_length = Size; @@ -318,7 +349,7 @@ template struct vec_base : public vec_struct_base 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 struct vec_base : public vec_struct_base 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 struct vec_base : public vec_struct_base 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 struct vec_base : public vec_struct_base 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 struct vec_base : public vec_struct_base 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 #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 inline bool is_zero(const T &a) return true; } -template 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 inline T abs(const T &a) { T result; @@ -241,24 +231,6 @@ template inline bT distance(const T &a, co return length(a - b); } -template 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(&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 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 #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" diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh index 8f0ff1ccd23..592db3f7877 100644 --- a/source/blender/editors/space_node/node_intern.hh +++ b/source/blender/editors/space_node/node_intern.hh @@ -7,7 +7,8 @@ #pragma once -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.h" +#include "BLI_math_vector.hh" #include "BLI_vector.hh" #include "BKE_node.h" diff --git a/source/blender/geometry/intern/mesh_merge_by_distance.cc b/source/blender/geometry/intern/mesh_merge_by_distance.cc index 0b47a5d3b83..6dc6271194b 100644 --- a/source/blender/geometry/intern/mesh_merge_by_distance.cc +++ b/source/blender/geometry/intern/mesh_merge_by_distance.cc @@ -3,6 +3,7 @@ #include "BLI_array.hh" #include "BLI_index_mask.hh" #include "BLI_kdtree.h" +#include "BLI_math_vector.h" #include "BLI_math_vector.hh" #include "BLI_vector.hh" diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc b/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc index 9d8d4850709..b8814b1bdd3 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc @@ -8,7 +8,8 @@ #include "BKE_node.h" #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 "DNA_material_types.h" diff --git a/source/blender/nodes/NOD_math_functions.hh b/source/blender/nodes/NOD_math_functions.hh index 28e9595489a..c1f0f043c92 100644 --- a/source/blender/nodes/NOD_math_functions.hh +++ b/source/blender/nodes/NOD_math_functions.hh @@ -6,7 +6,7 @@ #include "BLI_math_base_safe.h" #include "BLI_math_rotation.h" -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" #include "BLI_string_ref.hh" namespace blender::nodes { diff --git a/source/blender/nodes/composite/nodes/node_composite_chroma_matte.cc b/source/blender/nodes/composite/nodes/node_composite_chroma_matte.cc index f899fbd1800..69319c6825d 100644 --- a/source/blender/nodes/composite/nodes/node_composite_chroma_matte.cc +++ b/source/blender/nodes/composite/nodes/node_composite_chroma_matte.cc @@ -5,6 +5,8 @@ * \ingroup cmpnodes */ +#include "BLI_math_rotation.h" + #include "UI_interface.h" #include "UI_resources.h" diff --git a/source/blender/nodes/function/node_function_util.hh b/source/blender/nodes/function/node_function_util.hh index 05130949d23..fd0b6c31b1d 100644 --- a/source/blender/nodes/function/node_function_util.hh +++ b/source/blender/nodes/function/node_function_util.hh @@ -4,7 +4,7 @@ #include -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" #include "BLI_utildefines.h" #include "MEM_guardedalloc.h" diff --git a/source/blender/nodes/function/nodes/node_fn_align_euler_to_vector.cc b/source/blender/nodes/function/nodes/node_fn_align_euler_to_vector.cc index e3d3b8f5ebc..2cb455832e5 100644 --- a/source/blender/nodes/function/nodes/node_fn_align_euler_to_vector.cc +++ b/source/blender/nodes/function/nodes/node_fn_align_euler_to_vector.cc @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ +#include "BLI_math_vector.h" #include "BLI_task.hh" #include "RNA_enum_types.h" diff --git a/source/blender/nodes/function/nodes/node_fn_rotate_euler.cc b/source/blender/nodes/function/nodes/node_fn_rotate_euler.cc index cf5f1f2d336..3718ce6f359 100644 --- a/source/blender/nodes/function/nodes/node_fn_rotate_euler.cc +++ b/source/blender/nodes/function/nodes/node_fn_rotate_euler.cc @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #include "BLI_listbase.h" +#include "BLI_math_vector.h" #include "BLI_string.h" #include "RNA_enum_types.h" diff --git a/source/blender/nodes/shader/node_shader_util.hh b/source/blender/nodes/shader/node_shader_util.hh index df63f8d8b62..d5f54d9cac9 100644 --- a/source/blender/nodes/shader/node_shader_util.hh +++ b/source/blender/nodes/shader/node_shader_util.hh @@ -15,7 +15,7 @@ #include "BLI_color.hh" #include "BLI_math.h" #include "BLI_math_base_safe.h" -#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.hh" #include "BLI_rand.h" #include "BLI_threads.h" #include "BLI_utildefines.h" -- cgit v1.2.3