From 4f6ab508d783d11f26ff508b912832869308682a Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 29 Aug 2023 15:12:18 +0200 Subject: [PATCH] Base: fix incorrect method names of Matrix class about diagonal and trace --- src/Base/Matrix.h | 26 ++++++++++++++++++++------ src/Base/MatrixPy.xml | 8 ++++---- src/Base/MatrixPyImp.cpp | 8 ++++---- src/Mod/Test/BaseTests.py | 4 ++-- tests/src/Base/Matrix.cpp | 11 ++++++----- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/Base/Matrix.h b/src/Base/Matrix.h index 2af93a1621..5569dea636 100644 --- a/src/Base/Matrix.h +++ b/src/Base/Matrix.h @@ -107,14 +107,18 @@ public: inline Vector3d getRow(unsigned short usNdx) const; /// Get vector of column inline Vector3d getCol(unsigned short usNdx) const; - /// Get vector of trace - inline Vector3d trace() const; + /// Get vector of diagonal + inline Vector3d diagonal() const; + /// Get trace of the 3x3 matrix + inline double trace3() const; + /// Get trace of the 4x4 matrix + inline double trace() const; /// Set row to vector inline void setRow(unsigned short usNdx, const Vector3d&); /// Set column to vector inline void setCol(unsigned short usNdx, const Vector3d&); - /// Set trace to vector - inline void setTrace(const Vector3d&); + /// Set diagonal to vector + inline void setDiagonal(const Vector3d&); /// Compute the determinant of the matrix double determinant() const; /// Compute the determinant of the 3x3 sub-matrix @@ -423,11 +427,21 @@ inline Vector3d Matrix4D::getCol(unsigned short usNdx) const return Vector3d(dMtrx4D[0][usNdx], dMtrx4D[1][usNdx], dMtrx4D[2][usNdx]); } -inline Vector3d Matrix4D::trace() const +inline Vector3d Matrix4D::diagonal() const { return Vector3d(dMtrx4D[0][0], dMtrx4D[1][1], dMtrx4D[2][2]); } +inline double Matrix4D::trace3() const +{ + return dMtrx4D[0][0] + dMtrx4D[1][1] + dMtrx4D[2][2]; +} + +inline double Matrix4D::trace() const +{ + return dMtrx4D[0][0] + dMtrx4D[1][1] + dMtrx4D[2][2] + dMtrx4D[3][3]; +} + inline void Matrix4D::setRow(unsigned short usNdx, const Vector3d& vec) { dMtrx4D[usNdx][0] = vec.x; @@ -442,7 +456,7 @@ inline void Matrix4D::setCol(unsigned short usNdx, const Vector3d& vec) dMtrx4D[2][usNdx] = vec.z; } -inline void Matrix4D::setTrace(const Vector3d& vec) +inline void Matrix4D::setDiagonal(const Vector3d& vec) { dMtrx4D[0][0] = vec.x; dMtrx4D[1][1] = vec.y; diff --git a/src/Base/MatrixPy.xml b/src/Base/MatrixPy.xml index 160b0724de..f59db854ae 100644 --- a/src/Base/MatrixPy.xml +++ b/src/Base/MatrixPy.xml @@ -185,16 +185,16 @@ index : int vector : Base.Vector - + - trace() -> Base.Vector + diagonal() -> Base.Vector Return the diagonal of the 3x3 leading principal submatrix as vector. - + - setTrace(vector) -> None + setDiagonal(vector) -> None Set the diagonal of the 3x3 leading principal submatrix. diff --git a/src/Base/MatrixPyImp.cpp b/src/Base/MatrixPyImp.cpp index e4e9098e88..9cf794d37a 100644 --- a/src/Base/MatrixPyImp.cpp +++ b/src/Base/MatrixPyImp.cpp @@ -487,17 +487,17 @@ PyObject* MatrixPy::setRow(PyObject * args) Py_Return; } -PyObject* MatrixPy::trace(PyObject * args) +PyObject* MatrixPy::diagonal(PyObject * args) { if (!PyArg_ParseTuple(args, "")) return nullptr; Matrix4D* mat = getMatrixPtr(); - Base::Vector3d v = mat->trace(); + Base::Vector3d v = mat->diagonal(); return Py::new_reference_to(Py::Vector(v)); } -PyObject* MatrixPy::setTrace(PyObject * args) +PyObject* MatrixPy::setDiagonal(PyObject * args) { PyObject* o{}; if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &o)) @@ -505,7 +505,7 @@ PyObject* MatrixPy::setTrace(PyObject * args) Base::Vector3d v = Py::Vector(o, false).toVector(); Matrix4D* mat = getMatrixPtr(); - mat->setTrace(v); + mat->setDiagonal(v); Py_Return; } diff --git a/src/Mod/Test/BaseTests.py b/src/Mod/Test/BaseTests.py index 32e2728e11..30ec380436 100644 --- a/src/Mod/Test/BaseTests.py +++ b/src/Mod/Test/BaseTests.py @@ -638,9 +638,9 @@ class MatrixTestCase(unittest.TestCase): self.mat.setCol(0, FreeCAD.Vector(1, 0, 0)) self.mat.setRow(0, FreeCAD.Vector(1, 0, 0)) - def testTrace(self): + def testDiagonal(self): self.mat.scale(2.0, 2.0, 2.0) - self.assertEqual(self.mat.trace(), FreeCAD.Vector(2.0, 2.0, 2.0)) + self.assertEqual(self.mat.diagonal(), FreeCAD.Vector(2.0, 2.0, 2.0)) def testNumberProtocol(self): with self.assertRaises(NotImplementedError): diff --git a/tests/src/Base/Matrix.cpp b/tests/src/Base/Matrix.cpp index 96474dfd76..7527868776 100644 --- a/tests/src/Base/Matrix.cpp +++ b/tests/src/Base/Matrix.cpp @@ -1,7 +1,8 @@ #include "gtest/gtest.h" #include +#include -// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) +// NOLINTBEGIN(cppcoreguidelines-*,readability-magic-numbers) // clang-format off TEST(Matrix, TestShearing) { @@ -87,12 +88,12 @@ TEST(Matrix, TestNonUniformScaleLeftTwo) EXPECT_EQ(mat.hasScale(), Base::ScaleType::Uniform); } -TEST(Matrix, TestTrace) +TEST(Matrix, TestDiagonal) { Base::Matrix4D mat; mat.scale(2.0, 2.0, 2.0); - Base::Vector3d trace = mat.trace(); - EXPECT_EQ(trace.x + trace.y + trace.z, 6.0); + Base::Vector3d diag = mat.diagonal(); + EXPECT_EQ(diag.x + diag.y + diag.z, 6.0); } TEST(Matrix, TestColRow) @@ -264,4 +265,4 @@ TEST(Matrix, TestSubAssign) EXPECT_EQ(mat1, mat4); } // clang-format on -// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) +// NOLINTEND(cppcoreguidelines-*,readability-magic-numbers)