Browse Source

Math: ability to access also matrix row vectors.

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
75d8fefdde
  1. 6
      doc/matrix-vector.dox
  2. 3
      src/Math/Matrix.h
  3. 24
      src/Math/RectangularMatrix.h
  4. 12
      src/Math/Test/RectangularMatrixTest.cpp

6
doc/matrix-vector.dox

@ -129,6 +129,12 @@ Vector<3, Int> b;
b[1] = 1; // second element b[1] = 1; // second element
@endcode @endcode
Row vectors can be accessed too, but only for reading, and the access is slower
due to the way the matrix is stored (see explanation below):
@code
Vector<2, Int> c = a.row(2); // third row
@endcode
Fixed-size vector subclasses have functions for accessing named components Fixed-size vector subclasses have functions for accessing named components
and subparts: and subparts:
@code @code

3
src/Math/Matrix.h

@ -199,6 +199,9 @@ template<std::size_t size, class T> inline Corrade::Utility::Debug operator<<(Co
} \ } \
inline constexpr const VectorType<T> operator[](std::size_t col) const { \ inline constexpr const VectorType<T> operator[](std::size_t col) const { \
return VectorType<T>(Matrix<size, T>::operator[](col)); \ return VectorType<T>(Matrix<size, T>::operator[](col)); \
} \
inline VectorType<T> row(std::size_t row) const { \
return VectorType<T>(Matrix<size, T>::row(row)); \
} \ } \
\ \
inline Type<T> operator*(const Matrix<size, T>& other) const { \ inline Type<T> operator*(const Matrix<size, T>& other) const { \

24
src/Math/RectangularMatrix.h

@ -154,7 +154,7 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
* Float a = m[2][1]; * Float a = m[2][1];
* @endcode * @endcode
* *
* @see data() * @see row(), data()
*/ */
inline Vector<rows, T>& operator[](std::size_t col) { inline Vector<rows, T>& operator[](std::size_t col) {
return _data[col]; return _data[col];
@ -164,6 +164,22 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
return _data[col]; return _data[col];
} }
/**
* @brief %Matrix row
*
* Consider using transposed() when accessing rows frequently, as this
* is slower than accessing columns due to the way the matrix is stored.
* @see operator[]()
*/
inline Vector<cols, T> row(std::size_t row) const {
Vector<cols, T> out;
for(std::size_t i = 0; i != cols; ++i)
out[i] = _data[i][row];
return out;
}
/** @brief Equality comparison */ /** @brief Equality comparison */
inline bool operator==(const RectangularMatrix<cols, rows, T>& other) const { inline bool operator==(const RectangularMatrix<cols, rows, T>& other) const {
for(std::size_t i = 0; i != cols; ++i) for(std::size_t i = 0; i != cols; ++i)
@ -336,7 +352,11 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
return operator*(RectangularMatrix<1, rows, T>(other))[0]; return operator*(RectangularMatrix<1, rows, T>(other))[0];
} }
/** @brief Transposed matrix */ /**
* @brief Transposed matrix
*
* @see row()
*/
RectangularMatrix<rows, cols, T> transposed() const { RectangularMatrix<rows, cols, T> transposed() const {
RectangularMatrix<rows, cols, T> out; RectangularMatrix<rows, cols, T> out;

12
src/Math/Test/RectangularMatrixTest.cpp

@ -40,7 +40,9 @@ class RectangularMatrixTest: public Corrade::TestSuite::Tester {
void constructFromData(); void constructFromData();
void constructFromDiagonal(); void constructFromDiagonal();
void constructCopy(); void constructCopy();
void data(); void data();
void row();
void compare(); void compare();
@ -79,7 +81,9 @@ RectangularMatrixTest::RectangularMatrixTest() {
&RectangularMatrixTest::constructFromData, &RectangularMatrixTest::constructFromData,
&RectangularMatrixTest::constructFromDiagonal, &RectangularMatrixTest::constructFromDiagonal,
&RectangularMatrixTest::constructCopy, &RectangularMatrixTest::constructCopy,
&RectangularMatrixTest::data, &RectangularMatrixTest::data,
&RectangularMatrixTest::row,
&RectangularMatrixTest::compare, &RectangularMatrixTest::compare,
@ -199,6 +203,14 @@ void RectangularMatrixTest::data() {
CORRADE_COMPARE(d, 3.0f); CORRADE_COMPARE(d, 3.0f);
} }
void RectangularMatrixTest::row() {
const Matrix3x4 a(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));
CORRADE_COMPARE(a.row(1), Vector3(2.0f, 6.0f, 10.0f));
}
void RectangularMatrixTest::compare() { void RectangularMatrixTest::compare() {
Matrix2 a(Vector2(1.0f, -3.0f), Matrix2 a(Vector2(1.0f, -3.0f),
Vector2(5.0f, -10.0f)); Vector2(5.0f, -10.0f));

Loading…
Cancel
Save