Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8560 in orxonox.OLD for branches/bsp_model/src/lib/math


Ignore:
Timestamp:
Jun 17, 2006, 12:22:53 PM (18 years ago)
Author:
patrick
Message:

bsp: new quaterinon constructor created for 3x3 matrices, bone rotation interpolation via quaternions implemented

Location:
branches/bsp_model/src/lib/math
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/bsp_model/src/lib/math/quaternion.cc

    r7348 r8560  
    133133
    134134
     135
    135136/**
    136137 * @brief Slerps this QUaternion performs a smooth move.
     
    298299}
    299300
     301
     302/**
     303 * Creates a quaternion from a 3x3 rotation matrix.
     304 * @param mat The 3x3 source rotation matrix.
     305 * @return The equivalent 4 float quaternion.
     306 */
     307Quaternion::Quaternion(float mat[3][3])
     308{
     309  int   NXT[] = {1, 2, 0};
     310  float q[4];
     311
     312  // check the diagonal
     313  float tr = mat[0][0] + mat[1][1] + mat[2][2];
     314  if( tr > 0.0f) {
     315    float s = (float)sqrtf(tr + 1.0f);
     316    this->w = s * 0.5f;
     317    s = 0.5f / s;
     318    this->v.x = (mat[1][2] - mat[2][1]) * s;
     319    this->v.y = (mat[2][0] - mat[0][2]) * s;
     320    this->v.z = (mat[0][1] - mat[1][0]) * s;
     321  }
     322  else
     323  {
     324    // diagonal is negative
     325    // get biggest diagonal element
     326    int i = 0;
     327    if (mat[1][1] > mat[0][0]) i = 1;
     328    if (mat[2][2] > mat[i][i]) i = 2;
     329    //setup index sequence
     330    int j = NXT[i];
     331    int k = NXT[j];
     332
     333    float s = (float)sqrtf((mat[i][i] - (mat[j][j] + mat[k][k])) + 1.0f);
     334
     335    q[i] = s * 0.5f;
     336
     337    if (s != 0.0f) s = 0.5f / s;
     338
     339    q[j] = (mat[i][j] + mat[j][i]) * s;
     340    q[k] = (mat[i][k] + mat[k][i]) * s;
     341    q[3] = (mat[j][k] - mat[k][j]) * s;
     342
     343    this->v.x = q[0];
     344    this->v.y = q[1];
     345    this->v.z = q[2];
     346    this->w   = q[3];
     347  }
     348}
     349
    300350/**
    301351 * @brief outputs some nice formated debug information about this quaternion
     
    314364  PRINT(0)("angle = %f, axis: ax=%f, ay=%f, az=%f\n", this->getSpacialAxisAngle(), axis.x, axis.y, axis.z );
    315365}
     366
     367
     368
     369
     370
     371
  • branches/bsp_model/src/lib/math/quaternion.h

    r7348 r8560  
    4242  inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
    4343  Quaternion (float m[4][4]);
     44  Quaternion (float m[3][3]);
    4445  /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */
    4546  inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2.0); v = axis * sin(angle/2.0); }
     
    118119
    119120
     121// some helper functions
     122float* quaternionFromMatrix(float* mat);
     123
     124
    120125#endif /* __QUATERNION_H_ */
    121126
Note: See TracChangeset for help on using the changeset viewer.