#ifndef MATRIX_H
#define MATRIX_H
#include "vector.h"
class Matrix4 {
public:
union {
struct {
Real m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33;
};
struct { Real m[4][4]; };
struct { Real a[16]; };
};
inline explicit Matrix4(
const Vector4 &x = Vector4(1, 0, 0, 0),
const Vector4 &y = Vector4(0, 1, 0, 0),
const Vector4 &z = Vector4(0, 0, 1, 0),
const Vector4 &w = Vector4(0, 0, 0, 1)
):
m00(x.x), m01(x.y), m02(x.z), m03(x.w),
m10(y.x), m11(y.y), m12(y.z), m13(y.w),
m20(z.x), m21(z.y), m22(z.z), m23(z.w),
m30(w.x), m31(w.y), m32(w.z), m33(w.w) { }
inline Vector4& operator[] (int index) { return Vector4::cast(m[index]); }
inline const Vector4& operator[] (int index) const { return Vector4::cast(m[index]); }
inline Vector4& row_x() { return (*this)[0]; }
inline Vector4& row_y() { return (*this)[1]; }
inline Vector4& row_z() { return (*this)[2]; }
inline Vector4& row_w() { return (*this)[3]; }
inline const Vector4& row_x() const { return (*this)[0]; }
inline const Vector4& row_y() const { return (*this)[1]; }
inline const Vector4& row_z() const { return (*this)[2]; }
inline const Vector4& row_w() const { return (*this)[3]; }
inline Vector4 operator* (const Vector4 &v) const
{ return row_x()*v.x + row_y()*v.y + row_z()*v.z + row_w()*v.w; }
inline Matrix4 operator* (const Matrix4 &other) const {
return Matrix4( *this * other.row_x(),
*this * other.row_y(),
*this * other.row_z(),
*this * other.row_w() );
}
inline Matrix4& operator*= (const Matrix4 &other)
{ return *this = *this * other; }
Real det() const;
Matrix4 inverted() const;
Matrix4& invert()
{ return *this = inverted(); }
Matrix4& scale(Real x, Real y, Real z = 1.0) {
row_x().vec3() *= x;
row_y().vec3() *= y;
row_z().vec3() *= z;
return *this;
}
Matrix4 scaled(Real x, Real y, Real z = 1.0)
{ return Matrix4(*this).scale(x, y, z); }
static inline Matrix4 zero() { return Matrix4(Vector4(), Vector4(), Vector4(), Vector4()); }
static inline Matrix4 identity() { return Matrix4(); }
std::string to_string() const {
return "(" + row_x().to_string() + ", "
+ row_y().to_string() + ", "
+ row_z().to_string() + ", "
+ row_w().to_string() + ")";
}
};
typedef Matrix4 Matrix;
#endif