Al's Programming Resource Homepage  Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

quat.cpp

Go to the documentation of this file.
00001 #include <windows.h>
00002 #include "quat.h"
00003 #include "mmgr.h"
00004 
00005 
00006 QUAT::QUAT(float sx, float sy, float sz, float sw)
00007 :
00008     x(sx),
00009     y(sy),
00010     z(sz),
00011         w(sw)
00012 {
00013 }
00014 
00015 QUAT::~QUAT()
00016 {
00017 }
00018 
00019 void QUAT::Reset()
00020 {
00021     x = 0.0;
00022     y = 0.0;
00023     z = 0.0;
00024     w = 1.0;
00025 }
00026 
00027 void QUAT::CopyQuat(QUAT q)
00028 {
00029     x = q.x;
00030     y = q.y;
00031     z = q.z;
00032     w = q.w;
00033 }
00034 
00035 void QUAT::AxisAngleToQuat(VECTOR axis, float theta)
00036 {
00037         float halfTheta = theta * 0.5;
00038         float cosHalfTheta = cos(halfTheta);
00039         float sinHalfTheta = sin(halfTheta);
00040         x = axis.x * sinHalfTheta;
00041         y = axis.y * sinHalfTheta;
00042         z = axis.z * sinHalfTheta;
00043         w = cosHalfTheta;
00044 }
00045 
00046 void QUAT::EulerToQuat(float roll, float pitch, float yaw)
00047 {
00048         float cr, cp, cy, sr, sp, sy, cpcy, spsy;  // calculate trig identities
00049         cr = cos(roll/2);
00050         cp = cos(pitch/2);
00051         cy = cos(yaw/2);
00052         sr = sin(roll/2);
00053         sp = sin(pitch/2);
00054         sy = sin(yaw/2);
00055         cpcy = cp * cy;
00056         spsy = sp * sy;
00057         w = cr * cpcy + sr * spsy;
00058         x = sr * cpcy - cr * spsy;
00059         y = cr * sp * cy + sr * cp * sy;
00060         z = cr * cp * sy - sr * sp * cy;
00061 }
00062 
00063 float QUAT::MagnitudeQuat()
00064 {
00065       return( sqrt(w*w+x*x+y*y+z*z));
00066 }
00067 
00068 void QUAT::NormaliseQuat()
00069 {
00070       float Mag;
00071       Mag = MagnitudeQuat();
00072       w = w/Mag;
00073       x = x/Mag;
00074       y = y/Mag;
00075       z = z/Mag;
00076 }
00077 
00078 void QUAT::MultQuat(QUAT q)
00079 {
00080       QUAT q3;
00081       VECTOR vectorq1;
00082       VECTOR vectorq2;
00083       vectorq1.x = x;
00084       vectorq1.y = y;
00085       vectorq1.z = z;
00086       vectorq2.x = q.x;
00087       vectorq2.y = q.y;
00088       vectorq2.z = q.z;
00089 
00090       VECTOR tempvec1;
00091       VECTOR tempvec2;
00092       VECTOR tempvec3;
00093       tempvec1 = vectorq1;
00094       q3.w = (w*q.w) - tempvec1.DotProduct(vectorq2);
00095       tempvec1.CrossVector(vectorq2);
00096       tempvec2.x = w * q.x;
00097       tempvec2.y = w * q.y;
00098       tempvec2.z = w * q.z;
00099       tempvec3.x = q.w * x;
00100       tempvec3.y = q.w * y;
00101       tempvec3.z = q.w * z;
00102       q3.x = tempvec1.x + tempvec2.x + tempvec3.x;
00103       q3.y = tempvec1.y + tempvec2.y + tempvec3.y;
00104       q3.z = tempvec1.z + tempvec2.z + tempvec3.z;
00105       CopyQuat(q3);
00106 }
00107 
00108 void QUAT::MatrixToQuat(float m[4][4])
00109 {
00110   float  tr, s, q[4];
00111   int    i, j, k;
00112 
00113   int nxt[3] = {1, 2, 0};
00114 
00115   tr = m[0][0] + m[1][1] + m[2][2];
00116 
00117   // check the diagonal
00118   if (tr > 0.0) {
00119     s = sqrt (tr + 1.0);
00120     w = s / 2.0;
00121     s = 0.5 / s;
00122     x = (m[1][2] - m[2][1]) * s;
00123     y = (m[2][0] - m[0][2]) * s;
00124     z = (m[0][1] - m[1][0]) * s;
00125 } 
00126 else 
00127 {        
00128      // diagonal is negative
00129           i = 0;
00130           if (m[1][1] > m[0][0]) i = 1;
00131          if (m[2][2] > m[i][i]) i = 2;
00132             j = nxt[i];
00133             k = nxt[j];
00134 
00135             s = sqrt ((m[i][i] - (m[j][j] + m[k][k])) + 1.0);
00136       
00137          q[i] = s * 0.5;
00138             
00139             if (s != 0.0) s = 0.5 / s;
00140 
00141         q[3] = (m[j][k] - m[k][j]) * s;
00142             q[j] = (m[i][j] + m[j][i]) * s;
00143             q[k] = (m[i][k] + m[k][i]) * s;
00144 
00145       x = q[0];
00146       y = q[1];
00147       z = q[2];
00148       w = q[3];
00149   }
00150 }

Generated on Fri Dec 23 05:21:39 2005 for Sound by doxygen1.2.15