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

locmath.cpp File Reference

#include "vector.h"
#include "quat.h"
#include "vertex.h"
#include "mmgr.h"

Go to the source code of this file.

Functions

void LoadIdentity (float m[])
void CopyMatrix (float m[], float n[])
void MultMatrix (float m[], float n[])
void MatrixInverse (float m[])
QUAT AxisAngleToMatrix (VECTOR axis, float theta, float m[16])
float DotProduct (VECTOR vec1, VECTOR vec2)
VECTOR CrossVector (VECTOR vec1, VECTOR vec2)
void EulerToQuat (float roll, float pitch, float yaw, QUAT *quat)
float MagnitudeQuat (QUAT q1)
QUAT NormaliseQuat (QUAT q1)
void QuatToMatrix (QUAT quat, float m[16])
QUAT MultQuat (QUAT q1, QUAT q2)
VERTEX GetNorm (float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)


Function Documentation

QUAT AxisAngleToMatrix VECTOR    axis,
float    theta,
float    m[16]
 

Definition at line 164 of file locmath.cpp.

References QUAT::w, QUAT::x, VECTOR::x, QUAT::y, VECTOR::y, QUAT::z, and VECTOR::z.

00165 {
00166         QUAT q;
00167         float halfTheta = theta * 0.5;
00168         float cosHalfTheta = cos(halfTheta);
00169         float sinHalfTheta = sin(halfTheta);
00170         float xs, ys, zs, wx, wy, wz, xx, xy, xz, yy, yz, zz;
00171         q.x = axis.x * sinHalfTheta;
00172         q.y = axis.y * sinHalfTheta;
00173         q.z = axis.z * sinHalfTheta;
00174         q.w = cosHalfTheta;
00175         xs = q.x * 2;  ys = q.y * 2;  zs = q.z * 2;
00176         wx = q.w * xs; wy = q.w * ys; wz = q.w * zs;
00177         xx = q.x * xs; xy = q.x * ys; xz = q.x * zs;
00178         yy = q.y * ys; yz = q.y * zs; zz = q.z * zs;
00179         m[0] = 1 - (yy + zz);
00180         m[1] = xy - wz;
00181         m[2] = xz + wy;
00182         m[4] = xy + wz;
00183         m[5] = 1 - (xx + zz);
00184         m[6] = yz - wx;
00185         m[8] = xz - wy;
00186         m[9] = yz + wx;
00187         m[10] = 1 - (xx + yy);
00188         /* Fill in remainder of 4x4 homogeneous transform matrix. */
00189         m[12] = m[13] = m[14] = m[3] = m[7] = m[11] = 0;
00190         m[15] = 1;
00191         return (q);
00192 }

void CopyMatrix float    m[],
float    n[]
 

Definition at line 29 of file locmath.cpp.

Referenced by MatrixInverse(), and MultMatrix().

00030 {
00031         m[0 ] = n[0 ];
00032         m[1 ] = n[1 ];
00033         m[2 ] = n[2 ];
00034         m[3 ] = n[3 ];
00035         m[4 ] = n[4 ];
00036         m[5 ] = n[5 ];
00037         m[6 ] = n[6 ];
00038         m[7 ] = n[7 ];
00039         m[8 ] = n[8 ];
00040         m[9 ] = n[9 ];
00041         m[10] = n[10];
00042         m[11] = n[11];
00043         m[12] = n[12];
00044         m[13] = n[13];
00045         m[14] = n[14];
00046         m[15] = n[15];
00047 }

VECTOR CrossVector VECTOR    vec1,
VECTOR    vec2
 

Definition at line 209 of file locmath.cpp.

References VECTOR::x, VECTOR::y, and VECTOR::z.

Referenced by MultQuat().

00210 {
00211     /*
00212     Cross Product of Two Vectors.
00213 
00214     a × b = ( a.y * b.z - a.z * b.y,
00215 
00216     a.z * b.x - a.x * b.z,
00217 
00218     a.x * b.y - a.y * b.x )
00219 
00220     | a × b | = |a| * |b| * sin(ø)
00221     */
00222       VECTOR vec3;
00223       vec3.x = vec1.y * vec2.z - vec1.z * vec2.y;
00224       vec3.y = vec1.z * vec2.x - vec1.x * vec2.z;
00225       vec3.z = vec1.x * vec2.y - vec1.y * vec2.x;
00226       return vec3;
00227 }

