Fixed some issue with scalar multiplications (explicit) and continued with UNIT tests

This commit is contained in:
Klaus Basan
2013-04-21 01:13:23 +02:00
parent f4affe55ef
commit bd53371de4
16 changed files with 255 additions and 91 deletions

View File

@@ -51,8 +51,10 @@ CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateNed &ned)
dcm = dcm1 * dcm2 * dcm3;
bool inverse;
invDcm.setZero();
invDcm = dcm.inverse();
invDcm = dcm.inverse(inverse);
Q_ASSERT_X(inverse, "toEcef", "Inverse matrix could not be calculated");
CVector3D tempResult = invDcm * ned.toMathVector(); // to generic vector
CCoordinateEcef result(tempResult);

View File

@@ -25,18 +25,18 @@ public:
*/
CMatrix1x3() : CMatrixBase() {}
/*!
* \brief init with value
* \param fillValue
*/
CMatrix1x3(double fillValue) : CMatrixBase(fillValue) {}
/*!
* \brief Copy constructor
* \param other
*/
CMatrix1x3(const CMatrix1x3 &otherMatrix) : CMatrixBase(otherMatrix) {}
/*!
* \brief Init by fill value
* \param fillValue
*/
explicit CMatrix1x3(double fillValue) : CMatrixBase(fillValue) {}
/*!
* \brief CMatrix 3x1
* \param c1

View File

@@ -40,18 +40,18 @@ public:
this->m_matrix(2, 0) = r3;
}
/*!
* \brief init with value
* \param fillValue
*/
CMatrix3x1(qreal fillValue) : CMatrixBase(fillValue) {}
/*!
* \brief Copy constructor
* \param other
*/
CMatrix3x1(const CMatrix3x1 &otherMatrix) : CMatrixBase(otherMatrix) {}
/*!
* \brief Init by fill value
* \param fillValue
*/
explicit CMatrix3x1(double fillValue) : CMatrixBase(fillValue) {}
/*!
* \brief Convert to vector
* \return

View File

@@ -13,9 +13,9 @@ namespace Math
/*
* Determinant
*/
qreal CMatrix3x3::determinant() const
double CMatrix3x3::determinant() const
{
qreal determinant =
double determinant =
this->m_matrix(0, 0) * this->m_matrix(1, 1) * this->m_matrix(2, 2) +
this->m_matrix(0, 1) * this->m_matrix(1, 2) * this->m_matrix(2, 0) +
this->m_matrix(0, 2) * this->m_matrix(1, 0) * this->m_matrix(2, 1) -
@@ -27,17 +27,19 @@ qreal CMatrix3x3::determinant() const
}
/*
* Determinant
* Inverse
*/
CMatrix3x3 CMatrix3x3::inverse() const
CMatrix3x3 CMatrix3x3::inverse(bool &invertible) const
{
CMatrix3x3 inverse;
qreal det = determinant();
// should we throw an assert / error here?
if (det == 0) return inverse;
qreal invdet = 1.0 / det;
double det;
if (this->allValuesEqual() || (det = determinant()) == 0)
{
invertible = false;
inverse.setZero();
return inverse;
}
double invdet = 1.0 / det;
inverse.m_matrix(0, 0) = (this->m_matrix(1, 1) * this->m_matrix(2, 2) - this->m_matrix(1, 2) * this->m_matrix(2, 1)) * invdet;
inverse.m_matrix(0, 1) = (- this->m_matrix(0, 1) * this->m_matrix(2, 2) + this->m_matrix(0, 2) * this->m_matrix(2, 1)) * invdet;
@@ -49,6 +51,7 @@ CMatrix3x3 CMatrix3x3::inverse() const
inverse.m_matrix(2, 1) = (- this->m_matrix(0, 0) * this->m_matrix(2, 1) + this->m_matrix(0, 1) * this->m_matrix(2, 0)) * invdet;
inverse.m_matrix(2, 2) = (this->m_matrix(0, 0) * this->m_matrix(1, 1) - this->m_matrix(0, 1) * this->m_matrix(1, 0)) * invdet;
invertible = true;
return inverse;
}

View File

