| 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 |
#include <vjConfig.h> |
|---|
| 35 |
|
|---|
| 36 |
#include <Kernel/vjAssert.h> |
|---|
| 37 |
#include <Math/vjMatrix.h> |
|---|
| 38 |
#include <Math/vjVec3.h> |
|---|
| 39 |
#include <Math/vjCoord.h> |
|---|
| 40 |
#include <Math/vjQuat.h> |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
static inline void limitAngle(float& angle) |
|---|
| 44 |
{ |
|---|
| 45 |
if(angle > 1.0f) |
|---|
| 46 |
angle = 1.0f; |
|---|
| 47 |
else if(angle < -1.0f) |
|---|
| 48 |
angle = -1.0f; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
static inline void zeroClampAngle(float& angle) |
|---|
| 53 |
{ |
|---|
| 54 |
if(fabs(angle) < 1e-6) |
|---|
| 55 |
angle = 0.0f; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
vjMatrix::vjMatrix(vjCoord coord) |
|---|
| 59 |
{ |
|---|
| 60 |
makeXYZEuler(coord.orient[0], coord.orient[1], coord.orient[2]); |
|---|
| 61 |
setTrans(coord.pos[0], coord.pos[1], coord.pos[2]); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
void vjMatrix::makeXYZEuler(float xRot, float yRot, float zRot) |
|---|
| 65 |
{ |
|---|
| 66 |
float sx = sin(VJ_DEG2RAD(xRot)); float cx = cos(VJ_DEG2RAD(xRot)); |
|---|
| 67 |
float sy = sin(VJ_DEG2RAD(yRot)); float cy = cos(VJ_DEG2RAD(yRot)); |
|---|
| 68 |
float sz = sin(VJ_DEG2RAD(zRot)); float cz = cos(VJ_DEG2RAD(zRot)); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
mat[0][0] = cy*cz; mat[1][0] = -cy*sz; mat[2][0] = sy; mat[3][0] = 0.0f; |
|---|
| 73 |
mat[0][1] = sx*sy*cz + cx*sz; mat[1][1] = -sx*sy*sz + cx*cz; mat[2][1] = -sx*cy; mat[3][1] = 0.0f; |
|---|
| 74 |
mat[0][2] = -cx*sy*cz + sx*sz; mat[1][2] = cx*sy*sz + sx*cz; mat[2][2] = cx*cy; mat[3][2] = 0.0f; |
|---|
| 75 |
mat[0][3] = 0.0f; mat[1][3] = 0.0f; mat[2][3] = 0.0f; mat[3][3] = 1.0f; |
|---|
| 76 |
|
|---|
| 77 |
zeroClamp(); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
void vjMatrix::getXYZEuler(float& xRot, float& yRot, float& zRot) const |
|---|
| 81 |
{ |
|---|
| 82 |
float cz; |
|---|
| 83 |
|
|---|
| 84 |
zRot = atan2f(-mat[1][0], mat[0][0]); |
|---|
| 85 |
xRot = atan2f(-mat[2][1], mat[2][2]); |
|---|
| 86 |
cz = cos(zRot); |
|---|
| 87 |
yRot = atan2f(mat[2][0], mat[0][0]/cz); |
|---|
| 88 |
|
|---|
| 89 |
xRot = VJ_RAD2DEG(xRot); |
|---|
| 90 |
yRot = VJ_RAD2DEG(yRot); |
|---|
| 91 |
zRot = VJ_RAD2DEG(zRot); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
void vjMatrix::makeZYXEuler(float zRot, float yRot, float xRot) |
|---|
| 97 |
{ |
|---|
| 98 |
float sx = sin(VJ_DEG2RAD(xRot)); float cx = cos(VJ_DEG2RAD(xRot)); |
|---|
| 99 |
float sy = sin(VJ_DEG2RAD(yRot)); float cy = cos(VJ_DEG2RAD(yRot)); |
|---|
| 100 |
float sz = sin(VJ_DEG2RAD(zRot)); float cz = cos(VJ_DEG2RAD(zRot)); |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
mat[0][0] = cy*cz; mat[1][0] = -cx*sz + sx*sy*cz; mat[2][0] = sx*sz + cx*sy*cz; mat[3][0] = 0.0f; |
|---|
| 105 |
mat[0][1] = cy*sz; mat[1][1] = cx*cz + sx*sy*sz; mat[2][1] = -sx*cz + cx*sy*sz; mat[3][1] = 0.0f; |
|---|
| 106 |
mat[0][2] = -sy; mat[1][2] = sx*cy; mat[2][2] = cx*cy; mat[3][2] = 0.0f; |
|---|
| 107 |
mat[0][3] = 0.0f; mat[1][3] = 0.0f; mat[2][3] = 0.0f; mat[3][3] = 1.0f; |
|---|
| 108 |
|
|---|
| 109 |
zeroClamp(); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
void vjMatrix::getZYXEuler(float& zRot, float& yRot, float& xRot) const |
|---|
| 113 |
{ |
|---|
| 114 |
float sx; |
|---|
| 115 |
|
|---|
| 116 |
zRot = atan2f(mat[0][1], mat[0][0]); |
|---|
| 117 |
xRot = atan2f(mat[1][2], mat[2][2]); |
|---|
| 118 |
sx = sinf(xRot); |
|---|
| 119 |
yRot = atan2f(-mat[0][2],(mat[1][2]/sx) ); |
|---|
| 120 |
|
|---|
| 121 |
xRot = VJ_RAD2DEG(xRot); |
|---|
| 122 |
yRot = VJ_RAD2DEG(yRot); |
|---|
| 123 |
zRot = VJ_RAD2DEG(zRot); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
void vjMatrix::makeZXYEuler(float zRot, float xRot, float yRot) |
|---|
| 129 |
{ |
|---|
| 130 |
float sx = sin(VJ_DEG2RAD(xRot)); float cx = cos(VJ_DEG2RAD(xRot)); |
|---|
| 131 |
float sy = sin(VJ_DEG2RAD(yRot)); float cy = cos(VJ_DEG2RAD(yRot)); |
|---|
| 132 |
float sz = sin(VJ_DEG2RAD(zRot)); float cz = cos(VJ_DEG2RAD(zRot)); |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
mat[0][0] = cy*cz - sx*sy*sz; mat[1][0] = -cx*sz; mat[2][0] = sy*cz + sx*cy*sz; mat[3][0] = 0.0f; |
|---|
| 137 |
mat[0][1] = cy*sz + sx*sy*cz; mat[1][1] = cx*cz; mat[2][1] = sy*sz - sx*cy*cz; mat[3][1] = 0.0f; |
|---|
| 138 |
mat[0][2] = -cx*sy; mat[1][2] = sx; mat[2][2] = cx*cy; mat[3][2] = 0.0f; |
|---|
| 139 |
mat[0][3] = 0.0f; mat[1][3] = 0.0f; mat[2][3] = 0.0f; mat[3][3] = 1.0f; |
|---|
| 140 |
|
|---|
| 141 |
zeroClamp(); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
float vjMatrix::getYRot() const |
|---|
| 148 |
{ |
|---|
| 149 |
const vjVec3 forwardPoint( 0, 0, -1 ); |
|---|
| 150 |
const vjVec3 originPoint( 0, 0, 0 ); |
|---|
| 151 |
vjVec3 endPoint, startPoint; |
|---|
| 152 |
endPoint.xformFull( *this, forwardPoint ); |
|---|
| 153 |
startPoint.xformFull( *this, originPoint ); |
|---|
| 154 |
vjVec3 directionVector = endPoint - startPoint; |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
directionVector[VJ_Y] = 0.0f; |
|---|
| 158 |
directionVector.normalize(); |
|---|
| 159 |
float y_rot = VJ_RAD2DEG( acosf(directionVector.dot( forwardPoint )) ); |
|---|
| 160 |
vjVec3 whichSide = forwardPoint.cross(directionVector); |
|---|
| 161 |
if (whichSide[VJ_Y] < 0.0f) |
|---|
| 162 |
y_rot = -y_rot; |
|---|
| 163 |
|
|---|
| 164 |
return y_rot; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
float vjMatrix::getXRot() const |
|---|
| 171 |
{ |
|---|
| 172 |
const vjVec3 forwardPoint( 0, 0, -1 ); |
|---|
| 173 |
const vjVec3 originPoint( 0, 0, 0 ); |
|---|
| 174 |
vjVec3 endPoint, startPoint; |
|---|
| 175 |
endPoint.xformFull( *this, forwardPoint ); |
|---|
| 176 |
startPoint.xformFull( *this, originPoint ); |
|---|
| 177 |
vjVec3 directionVector = endPoint - startPoint; |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
directionVector[VJ_X] = 0.0f; |
|---|
| 181 |
directionVector.normalize(); |
|---|
| 182 |
float x_rot = VJ_RAD2DEG( acosf(directionVector.dot( forwardPoint )) ); |
|---|
| 183 |
vjVec3 whichSide = forwardPoint.cross(directionVector); |
|---|
| 184 |
if (whichSide[VJ_X] < 0.0f) |
|---|
| 185 |
x_rot = -x_rot; |
|---|
| 186 |
|
|---|
| 187 |
return x_rot; |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
float vjMatrix::getZRot() const |
|---|
| 194 |
{ |
|---|
| 195 |
const vjVec3 up_point( 0, 1, 0 ); |
|---|
| 196 |
const vjVec3 origin_point( 0, 0, 0 ); |
|---|
| 197 |
vjVec3 end_point, start_point; |
|---|
| 198 |
end_point.xformFull( *this, up_point ); |
|---|
| 199 |
start_point.xformFull( *this, origin_point ); |
|---|
| 200 |
vjVec3 direction_vector = end_point - start_point; |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
direction_vector[VJ_Z] = 0.0f; |
|---|
| 204 |
direction_vector.normalize(); |
|---|
| 205 |
float z_rot = VJ_RAD2DEG( acosf(direction_vector.dot( up_point )) ); |
|---|
| 206 |
vjVec3 which_side = up_point.cross(direction_vector); |
|---|
| 207 |
if (which_side[VJ_Z] < 0.0f) |
|---|
| 208 |
z_rot = -z_rot; |
|---|
| 209 |
|
|---|
| 210 |
return z_rot; |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
void vjMatrix::_kevn_constrainRotAxis( const bool& usePitch, const bool& useYaw, const bool& useRoll, vjMatrix& result ) |
|---|
| 216 |
{ |
|---|
| 217 |
|
|---|
| 218 |
vjMatrix constrainedMatrix; |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
float xRot, yRot, zRot; |
|---|
| 222 |
vjVec3 xAxis( 1, 0, 0 ), yAxis( 0, 1, 0 ), zAxis( 0, 0, 1 ); |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
vjVec3 trans; |
|---|
| 226 |
this->getTrans( trans[0],trans[1],trans[2] ); |
|---|
| 227 |
constrainedMatrix.makeTrans( trans[0],trans[1],trans[2] ); |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
if (usePitch) |
|---|
| 231 |
{ |
|---|
| 232 |
vjMatrix mx; |
|---|
| 233 |
xRot = this->getPitch(); |
|---|
| 234 |
mx.makeRot( xRot, xAxis ); |
|---|
| 235 |
constrainedMatrix.preMult( mx ); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
if (useYaw) |
|---|
| 240 |
{ |
|---|
| 241 |
vjMatrix my; |
|---|
| 242 |
yRot = this->getYaw(); |
|---|
| 243 |
my.makeRot( yRot, yAxis ); |
|---|
| 244 |
constrainedMatrix.postMult( my ); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
if (useRoll) |
|---|
| 249 |
{ |
|---|
| 250 |
vjMatrix mz; |
|---|
| 251 |
zRot = this->getRoll(); |
|---|
| 252 |
mz.makeRot( zRot, zAxis ); |
|---|
| 253 |
constrainedMatrix.preMult( mz ); |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
result.copy( constrainedMatrix ); |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
void vjMatrix::constrainRotAxis( const bool& allowXRot, const bool& allowYRot, const bool& allowZRot, vjMatrix& result ) |
|---|
| 262 |
{ |
|---|
| 263 |
|
|---|
| 264 |
vjMatrix constrainedMatrix; |
|---|
| 265 |
constrainedMatrix = *this; |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
float xRot, yRot, zRot; |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
this->getXYZEuler(xRot,yRot,zRot); |
|---|
| 283 |
constrainedMatrix.makeXYZEuler(((allowXRot)?xRot:0.0f),((allowYRot)?yRot:0.0f),((allowZRot)?zRot:0.0f)); |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
if (!allowXRot) |
|---|
| 289 |
{ |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
if (!allowYRot) |
|---|
| 308 |
{ |
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
if (!allowZRot) |
|---|
| 327 |
{ |
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
result = constrainedMatrix; |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
void vjMatrix::getZXYEuler(float& zRot, float& xRot, float& yRot) const |
|---|
| 348 |
{ |
|---|
| 349 |
|
|---|
| 350 |
float x_angle; |
|---|
| 351 |
float y_angle; |
|---|
| 352 |
float z_angle; |
|---|
| 353 |
float cos_y, sin_y; |
|---|
| 354 |
float cos_x, sin_x; |
|---|
| 355 |
float cos_z, sin_z; |
|---|
| 356 |
|
|---|
| 357 |
sin_x = mat[1][2]; |
|---|
| 358 |
x_angle = asinf(sin_x); |
|---|
| 359 |
cos_x = cos(x_angle); |
|---|
| 360 |
zeroClampAngle(cos_x); |
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
if(cos_x != 0.0f) |
|---|
| 364 |
{ |
|---|
| 365 |
|
|---|
| 366 |
cos_y = mat[2][2]/cos_x; |
|---|
| 367 |
sin_y = -mat[0][2]/cos_x; |
|---|
| 368 |
y_angle = atan2(cos_y, sin_y); |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
cos_z = mat[1][1]/cos_x; |
|---|
| 372 |
sin_z = -mat[1][0]/cos_x; |
|---|
| 373 |
z_angle = atan2(cos_z, sin_z); |
|---|
| 374 |
} |
|---|
| 375 |
else |
|---|
| 376 |
{ |
|---|
| 377 |
|
|---|
| 378 |
z_angle = 0; |
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
cos_y = mat[0][0]; |
|---|
| 382 |
sin_y = mat[0][1]; |
|---|
| 383 |
y_angle = atan2(cos_y, sin_y); |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
xRot = VJ_RAD2DEG(x_angle); |
|---|
| 387 |
yRot = VJ_RAD2DEG(y_angle); |
|---|
| 388 |
zRot = VJ_RAD2DEG(z_angle); |
|---|
| 389 |
} |
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
void vjMatrix::makeDirCos(vjVec3 secXAxis, vjVec3 secYAxis, vjVec3 secZAxis) |
|---|
| 394 |
{ |
|---|
| 395 |
vjASSERT(secXAxis.isNormalized()); |
|---|
| 396 |
vjASSERT(secYAxis.isNormalized()); |
|---|
| 397 |
vjASSERT(secZAxis.isNormalized()); |
|---|
| 398 |
|
|---|
| 399 |
float Xa, Xb, Xy; |
|---|
| 400 |
float Ya, Yb, Yy; |
|---|
| 401 |
float Za, Zb, Zy; |
|---|
| 402 |
|
|---|
| 403 |
vjVec3 xAxis(1,0,0), yAxis(0,1,0), zAxis(0,0,1); |
|---|
| 404 |
|
|---|
| 405 |
Xa = secXAxis.dot(xAxis); Xb = secXAxis.dot(yAxis); Xy = secXAxis.dot(zAxis); |
|---|
| 406 |
Ya = secYAxis.dot(xAxis); Yb = secYAxis.dot(yAxis); Yy = secYAxis.dot(zAxis); |
|---|
| 407 |
Za = secZAxis.dot(xAxis); Zb = secZAxis.dot(yAxis); Zy = secZAxis.dot(zAxis); |
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
set(Xa, Xb, Xy, 0, |
|---|
| 411 |
Ya, Yb, Yy, 0, |
|---|
| 412 |
Za, Zb, Zy, 0, |
|---|
| 413 |
0, 0, 0, 1); |
|---|
| 414 |
} |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
void vjMatrix::makeQuaternion( const float* const q ) |
|---|
| 418 |
{ |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
float wx, wy, wz, xx, yy, yz, xy, xz, zz, xs, ys, zs; |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
xs = q[VJ_X] + q[VJ_X]; ys = q[VJ_Y] + q[VJ_Y]; zs = q[VJ_Z] + q[VJ_Z]; |
|---|
| 455 |
xx = q[VJ_X] * xs; xy = q[VJ_X] * ys; xz = q[VJ_X] * zs; |
|---|
| 456 |
yy = q[VJ_Y] * ys; yz = q[VJ_Y] * zs; zz = q[VJ_Z] * zs; |
|---|
| 457 |
wx = q[VJ_W] * xs; wy = q[VJ_W] * ys; wz = q[VJ_W] * zs; |
|---|
| 458 |
|
|---|
| 459 |
mat[0][0] = 1.0 - (yy+zz); |
|---|
| 460 |
mat[0][1] = xy+wz; |
|---|
| 461 |
mat[0][2] = xz-wy; |
|---|
| 462 |
mat[0][3] = 0.0; |
|---|
| 463 |
|
|---|
| 464 |
mat[1][0] = xy-wz; |
|---|
| 465 |
mat[1][1] = 1.0 - (xx+zz); |
|---|
| 466 |
mat[1][2] = yz+wx; |
|---|
| 467 |
mat[1][3] = 0.0; |
|---|
| 468 |
|
|---|
| 469 |
mat[2][0] = xz+wy; |
|---|
| 470 |
mat[2][1] = yz-wx; |
|---|
| 471 |
mat[2][2] = 1.0 - (xx+yy); |
|---|
| 472 |
mat[2][3] = 0.0; |
|---|
| 473 |
|
|---|
| 474 |
mat[3][0] = 0.0; |
|---|
| 475 |
mat[3][1] = 0.0; |
|---|
| 476 |
mat[3][2] = 0.0; |
|---|
| 477 |
mat[3][3] = 1.0; |
|---|
| 478 |
} |
|---|
| 479 |
|
|---|
| 480 |
void vjMatrix::makeQuaternion( const vjQuat& q ) |
|---|
| 481 |
{ |
|---|
| 482 |
makeQuaternion(q.vec); |
|---|
| 483 |
} |
|---|
| 484 |
|
|---|
| 485 |
void vjMatrix::makeRot(float _degrees, vjVec3 _axis) |
|---|
| 486 |
{ |
|---|
| 487 |
_axis.normalize(); |
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
float s = sin(VJ_DEG2RAD(_degrees)); |
|---|
| 491 |
float c = cos(VJ_DEG2RAD(_degrees)); |
|---|
| 492 |
float t = 1-c; |
|---|
| 493 |
float x = _axis[0]; |
|---|
| 494 |
float y = _axis[1]; |
|---|
| 495 |
float z = _axis[2]; |
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
mat[0][0] = (t*x*x)+c; mat[1][0] = (t*x*y)-(s*z); mat[2][0] = (t*x*z)+(s*y); mat[3][0] = 0.0f; |
|---|
| 506 |
mat[0][1] = (t*x*y)+(s*z); mat[1][1] = (t*y*y)+c; mat[2][1] = (t*y*z)-(s*x); mat[3][1] = 0.0f; |
|---|
| 507 |
mat[0][2] = (t*x*z)-(s*y); mat[1][2] = (t*y*z)+(s*x); mat[2][2] = (t*z*z)+c; mat[3][2] = 0.0f; |
|---|
| 508 |
mat[0][3] = 0.0f; mat[1][3] = 0.0f; mat[2][3] = 0.0f; mat[3][3] = 1.0f; |
|---|
| 509 |
|
|---|
| 510 |
zeroClamp(); |
|---|
| 511 |
} |
|---|
| 512 |
|
|---|
| 513 |
void vjMatrix::makeTrans(float _x, float _y, float _z) |
|---|
| 514 |
{ |
|---|
| 515 |
makeIdent(); |
|---|
| 516 |
mat[3][0] = _x; |
|---|
| 517 |
mat[3][1] = _y; |
|---|
| 518 |
mat[3][2] = _z; |
|---|
| 519 |
} |
|---|
| 520 |
|
|---|
| 521 |
void vjMatrix::makeTrans(const vjVec3& trans) |
|---|
| 522 |
{ makeTrans(trans[0],trans[1],trans[2]); } |
|---|
| 523 |
|
|---|
| 524 |
void vjMatrix::setTrans(float _x, float _y, float _z) |
|---|
| 525 |
{ |
|---|
| 526 |
mat[3][0] = _x; |
|---|
| 527 |
mat[3][1] = _y; |
|---|
| 528 |
mat[3][2] = _z; |
|---|
| 529 |
} |
|---|
| 530 |
|
|---|
| 531 |
void vjMatrix::setTrans(const vjVec3& trans) |
|---|
| 532 |
{ setTrans(trans[0],trans[1],trans[2]); } |
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
void vjMatrix::getTrans(float& _x, float& _y, float& _z) const |
|---|
| 536 |
{ |
|---|
| 537 |
_x = mat[3][0]; |
|---|
| 538 |
_y = mat[3][1]; |
|---|
| 539 |
_z = mat[3][2]; |
|---|
| 540 |
} |
|---|
| 541 |
|
|---|
| 542 |
vjVec3 vjMatrix::getTrans() const |
|---|
| 543 |
{ |
|---|
| 544 |
vjVec3 trans;; |
|---|
| 545 |
getTrans(trans[0],trans[1],trans[2]); |
|---|
| 546 |
return trans; |
|---|
| 547 |
} |
|---|
| 548 |
|
|---|
| 549 |
void vjMatrix::makeScale(float _x, float _y, float _z) |
|---|
| 550 |
{ |
|---|
| 551 |
makeIdent(); |
|---|
| 552 |
mat[0][0] = _x; |
|---|
| 553 |
mat[1][1] = _y; |
|---|
| 554 |
mat[2][2] = _z; |
|---|
| 555 |
} |
|---|
| 556 |
|
|---|
| 557 |
void vjMatrix::preTrans(float _x, float _y, float _z, vjMatrix& _m) |
|---|
| 558 |
{ |
|---|
| 559 |
vjMatrix transMat; |
|---|
| 560 |
transMat.makeTrans(_x, _y, _z); |
|---|
| 561 |
transMat.postMult(_m); |
|---|
| 562 |
*this = transMat; |
|---|
| 563 |
} |
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
void vjMatrix::preTrans(vjVec3& _trans, vjMatrix& _m) |
|---|
| 568 |
{ preTrans(_trans.vec[0], _trans.vec[1], _trans.vec[2], _m); } |
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
void vjMatrix::postTrans(const vjMatrix& _m, float _x, float _y, float _z) |
|---|
| 573 |
{ |
|---|
| 574 |
vjMatrix transMat; |
|---|
| 575 |
transMat.makeTrans(_x, _y, _z); |
|---|
| 576 |
transMat.preMult(_m); |
|---|
| 577 |
*this = transMat; |
|---|
| 578 |
} |
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
void vjMatrix::postTrans(const vjMatrix& _m, vjVec3& _trans) |
|---|
| 582 |
{ postTrans(_m, _trans.vec[0], _trans.vec[1], _trans.vec[2]); } |
|---|
| 583 |
|
|---|
| 584 |
void vjMatrix::preRot(const float& _degrees, const vjVec3& axis, vjMatrix& _m) |
|---|
| 585 |
{ |
|---|
| 586 |
vjMatrix rotMat; |
|---|
| 587 |
rotMat.makeRot(_degrees, axis); |
|---|
| 588 |
rotMat.postMult(_m); |
|---|
| 589 |
*this = rotMat; |
|---|
| 590 |
} |
|---|
| 591 |
|
|---|
| 592 |
void vjMatrix::postRot(const vjMatrix& _m, const float& _degrees, const vjVec3& axis) |
|---|
| 593 |
{ |
|---|
| 594 |
vjMatrix rotMat; |
|---|
| 595 |
rotMat.makeRot(_degrees, axis); |
|---|
| 596 |
rotMat.preMult(_m); |
|---|
| 597 |
*this = rotMat; |
|---|
| 598 |
} |
|---|
| 599 |
|
|---|
| 600 |
void vjMatrix::preXYZEuler(float x, float y, float z, vjMatrix& _m) |
|---|
| 601 |
{ |
|---|
| 602 |
vjMatrix rotMat; |
|---|
| 603 |
rotMat.makeXYZEuler(x, y, z); |
|---|
| 604 |
rotMat.postMult(_m); |
|---|
| 605 |
*this = rotMat; |
|---|
| 606 |
} |
|---|
| 607 |
|
|---|
| 608 |
void vjMatrix::postXYZEuler(vjMatrix& _m, float x, float y, float z) |
|---|
| 609 |
{ |
|---|
| 610 |
vjMatrix rotMat; |
|---|
| 611 |
rotMat.makeXYZEuler(x, y, z); |
|---|
| 612 |
rotMat.preMult(_m); |
|---|
| 613 |
*this = rotMat; |
|---|
| 614 |
} |
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
void vjMatrix::preScale(float _xs, float _ys, float _zs, vjMatrix& _m) |
|---|
| 618 |
{ |
|---|
| 619 |
vjMatrix scaleMat; |
|---|
| 620 |
scaleMat.makeScale(_xs, _ys, _zs); |
|---|
| 621 |
scaleMat.postMult(_m); |
|---|
| 622 |
*this = scaleMat; |
|---|
| 623 |
} |
|---|
| 624 |
|
|---|
| 625 |
void vjMatrix::postScale(const vjMatrix& _m, float _xs, float _ys, float _zs) |
|---|
| 626 |
{ |
|---|
| 627 |
vjMatrix scaleMat; |
|---|
| 628 |
scaleMat.makeScale(_xs, _ys, _zs); |
|---|
| 629 |
scaleMat.preMult(_m); |
|---|
| 630 |
*this = scaleMat; |
|---|
| 631 |
} |
|---|
| 632 |
|
|---|
| 633 |
inline vjMatrix operator *(float _s, const vjMatrix& _m) { |
|---|
| 634 |
vjMatrix dst; dst.scale(_s, _m); return dst; |
|---|
| 635 |
} |
|---|
| 636 |
|
|---|
| 637 |
inline vjMatrix operator *(const vjMatrix& _m, float _s) { |
|---|
| 638 |
vjMatrix dst; dst.scale(_s, _m); return dst; |
|---|
| 639 |
} |
|---|
| 640 |
|
|---|
| 641 |
inline vjMatrix operator /(const vjMatrix& _m, float _s) { |
|---|
| 642 |
vjMatrix dst; dst.scale(1.0f/_s, _m); return dst; |
|---|
| 643 |
} |
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
void vjMatrix::postMult(const vjMatrix& _m) |
|---|
| 650 |
{ |
|---|
| 651 |
vjMatrix prevMat = *this; |
|---|
| 652 |
zero(); |
|---|
| 653 |
|
|---|
| 654 |
for(int i=0;i<4;i++) |
|---|
| 655 |
for(int j=0;j<4;j++) |
|---|
| 656 |
for(int k=0;k<4;k++) |
|---|
| 657 |
mat[j][i] += ( prevMat.mat[k][i] * _m.mat[j][k]); |
|---|
| 658 |
} |
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
void vjMatrix::preMult(const vjMatrix& _m) |
|---|
| 662 |
{ |
|---|
| 663 |
vjMatrix prevMat = *this; |
|---|
| 664 |
zero(); |
|---|
| 665 |
|
|---|
| 666 |
for(int i=0;i<4;i++) |
|---|
| 667 |
for(int j=0;j<4;j++) |
|---|
| 668 |
for(int k=0;k<4;k++) |
|---|
| 669 |
mat[i][j] += ( prevMat.mat[i][k] * _m.mat[k][j]);; |
|---|
| 670 |
} |
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
int vjMatrix::invert(vjMatrix& _m) |
|---|
| 689 |
|
|---|
| 690 |
{ |
|---|
| 691 |
float* a = _m.getFloatPtr(); |
|---|
| 692 |
float* b = getFloatPtr(); |
|---|
| 693 |
|
|---|
| 694 |
int n = 4; |
|---|
| 695 |
|
|---|
| 696 |
int i, j, k; |
|---|
| 697 |
int r[ 4], c[ 4], row[ 4], col[ 4]; |
|---|
| 698 |
float m[ 4][ 4*2], pivot, max_m, tmp_m, fac; |
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
|
|---|
| 702 |
for ( i = 0; i < n; i ++ ) |
|---|
| 703 |
{ |
|---|
| 704 |
r[ i] = c[ i] = 0; |
|---|
| 705 |
row[ i] = col[ i] = 0; |
|---|
| 706 |
} |
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 |
for ( i = 0; i < n; i++ ) |
|---|
| 710 |
{ |
|---|
| 711 |
for ( j = 0; j < n; j++ ) |
|---|
| 712 |
{ |
|---|
| 713 |
m[ i][ j] = a[ i * n + j]; |
|---|
| 714 |
m[ i][ j + n] = ( i == j ) ? 1.0 : 0.0 ; |
|---|
| 715 |
} |
|---|
| 716 |
} |
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
for ( k = 0; k < n; k++ ) |
|---|
| 720 |
{ |
|---|
| 721 |
|
|---|
| 722 |
for ( i = 0, max_m = 0; i < n; i++ ) |
|---|
| 723 |
{ |
|---|
| 724 |
if ( row[ i] ) continue; |
|---|
| 725 |
for ( j = 0; j < n; j++ ) |
|---|
| 726 |
{ |
|---|
| 727 |
if ( col[ j] ) continue; |
|---|
| 728 |
tmp_m = fabs( m[ i][ j]); |
|---|
| 729 |
if ( tmp_m > max_m) |
|---|
| 730 |
{ |
|---|
| 731 |
max_m = tmp_m; |
|---|
| 732 |
r[ k] = i; |
|---|
| 733 |
c[ k] = j; |
|---|
| 734 |
} |
|---|
| 735 |
} |
|---|
| 736 |
} |
|---|
| 737 |
row[ r[k] ] = col[ c[k] ] = 1; |
|---|
| 738 |
pivot = m[ r[ k] ][ c[ k] ]; |
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 |
if ( fabs( pivot) <= 1e-20) |
|---|
| 742 |
{ |
|---|
| 743 |
std::cerr << "*** pivot = %f in mat_inv. ***\n"; |
|---|
| 744 |
|
|---|
| 745 |
return -1; |
|---|
| 746 |
} |
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
for ( j = 0; j < 2*n; j++ ) |
|---|
| 750 |
{ |
|---|
| 751 |
if ( j == c[ k] ) |
|---|
| 752 |
m[ r[ k]][ j] = 1.0; |
|---|
| 753 |
else |
|---|
| 754 |
m[ r[ k]][ j] /= pivot; |
|---|
| 755 |
} |
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
for ( i = 0; i < n; i++ ) |
|---|
| 759 |
{ |
|---|
| 760 |
if ( i == r[ k] ) |
|---|
| 761 |
continue; |
|---|
| 762 |
|
|---|
| 763 |
for ( j=0, fac = m[ i][ c[k]]; j < 2*n; j++ ) |
|---|
| 764 |
{ |
|---|
| 765 |
if ( j == c[ k] ) |
|---|
| 766 |
m[ i][ j] = 0.0; |
|---|
| 767 |
else |
|---|
| 768 |
m[ i][ j] -= fac * m[ r[k]][ j]; |
|---|
| 769 |
} |
|---|
| 770 |
} |
|---|
| 771 |
} |
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
for ( i = 0; i < n; i++ ) |
|---|
| 775 |
for ( j = 0; j < n; j++ ) |
|---|
| 776 |
row[ i] = ( c[ j] == i ) ? r[ j] : row[ i]; |
|---|
| 777 |
|
|---|
| 778 |
for ( i = 0; i < n; i++ ) |
|---|
| 779 |
for ( j = 0; j < n; j++ ) |
|---|
| 780 |
b[ i * n + j] = m[ row[ i]][ j + n]; |
|---|
| 781 |
|
|---|
| 782 |
return 1; |
|---|
| 783 |
} |
|---|
| 784 |
|
|---|
| 785 |
|
|---|
| 786 |
std::ostream& operator<<(std::ostream& out, vjMatrix& _mat) |
|---|
| 787 |
{ |
|---|
| 788 |
for(int j=0;j<4;j++) |
|---|
| 789 |
{ |
|---|
| 790 |
for(int i=0;i<4;i++) |
|---|
| 791 |
out << _mat.mat[i][j] << " "; |
|---|
| 792 |
out << std::endl; |
|---|
| 793 |
} |
|---|
| 794 |
|
|---|
| 795 |
return out; |
|---|
| 796 |
} |
|---|
| 797 |
|
|---|