float DotProduct VECTOR    vec1,
VECTOR    vec2
 

Definition at line 194 of file locmath.cpp.

References VECTOR::x, VECTOR::y, and VECTOR::z.

Referenced by MultQuat().

00195 {
00196     /*
00197     Dot Product of two Vectors.
00198 
00199     U = (Ux, Uy, Uz)
00200     V = (Vx, Vy, Vz)
00201     U*V = UxVx + UyVy + UzVz
00202     U*V = |U||V|cos(t) (where t is the angle theta between the two vectors)
00203     */
00204       float dot;
00205       dot = vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
00206       return dot;
00207 }

void EulerToQuat float    roll,
float    pitch,
float    yaw,
QUAT   quat
 

Definition at line 229 of file locmath.cpp.

References QUAT::w, QUAT::x, QUAT::y, and QUAT::z.

00230 {
00231     /*
00232     Euler Angle to Quarternion.
00233 
00234     q = qyaw qpitch qroll where:
00235 
00236     qyaw = [cos(f /2), (0, 0, sin(f /2)]
00237     qpitch = [cos (q/2), (0, sin(q/2), 0)]
00238     qroll = [cos (y/2), (sin(y/2), 0, 0)]
00239     */
00240     float cr, cp, cy, sr, sp, sy, cpcy, spsy;  // calculate trig identities
00241         cr = cos(roll/2);
00242     cp = cos(pitch/2);
00243         cy = cos(yaw/2);
00244         sr = sin(roll/2);
00245     sp = sin(pitch/2);
00246         sy = sin(yaw/2);
00247         cpcy = cp * cy;
00248         spsy = sp * sy;
00249     quat->w = cr * cpcy + sr * spsy;
00250         quat->x = sr * cpcy - cr * spsy;
00251     quat->y = cr * sp * cy + sr * cp * sy;
00252         quat->z = cr * cp * sy - sr * sp * cy;
00253 }

VERTEX GetNorm float    x1,
float    y1,
float    z1,
float    x2,
float    y2,
float    z2,
float    x3,
float    y3,
float    z3
 

Definition at line 341 of file locmath.cpp.

References VERTEX::nx, VERTEX::ny, and VERTEX::nz.

00342 {
00343         float ux;
00344         float uy;
00345         float uz;
00346         float vx;
00347         float vy;
00348         float vz;
00349           VERTEX temp_vertex;
00350           ux = x1 - x2;
00351           uy = y1 - y2;
00352           uz = z1 - z2;
00353           vx = x3 - x2;
00354           vy = y3 - y2;
00355           vz = z3 - z2;
00356           temp_vertex.nx = (uy*vz)-(vy*uz);
00357           temp_vertex.ny = (uz*vx)-(vz*ux);
00358           temp_vertex.nz = (ux*vy)-(vx*uy);
00359           return temp_vertex;
00360 }

void LoadIdentity float    m[]
 

Definition at line 6 of file locmath.cpp.

00007 {
00008         m[0]=1.0f;
00009         m[1]=0.0f;
00010         m[2]=0.0f;
00011         m[3]=0.0f;
00012 
00013         m[4]=0.0f;
00014         m[5]=1.0f;
00015         m[6]=0.0f;
00016         m[7]=0.0f;
00017 
00018         m[8]=0.0f;
00019         m[9]=0.0f;
00020         m[10]=1.0f;
00021         m[11]=0.0f;
00022 
00023         m[12]=0.0f;
00024         m[13]=0.0f;
00025         m[14]=0.0f;
00026         m[15]=1.0f;
00027 }

float MagnitudeQuat QUAT    q1
 

Definition at line 255 of file locmath.cpp.

References QUAT::w, QUAT::x, QUAT::y, and QUAT::z.

Referenced by NormaliseQuat().

