| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
#ifndef _VJ_MATRIX_ |
|---|
| 35 |
#define _VJ_MATRIX_ |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
#include <vjConfig.h> |
|---|
| 39 |
#include <math.h> |
|---|
| 40 |
#include <string.h> |
|---|
| 41 |
|
|---|
| 42 |
class vjQuat; |
|---|
| 43 |
class vjVec3; |
|---|
| 44 |
class vjCoord; |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
class vjMatrix |
|---|
| 72 |
{ |
|---|
| 73 |
public: |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
float mat[4][4]; |
|---|
| 77 |
|
|---|
| 78 |
public: |
|---|
| 79 |
|
|---|
| 80 |
vjMatrix() : mat() { makeIdent(); }; |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
vjMatrix(float a00, float a01, float a02, float a03, |
|---|
| 84 |
float a10, float a11, float a12, float a13, |
|---|
| 85 |
float a20, float a21, float a22, float a23, |
|---|
| 86 |
float a30, float a31, float a32, float a33) |
|---|
| 87 |
{ |
|---|
| 88 |
mat[0][0] = a00; mat[1][0] = a01; mat[2][0] = a02; mat[3][0] = a03; |
|---|
| 89 |
mat[0][1] = a10; mat[1][1] = a11; mat[2][1] = a12; mat[3][1] = a13; |
|---|
| 90 |
mat[0][2] = a20; mat[1][2] = a21; mat[2][2] = a22; mat[3][2] = a23; |
|---|
| 91 |
mat[0][3] = a30; mat[1][3] = a31; mat[2][3] = a32; mat[3][3] = a33; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
vjMatrix(vjCoord coord); |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
vjMatrix( const vjMatrix& matrix ) |
|---|
| 101 |
{ |
|---|
| 102 |
this->copy( matrix ); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
public: |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
void set(float *m) { |
|---|
| 110 |
memcpy(mat, m, sizeof(float) * 16); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
void set(float a00, float a01, float a02, float a03, |
|---|
| 118 |
float a10, float a11, float a12, float a13, |
|---|
| 119 |
float a20, float a21, float a22, float a23, |
|---|
| 120 |
float a30, float a31, float a32, float a33) |
|---|
| 121 |
{ |
|---|
| 122 |
mat[0][0] = a00; mat[1][0] = a01; mat[2][0] = a02; mat[3][0] = a03; |
|---|
| 123 |
mat[0][1] = a10; mat[1][1] = a11; mat[2][1] = a12; mat[3][1] = a13; |
|---|
| 124 |
mat[0][2] = a20; mat[1][2] = a21; mat[2][2] = a22; mat[3][2] = a23; |
|---|
| 125 |
mat[0][3] = a30; mat[1][3] = a31; mat[2][3] = a32; mat[3][3] = a33; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
public: |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
void makeIdent() |
|---|
| 132 |
{ zero(); mat[0][0] = mat[1][1] = mat[2][2] = mat[3][3] = 1.0f;} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
void zero() |
|---|
| 137 |
{ |
|---|
| 138 |
mat[0][0]=0.0f; mat[1][0]=0.0f; mat[2][0]=0.0f; mat[3][0]=0.0f; |
|---|
| 139 |
mat[0][1]=0.0f; mat[1][1]=0.0f; mat[2][1]=0.0f; mat[3][1]=0.0f; |
|---|
| 140 |
mat[0][2]=0.0f; mat[1][2]=0.0f; mat[2][2]=0.0f; mat[3][2]=0.0f; |
|---|
| 141 |
mat[0][3]=0.0f; mat[1][3]=0.0f; mat[2][3]=0.0f; mat[3][3]=0.0f; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
void makeXYZEuler(float xRot, float yRot, float zRot); |
|---|
| 151 |
void getXYZEuler(float& xRot, float& yRot, float& zRot) const; |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
void makeZYXEuler(float zRot, float yRot, float xRot); |
|---|
| 159 |
void getZYXEuler(float& zRot, float& yRot, float& xRot) const; |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
void makeZXYEuler( float zRot, float xRot, float yRot ); |
|---|
| 163 |
void getZXYEuler( float& zRot, float& xRot, float& yRot ) const; |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
float getYRot() const; |
|---|
| 168 |
float getYaw() const |
|---|
| 169 |
{ return getYRot(); } |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
float getXRot() const; |
|---|
| 174 |
float getPitch() const |
|---|
| 175 |
{return getXRot();} |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
float getZRot() const; |
|---|
| 180 |
float getRoll() const |
|---|
| 181 |
{ return getZRot();} |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
void constrainRotAxis( const bool& allowXRot, const bool& allowYRot, const bool& allowZRot, vjMatrix& result ); |
|---|
| 191 |
void _kevn_constrainRotAxis( const bool& pitch, const bool& yaw, const bool& roll, vjMatrix& result ); |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
void makeDirCos( vjVec3 secXAxis, vjVec3 secYAxis, vjVec3 secZAxis ); |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
void makeQuaternion( const float* const q ); |
|---|
| 212 |
void makeQuaternion( const vjQuat& q ); |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
void makeRot(float _degrees, vjVec3 _axis); |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
void makeTrans(float _x, float _y, float _z); |
|---|
| 223 |
void makeTrans(const vjVec3& trans); |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
void setTrans(float _x, float _y, float _z); |
|---|
| 228 |
void setTrans(const vjVec3& trans); |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
void getTrans(float& _x, float& _y, float& _z) const; |
|---|
| 233 |
vjVec3 getTrans() const; |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
void makeScale(float _x, float _y, float _z); |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
void copy( const vjMatrix& _m ) |
|---|
| 246 |
{ |
|---|
| 247 |
((mat)[0][0] = (_m)[0][0]); ((mat)[0][1] = (_m)[0][1]); ((mat)[0][2] = (_m)[0][2]); ((mat)[0][3] = (_m)[0][3]); |
|---|
| 248 |
((mat)[1][0] = (_m)[1][0]); ((mat)[1][1] = (_m)[1][1]); ((mat)[1][2] = (_m)[1][2]); ((mat)[1][3] = (_m)[1][3]); |
|---|
| 249 |
((mat)[2][0] = (_m)[2][0]); ((mat)[2][1] = (_m)[2][1]); ((mat)[2][2] = (_m)[2][2]); ((mat)[2][3] = (_m)[2][3]); |
|---|
| 250 |
((mat)[3][0] = (_m)[3][0]); ((mat)[3][1] = (_m)[3][1]); ((mat)[3][2] = (_m)[3][2]); ((mat)[3][3] = (_m)[3][3]); |
|---|
| 251 |
} |
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
bool equal(const vjMatrix& _m) const { |
|---|
| 257 |
return (((mat)[0][0] == (_m)[0][0]) && |
|---|
| 258 |
((mat)[0][1] == (_m)[0][1]) && |
|---|
| 259 |
((mat)[0][2] == (_m)[0][2]) && |
|---|
| 260 |
((mat)[0][3] == (_m)[0][3]) && |
|---|
| 261 |
((mat)[1][0] == (_m)[1][0]) && |
|---|
| 262 |
((mat)[1][1] == (_m)[1][1]) && |
|---|
| 263 |
((mat)[1][2] == (_m)[1][2]) && |
|---|
| 264 |
((mat)[1][3] == (_m)[1][3]) && |
|---|
| 265 |
((mat)[2][0] == (_m)[2][0]) && |
|---|
| 266 |
((mat)[2][1] == (_m)[2][1]) && |
|---|
| 267 |
((mat)[2][2] == (_m)[2][2]) && |
|---|
| 268 |
((mat)[2][3] == (_m)[2][3]) && |
|---|
| 269 |
((mat)[3][0] == (_m)[3][0]) && |
|---|
| 270 |
((mat)[3][1] == (_m)[3][1]) && |
|---|
| 271 |
((mat)[3][2] == (_m)[3][2]) && |
|---|
| 272 |
((mat)[3][3] == (_m)[3][3])); |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
void zeroClamp() |
|---|
| 278 |
{ |
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
for(int i=0;i<4;i++) |
|---|
| 282 |
for(int j=0;j<4;j++) |
|---|
| 283 |
mat[i][j] = VJ_ZERO_CLAMP(mat[i][j]); |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
public: |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
void transpose(vjMatrix& _m) |
|---|
| 290 |
{ |
|---|
| 291 |
for (int i=0; i<4; i++) |
|---|
| 292 |
for (int j=0; j<4; j++) |
|---|
| 293 |
mat[i][j] = _m.mat[j][i]; |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
void mult(const vjMatrix& _m1, const vjMatrix &_m2) |
|---|
| 299 |
{ |
|---|
| 300 |
*this = _m1; |
|---|
| 301 |
postMult(_m2); |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
void add(const vjMatrix& _m1, const vjMatrix &_m2) |
|---|
| 307 |
{ |
|---|
| 308 |
for (int n=0;n<4;n++) |
|---|
| 309 |
for (int m=0;m<4;m++) |
|---|
| 310 |
mat[n][m] = (_m1.mat[n][m] + _m2.mat[n][m]); |
|---|
| 311 |
} |
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
void sub(const vjMatrix& _m1, const vjMatrix &_m2) |
|---|
| 316 |
{ |
|---|
| 317 |
for (int n=0;n<4;n++) |
|---|
| 318 |
for (int m=0;m<4;m++) |
|---|
| 319 |
mat[n][m] = (_m1.mat[n][m] - _m2.mat[n][m]); |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
void scale(float _s, const vjMatrix &_m) |
|---|
| 326 |
{ |
|---|
| 327 |
for (int n=0;n<4;n++) |
|---|
| 328 |
for (int m=0;m<4;m++) |
|---|
| 329 |
mat[n][m] = (_m.mat[n][m] * _s); |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
void postMult(const vjMatrix& _m); |
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
void preMult(const vjMatrix& _m); |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
int invert(vjMatrix& _m); |
|---|
| 344 |
|
|---|
| 345 |
public: |
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
void preTrans(float _x, float _y, float _z, vjMatrix& _m); |
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
void preTrans(vjVec3& _trans, vjMatrix& _m); |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
void postTrans(const vjMatrix& _m, float _x, float _y, float _z); |
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
void postTrans(const vjMatrix& _m, vjVec3& _trans); |
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
void preRot(const float& _degrees, const vjVec3& axis, vjMatrix& _m); |
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
void postRot( const vjMatrix& _m, const float& _degrees, const vjVec3& axis ); |
|---|
| 371 |
|
|---|
| 372 |
void preXYZEuler( float x, float y, float z, vjMatrix& _m ); |
|---|
| 373 |
void postXYZEuler( vjMatrix& _m, float x, float y, float z ); |
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
void preScale( float _xs, float _ys, float _zs, vjMatrix& _m ); |
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
void postScale( const vjMatrix& _m, float _xs, float _ys, float _zs ); |
|---|
| 380 |
|
|---|
| 381 |
public: |
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
inline float* getFloatPtr() { return (float*)mat;} |
|---|
| 385 |
inline const float* getFloatPtr() const { return (float*)mat;} |
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
inline float* operator[]( int i ) { return &mat[i][0];} |
|---|
| 389 |
inline const float* operator[]( int i ) const { return &mat[i][0];} |
|---|
| 390 |
inline float& operator()( const int& row, const int& column ) { return mat[column][row];} |
|---|
| 391 |
inline const float& operator()( const int& row, const int& column ) const { return mat[column][row];} |
|---|
| 392 |
|
|---|
| 393 |
int operator==( const vjMatrix& _m ) const |
|---|
| 394 |
{ |
|---|
| 395 |
return this->equal(_m); |
|---|
| 396 |
} |
|---|
| 397 |
|
|---|
| 398 |
int operator!=( const vjMatrix& _m ) const |
|---|
| 399 |
{ |
|---|
| 400 |
return !this->equal(_m); |
|---|
| 401 |
} |
|---|
| 402 |
|
|---|
| 403 |
public: |
|---|
| 404 |
|
|---|
| 405 |
vjMatrix operator*( const vjMatrix& _m ) const |
|---|
| 406 |
{ |
|---|
| 407 |
vjMatrix dst; dst.mult(*this, _m); return dst; |
|---|
| 408 |
} |
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
vjMatrix operator+( const vjMatrix& _m ) const |
|---|
| 412 |
{ |
|---|
| 413 |
vjMatrix dst; dst.add(*this, _m); return dst; |
|---|
| 414 |
} |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
vjMatrix operator-(const vjMatrix& _m) const |
|---|
| 418 |
{ |
|---|
| 419 |
vjMatrix dst; |
|---|
| 420 |
dst.sub(*this, _m); |
|---|
| 421 |
return dst; |
|---|
| 422 |
} |
|---|
| 423 |
|
|---|
| 424 |
friend inline vjMatrix operator*(float _s, const vjMatrix&); |
|---|
| 425 |
friend inline vjMatrix operator*(const vjMatrix& _v, float _s); |
|---|
| 426 |
friend inline vjMatrix operator/(const vjMatrix& _v, float _s); |
|---|
| 427 |
friend std::ostream& operator<<(std::ostream& out, vjMatrix& _mat); |
|---|
| 428 |
|
|---|
| 429 |
public: |
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
inline vjMatrix& operator=( const vjMatrix& _m ) |
|---|
| 433 |
{ |
|---|
| 434 |
this->copy( _m ); |
|---|
| 435 |
return *this; |
|---|
| 436 |
} |
|---|
| 437 |
|
|---|
| 438 |
vjMatrix& operator*=( const vjMatrix& _m ) |
|---|
| 439 |
{ |
|---|
| 440 |
this->postMult(_m); return *this; |
|---|
| 441 |
} |
|---|
| 442 |
|
|---|
| 443 |
vjMatrix& operator*=(float _s) |
|---|
| 444 |
{ |
|---|
| 445 |
for (int i=0; i<4; i++) |
|---|
| 446 |
for (int j=0; j<4; j++) |
|---|
| 447 |
mat[i][j] = mat[i][j] * _s; |
|---|
| 448 |
|
|---|
| 449 |
return *this; |
|---|
| 450 |
} |
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
}; |
|---|
| 456 |
|
|---|
| 457 |
#endif |
|---|