]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/mathlib/mathlib.c
eol style
[xonotic/netradiant.git] / libs / mathlib / mathlib.c
index 0c26a96363019b57225cbc3cb7a238879247e142..b9b7b869f18bcc087e3f7df611d57a073e02c6cd 100644 (file)
-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-*/\r
-\r
-// mathlib.c -- math primitives\r
-#include "mathlib.h"\r
-// we use memcpy and memset\r
-#include <memory.h>\r
-\r
-vec3_t vec3_origin = {0.0f,0.0f,0.0f};\r
-\r
-/*\r
-================\r
-MakeNormalVectors\r
-\r
-Given a normalized forward vector, create two\r
-other perpendicular vectors\r
-================\r
-*/\r
-void MakeNormalVectors (vec3_t forward, vec3_t right, vec3_t up)\r
-{\r
-       float           d;\r
-\r
-       // this rotate and negate guarantees a vector\r
-       // not colinear with the original\r
-       right[1] = -forward[0];\r
-       right[2] = forward[1];\r
-       right[0] = forward[2];\r
-\r
-       d = DotProduct (right, forward);\r
-       VectorMA (right, -d, forward, right);\r
-       VectorNormalize (right, right);\r
-       CrossProduct (right, forward, up);\r
-}\r
-\r
-vec_t VectorLength(vec3_t v)\r
-{\r
-       int             i;\r
-       float   length;\r
-       \r
-       length = 0.0f;\r
-       for (i=0 ; i< 3 ; i++)\r
-               length += v[i]*v[i];\r
-       length = (float)sqrt (length);\r
-\r
-       return length;\r
-}\r
-\r
-qboolean VectorCompare (vec3_t v1, vec3_t v2)\r
-{\r
-       int             i;\r
-       \r
-       for (i=0 ; i<3 ; i++)\r
-               if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)\r
-                       return qfalse;\r
-                       \r
-       return qtrue;\r
-}\r
-\r
-/*\r
-// FIXME TTimo this implementation has to be particular to radiant\r
-//   through another name I'd say\r
-vec_t Q_rint (vec_t in)\r
-{\r
-  if (g_PrefsDlg.m_bNoClamp)\r
-    return in;\r
-  else\r
-    return (float)floor (in + 0.5);\r
-}\r
-*/\r
-\r
-void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc )\r
-{\r
-       vc[0] = va[0] + scale*vb[0];\r
-       vc[1] = va[1] + scale*vb[1];\r
-       vc[2] = va[2] + scale*vb[2];\r
-}\r
-\r
-void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)\r
-{\r
-       cross[0] = v1[1]*v2[2] - v1[2]*v2[1];\r
-       cross[1] = v1[2]*v2[0] - v1[0]*v2[2];\r
-       cross[2] = v1[0]*v2[1] - v1[1]*v2[0];\r
-}\r
-\r
-vec_t _DotProduct (vec3_t v1, vec3_t v2)\r
-{\r
-       return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];\r
-}\r
-\r
-void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)\r
-{\r
-       out[0] = va[0]-vb[0];\r
-       out[1] = va[1]-vb[1];\r
-       out[2] = va[2]-vb[2];\r
-}\r
-\r
-void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)\r
-{\r
-       out[0] = va[0]+vb[0];\r
-       out[1] = va[1]+vb[1];\r
-       out[2] = va[2]+vb[2];\r
-}\r
-\r
-void _VectorCopy (vec3_t in, vec3_t out)\r
-{\r
-       out[0] = in[0];\r
-       out[1] = in[1];\r
-       out[2] = in[2];\r
-}\r
-\r
-vec_t VectorNormalize( const vec3_t in, vec3_t out ) {\r
-       vec_t   length, ilength;\r
-\r
-       length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);\r
-       if (length == 0)\r
-       {\r
-               VectorClear (out);\r
-               return 0;\r
-       }\r
-\r
-       ilength = 1.0f/length;\r
-       out[0] = in[0]*ilength;\r
-       out[1] = in[1]*ilength;\r
-       out[2] = in[2]*ilength;\r
-\r
-       return length;\r
-}\r
-\r
-vec_t ColorNormalize( const vec3_t in, vec3_t out ) {\r
-       float   max, scale;\r
-\r
-       max = in[0];\r
-       if (in[1] > max)\r
-               max = in[1];\r
-       if (in[2] > max)\r
-               max = in[2];\r
-\r
-       if (max == 0) {\r
-               out[0] = out[1] = out[2] = 1.0;\r
-               return 0;\r
-       }\r
-\r
-       scale = 1.0f / max;\r
-\r
-       VectorScale (in, scale, out);\r
-\r
-       return max;\r
-}\r
-\r
-void VectorInverse (vec3_t v)\r
-{\r
-       v[0] = -v[0];\r
-       v[1] = -v[1];\r
-       v[2] = -v[2];\r
-}\r
-\r
-/*\r
-void VectorScale (vec3_t v, vec_t scale, vec3_t out)\r
-{\r
-       out[0] = v[0] * scale;\r
-       out[1] = v[1] * scale;\r
-       out[2] = v[2] * scale;\r
-}\r
-*/\r
-\r
-void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out)\r
-{\r
-  vec3_t vWork, va;\r
-  int nIndex[3][2];\r
-  int i;\r
-\r
-  VectorCopy(vIn, va);\r
-  VectorCopy(va, vWork);\r
-  nIndex[0][0] = 1; nIndex[0][1] = 2;\r
-  nIndex[1][0] = 2; nIndex[1][1] = 0;\r
-  nIndex[2][0] = 0; nIndex[2][1] = 1;\r
-\r
-  for (i = 0; i < 3; i++)\r
-  {\r
-    if (vRotation[i] != 0)\r
-    {\r
-      float dAngle = vRotation[i] * Q_PI / 180.0f;\r
-           float c = (vec_t)cos(dAngle);\r
-      float s = (vec_t)sin(dAngle);\r
-      vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;\r
-      vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;\r
-    }\r
-    VectorCopy(vWork, va);\r
-  }\r
-  VectorCopy(vWork, out);\r
-}\r
-\r
-void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out)\r
-{\r
-  vec3_t vTemp, vTemp2;\r
-\r
-  VectorSubtract(vIn, vOrigin, vTemp);\r
-  VectorRotate(vTemp, vRotation, vTemp2);\r
-  VectorAdd(vTemp2, vOrigin, out);\r
-}\r
-\r
-void VectorPolar(vec3_t v, float radius, float theta, float phi)\r
-{\r
-       v[0]=(float)(radius * cos(theta) * cos(phi));\r
-       v[1]=(float)(radius * sin(theta) * cos(phi));\r
-       v[2]=(float)(radius * sin(phi));\r
-}\r
-\r
-void VectorSnap(vec3_t v)\r
-{\r
-  int i;\r
-  for (i = 0; i < 3; i++)\r
-  {\r
-    v[i] = (vec_t)floor (v[i] + 0.5);\r
-  }\r
-}\r
-\r
-void VectorISnap(vec3_t point, int snap)\r
-{\r
-  int i;\r
-       for (i = 0 ;i < 3 ; i++)\r
-       {\r
-               point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;\r
-       }\r
-}\r
-\r
-void VectorFSnap(vec3_t point, float snap)\r
-{\r
-  int i;\r
-       for (i = 0 ;i < 3 ; i++)\r
-       {\r
-               point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;\r
-       }\r
-}\r
-\r
-void _Vector5Add (vec5_t va, vec5_t vb, vec5_t out)\r
-{\r
-       out[0] = va[0]+vb[0];\r
-       out[1] = va[1]+vb[1];\r
-       out[2] = va[2]+vb[2];\r
-       out[3] = va[3]+vb[3];\r
-       out[4] = va[4]+vb[4];\r
-}\r
-\r
-void _Vector5Scale (vec5_t v, vec_t scale, vec5_t out)\r
-{\r
-       out[0] = v[0] * scale;\r
-       out[1] = v[1] * scale;\r
-       out[2] = v[2] * scale;\r
-       out[3] = v[3] * scale;\r
-       out[4] = v[4] * scale;\r
-}\r
-\r
-void _Vector53Copy (vec5_t in, vec3_t out)\r
-{\r
-       out[0] = in[0];\r
-       out[1] = in[1];\r
-       out[2] = in[2];\r
-}\r
-\r
-// NOTE: added these from Ritual's Q3Radiant\r
-void ClearBounds (vec3_t mins, vec3_t maxs)\r
-{\r
-       mins[0] = mins[1] = mins[2] = 99999;\r
-       maxs[0] = maxs[1] = maxs[2] = -99999;\r
-}\r
-\r
-void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)\r
-{\r
-       int             i;\r
-       vec_t   val;\r
-       \r
-       for (i=0 ; i<3 ; i++)\r
-       {\r
-               val = v[i];\r
-               if (val < mins[i])\r
-                       mins[i] = val;\r
-               if (val > maxs[i])\r
-                       maxs[i] = val;\r
-       }\r
-}\r
-\r
-#define        PITCH                           0               // up / down\r
-#define        YAW                                     1               // left / right\r
-#define        ROLL                            2               // fall over\r
-#ifndef M_PI\r
-#define M_PI           3.14159265358979323846f // matches value in gcc v2 math.h\r
-#endif\r
-\r
-void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)\r
-{\r
-       float           angle;\r
-       static float            sr, sp, sy, cr, cp, cy;\r
-       // static to help MS compiler fp bugs\r
-       \r
-       angle = angles[YAW] * (M_PI*2.0f / 360.0f);\r
-       sy = (vec_t)sin(angle);\r
-       cy = (vec_t)cos(angle);\r
-       angle = angles[PITCH] * (M_PI*2.0f / 360.0f);\r
-       sp = (vec_t)sin(angle);\r
-       cp = (vec_t)cos(angle);\r
-       angle = angles[ROLL] * (M_PI*2.0f / 360.0f);\r
-       sr = (vec_t)sin(angle);\r
-       cr = (vec_t)cos(angle);\r
-       \r
-       if (forward)\r
-       {\r
-               forward[0] = cp*cy;\r
-               forward[1] = cp*sy;\r
-               forward[2] = -sp;\r
-       }\r
-       if (right)\r
-       {\r
-               right[0] = -sr*sp*cy+cr*sy;\r
-               right[1] = -sr*sp*sy-cr*cy;\r
-               right[2] = -sr*cp;\r
-       }\r
-       if (up)\r
-       {\r
-               up[0] = cr*sp*cy+sr*sy;\r
-               up[1] = cr*sp*sy-sr*cy;\r
-               up[2] = cr*cp;\r
-       }\r
-}\r
-\r
-void VectorToAngles( vec3_t vec, vec3_t angles )\r
-{\r
-       float forward;\r
-       float yaw, pitch;\r
-       \r
-       if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) )\r
-       {\r
-               yaw = 0;\r
-               if ( vec[ 2 ] > 0 )\r
-               {\r
-                       pitch = 90;\r
-               }\r
-               else\r
-               {\r
-                       pitch = 270;\r
-               }\r
-       }\r
-       else\r
-       {\r
-               yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / M_PI;\r
-               if ( yaw < 0 )\r
-               {\r
-                       yaw += 360;\r
-               }\r
-               \r
-               forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );\r
-               pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / M_PI;\r
-               if ( pitch < 0 )\r
-               {\r
-                       pitch += 360;\r
-               }\r
-       }\r
-       \r
-       angles[ 0 ] = pitch;\r
-       angles[ 1 ] = yaw;\r
-       angles[ 2 ] = 0;\r
-}\r
-\r
-/*\r
-=====================\r
-PlaneFromPoints\r
-\r
-Returns false if the triangle is degenrate.\r
-The normal will point out of the clock for clockwise ordered points\r
-=====================\r
-*/\r
-qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {\r
-       vec3_t  d1, d2;\r
-\r
-       VectorSubtract( b, a, d1 );\r
-       VectorSubtract( c, a, d2 );\r
-       CrossProduct( d2, d1, plane );\r
-       if ( VectorNormalize( plane, plane ) == 0 ) {\r
-               return qfalse;\r
-       }\r
-\r
-       plane[3] = DotProduct( a, plane );\r
-       return qtrue;\r
-}\r
-\r
-/*\r
-** NormalToLatLong\r
-**\r
-** We use two byte encoded normals in some space critical applications.\r
-** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format\r
-** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format\r
-**\r
-*/\r
-void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {\r
-       // check for singularities\r
-       if ( normal[0] == 0 && normal[1] == 0 ) {\r
-               if ( normal[2] > 0 ) {\r
-                       bytes[0] = 0;\r
-                       bytes[1] = 0;           // lat = 0, long = 0\r
-               } else {\r
-                       bytes[0] = 128;\r
-                       bytes[1] = 0;           // lat = 0, long = 128\r
-               }\r
-       } else {\r
-               int     a, b;\r
-\r
-               a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ) );\r
-               a &= 0xff;\r
-\r
-               b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );\r
-               b &= 0xff;\r
-\r
-               bytes[0] = b;   // longitude\r
-               bytes[1] = a;   // lattitude\r
-       }\r
-}\r
-\r
-/*\r
-=================\r
-PlaneTypeForNormal\r
-=================\r
-*/\r
-int    PlaneTypeForNormal (vec3_t normal) {\r
-       if (normal[0] == 1.0 || normal[0] == -1.0)\r
-               return PLANE_X;\r
-       if (normal[1] == 1.0 || normal[1] == -1.0)\r
-               return PLANE_Y;\r
-       if (normal[2] == 1.0 || normal[2] == -1.0)\r
-               return PLANE_Z;\r
-       \r
-       return PLANE_NON_AXIAL;\r
-}\r
-\r
-/*\r
-================\r
-MatrixMultiply\r
-================\r
-*/\r
-void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {\r
-       out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +\r
-                               in1[0][2] * in2[2][0];\r
-       out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +\r
-                               in1[0][2] * in2[2][1];\r
-       out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +\r
-                               in1[0][2] * in2[2][2];\r
-       out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +\r
-                               in1[1][2] * in2[2][0];\r
-       out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +\r
-                               in1[1][2] * in2[2][1];\r
-       out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +\r
-                               in1[1][2] * in2[2][2];\r
-       out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +\r
-                               in1[2][2] * in2[2][0];\r
-       out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +\r
-                               in1[2][2] * in2[2][1];\r
-       out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +\r
-                               in1[2][2] * in2[2][2];\r
-}\r
-\r
-void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )\r
-{\r
-       float d;\r
-       vec3_t n;\r
-       float inv_denom;\r
-\r
-       inv_denom = 1.0F / DotProduct( normal, normal );\r
-\r
-       d = DotProduct( normal, p ) * inv_denom;\r
-\r
-       n[0] = normal[0] * inv_denom;\r
-       n[1] = normal[1] * inv_denom;\r
-       n[2] = normal[2] * inv_denom;\r
-\r
-       dst[0] = p[0] - d * n[0];\r
-       dst[1] = p[1] - d * n[1];\r
-       dst[2] = p[2] - d * n[2];\r
-}\r
-\r
-/*\r
-** assumes "src" is normalized\r
-*/\r
-void PerpendicularVector( vec3_t dst, const vec3_t src )\r
-{\r
-       int     pos;\r
-       int i;\r
-       vec_t minelem = 1.0F;\r
-       vec3_t tempvec;\r
-\r
-       /*\r
-       ** find the smallest magnitude axially aligned vector\r
-       */\r
-       for ( pos = 0, i = 0; i < 3; i++ )\r
-       {\r
-               if ( fabs( src[i] ) < minelem )\r
-               {\r
-                       pos = i;\r
-                       minelem = (vec_t)fabs( src[i] );\r
-               }\r
-       }\r
-       tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;\r
-       tempvec[pos] = 1.0F;\r
-\r
-       /*\r
-       ** project the point onto the plane defined by src\r
-       */\r
-       ProjectPointOnPlane( dst, tempvec, src );\r
-\r
-       /*\r
-       ** normalize the result\r
-       */\r
-       VectorNormalize( dst, dst );\r
-}\r
-\r
-/*\r
-===============\r
-RotatePointAroundVector\r
-\r
-This is not implemented very well...\r
-===============\r
-*/\r
-void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,\r
-                                                        float degrees ) {\r
-       float   m[3][3];\r
-       float   im[3][3];\r
-       float   zrot[3][3];\r
-       float   tmpmat[3][3];\r
-       float   rot[3][3];\r
-       int     i;\r
-       vec3_t vr, vup, vf;\r
-       float   rad;\r
-\r
-       vf[0] = dir[0];\r
-       vf[1] = dir[1];\r
-       vf[2] = dir[2];\r
-\r
-       PerpendicularVector( vr, dir );\r
-       CrossProduct( vr, vf, vup );\r
-\r
-       m[0][0] = vr[0];\r
-       m[1][0] = vr[1];\r
-       m[2][0] = vr[2];\r
-\r
-       m[0][1] = vup[0];\r
-       m[1][1] = vup[1];\r
-       m[2][1] = vup[2];\r
-\r
-       m[0][2] = vf[0];\r
-       m[1][2] = vf[1];\r
-       m[2][2] = vf[2];\r
-\r
-       memcpy( im, m, sizeof( im ) );\r
-\r
-       im[0][1] = m[1][0];\r
-       im[0][2] = m[2][0];\r
-       im[1][0] = m[0][1];\r
-       im[1][2] = m[2][1];\r
-       im[2][0] = m[0][2];\r
-       im[2][1] = m[1][2];\r
-\r
-       memset( zrot, 0, sizeof( zrot ) );\r
-       zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;\r
-\r
-       rad = DEG2RAD( degrees );\r
-       zrot[0][0] = (vec_t)cos( rad );\r
-       zrot[0][1] = (vec_t)sin( rad );\r
-       zrot[1][0] = (vec_t)-sin( rad );\r
-       zrot[1][1] = (vec_t)cos( rad );\r
-\r
-       MatrixMultiply( m, zrot, tmpmat );\r
-       MatrixMultiply( tmpmat, im, rot );\r
-\r
-       for ( i = 0; i < 3; i++ ) {\r
-               dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];\r
-       }\r
-}\r
+/*
+Copyright (C) 1999-2007 id Software, Inc. and contributors.
+For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+This file is part of GtkRadiant.
+
+GtkRadiant is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+GtkRadiant is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GtkRadiant; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+// mathlib.c -- math primitives
+#include "mathlib.h"
+// we use memcpy and memset
+#include <memory.h>
+
+vec3_t vec3_origin = {0.0f,0.0f,0.0f};
+
+/*
+================
+MakeNormalVectors
+
+Given a normalized forward vector, create two
+other perpendicular vectors
+================
+*/
+void MakeNormalVectors (vec3_t forward, vec3_t right, vec3_t up)
+{
+       float           d;
+
+       // this rotate and negate guarantees a vector
+       // not colinear with the original
+       right[1] = -forward[0];
+       right[2] = forward[1];
+       right[0] = forward[2];
+
+       d = DotProduct (right, forward);
+       VectorMA (right, -d, forward, right);
+       VectorNormalize (right, right);
+       CrossProduct (right, forward, up);
+}
+
+vec_t VectorLength(vec3_t v)
+{
+       int             i;
+       float   length;
+       
+       length = 0.0f;
+       for (i=0 ; i< 3 ; i++)
+               length += v[i]*v[i];
+       length = (float)sqrt (length);
+
+       return length;
+}
+
+qboolean VectorCompare (vec3_t v1, vec3_t v2)
+{
+       int             i;
+       
+       for (i=0 ; i<3 ; i++)
+               if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
+                       return qfalse;
+                       
+       return qtrue;
+}
+
+/*
+// FIXME TTimo this implementation has to be particular to radiant
+//   through another name I'd say
+vec_t Q_rint (vec_t in)
+{
+  if (g_PrefsDlg.m_bNoClamp)
+    return in;
+  else
+    return (float)floor (in + 0.5);
+}
+*/
+
+void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc )
+{
+       vc[0] = va[0] + scale*vb[0];
+       vc[1] = va[1] + scale*vb[1];
+       vc[2] = va[2] + scale*vb[2];
+}
+
+void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
+{
+       cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
+       cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
+       cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
+}
+
+vec_t _DotProduct (vec3_t v1, vec3_t v2)
+{
+       return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
+}
+
+void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
+{
+       out[0] = va[0]-vb[0];
+       out[1] = va[1]-vb[1];
+       out[2] = va[2]-vb[2];
+}
+
+void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
+{
+       out[0] = va[0]+vb[0];
+       out[1] = va[1]+vb[1];
+       out[2] = va[2]+vb[2];
+}
+
+void _VectorCopy (vec3_t in, vec3_t out)
+{
+       out[0] = in[0];
+       out[1] = in[1];
+       out[2] = in[2];
+}
+
+vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
+       vec_t   length, ilength;
+
+       length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
+       if (length == 0)
+       {
+               VectorClear (out);
+               return 0;
+       }
+
+       ilength = 1.0f/length;
+       out[0] = in[0]*ilength;
+       out[1] = in[1]*ilength;
+       out[2] = in[2]*ilength;
+
+       return length;
+}
+
+vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
+       float   max, scale;
+
+       max = in[0];
+       if (in[1] > max)
+               max = in[1];
+       if (in[2] > max)
+               max = in[2];
+
+       if (max == 0) {
+               out[0] = out[1] = out[2] = 1.0;
+               return 0;
+       }
+
+       scale = 1.0f / max;
+
+       VectorScale (in, scale, out);
+
+       return max;
+}
+
+void VectorInverse (vec3_t v)
+{
+       v[0] = -v[0];
+       v[1] = -v[1];
+       v[2] = -v[2];
+}
+
+/*
+void VectorScale (vec3_t v, vec_t scale, vec3_t out)
+{
+       out[0] = v[0] * scale;
+       out[1] = v[1] * scale;
+       out[2] = v[2] * scale;
+}
+*/
+
+void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out)
+{
+  vec3_t vWork, va;
+  int nIndex[3][2];
+  int i;
+
+  VectorCopy(vIn, va);
+  VectorCopy(va, vWork);
+  nIndex[0][0] = 1; nIndex[0][1] = 2;
+  nIndex[1][0] = 2; nIndex[1][1] = 0;
+  nIndex[2][0] = 0; nIndex[2][1] = 1;
+
+  for (i = 0; i < 3; i++)
+  {
+    if (vRotation[i] != 0)
+    {
+      float dAngle = vRotation[i] * Q_PI / 180.0f;
+           float c = (vec_t)cos(dAngle);
+      float s = (vec_t)sin(dAngle);
+      vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;
+      vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;
+    }
+    VectorCopy(vWork, va);
+  }
+  VectorCopy(vWork, out);
+}
+
+void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out)
+{
+  vec3_t vTemp, vTemp2;
+
+  VectorSubtract(vIn, vOrigin, vTemp);
+  VectorRotate(vTemp, vRotation, vTemp2);
+  VectorAdd(vTemp2, vOrigin, out);
+}
+
+void VectorPolar(vec3_t v, float radius, float theta, float phi)
+{
+       v[0]=(float)(radius * cos(theta) * cos(phi));
+       v[1]=(float)(radius * sin(theta) * cos(phi));
+       v[2]=(float)(radius * sin(phi));
+}
+
+void VectorSnap(vec3_t v)
+{
+  int i;
+  for (i = 0; i < 3; i++)
+  {
+    v[i] = (vec_t)floor (v[i] + 0.5);
+  }
+}
+
+void VectorISnap(vec3_t point, int snap)
+{
+  int i;
+       for (i = 0 ;i < 3 ; i++)
+       {
+               point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
+       }
+}
+
+void VectorFSnap(vec3_t point, float snap)
+{
+  int i;
+       for (i = 0 ;i < 3 ; i++)
+       {
+               point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
+       }
+}
+
+void _Vector5Add (vec5_t va, vec5_t vb, vec5_t out)
+{
+       out[0] = va[0]+vb[0];
+       out[1] = va[1]+vb[1];
+       out[2] = va[2]+vb[2];
+       out[3] = va[3]+vb[3];
+       out[4] = va[4]+vb[4];
+}
+
+void _Vector5Scale (vec5_t v, vec_t scale, vec5_t out)
+{
+       out[0] = v[0] * scale;
+       out[1] = v[1] * scale;
+       out[2] = v[2] * scale;
+       out[3] = v[3] * scale;
+       out[4] = v[4] * scale;
+}
+
+void _Vector53Copy (vec5_t in, vec3_t out)
+{
+       out[0] = in[0];
+       out[1] = in[1];
+       out[2] = in[2];
+}
+
+// NOTE: added these from Ritual's Q3Radiant
+void ClearBounds (vec3_t mins, vec3_t maxs)
+{
+       mins[0] = mins[1] = mins[2] = 99999;
+       maxs[0] = maxs[1] = maxs[2] = -99999;
+}
+
+void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
+{
+       int             i;
+       vec_t   val;
+       
+       for (i=0 ; i<3 ; i++)
+       {
+               val = v[i];
+               if (val < mins[i])
+                       mins[i] = val;
+               if (val > maxs[i])
+                       maxs[i] = val;
+       }
+}
+
+#define        PITCH                           0               // up / down
+#define        YAW                                     1               // left / right
+#define        ROLL                            2               // fall over
+#ifndef M_PI
+#define M_PI           3.14159265358979323846f // matches value in gcc v2 math.h
+#endif
+
+void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
+{
+       float           angle;
+       static float            sr, sp, sy, cr, cp, cy;
+       // static to help MS compiler fp bugs
+       
+       angle = angles[YAW] * (M_PI*2.0f / 360.0f);
+       sy = (vec_t)sin(angle);
+       cy = (vec_t)cos(angle);
+       angle = angles[PITCH] * (M_PI*2.0f / 360.0f);
+       sp = (vec_t)sin(angle);
+       cp = (vec_t)cos(angle);
+       angle = angles[ROLL] * (M_PI*2.0f / 360.0f);
+       sr = (vec_t)sin(angle);
+       cr = (vec_t)cos(angle);
+       
+       if (forward)
+       {
+               forward[0] = cp*cy;
+               forward[1] = cp*sy;
+               forward[2] = -sp;
+       }
+       if (right)
+       {
+               right[0] = -sr*sp*cy+cr*sy;
+               right[1] = -sr*sp*sy-cr*cy;
+               right[2] = -sr*cp;
+       }
+       if (up)
+       {
+               up[0] = cr*sp*cy+sr*sy;
+               up[1] = cr*sp*sy-sr*cy;
+               up[2] = cr*cp;
+       }
+}
+
+void VectorToAngles( vec3_t vec, vec3_t angles )
+{
+       float forward;
+       float yaw, pitch;
+       
+       if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) )
+       {
+               yaw = 0;
+               if ( vec[ 2 ] > 0 )
+               {
+                       pitch = 90;
+               }
+               else
+               {
+                       pitch = 270;
+               }
+       }
+       else
+       {
+               yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / M_PI;
+               if ( yaw < 0 )
+               {
+                       yaw += 360;
+               }
+               
+               forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
+               pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / M_PI;
+               if ( pitch < 0 )
+               {
+                       pitch += 360;
+               }
+       }
+       
+       angles[ 0 ] = pitch;
+       angles[ 1 ] = yaw;
+       angles[ 2 ] = 0;
+}
+
+/*
+=====================
+PlaneFromPoints
+
+Returns false if the triangle is degenrate.
+The normal will point out of the clock for clockwise ordered points
+=====================
+*/
+qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
+       vec3_t  d1, d2;
+
+       VectorSubtract( b, a, d1 );
+       VectorSubtract( c, a, d2 );
+       CrossProduct( d2, d1, plane );
+       if ( VectorNormalize( plane, plane ) == 0 ) {
+               return qfalse;
+       }
+
+       plane[3] = DotProduct( a, plane );
+       return qtrue;
+}
+
+/*
+** NormalToLatLong
+**
+** We use two byte encoded normals in some space critical applications.
+** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
+** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
+**
+*/
+void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {
+       // check for singularities
+       if ( normal[0] == 0 && normal[1] == 0 ) {
+               if ( normal[2] > 0 ) {
+                       bytes[0] = 0;
+                       bytes[1] = 0;           // lat = 0, long = 0
+               } else {
+                       bytes[0] = 128;
+                       bytes[1] = 0;           // lat = 0, long = 128
+               }
+       } else {
+               int     a, b;
+
+               a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ) );
+               a &= 0xff;
+
+               b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
+               b &= 0xff;
+
+               bytes[0] = b;   // longitude
+               bytes[1] = a;   // lattitude
+       }
+}
+
+/*
+=================
+PlaneTypeForNormal
+=================
+*/
+int    PlaneTypeForNormal (vec3_t normal) {
+       if (normal[0] == 1.0 || normal[0] == -1.0)
+               return PLANE_X;
+       if (normal[1] == 1.0 || normal[1] == -1.0)
+               return PLANE_Y;
+       if (normal[2] == 1.0 || normal[2] == -1.0)
+               return PLANE_Z;
+       
+       return PLANE_NON_AXIAL;
+}
+
+/*
+================
+MatrixMultiply
+================
+*/
+void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
+       out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
+                               in1[0][2] * in2[2][0];
+       out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
+                               in1[0][2] * in2[2][1];
+       out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
+                               in1[0][2] * in2[2][2];
+       out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
+                               in1[1][2] * in2[2][0];
+       out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
+                               in1[1][2] * in2[2][1];
+       out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
+                               in1[1][2] * in2[2][2];
+       out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
+                               in1[2][2] * in2[2][0];
+       out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
+                               in1[2][2] * in2[2][1];
+       out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
+                               in1[2][2] * in2[2][2];
+}
+
+void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
+{
+       float d;
+       vec3_t n;
+       float inv_denom;
+
+       inv_denom = 1.0F / DotProduct( normal, normal );
+
+       d = DotProduct( normal, p ) * inv_denom;
+
+       n[0] = normal[0] * inv_denom;
+       n[1] = normal[1] * inv_denom;
+       n[2] = normal[2] * inv_denom;
+
+       dst[0] = p[0] - d * n[0];
+       dst[1] = p[1] - d * n[1];
+       dst[2] = p[2] - d * n[2];
+}
+
+/*
+** assumes "src" is normalized
+*/
+void PerpendicularVector( vec3_t dst, const vec3_t src )
+{
+       int     pos;
+       int i;
+       vec_t minelem = 1.0F;
+       vec3_t tempvec;
+
+       /*
+       ** find the smallest magnitude axially aligned vector
+       */
+       for ( pos = 0, i = 0; i < 3; i++ )
+       {
+               if ( fabs( src[i] ) < minelem )
+               {
+                       pos = i;
+                       minelem = (vec_t)fabs( src[i] );
+               }
+       }
+       tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
+       tempvec[pos] = 1.0F;
+
+       /*
+       ** project the point onto the plane defined by src
+       */
+       ProjectPointOnPlane( dst, tempvec, src );
+
+       /*
+       ** normalize the result
+       */
+       VectorNormalize( dst, dst );
+}
+
+/*
+===============
+RotatePointAroundVector
+
+This is not implemented very well...
+===============
+*/
+void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
+                                                        float degrees ) {
+       float   m[3][3];
+       float   im[3][3];
+       float   zrot[3][3];
+       float   tmpmat[3][3];
+       float   rot[3][3];
+       int     i;
+       vec3_t vr, vup, vf;
+       float   rad;
+
+       vf[0] = dir[0];
+       vf[1] = dir[1];
+       vf[2] = dir[2];
+
+       PerpendicularVector( vr, dir );
+       CrossProduct( vr, vf, vup );
+
+       m[0][0] = vr[0];
+       m[1][0] = vr[1];
+       m[2][0] = vr[2];
+
+       m[0][1] = vup[0];
+       m[1][1] = vup[1];
+       m[2][1] = vup[2];
+
+       m[0][2] = vf[0];
+       m[1][2] = vf[1];
+       m[2][2] = vf[2];
+
+       memcpy( im, m, sizeof( im ) );
+
+       im[0][1] = m[1][0];
+       im[0][2] = m[2][0];
+       im[1][0] = m[0][1];
+       im[1][2] = m[2][1];
+       im[2][0] = m[0][2];
+       im[2][1] = m[1][2];
+
+       memset( zrot, 0, sizeof( zrot ) );
+       zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
+
+       rad = DEG2RAD( degrees );
+       zrot[0][0] = (vec_t)cos( rad );
+       zrot[0][1] = (vec_t)sin( rad );
+       zrot[1][0] = (vec_t)-sin( rad );
+       zrot[1][1] = (vec_t)cos( rad );
+
+       MatrixMultiply( m, zrot, tmpmat );
+       MatrixMultiply( tmpmat, im, rot );
+
+       for ( i = 0; i < 3; i++ ) {
+               dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
+       }
+}