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

math.cpp

Go to the documentation of this file.
00001 void LoadIdentity(float m[])
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 }
00023 
00024 void CopyMatrix(float m[], float n[])
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 }
00043 
00044 void MultMatrix(float m[], float n[])
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 }
00129 
00130 void MatrixInverse(float m[])
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 }
00151 
00152     /* The following routine converts an angle and a unit axis vector
00153         * to a matrix, returning the corresponding unit quaternion at no
00154         * extra cost. It is written in such a way as to allow both fixed
00155         * point and floating point versions to be created by appropriate
00156         * definitions of FPOINT, ANGLE, VECTOR, QUAT, MATRIX, MUL, HALF,
00157         * TWICE, COS, SIN, ONE, and ZERO.
00158         * The following is an example of floating point definitions.*/
00159 QUAT AxisAngleToMatrix(VECTOR axis, float theta, float m[16])
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 }
00188 
00189 float DotProduct(VECTOR vec1, VECTOR vec2)
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 }
00203 
00204 VECTOR CrossVector(VECTOR vec1, VECTOR vec2)
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 }
00223 
00224 void EulerToQuat(float roll, float pitch, float yaw, QUAT * quat)
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 }
00249 
00250 float MagnitudeQuat(QUAT q1)
00251 {
00252       return( sqrt(q1.w*q1.w+q1.x*q1.x+q1.y*q1.y+q1.z*q1.z));
00253 }
00254 
00255 QUAT NormaliseQuat(QUAT q1)
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 }
00266 
00267 void QuatToMatrix(QUAT quat, float m[16])
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 }
00300 
00301 QUAT MultQuat(QUAT q1, QUAT q2)
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 }
00335 
00336 VERTEX GetNorm(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)
00337 {
00338         float ux;
00339         float uy;
00340         float uz;
00341         float vx;
00342         float vy;
00343         float vz;
00344           VERTEX temp_vertex;
00345           ux = x1 - x2;
00346           uy = y1 - y2;
00347           uz = z1 - z2;
00348           vx = x3 - x2;
00349           vy = y3 - y2;
00350           vz = z3 - z2;
00351           temp_vertex.nx = (uy*vz)-(vy*uz);
00352           temp_vertex.ny = (uz*vx)-(vz*ux);
00353           temp_vertex.nz = (ux*vy)-(vx*uy);
00354           return temp_vertex;
00355 }
00356 
00357 float MagnitudeVector(VECTOR vec1)
00358 {
00359   return(sqrt(vec1.x*vec1.x+vec1.y*vec1.y+vec1.z*vec1.z));
00360 }
00361 
00362 VECTOR GetUnitVector(VECTOR vector)
00363 {
00364     // Reduces a normal vector specified as a set of three coordinates,
00365     // to a unit normal vector of length one.
00366 
00367     // Calculate the length of the vector        
00368     float length = (float) sqrt(( vector.x * vector.x) + 
00369                                 ( vector.y * vector.y) +
00370                                 ( vector.z * vector.z) );
00371 
00372     // Keep the program from blowing up by providing an exceptable
00373     // value for vectors that may calculated too close to zero.
00374     if(length == 0.0f)
00375         length = 1.0f;
00376 
00377     // Dividing each element by the length will result in a
00378     // unit normal vector.
00379     vector.x /= length;
00380     vector.y /= length;
00381     vector.z /= length;
00382         return vector;
00383 }
00384 
00385 VECTOR GetEdgeVector(VECTOR point1, VECTOR point2)
00386 {
00387   VECTOR temp_vector;
00388   temp_vector.x = point1.x - point2.x;
00389   temp_vector.y = point1.y - point2.y;
00390   temp_vector.z = point1.z - point2.z;
00391   return temp_vector;
00392 }

Generated on Fri Dec 23 05:20:39 2005 for Potentially Visible Sets by doxygen1.2.15