diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:19:00 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:19:39 +0300 |
commit | a2c1c368af48644fa8995ecbe7138cc0d7900c30 (patch) | |
tree | 39cd435d2033f2952d2c39ce302816b037263c63 /source/blender/blenlib/intern/math_vec.cc | |
parent | d320f3677e2a98b12d386a3e6f3da2d7b7018463 (diff) |
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:float2) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the blender::math namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others we
currently don't have (uintX, intX). All these variations were asking
for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector functions
should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the BLI_(float|double|mpq)(2|3|4).hh is a bit of a
let down. Most clases are incomplete, out of sync with each others with
different codestyles, and some functions that should be static are not
(i.e: float3::reflect()).
Upsides:
- Still support .x, .y, .z, .w for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types and
can be restricted to certain types. Also template specialization let us
define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance is
the same.
Downsides:
- Might impact debugability. Though I would arge that the bugs are rarelly
caused by the vector class itself (since the operations are quite trivial)
but by the type conversions.
- Might impact compile time. I did not saw a significant impact since the
usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length. For
instance, one can't call len_squared_v3v3 in math::length_squared() and
call it a day.
- Type cast does not work with the template version of the math:: vector
functions. Meaning you need to manually cast float * and (float *)[3] to
float3 for the function calls.
i.e: math::distance_squared(float3(nearest.co), positions[i]);
- Some parts might loose in readability:
float3::dot(v1.normalized(), v2.normalized())
becoming
math::dot(math::normalize(v1), math::normalize(v2))
But I propose, when appropriate, to use
using namespace blender::math; on function local or file scope to
increase readability. dot(normalize(v1), normalize(v2))
Consideration:
- Include back .length() method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement.
It felt like too much for what we need and would be difficult to
extend / modify to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches delaunay_2d.cc and the intersection code. I would like to
know @Howard Trickey (howardt) opinion on the matter.
- The noexcept on the copy constructor of mpq(2|3) is being removed.
But according to @Jacques Lucke (JacquesLucke) it is not a real problem
for now.
I would like to give a huge thanks to @Jacques Lucke (JacquesLucke) who
helped during this and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: http://developer.blender.org/D13791
Diffstat (limited to 'source/blender/blenlib/intern/math_vec.cc')
-rw-r--r-- | source/blender/blenlib/intern/math_vec.cc | 133 |
1 files changed, 34 insertions, 99 deletions
diff --git a/source/blender/blenlib/intern/math_vec.cc b/source/blender/blenlib/intern/math_vec.cc index 223c0e273f0..6fab6c9a383 100644 --- a/source/blender/blenlib/intern/math_vec.cc +++ b/source/blender/blenlib/intern/math_vec.cc @@ -18,89 +18,83 @@ * \ingroup bli */ -#include "BLI_double2.hh" -#include "BLI_double3.hh" -#include "BLI_float2.hh" -#include "BLI_float3.hh" #include "BLI_hash.hh" -#include "BLI_math_mpq.hh" -#include "BLI_mpq2.hh" -#include "BLI_mpq3.hh" +#include "BLI_math_vec_mpq_types.hh" +#include "BLI_math_vector.hh" #include "BLI_span.hh" #include "BLI_utildefines.h" -namespace blender { +namespace blender::math { -float2::isect_result float2::isect_seg_seg(const float2 &v1, - const float2 &v2, - const float2 &v3, - const float2 &v4) +template<> +isect_result<float2> isect_seg_seg(const float2 &v1, + const float2 &v2, + const float2 &v3, + const float2 &v4) { - float2::isect_result ans; + isect_result<float2> ans; float div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]); if (div == 0.0f) { ans.lambda = 0.0f; - ans.mu = 0.0f; - ans.kind = float2::isect_result::LINE_LINE_COLINEAR; + ans.kind = isect_result<float2>::LINE_LINE_COLINEAR; } else { ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div; - ans.mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div; - if (ans.lambda >= 0.0f && ans.lambda <= 1.0f && ans.mu >= 0.0f && ans.mu <= 1.0f) { - if (ans.lambda == 0.0f || ans.lambda == 1.0f || ans.mu == 0.0f || ans.mu == 1.0f) { - ans.kind = float2::isect_result::LINE_LINE_EXACT; + float mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div; + if (ans.lambda >= 0.0f && ans.lambda <= 1.0f && mu >= 0.0f && mu <= 1.0f) { + if (ans.lambda == 0.0f || ans.lambda == 1.0f || mu == 0.0f || mu == 1.0f) { + ans.kind = isect_result<float2>::LINE_LINE_EXACT; } else { - ans.kind = float2::isect_result::LINE_LINE_CROSS; + ans.kind = isect_result<float2>::LINE_LINE_CROSS; } } else { - ans.kind = float2::isect_result::LINE_LINE_NONE; + ans.kind = isect_result<float2>::LINE_LINE_NONE; } } return ans; } -double2::isect_result double2::isect_seg_seg(const double2 &v1, - const double2 &v2, - const double2 &v3, - const double2 &v4) +template<> +isect_result<double2> isect_seg_seg(const double2 &v1, + const double2 &v2, + const double2 &v3, + const double2 &v4) { - double2::isect_result ans; + isect_result<double2> ans; double div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]); if (div == 0.0) { ans.lambda = 0.0; - ans.kind = double2::isect_result::LINE_LINE_COLINEAR; + ans.kind = isect_result<double2>::LINE_LINE_COLINEAR; } else { ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div; double mu = ((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div; if (ans.lambda >= 0.0 && ans.lambda <= 1.0 && mu >= 0.0 && mu <= 1.0) { if (ans.lambda == 0.0 || ans.lambda == 1.0 || mu == 0.0 || mu == 1.0) { - ans.kind = double2::isect_result::LINE_LINE_EXACT; + ans.kind = isect_result<double2>::LINE_LINE_EXACT; } else { - ans.kind = double2::isect_result::LINE_LINE_CROSS; + ans.kind = isect_result<double2>::LINE_LINE_CROSS; } } else { - ans.kind = double2::isect_result::LINE_LINE_NONE; + ans.kind = isect_result<double2>::LINE_LINE_NONE; } } return ans; } #ifdef WITH_GMP -mpq2::isect_result mpq2::isect_seg_seg(const mpq2 &v1, - const mpq2 &v2, - const mpq2 &v3, - const mpq2 &v4) +template<> +isect_result<mpq2> isect_seg_seg(const mpq2 &v1, const mpq2 &v2, const mpq2 &v3, const mpq2 &v4) { - mpq2::isect_result ans; + isect_result<mpq2> ans; mpq_class div = (v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]); if (div == 0.0) { ans.lambda = 0.0; - ans.kind = mpq2::isect_result::LINE_LINE_COLINEAR; + ans.kind = isect_result<mpq2>::LINE_LINE_COLINEAR; } else { ans.lambda = ((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div; @@ -109,66 +103,21 @@ mpq2::isect_result mpq2::isect_seg_seg(const mpq2 &v1, if (ans.lambda >= 0 && ans.lambda <= 1 && ((div > 0 && mudiv >= 0 && mudiv <= div) || (div < 0 && mudiv <= 0 && mudiv >= div))) { if (ans.lambda == 0 || ans.lambda == 1 || mudiv == 0 || mudiv == div) { - ans.kind = mpq2::isect_result::LINE_LINE_EXACT; + ans.kind = isect_result<mpq2>::LINE_LINE_EXACT; } else { - ans.kind = mpq2::isect_result::LINE_LINE_CROSS; + ans.kind = isect_result<mpq2>::LINE_LINE_CROSS; } } else { - ans.kind = mpq2::isect_result::LINE_LINE_NONE; + ans.kind = isect_result<mpq2>::LINE_LINE_NONE; } } return ans; } #endif -double3 double3::cross_poly(Span<double3> poly) -{ - /* Newell's Method. */ - int nv = static_cast<int>(poly.size()); - if (nv < 3) { - return double3(0, 0, 0); - } - const double3 *v_prev = &poly[nv - 1]; - const double3 *v_curr = &poly[0]; - double3 n(0, 0, 0); - for (int i = 0; i < nv;) { - n[0] = n[0] + ((*v_prev)[1] - (*v_curr)[1]) * ((*v_prev)[2] + (*v_curr)[2]); - n[1] = n[1] + ((*v_prev)[2] - (*v_curr)[2]) * ((*v_prev)[0] + (*v_curr)[0]); - n[2] = n[2] + ((*v_prev)[0] - (*v_curr)[0]) * ((*v_prev)[1] + (*v_curr)[1]); - v_prev = v_curr; - ++i; - if (i < nv) { - v_curr = &poly[i]; - } - } - return n; -} - #ifdef WITH_GMP -mpq3 mpq3::cross_poly(Span<mpq3> poly) -{ - /* Newell's Method. */ - int nv = static_cast<int>(poly.size()); - if (nv < 3) { - return mpq3(0); - } - const mpq3 *v_prev = &poly[nv - 1]; - const mpq3 *v_curr = &poly[0]; - mpq3 n(0); - for (int i = 0; i < nv;) { - n[0] = n[0] + ((*v_prev)[1] - (*v_curr)[1]) * ((*v_prev)[2] + (*v_curr)[2]); - n[1] = n[1] + ((*v_prev)[2] - (*v_curr)[2]) * ((*v_prev)[0] + (*v_curr)[0]); - n[2] = n[2] + ((*v_prev)[0] - (*v_curr)[0]) * ((*v_prev)[1] + (*v_curr)[1]); - v_prev = v_curr; - ++i; - if (i < nv) { - v_curr = &poly[i]; - } - } - return n; -} uint64_t hash_mpq_class(const mpq_class &value) { @@ -176,20 +125,6 @@ uint64_t hash_mpq_class(const mpq_class &value) return get_default_hash(static_cast<float>(value.get_d())); } -uint64_t mpq2::hash() const -{ - uint64_t hashx = hash_mpq_class(this->x); - uint64_t hashy = hash_mpq_class(this->y); - return hashx ^ (hashy * 33); -} - -uint64_t mpq3::hash() const -{ - uint64_t hashx = hash_mpq_class(this->x); - uint64_t hashy = hash_mpq_class(this->y); - uint64_t hashz = hash_mpq_class(this->z); - return hashx ^ (hashy * 33) ^ (hashz * 33 * 37); -} #endif -} // namespace blender +} // namespace blender::math |