Browse Source

Math: slight improvement of Gram-Schmidt orthonormalization algorithm.

Using Vector::projectOntoNormalized(), as it is slightly faster, renamed
the functions to properly mention that this is orthonormalization.
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
1967179a40
  1. 16
      src/Math/Algorithms/GramSchmidt.h
  2. 8
      src/Math/Algorithms/Test/GramSchmidtTest.cpp
  3. 2
      src/SceneGraph/EuclideanMatrixTransformation2D.h
  4. 2
      src/SceneGraph/EuclideanMatrixTransformation3D.h

16
src/Math/Algorithms/GramSchmidt.h

@ -16,7 +16,7 @@
*/
/** @file
* @brief Function Magnum::Math::Algorithms::gramSchmidtInPlace(), Magnum::Math::Algorithms::gramSchmidt()
* @brief Function Magnum::Math::Algorithms::gramSchmidtOrthonormalizeInPlace(), Magnum::Math::Algorithms::gramSchmidtOrthonormalize()
*/
#include "Math/RectangularMatrix.h"
@ -24,26 +24,26 @@
namespace Magnum { namespace Math { namespace Algorithms {
/**
@brief Gram-Schmidt matrix orthonormalization
@brief In-place Gram-Schmidt matrix orthonormalization
@param[in,out] matrix Matrix to perform orthonormalization on
*/
template<std::size_t cols, std::size_t rows, class T> void gramSchmidtInPlace(RectangularMatrix<cols, rows, T>& matrix) {
template<std::size_t cols, std::size_t rows, class T> void gramSchmidtOrthonormalizeInPlace(RectangularMatrix<cols, rows, T>& matrix) {
static_assert(cols <= rows, "Unsupported matrix aspect ratio");
for(std::size_t i = 0; i != cols; ++i) {
matrix[i] = matrix[i].normalized();
for(std::size_t j = i+1; j != cols; ++j)
matrix[j] -= matrix[j].projected(matrix[i]);
matrix[j] -= matrix[j].projectedOntoNormalized(matrix[i]);
}
}
/**
@brief Gram-Schmidt matrix orthonormalization
Unlike gramSchmidtInPlace() returns the modified matrix instead of performing
the orthonormalization in-place.
Unlike gramSchmidtOrthonormalizeInPlace() returns the modified matrix instead
of performing the orthonormalization in-place.
*/
template<std::size_t cols, std::size_t rows, class T> RectangularMatrix<cols, rows, T> gramSchmidt(RectangularMatrix<cols, rows, T> matrix) {
gramSchmidtInPlace(matrix);
template<std::size_t cols, std::size_t rows, class T> RectangularMatrix<cols, rows, T> gramSchmidtOrthonormalize(RectangularMatrix<cols, rows, T> matrix) {
gramSchmidtOrthonormalizeInPlace(matrix);
return matrix;
}

8
src/Math/Algorithms/Test/GramSchmidtTest.cpp

@ -23,22 +23,22 @@ class GramSchmidtTest: public Corrade::TestSuite::Tester {
public:
GramSchmidtTest();
void test();
void orthonormalize();
};
typedef RectangularMatrix<3, 3, float> Matrix3;
typedef Vector<3, float> Vector3;
GramSchmidtTest::GramSchmidtTest() {
addTests(&GramSchmidtTest::test);
addTests(&GramSchmidtTest::orthonormalize);
}
void GramSchmidtTest::test() {
void GramSchmidtTest::orthonormalize() {
Matrix3 m(Vector3(3.0f, 5.0f, 8.0f),
Vector3(4.0f, 4.0f, 7.0f),
Vector3(7.0f, -1.0f, 8.0f));
Matrix3 normalized = Algorithms::gramSchmidt(m);
Matrix3 normalized = Algorithms::gramSchmidtOrthonormalize(m);
/* Verify the first vector is in direction of first original */
CORRADE_COMPARE(normalized[0], m[0].normalized());

2
src/SceneGraph/EuclideanMatrixTransformation2D.h

@ -85,7 +85,7 @@ class EuclideanMatrixTransformation2D: public AbstractTranslationRotation2D<T> {
*/
EuclideanMatrixTransformation2D<T>* normalizeRotation() {
setTransformation(Math::Matrix3<T>::from(
Math::Algorithms::gramSchmidt(_transformation.rotationScaling()),
Math::Algorithms::gramSchmidtOrthonormalize(_transformation.rotationScaling()),
_transformation.translation()));
return this;
}

2
src/SceneGraph/EuclideanMatrixTransformation3D.h

@ -85,7 +85,7 @@ class EuclideanMatrixTransformation3D: public AbstractTranslationRotation3D<T> {
*/
EuclideanMatrixTransformation3D<T>* normalizeRotation() {
setTransformation(Math::Matrix4<T>::from(
Math::Algorithms::gramSchmidt(_transformation.rotationScaling()),
Math::Algorithms::gramSchmidtOrthonormalize(_transformation.rotationScaling()),
_transformation.translation()));
return this;
}

Loading…
Cancel
Save