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

math.cpp File Reference

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)
VECTOR GetNormal (VECTOR vertex1, VECTOR vertex2, VECTOR vertex3)
VERTEX GetNorm (float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)
float MagnitudeVector (VECTOR vec1)
VECTOR GetUnitVector (VECTOR vector)
VECTOR GetEdgeVector (VECTOR point1, VECTOR point2)


Function Documentation

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

Definition at line 159 of file math.cpp.

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

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

void CopyMatrix float    m[],
float    n[]
 

Definition at line 24 of file math.cpp.

Referenced by MatrixInverse(), and MultMatrix().

00025 {
00026         m[0 ] = n[0 ];
00027         m[1 ] = n[1 ];
00028         m[2 ] = n[2 ];
00029         m[3 ] = n[3 ];
00030         m[4 ] = n[4 ];
00031         m[5 ] = n[5 ];
00032         m[6 ] = n[6 ];
00033         m[7 ] = n[7 ];
00034         m[8 ] = n[8 ];
00035         m[9 ] = n[9 ];
00036         m[10] = n[10];
00037         m[11] = n[11];
00038         m[12] = n[12];
00039         m[13] = n[13];
00040         m[14] = n[14];
00041         m[15] = n[15];
00042 }

VECTOR CrossVector VECTOR    vec1,
VECTOR    vec2
 

Definition at line 204 of file math.cpp.

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

Referenced by MultQuat().

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

float DotProduct VECTOR    vec1,
VECTOR    vec2
 

Definition at line 189 of file math.cpp.

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

Referenced by CheckForCollision(), CheckPointInTriangle(), ClassifyPoint(), ClosestPointOnLine(), IntersectRayPlane(), IntersectRaySphere(), and MultQuat().

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

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

Definition at line 224 of file math.cpp.

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

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

VECTOR GetEdgeVector VECTOR    point1,
VECTOR    point2
 

Definition at line 403 of file math.cpp.

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

00404 {
00405   VECTOR temp_vector;
00406   temp_vector.x = point1.x - point2.x;
00407   temp_vector.y = point1.y - point2.y;
00408   temp_vector.z = point1.z - point2.z;
00409   return temp_vector;
00410 }

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

Definition at line 354 of file math.cpp.

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

00355 {
00356         float ux;
00357         float uy;
00358         float uz;
00359         float vx;
00360         float vy;
00361         float vz;
00362           VERTEX temp_vertex;
00363           ux = x1 - x2;
00364           uy = y1 - y2;
00365           uz = z1 - z2;
00366           vx = x3 - x2;
00367           vy = y3 - y2;
00368           vz = z3 - z2;
00369           temp_vertex.normal.x = (uy*vz)-(vy*uz);
00370           temp_vertex.normal.y = (uz*vx)-(vz*ux);
00371           temp_vertex.normal.z = (ux*vy)-(vx*uy);
00372           return temp_vertex;
00373 }

VECTOR GetNormal VECTOR    vertex1,
VECTOR    vertex2,
VECTOR    vertex3
 

Definition at line 337 of file math.cpp.

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

Referenced by DrawSkybox().

00338 {
00339         float ux, uy, uz, vx, vy, vz;
00340           VECTOR temp_vertex;
00341 
00342           ux = vertex1.x - vertex2.x;
00343           uy = vertex1.y - vertex2.y;
00344           uz = vertex1.z - vertex2.z;
00345           vx = vertex3.x - vertex2.x;
00346           vy = vertex3.y - vertex2.y;
00347           vz = vertex3.z - vertex2.z;
00348           temp_vertex.x = (uy*vz)-(vy*uz);
00349           temp_vertex.y = (uz*vx)-(vz*ux);
00350           temp_vertex.z = (ux*vy)-(vx*uy);
00351           return temp_vertex;
00352 }

VECTOR GetUnitVector VECTOR    vector
 

Definition at line 380 of file math.cpp.

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

Referenced by CheckForCollision(), CheckPointInTriangle(), ClosestPointOnLine(), and TangentPlaneNormalOfEllipsoid().

00381 {
00382     // Reduces a normal vector specified as a set of three coordinates,
00383     // to a unit normal vector of length one.
00384 
00385     // Calculate the length of the vector        
00386     float length = (float) sqrt(( vector.x * vector.x) + 
00387                                 ( vector.y * vector.y) +
00388                                 ( vector.z * vector.z) );
00389 
00390     // Keep the program from blowing up by providing an exceptable
00391     // value for vectors that may calculated too close to zero.
00392     if(length == 0.0f)
00393         length = 1.0f;
00394 
00395     // Dividing each element by the length will result in a
00396     // unit normal vector.
00397     vector.x /= length;
00398     vector.y /= length;
00399     vector.z /= length;
00400         return vector;
00401 }

void LoadIdentity float    m[]
 

Definition at line 1 of file math.cpp.

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

float MagnitudeQuat QUAT    q1
 

Definition at line 250 of file math.cpp.

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

Referenced by NormaliseQuat().

00251 {
00252       return( sqrt(q1.w*q1.w+q1.x*q1.x+q1.y*q1.y+q1.z*q1.z));
00253 }

float MagnitudeVector VECTOR    vec1
 

Definition at line 375 of file math.cpp.

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

Referenced by CheckClipPlanes(), CheckForCollision(), CheckPointInSphere(), ClosestPointOnLine(), ClosestPointOnPolygon(), and IntersectRaySphere().

00376 {
00377   return(sqrt(vec1.x*vec1.x+vec1.y*vec1.y+vec1.z*vec1.z));
00378 }

void MatrixInverse float    m[]
 

Definition at line 130 of file math.cpp.

References CopyMatrix().

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

void MultMatrix float    m[],
float    n[]
 

Definition at line 44 of file math.cpp.

References CopyMatrix().

Referenced by CheckClipPlanes().

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

QUAT MultQuat QUAT    q1,
QUAT    q2
 

Definition at line 301 of file math.cpp.

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

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

QUAT NormaliseQuat QUAT    q1
 

Definition at line 255 of file math.cpp.

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

Referenced by MultQuat().

00256 {
00257       QUAT q2;
00258       float Mag;
00259       Mag = MagnitudeQuat(q1);
00260       q2.w = q1.w/Mag;
00261       q2.x = q1.x/Mag;
00262       q2.y = q1.y/Mag;
00263       q2.z = q1.z/Mag;
00264       return q2;
00265 }

void QuatToMatrix QUAT    quat,
float    m[16]
 

Definition at line 267 of file math.cpp.

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

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


Generated on Fri Dec 23 05:21:00 2005 for Polygon Selection by doxygen1.2.15