12#ifndef TT_INCLUDE__MATRIX3_H
13#define TT_INCLUDE__MATRIX3_H
18class SCRIPTS_API Matrix3
21 TT_INLINE Matrix3(
void) {};
22 TT_INLINE Matrix3(
const Matrix3 & m)
24 Row[0] = m.Row[0]; Row[1] = m.Row[1]; Row[2] = m.Row[2];
26 TT_INLINE
explicit Matrix3(
bool identity)
30 Row[0].Set(1.0,0.0,0.0);
31 Row[1].Set(0.0,1.0,0.0);
32 Row[2].Set(0.0,0.0,1.0);
35 TT_INLINE
explicit Matrix3(
const Vector3 & v0,
const Vector3 & v1,
const Vector3 & v2)
41 explicit Matrix3(
const Matrix3D & m);
42 explicit Matrix3(
const Matrix4 & m);
43 TT_INLINE
explicit Matrix3(
float m11,
float m12,
float m13,
float m21,
float m22,
float m23,
float m31,
float m32,
float m33)
45 Row[0].Set(m11,m12,m13);
46 Row[1].Set(m21,m22,m23);
47 Row[2].Set(m31,m32,m33);
49 TT_INLINE
explicit Matrix3(
const Vector3 & axis,
float angle)
53 TT_INLINE
explicit Matrix3(
const Vector3 & axis,
float s_angle,
float c_angle)
55 Set(axis,s_angle,c_angle);
57 TT_INLINE Matrix3(
const Quaternion & q)
61 void Set(
const Matrix3D & m);
62 void Set(
const Matrix4 & m);
63 TT_INLINE
void Set(
const Vector3 & v0,
const Vector3 & v1,
const Vector3 & v2)
69 TT_INLINE
void Set(
float m11,
float m12,
float m13,
float m21,
float m22,
float m23,
float m31,
float m32,
float m33)
71 Row[0].Set(m11,m12,m13);
72 Row[1].Set(m21,m22,m23);
73 Row[2].Set(m31,m32,m33);
75 TT_INLINE
void Set(
const Vector3 & axis,
float angle)
77 Set(axis,sinf(angle),cosf(angle));
79 TT_INLINE
void Set(
const Vector3 & axis,
float s,
float c)
82 (
float)(axis[0]*axis[0] + c*(1.0f - axis[0]*axis[0])),
83 (
float)(axis[0]*axis[1]*(1.0f - c) - axis[2]*s),
84 (
float)(axis[2]*axis[0]*(1.0f - c) + axis[1]*s)
87 (
float)(axis[0]*axis[1]*(1.0f - c) + axis[2]*s),
88 (
float)(axis[1]*axis[1] + c*(1.0f - axis[1]*axis[1])),
89 (
float)(axis[1]*axis[2]*(1.0f - c) - axis[0]*s)
92 (
float)(axis[2]*axis[0]*(1.0f - c) - axis[1]*s),
93 (
float)(axis[1]*axis[2]*(1.0f - c) + axis[0]*s),
94 (
float)(axis[2]*axis[2] + c*(1 - axis[2]*axis[2]))
97 void Set(
const Quaternion & q);
98 TT_INLINE Vector3 & operator [] (
int i) {
return Row[i]; }
99 TT_INLINE
const Vector3 & operator [] (
int i)
const {
return Row[i]; }
100 TT_INLINE Matrix3 Transpose (
void)
const
103 Vector3(Row[0][0], Row[1][0], Row[2][0]),
104 Vector3(Row[0][1], Row[1][1], Row[2][1]),
105 Vector3(Row[0][2], Row[1][2], Row[2][2])
108 TT_INLINE
float Determinant (
void)
const
110 return Row[0][0] * (Row[1][1] * Row[2][2] - Row[1][2] * Row[2][1])
111 - Row[0][1] * (Row[1][0] * Row[2][2] - Row[1][2] * Row[2][0])
112 - Row[0][2] * (Row[1][0] * Row[2][1] - Row[1][1] * Row[2][0]);
114 TT_INLINE Matrix3 & operator = (
const Matrix3 & m)
116 Row[0] = m.Row[0]; Row[1] = m.Row[1]; Row[2] = m.Row[2];
119 Matrix3 & operator = (
const Matrix3D & m);
120 Matrix3 & operator = (
const Matrix4 & m);
121 TT_INLINE Matrix3 & operator += (
const Matrix3 & m)
123 Row[0] += m.Row[0]; Row[1] += m.Row[1]; Row[2] += m.Row[2];
126 TT_INLINE Matrix3 & operator -= (
const Matrix3 & m)
128 Row[0] -= m.Row[0]; Row[1] -= m.Row[1]; Row[2] -= m.Row[2];
131 TT_INLINE Matrix3 & operator *= (
float d)
133 Row[0] *= d; Row[1] *= d; Row[2] *= d;
136 TT_INLINE Matrix3 & operator /= (
float d)
138 Row[0] /= d; Row[1] /= d; Row[2] /= d;
141 TT_INLINE
void Make_Identity(
void)
143 Row[0].Set(1.0f,0.0f,0.0f);
144 Row[1].Set(0.0f,1.0f,0.0f);
145 Row[2].Set(0.0f,0.0f,1.0f);
147 TT_INLINE
void Rotate_X(
float theta)
149 Rotate_X(sinf(theta),cosf(theta));
151 TT_INLINE
void Rotate_X(
float s,
float c)
154 tmp1 = Row[0][1]; tmp2 = Row[0][2];
155 Row[0][1] = (float)( c*tmp1 + s*tmp2);
156 Row[0][2] = (float)(-s*tmp1 + c*tmp2);
157 tmp1 = Row[1][1]; tmp2 = Row[1][2];
158 Row[1][1] = (float)( c*tmp1 + s*tmp2);
159 Row[1][2] = (float)(-s*tmp1 + c*tmp2);
160 tmp1 = Row[2][1]; tmp2 = Row[2][2];
161 Row[2][1] = (float)( c*tmp1 + s*tmp2);
162 Row[2][2] = (float)(-s*tmp1 + c*tmp2);
164 TT_INLINE
void Rotate_Y(
float theta)
166 Rotate_Y(sinf(theta),cosf(theta));
168 TT_INLINE
void Rotate_Y(
float s,
float c)
171 tmp1 = Row[0][0]; tmp2 = Row[0][2];
172 Row[0][0] = (float)(c*tmp1 - s*tmp2);
173 Row[0][2] = (float)(s*tmp1 + c*tmp2);
174 tmp1 = Row[1][0]; tmp2 = Row[1][2];
175 Row[1][0] = (float)(c*tmp1 - s*tmp2);
176 Row[1][2] = (float)(s*tmp1 + c*tmp2);
177 tmp1 = Row[2][0]; tmp2 = Row[2][2];
178 Row[2][0] = (float)(c*tmp1 - s*tmp2);
179 Row[2][2] = (float)(s*tmp1 + c*tmp2);
181 TT_INLINE
void Rotate_Z(
float theta)
183 Rotate_Z(sinf(theta),cosf(theta));
185 TT_INLINE
void Rotate_Z(
float s,
float c)
188 tmp1 = Row[0][0]; tmp2 = Row[0][1];
189 Row[0][0] = (float)( c*tmp1 + s*tmp2);
190 Row[0][1] = (float)(-s*tmp1 + c*tmp2);
191 tmp1 = Row[1][0]; tmp2 = Row[1][1];
192 Row[1][0] = (float)( c*tmp1 + s*tmp2);
193 Row[1][1] = (float)(-s*tmp1 + c*tmp2);
194 tmp1 = Row[2][0]; tmp2 = Row[2][1];
195 Row[2][0] = (float)( c*tmp1 + s*tmp2);
196 Row[2][1] = (float)(-s*tmp1 + c*tmp2);
198 TT_INLINE
float Get_X_Rotation(
void)
const
200 Vector3 v = (*this) * Vector3(0.0,1.0,0.0);
201 return atan2(v[2], v[1]);
203 TT_INLINE
float Get_Y_Rotation(
void)
const
205 Vector3 v = (*this) * Vector3(0.0,0.0,1.0);
206 return atan2(v[0],v[2]);
208 TT_INLINE
float Get_Z_Rotation(
void)
const
210 Vector3 v = (*this) * Vector3(1.0,0.0,0.0);
211 return atan2(v[1],v[0]);
213 TT_INLINE Vector3 Get_X_Vector(
void)
const
215 return Vector3(Row[0][0], Row[1][0], Row[2][0]);
217 TT_INLINE Vector3 Get_Y_Vector(
void)
const
219 return Vector3(Row[0][1], Row[1][1], Row[2][1]);
221 TT_INLINE Vector3 Get_Z_Vector(
void)
const
223 return Vector3(Row[0][2], Row[1][2], Row[2][2]);
225 TT_INLINE
void Get_X_Vector(Vector3 * set_x)
const
227 set_x->Set(Row[0][0], Row[1][0], Row[2][0]);
229 TT_INLINE
void Get_Y_Vector(Vector3 * set_y)
const
231 set_y->Set(Row[0][1], Row[1][1], Row[2][1]);
233 TT_INLINE
void Get_Z_Vector(Vector3 * set_z)
const
235 set_z->Set(Row[0][2], Row[1][2], Row[2][2]);
237 friend Matrix3 operator - (
const Matrix3& a);
238 friend Matrix3 operator * (
const Matrix3& a,
float d);
239 friend Matrix3 operator * (
float d,
const Matrix3& a);
240 friend Matrix3 operator / (
const Matrix3& a,
float d);
241 friend Matrix3 operator + (
const Matrix3& a,
const Matrix3& b);
242 friend Matrix3 operator - (
const Matrix3 & a,
const Matrix3 & b);
243 friend Matrix3 operator * (
const Matrix3 & a,
const Matrix3 & b);
244 friend Matrix3 operator * (
const Matrix3D & a,
const Matrix3 & b);
245 friend Matrix3 operator * (
const Matrix3 & a,
const Matrix3D & b);
246 friend int operator == (
const Matrix3 & a,
const Matrix3 & b);
247 friend int operator != (
const Matrix3 & a,
const Matrix3 & b);
248 friend void Swap(Matrix3 & a,Matrix3 & b);
249 friend Vector3 operator * (
const Matrix3 & a,
const Vector3 & v);
250 TT_INLINE
static void Add(
const Matrix3 & a,
const Matrix3 & b,Matrix3 * res)
252 Vector3::Add(a.Row[0],b.Row[0],&(res->Row[0]));
253 Vector3::Add(a.Row[1],b.Row[1],&(res->Row[1]));
254 Vector3::Add(a.Row[2],b.Row[2],&(res->Row[2]));
256 TT_INLINE
static void Subtract(
const Matrix3 & a,
const Matrix3 & b,Matrix3 * res)
258 Vector3::Subtract(a.Row[0],b.Row[0],&(res->Row[0]));
259 Vector3::Subtract(a.Row[1],b.Row[1],&(res->Row[1]));
260 Vector3::Subtract(a.Row[2],b.Row[2],&(res->Row[2]));
262 static void Multiply(
const Matrix3 & a,
const Matrix3 & b,Matrix3 * res);
263 static void Multiply(
const Matrix3D & a,
const Matrix3 & b,Matrix3 * res);
264 static void Multiply(
const Matrix3 & a,
const Matrix3D & b,Matrix3 * res);
265 TT_INLINE
static void Rotate_Vector(
const Matrix3 & A,
const Vector3 & in,Vector3 * out)
278 out->X = (A[0][0] * v->X + A[0][1] * v->Y + A[0][2] * v->Z);
279 out->Y = (A[1][0] * v->X + A[1][1] * v->Y + A[1][2] * v->Z);
280 out->Z = (A[2][0] * v->X + A[2][1] * v->Y + A[2][2] * v->Z);
282 TT_INLINE
static void Transpose_Rotate_Vector(
const Matrix3 & A,
const Vector3 & in,Vector3 * out)
293 out->X = (A[0][0] * v->X + A[1][0] * v->Y + A[2][0] * v->Z);
294 out->Y = (A[0][1] * v->X + A[1][1] * v->Y + A[2][1] * v->Z);
295 out->Z = (A[0][2] * v->X + A[1][2] * v->Y + A[2][2] * v->Z);
297 TT_INLINE
void Rotate_AABox_Extent(
const Vector3 & extent,Vector3 * new_extent)
299 for (
int i=0; i<3; i++)
301 (*new_extent)[i] = 0.0f;
302 for (
int j=0; j<3; j++)
304 (*new_extent)[i] += WWMath::Fabs(Row[i][j] * extent[j]);
308 TT_INLINE Matrix3 Inverse (
void)
const
315 for (j=0; j<3; j++) {
319 for (i=j+1; i<3; i++) {
320 if (WWMath::Fabs(a[i][j]) > WWMath::Fabs(a[i1][j])) {
326 Swap(a.Row[i1], a.Row[j]);
327 Swap(b.Row[i1], b.Row[j]);
333 b.Row[j] /= a.Row[j][j];
334 a.Row[j] /= a.Row[j][j];
337 for (i=0; i<3; i++) {
339 b.Row[i] -= a[i][j] * b.Row[j];
340 a.Row[i] -= a[i][j] * a.Row[j];
346 static const Matrix3 Identity;
350TT_INLINE Matrix3 operator - (
const Matrix3 & a)
352 return Matrix3(-a.Row[0], -a.Row[1], -a.Row[2]);
355TT_INLINE Matrix3 operator * (
const Matrix3 & a,
float d)
357 return Matrix3(a.Row[0] * d, a.Row[1] * d, a.Row[2] * d);
360TT_INLINE Matrix3 operator * (
float d,
const Matrix3 & a)
365TT_INLINE Matrix3 operator / (
const Matrix3 & a,
float d)
367 float ood = 1.0f / d;
368 return Matrix3(a.Row[0] * ood, a.Row[1] * ood, a.Row[2] * ood);
370TT_INLINE Matrix3 operator + (
const Matrix3 & a,
const Matrix3 & b)
378TT_INLINE Matrix3 operator - (
const Matrix3 & a,
const Matrix3 & b)
386TT_INLINE Matrix3 operator * (
const Matrix3 & a,
const Matrix3 & b)
388 #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
390 Vector3(ROWCOL(0,0), ROWCOL(0,1), ROWCOL(0,2) ),
391 Vector3(ROWCOL(1,0), ROWCOL(1,1), ROWCOL(1,2) ),
392 Vector3(ROWCOL(2,0), ROWCOL(2,1), ROWCOL(2,2) )
397TT_INLINE Vector3 operator * (
const Matrix3 & a,
const Vector3 & v)
400 a[0][0] * v[0] + a[0][1] * v[1] + a[0][2] * v[2],
401 a[1][0] * v[0] + a[1][1] * v[1] + a[1][2] * v[2],
402 a[2][0] * v[0] + a[2][1] * v[1] + a[2][2] * v[2]
406TT_INLINE
int operator == (
const Matrix3 & a,
const Matrix3 & b)
408 return ((a [0] == b [0]) && (a [1] == b [1]) && (a [2] == b [2]));
411TT_INLINE
int operator != (
const Matrix3 & a,
const Matrix3 & b)
415TT_INLINE Matrix3 Create_X_Rotation_Matrix3(
float s,
float c)
429TT_INLINE Matrix3 Create_X_Rotation_Matrix3(
float rad)
431 return Create_X_Rotation_Matrix3(sinf(rad),cosf(rad));
433TT_INLINE Matrix3 Create_Y_Rotation_Matrix3(
float s,
float c)
447TT_INLINE Matrix3 Create_Y_Rotation_Matrix3(
float rad)
449 return Create_Y_Rotation_Matrix3(sinf(rad),cosf(rad));
451TT_INLINE Matrix3 Create_Z_Rotation_Matrix3(
float s,
float c)
465TT_INLINE Matrix3 Create_Z_Rotation_Matrix3(
float rad)
467 return Create_Z_Rotation_Matrix3(sinf(rad),cosf(rad));