From f72f4d96bf007ef8caf98663912c4218535fdf8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Tue, 11 Jan 2022 14:19:35 +0100 Subject: Deduplicate cross_poly implementation --- source/blender/blenlib/BLI_double3.hh | 6 ---- source/blender/blenlib/BLI_math_vector.hh | 25 ++++++++++++++ source/blender/blenlib/BLI_mpq3.hh | 2 -- source/blender/blenlib/intern/math_vec.cc | 45 ------------------------- 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; -namespace math { - -double3 cross_poly(Span 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 +inline T cross_poly(Span poly) +{ + /* Newell's Method. */ + int nv = static_cast(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 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; namespace math { -mpq3 cross_poly(Span 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 isect_seg_seg(const mpq2 &v1, const mpq2 &v2, const mpq2 &v3, } #endif -double3 cross_poly(Span poly) -{ - /* Newell's Method. */ - int nv = static_cast(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 poly) -{ - /* Newell's Method. */ - int nv = static_cast(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; -- cgit v1.2.3