00256 {
00257       return( sqrt(q1.w*q1.w+q1.x*q1.x+q1.y*q1.y+q1.z*q1.z));
00258 }

void MatrixInverse float    m[]
 

Definition at line 135 of file locmath.cpp.

References CopyMatrix().

00136 {
00137     float n[16];
00138 
00139       CopyMatrix(n, m);
00140       m[0 ] = n[0 ];
00141       m[1 ] = n[4 ];
00142       m[2 ] = n[8 ];
00143 
00144       m[4 ] = n[1 ];
00145       m[5 ] = n[5 ];
00146       m[6 ] = n[9 ];
00147 
00148       m[8 ] = n[2 ];
00149       m[9 ] = n[6 ];
00150       m[10] = n[10];
00151 
00152       m[12] *= -1.0f;
00153       m[13] *= -1.0f;
00154       m[14] *= -1.0f;
00155 }

void MultMatrix float    m[],
float    n[]
 

Definition at line 49 of file locmath.cpp.

References CopyMatrix().

00050 {
00051         float temp[16];
00052 
00053         CopyMatrix(temp, m);
00054       m[0] = temp[0 ]*n[0 ]
00055                + temp[4 ]*n[1 ]
00056                + temp[8 ]*n[2 ]
00057                + temp[12]*n[3 ];
00058 
00059       m[1] = temp[1 ]*n[0 ]
00060                + temp[5 ]*n[1 ]
00061                + temp[9 ]*n[2 ]
00062                + temp[13]*n[3 ];
00063 
00064       m[2] = temp[2 ]*n[0 ]
00065                + temp[6 ]*n[1 ]
00066                + temp[10]*n[2 ]
00067                + temp[14]*n[3 ];
00068 
00069       m[3] = temp[3 ]*n[0 ]
00070                + temp[7 ]*n[1 ]
00071                + temp[11]*n[2 ]
00072                + temp[15]*n[3 ];
00073 
00074       m[4] = temp[0 ]*n[4 ]
00075                + temp[4 ]*n[5 ]
00076                + temp[8 ]*n[6 ]
00077                + temp[12]*n[7 ];
00078 
00079       m[5] = temp[1 ]*n[4 ]
00080                + temp[5 ]*n[5 ]
00081                + temp[9 ]*n[6 ]
00082                + temp[13]*n[7 ];
00083 
00084       m[6] = temp[2 ]*n[4 ]
00085                + temp[6 ]*n[5 ]
00086                + temp[10]*n[6 ]
00087                + temp[14]*n[7 ];
00088 
00089       m[7] = temp[3 ]*n[4 ]
00090                + temp[7 ]*n[5 ]
00091                + temp[11]*n[6 ]
00092                + temp[15]*n[7 ];
00093 
00094       m[8] = temp[0 ]*n[8 ]
00095                + temp[4 ]*n[9 ]
00096                + temp[8 ]*n[10]
00097                + temp[12]*n[11];
00098 
00099       m[9] = temp[1 ]*n[8 ]
00100                + temp[5 ]*n[9 ]
00101                + temp[9 ]*n[10]
00102                + temp[13]*n[11];
00103 
00104       m[10]= temp[2 ]*n[8 ]
00105                + temp[6 ]*n[9 ]
00106                + temp[10]*n[10]
00107                + temp[14]*n[11];
00108 
00109       m[11]= temp[3 ]*n[8 ]
00110                + temp[7 ]*n[9 ]
00111                + temp[11]*n[10]
00112                + temp[15]*n[11];
00113 
00114       m[12]= temp[0 ]*n[12]
00115                + temp[4 ]*n[13]
00116                + temp[8 ]*n[14]
00117                + temp[12]*n[15];
00118 
00119       m[13]= temp[1 ]*n[12]
00120                + temp[5 ]*n[13]
00121                + temp[9 ]*n[14]
00122                + temp[13]*n[15];
00123 
00124       m[14]= temp[2 ]*n[12]
00125                + temp[6 ]*n[13]
00126                + temp[10]*n[14]
00127                + temp[14]*n[15];
00128 
00129       m[15]= temp[3 ]*n[12]
00130                + temp[7 ]*n[13]
00131                + temp[11]*n[14]
00132                + temp[15]*n[15];
00133 }

