Browse Source

Ability to construct Matrix from column vectors.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
5293429f31
  1. 28
      src/Math/Matrix.h
  2. 5
      src/Math/Matrix3.h
  3. 5
      src/Math/Matrix4.h
  4. 14
      src/Math/Test/MatrixTest.cpp
  5. 1
      src/Math/Test/MatrixTest.h

28
src/Math/Matrix.h

@ -25,6 +25,17 @@ namespace Magnum { namespace Math {
namespace Implementation {
template<class T, size_t size> class MatrixDeterminant;
#ifndef DOXYGEN_GENERATING_OUTPUT
template<size_t ...> struct Sequence {};
template<size_t N, size_t ...S> struct GenerateSequence:
GenerateSequence<N-1, N-1, S...> {};
template<size_t ...S> struct GenerateSequence<0, S...> {
typedef Sequence<S...> Type;
};
#endif
}
/**
@ -52,6 +63,15 @@ template<class T, size_t size> class Matrix {
return *reinterpret_cast<const Matrix<T, size>*>(data);
}
/**
* @brief %Matrix from column vectors
* @param first First column vector
* @param next Next column vectors
*/
template<class ...U> inline constexpr static Matrix<T, size> from(const Vector<T, size>& first, const U&... next) {
return from(typename Implementation::GenerateSequence<size>::Type(), first, next...);
}
/**
* @brief Default constructor
* @param identity Create identity matrix instead of zero matrix.
@ -189,6 +209,14 @@ template<class T, size_t size> class Matrix {
}
private:
template<size_t ...sequence, class ...U> inline constexpr static Matrix<T, size> from(Implementation::Sequence<sequence...> s, const Vector<T, size>& first, U... next) {
return from(s, next..., first[sequence]...);
}
template<size_t ...sequence, class ...U> inline constexpr static Matrix<T, size> from(Implementation::Sequence<sequence...> s, T first, U... next) {
return Matrix<T, size>(first, next...);
}
T _data[size*size];
};

5
src/Math/Matrix3.h

@ -37,6 +37,11 @@ template<class T> class Matrix3: public Matrix<T, 3> {
return *reinterpret_cast<const Matrix3<T>*>(data);
}
/** @copydoc Matrix::from(const Vector<T, size>&, const U&...) */
template<class ...U> inline constexpr static Matrix3<T> from(const Vector<T, 3>& first, const U&... next) {
return Matrix<T, 3>::from(first, next...);
}
/** @copydoc Matrix::Matrix(bool) */
inline constexpr explicit Matrix3(bool identity = true): Matrix<T, 3>{
/** @todo Make this in Matrix itself, after it will be constexpr */

5
src/Math/Matrix4.h

@ -43,6 +43,11 @@ template<class T> class Matrix4: public Matrix<T, 4> {
return *reinterpret_cast<const Matrix4<T>*>(data);
}
/** @copydoc Matrix::from(const Vector<T, size>&, const U&...) */
template<class ...U> inline constexpr static Matrix4<T> from(const Vector<T, 4>& first, const U&... next) {
return Matrix<T, 4>::from(first, next...);
}
/**
* @brief Translation matrix
* @param vec Translation vector

14
src/Math/Test/MatrixTest.cpp

@ -49,6 +49,20 @@ void MatrixTest::construct() {
QVERIFY(Matrix4::from(m) == expected);
}
void MatrixTest::constructFromVectors() {
Matrix4 actual = Matrix4::from(Vector4(1.0f, 2.0f, 3.0f, 4.0f),
Vector4(5.0f, 6.0f, 7.0f, 8.0f),
Vector4(9.0f, 10.0f, 11.0f, 12.0f),
Vector4(13.0f, 14.0f, 15.0f, 16.0f));
Matrix4 expected(1.0f, 2.0f, 3.0f, 4.0f,
5.0f, 6.0f, 7.0f, 8.0f,
9.0f, 10.0f, 11.0f, 12.0f,
13.0f, 14.0f, 15.0f, 16.0f);
QVERIFY(actual == expected);
}
void MatrixTest::constructIdentity() {
Matrix4 identity;

1
src/Math/Test/MatrixTest.h

@ -24,6 +24,7 @@ class MatrixTest: public QObject {
private slots:
void construct();
void constructFromVectors();
void constructIdentity();
void constructZero();
void data();

Loading…
Cancel
Save