From 29959d8cf5e78ca6eef1916010a254f0c6ef6629 Mon Sep 17 00:00:00 2001 From: Martin Heistermann Date: Mon, 30 Mar 2020 19:04:26 +0200 Subject: [PATCH 1/2] VectorT: Implement .cross() and .dot() methods. Aliases for operators % and | for better compatibility with Eigen3. --- src/OpenMesh/Core/Geometry/Vector11T.hh | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/OpenMesh/Core/Geometry/Vector11T.hh b/src/OpenMesh/Core/Geometry/Vector11T.hh index c11a77e9..44258377 100644 --- a/src/OpenMesh/Core/Geometry/Vector11T.hh +++ b/src/OpenMesh/Core/Geometry/Vector11T.hh @@ -369,7 +369,7 @@ class VectorT { } /// cross product: only defined for Vec3* as specialization - /// \see OpenMesh::cross + /// \see OpenMesh::cross and .cross() template auto operator% (const VectorT &_rhs) const -> typename std::enable_if + auto cross (const VectorT &_rhs) const -> + decltype(*this % _rhs) + { + return *this % _rhs; + } + + /// compute scalar product - /// \see OpenMesh::dot + /// \see OpenMesh::dot and .dot() template auto operator|(const VectorT& _rhs) const -> decltype(*this->data() * *_rhs.data()) { @@ -392,6 +402,15 @@ class VectorT { *begin() * *_rhs.begin()); } + /// compute scalar product + /// \see OpenMesh::dot and .operator| + template + auto dot(const VectorT& _rhs) const -> + decltype(*this | _rhs) + { + return *this | _rhs; + } + //------------------------------------------------------------ euclidean norm /// \name Euclidean norm calculations From dfa22839d08e2cab24435c058a6130606bc5909d Mon Sep 17 00:00:00 2001 From: Martin Heistermann Date: Mon, 6 Apr 2020 19:02:07 +0200 Subject: [PATCH 2/2] Implement vector dot/cross unit tests --- src/Unittests/unittests_vector_type.cc | 29 +++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Unittests/unittests_vector_type.cc b/src/Unittests/unittests_vector_type.cc index 878c3248..98643d9e 100644 --- a/src/Unittests/unittests_vector_type.cc +++ b/src/Unittests/unittests_vector_type.cc @@ -263,6 +263,23 @@ TEST_F(OpenMeshVectorTest, BasicArithmeticImmutable) { EXPECT_NEAR(-2, v2neg[1], epsilon); EXPECT_NEAR(-3, v2neg[2], epsilon); } +template +void test_dot(const V1 &v1, const V2 &v2, const S&s) { + EXPECT_NEAR(s, v1 | v2, 1e-6); + EXPECT_NEAR(s, v1.dot(v2), 1e-6); + EXPECT_NEAR(-s, (-v1) | v2, 1e-6); + EXPECT_NEAR(-s, (-v1).dot(v2), 1e-6); + EXPECT_NEAR(s, v2 | v1, 1e-6); + EXPECT_NEAR(s, v2.dot(v1), 1e-6); +} + +template +void test_cross(const V1 &v1, const V2 &v2, const V3 &res) { + EXPECT_NEAR(0, (v1.cross(v2) - res).norm(), 1e-6); + EXPECT_NEAR(0, (v1 % v2 - res).norm(), 1e-6); + EXPECT_NEAR(0, (v2.cross(v1) + res).norm(), 1e-6); + EXPECT_NEAR(0, (v2 % v1 + res).norm(), 1e-6); +} TEST_F(OpenMeshVectorTest, BasicLinearAlgebra) { OpenMesh::Vec3d v(1, 2, 3); @@ -285,9 +302,15 @@ TEST_F(OpenMeshVectorTest, BasicLinearAlgebra) { EXPECT_EQ(1, OpenMesh::Vec3d(1, 3, -4).min_abs()); EXPECT_EQ(2, OpenMesh::Vec3d(-4, 2, 3).min_abs()); - EXPECT_NEAR(14, OpenMesh::Vec3d(1, 2, 3) | OpenMesh::Vec3d(1, 2, 3), 1e-6); - EXPECT_NEAR(-14, OpenMesh::Vec3d(1, 2, 3) | OpenMesh::Vec3d(-1, -2, -3), 1e-6); - EXPECT_NEAR(14, OpenMesh::Vec3d(-1, -2, -3) | OpenMesh::Vec3d(-1, -2, -3), 1e-6); + + test_dot(OpenMesh::Vec3d(1, 2, 3), OpenMesh::Vec3d(1, 2, 3), 14.); + test_dot(OpenMesh::Vec3d(1, 2, 3), OpenMesh::Vec3d(-1, -2, -3), -14.); + test_dot(OpenMesh::Vec3d(1, 2, 3), OpenMesh::Vec3i(1, 2, 3), 14); + test_dot(OpenMesh::Vec3i(1, 2, 3), OpenMesh::Vec3i(1, 2, 3), 14); + + test_cross(OpenMesh::Vec3i(2, 0, 0), OpenMesh::Vec3i(0, 3, 0), OpenMesh::Vec3i(0, 0, 6)); + test_cross(OpenMesh::Vec3d(2, 0, 0), OpenMesh::Vec3d(0, 3, 0), OpenMesh::Vec3d(0, 0, 6)); + test_cross(OpenMesh::Vec3d(2, 0, 0), OpenMesh::Vec3i(0, 3, 0), OpenMesh::Vec3d(0, 0, 6)); } TEST_F(OpenMeshVectorTest, array_init) {