@@ -24,18 +24,45 @@ public:
*/
CMatrix3x3() : CMatrixBase() {}
/*!
* \brief init with value
* \param fillValue
*/
CMatrix3x3(double fillValue) : CMatrixBase(fillValue) {}
/*!
* \brief Copy constructor
* \param other
*/
CMatrix3x3(const CMatrix3x3 &otherMatrix) : CMatrixBase(otherMatrix) {}
/*!
* \brief Init by fill value
* \param fillValue
*/
explicit CMatrix3x3(double fillValue) : CMatrixBase(fillValue) {}
/*!
* \brief Stupid but handy constructor
* \param r1c1
* \param r1c2
* \param r1c3
* \param r2c1
* \param r2c2
* \param r2c3
* \param r3c1
* \param r3c2
* \param r3c3
*/
explicit CMatrix3x3(double r1c1, double r1c2, double r1c3,
double r2c1, double r2c2, double r2c3,
double r3c1, double r3c2, double r3c3) : CMatrixBase()
{
this->setElement(0, 0, r1c1);
this->setElement(0, 1, r1c2);
this->setElement(0, 2, r1c3);
this->setElement(1, 0, r2c1);
this->setElement(1, 1, r2c2);
this->setElement(1, 2, r2c3);
this->setElement(2, 0, r3c1);
this->setElement(2, 1, r3c2);
this->setElement(2, 2, r3c3);
}
/*!
* \brief Calculates the determinant of the matrix
* \return
@@ -44,9 +71,10 @@ public:
/*!
* \brief Calculate the inverse
* \param invertible
* \return
*/
CMatrix3x3 inverse() const;
CMatrix3x3 inverse(bool &invertible) const;
/*!
* \brief Operator *=
@@ -78,7 +106,7 @@ public:
*/
CMatrix3x3 operator *(const CMatrix3x3 &otherMatrix) const
{
CMatrix3x3 m(otherMatrix);
CMatrix3x3 m(*this);
m *= otherMatrix;
return m;
}
@@ -95,13 +123,36 @@ public:
return v;
}
/*!
* \brief Multiply with factor
* \param factor
* \return
*/
CMatrix3x3 operator *(double factor) const
{
CMatrix3x3 m(*this);
m *= factor;
return m;
}
/*!
* \brief Multiply with factor
* \param factor
* \return
*/
CMatrix3x3 &operator *=(double factor)
{
CMatrixBase::operator *=(factor);
return (*this);
}
/*!
* \brief Transposed matrix
* \return
*/
CMatrix3x3 transposed() const
{
CMatrix3x3 m(0.0);
CMatrix3x3 m(0);
m.m_matrix = this->m_matrix.transposed();
return m;
}
@@ -126,4 +177,4 @@ public:
} // namespace
#endif // BLACKMISC_POSMATRIX3X3_H
#endif // guard

View File

@@ -68,6 +68,23 @@ template<class ImplMatrix, int Rows, int Columns> bool CMatrixBase<ImplMatrix, R
return true;
}
/*
* All values zero?
*/
template<class ImplMatrix, int Rows, int Columns> bool CMatrixBase<ImplMatrix, Rows, Columns>::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;
}
/*
* Convert to string
*/

View File

@@ -37,18 +37,21 @@ public:
*/
CMatrixBase() : m_matrix() {}
/*!
* \brief Constructor with init value
* \param fillValue
*/
CMatrixBase(double fillValue) : m_matrix() { this->m_matrix.fill(fillValue);}
/*!
* \brief Copy constructor
* \param other
*/
CMatrixBase(const CMatrixBase &otherMatrix) : m_matrix(otherMatrix.m_matrix) {}
/*!
* \brief Fill with value
* \param fillValue
*/
explicit CMatrixBase(double fillValue) : m_matrix()
{
this->fill(fillValue);
}
/*!
* \brief Virtual destructor
*/
@@ -104,9 +107,9 @@ public:
* \param factor
* \return
*/
CMatrixBase &operator *(double factor)
ImplMatrix operator *(double factor) const
{
ImplMatrix m(0.0);
ImplMatrix m(0);
m += (*this);
m *= factor;
return m;
@@ -135,9 +138,9 @@ public:
* \param factor
* \return
*/
CMatrixBase &operator /(double factor)
ImplMatrix operator /(double factor) const
{
ImplMatrix m(0.0);
ImplMatrix m(0);
m += (*this);
m /= factor;
return m;
@@ -161,7 +164,7 @@ public:
*/
ImplMatrix operator +(const ImplMatrix &otherMatrix) const
{
ImplMatrix m(0.0);
ImplMatrix m(0);
m += (*this);
m += otherMatrix;
return m;
@@ -185,7 +188,7 @@ public:
*/
ImplMatrix operator -(const ImplMatrix &otherMatrix) const
{
ImplMatrix m(0.0);
ImplMatrix m(0);
m += (*this);
m -= otherMatrix;
return m;
@@ -220,11 +223,17 @@ public:
void setZero() { this->m_matrix.fill(0.0); }
/*!
* \brief isZero
* \brief Is zero
* \return
*/
bool isZero() const;
/*!
* \brief All values equal, if so matirx is not invertible
* \return
*/
bool allValuesEqual() const;
/*!
* \brief Set a dedicated value
* \param value

View File

@@ -31,7 +31,7 @@ public:
* \brief Constructor by value
* \param value
*/
CVector3D(double value) : CVector3DBase(value) {}
explicit CVector3D(double value) : CVector3DBase(value) {}
/*!
* \brief Copy constructor

View File

@@ -46,7 +46,7 @@ protected:
* \brief Constructor by value
* \param value
*/
CVector3DBase(double value) : m_i(value), m_j(value), m_k(value) {}
explicit CVector3DBase(double value) : m_i(value), m_j(value), m_k(value) {}
/*!
* \brief Copy constructor
@@ -225,6 +225,58 @@ public:
return v;
}
/*!
* \brief Multiply with scalar
* \param factor
* \return
*/
CVector3DBase &operator *=(double factor)
{
this->m_i *= factor;
this->m_j *= factor;
this->m_k *= factor;
return (*this);
}
/*!
* \brief Multiply with scalar
* \param factor
* \return
*/
ImplClass operator *(double factor) const
{
ImplClass v;
v += (*this);
v *= factor;
return v;
}
/*!
* \brief Divide by scalar
* \param divisor
* \return
*/
CVector3DBase &operator /=(double divisor)
{
this->m_i /= divisor;
this->m_j /= divisor;
this->m_k /= divisor;
return (*this);
}
/*!
* \brief Divide by scalar
* \param divisor
* \return
*/
ImplClass operator /(double divisor) const
{
ImplClass v;
v += (*this);
v /= divisor;
return v;
}
/*!
* \brief Operator /=, just x/x, y/y, z/z
* \param otherVector