From 7413c7252b38018459139a3488757f0123b8ff51 Mon Sep 17 00:00:00 2001 From: andrey Date: Thu, 22 Oct 2020 16:13:39 +0300 Subject: [PATCH] refactor PIMathMatrixT and fix pimathvector.h --- libs/main/math/pimathmatrix.h | 133 +++++++++++++--------------------- libs/main/math/pimathvector.h | 27 ++++--- 2 files changed, 68 insertions(+), 92 deletions(-) diff --git a/libs/main/math/pimathmatrix.h b/libs/main/math/pimathmatrix.h index 1e263e15..05f7a551 100644 --- a/libs/main/math/pimathmatrix.h +++ b/libs/main/math/pimathmatrix.h @@ -53,20 +53,15 @@ class PIP_EXPORT PIMathMatrixT { static_assert(Cols > 0, "Column count must be > 0"); public: /** - * @brief Constructor that calls the private resize method - * - * @return identitied matrix of type PIMathMatrixT + * @brief Constructs PIMathMatrixT that is filled by \a new_value */ - PIMathMatrixT() { resize(Rows, Cols); } + PIMathMatrixT(const Type &new_value = Type()) {PIMM_FOR m[r][c] = new_value;} /** - * @brief Constructor that calls the private resize method - * - * @param val is the PIVector with which the matrix is ​​filled - * @return identitied matrix of type PIMathMatrixT + * @brief Contructs PIMathMatrixT from PIVector */ PIMathMatrixT(const PIVector &val) { - resize(Rows, Cols); + assert(Rows*Cols == val.size()); int i = 0; PIMM_FOR m[r][c] = val[i++]; } @@ -91,31 +86,19 @@ public: return tm; } - /** - * @brief Creates a matrix that is filled with elements - * - * @param v is a parameter the type and value of which is selected and later filled into the matrix - * @return filled matrix of type PIMathMatrixT - */ - static _CMatrix filled(const Type &v) { - _CMatrix tm; - PIMM_FOR tm.m[r][c] = v; - return tm; - } - /** * @brief Method which returns number of columns in matrix * * @return type uint shows number of columns */ - uint cols() const { return Cols; } + constexpr uint cols() const {return Cols;} /** * @brief Method which returns number of rows in matrix * * @return type uint shows number of rows */ - uint rows() const { return Rows; } + constexpr uint rows() const {return Rows;} /** * @brief Method which returns the selected column in PIMathVectorT format. @@ -177,13 +160,8 @@ public: * @param r1 is the number of the second selected row * @return matrix type _CMatrix */ - _CMatrix &swapRows(uint r0, uint r1) { - Type t; - PIMM_FOR_C { - t = m[r0][i]; - m[r0][i] = m[r1][i]; - m[r1][i] = t; - } + _CMatrix &swapRows(uint rf, uint rs) { + PIMM_FOR_C piSwap(m[rf][i], m[rs][i]); return *this; } @@ -195,13 +173,8 @@ public: * @param c1 is the number of the second selected column * @return matrix type _CMatrix */ - _CMatrix &swapCols(uint c0, uint c1) { - Type t; - PIMM_FOR_R { - t = m[i][c0]; - m[i][c0] = m[i][c1]; - m[i][c1] = t; - } + _CMatrix &swapCols(uint cf, uint cs) { + PIMM_FOR_R piSwap(m[i][cf], m[i][cs]); return *this; } @@ -221,7 +194,7 @@ public: * * @return true if matrix is square, else false */ - bool isSquare() const { return cols() == rows(); } + constexpr bool isSquare() const { return Rows == Cols; } /** * @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros @@ -243,30 +216,41 @@ public: return true; } - /** - * @brief Full access to elements reference by row "row" and col "col". - * If you enter an index out of the border of the matrix there will be "undefined behavior" - * - * @param row is a parameter that shows the row number of the matrix of the selected element - * @param col is a parameter that shows the column number of the matrix of the selected element - * @return reference to element of matrix by row "row" and col "col" - */ - Type &at(uint row, uint col) { return m[row][col]; } /** - * @brief Full access to element by row "row" and col "col". + * @brief Read-only access to element by \a row and \a col. * If you enter an index out of the border of the matrix there will be "undefined behavior" * - * @param row is a parameter that shows the row number of the matrix of the selected element - * @param col is a parameter that shows the column number of the matrix of the selected element - * @return element of matrix by row "row" and col "col" + * @param row of matrix + * @param col of matrix + * @return copy of element of matrix */ Type at(uint row, uint col) const { return m[row][col]; } + /** + * @brief Full access to element by \a row and \a col. + * If you enter an index out of the border of the matrix there will be "undefined behavior" + * + * @param row of matrix + * @param col of matrix + * @return element of matrix + */ + inline Type & element(uint row, uint col) {return m[row][col];} + + /** + * @brief Read-only access to element by \a row and \a col. + * If you enter an index out of the border of the matrix there will be "undefined behavior" + * + * @param row of matrix + * @param col of matrix + * @return element of matrix + */ + inline const Type & element(uint row, uint col) const {return m[row][col];} + /** * @brief Full access to the matrix row pointer. If you enter an index out of the border of the matrix there will be "undefined behavior" * - * @param row is a row of necessary matrix + * @param row of matrix * @return matrix row pointer */ Type *operator[](uint row) { return m[row]; } @@ -274,26 +258,15 @@ public: /** * @brief Read-only access to the matrix row pointer. If you enter an index out of the border of the matrix there will be "undefined behavior" * - * @param row is a row of necessary matrix + * @param row of matrix * @return matrix row pointer */ - const Type *operator[](uint row) const { return m[row]; } + const Type *operator[](uint row) const {return m[row];} /** - * @brief Matrix assignment to matrix "sm" + * @brief Matrix compare * - * @param sm matrix for the assigment - * @return matrix equal with sm - */ - _CMatrix &operator=(const _CMatrix &sm) { - memcpy(m, sm.m, sizeof(Type) * Cols * Rows); - return *this; - } - - /** - * @brief Compare with matrix "sm" - * - * @param sm matrix for the compare + * @param sm matrix for compare * @return if matrices are equal true, else false */ bool operator==(const _CMatrix &sm) const { @@ -302,9 +275,9 @@ public: } /** - * @brief Compare with matrix "sm" + * @brief Matrix negative compare * - * @param sm matrix for the compare + * @param sm matrix for compare * @return if matrices are not equal true, else false */ bool operator!=(const _CMatrix &sm) const { return !(*this == sm); } @@ -314,28 +287,31 @@ public: * * @param sm matrix for the addition assigment */ - void operator+=(const _CMatrix &sm) { PIMM_FOR m[r][c] += sm.m[r][c]; } + void operator+=(const _CMatrix &sm) {PIMM_FOR m[r][c] += sm.m[r][c];} /** * @brief Subtraction assignment with matrix "sm" * * @param sm matrix for the subtraction assigment */ - void operator-=(const _CMatrix &sm) { PIMM_FOR m[r][c] -= sm.m[r][c]; } + void operator-=(const _CMatrix &sm) {PIMM_FOR m[r][c] -= sm.m[r][c];} /** * @brief Multiplication assignment with value "v" * * @param v value for the multiplication assigment */ - void operator*=(const Type &v) { PIMM_FOR m[r][c] *= v; } + void operator*=(const Type &v) {PIMM_FOR m[r][c] *= v;} /** * @brief Division assignment with value "v" * * @param v value for the division assigment */ - void operator/=(const Type &v) { PIMM_FOR m[r][c] /= v; } + void operator/=(const Type &v) { + assert(piAbs(v) > PIMATHVECTOR_ZERO_CMP); + PIMM_FOR m[r][c] /= v; + } /** * @brief Matrix substraction @@ -391,6 +367,7 @@ public: * @return the result of matrix division */ _CMatrix operator/(const Type &v) const { + assert(piAbs(v) > PIMATHVECTOR_ZERO_CMP); _CMatrix tm = _CMatrix(*this); PIMM_FOR tm.m[r][c] /= v; return tm; @@ -535,22 +512,12 @@ public: } private: - void resize(uint rows_, uint cols_, const Type &new_value = Type()) { - r_ = rows_; - c_ = cols_; - PIMM_FOR m[r][c] = new_value; - } - - int c_, r_; Type m[Rows][Cols]; - }; #pragma pack(pop) - - #ifdef PIP_STD_IOSTREAM template inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT & m) { diff --git a/libs/main/math/pimathvector.h b/libs/main/math/pimathvector.h index 4f89a7b9..a19ae5e3 100644 --- a/libs/main/math/pimathvector.h +++ b/libs/main/math/pimathvector.h @@ -56,10 +56,14 @@ public: return tv; } - uint size() const {return Size;} + constexpr uint size() const {return Size;} _CVector & fill(const Type & v) {PIMV_FOR c[i] = v; return *this;} _CVector & move(const Type & v) {PIMV_FOR c[i] += v; return *this;} _CVector & move(const _CVector & v) {PIMV_FOR c[i] += v[i]; return *this;} + _CVector & swapElements(uint f, uint s) { + piSwap(c[f], c[s]); + return *this; + } Type lengthSqr() const { Type tv(0); PIMV_FOR tv += c[i] * c[i]; @@ -99,13 +103,15 @@ public: bool isNull() const {PIMV_FOR if (c[i] != Type(0)) return false; return true;} bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);} - Type & at(uint index) {return c[index];} - Type at(uint index) const {return c[index];} Type & operator [](uint index) {return c[index];} - Type operator [](uint index) const {return c[index];} + const Type & operator [](uint index) const {return c[index];} + Type at(uint index) const {return c[index];} + _CVector & operator =(const Type & v) {PIMV_FOR c[i] = v; return *this;} + bool operator ==(const _CVector & v) const {PIMV_FOR if (c[i] != v[i]) return false; return true;} bool operator !=(const _CVector & v) const {return !(*this == c);} + void operator +=(const _CVector & v) {PIMV_FOR c[i] += v[i];} void operator -=(const _CVector & v) {PIMV_FOR c[i] -= v[i];} void operator *=(const Type & v) {PIMV_FOR c[i] *= v;} @@ -287,8 +293,8 @@ public: PIMV_FOR c[i] += v[i]; return *this; } - _CVector & swapElements(uint fe, uint se) { - piSwap(c[fe], c[se]); + _CVector & swapElements(uint f, uint s) { + piSwap(c[f], c[s]); return *this; } Type lengthSqr() const { @@ -340,12 +346,15 @@ public: bool isValid() const {return !c.isEmpty();} bool isOrtho(const _CVector & v) const {return dot(v) == Type(0);} - Type & at(uint index) {return c[index];} - Type at(uint index) const {return c[index];} Type & operator [](uint index) {return c[index];} - Type operator [](uint index) const {return c[index];} + const Type & operator [](uint index) const {return c[index];} + Type at(uint index) const {return c[index];} + + _CVector & operator =(const Type & v) {PIMV_FOR c[i] = v; return *this;} + bool operator ==(const _CVector & v) const {return c == v.c;} bool operator !=(const _CVector & v) const {return c != v.c;} + void operator +=(const _CVector & v) { assert(c.size() == v.size()); PIMV_FOR c[i] += v[i];