00001
00002
00003
00004
00005
00006
00007
00008
00016 #include "fMatrix3.h"
00017 #include "fEulerPara.h"
00018
00019
00020
00021
00022
00023 void fMat33::ea2mat_xyz(const fVec3& ea)
00024 {
00025 double ca = cos(ea.m1), sa = sin(ea.m1);
00026 double cb = cos(ea.m2), sb = sin(ea.m2);
00027 double cc = cos(ea.m3), sc = sin(ea.m3);
00028 m11 = cb*cc;
00029 m12 = sa*sb*cc - ca*sc;
00030 m13 = ca*sb*cc + sa*sc;
00031 m21 = cb*sc;
00032 m22 = sa*sb*sc + ca*cc;
00033 m23 = ca*sb*sc - sa*cc;
00034 m31 = -sb;
00035 m32 = sa*cb;
00036 m33 = ca*cb;
00037 }
00038
00039 void fVec3::mat2ea_xyz(const fMat33& mat)
00040 {
00041 double cb = sqrt(mat.m32*mat.m32 + mat.m33*mat.m33);
00042 m2 = atan2(-mat.m31, cb);
00043 if(cb < TINY)
00044 {
00045 m1 = atan2(mat.m12, mat.m22);
00046 m3 = 0.0;
00047 }
00048 else
00049 {
00050 m1 = atan2(mat.m32/cb, mat.m33/cb);
00051 m3 = atan2(mat.m21/cb, mat.m11/cb);
00052 }
00053 }
00054
00055
00056 void fMat33::ea2mat_xzy(const fVec3& ea)
00057 {
00058 double c1 = cos(ea.m1), s1 = sin(ea.m1);
00059 double c2 = cos(ea.m2), s2 = sin(ea.m2);
00060 double c3 = cos(ea.m3), s3 = sin(ea.m3);
00061 m11 = c2*c3;
00062 m12 = s1*s3 - c1*s2*c3;
00063 m13 = c1*s3 + s1*s2*c3;
00064 m21 = s2;
00065 m22 = c1*c2;
00066 m23 = -s1*c2;
00067 m31 = -c2*s3;
00068 m32 = s1*c3 + c1*s2*s3;
00069 m33 = c1*c3 - s1*s2*s3;
00070 }
00071
00072 void fVec3::mat2ea_xzy(const fMat33& mat)
00073 {
00074 double c2 = sqrt(mat.m22*mat.m22 + mat.m23*mat.m23);
00075 m2 = atan2(mat.m21, c2);
00076 if(c2 < TINY)
00077 {
00078 m1 = atan2(mat.m32, mat.m33);
00079 m3 = 0.0;
00080 }
00081 else
00082 {
00083 m1 = atan2(-mat.m23, mat.m22);
00084 m3 = atan2(-mat.m31, mat.m11);
00085 }
00086 }
00087
00088
00089 void fMat33::ea2mat_zyx(const fVec3& ea)
00090 {
00091 double c1 = cos(ea.m1), s1 = sin(ea.m1);
00092 double c2 = cos(ea.m2), s2 = sin(ea.m2);
00093 double c3 = cos(ea.m3), s3 = sin(ea.m3);
00094 m11 = c1*c2;
00095 m12 = -s1*c2;
00096 m13 = s2;
00097 m21 = s1*c3 + c1*s2*s3;
00098 m22 = c1*c3 - s1*s2*s3;
00099 m23 = -c2*s3;
00100 m31 = s1*s3 - c1*s2*c3;
00101 m32 = c1*s3 + s1*s2*s3;
00102 m33 = c2*c3;
00103 }
00104
00105 void fVec3::mat2ea_zyx(const fMat33& mat)
00106 {
00107 double c2 = sqrt(mat.m11*mat.m11 + mat.m12*mat.m12);
00108 m2 = atan2(mat.m13, c2);
00109 if(c2 < TINY)
00110 {
00111 m1 = atan2(mat.m21, mat.m22);
00112 m3 = 0.0;
00113 }
00114 else
00115 {
00116 m1 = atan2(-mat.m12, mat.m11);
00117 m3 = atan2(-mat.m23, mat.m33);
00118 }
00119 }
00120
00121
00122 void fMat33::ea2mat_yzx(const fVec3& ea)
00123 {
00124 double c1 = cos(ea.m1), s1 = sin(ea.m1);
00125 double c2 = cos(ea.m2), s2 = sin(ea.m2);
00126 double c3 = cos(ea.m3), s3 = sin(ea.m3);
00127 m11 = c1*c2;
00128 m12 = -s2;
00129 m13 = s1*c2;
00130 m21 = s1*s3 + c1*s2*c3;
00131 m22 = c2*c3;
00132 m23 = -c1*s3 + s1*s2*c3;
00133 m31 = -s1*c3 + c1*s2*s3;
00134 m32 = c2*s3;
00135 m33 = c1*c3 + s1*s2*s3;
00136 }
00137
00138 void fVec3::mat2ea_yzx(const fMat33& mat)
00139 {
00140 double c2 = sqrt(mat.m11*mat.m11 + mat.m13*mat.m13);
00141 m2 = atan2(-mat.m12, c2);
00142 if(c2 < TINY)
00143 {
00144 m1 = atan2(mat.m23, mat.m33);
00145 m3 = 0.0;
00146 }
00147 else
00148 {
00149 m1 = atan2(mat.m13, mat.m11);
00150 m3 = atan2(mat.m32, mat.m22);
00151 }
00152 }
00153
00154 void fVec3::mat2ea_xyz(const fMat33& mat, const fVec3& ea_ref)
00155 {
00156 mat2ea_xyz(mat);
00157 if(fabs(m1-ea_ref(0)) > PI)
00158 {
00159 if(m1 < -PI*0.5 || ea_ref(0) > PI*0.5)
00160 {
00161 m1 += 2.0*PI;
00162 }
00163 else if(m1 > PI*0.5 || ea_ref(0) < -PI*0.5)
00164 {
00165 m1 -= 2.0*PI;
00166 }
00167 }
00168 if(fabs(m3-ea_ref(2)) > PI)
00169 {
00170
00171 if(m3 < -PI*0.5 || ea_ref(2) > PI*0.5)
00172 {
00173
00174 m3 += 2.0*PI;
00175
00176 }
00177 else if(m3 > PI*0.5 || ea_ref(2) < -PI*0.5)
00178 {
00179
00180 m3 -= 2.0*PI;
00181
00182 }
00183 }
00184 }
00185
00186 void fVec3::epdot2angvel(const fEulerPara& _epara, const fEulerPara& _edot)
00187 {
00188 static fVec3 temp;
00189 fEulerPara epara(_epara), edot(_edot);
00190 fMat33 mat(epara(3), epara(2), -epara(1), -epara(2), epara(3), epara(0), epara(1), -epara(0), epara(3));
00191 mul(mat, edot.Vec());
00192 temp.mul(-edot(3), epara.Vec());
00193 add(temp);
00194 (*this) *= 2.0;
00195
00196 }
00197
00198 void fVec3::epddot2angacc(const fEulerPara& _e, const fEulerPara& _de, const fEulerPara& _dde)
00199 {
00200 fEulerPara e(_e), de(_de), dde(_dde);
00201 fMat33 mat1(e(3), e(2), -e(1), -e(2), e(3), e(0), e(1), -e(0), e(3));
00202 fMat33 mat2(de(3), de(2), -de(1), -de(2), de(3), de(0), de(1), -de(0), de(3));
00203 static fVec3 tmp;
00204 mul(mat2, de.Vec());
00205 tmp.mul(mat1, dde.Vec());
00206 add(tmp);
00207 tmp.mul(-de(3), de.Vec());
00208 add(tmp);
00209 tmp.mul(-dde(3), e.Vec());
00210 add(tmp);
00211 (*this) *= 2.0;
00212 }
00213
00214
00215
00216
00217 void fMat33::cross(const fVec3& p)
00218 {
00219 m11 = 0; m12 = -p.m3; m13 = p.m2;
00220 m21 = p.m3; m22 = 0; m23 = -p.m1;
00221 m31 = -p.m2; m32 = p.m1; m33 = 0;
00222 }
00223
00224 void fMat33::diag(double v1, double v2, double v3)
00225 {
00226 m11 = v1; m12 = 0; m13 = 0;
00227 m21 = 0; m22 = v2; m23 = 0;
00228 m31 = 0; m32 = 0; m33 = v3;
00229 }
00230 void fMat33::identity()
00231 {
00232 m11 = 1; m12 = 0; m13 = 0;
00233 m21 = 0; m22 = 1; m23 = 0;
00234 m31 = 0; m32 = 0; m33 = 1;
00235 }
00236 void fMat33::zero()
00237 {
00238 m11 = 0; m12 = 0; m13 = 0;
00239 m21 = 0; m22 = 0; m23 = 0;
00240 m31 = 0; m32 = 0; m33 = 0;
00241 }
00242
00243
00244
00245
00246 inline double sgn(double x)
00247 {
00248 if(x < 0.0) return -1.0;
00249 else if(x > 0.0) return 1.0;
00250 else return 0.0;
00251 }
00252
00253 void fMat33::mat2rot(fVec3& axis, double& angle) const
00254 {
00255 double e = ((*this)(2,1) - (*this)(1,2)) * ((*this)(2,1) - (*this)(1,2)) +
00256 ((*this)(0,2) - (*this)(2,0)) * ((*this)(0,2) - (*this)(2,0)) +
00257 ((*this)(1,0) - (*this)(0,1)) * ((*this)(1,0) - (*this)(0,1));
00258 double c = 0.5 * ((*this)(0,0) + (*this)(1,1) + (*this)(2,2) - 1.0);
00259
00260 double v = 1.0 - c;
00261 if(v < TINY)
00262 {
00263 angle = 0.0;
00264 axis(0) = 0.0;
00265 axis(1) = 0.0;
00266 axis(2) = 1.0;
00267 }
00268 else
00269 {
00270 double s = 0.5 * sqrt(e);
00271 angle = atan2(s, c);
00272
00273 if(c > -0.7071)
00274 {
00275
00276 if(fabs(s) < TINY)
00277 {
00278 axis(0) = 0.0;
00279 axis(1) = 0.0;
00280 axis(2) = 1.0;
00281 }
00282 else
00283 {
00284 axis(0) = 0.5 * ((*this)(2,1) - (*this)(1,2)) / s;
00285 axis(1) = 0.5 * ((*this)(0,2) - (*this)(2,0)) / s;
00286 axis(2) = 0.5 * ((*this)(1,0) - (*this)(0,1)) / s;
00287 }
00288 }
00289 else
00290 {
00291
00292 double rx = (*this)(0,0) - c;
00293 double ry = (*this)(1,1) - c;
00294 double rz = (*this)(2,2) - c;
00295
00296 if(rx > ry)
00297 {
00298 if(rx > rz)
00299 {
00300
00301 if (fabs(s) < TINY)
00302 {
00303 axis(0) = sqrt(rx / v);
00304 }
00305 else
00306 {
00307 axis(0) = sgn((*this)(2,1) - (*this)(1,2)) * sqrt(rx / v);
00308 }
00309 axis(1) = ((*this)(1,0) + (*this)(0,1)) / (2 * axis(0) * v);
00310 axis(2) = ((*this)(0,2) + (*this)(2,0)) / (2 * axis(0) * v);
00311 }
00312 else
00313 {
00314
00315 if (fabs(s) < TINY)
00316 {
00317 axis(2) = sqrt(rz / v);
00318 }
00319 else
00320 {
00321 axis(2) = sgn((*this)(1,0) - (*this)(0,1)) * sqrt(rz / v);
00322 }
00323 axis(0) = ((*this)(0,2) + (*this)(2,0)) / (2 * axis(2) * v);
00324 axis(1) = ((*this)(1,2) + (*this)(2,1)) / (2 * axis(2) * v);
00325 }
00326 }
00327 else
00328 {
00329 if(ry > rz)
00330 {
00331
00332 if (fabs(s) < TINY)
00333 {
00334 axis(1) = sqrt(ry / v);
00335 }
00336 else
00337 {
00338 axis(1) = sgn((*this)(0,2) - (*this)(2,0)) * sqrt(ry / v);
00339 }
00340 axis(0) = ((*this)(0,1) + (*this)(1,0)) / (2 * axis(1) * v);
00341 axis(2) = ((*this)(1,2) + (*this)(2,1)) / (2 * axis(1) * v);
00342 }
00343 else
00344 {
00345
00346 if (fabs(s) < TINY)
00347 {
00348 axis(2) = sqrt(rz / v);
00349 }
00350 else
00351 {
00352 axis(2) = sgn((*this)(1,0) - (*this)(0,1)) * sqrt(rz / v);
00353 }
00354 axis(0) = ((*this)(0,2) + (*this)(2,0)) / (2 * axis(2) * v);
00355 axis(1) = ((*this)(1,2) + (*this)(2,1)) / (2 * axis(2) * v);
00356 }
00357 }
00358 }
00359 }
00360 }
00361
00362 void fMat33::rot2mat(const fVec3& axis, double angle)
00363 {
00364 double x = axis.m1, y = axis.m2, z = axis.m3;
00365 double sa = sin(angle), ca = cos(angle);
00366 (*this)(0,0) = ca + (1-ca)*x*x;
00367 (*this)(0,1) = (1-ca)*x*y - sa*z;
00368 (*this)(0,2) = (1-ca)*x*z + sa*y;
00369 (*this)(1,0) = (1-ca)*y*x + sa*z;
00370 (*this)(1,1) = ca + (1-ca)*y*y;
00371 (*this)(1,2) = (1-ca)*y*z - sa*x;
00372 (*this)(2,0) = (1-ca)*z*x - sa*y;
00373 (*this)(2,1) = (1-ca)*z*y + sa*x;
00374 (*this)(2,2) = ca + (1-ca)*z*z;
00375 }
00376
00377
00378
00379
00380 fMat33 fMat33::operator = (const fMat33& mat)
00381 {
00382 m11 = mat.m11;
00383 m12 = mat.m12;
00384 m13 = mat.m13;
00385 m21 = mat.m21;
00386 m22 = mat.m22;
00387 m23 = mat.m23;
00388 m31 = mat.m31;
00389 m32 = mat.m32;
00390 m33 = mat.m33;
00391 return *this;
00392 }
00393
00394 void fMat33::operator = (double d)
00395 {
00396 m11 = m22 = m33 = d;
00397 m12 = m13 = 0.0;
00398 m21 = m23 = 0.0;
00399 m31 = m32 = 0.0;
00400 }
00401
00402 fMat33 operator - (const fMat33& mat)
00403 {
00404 fMat33 ret;
00405 ret.m11 = - mat.m11;
00406 ret.m12 = - mat.m12;
00407 ret.m13 = - mat.m13;
00408 ret.m21 = - mat.m21;
00409 ret.m22 = - mat.m22;
00410 ret.m23 = - mat.m23;
00411 ret.m31 = - mat.m31;
00412 ret.m32 = - mat.m32;
00413 ret.m33 = - mat.m33;
00414 return ret;
00415 }
00416
00417 void fMat33::operator += (const fMat33& mat)
00418 {
00419 m11 += mat.m11;
00420 m12 += mat.m12;
00421 m13 += mat.m13;
00422 m21 += mat.m21;
00423 m22 += mat.m22;
00424 m23 += mat.m23;
00425 m31 += mat.m31;
00426 m32 += mat.m32;
00427 m33 += mat.m33;
00428 }
00429
00430 void fMat33::operator -= (const fMat33& mat)
00431 {
00432 m11 -= mat.m11;
00433 m12 -= mat.m12;
00434 m13 -= mat.m13;
00435 m21 -= mat.m21;
00436 m22 -= mat.m22;
00437 m23 -= mat.m23;
00438 m31 -= mat.m31;
00439 m32 -= mat.m32;
00440 m33 -= mat.m33;
00441 }
00442
00443 void fMat33::operator *= (double d)
00444 {
00445 m11 *= d; m12 *= d; m13 *= d;
00446 m21 *= d; m22 *= d; m23 *= d;
00447 m31 *= d; m32 *= d; m33 *= d;
00448 }
00449
00450 void fMat33::operator /= (double d)
00451 {
00452 m11 /= d; m12 /= d; m13 /= d;
00453 m21 /= d; m22 /= d; m23 /= d;
00454 m31 /= d; m32 /= d; m33 /= d;
00455 }
00456
00457 fMat33 operator + (const fMat33& mat1, const fMat33& mat2)
00458 {
00459 fMat33 ret;
00460 ret.m11 = mat1.m11 + mat2.m11;
00461 ret.m12 = mat1.m12 + mat2.m12;
00462 ret.m13 = mat1.m13 + mat2.m13;
00463 ret.m21 = mat1.m21 + mat2.m21;
00464 ret.m22 = mat1.m22 + mat2.m22;
00465 ret.m23 = mat1.m23 + mat2.m23;
00466 ret.m31 = mat1.m31 + mat2.m31;
00467 ret.m32 = mat1.m32 + mat2.m32;
00468 ret.m33 = mat1.m33 + mat2.m33;
00469 return ret;
00470 }
00471
00472 fMat33 operator - (const fMat33& mat1, const fMat33& mat2)
00473 {
00474 fMat33 ret;
00475 ret.m11 = mat1.m11 - mat2.m11;
00476 ret.m12 = mat1.m12 - mat2.m12;
00477 ret.m13 = mat1.m13 - mat2.m13;
00478 ret.m21 = mat1.m21 - mat2.m21;
00479 ret.m22 = mat1.m22 - mat2.m22;
00480 ret.m23 = mat1.m23 - mat2.m23;
00481 ret.m31 = mat1.m31 - mat2.m31;
00482 ret.m32 = mat1.m32 - mat2.m32;
00483 ret.m33 = mat1.m33 - mat2.m33;
00484 return ret;
00485 }
00486
00487 fMat33 operator * (double d, const fMat33& mat)
00488 {
00489 fMat33 ret;
00490 ret.m11 = d * mat.m11;
00491 ret.m12 = d * mat.m12;
00492 ret.m13 = d * mat.m13;
00493 ret.m21 = d * mat.m21;
00494 ret.m22 = d * mat.m22;
00495 ret.m23 = d * mat.m23;
00496 ret.m31 = d * mat.m31;
00497 ret.m32 = d * mat.m32;
00498 ret.m33 = d * mat.m33;
00499 return ret;
00500 }
00501
00502 fMat33 operator * (const fMat33& mat1, const fMat33& mat2)
00503 {
00504 fMat33 ret;
00505 ret.m11 = mat1.m11*mat2.m11 + mat1.m12*mat2.m21 + mat1.m13*mat2.m31;
00506 ret.m21 = mat1.m21*mat2.m11 + mat1.m22*mat2.m21 + mat1.m23*mat2.m31;
00507 ret.m31 = mat1.m31*mat2.m11 + mat1.m32*mat2.m21 + mat1.m33*mat2.m31;
00508 ret.m12 = mat1.m11*mat2.m12 + mat1.m12*mat2.m22 + mat1.m13*mat2.m32;
00509 ret.m22 = mat1.m21*mat2.m12 + mat1.m22*mat2.m22 + mat1.m23*mat2.m32;
00510 ret.m32 = mat1.m31*mat2.m12 + mat1.m32*mat2.m22 + mat1.m33*mat2.m32;
00511 ret.m13 = mat1.m11*mat2.m13 + mat1.m12*mat2.m23 + mat1.m13*mat2.m33;
00512 ret.m23 = mat1.m21*mat2.m13 + mat1.m22*mat2.m23 + mat1.m23*mat2.m33;
00513 ret.m33 = mat1.m31*mat2.m13 + mat1.m32*mat2.m23 + mat1.m33*mat2.m33;
00514 return ret;
00515 }
00516
00517 double& fMat33::operator () (int i, int j)
00518 {
00519 #ifndef NDEBUG
00520 if(i<0 || i>=3 || j<0 || j>=3)
00521 {
00522 cerr << "matrix size error at operator ()" << endl;
00523 return temp;
00524 }
00525 #endif
00526 return *(&m11 + i + j*3);
00527 }
00528
00529 double fMat33::operator () (int i, int j) const
00530 {
00531 #ifndef NDEBUG
00532 if(i<0 || i>=3 || j<0 || j>=3)
00533 {
00534 cerr << "matrix size error at operator ()" << endl;
00535 return temp;
00536 }
00537 #endif
00538 return *(&m11 + i + j*3);
00539 }
00540
00541 double& fVec3::operator () (int i)
00542 {
00543 #ifndef NDEBUG
00544 if(i<0 || i>=3)
00545 {
00546 cerr << "vector size error at operator ()" << endl;
00547 return temp;
00548 }
00549 #endif
00550 return *(&m1 + i);
00551 }
00552
00553 double fVec3::operator () (int i) const
00554 {
00555 #ifndef NDEBUG
00556 if(i<0 || i>=3)
00557 {
00558 cerr << "vector size error at operator ()" << endl;
00559 return temp;
00560 }
00561 #endif
00562 return *(&m1 + i);
00563 }
00564
00565 fVec3 operator * (const fMat33& m, const fVec3& v)
00566 {
00567 fVec3 ret;
00568 ret.data()[0] = m.m11*v[0] + m.m12*v[1] + m.m13*v[2];
00569 ret.data()[1] = m.m21*v[0] + m.m22*v[1] + m.m23*v[2];
00570 ret.data()[2] = m.m31*v[0] + m.m32*v[1] + m.m33*v[2];
00571 return ret;
00572 }
00573
00574 fMat33 operator * (const fMat33& mat, double d)
00575 {
00576 fMat33 ret;
00577 ret.m11 = mat.m11 * d;
00578 ret.m12 = mat.m12 * d;
00579 ret.m13 = mat.m13 * d;
00580 ret.m21 = mat.m21 * d;
00581 ret.m22 = mat.m22 * d;
00582 ret.m23 = mat.m23 * d;
00583 ret.m31 = mat.m31 * d;
00584 ret.m32 = mat.m32 * d;
00585 ret.m33 = mat.m33 * d;
00586 return ret;
00587 }
00588
00589 fMat33 operator / (const fMat33& mat, double d)
00590 {
00591 fMat33 ret;
00592 ret.m11 = mat.m11 / d;
00593 ret.m12 = mat.m12 / d;
00594 ret.m13 = mat.m13 / d;
00595 ret.m21 = mat.m21 / d;
00596 ret.m22 = mat.m22 / d;
00597 ret.m23 = mat.m23 / d;
00598 ret.m31 = mat.m31 / d;
00599 ret.m32 = mat.m32 / d;
00600 ret.m33 = mat.m33 / d;
00601 return ret;
00602 }
00603
00604
00605
00606
00607 fMat33 tran(const fMat33& mat)
00608 {
00609 fMat33 ret;
00610 ret.m11 = mat.m11; ret.m12 = mat.m21; ret.m13 = mat.m31;
00611 ret.m21 = mat.m12; ret.m22 = mat.m22; ret.m23 = mat.m32;
00612 ret.m31 = mat.m13; ret.m32 = mat.m23; ret.m33 = mat.m33;
00613 return ret;
00614 }
00615
00616 void fMat33::tran(const fMat33& mat)
00617 {
00618 m11 = mat.m11; m12 = mat.m21; m13 = mat.m31;
00619 m21 = mat.m12; m22 = mat.m22; m23 = mat.m32;
00620 m31 = mat.m13; m32 = mat.m23; m33 = mat.m33;
00621 }
00622
00623 void fMat33::set(const fMat33& mat)
00624 {
00625 m11 = mat.m11; m12 = mat.m12; m13 = mat.m13;
00626 m21 = mat.m21; m22 = mat.m22; m23 = mat.m23;
00627 m31 = mat.m31; m32 = mat.m32; m33 = mat.m33;
00628 }
00629
00630 void fMat33::set(const fEulerPara& ep)
00631 {
00632 fEulerPara epara(ep);
00633 double ex = epara(0), ey = epara(1), ez = epara(2), e = epara(3);
00634 double ee = e * e, exx = ex * ex, eyy = ey * ey, ezz = ez * ez;
00635 double exy = ex * ey, eyz = ey * ez, ezx = ez * ex;
00636 double eex = e * ex, eey = e * ey, eez = e * ez;
00637 m11 = exx - eyy - ezz + ee;
00638 m22 = - exx + eyy - ezz + ee;
00639 m33 = - exx - eyy + ezz + ee;
00640 m12 = 2.0 * (exy - eez);
00641 m21 = 2.0 * (exy + eez);
00642 m13 = 2.0 * (ezx + eey);
00643 m31 = 2.0 * (ezx - eey);
00644 m23 = 2.0 * (eyz - eex);
00645 m32 = 2.0 * (eyz + eex);
00646 }
00647
00648 void fMat33::neg(const fMat33& mat)
00649 {
00650 m11 = -mat.m11; m12 = -mat.m12; m13 = -mat.m13;
00651 m21 = -mat.m21; m22 = -mat.m22; m23 = -mat.m23;
00652 m31 = -mat.m31; m32 = -mat.m32; m33 = -mat.m33;
00653 }
00654
00655 void fMat33::add(const fMat33& mat1, const fMat33& mat2)
00656 {
00657 m11 = mat1.m11 + mat2.m11;
00658 m12 = mat1.m12 + mat2.m12;
00659 m13 = mat1.m13 + mat2.m13;
00660 m21 = mat1.m21 + mat2.m21;
00661 m22 = mat1.m22 + mat2.m22;
00662 m23 = mat1.m23 + mat2.m23;
00663 m31 = mat1.m31 + mat2.m31;
00664 m32 = mat1.m32 + mat2.m32;
00665 m33 = mat1.m33 + mat2.m33;
00666 }
00667
00668 void fMat33::add(const fMat33& mat)
00669 {
00670 m11 += mat.m11;
00671 m12 += mat.m12;
00672 m13 += mat.m13;
00673 m21 += mat.m21;
00674 m22 += mat.m22;
00675 m23 += mat.m23;
00676 m31 += mat.m31;
00677 m32 += mat.m32;
00678 m33 += mat.m33;
00679 }
00680
00681 void fMat33::sub(const fMat33& mat1, const fMat33& mat2)
00682 {
00683 m11 = mat1.m11 - mat2.m11;
00684 m12 = mat1.m12 - mat2.m12;
00685 m13 = mat1.m13 - mat2.m13;
00686 m21 = mat1.m21 - mat2.m21;
00687 m22 = mat1.m22 - mat2.m22;
00688 m23 = mat1.m23 - mat2.m23;
00689 m31 = mat1.m31 - mat2.m31;
00690 m32 = mat1.m32 - mat2.m32;
00691 m33 = mat1.m33 - mat2.m33;
00692 }
00693
00694 void fMat33::mul(const fMat33& mat1, const fMat33& mat2)
00695 {
00696 m11 = mat1.m11*mat2.m11 + mat1.m12*mat2.m21 + mat1.m13*mat2.m31;
00697 m21 = mat1.m21*mat2.m11 + mat1.m22*mat2.m21 + mat1.m23*mat2.m31;
00698 m31 = mat1.m31*mat2.m11 + mat1.m32*mat2.m21 + mat1.m33*mat2.m31;
00699 m12 = mat1.m11*mat2.m12 + mat1.m12*mat2.m22 + mat1.m13*mat2.m32;
00700 m22 = mat1.m21*mat2.m12 + mat1.m22*mat2.m22 + mat1.m23*mat2.m32;
00701 m32 = mat1.m31*mat2.m12 + mat1.m32*mat2.m22 + mat1.m33*mat2.m32;
00702 m13 = mat1.m11*mat2.m13 + mat1.m12*mat2.m23 + mat1.m13*mat2.m33;
00703 m23 = mat1.m21*mat2.m13 + mat1.m22*mat2.m23 + mat1.m23*mat2.m33;
00704 m33 = mat1.m31*mat2.m13 + mat1.m32*mat2.m23 + mat1.m33*mat2.m33;
00705 }
00706
00707 void fMat33::mul(double d, const fMat33& mat)
00708 {
00709 m11 = d * mat.m11;
00710 m12 = d * mat.m12;
00711 m13 = d * mat.m13;
00712 m21 = d * mat.m21;
00713 m22 = d * mat.m22;
00714 m23 = d * mat.m23;
00715 m31 = d * mat.m31;
00716 m32 = d * mat.m32;
00717 m33 = d * mat.m33;
00718 }
00719
00720 void fMat33::mul(const fMat33& mat, double d)
00721 {
00722 m11 = d * mat.m11;
00723 m12 = d * mat.m12;
00724 m13 = d * mat.m13;
00725 m21 = d * mat.m21;
00726 m22 = d * mat.m22;
00727 m23 = d * mat.m23;
00728 m31 = d * mat.m31;
00729 m32 = d * mat.m32;
00730 m33 = d * mat.m33;
00731 }
00732
00733 void fMat33::mul(const fVec3& v1, const fVec3& v2)
00734 {
00735 m11 = v1.m1 * v2.m1;
00736 m12 = v1.m1 * v2.m2;
00737 m13 = v1.m1 * v2.m3;
00738 m21 = v1.m2 * v2.m1;
00739 m22 = v1.m2 * v2.m2;
00740 m23 = v1.m2 * v2.m3;
00741 m31 = v1.m3 * v2.m1;
00742 m32 = v1.m3 * v2.m2;
00743 m33 = v1.m3 * v2.m3;
00744 }
00745
00746 void fMat33::div(const fMat33& mat, double d)
00747 {
00748 m11 = mat.m11 / d;
00749 m12 = mat.m12 / d;
00750 m13 = mat.m13 / d;
00751 m21 = mat.m21 / d;
00752 m22 = mat.m22 / d;
00753 m23 = mat.m23 / d;
00754 m31 = mat.m31 / d;
00755 m32 = mat.m32 / d;
00756 m33 = mat.m33 / d;
00757 }
00758
00759
00760
00761
00762 void fVec3::operator = (double d)
00763 {
00764 m1 = m2 = m3 = d;
00765 }
00766
00767 fVec3 fVec3::operator = (const fVec3& vec)
00768 {
00769 m1 = vec.m1; m2 = vec.m2; m3 = vec.m3;
00770 return *this;
00771 }
00772
00773 fVec3 operator & (const fVec3& vec1, const fVec3& vec2)
00774 {
00775 fVec3 ret;
00776 ret.m1 = vec1.m2*vec2.m3 - vec1.m3*vec2.m2;
00777 ret.m2 = vec1.m3*vec2.m1 - vec1.m1*vec2.m3;
00778 ret.m3 = vec1.m1*vec2.m2 - vec1.m2*vec2.m1;
00779 return ret;
00780 }
00781
00782 fVec3 operator - (const fVec3& vec)
00783 {
00784 fVec3 ret;
00785 ret.m1 = -vec.m1;
00786 ret.m2 = -vec.m2;
00787 ret.m3 = -vec.m3;
00788 return ret;
00789 }
00790
00791 void fVec3::operator += (const fVec3& vec)
00792 {
00793 m1 += vec.m1;
00794 m2 += vec.m2;
00795 m3 += vec.m3;
00796 }
00797
00798 void fVec3::operator -= (const fVec3& vec)
00799 {
00800 m1 -= vec.m1;
00801 m2 -= vec.m2;
00802 m3 -= vec.m3;
00803 }
00804
00805 void fVec3::operator *= (double d)
00806 {
00807 m1 *= d;
00808 m2 *= d;
00809 m3 *= d;
00810 }
00811
00812 void fVec3::operator /= (double d)
00813 {
00814 m1 /= d;
00815 m2 /= d;
00816 m3 /= d;
00817 }
00818
00819 fVec3 operator - (const fVec3& vec1, const fVec3& vec2)
00820 {
00821 fVec3 ret;
00822 ret.m1 = vec1.m1 - vec2.m1;
00823 ret.m2 = vec1.m2 - vec2.m2;
00824 ret.m3 = vec1.m3 - vec2.m3;
00825 return ret;
00826 }
00827
00828 fVec3 operator + (const fVec3& vec1, const fVec3& vec2)
00829 {
00830 fVec3 ret;
00831 ret.m1 = vec1.m1 + vec2.m1;
00832 ret.m2 = vec1.m2 + vec2.m2;
00833 ret.m3 = vec1.m3 + vec2.m3;
00834 return ret;
00835 }
00836
00837 fVec3 operator / (const fVec3& vec, double d)
00838 {
00839 fVec3 ret;
00840 ret.m1 = vec.m1 / d;
00841 ret.m2 = vec.m2 / d;
00842 ret.m3 = vec.m3 / d;
00843 return ret;
00844 }
00845
00846 double operator * (const fVec3& vec1, const fVec3& vec2)
00847 {
00848 double ret = 0;
00849 ret = vec1.m1*vec2.m1 + vec1.m2*vec2.m2 + vec1.m3*vec2.m3;
00850 return ret;
00851 }
00852
00853 fVec3 operator * (const fVec3& vec1, double d)
00854 {
00855 fVec3 ret;
00856 ret.m1 = vec1.m1 * d;
00857 ret.m2 = vec1.m2 * d;
00858 ret.m3 = vec1.m3 * d;
00859 return ret;
00860 }
00861
00862 fVec3 operator * (double d, const fVec3& vec1)
00863 {
00864 fVec3 ret;
00865 ret.m1 = vec1.m1 * d;
00866 ret.m2 = vec1.m2 * d;
00867 ret.m3 = vec1.m3 * d;
00868 return ret;
00869 }
00870
00871
00872
00873
00874 void fVec3::set(const fVec3& vec)
00875 {
00876 m1 = vec.m1;
00877 m2 = vec.m2;
00878 m3 = vec.m3;
00879 }
00880
00881 void fVec3::neg(const fVec3& vec)
00882 {
00883 m1 = -vec.m1;
00884 m2 = -vec.m2;
00885 m3 = -vec.m3;
00886 }
00887
00888 void fVec3::add(const fVec3& vec1, const fVec3& vec2)
00889 {
00890 m1 = vec1.m1 + vec2.m1;
00891 m2 = vec1.m2 + vec2.m2;
00892 m3 = vec1.m3 + vec2.m3;
00893 }
00894
00895 void fVec3::add(const fVec3& vec)
00896 {
00897 m1 += vec.m1;
00898 m2 += vec.m2;
00899 m3 += vec.m3;
00900 }
00901
00902 void fVec3::sub(const fVec3& vec1, const fVec3& vec2)
00903 {
00904 m1 = vec1.m1 - vec2.m1;
00905 m2 = vec1.m2 - vec2.m2;
00906 m3 = vec1.m3 - vec2.m3;
00907 }
00908
00909 void fVec3::div(const fVec3& vec, double d)
00910 {
00911 m1 = vec.m1 / d;
00912 m2 = vec.m2 / d;
00913 m3 = vec.m3 / d;
00914 }
00915
00916 void fVec3::mul(const fVec3& vec, double d)
00917 {
00918 m1 = vec.m1 * d;
00919 m2 = vec.m2 * d;
00920 m3 = vec.m3 * d;
00921 }
00922
00923 void fVec3::mul(double d, const fVec3& vec)
00924 {
00925 m1 = vec.m1 * d;
00926 m2 = vec.m2 * d;
00927 m3 = vec.m3 * d;
00928 }
00929
00930 void fVec3::mul(const fMat33& mat, const fVec3& vec)
00931 {
00932 m1 = mat.m11*vec.m1 + mat.m12*vec.m2 + mat.m13*vec.m3;
00933 m2 = mat.m21*vec.m1 + mat.m22*vec.m2 + mat.m23*vec.m3;
00934 m3 = mat.m31*vec.m1 + mat.m32*vec.m2 + mat.m33*vec.m3;
00935 }
00936
00937 void fVec3::mul(const fVec3& vec, const fMat33& mat)
00938 {
00939 m1 = vec.m1*mat.m11 + vec.m2*mat.m21 + vec.m3*mat.m31;
00940 m2 = vec.m1*mat.m12 + vec.m2*mat.m22 + vec.m3*mat.m32;
00941 m3 = vec.m1*mat.m13 + vec.m2*mat.m23 + vec.m3*mat.m33;
00942 }
00943
00944 void fVec3::cross(const fVec3& vec1, const fVec3& vec2)
00945 {
00946 m1 = vec1.m2*vec2.m3 - vec1.m3*vec2.m2;
00947 m2 = vec1.m3*vec2.m1 - vec1.m1*vec2.m3;
00948 m3 = vec1.m1*vec2.m2 - vec1.m2*vec2.m1;
00949 }
00950
00951
00952
00953
00954 void fVec3::rotation(const fMat33& ref, const fMat33& target)
00955 {
00956 static fMat33 tmp;
00957 static fVec3 v;
00958 tmp.mul(ref, tran(target));
00959 v.m1 = tmp.m32 - tmp.m23;
00960 v.m2 = tmp.m13 - tmp.m31;
00961 v.m3 = tmp.m21 - tmp.m12;
00962 (*this) = 0.5 * v;
00963 }
00964
00965
00966
00967
00968 ostream& operator << (ostream& ost, const fVec3& v)
00969 {
00970 ost << "(" << v.m1 << ", " << v.m2 << ", " << v.m3 << ")" << flush;
00971 return ost;
00972 }
00973
00974 ostream& operator << (ostream& ost, const fMat33& m)
00975 {
00976 ost << "(" << m.m11 << ", " << m.m12 << ", " << m.m13 << "," << endl
00977 << " " << m.m21 << ", " << m.m22 << ", " << m.m23 << "," << endl
00978 << " " << m.m31 << ", " << m.m32 << ", " << m.m33 << ")" << flush;
00979 return ost;
00980 }