Browse Source

Forced exclusive use of bracket operators in Matrix and Vector.

Removed functions at(), set() and add(), everything (and more) can be
now done using operator[]. Accessing matrix elements is now done through
column vectors, e.g.:

    Matrix4 a;
    a.at(row, col); // before
    a[col][row]; // now

Note that because operator[] on Matrix returns column vector (there is
nothing like row vector), the parameter "order" is now swapped.
pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
e8c76b7d96
  1. 24
      src/AbstractTexture.h
  2. 2
      src/Camera.cpp
  3. 2
      src/Light.cpp
  4. 4
      src/Math/GeometryUtils.h
  5. 73
      src/Math/Matrix.h
  6. 8
      src/Math/Matrix3.h
  7. 8
      src/Math/Matrix4.h
  8. 21
      src/Math/Test/MatrixTest.cpp
  9. 28
      src/Math/Test/VectorTest.cpp
  10. 1
      src/Math/Test/VectorTest.h
  11. 38
      src/Math/Vector.h
  12. 8
      src/Math/Vector2.h
  13. 24
      src/Math/Vector3.h
  14. 32
      src/Math/Vector4.h

24
src/AbstractTexture.h

@ -532,15 +532,15 @@ template<> struct AbstractTexture::DataHelper<1> {
inline constexpr static Target target() { return Target::Texture1D; } inline constexpr static Target target() { return Target::Texture1D; }
inline static void setWrapping(Target target, const Math::Vector<Wrapping, 1>& wrapping) { inline static void setWrapping(Target target, const Math::Vector<Wrapping, 1>& wrapping) {
glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_S, static_cast<GLint>(wrapping.at(0))); glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_S, static_cast<GLint>(wrapping[0]));
} }
template<class T> inline static void set(Target target, GLint mipLevel, InternalFormat internalFormat, T* image) { template<class T> inline static void set(Target target, GLint mipLevel, InternalFormat internalFormat, T* image) {
glTexImage1D(static_cast<GLenum>(target), mipLevel, internalFormat, image->dimensions().at(0), 0, static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data()); glTexImage1D(static_cast<GLenum>(target), mipLevel, internalFormat, image->dimensions()[0], 0, static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data());
} }
template<class T> inline static void setSub(Target target, GLint mipLevel, const Math::Vector<GLint, 1>& offset, T* image) { template<class T> inline static void setSub(Target target, GLint mipLevel, const Math::Vector<GLint, 1>& offset, T* image) {
glTexSubImage1D(static_cast<GLenum>(target), mipLevel, offset.at(0), image->dimensions().at(0), static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data()); glTexSubImage1D(static_cast<GLenum>(target), mipLevel, offset[0], image->dimensions()[0], static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data());
} }
}; };
template<> struct AbstractTexture::DataHelper<2> { template<> struct AbstractTexture::DataHelper<2> {
@ -560,16 +560,16 @@ template<> struct AbstractTexture::DataHelper<2> {
inline constexpr static Target target() { return Target::Texture2D; } inline constexpr static Target target() { return Target::Texture2D; }
inline static void setWrapping(Target target, const Math::Vector<Wrapping, 2>& wrapping) { inline static void setWrapping(Target target, const Math::Vector<Wrapping, 2>& wrapping) {
glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_S, static_cast<GLint>(wrapping.at(0))); glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_S, static_cast<GLint>(wrapping[0]));
glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_T, static_cast<GLint>(wrapping.at(1))); glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_T, static_cast<GLint>(wrapping[1]));
} }
template<class T> inline static void set(Target target, GLint mipLevel, InternalFormat internalFormat, T* image) { template<class T> inline static void set(Target target, GLint mipLevel, InternalFormat internalFormat, T* image) {
glTexImage2D(static_cast<GLenum>(target), mipLevel, internalFormat, image->dimensions().at(0), image->dimensions().at(1), 0, static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data()); glTexImage2D(static_cast<GLenum>(target), mipLevel, internalFormat, image->dimensions()[0], image->dimensions()[1], 0, static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data());
} }
template<class T> inline static void setSub(Target target, GLint mipLevel, const Math::Vector<GLint, 2>& offset, T* image) { template<class T> inline static void setSub(Target target, GLint mipLevel, const Math::Vector<GLint, 2>& offset, T* image) {
glTexSubImage2D(static_cast<GLenum>(target), mipLevel, offset.at(0), offset.at(1), image->dimensions().at(0), image->dimensions().at(1), static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data()); glTexSubImage2D(static_cast<GLenum>(target), mipLevel, offset[0], offset[1], image->dimensions()[0], image->dimensions()[1], static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data());
} }
}; };
template<> struct AbstractTexture::DataHelper<3> { template<> struct AbstractTexture::DataHelper<3> {
@ -581,17 +581,17 @@ template<> struct AbstractTexture::DataHelper<3> {
inline constexpr static Target target() { return Target::Texture3D; } inline constexpr static Target target() { return Target::Texture3D; }
inline static void setWrapping(Target target, const Math::Vector<Wrapping, 3>& wrapping) { inline static void setWrapping(Target target, const Math::Vector<Wrapping, 3>& wrapping) {
glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_S, static_cast<GLint>(wrapping.at(0))); glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_S, static_cast<GLint>(wrapping[0]));
glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_T, static_cast<GLint>(wrapping.at(1))); glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_T, static_cast<GLint>(wrapping[1]));
glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_R, static_cast<GLint>(wrapping.at(2))); glTexParameteri(static_cast<GLenum>(target), GL_TEXTURE_WRAP_R, static_cast<GLint>(wrapping[2]));
} }
template<class T> inline static void set(Target target, GLint mipLevel, InternalFormat internalFormat, T* image) { template<class T> inline static void set(Target target, GLint mipLevel, InternalFormat internalFormat, T* image) {
glTexImage3D(static_cast<GLenum>(target), mipLevel, internalFormat, image->dimensions().at(0), image->dimensions().at(1), image->dimensions().at(2), 0, static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data()); glTexImage3D(static_cast<GLenum>(target), mipLevel, internalFormat, image->dimensions()[0], image->dimensions()[1], image->dimensions()[2], 0, static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data());
} }
template<class T> inline static void setSub(Target target, GLint mipLevel, const Math::Vector<GLint, 3>& offset, T* image) { template<class T> inline static void setSub(Target target, GLint mipLevel, const Math::Vector<GLint, 3>& offset, T* image) {
glTexSubImage3D(static_cast<GLenum>(target), mipLevel, offset.at(0), offset.at(1), offset.at(2), image->dimensions().at(0), image->dimensions().at(1), image->dimensions().at(2), static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data()); glTexSubImage3D(static_cast<GLenum>(target), mipLevel, offset[0], offset[1], offset[2], image->dimensions()[0], image->dimensions()[1], image->dimensions()[2], static_cast<GLenum>(image->components()), static_cast<GLenum>(image->type()), image->data());
} }
}; };
#endif #endif

