From 837809b96dbdc37cf5fe1f432e4fa81918ce2f94 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 1 May 2013 00:00:03 +0200 Subject: [PATCH] Changed round and renamed length in vector / matrix --- .../samplesvectormatrix.cpp | 2 +- src/blackmisc/mathmatrixbase.cpp | 3 +- src/blackmisc/mathmatrixbase.h | 22 +++++++--- src/blackmisc/mathvector3dbase.h | 43 ++++++++----------- tests/blackmisc/testgeo.cpp | 2 +- tests/blackmisc/testvectormatrix.cpp | 4 +- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/samples/blackmiscvectorgeo/samplesvectormatrix.cpp b/samples/blackmiscvectorgeo/samplesvectormatrix.cpp index 8ac657a21..c39d4507c 100644 --- a/samples/blackmiscvectorgeo/samplesvectormatrix.cpp +++ b/samples/blackmiscvectorgeo/samplesvectormatrix.cpp @@ -9,7 +9,7 @@ int BlackMiscTest::CSamplesVectorMatrix::samples() { CVector3D v1; CVector3D v2(1, 2, 3); - qDebug() << v1 << "value:" << v2[2] << v2.magnitude(); + qDebug() << v1 << "value:" << v2[2] << v2.length(); v2 *= v2; // v2 * v2 qDebug() << v2 << 2 * v1 << v1 *v1; diff --git a/src/blackmisc/mathmatrixbase.cpp b/src/blackmisc/mathmatrixbase.cpp index 99a1dadd3..07dfc60b2 100644 --- a/src/blackmisc/mathmatrixbase.cpp +++ b/src/blackmisc/mathmatrixbase.cpp @@ -88,7 +88,7 @@ template bool CMatrixBase ImplMatrix &CMatrixBase::round() +template void CMatrixBase::round() { for (int r = 0; r < Rows; r++) { @@ -97,7 +97,6 @@ template ImplMatrix &CMatrixBasem_matrix(r, c) = CMath::roundEpsilon(this->m_matrix(r, c), 1E-10); } } - return *derived(); } /* diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h index 8b3b6152b..722b64943 100644 --- a/src/blackmisc/mathmatrixbase.h +++ b/src/blackmisc/mathmatrixbase.h @@ -35,7 +35,7 @@ protected: * \brief Easy access to derived class (CRTP template parameter) * \return */ - ImplMatrix const* derived() const + ImplMatrix const *derived() const { return static_cast(this); } @@ -44,7 +44,7 @@ protected: * \brief Easy access to derived class (CRTP template parameter) * \return */ - ImplMatrix* derived() + ImplMatrix *derived() { return static_cast(this); } @@ -235,7 +235,8 @@ public: bool isIdentityEpsilon() const { ImplMatrix m = *derived(); - return m.round().isIdentity(); + m.round(); + return m.isIdentity(); } /*! @@ -270,7 +271,8 @@ public: bool isZeroEpsilon() const { ImplMatrix m = *derived(); - return m.round().isZero(); + m.round(); + return m.isZero(); } /*! @@ -287,9 +289,19 @@ public: /*! * \brief Round all values + */ + void round(); + + /*! + * \brief Return a rounded matrix * \return */ - ImplMatrix &round(); + ImplMatrix roundedMatrix() const + { + ImplMatrix m = *derived(); + m.round(); + return m; + } /*! * \brief Get element diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h index 1fe7d6133..a289138eb 100644 --- a/src/blackmisc/mathvector3dbase.h +++ b/src/blackmisc/mathvector3dbase.h @@ -65,7 +65,7 @@ protected: * \brief Easy access to derived class (CRTP template parameter) * \return */ - ImplVector const* derived() const + ImplVector const *derived() const { return static_cast(this); } @@ -74,7 +74,7 @@ protected: * \brief Easy access to derived class (CRTP template parameter) * \return */ - ImplVector* derived() + ImplVector *derived() { return static_cast(this); } @@ -378,24 +378,6 @@ public: return v; } - /*! - * \brief Length - * \return - */ - double length()const - { - return this->m_i + this->m_j + this->m_k; - } - - /*! - * \brief Length squared - * \return - */ - double lengthSquared()const - { - return this->m_i * this->m_i + this->m_j * this->m_j + this->m_k * this->m_k; - } - /*! * \brief Converted to matrix * \return @@ -403,25 +385,34 @@ public: CMatrix3x1 toMatrix3x1() const; /*! - * \brief Magnitude + * \brief length / magnitude * \return */ - double magnitude() const + double length() const { - return sqrt(this->lengthSquared()); + return sqrt(this->m_i * this->m_i + this->m_j * this->m_j + this->m_k * this->m_k); } /*! * \brief Round this vector - * \return */ - ImplVector &round() + void round() { const double epsilon = 1E-10; this->m_i = BlackMisc::Math::CMath::roundEpsilon(this->m_i, epsilon); this->m_j = BlackMisc::Math::CMath::roundEpsilon(this->m_j, epsilon); this->m_k = BlackMisc::Math::CMath::roundEpsilon(this->m_k, epsilon); - return *derived(); + } + + /*! + * \brief Rounded vector + * \return + */ + ImplVector roundedVector() const + { + ImplVector v = *derived(); + v.round(); + return v; } }; diff --git a/tests/blackmisc/testgeo.cpp b/tests/blackmisc/testgeo.cpp index 57c0fab60..6ea4cc34d 100644 --- a/tests/blackmisc/testgeo.cpp +++ b/tests/blackmisc/testgeo.cpp @@ -28,7 +28,7 @@ void CTestGeo::geoBasics() CCoordinateEcef ecefReconvert = CCoordinateTransformation::toEcef(nedVec); // check against rounded reconvert - QVERIFY2(mediumEcefVec.round() == ecefReconvert.round(), "Reconverted geo vector should be equal"); + QVERIFY2(mediumEcefVec.roundedVector() == ecefReconvert.roundedVector(), "Reconverted geo vector should be equal"); } } // namespace diff --git a/tests/blackmisc/testvectormatrix.cpp b/tests/blackmisc/testvectormatrix.cpp index 2a9b5616d..29a45e9b6 100644 --- a/tests/blackmisc/testvectormatrix.cpp +++ b/tests/blackmisc/testvectormatrix.cpp @@ -28,9 +28,7 @@ void CTestVectorMatrix::vectorBasics() CVector3D v7(3, 4, 5); QVERIFY2(v6.crossProduct(v7) == CVector3D(-2, 4, -2), "Cross product is wrong"); QVERIFY2(v6.dotProduct(v7) == 26, "Dot product is wrong, 26 expected"); - QVERIFY2(v6.length() == (1 + 2 + 3), "Wrong vector length"); - QVERIFY2(v6.lengthSquared() == (1 + 4 + 9), "Wrong squared vector length"); - QVERIFY2(v6.magnitude() == sqrt(1.0 + 4.0 + 9.0), "Wrong vector magnitude"); + QVERIFY2(v6.length() == sqrt(1.0 + 4.0 + 9.0), "Wrong vector magnitude"); } /*