Browse Source

Math: added Matrix::isOrthogonal().

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
4714cafedc
  1. 23
      src/Math/Matrix.h
  2. 16
      src/Math/Test/MatrixTest.cpp

23
src/Math/Matrix.h

@ -102,6 +102,29 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
/** @brief Copy constructor */
inline constexpr Matrix(const RectangularMatrix<size, size, T>& other): RectangularMatrix<size, size, T>(other) {}
/**
* @brief Whether the matrix is orthogonal
*
* The matrix is orthogonal if its transpose is equal to its inverse: @f[
* Q^T = Q^{-1}
* @f]
* @see transposed(), inverted()
*/
bool isOrthogonal() const {
/* Normality */
for(std::size_t i = 0; i != size; ++i)
if(!TypeTraits<T>::equals((*this)[i].dot(), T(1)))
return false;
/* Orthogonality */
for(std::size_t i = 0; i != size-1; ++i)
for(std::size_t j = i+1; j != size; ++j)
if(!TypeTraits<T>::equals(Vector<size, T>::dot((*this)[i], (*this)[j]), T(0)))
return false;
return true;
}
/**
* @brief Trace of the matrix
*

16
src/Math/Test/MatrixTest.cpp

@ -40,6 +40,8 @@ class MatrixTest: public Corrade::TestSuite::Tester {
void constructConversion();
void constructCopy();
void isOrthogonal();
void trace();
void ij();
void determinant();
@ -63,6 +65,8 @@ MatrixTest::MatrixTest() {
&MatrixTest::constructConversion,
&MatrixTest::constructCopy,
&MatrixTest::isOrthogonal,
&MatrixTest::trace,
&MatrixTest::ij,
&MatrixTest::determinant,
@ -139,6 +143,18 @@ void MatrixTest::constructCopy() {
Vector4(7.9f, -1.0f, 8.0f, -1.5f)));
}
void MatrixTest::isOrthogonal() {
CORRADE_VERIFY(!Matrix3(Vector3(1.0f, 0.0f, 0.0f),
Vector3(0.0f, 1.0f, 0.0f),
Vector3(0.0f, 0.1f, 1.0f)).isOrthogonal());
CORRADE_VERIFY(!Matrix3(Vector3(1.0f, 0.0f, 0.0f),
Vector3(0.0f, 1.0f, 0.0f),
Vector3(0.0f, 1.0f, 0.0f)).isOrthogonal());
CORRADE_VERIFY(Matrix3(Vector3(1.0f, 0.0f, 0.0f),
Vector3(0.0f, 1.0f, 0.0f),
Vector3(0.0f, 0.0f, 1.0f)).isOrthogonal());
}
void MatrixTest::trace() {
Matrix<5, Int> m(
Vector<5, Int>(1, 2, 3, 0, 0),

Loading…
Cancel
Save