From 5f267e8245845ebab47eed84130f4b72969156df Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Mon, 19 Aug 2013 17:44:33 +0100 Subject: [PATCH] minor regularization of vectors and matrices --- .../samplesvectormatrix.cpp | 4 +- src/blackmisc/mathmatrix1x3.h | 2 +- src/blackmisc/mathmatrix3x1.h | 4 +- src/blackmisc/mathmatrix3x3.cpp | 4 +- src/blackmisc/mathmatrix3x3.h | 52 ++++++------- src/blackmisc/mathmatrixbase.cpp | 32 +------- src/blackmisc/mathmatrixbase.h | 24 +----- src/blackmisc/mathvector3dbase.cpp | 49 +++++------- src/blackmisc/mathvector3dbase.h | 78 ++++--------------- 9 files changed, 72 insertions(+), 177 deletions(-) diff --git a/samples/blackmiscvectorgeo/samplesvectormatrix.cpp b/samples/blackmiscvectorgeo/samplesvectormatrix.cpp index c39d4507c..2423da9a0 100644 --- a/samples/blackmiscvectorgeo/samplesvectormatrix.cpp +++ b/samples/blackmiscvectorgeo/samplesvectormatrix.cpp @@ -10,13 +10,11 @@ int BlackMiscTest::CSamplesVectorMatrix::samples() CVector3D v1; CVector3D v2(1, 2, 3); qDebug() << v1 << "value:" << v2[2] << v2.length(); - v2 *= v2; // v2 * v2 - qDebug() << v2 << 2 * v1 << v1 *v1; + qDebug() << v2 << (2 * v1); CMatrix3x3 m; CMatrix3x3 mr = m; qDebug() << m << mr; - mr.setRandom(); bool im; CMatrix3x3 mi = mr.inverse(im); CMatrix3x3 mid = mr * mi; diff --git a/src/blackmisc/mathmatrix1x3.h b/src/blackmisc/mathmatrix1x3.h index 430307397..fede41204 100644 --- a/src/blackmisc/mathmatrix1x3.h +++ b/src/blackmisc/mathmatrix1x3.h @@ -15,7 +15,7 @@ namespace Math { /*! - * \brief 3D matrix + * \brief Matrix 1x3 */ class CMatrix1x3 : public CMatrixBase { diff --git a/src/blackmisc/mathmatrix3x1.h b/src/blackmisc/mathmatrix3x1.h index 81078618a..b28bbc979 100644 --- a/src/blackmisc/mathmatrix3x1.h +++ b/src/blackmisc/mathmatrix3x1.h @@ -14,12 +14,14 @@ namespace BlackMisc namespace Math { +class CMatrix3x3; + /*! * \brief Matrix 3x1 */ class CMatrix3x1 : public CMatrixBase { - friend class CMatrix3x3; // for matrix multiplicaion to access m_matrix + friend class CMatrix3x3; public: /*! diff --git a/src/blackmisc/mathmatrix3x3.cpp b/src/blackmisc/mathmatrix3x3.cpp index 21185d857..fed2ad73f 100644 --- a/src/blackmisc/mathmatrix3x3.cpp +++ b/src/blackmisc/mathmatrix3x3.cpp @@ -30,8 +30,8 @@ double CMatrix3x3::determinant() const CMatrix3x3 CMatrix3x3::inverse(bool &o_invertible) const { CMatrix3x3 inverse; - double det; - if (this->allValuesEqual() || (det = this->determinant()) == 0) + double det = this->determinant(); + if (det == 0) { o_invertible = false; inverse.setZero(); diff --git a/src/blackmisc/mathmatrix3x3.h b/src/blackmisc/mathmatrix3x3.h index 44e9c5b31..f21a1d7be 100644 --- a/src/blackmisc/mathmatrix3x3.h +++ b/src/blackmisc/mathmatrix3x3.h @@ -87,6 +87,17 @@ public: this->m_matrix = this->m_matrix * other.m_matrix; return *this; } + /*! + * \brief Operator * + * \param other + * \return + */ + CMatrix3x3 operator *(const CMatrix3x3 &other) const + { + CMatrix3x3 m(*this); + m *= other; + return m; + } /*! * \brief Operator * @@ -101,27 +112,24 @@ public: } /*! - * \brief Operator * - * \param other - * \return - */ - CMatrix3x3 operator *(const CMatrix3x3 &other) const - { - CMatrix3x3 m(*this); - m *= other; - return m; - } - - /*! - * \brief Multiply vector with this 3x3 matrix + * \brief Multiply this matrix with vector * \param vector * \return */ CVector3D operator *(const CVector3D &vector) const { - CVector3D v(vector); - v.matrixMultiplication(*this); - return v; + return ((*this) * CMatrix3x1(vector.toMatrix3x1())).toVector3D(); + } + + /*! + * \brief Multiply with factor + * \param factor + * \return + */ + CMatrix3x3 &operator *=(double factor) + { + this->CMatrixBase::operator *=(factor); + return *this; } /*! @@ -136,17 +144,6 @@ public: return m; } - /*! - * \brief Multiply with factor - * \param factor - * \return - */ - CMatrix3x3 &operator *=(double factor) - { - CMatrixBase::operator *=(factor); - return *this; - } - /*! * \brief Transposed matrix * \return @@ -171,7 +168,6 @@ public: * \return */ CMatrix1x3 getRow(int row) const; - }; } // namespace diff --git a/src/blackmisc/mathmatrixbase.cpp b/src/blackmisc/mathmatrixbase.cpp index 6d6d5819a..d0a7b01b8 100644 --- a/src/blackmisc/mathmatrixbase.cpp +++ b/src/blackmisc/mathmatrixbase.cpp @@ -40,20 +40,6 @@ template void CMatrixBase void CMatrixBase::setRandom() -{ - for (int r = 0; r < Rows; r++) - { - for (int c = 0; c < Columns; c++) - { - this->m_matrix(r, c) = (qrand() % 101); // 0...100 - } - } -} - /* * All values zero? */ @@ -87,7 +73,7 @@ template void CMatrixBase const QList CMatrixBase::toList() const +template QList CMatrixBase::toList() const { QList list; for (int r = 0; r < Rows; r++) @@ -116,22 +102,6 @@ template void CMatrixBase bool CMatrixBase::allValuesEqual() const -{ - double v = this->getElement(0, 0); - for (int r = 0; r < Rows; r++) - { - for (int c = 0; c < Columns; c++) - { - if (this->m_matrix(r, c) != v) return false; - } - } - return true; -} - /* * Round all values */ diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h index de8d3b5d3..48a9e310b 100644 --- a/src/blackmisc/mathmatrixbase.h +++ b/src/blackmisc/mathmatrixbase.h @@ -111,7 +111,7 @@ public: * \brief List of values * \return */ - const QList toList() const; + QList toList() const; /*! * \brief List of values @@ -164,7 +164,7 @@ public: } /*! - * \brief Operator to support commutative multiplication + * \brief Operator to support commutative scalar multiplication * \param factor * \param other * \return @@ -174,13 +174,6 @@ public: return other * factor; } - /*! - * \brief Multiply with 3D vector operator * - * \param matrix - * \return - */ - template ImplVector operator*(const ImplVector matrix) const; - /*! * \brief Operator /= * \param factor @@ -279,11 +272,6 @@ public: this->m_matrix.setToIdentity(); } - /*! - * \brief Fills the matrix with random elements - */ - void setRandom(); - /*! * \brief All values to zero */ @@ -312,13 +300,7 @@ public: } /*! - * \brief All values equal, if so matirx is not invertible - * \return - */ - bool allValuesEqual() const; - - /*! - * \brief Set a dedicated value + * \brief Set all elements the same * \param value */ void fill(double value) { this->m_matrix.fill(value); } diff --git a/src/blackmisc/mathvector3dbase.cpp b/src/blackmisc/mathvector3dbase.cpp index 0e3df39f3..6484e6ae9 100644 --- a/src/blackmisc/mathvector3dbase.cpp +++ b/src/blackmisc/mathvector3dbase.cpp @@ -43,29 +43,31 @@ template void CVector3DBase::fill(double value) this->m_k = value; } +/* + * Element (return by reference) + */ +template double &CVector3DBase::getElement(size_t row) +{ + switch (row) + { + case 0: + return this->m_i; + case 1: + return this->m_j; + case 2: + return this->m_k; + default: + Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector"); + throw std::range_error("Detected invalid index in 3D vector"); + } +} + /* * Element */ template double CVector3DBase::getElement(size_t row) const { - double d; - switch (row) - { - case 0: - d = this->m_i; - break; - case 1: - d = this->m_j; - break; - case 2: - d = this->m_k; - break; - default: - Q_ASSERT_X(true, "getElement", "Detected invalid index in 3D vector"); - throw std::range_error("Detected invalid index in 3D vector"); - break; - } - return d; + return const_cast*>(this)->getElement(row); } /* @@ -112,17 +114,6 @@ template double CVector3DBase::dotProduct(const I } -/* - * Multiply with matrix - */ -template void CVector3DBase::matrixMultiplication(const CMatrix3x3 &matrix) -{ - CMatrix3x1 m = matrix * this->toMatrix3x1(); - this->m_i = m(0, 0); - this->m_j = m(1, 0); - this->m_k = m(2, 0); -} - /* * Convert to matrix */ diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h index dac6f3e8d..0d40cf8df 100644 --- a/src/blackmisc/mathvector3dbase.h +++ b/src/blackmisc/mathvector3dbase.h @@ -14,9 +14,7 @@ namespace BlackMisc namespace Math { -class CMatrix3x3; // forward declaration -class CMatrix3x1; // forward declaration - +class CMatrix3x1; /*! * \brief 3D vector base (x, y, z) @@ -101,6 +99,13 @@ protected: */ CVector3DBase(const CVector3DBase &other) : m_i(other.m_i), m_j(other.m_j), m_k(other.m_k) {} + /*! + * \brief Get element + * \param row + * \return Mutable reference + */ + double &getElement(size_t row); + /*! * \brief String for converter * \param i18n @@ -155,7 +160,8 @@ public: } /*! - * \brief Set zeros + * \brief Set all elements the same + * \param value */ void fill(double value); @@ -180,6 +186,13 @@ public: */ double operator[](size_t row) const { return this->getElement(row); } + /*! + * \brief Operator [] + * \param row + * \return Mutable reference + */ + double &operator[](size_t row) { return this->getElement(row); } + /*! * \brief Equal operator == * \param other @@ -253,31 +266,6 @@ public: return v; } - /*! - * \brief Operator *=, just x*x, y*y, z*z neither vector nor dot product (like a matrix produc) - * \param other - * \return - */ - CVector3DBase &operator *=(const CVector3DBase &other) - { - this->m_i *= other.m_i; - this->m_j *= other.m_j; - this->m_k *= other.m_k; - return *this; - } - - /*! - * \brief Operator, just x*x, y*y, z*z neither vector nor dot product, (like a matrix produc) - * \param other - * \return - */ - ImplVector operator *(const ImplVector &other) const - { - ImplVector v = *derived(); - v *= other; - return v; - } - /*! * \brief Multiply with scalar * \param factor @@ -339,31 +327,6 @@ public: return v; } - /*! - * \brief Operator /=, just x/x, y/y, z/z - * \param other - * \return - */ - CVector3DBase &operator /=(const CVector3DBase &other) - { - this->m_i /= other.m_i; - this->m_j /= other.m_j; - this->m_k /= other.m_k; - return *this; - } - - /*! - * \brief Operator, just x/x, y/y, z/z - * \param other - * \return - */ - ImplVector operator /(const ImplVector &other) const - { - ImplVector v = *derived(); - v /= other; - return v; - } - /*! * \brief Dot product * \param other @@ -378,13 +341,6 @@ public: */ ImplVector crossProduct(const ImplVector &other) const; - /*! - * \brief Matrix * this vector - * \param matrix - * \return - */ - void matrixMultiplication(const CMatrix3x3 &matrix); - /*! * \brief Reciprocal value * \return