Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

l_math.c

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 // mathlib.c -- math primitives
00023 
00024 #include "l_cmd.h"
00025 #include "l_math.h"
00026 
00027 vec3_t vec3_origin = {0,0,0};
00028 
00029 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
00030 {
00031     float       angle;
00032     static float        sr, sp, sy, cr, cp, cy;
00033     // static to help MS compiler fp bugs
00034 
00035     angle = angles[YAW] * (M_PI*2 / 360);
00036     sy = sin(angle);
00037     cy = cos(angle);
00038     angle = angles[PITCH] * (M_PI*2 / 360);
00039     sp = sin(angle);
00040     cp = cos(angle);
00041     angle = angles[ROLL] * (M_PI*2 / 360);
00042     sr = sin(angle);
00043     cr = cos(angle);
00044 
00045     if (forward)
00046     {
00047         forward[0] = cp*cy;
00048         forward[1] = cp*sy;
00049         forward[2] = -sp;
00050     }
00051     if (right)
00052     {
00053         right[0] = (-1*sr*sp*cy+-1*cr*-sy);
00054         right[1] = (-1*sr*sp*sy+-1*cr*cy);
00055         right[2] = -1*sr*cp;
00056     }
00057     if (up)
00058     {
00059         up[0] = (cr*sp*cy+-sr*-sy);
00060         up[1] = (cr*sp*sy+-sr*cy);
00061         up[2] = cr*cp;
00062     }
00063 }
00064 
00065 /*
00066 =================
00067 RadiusFromBounds
00068 =================
00069 */
00070 float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
00071     int     i;
00072     vec3_t  corner;
00073     float   a, b;
00074 
00075     for (i=0 ; i<3 ; i++) {
00076         a = fabs( mins[i] );
00077         b = fabs( maxs[i] );
00078         corner[i] = a > b ? a : b;
00079     }
00080 
00081     return VectorLength (corner);
00082 }
00083 
00084 /*
00085 ================
00086 R_ConcatRotations
00087 ================
00088 */
00089 void R_ConcatRotations (float in1[3][3], float in2[3][3], float out[3][3])
00090 {
00091     out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
00092                 in1[0][2] * in2[2][0];
00093     out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
00094                 in1[0][2] * in2[2][1];
00095     out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
00096                 in1[0][2] * in2[2][2];
00097     out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
00098                 in1[1][2] * in2[2][0];
00099     out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
00100                 in1[1][2] * in2[2][1];
00101     out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
00102                 in1[1][2] * in2[2][2];
00103     out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
00104                 in1[2][2] * in2[2][0];
00105     out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
00106                 in1[2][2] * in2[2][1];
00107     out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
00108                 in1[2][2] * in2[2][2];
00109 }
00110 
00111 void AxisClear( vec3_t axis[3] ) {
00112     axis[0][0] = 1;
00113     axis[0][1] = 0;
00114     axis[0][2] = 0;
00115     axis[1][0] = 0;
00116     axis[1][1] = 1;
00117     axis[1][2] = 0;
00118     axis[2][0] = 0;
00119     axis[2][1] = 0;
00120     axis[2][2] = 1;
00121 }
00122 
00123 float VectorLengthSquared(vec3_t v) {
00124     return DotProduct(v, v);
00125 }
00126 
00127 double VectorLength(vec3_t v)
00128 {
00129     int     i;
00130     double  length;
00131     
00132     length = 0;
00133     for (i=0 ; i< 3 ; i++)
00134         length += v[i]*v[i];
00135     length = sqrt (length);     // FIXME
00136 
00137     return length;
00138 }
00139 
00140 qboolean VectorCompare (vec3_t v1, vec3_t v2)
00141 {
00142     int     i;
00143     
00144     for (i=0 ; i<3 ; i++)
00145         if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
00146             return false;
00147             
00148     return true;
00149 }
00150 
00151 vec_t Q_rint (vec_t in)
00152 {
00153     return floor(in + 0.5);
00154 }
00155 
00156 void CrossProduct (const vec3_t v1, const vec3_t v2, vec3_t cross)
00157 {
00158     cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
00159     cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
00160     cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
00161 }
00162 
00163 void _VectorMA (vec3_t va, double scale, vec3_t vb, vec3_t vc)
00164 {
00165     vc[0] = va[0] + scale*vb[0];
00166     vc[1] = va[1] + scale*vb[1];
00167     vc[2] = va[2] + scale*vb[2];
00168 }
00169 
00170 vec_t _DotProduct (vec3_t v1, vec3_t v2)
00171 {
00172     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
00173 }
00174 
00175 void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
00176 {
00177     out[0] = va[0]-vb[0];
00178     out[1] = va[1]-vb[1];
00179     out[2] = va[2]-vb[2];
00180 }
00181 
00182 void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
00183 {
00184     out[0] = va[0]+vb[0];
00185     out[1] = va[1]+vb[1];
00186     out[2] = va[2]+vb[2];
00187 }
00188 
00189 void _VectorCopy (vec3_t in, vec3_t out)
00190 {
00191     out[0] = in[0];
00192     out[1] = in[1];
00193     out[2] = in[2];
00194 }
00195 
00196 void _VectorScale (vec3_t v, vec_t scale, vec3_t out)
00197 {
00198     out[0] = v[0] * scale;
00199     out[1] = v[1] * scale;
00200     out[2] = v[2] * scale;
00201 }
00202 
00203 vec_t VectorNormalize(vec3_t inout)
00204 {
00205     vec_t   length, ilength;
00206 
00207     length = sqrt (inout[0]*inout[0] + inout[1]*inout[1] + inout[2]*inout[2]);
00208     if (length == 0)
00209     {
00210         VectorClear (inout);
00211         return 0;
00212     }
00213 
00214     ilength = 1.0/length;
00215     inout[0] = inout[0]*ilength;
00216     inout[1] = inout[1]*ilength;
00217     inout[2] = inout[2]*ilength;
00218 
00219     return length;
00220 }
00221 
00222 vec_t VectorNormalize2(const vec3_t in, vec3_t out)
00223 {
00224     vec_t   length, ilength;
00225 
00226     length = sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
00227     if (length == 0)
00228     {
00229         VectorClear (out);
00230         return 0;
00231     }
00232 
00233     ilength = 1.0/length;
00234     out[0] = in[0]*ilength;
00235     out[1] = in[1]*ilength;
00236     out[2] = in[2]*ilength;
00237 
00238     return length;
00239 }
00240 
00241 vec_t ColorNormalize (vec3_t in, vec3_t out)
00242 {
00243     float   max, scale;
00244 
00245     max = in[0];
00246     if (in[1] > max)
00247         max = in[1];
00248     if (in[2] > max)
00249         max = in[2];
00250 
00251     if (max == 0)
00252         return 0;
00253 
00254     scale = 1.0 / max;
00255 
00256     VectorScale (in, scale, out);
00257 
00258     return max;
00259 }
00260 
00261 
00262 
00263 void VectorInverse (vec3_t v)
00264 {
00265     v[0] = -v[0];
00266     v[1] = -v[1];
00267     v[2] = -v[2];
00268 }
00269 
00270 void ClearBounds(vec3_t mins, vec3_t maxs)
00271 {
00272     mins[0] = mins[1] = mins[2] = 99999;
00273     maxs[0] = maxs[1] = maxs[2] = -99999;
00274 }
00275 
00276 void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs)
00277 {
00278     int     i;
00279     vec_t   val;
00280 
00281     for (i=0 ; i<3 ; i++)
00282     {
00283         val = v[i];
00284         if (val < mins[i])
00285             mins[i] = val;
00286         if (val > maxs[i])
00287             maxs[i] = val;
00288     }
00289 }

Generated on Thu Aug 25 12:37:17 2005 for Quake III Arena by  doxygen 1.3.9.1