diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-01-11 16:19:35 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-01-11 16:19:35 +0300 |
commit | f72f4d96bf007ef8caf98663912c4218535fdf8d (patch) | |
tree | f89634e079c3f5240c05b1d58fb241c428484506 | |
parent | c3b3821cce2a4eab4126e526f85121a6b671c930 (diff) |
Deduplicate cross_poly implementation
-rw-r--r-- | source/blender/blenlib/BLI_double3.hh | 6 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vector.hh | 25 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_mpq3.hh | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_vec.cc | 45 | ||||
-rw-r--r-- | source/blender/blenlib/intern/mesh_intersect.cc | 4 |
5 files changed, 27 insertions, 55 deletions
diff --git a/source/blender/blenlib/BLI_double3.hh b/source/blender/blenlib/BLI_double3.hh index cfc39c4ad0a..87b34a6c147 100644 --- a/source/blender/blenlib/BLI_double3.hh +++ b/source/blender/blenlib/BLI_double3.hh @@ -27,10 +27,4 @@ namespace blender { using double3 = vec_base<double, 3>; -namespace math { - -double3 cross_poly(Span<double3> poly); - -} // namespace math - } // namespace blender diff --git a/source/blender/blenlib/BLI_math_vector.hh b/source/blender/blenlib/BLI_math_vector.hh index 9b485bf5187..b9b0d1fcbb2 100644 --- a/source/blender/blenlib/BLI_math_vector.hh +++ b/source/blender/blenlib/BLI_math_vector.hh @@ -27,6 +27,7 @@ #include "BLI_math_base_safe.h" #include "BLI_math_vector.h" +#include "BLI_span.hh" #include "BLI_utildefines.h" #ifdef WITH_GMP @@ -317,6 +318,30 @@ inline T cross_high_precision(const T &a, const T &b) (float)((double)a.x * b.y - (double)a.y * b.x)}; } +template<typename T, BLI_ENABLE_IF_FLT_VEC(T), BLI_ENABLE_IF((T::type_length == 3))> +inline T cross_poly(Span<T> poly) +{ + /* Newell's Method. */ + int nv = static_cast<int>(poly.size()); + if (nv < 3) { + return T(0, 0, 0); + } + const T *v_prev = &poly[nv - 1]; + const T *v_curr = &poly[0]; + T 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; +} + template<typename T, BLI_ENABLE_IF_FLT_VEC(T)> inline T interpolate(const T &a, const T &b, bT t) { return a * (1 - t) + b * t; diff --git a/source/blender/blenlib/BLI_mpq3.hh b/source/blender/blenlib/BLI_mpq3.hh index 985222cb706..4bc9564a502 100644 --- a/source/blender/blenlib/BLI_mpq3.hh +++ b/source/blender/blenlib/BLI_mpq3.hh @@ -35,8 +35,6 @@ using mpq3 = vec_base<mpq_class, 3>; namespace math { -mpq3 cross_poly(Span<mpq3> poly); - uint64_t hash_mpq_class(const mpq_class &value); template<> inline uint64_t vector_hash(const mpq3 &vec) diff --git a/source/blender/blenlib/intern/math_vec.cc b/source/blender/blenlib/intern/math_vec.cc index e1d3707f1e8..a4c825327d2 100644 --- a/source/blender/blenlib/intern/math_vec.cc +++ b/source/blender/blenlib/intern/math_vec.cc @@ -122,52 +122,7 @@ isect_result<mpq2> isect_seg_seg(const mpq2 &v1, const mpq2 &v2, const mpq2 &v3, } #endif -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 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) { diff --git a/source/blender/blenlib/intern/mesh_intersect.cc b/source/blender/blenlib/intern/mesh_intersect.cc index 2485c222ee6..6f559311fb0 100644 --- a/source/blender/blenlib/intern/mesh_intersect.cc +++ b/source/blender/blenlib/intern/mesh_intersect.cc @@ -198,7 +198,7 @@ void Face::populate_plane(bool need_exact) for (int i : index_range()) { co[i] = vert[i]->co_exact; } - normal_exact = math::cross_poly(co); + normal_exact = math::cross_poly(co.as_span()); } else { mpq3 tr02 = vert[0]->co_exact - vert[2]->co_exact; @@ -215,7 +215,7 @@ void Face::populate_plane(bool need_exact) for (int i : index_range()) { co[i] = vert[i]->co; } - normal = math::cross_poly(co); + normal = math::cross_poly(co.as_span()); } else { double3 tr02 = vert[0]->co - vert[2]->co; |