7 #ifndef CAFU_MATH_MATRIX_4X4_HPP_INCLUDED
8 #define CAFU_MATH_MATRIX_4X4_HPP_INCLUDED
10 #include "Vector3.hpp"
13 namespace cf {
namespace math {
template<
class T>
class QuaternionT; } }
29 for (
unsigned int i = 0; i < 4; i++)
30 for (
unsigned int j = 0; j < 4; j++)
31 m[i][j] = (i == j) ? T(1.0) : 0;
37 for (
unsigned int i = 0; i < 4; i++)
38 for (
unsigned int j = 0; j < 4; j++)
45 for (
unsigned int i = 0; i < 4; i++)
46 for (
unsigned int j = 0; j < 4; j++)
52 T m10, T m11, T m12, T m13,
53 T m20, T m21, T m22, T m23,
54 T m30, T m31, T m32, T m33)
56 m[0][0] = m00;
m[0][1] = m01;
m[0][2] = m02;
m[0][3] = m03;
57 m[1][0] = m10;
m[1][1] = m11;
m[1][2] = m12;
m[1][3] = m13;
58 m[2][0] = m20;
m[2][1] = m21;
m[2][2] = m22;
m[2][3] = m23;
59 m[3][0] = m30;
m[3][1] = m31;
m[3][2] = m32;
m[3][3] = m33;
103 const T*
operator [] (
unsigned int i)
const { assert(i < 4);
return m[i]; }
131 m[1][0]*v.
x +
m[1][1]*v.
y +
m[1][2]*v.
z,
132 m[2][0]*v.
x +
m[2][1]*v.
y +
m[2][2]*v.
z);
141 float(
m[1][0]*v.
x +
m[1][1]*v.
y +
m[1][2]*v.
z),
142 float(
m[2][0]*v.
x +
m[2][1]*v.
y +
m[2][2]*v.
z));
154 m[1][0]*v.
x +
m[1][1]*v.
y +
m[1][2]*v.
z +
m[1][3],
155 m[2][0]*v.
x +
m[2][1]*v.
y +
m[2][2]*v.
z +
m[2][3]);
164 float(
m[1][0]*v.
x +
m[1][1]*v.
y +
m[1][2]*v.
z +
m[1][3]),
165 float(
m[2][0]*v.
x +
m[2][1]*v.
y +
m[2][2]*v.
z +
m[2][3]));
177 m[1][0]*v.
x +
m[1][1]*v.
y +
m[1][2]*v.
z +
m[1][3],
178 m[2][0]*v.
x +
m[2][1]*v.
y +
m[2][2]*v.
z +
m[2][3]);
190 const T Result[3] = {
m[0][0]*v[0] +
m[0][1]*v[1] +
m[0][2]*v[2] +
m[0][3],
191 m[1][0]*v[0] + m[1][1]*v[1] + m[1][2]*v[2] + m[1][3],
192 m[2][0]*v[0] + m[2][1]*v[1] + m[2][2]*v[2] + m[2][3] };
194 for (
unsigned int i = 0; i < 3; i++) out[i] = Result[i];
205 const T v_w =
m[3][0]*v.
x +
m[3][1]*v.
y +
m[3][2]*v.
z +
m[3][3];
207 return (v_w != 0) ? scale(ProjPoint, 1 / v_w) : ProjPoint;
214 void Mul(
const T v[4], T out[4])
const
217 const T Result[4] = {
m[0][0]*v[0] +
m[0][1]*v[1] +
m[0][2]*v[2] +
m[0][3]*v[3],
218 m[1][0]*v[0] +
m[1][1]*v[1] +
m[1][2]*v[2] +
m[1][3]*v[3],
219 m[2][0]*v[0] +
m[2][1]*v[1] +
m[2][2]*v[2] +
m[2][3]*v[3],
220 m[3][0]*v[0] +
m[3][1]*v[1] +
m[3][2]*v[2] +
m[3][3]*v[3] };
222 for (
unsigned int i = 0; i < 4; i++) out[i] = Result[i];
231 for (
unsigned int i = 0; i < 4; i++)
232 for (
unsigned int j = 0; j < 4; j++)
233 if (fabs(
m[i][j] - Other.
m[i][j]) > Epsilon)
250 m[0][1]*v.
x +
m[1][1]*v.
y +
m[2][1]*v.
z,
251 m[0][2]*v.
x +
m[1][2]*v.
y +
m[2][2]*v.
z);
Vector3T< T > ProjectPoint(const Vector3T< T > &v) const
Computes M*v, where M is this matrix.
Definition: Matrix.hpp:202
Matrix4x4T GetTranspose() const
Returns the transpose of this matrix.
Definition: Matrix.cpp:457
Vector3fT Mul0(const Vector3fT &v) const
Definition: Matrix.hpp:138
static Matrix4x4T GetRotateZMatrix(T Angle)
Returns a rotation matrix about Angle degrees around the z-axis.
Definition: Matrix.cpp:152
bool IsEqual(const Matrix4x4T &Other, const T Epsilon=0) const
Returns whether this matrix is equal to Other.
Definition: Matrix.hpp:229
void RotateY_RM(T Angle)
Computes M=R*M, where R=GetRotateYMatrix (Angle).
Definition: Matrix.cpp:313
void RotateZ_RM(T Angle)
Computes M=R*M, where R=GetRotateZMatrix (Angle).
Definition: Matrix.cpp:347
Vector3dT Mul0(const Vector3dT &v) const
Computes M*v, where M is this matrix.
Definition: Matrix.hpp:128
static Matrix4x4T GetProjPerspectiveMatrix(T fovY, T aspect, T zNear, T zFar)
Returns a matrix for perspective projection. If zFar <= zNear, the far plane is assumed to be at infi...
Definition: Matrix.cpp:59
void RotateX_MR(T Angle)
Computes M=M*R, where R=GetRotateXMatrix (Angle).
Definition: Matrix.cpp:262
static Matrix4x4T GetRotateYMatrix(T Angle)
Returns a rotation matrix about Angle degrees around the y-axis.
Definition: Matrix.cpp:137
Matrix4x4T(const double M[4][4])
Constructor for creating an arbitrary matrix.
Definition: Matrix.hpp:43
static Matrix4x4T GetTranslateMatrix(const Vector3T< T > &t)
Returns a translate matrix about t.
Definition: Matrix.cpp:98
Matrix4x4T(const float M[4][4])
Constructor for creating an arbitrary matrix.
Definition: Matrix.hpp:35
void Mul_xyz1(const T v[3], T out[3]) const
Computes M*v, where M is this matrix.
Definition: Matrix.hpp:187
T y
The y-component of this vector.
Definition: Vector3.hpp:41
This class represents a generic 4x4 matrix.
Definition: MapElement.hpp:30
Vector3fT Mul1(const Vector3fT &v) const
Definition: Matrix.hpp:161
void RotateX_RM(T Angle)
Computes M=R*M, where R=GetRotateXMatrix (Angle).
Definition: Matrix.cpp:279
static Matrix4x4T GetProjFrustumMatrix(T left, T right, T bottom, T top, T zNear, T zFar)
Returns a matrix for perspective projection. If zFar <= zNear, the far plane is assumed to be at infi...
Definition: Matrix.cpp:40
This class represents a polymorphic 3-dimensional vector.
Definition: Misc.hpp:11
T m[4][4]
The matrix values.
Definition: Matrix.hpp:267
Vector3dT Mul1(const Vector3dT &v) const
Computes M*v, where M is this matrix.
Definition: Matrix.hpp:151
Matrix4x4T()
The default constructor for creating a "1" (identity) matrix.
Definition: Matrix.hpp:27
static Matrix4x4T GetScaleMatrix(T sx, T sy, T sz)
Returns a scale matrix with scale factors (sx sy sz).
Definition: Matrix.cpp:110
Vector3T< T > InvXForm(Vector3T< T > v) const
If this matrix represents a rigid transformation (rotation and translation only, no scale...
Definition: Matrix.hpp:243
void RotateZ_MR(T Angle)
Computes M=M*R, where R=GetRotateZMatrix (Angle).
Definition: Matrix.cpp:330
void Scale_SM(T sx, T sy, T sz)
Computes M=S*M, where S=GetScaleMatrix (sx, sy, sz).
Definition: Matrix.cpp:251
static Matrix4x4T GetRotateMatrix(T Angle, const Vector3T< T > &Axis)
Returns a rotation matrix about Angle degrees around Axis.
Definition: Matrix.cpp:167
T * operator[](unsigned int i)
Returns the i-th row of this matrix.
Definition: Matrix.hpp:100
static Matrix4x4T GetProjPickMatrix(T x, T y, T width, T height, int viewport[4])
Returns a matrix for picking, i.e. the same matrix that gluPickMatrix() uses.
Definition: Matrix.cpp:83
void Scale_MS(T sx, T sy, T sz)
Computes M=M*S, where S=GetScaleMatrix (sx, sy, sz).
Definition: Matrix.cpp:240
static Matrix4x4T GetRotateXMatrix(T Angle)
Returns a rotation matrix about Angle degrees around the x-axis.
Definition: Matrix.cpp:122
void RotateY_MR(T Angle)
Computes M=M*R, where R=GetRotateYMatrix (Angle).
Definition: Matrix.cpp:296
T z
The z-component of this vector.
Definition: Vector3.hpp:42
This class represents a quaternion.
Definition: Angles.hpp:18
Vector3T< T > Mul_xyz1(const Vector3T< T > &v) const
Computes M*v, where M is this matrix.
Definition: Matrix.hpp:174
T x
The x-component of this vector.
Definition: Vector3.hpp:40
void Mul(const T v[4], T out[4]) const
Computes M*v, where M is this matrix.
Definition: Matrix.hpp:214
Matrix4x4T GetInverse(bool *Result=NULL) const
Computes the inverse of this matrix.
Definition: Matrix.cpp:364
Matrix4x4T operator*(const Matrix4x4T &Other) const
Computes M*Other, that is, the matrix product of this and the Other matrix.
Definition: Matrix.cpp:189
void Translate_TM(const Vector3dT &Trans)
Computes M=T*M, where T=GetTranslationMatrix(Trans). Assumes that the last row is (0 0 0 1)...
Definition: Matrix.cpp:232
void Translate_MT(const Vector3dT &Trans)
Computes M=M*T, where T=GetTranslationMatrix(Trans). Assumes that the last row is (0 0 0 1)...
Definition: Matrix.cpp:214
static Matrix4x4T GetProjOrthoMatrix(T left, T right, T bottom, T top, T zNear, T zFar)
Returns a matrix for orthographic projection.
Definition: Matrix.cpp:30
Matrix4x4T(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, T m20, T m21, T m22, T m23, T m30, T m31, T m32, T m33)
Constructor for creating an arbitrary matrix.
Definition: Matrix.hpp:51