2
src/Camera.cpp

@ -57,7 +57,7 @@ void Camera::setPerspective(GLfloat fov, GLfloat near, GLfloat far) {
rawProjectionMatrix = Matrix4::scaling({xyScale, xyScale, zScale})*rawProjectionMatrix; rawProjectionMatrix = Matrix4::scaling({xyScale, xyScale, zScale})*rawProjectionMatrix;
/* And... another magic */ /* And... another magic */
rawProjectionMatrix.set(3, 3, 0); rawProjectionMatrix[3][3] = 0;
fixAspectRatio(); fixAspectRatio();
} }

2
src/Light.cpp

@ -19,7 +19,7 @@ namespace Magnum {
void Light::setClean() { void Light::setClean() {
if(!isDirty()) return; if(!isDirty()) return;
_position = absoluteTransformation().at(3); _position = absoluteTransformation()[3];
Object::setClean(); Object::setClean();
} }

4
src/Math/GeometryUtils.h

@ -65,11 +65,11 @@ template<class T> class GeometryUtils {
*/ */
static T intersection(const Matrix3<T>& plane, const Vector3<T>& a, const Vector3<T>& b) { static T intersection(const Matrix3<T>& plane, const Vector3<T>& a, const Vector3<T>& b) {
/* Cross product of two vectors defining the plane */ /* Cross product of two vectors defining the plane */
Vector3<T> crossProduct = Vector3<T>::cross(plane.at(1)-plane.at(0), plane.at(2)-plane.at(0)); Vector3<T> crossProduct = Vector3<T>::cross(plane[1]-plane[0], plane[2]-plane[0]);
/* Compute f with cross product and one of the points defining the /* Compute f with cross product and one of the points defining the
plane */ plane */
T f = crossProduct*plane.at(0); T f = crossProduct*plane[0];
/* Compute t */ /* Compute t */
return (f-crossProduct*a)/(crossProduct*(b-a)); return (f-crossProduct*a)/(crossProduct*(b-a));

73
src/Math/Matrix.h

@ -86,39 +86,24 @@ template<class T, size_t size> class Matrix {
* @return One-dimensional array of `size*size` length in column-major * @return One-dimensional array of `size*size` length in column-major
* order. * order.
*/ */
inline constexpr const T* data() const { return _data; } inline T* data() { return _data; }
inline constexpr const T* data() const { return _data; } /**< @copydoc data() */
/** @brief Value at given position */
inline constexpr T at(size_t row, size_t col) const {
return _data[col*size+row];
}
/** @brief %Matrix column */ /** @brief %Matrix column */
inline constexpr Vector<T, size> at(size_t col) const { inline Vector<T, size>& operator[](size_t col) {
return Vector<T, size>::from(_data+col*size); return Vector<T, size>::from(_data+col*size);
} }
/** @brief Set value at given position */ /** @copydoc operator[]() */
inline void set(size_t row, size_t col, T value) { inline constexpr const Vector<T, size>& operator[](size_t col) const {
_data[col*size+row] = value; return Vector<T, size>::from(_data+col*size);
}
/** @brief Set matrix column */
inline void set(size_t col, const Vector<T, size>& value) {
Vector<T, size>::from(_data+col*size) = value;
}
/** @brief Add value to given position */
inline void add(size_t row, size_t col, T value) {
_data[col*size+row] += value;
} }
/** @brief Equality operator */ /** @brief Equality operator */
inline bool operator==(const Matrix<T, size>& other) const { inline bool operator==(const Matrix<T, size>& other) const {
for(size_t row = 0; row != size; ++row) { for(size_t row = 0; row != size; ++row)
for(size_t col = 0; col != size; ++col) for(size_t col = 0; col != size; ++col)
if(!TypeTraits<T>::equals(at(row, col), other.at(row, col))) return false; if(!TypeTraits<T>::equals((*this)[col][row], other[col][row])) return false;
}
return true; return true;
} }
@ -132,12 +117,10 @@ template<class T, size_t size> class Matrix {
Matrix<T, size> operator*(const Matrix<T, size>& other) const { Matrix<T, size> operator*(const Matrix<T, size>& other) const {
Matrix<T, size> out(false); Matrix<T, size> out(false);
for(size_t row = 0; row != size; ++row) { for(size_t row = 0; row != size; ++row)
for(size_t col = 0; col != size; ++col) { for(size_t col = 0; col != size; ++col)
for(size_t pos = 0; pos != size; ++pos) for(size_t pos = 0; pos != size; ++pos)
out.add(row, col, at(row, pos)*other.at(pos, col)); out[col][row] += (*this)[pos][row]*other[col][pos];
}
}
return out; return out;
} }
@ -151,10 +134,9 @@ template<class T, size_t size> class Matrix {
Vector<T, size> operator*(const Vector<T, size>& other) const { Vector<T, size> operator*(const Vector<T, size>& other) const {
Vector<T, size> out; Vector<T, size> out;
for(size_t row = 0; row != size; ++row) { for(size_t row = 0; row != size; ++row)
for(size_t pos = 0; pos != size; ++pos) for(size_t pos = 0; pos != size; ++pos)
out.add(row, at(row, pos)*other.at(pos)); out[row] += (*this)[pos][row]*other[pos];
}
return out; return out;
} }
@ -163,23 +145,21 @@ template<class T, size_t size> class Matrix {
Matrix<T, size> transposed() const { Matrix<T, size> transposed() const {
Matrix<T, size> out(false); Matrix<T, size> out(false);
for(size_t row = 0; row != size; ++row) { for(size_t row = 0; row != size; ++row)
for(size_t col = 0; col != size; ++col) for(size_t col = 0; col != size; ++col)
out.set(col, row, at(row, col)); out[row][col] = (*this)[col][row];
}
return out; return out;
} }
/** @brief %Matrix without given row and column */ /** @brief %Matrix without given column and row */
Matrix<T, size-1> ij(size_t skipRow, size_t skipCol) const { Matrix<T, size-1> ij(size_t skipCol, size_t skipRow) const {
Matrix<T, size-1> out(false); Matrix<T, size-1> out(false);
for(size_t row = 0; row != size-1; ++row) { for(size_t row = 0; row != size-1; ++row)
for(size_t col = 0; col != size-1; ++col) for(size_t col = 0; col != size-1; ++col)
out.set(row, col, at(row + (row >= skipRow), out[col][row] = (*this)[col + (col >= skipCol)]
col + (col >= skipCol))); [row + (row >= skipRow)];
}
return out; return out;
} }
@ -201,10 +181,9 @@ template<class T, size_t size> class Matrix {
T _determinant = determinant(); T _determinant = determinant();
for(size_t row = 0; row != size; ++row) { for(size_t row = 0; row != size; ++row)
for(size_t col = 0; col != size; ++col) for(size_t col = 0; col != size; ++col)
out.set(row, col, (((row+col) & 1) ? -1 : 1)*ij(col, row).determinant()/_determinant); out[col][row] = (((row+col) & 1) ? -1 : 1)*ij(row, col).determinant()/_determinant;
}
return out; return out;
} }
@ -223,7 +202,7 @@ template<class T, size_t size> class MatrixDeterminant {
T out(0); T out(0);
for(size_t col = 0; col != size; ++col) for(size_t col = 0; col != size; ++col)
out += ((col & 1) ? -1 : 1)*m.at(0, col)*m.ij(0, col).determinant(); out += ((col & 1) ? -1 : 1)*m[col][0]*m.ij(col, 0).determinant();
return out; return out;
} }
@ -234,7 +213,7 @@ template<class T> class MatrixDeterminant<T, 2> {
public: public:
/** @brief Functor */ /** @brief Functor */
inline constexpr T operator()(const Matrix<T, 2>& m) { inline constexpr T operator()(const Matrix<T, 2>& m) {
return m.at(0, 0)*m.at(1, 1) - m.at(0, 1)*m.at(1, 0); return m[0][0]*m[1][1] - m[1][0]*m[0][1];
} }
}; };
@ -243,7 +222,7 @@ template<class T> class MatrixDeterminant<T, 1> {
public: public:
/** @brief Functor */ /** @brief Functor */
inline constexpr T operator()(const Matrix<T, 1>& m) { inline constexpr T operator()(const Matrix<T, 1>& m) {
return m.at(0, 0); return m[0][0];
} }
}; };
@ -257,7 +236,7 @@ template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utili
if(row != 0) debug << ",\n "; if(row != 0) debug << ",\n ";
for(size_t col = 0; col != size; ++col) { for(size_t col = 0; col != size; ++col) {
if(col != 0) debug << ", "; if(col != 0) debug << ", ";
debug << value.at(row, col); debug << value[col][row];
} }
} }
debug << ')'; debug << ')';

8
src/Math/Matrix3.h

@ -58,11 +58,11 @@ template<class T> class Matrix3: public Matrix<T, 3> {
/** @copydoc Matrix::operator=() */ /** @copydoc Matrix::operator=() */
inline constexpr Matrix3<T>& operator=(const Matrix<T, 3>& other) { return Matrix<T, 3>::operator=(other); } inline constexpr Matrix3<T>& operator=(const Matrix<T, 3>& other) { return Matrix<T, 3>::operator=(other); }
/** @copydoc Matrix::at(size_t) const */ /** @copydoc Matrix::operator[](size_t) */
inline constexpr Vector3<T> at(size_t col) const { return Matrix<T, 3>::at(col); } inline Vector3<T>& operator[](size_t col) { return Vector3<T>::from(Matrix<T, 3>::data()+col*3); }
/** @copydoc Matrix::at(size_t, size_t) const */ /** @copydoc Matrix::operator[](size_t) const */
inline constexpr T at(size_t row, size_t col) const { return Matrix<T, 3>::at(row, col); } inline constexpr const Vector3<T>& operator[](size_t col) const { return Vector3<T>::from(Matrix<T, 3>::data()+col*3); }
/** @copydoc Matrix::operator*(const Matrix<T, size>&) const */ /** @copydoc Matrix::operator*(const Matrix<T, size>&) const */
inline Matrix3<T> operator*(const Matrix<T, 3>& other) const { return Matrix<T, 3>::operator*(other); } inline Matrix3<T> operator*(const Matrix<T, 3>& other) const { return Matrix<T, 3>::operator*(other); }

8
src/Math/Matrix4.h

@ -127,11 +127,11 @@ template<class T> class Matrix4: public Matrix<T, 4> {
/** @copydoc Matrix::operator=() */ /** @copydoc Matrix::operator=() */
inline constexpr Matrix4<T>& operator=(const Matrix<T, 4>& other) { return Matrix<T, 4>::operator=(other); } inline constexpr Matrix4<T>& operator=(const Matrix<T, 4>& other) { return Matrix<T, 4>::operator=(other); }
/** @copydoc Matrix::at(size_t) const */ /** @copydoc Matrix::operator[](size_t) */
inline constexpr Vector4<T> at(size_t col) const { return Matrix<T, 4>::at(col); } inline Vector4<T>& operator[](size_t col) { return Vector4<T>::from(Matrix<T, 4>::data()+col*4); }
/** @copydoc Matrix::at(size_t, size_t) const */ /** @copydoc Matrix::operator[](size_t) const */
inline constexpr T at(size_t row, size_t col) const { return Matrix<T, 4>::at(row, col); } inline constexpr const Vector4<T>& operator[](size_t col) const { return Vector4<T>::from(Matrix<T, 4>::data()+col*4); }
/** @copydoc Matrix::operator*(const Matrix<T, size>&) const */ /** @copydoc Matrix::operator*(const Matrix<T, size>&) const */
inline Matrix4<T> operator*(const Matrix<T, 4>& other) const { return Matrix<T, 4>::operator*(other); } inline Matrix4<T> operator*(const Matrix<T, 4>& other) const { return Matrix<T, 4>::operator*(other); }

21
src/Math/Test/MatrixTest.cpp

@ -80,14 +80,13 @@ void MatrixTest::data() {
Vector4 vector(4.0f, 5.0f, 6.0f, 7.0f); Vector4 vector(4.0f, 5.0f, 6.0f, 7.0f);
m.set(3, vector); m[3] = vector;
m.set(1, 2, 1.0f); m[2][1] = 1.0f;
m.set(2, 1, 1.0f); m[1][2] = 1.5f;
m.add(2, 1, 0.5f);
QVERIFY(m.at(1, 2) == 1.0f); QVERIFY(m[2][1] == 1.0f);
QVERIFY(m.at(3) == vector); QVERIFY(m[3] == vector);
Matrix4 expected( Matrix4 expected(
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
@ -102,20 +101,20 @@ void MatrixTest::data() {
void MatrixTest::copy() { void MatrixTest::copy() {
Matrix4 m1(false); Matrix4 m1(false);
m1.set(3, 2, 1.0f); m1[2][3] = 1.0f;
/* Copy */ /* Copy */
Matrix4 m2(m1); Matrix4 m2(m1);
Matrix4 m3; Matrix4 m3;
m3.set(0, 0, 1.0f); /* this line is here so it's not optimized to Matrix4 m3(m1) */ m3[0][0] = 1.0f; /* this line is here so it's not optimized to Matrix4 m3(m1) */
m3 = m1; m3 = m1;
/* Change original */ /* Change original */
m1.set(2, 3, 1.0f); m1[3][2] = 1.0f;
/* Verify the copy is the same as original */ /* Verify the copy is the same as original */
Matrix4 original(false); Matrix4 original(false);
original.set(3, 2, 1.0f); original[2][3] = 1.0f;
QVERIFY(m2 == original); QVERIFY(m2 == original);
QVERIFY(m3 == original); QVERIFY(m3 == original);
@ -207,7 +206,7 @@ void MatrixTest::ij() {
12.0f, 13.0f, 15.0f 12.0f, 13.0f, 15.0f
); );
QVERIFY(original.ij(2, 1) == skipped); QVERIFY(original.ij(1, 2) == skipped);
} }
void MatrixTest::determinant() { void MatrixTest::determinant() {

28
src/Math/Test/VectorTest.cpp

@ -40,45 +40,29 @@ void VectorTest::construct() {
void VectorTest::data() { void VectorTest::data() {
Vector4 v; Vector4 v;
v.set(2, 1.0f); v[2] = 1.5f;
v.add(2, 0.5f);
v.set(0, 1.0f); v[0] = 1.0f;
QVERIFY(v == Vector4(1.0f, 0.0f, 1.5f, 0.0f)); QVERIFY(v == Vector4(1.0f, 0.0f, 1.5f, 0.0f));
} }
void VectorTest::bracketOperator() {
Vector4 v1, v2;
v1.set(0, 1.0f);
v1.set(1, v1.at(0));
v1.set(3, 0.5f);
v1.add(3, 2.5f);
v2[0] = 1.0f;
v2[1] = v2[0];
v2[3] = 0.5f;
v2[3] += 2.5f;
QVERIFY(v1 == v2);
}
void VectorTest::copy() { void VectorTest::copy() {
Vector4 v1; Vector4 v1;
v1.set(3, 1.0f); v1[3] = 1.0f;
Vector4 v2(v1); Vector4 v2(v1);
Vector4 v3; Vector4 v3;
v3.set(0, 0.0f); /* this line is here so it's not optimized to Vector4 v3(v1) */ v3[0] = 0.0f; /* this line is here so it's not optimized to Vector4 v3(v1) */
v3 = v1; v3 = v1;
/* Change original */ /* Change original */
v1.set(2, 1.0f); v1[2] = 1.0f;
/* Verify the copy is the same as original original */ /* Verify the copy is the same as original original */
Vector4 original; Vector4 original;
original.set(3, 1.0f); original[3] = 1.0f;
QVERIFY(v2 == original); QVERIFY(v2 == original);
QVERIFY(v3 == original); QVERIFY(v3 == original);

1
src/Math/Test/VectorTest.h

@ -25,7 +25,6 @@ class VectorTest: public QObject {
private slots: private slots:
void construct(); void construct();
void data(); void data();
void bracketOperator();
void copy(); void copy();
void dot(); void dot();
void multiplyDivide(); void multiplyDivide();

38
src/Math/Vector.h

@ -91,27 +91,17 @@ template<class T, size_t size> class Vector {
* @brief Raw data * @brief Raw data
* @return Array with the same size as the vector * @return Array with the same size as the vector
*/ */
inline constexpr const T* data() const { return _data; } inline T* data() { return _data; }
inline constexpr const T* data() const { return _data; } /**< @copydoc data() */
/** @brief Value at given position */ /** @brief Value at given position */
inline constexpr T at(size_t pos) const { return _data[pos]; }
/** @brief Value at given position */
inline constexpr T operator[](size_t pos) const { return _data[pos]; }
/** @brief Reference to value at given position */
inline T& operator[](size_t pos) { return _data[pos]; } inline T& operator[](size_t pos) { return _data[pos]; }
inline constexpr T operator[](size_t pos) const { return _data[pos]; } /**< @copydoc operator[]() */
/** @brief Set value at given position */
inline void set(size_t pos, T value) { _data[pos] = value; }
/** @brief Add value to given position */
inline void add(size_t pos, T value) { _data[pos] += value; }
/** @brief Equality operator */ /** @brief Equality operator */
inline bool operator==(const Vector<T, size>& other) const { inline bool operator==(const Vector<T, size>& other) const {
for(size_t pos = 0; pos != size; ++pos) for(size_t pos = 0; pos != size; ++pos)
if(!TypeTraits<T>::equals(at(pos), other.at(pos))) return false; if(!TypeTraits<T>::equals((*this)[pos], other[pos])) return false;
return true; return true;
} }
@ -126,7 +116,7 @@ template<class T, size_t size> class Vector {
T out(0); T out(0);
for(size_t i = 0; i != size; ++i) for(size_t i = 0; i != size; ++i)
out += at(i)*other.at(i); out += (*this)[i]*other[i];
return out; return out;
} }
@ -144,7 +134,8 @@ template<class T, size_t size> class Vector {
*/ */
Vector<T, size>& operator*=(T number) { Vector<T, size>& operator*=(T number) {
for(size_t i = 0; i != size; ++i) for(size_t i = 0; i != size; ++i)
set(i, at(i)*number); (*this)[i] *= number;
return *this; return *this;
} }
@ -161,7 +152,8 @@ template<class T, size_t size> class Vector {
*/ */
Vector<T, size>& operator/=(T number) { Vector<T, size>& operator/=(T number) {
for(size_t i = 0; i != size; ++i) for(size_t i = 0; i != size; ++i)
set(i, at(i)/number); (*this)[i] /= number;
return *this; return *this;
} }
@ -178,7 +170,8 @@ template<class T, size_t size> class Vector {
*/ */
Vector<T, size>& operator+=(const Vector<T, size>& other) { Vector<T, size>& operator+=(const Vector<T, size>& other) {
for(size_t i = 0; i != size; ++i) for(size_t i = 0; i != size; ++i)
set(i, at(i)+other.at(i)); (*this)[i] += other[i];
return *this; return *this;
} }
@ -195,7 +188,8 @@ template<class T, size_t size> class Vector {
*/ */
Vector<T, size>& operator-=(const Vector<T, size>& other) { Vector<T, size>& operator-=(const Vector<T, size>& other) {
for(size_t i = 0; i != size; ++i) for(size_t i = 0; i != size; ++i)
set(i, at(i)-other.at(i)); (*this)[i] -= other[i];
return *this; return *this;
} }
@ -204,7 +198,7 @@ template<class T, size_t size> class Vector {
Vector<T, size> out; Vector<T, size> out;
for(size_t i = 0; i != size; ++i) for(size_t i = 0; i != size; ++i)
out.set(i, -at(i)); out[i] = -(*this)[i];
return out; return out;
} }
@ -224,7 +218,7 @@ template<class T, size_t size> class Vector {
T out = 1; T out = 1;
for(size_t i = 0; i != size; ++i) for(size_t i = 0; i != size; ++i)
out *= at(i); out *= (*this)[i];
return out; return out;
} }
@ -239,7 +233,7 @@ template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utili
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false); debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false);
for(size_t i = 0; i != size; ++i) { for(size_t i = 0; i != size; ++i) {
if(i != 0) debug << ", "; if(i != 0) debug << ", ";
debug << value.at(i); debug << value[i];
} }
debug << ')'; debug << ')';
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, true); debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, true);

8
src/Math/Vector2.h

@ -49,11 +49,11 @@ template<class T> class Vector2: public Vector<T, 2> {
*/ */
inline constexpr Vector2(T x, T y): Vector<T, 2>(x, y) {} inline constexpr Vector2(T x, T y): Vector<T, 2>(x, y) {}
inline constexpr T x() const { return Vector<T, 2>::at(0); } /**< @brief X component */ inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */
inline constexpr T y() const { return Vector<T, 2>::at(1); } /**< @brief Y component */ inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */
inline void setX(T value) { this->set(0, value); } /**< @brief Set X component */ inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */
inline void setY(T value) { this->set(1, value); } /**< @brief Set Y component */ inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
/** @copydoc Vector::operator=() */ /** @copydoc Vector::operator=() */
inline Vector2<T>& operator=(const Vector<T, 2>& other) { return Vector<T, 2>::operator=(other); } inline Vector2<T>& operator=(const Vector<T, 2>& other) { return Vector<T, 2>::operator=(other); }

24
src/Math/Vector3.h

@ -73,21 +73,21 @@ template<class T> class Vector3: public Vector<T, 3> {
*/ */
inline constexpr Vector3(const Vector<T, 2>& other, T z = T(0)): Vector<T, 3>(other[0], other[1], z) {} inline constexpr Vector3(const Vector<T, 2>& other, T z = T(0)): Vector<T, 3>(other[0], other[1], z) {}
inline constexpr T x() const { return Vector<T, 3>::at(0); } /**< @brief X component */ inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */
inline constexpr T y() const { return Vector<T, 3>::at(1); } /**< @brief Y component */ inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */
inline constexpr T z() const { return Vector<T, 3>::at(2); } /**< @brief Z component */ inline constexpr T z() const { return (*this)[2]; } /**< @brief Z component */
inline void setX(T value) { this->set(0, value); } /**< @brief Set X component */ inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */
inline void setY(T value) { this->set(1, value); } /**< @brief Set Y component */ inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
inline void setZ(T value) { this->set(2, value); } /**< @brief Set Z component */ inline void setZ(T value) { (*this)[2] = value; } /**< @brief Set Z component */
inline constexpr T r() const { return x(); } /**< @brief R component */ inline constexpr T r() const { return x(); } /**< @brief R component */
inline constexpr T g() const { return x(); } /**< @brief G component */ inline constexpr T g() const { return x(); } /**< @brief G component */
inline constexpr T b() const { return z(); } /**< @brief B component */ inline constexpr T b() const { return z(); } /**< @brief B component */
inline void setR(T value) { setX(value); } /**< @brief Set R component */ inline void setR(T value) { setX(value); } /**< @brief Set R component */
inline void setG(T value) { setY(value); } /**< @brief Set G component */ inline void setG(T value) { setY(value); } /**< @brief Set G component */
inline void setB(T value) { setZ(value); } /**< @brief Set B component */ inline void setB(T value) { setZ(value); } /**< @brief Set B component */
/** @copydoc Vector::operator=() */ /** @copydoc Vector::operator=() */
inline Vector3<T>& operator=(const Vector<T, 3>& other) { return Vector<T, 3>::operator=(other); } inline Vector3<T>& operator=(const Vector<T, 3>& other) { return Vector<T, 3>::operator=(other); }

32
src/Math/Vector4.h

@ -65,15 +65,15 @@ template<class T> class Vector4: public Vector<T, 4> {
*/ */
inline constexpr Vector4(const Vector<T, 3>& other, T w = T(1)): Vector<T, 4>(other[0], other[1], other[2], w) {} inline constexpr Vector4(const Vector<T, 3>& other, T w = T(1)): Vector<T, 4>(other[0], other[1], other[2], w) {}
inline constexpr T x() const { return Vector<T, 4>::at(0); } /**< @brief X component */ inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */
inline constexpr T y() const { return Vector<T, 4>::at(1); } /**< @brief Y component */ inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */
inline constexpr T z() const { return Vector<T, 4>::at(2); } /**< @brief Z component */ inline constexpr T z() const { return (*this)[2]; } /**< @brief Z component */
inline constexpr T w() const { return Vector<T, 4>::at(3); } /**< @brief W component */ inline constexpr T w() const { return (*this)[3]; } /**< @brief W component */
inline void setX(T value) { this->set(0, value); } /**< @brief Set X component */ inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */
inline void setY(T value) { this->set(1, value); } /**< @brief Set Y component */ inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
inline void setZ(T value) { this->set(2, value); } /**< @brief Set Z component */ inline void setZ(T value) { (*this)[2] = value; } /**< @brief Set Z component */
inline void setW(T value) { this->set(3, value); } /**< @brief Set W component */ inline void setW(T value) { (*this)[3] = value; } /**< @brief Set W component */
/** /**
* @brief XYZ part of the vector * @brief XYZ part of the vector
@ -81,15 +81,15 @@ template<class T> class Vector4: public Vector<T, 4> {
*/ */
inline constexpr Vector3<T> xyz() const { return Vector3<T>::from(Vector<T, 4>::data()); } inline constexpr Vector3<T> xyz() const { return Vector3<T>::from(Vector<T, 4>::data()); }
inline constexpr T r() const { return x(); } /**< @brief R component */ inline constexpr T r() const { return x(); } /**< @brief R component */
inline constexpr T g() const { return y(); } /**< @brief G component */ inline constexpr T g() const { return y(); } /**< @brief G component */
inline constexpr T b() const { return z(); } /**< @brief B component */ inline constexpr T b() const { return z(); } /**< @brief B component */
inline constexpr T a() const { return w(); } /**< @brief A component */ inline constexpr T a() const { return w(); } /**< @brief A component */
inline void setR(T value) { setX(value); } /**< @brief Set R component */ inline void setR(T value) { setX(value); } /**< @brief Set R component */
inline void setG(T value) { setY(value); } /**< @brief Set G component */ inline void setG(T value) { setY(value); } /**< @brief Set G component */
inline void setB(T value) { setZ(value); } /**< @brief Set B component */ inline void setB(T value) { setZ(value); } /**< @brief Set B component */
inline void setA(T value) { setA(value); } /**< @brief Set A component */ inline void setA(T value) { setA(value); } /**< @brief Set A component */
/** /**
* @brief RGB part of the vector * @brief RGB part of the vector

Loading…
Cancel
Save