QUAT MultQuat QUAT    q1,
QUAT    q2
 

Definition at line 306 of file locmath.cpp.

References CrossVector(), DotProduct(), NormaliseQuat(), QUAT::w, VECTOR::x, QUAT::x, VECTOR::y, QUAT::y, VECTOR::z, and QUAT::z.

00307 {
00308     /*
00309     Multiplication of two Quarternions.
00310 
00311     qq´ = [ww´ - v · v´, v x v´ + wv´ +w´v]
00312     ( · is vector dot product and x is vector cross product )
00313     */
00314       QUAT q3;
00315       VECTOR vectorq1;
00316       VECTOR vectorq2;
00317       vectorq1.x = q1.x;
00318       vectorq1.y = q1.y;
00319       vectorq1.z = q1.z;
00320       vectorq2.x = q2.x;
00321       vectorq2.y = q2.y;
00322       vectorq2.z = q2.z;
00323 
00324       VECTOR tempvec1;
00325       VECTOR tempvec2;
00326       VECTOR tempvec3;
00327       q3.w = (q1.w*q2.w) - DotProduct(vectorq1, vectorq2);
00328       tempvec1 = CrossVector(vectorq1, vectorq2);
00329       tempvec2.x = q1.w * q2.x;
00330       tempvec2.y = q1.w * q2.y;
00331       tempvec2.z = q1.w * q2.z;
00332       tempvec3.x = q2.w * q1.x;
00333       tempvec3.y = q2.w * q1.y;
00334       tempvec3.z = q2.w * q1.z;
00335       q3.x = tempvec1.x + tempvec2.x + tempvec3.x;
00336       q3.y = tempvec1.y + tempvec2.y + tempvec3.y;
00337       q3.z = tempvec1.z + tempvec2.z + tempvec3.z;
00338       return NormaliseQuat(q3);
00339 }

QUAT NormaliseQuat QUAT    q1
 

Definition at line 260 of file locmath.cpp.

References MagnitudeQuat(), QUAT::w, QUAT::x, QUAT::y, and QUAT::z.

Referenced by MultQuat().

00261 {
00262       QUAT q2;
00263       float Mag;
00264       Mag = MagnitudeQuat(q1);
00265       q2.w = q1.w/Mag;
00266       q2.x = q1.x/Mag;
00267       q2.y = q1.y/Mag;
00268       q2.z = q1.z/Mag;
00269       return q2;
00270 }

void QuatToMatrix QUAT    quat,
float    m[16]
 

Definition at line 272 of file locmath.cpp.

References QUAT::w, QUAT::x, QUAT::y, and QUAT::z.

00273 {
00274       float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
00275       // calculate coefficients
00276       x2 = quat.x + quat.x;
00277       y2 = quat.y + quat.y;
00278       z2 = quat.z + quat.z;
00279       xx = quat.x * x2;
00280       xy = quat.x * y2;
00281       xz = quat.x * z2;
00282       yy = quat.y * y2;
00283       yz = quat.y * z2;
00284       zz = quat.z * z2;
00285       wx = quat.w * x2;
00286       wy = quat.w * y2;
00287       wz = quat.w * z2;
00288       m[0] = 1.0 - (yy + zz);
00289       m[1] = xy - wz;
00290       m[2] = xz + wy;
00291       m[3] = 0.0;
00292       m[4] = xy + wz;
00293       m[5] = 1.0 - (xx + zz);
00294       m[6] = yz - wx;
00295       m[7] = 0.0;
00296       m[8] = xz - wy;
00297       m[9] = yz + wx;
00298       m[10] = 1.0 - (xx + yy);
00299       m[11] = 0.0;
00300       m[12] = 0;
00301       m[13] = 0;
00302       m[14] = 0;
00303       m[15] = 1;
00304 }


Generated on Fri Dec 23 05:18:44 2005 for Lighting by doxygen1.2.15