|
|
b8976b |
#ifndef MATRIX_H
|
|
|
b8976b |
#define MATRIX_H
|
|
|
b8976b |
|
|
|
b8976b |
|
|
|
b8976b |
#include <cairomm matrix.h=""></cairomm>
|
|
|
b8976b |
|
|
|
b8976b |
#include "vector.h"
|
|
|
b8976b |
|
|
|
b8976b |
|
|
|
b8976b |
|
|
|
f77c6c |
class Matrix2 {
|
|
|
f77c6c |
public:
|
|
|
f77c6c |
union {
|
|
|
f77c6c |
struct {
|
|
|
f77c6c |
Real m00, m01,
|
|
|
f77c6c |
m10, m11;
|
|
|
f77c6c |
};
|
|
|
f77c6c |
struct { Real m[2][2]; };
|
|
|
f77c6c |
struct { Real a[4]; };
|
|
|
f77c6c |
};
|
|
|
f77c6c |
|
|
|
f77c6c |
inline explicit Matrix2(
|
|
|
f77c6c |
const Vector2 &x = Vector2(1, 0),
|
|
|
f77c6c |
const Vector2 &y = Vector2(0, 1)
|
|
|
f77c6c |
):
|
|
|
f77c6c |
m00(x.x), m01(x.y),
|
|
|
f77c6c |
m10(y.x), m11(y.y)
|
|
|
f77c6c |
{ }
|
|
|
f77c6c |
|
|
|
f77c6c |
inline Real det() const
|
|
|
f77c6c |
{ return m00*m11 - m01*m10; }
|
|
|
f77c6c |
};
|
|
|
f77c6c |
|
|
|
f77c6c |
|
|
|
b8976b |
class Matrix3 {
|
|
|
b8976b |
public:
|
|
|
b8976b |
union {
|
|
|
b8976b |
struct {
|
|
|
b8976b |
Real m00, m01, m02,
|
|
|
b8976b |
m10, m11, m12,
|
|
|
b8976b |
m20, m21, m22;
|
|
|
b8976b |
};
|
|
|
b8976b |
struct { Real m[3][3]; };
|
|
|
b8976b |
struct { Real a[9]; };
|
|
|
b8976b |
};
|
|
|
b8976b |
|
|
|
b8976b |
inline explicit Matrix3(
|
|
|
b8976b |
const Vector3 &x = Vector3(1, 0, 0),
|
|
|
b8976b |
const Vector3 &y = Vector3(0, 1, 0),
|
|
|
b8976b |
const Vector3 &z = Vector3(0, 0, 1)
|
|
|
b8976b |
):
|
|
|
b8976b |
m00(x.x), m01(x.y), m02(x.z),
|
|
|
b8976b |
m10(y.x), m11(y.y), m12(y.z),
|
|
|
b8976b |
m20(z.x), m21(z.y), m22(z.z) { }
|
|
|
b8976b |
|
|
|
b8976b |
inline Vector3& operator[] (int index) { return Vector3::cast(m[index]); }
|
|
|
b8976b |
inline const Vector3& operator[] (int index) const { return Vector3::cast(m[index]); }
|
|
|
b8976b |
|
|
|
b8976b |
inline Vector3& row_x() { return (*this)[0]; }
|
|
|
b8976b |
inline Vector3& row_y() { return (*this)[1]; }
|
|
|
b8976b |
inline Vector3& row_z() { return (*this)[2]; }
|
|
|
b8976b |
|
|
|
b8976b |
inline const Vector3& row_x() const { return (*this)[0]; }
|
|
|
b8976b |
inline const Vector3& row_y() const { return (*this)[1]; }
|
|
|
b8976b |
inline const Vector3& row_z() const { return (*this)[2]; }
|
|
|
b8976b |
|
|
|
b8976b |
inline const Vector3 get_col(int index) const
|
|
|
b8976b |
{ return Vector3( m[0][index], m[1][index], m[2][index] ); }
|
|
|
b8976b |
|
|
|
b8976b |
inline Vector3 operator* (const Vector3 &v) const
|
|
|
b8976b |
{ return row_x()*v.x + row_y()*v.y + row_z()*v.z; }
|
|
|
b8976b |
inline Matrix3 operator* (const Matrix3 &other) const {
|
|
|
b8976b |
return Matrix3( *this * other.row_x(),
|
|
|
b8976b |
*this * other.row_y(),
|
|
|
b8976b |
*this * other.row_z() );
|
|
|
b8976b |
}
|
|
|
b8976b |
|
|
|
b8976b |
inline Matrix3& operator*= (const Matrix3 &other)
|
|
|
b8976b |
{ return *this = *this * other; }
|
|
|
b8976b |
|
|
|
b8976b |
Real det() const;
|
|
|
b8976b |
Matrix3 inverted(bool *success = NULL) const;
|
|
|
b8976b |
inline Matrix3& invert(bool *success = NULL)
|
|
|
b8976b |
{ return *this = inverted(); }
|
|
|
b8976b |
|
|
|
b8976b |
static Real det(const Matrix3 matrix, const Matrix3 preinverted_matrix);
|
|
|
b8976b |
static inline Matrix3 zero() { return Matrix3(Vector3(), Vector3(), Vector3()); }
|
|
|
b8976b |
|
|
|
b8976b |
static inline Matrix3 identity() { return Matrix3(); }
|
|
|
b8976b |
static inline Matrix3 translation(const Vector2 &v) {
|
|
|
b8976b |
return Matrix3(
|
|
|
b8976b |
Vector3(1, 0, 0),
|
|
|
b8976b |
Vector3(0, 1, 0),
|
|
|
b8976b |
Vector3(v , 1) );
|
|
|
b8976b |
}
|
|
|
b8976b |
static inline Matrix3 scaling(const Vector2 &v) {
|
|
|
b8976b |
return Matrix3(
|
|
|
b8976b |
Vector3(v.x, 0, 0),
|
|
|
b8976b |
Vector3( 0, v.y, 0),
|
|
|
b8976b |
Vector3( 0, 0, 1) );
|
|
|
b8976b |
}
|
|
|
b8976b |
|
|
|
b8976b |
std::string to_string() const {
|
|
|
b8976b |
return "(" + row_x().to_string() + ", "
|
|
|
b8976b |
+ row_y().to_string() + ", "
|
|
|
b8976b |
+ row_z().to_string() + ")";
|
|
|
b8976b |
}
|
|
|
b8976b |
|
|
|
b8976b |
Cairo::Matrix to_cairo() const {
|
|
|
b8976b |
return Cairo::Matrix(
|
|
|
b8976b |
m00, m10, m01,
|
|
|
b8976b |
m11, m20, m21 );
|
|
|
b8976b |
}
|
|
|
b8976b |
};
|
|
|
b8976b |
|
|
|
b8976b |
typedef Matrix3 Matrix;
|
|
|
b8976b |
|
|
|
b8976b |
|
|
|
b8976b |
#endif
|