| 1 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 |
/** |
| 3 |
* Contains code for 4x4 matrices. |
| 4 |
* \file IceMatrix4x4.h |
| 5 |
* \author Pierre Terdiman |
| 6 |
* \date April, 4, 2000 |
| 7 |
*/ |
| 8 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 9 |
|
| 10 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 11 |
// Include Guard |
| 12 |
#ifndef __ICEMATRIX4X4_H__ |
| 13 |
#define __ICEMATRIX4X4_H__ |
| 14 |
|
| 15 |
// Forward declarations |
| 16 |
class PRS; |
| 17 |
class PR; |
| 18 |
|
| 19 |
#define MATRIX4X4_EPSILON (1.0e-7f) |
| 20 |
|
| 21 |
class ICEMATHS_API Matrix4x4 |
| 22 |
{ |
| 23 |
// void LUBackwardSubstitution( sdword *indx, float* b ); |
| 24 |
// void LUDecomposition( sdword* indx, float* d ); |
| 25 |
|
| 26 |
public: |
| 27 |
//! Empty constructor. |
| 28 |
inline_ Matrix4x4() {} |
| 29 |
//! Constructor from 16 values |
| 30 |
inline_ Matrix4x4( float m00, float m01, float m02, float m03, |
| 31 |
float m10, float m11, float m12, float m13, |
| 32 |
float m20, float m21, float m22, float m23, |
| 33 |
float m30, float m31, float m32, float m33) |
| 34 |
{ |
| 35 |
m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; |
| 36 |
m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; |
| 37 |
m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; |
| 38 |
m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; |
| 39 |
} |
| 40 |
//! Copy constructor |
| 41 |
inline_ Matrix4x4(const Matrix4x4& mat) { CopyMemory(m, &mat.m, 16*sizeof(float)); } |
| 42 |
//! Destructor. |
| 43 |
inline_ ~Matrix4x4() {} |
| 44 |
|
| 45 |
//! Assign values (rotation only) |
| 46 |
template<typename trotationfloat> |
| 47 |
inline_ Matrix4x4& Set( trotationfloat m00, trotationfloat m01, trotationfloat m02, |
| 48 |
trotationfloat m10, trotationfloat m11, trotationfloat m12, |
| 49 |
trotationfloat m20, trotationfloat m21, trotationfloat m22) |
| 50 |
{ |
| 51 |
m[0][0] = (float)m00; m[0][1] = (float)m01; m[0][2] = (float)m02; |
| 52 |
m[1][0] = (float)m10; m[1][1] = (float)m11; m[1][2] = (float)m12; |
| 53 |
m[2][0] = (float)m20; m[2][1] = (float)m21; m[2][2] = (float)m22; |
| 54 |
return *this; |
| 55 |
} |
| 56 |
//! Assign values |
| 57 |
template<typename trotationfloat, typename toffsetfloat, typename textrafloat> |
| 58 |
inline_ Matrix4x4& Set( trotationfloat m00, trotationfloat m01, trotationfloat m02, textrafloat m03, |
| 59 |
trotationfloat m10, trotationfloat m11, trotationfloat m12, textrafloat m13, |
| 60 |
trotationfloat m20, trotationfloat m21, trotationfloat m22, textrafloat m23, |
| 61 |
toffsetfloat m30, toffsetfloat m31, toffsetfloat m32, textrafloat m33) |
| 62 |
{ |
| 63 |
m[0][0] = (float)m00; m[0][1] = (float)m01; m[0][2] = (float)m02; m[0][3] = (float)m03; |
| 64 |
m[1][0] = (float)m10; m[1][1] = (float)m11; m[1][2] = (float)m12; m[1][3] = (float)m13; |
| 65 |
m[2][0] = (float)m20; m[2][1] = (float)m21; m[2][2] = (float)m22; m[2][3] = (float)m23; |
| 66 |
m[3][0] = (float)m30; m[3][1] = (float)m31; m[3][2] = (float)m32; m[3][3] = (float)m33; |
| 67 |
return *this; |
| 68 |
} |
| 69 |
|
| 70 |
//! Copy from a Matrix4x4 |
| 71 |
inline_ void Copy(const Matrix4x4& source) { CopyMemory(m, source.m, 16*sizeof(float)); } |
| 72 |
|
| 73 |
// Row-column access |
| 74 |
//! Returns a row. |
| 75 |
inline_ void GetRow(const udword r, HPoint& p) const { p.x=m[r][0]; p.y=m[r][1]; p.z=m[r][2]; p.w=m[r][3]; } |
| 76 |
//! Returns a row. |
| 77 |
inline_ void GetRow(const udword r, Point& p) const { p.x=m[r][0]; p.y=m[r][1]; p.z=m[r][2]; } |
| 78 |
//! Returns a row. |
| 79 |
inline_ const HPoint& GetRow(const udword r) const { return *(const HPoint*)&m[r][0]; } |
| 80 |
//! Returns a row. |
| 81 |
inline_ HPoint& GetRow(const udword r) { return *(HPoint*)&m[r][0]; } |
| 82 |
//! Sets a row. |
| 83 |
inline_ void SetRow(const udword r, const HPoint& p) { m[r][0]=p.x; m[r][1]=p.y; m[r][2]=p.z; m[r][3]=p.w; } |
| 84 |
//! Sets a row. |
| 85 |
inline_ void SetRow(const udword r, const Point& p) { m[r][0]=p.x; m[r][1]=p.y; m[r][2]=p.z; m[r][3]= (r!=3) ? 0.0f : 1.0f; } |
| 86 |
//! Returns a column. |
| 87 |
inline_ void GetCol(const udword c, HPoint& p) const { p.x=m[0][c]; p.y=m[1][c]; p.z=m[2][c]; p.w=m[3][c]; } |
| 88 |
//! Returns a column. |
| 89 |
inline_ void GetCol(const udword c, Point& p) const { p.x=m[0][c]; p.y=m[1][c]; p.z=m[2][c]; } |
| 90 |
//! Sets a column. |
| 91 |
inline_ void SetCol(const udword c, const HPoint& p) { m[0][c]=p.x; m[1][c]=p.y; m[2][c]=p.z; m[3][c]=p.w; } |
| 92 |
//! Sets a column. |
| 93 |
inline_ void SetCol(const udword c, const Point& p) { m[0][c]=p.x; m[1][c]=p.y; m[2][c]=p.z; m[3][c]= (c!=3) ? 0.0f : 1.0f; } |
| 94 |
|
| 95 |
// Translation |
| 96 |
//! Returns the translation part of the matrix. |
| 97 |
inline_ const HPoint& GetTrans() const { return GetRow(3); } |
| 98 |
//! Gets the translation part of the matrix |
| 99 |
inline_ void GetTrans(Point& p) const { p.x=m[3][0]; p.y=m[3][1]; p.z=m[3][2]; } |
| 100 |
//! Sets the translation part of the matrix, from a Point. |
| 101 |
inline_ void SetTrans(const Point& p) { m[3][0]=p.x; m[3][1]=p.y; m[3][2]=p.z; } |
| 102 |
//! Sets the translation part of the matrix, from a HPoint. |
| 103 |
inline_ void SetTrans(const HPoint& p) { m[3][0]=p.x; m[3][1]=p.y; m[3][2]=p.z; m[3][3]=p.w; } |
| 104 |
//! Sets the translation part of the matrix, from floats. |
| 105 |
inline_ void SetTrans(float tx, float ty, float tz) { m[3][0]=tx; m[3][1]=ty; m[3][2]=tz; } |
| 106 |
|
| 107 |
// Scale |
| 108 |
//! Sets the scale from a Point. The point is put on the diagonal. |
| 109 |
inline_ void SetScale(const Point& p) { m[0][0]=p.x; m[1][1]=p.y; m[2][2]=p.z; } |
| 110 |
//! Sets the scale from floats. Values are put on the diagonal. |
| 111 |
inline_ void SetScale(float sx, float sy, float sz) { m[0][0]=sx; m[1][1]=sy; m[2][2]=sz; } |
| 112 |
//! Scales from a Point. Each row is multiplied by a component. |
| 113 |
void Scale(const Point& p) |
| 114 |
{ |
| 115 |
m[0][0] *= p.x; m[1][0] *= p.y; m[2][0] *= p.z; |
| 116 |
m[0][1] *= p.x; m[1][1] *= p.y; m[2][1] *= p.z; |
| 117 |
m[0][2] *= p.x; m[1][2] *= p.y; m[2][2] *= p.z; |
| 118 |
} |
| 119 |
//! Scales from floats. Each row is multiplied by a value. |
| 120 |
void Scale(float sx, float sy, float sz) |
| 121 |
{ |
| 122 |
m[0][0] *= sx; m[1][0] *= sy; m[2][0] *= sz; |
| 123 |
m[0][1] *= sx; m[1][1] *= sy; m[2][1] *= sz; |
| 124 |
m[0][2] *= sx; m[1][2] *= sy; m[2][2] *= sz; |
| 125 |
} |
| 126 |
/* |
| 127 |
//! Returns a row. |
| 128 |
inline_ HPoint GetRow(const udword row) const { return mRow[row]; } |
| 129 |
//! Sets a row. |
| 130 |
inline_ Matrix4x4& SetRow(const udword row, const HPoint& p) { mRow[row] = p; return *this; } |
| 131 |
//! Sets a row. |
| 132 |
Matrix4x4& SetRow(const udword row, const Point& p) |
| 133 |
{ |
| 134 |
m[row][0] = p.x; |
| 135 |
m[row][1] = p.y; |
| 136 |
m[row][2] = p.z; |
| 137 |
m[row][3] = (row != 3) ? 0.0f : 1.0f; |
| 138 |
return *this; |
| 139 |
} |
| 140 |
//! Returns a column. |
| 141 |
HPoint GetCol(const udword col) const |
| 142 |
{ |
| 143 |
HPoint Res; |
| 144 |
Res.x = m[0][col]; |
| 145 |
Res.y = m[1][col]; |
| 146 |
Res.z = m[2][col]; |
| 147 |
Res.w = m[3][col]; |
| 148 |
return Res; |
| 149 |
} |
| 150 |
//! Sets a column. |
| 151 |
Matrix4x4& SetCol(const udword col, const HPoint& p) |
| 152 |
{ |
| 153 |
m[0][col] = p.x; |
| 154 |
m[1][col] = p.y; |
| 155 |
m[2][col] = p.z; |
| 156 |
m[3][col] = p.w; |
| 157 |
return *this; |
| 158 |
} |
| 159 |
//! Sets a column. |
| 160 |
Matrix4x4& SetCol(const udword col, const Point& p) |
| 161 |
{ |
| 162 |
m[0][col] = p.x; |
| 163 |
m[1][col] = p.y; |
| 164 |
m[2][col] = p.z; |
| 165 |
m[3][col] = (col != 3) ? 0.0f : 1.0f; |
| 166 |
return *this; |
| 167 |
} |
| 168 |
*/ |
| 169 |
//! Computes the trace. The trace is the sum of the 4 diagonal components. |
| 170 |
inline_ float Trace() const { return m[0][0] + m[1][1] + m[2][2] + m[3][3]; } |
| 171 |
//! Computes the trace of the upper 3x3 matrix. |
| 172 |
inline_ float Trace3x3() const { return m[0][0] + m[1][1] + m[2][2]; } |
| 173 |
//! Clears the matrix. |
| 174 |
inline_ void Zero() { ZeroMemory(&m, sizeof(m)); } |
| 175 |
//! Sets the identity matrix. |
| 176 |
inline_ void Identity() { Zero(); m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0f; } |
| 177 |
//! Checks for identity |
| 178 |
inline_ bool IsIdentity() const |
| 179 |
{ |
| 180 |
if(IR(m[0][0])!=IEEE_1_0) return false; |
| 181 |
if(IR(m[0][1])!=0) return false; |
| 182 |
if(IR(m[0][2])!=0) return false; |
| 183 |
if(IR(m[0][3])!=0) return false; |
| 184 |
|
| 185 |
if(IR(m[1][0])!=0) return false; |
| 186 |
if(IR(m[1][1])!=IEEE_1_0) return false; |
| 187 |
if(IR(m[1][2])!=0) return false; |
| 188 |
if(IR(m[1][3])!=0) return false; |
| 189 |
|
| 190 |
if(IR(m[2][0])!=0) return false; |
| 191 |
if(IR(m[2][1])!=0) return false; |
| 192 |
if(IR(m[2][2])!=IEEE_1_0) return false; |
| 193 |
if(IR(m[2][3])!=0) return false; |
| 194 |
|
| 195 |
if(IR(m[3][0])!=0) return false; |
| 196 |
if(IR(m[3][1])!=0) return false; |
| 197 |
if(IR(m[3][2])!=0) return false; |
| 198 |
if(IR(m[3][3])!=IEEE_1_0) return false; |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
//! Checks matrix validity |
| 203 |
inline_ BOOL IsValid() const |
| 204 |
{ |
| 205 |
for(udword j=0;j<4;j++) |
| 206 |
{ |
| 207 |
for(udword i=0;i<4;i++) |
| 208 |
{ |
| 209 |
if(!IsValidFloat(m[j][i])) return FALSE; |
| 210 |
} |
| 211 |
} |
| 212 |
return TRUE; |
| 213 |
} |
| 214 |
|
| 215 |
//! Sets a rotation matrix around the X axis. |
| 216 |
void RotX(float angle) { float Cos = cosf(angle), Sin = sinf(angle); Identity(); m[1][1] = m[2][2] = Cos; m[2][1] = -Sin; m[1][2] = Sin; } |
| 217 |
//! Sets a rotation matrix around the Y axis. |
| 218 |
void RotY(float angle) { float Cos = cosf(angle), Sin = sinf(angle); Identity(); m[0][0] = m[2][2] = Cos; m[2][0] = Sin; m[0][2] = -Sin; } |
| 219 |
//! Sets a rotation matrix around the Z axis. |
| 220 |
void RotZ(float angle) { float Cos = cosf(angle), Sin = sinf(angle); Identity(); m[0][0] = m[1][1] = Cos; m[1][0] = -Sin; m[0][1] = Sin; } |
| 221 |
|
| 222 |
//! Makes a rotation matrix about an arbitrary axis |
| 223 |
Matrix4x4& Rot(float angle, Point& p1, Point& p2); |
| 224 |
|
| 225 |
//! Transposes the matrix. |
| 226 |
void Transpose() |
| 227 |
{ |
| 228 |
TSwap(m[1][0], m[0][1]); |
| 229 |
TSwap(m[2][0], m[0][2]); |
| 230 |
TSwap(m[3][0], m[0][3]); |
| 231 |
TSwap(m[1][2], m[2][1]); |
| 232 |
TSwap(m[1][3], m[3][1]); |
| 233 |
TSwap(m[2][3], m[3][2]); |
| 234 |
} |
| 235 |
|
| 236 |
//! Computes a cofactor. Used for matrix inversion. |
| 237 |
float CoFactor(udword row, udword col) const; |
| 238 |
//! Computes the determinant of the matrix. |
| 239 |
float Determinant() const; |
| 240 |
//! Inverts the matrix. Determinant must be different from zero, else matrix can't be inverted. |
| 241 |
Matrix4x4& Invert(); |
| 242 |
// Matrix& ComputeAxisMatrix(Point& axis, float angle); |
| 243 |
|
| 244 |
// Cast operators |
| 245 |
//! Casts a Matrix4x4 to a Matrix3x3. |
| 246 |
inline_ operator Matrix3x3() const |
| 247 |
{ |
| 248 |
return Matrix3x3( |
| 249 |
m[0][0], m[0][1], m[0][2], |
| 250 |
m[1][0], m[1][1], m[1][2], |
| 251 |
m[2][0], m[2][1], m[2][2]); |
| 252 |
} |
| 253 |
//! Casts a Matrix4x4 to a Quat. |
| 254 |
operator Quat() const; |
| 255 |
//! Casts a Matrix4x4 to a PR. |
| 256 |
operator PR() const; |
| 257 |
|
| 258 |
// Arithmetic operators |
| 259 |
//! Operator for Matrix4x4 Plus = Matrix4x4 + Matrix4x4; |
| 260 |
inline_ Matrix4x4 operator+(const Matrix4x4& mat) const |
| 261 |
{ |
| 262 |
return Matrix4x4( |
| 263 |
m[0][0]+mat.m[0][0], m[0][1]+mat.m[0][1], m[0][2]+mat.m[0][2], m[0][3]+mat.m[0][3], |
| 264 |
m[1][0]+mat.m[1][0], m[1][1]+mat.m[1][1], m[1][2]+mat.m[1][2], m[1][3]+mat.m[1][3], |
| 265 |
m[2][0]+mat.m[2][0], m[2][1]+mat.m[2][1], m[2][2]+mat.m[2][2], m[2][3]+mat.m[2][3], |
| 266 |
m[3][0]+mat.m[3][0], m[3][1]+mat.m[3][1], m[3][2]+mat.m[3][2], m[3][3]+mat.m[3][3]); |
| 267 |
} |
| 268 |
|
| 269 |
//! Operator for Matrix4x4 Minus = Matrix4x4 - Matrix4x4; |
| 270 |
inline_ Matrix4x4 operator-(const Matrix4x4& mat) const |
| 271 |
{ |
| 272 |
return Matrix4x4( |
| 273 |
m[0][0]-mat.m[0][0], m[0][1]-mat.m[0][1], m[0][2]-mat.m[0][2], m[0][3]-mat.m[0][3], |
| 274 |
m[1][0]-mat.m[1][0], m[1][1]-mat.m[1][1], m[1][2]-mat.m[1][2], m[1][3]-mat.m[1][3], |
| 275 |
m[2][0]-mat.m[2][0], m[2][1]-mat.m[2][1], m[2][2]-mat.m[2][2], m[2][3]-mat.m[2][3], |
| 276 |
m[3][0]-mat.m[3][0], m[3][1]-mat.m[3][1], m[3][2]-mat.m[3][2], m[3][3]-mat.m[3][3]); |
| 277 |
} |
| 278 |
|
| 279 |
//! Operator for Matrix4x4 Mul = Matrix4x4 * Matrix4x4; |
| 280 |
inline_ Matrix4x4 operator*(const Matrix4x4& mat) const |
| 281 |
{ |
| 282 |
return Matrix4x4( |
| 283 |
m[0][0]*mat.m[0][0] + m[0][1]*mat.m[1][0] + m[0][2]*mat.m[2][0] + m[0][3]*mat.m[3][0], |
| 284 |
m[0][0]*mat.m[0][1] + m[0][1]*mat.m[1][1] + m[0][2]*mat.m[2][1] + m[0][3]*mat.m[3][1], |
| 285 |
m[0][0]*mat.m[0][2] + m[0][1]*mat.m[1][2] + m[0][2]*mat.m[2][2] + m[0][3]*mat.m[3][2], |
| 286 |
m[0][0]*mat.m[0][3] + m[0][1]*mat.m[1][3] + m[0][2]*mat.m[2][3] + m[0][3]*mat.m[3][3], |
| 287 |
|
| 288 |
m[1][0]*mat.m[0][0] + m[1][1]*mat.m[1][0] + m[1][2]*mat.m[2][0] + m[1][3]*mat.m[3][0], |
| 289 |
m[1][0]*mat.m[0][1] + m[1][1]*mat.m[1][1] + m[1][2]*mat.m[2][1] + m[1][3]*mat.m[3][1], |
| 290 |
m[1][0]*mat.m[0][2] + m[1][1]*mat.m[1][2] + m[1][2]*mat.m[2][2] + m[1][3]*mat.m[3][2], |
| 291 |
m[1][0]*mat.m[0][3] + m[1][1]*mat.m[1][3] + m[1][2]*mat.m[2][3] + m[1][3]*mat.m[3][3], |
| 292 |
|
| 293 |
m[2][0]*mat.m[0][0] + m[2][1]*mat.m[1][0] + m[2][2]*mat.m[2][0] + m[2][3]*mat.m[3][0], |
| 294 |
m[2][0]*mat.m[0][1] + m[2][1]*mat.m[1][1] + m[2][2]*mat.m[2][1] + m[2][3]*mat.m[3][1], |
| 295 |
m[2][0]*mat.m[0][2] + m[2][1]*mat.m[1][2] + m[2][2]*mat.m[2][2] + m[2][3]*mat.m[3][2], |
| 296 |
m[2][0]*mat.m[0][3] + m[2][1]*mat.m[1][3] + m[2][2]*mat.m[2][3] + m[2][3]*mat.m[3][3], |
| 297 |
|
| 298 |
m[3][0]*mat.m[0][0] + m[3][1]*mat.m[1][0] + m[3][2]*mat.m[2][0] + m[3][3]*mat.m[3][0], |
| 299 |
m[3][0]*mat.m[0][1] + m[3][1]*mat.m[1][1] + m[3][2]*mat.m[2][1] + m[3][3]*mat.m[3][1], |
| 300 |
m[3][0]*mat.m[0][2] + m[3][1]*mat.m[1][2] + m[3][2]*mat.m[2][2] + m[3][3]*mat.m[3][2], |
| 301 |
m[3][0]*mat.m[0][3] + m[3][1]*mat.m[1][3] + m[3][2]*mat.m[2][3] + m[3][3]*mat.m[3][3]); |
| 302 |
} |
| 303 |
|
| 304 |
//! Operator for HPoint Mul = Matrix4x4 * HPoint; |
| 305 |
inline_ HPoint operator*(const HPoint& v) const { return HPoint(GetRow(0)|v, GetRow(1)|v, GetRow(2)|v, GetRow(3)|v); } |
| 306 |
|
| 307 |
//! Operator for Point Mul = Matrix4x4 * Point; |
| 308 |
inline_ Point operator*(const Point& v) const |
| 309 |
{ |
| 310 |
return Point( m[0][0]*v.x + m[0][1]*v.y + m[0][2]*v.z + m[0][3], |
| 311 |
m[1][0]*v.x + m[1][1]*v.y + m[1][2]*v.z + m[1][3], |
| 312 |
m[2][0]*v.x + m[2][1]*v.y + m[2][2]*v.z + m[2][3] ); |
| 313 |
} |
| 314 |
|
| 315 |
//! Operator for Matrix4x4 Scale = Matrix4x4 * float; |
| 316 |
inline_ Matrix4x4 operator*(float s) const |
| 317 |
{ |
| 318 |
return Matrix4x4( |
| 319 |
m[0][0]*s, m[0][1]*s, m[0][2]*s, m[0][3]*s, |
| 320 |
m[1][0]*s, m[1][1]*s, m[1][2]*s, m[1][3]*s, |
| 321 |
m[2][0]*s, m[2][1]*s, m[2][2]*s, m[2][3]*s, |
| 322 |
m[3][0]*s, m[3][1]*s, m[3][2]*s, m[3][3]*s); |
| 323 |
} |
| 324 |
|
| 325 |
//! Operator for Matrix4x4 Scale = float * Matrix4x4; |
| 326 |
inline_ friend Matrix4x4 operator*(float s, const Matrix4x4& mat) |
| 327 |
{ |
| 328 |
return Matrix4x4( |
| 329 |
s*mat.m[0][0], s*mat.m[0][1], s*mat.m[0][2], s*mat.m[0][3], |
| 330 |
s*mat.m[1][0], s*mat.m[1][1], s*mat.m[1][2], s*mat.m[1][3], |
| 331 |
s*mat.m[2][0], s*mat.m[2][1], s*mat.m[2][2], s*mat.m[2][3], |
| 332 |
s*mat.m[3][0], s*mat.m[3][1], s*mat.m[3][2], s*mat.m[3][3]); |
| 333 |
} |
| 334 |
|
| 335 |
//! Operator for Matrix4x4 Div = Matrix4x4 / float; |
| 336 |
inline_ Matrix4x4 operator/(float s) const |
| 337 |
{ |
| 338 |
if(s) s = 1.0f / s; |
| 339 |
|
| 340 |
return Matrix4x4( |
| 341 |
m[0][0]*s, m[0][1]*s, m[0][2]*s, m[0][3]*s, |
| 342 |
m[1][0]*s, m[1][1]*s, m[1][2]*s, m[1][3]*s, |
| 343 |
m[2][0]*s, m[2][1]*s, m[2][2]*s, m[2][3]*s, |
| 344 |
m[3][0]*s, m[3][1]*s, m[3][2]*s, m[3][3]*s); |
| 345 |
} |
| 346 |
|
| 347 |
//! Operator for Matrix4x4 Div = float / Matrix4x4; |
| 348 |
inline_ friend Matrix4x4 operator/(float s, const Matrix4x4& mat) |
| 349 |
{ |
| 350 |
return Matrix4x4( |
| 351 |
s/mat.m[0][0], s/mat.m[0][1], s/mat.m[0][2], s/mat.m[0][3], |
| 352 |
s/mat.m[1][0], s/mat.m[1][1], s/mat.m[1][2], s/mat.m[1][3], |
| 353 |
s/mat.m[2][0], s/mat.m[2][1], s/mat.m[2][2], s/mat.m[2][3], |
| 354 |
s/mat.m[3][0], s/mat.m[3][1], s/mat.m[3][2], s/mat.m[3][3]); |
| 355 |
} |
| 356 |
|
| 357 |
//! Operator for Matrix4x4 += Matrix4x4; |
| 358 |
inline_ Matrix4x4& operator+=(const Matrix4x4& mat) |
| 359 |
{ |
| 360 |
m[0][0]+=mat.m[0][0]; m[0][1]+=mat.m[0][1]; m[0][2]+=mat.m[0][2]; m[0][3]+=mat.m[0][3]; |
| 361 |
m[1][0]+=mat.m[1][0]; m[1][1]+=mat.m[1][1]; m[1][2]+=mat.m[1][2]; m[1][3]+=mat.m[1][3]; |
| 362 |
m[2][0]+=mat.m[2][0]; m[2][1]+=mat.m[2][1]; m[2][2]+=mat.m[2][2]; m[2][3]+=mat.m[2][3]; |
| 363 |
m[3][0]+=mat.m[3][0]; m[3][1]+=mat.m[3][1]; m[3][2]+=mat.m[3][2]; m[3][3]+=mat.m[3][3]; |
| 364 |
return *this; |
| 365 |
} |
| 366 |
|
| 367 |
//! Operator for Matrix4x4 -= Matrix4x4; |
| 368 |
inline_ Matrix4x4& operator-=(const Matrix4x4& mat) |
| 369 |
{ |
| 370 |
m[0][0]-=mat.m[0][0]; m[0][1]-=mat.m[0][1]; m[0][2]-=mat.m[0][2]; m[0][3]-=mat.m[0][3]; |
| 371 |
m[1][0]-=mat.m[1][0]; m[1][1]-=mat.m[1][1]; m[1][2]-=mat.m[1][2]; m[1][3]-=mat.m[1][3]; |
| 372 |
m[2][0]-=mat.m[2][0]; m[2][1]-=mat.m[2][1]; m[2][2]-=mat.m[2][2]; m[2][3]-=mat.m[2][3]; |
| 373 |
m[3][0]-=mat.m[3][0]; m[3][1]-=mat.m[3][1]; m[3][2]-=mat.m[3][2]; m[3][3]-=mat.m[3][3]; |
| 374 |
return *this; |
| 375 |
} |
| 376 |
|
| 377 |
//! Operator for Matrix4x4 *= Matrix4x4; |
| 378 |
Matrix4x4& operator*=(const Matrix4x4& mat) |
| 379 |
{ |
| 380 |
HPoint TempRow; |
| 381 |
|
| 382 |
GetRow(0, TempRow); |
| 383 |
m[0][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0]; |
| 384 |
m[0][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1]; |
| 385 |
m[0][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2]; |
| 386 |
m[0][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3]; |
| 387 |
|
| 388 |
GetRow(1, TempRow); |
| 389 |
m[1][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0]; |
| 390 |
m[1][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1]; |
| 391 |
m[1][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2]; |
| 392 |
m[1][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3]; |
| 393 |
|
| 394 |
GetRow(2, TempRow); |
| 395 |
m[2][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0]; |
| 396 |
m[2][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1]; |
| 397 |
m[2][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2]; |
| 398 |
m[2][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3]; |
| 399 |
|
| 400 |
GetRow(3, TempRow); |
| 401 |
m[3][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0]; |
| 402 |
m[3][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1]; |
| 403 |
m[3][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2]; |
| 404 |
m[3][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3]; |
| 405 |
|
| 406 |
return *this; |
| 407 |
} |
| 408 |
|
| 409 |
//! Operator for Matrix4x4 *= float; |
| 410 |
inline_ Matrix4x4& operator*=(float s) |
| 411 |
{ |
| 412 |
m[0][0]*=s; m[0][1]*=s; m[0][2]*=s; m[0][3]*=s; |
| 413 |
m[1][0]*=s; m[1][1]*=s; m[1][2]*=s; m[1][3]*=s; |
| 414 |
m[2][0]*=s; m[2][1]*=s; m[2][2]*=s; m[2][3]*=s; |
| 415 |
m[3][0]*=s; m[3][1]*=s; m[3][2]*=s; m[3][3]*=s; |
| 416 |
return *this; |
| 417 |
} |
| 418 |
|
| 419 |
//! Operator for Matrix4x4 /= float; |
| 420 |
inline_ Matrix4x4& operator/=(float s) |
| 421 |
{ |
| 422 |
if(s) s = 1.0f / s; |
| 423 |
m[0][0]*=s; m[0][1]*=s; m[0][2]*=s; m[0][3]*=s; |
| 424 |
m[1][0]*=s; m[1][1]*=s; m[1][2]*=s; m[1][3]*=s; |
| 425 |
m[2][0]*=s; m[2][1]*=s; m[2][2]*=s; m[2][3]*=s; |
| 426 |
m[3][0]*=s; m[3][1]*=s; m[3][2]*=s; m[3][3]*=s; |
| 427 |
return *this; |
| 428 |
} |
| 429 |
|
| 430 |
inline_ const HPoint& operator[](int row) const { return *(const HPoint*)&m[row][0]; } |
| 431 |
inline_ HPoint& operator[](int row) { return *(HPoint*)&m[row][0]; } |
| 432 |
|
| 433 |
public: |
| 434 |
|
| 435 |
float m[4][4]; |
| 436 |
}; |
| 437 |
|
| 438 |
//! Quickly rotates & translates a vector, using the 4x3 part of a 4x4 matrix |
| 439 |
inline_ void TransformPoint4x3(Point& dest, const Point& source, const Matrix4x4& rot) |
| 440 |
{ |
| 441 |
dest.x = rot.m[3][0] + source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0]; |
| 442 |
dest.y = rot.m[3][1] + source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1]; |
| 443 |
dest.z = rot.m[3][2] + source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2]; |
| 444 |
} |
| 445 |
|
| 446 |
//! Quickly rotates a vector, using the 3x3 part of a 4x4 matrix |
| 447 |
inline_ void TransformPoint3x3(Point& dest, const Point& source, const Matrix4x4& rot) |
| 448 |
{ |
| 449 |
dest.x = source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0]; |
| 450 |
dest.y = source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1]; |
| 451 |
dest.z = source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2]; |
| 452 |
} |
| 453 |
|
| 454 |
ICEMATHS_API void InvertPRMatrix(Matrix4x4& dest, const Matrix4x4& src); |
| 455 |
|
| 456 |
#endif // __ICEMATRIX4X4_H__ |
| 457 |
|