l_math.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. // mathlib.c -- math primitives
  19. #include "l_cmd.h"
  20. #include "l_math.h"
  21. vec3_t vec3_origin = {0,0,0};
  22. void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
  23. {
  24. float angle;
  25. static float sr, sp, sy, cr, cp, cy;
  26. // static to help MS compiler fp bugs
  27. angle = angles[YAW] * (M_PI*2 / 360);
  28. sy = sin(angle);
  29. cy = cos(angle);
  30. angle = angles[PITCH] * (M_PI*2 / 360);
  31. sp = sin(angle);
  32. cp = cos(angle);
  33. angle = angles[ROLL] * (M_PI*2 / 360);
  34. sr = sin(angle);
  35. cr = cos(angle);
  36. if (forward)
  37. {
  38. forward[0] = cp*cy;
  39. forward[1] = cp*sy;
  40. forward[2] = -sp;
  41. }
  42. if (right)
  43. {
  44. right[0] = (-1*sr*sp*cy+-1*cr*-sy);
  45. right[1] = (-1*sr*sp*sy+-1*cr*cy);
  46. right[2] = -1*sr*cp;
  47. }
  48. if (up)
  49. {
  50. up[0] = (cr*sp*cy+-sr*-sy);
  51. up[1] = (cr*sp*sy+-sr*cy);
  52. up[2] = cr*cp;
  53. }
  54. }
  55. /*
  56. =================
  57. RadiusFromBounds
  58. =================
  59. */
  60. float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
  61. int i;
  62. vec3_t corner;
  63. float a, b;
  64. for (i=0 ; i<3 ; i++) {
  65. a = fabs( mins[i] );
  66. b = fabs( maxs[i] );
  67. corner[i] = a > b ? a : b;
  68. }
  69. return VectorLength (corner);
  70. }
  71. /*
  72. ================
  73. R_ConcatRotations
  74. ================
  75. */
  76. void R_ConcatRotations (float in1[3][3], float in2[3][3], float out[3][3])
  77. {
  78. out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  79. in1[0][2] * in2[2][0];
  80. out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  81. in1[0][2] * in2[2][1];
  82. out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  83. in1[0][2] * in2[2][2];
  84. out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  85. in1[1][2] * in2[2][0];
  86. out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  87. in1[1][2] * in2[2][1];
  88. out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  89. in1[1][2] * in2[2][2];
  90. out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  91. in1[2][2] * in2[2][0];
  92. out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  93. in1[2][2] * in2[2][1];
  94. out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  95. in1[2][2] * in2[2][2];
  96. }
  97. void AxisClear( vec3_t axis[3] ) {
  98. axis[0][0] = 1;
  99. axis[0][1] = 0;
  100. axis[0][2] = 0;
  101. axis[1][0] = 0;
  102. axis[1][1] = 1;
  103. axis[1][2] = 0;
  104. axis[2][0] = 0;
  105. axis[2][1] = 0;
  106. axis[2][2] = 1;
  107. }
  108. float VectorLengthSquared(vec3_t v) {
  109. return DotProduct(v, v);
  110. }
  111. double VectorLength(vec3_t v)
  112. {
  113. int i;
  114. double length;
  115. length = 0;
  116. for (i=0 ; i< 3 ; i++)
  117. length += v[i]*v[i];
  118. length = sqrt (length); // FIXME
  119. return length;
  120. }
  121. qboolean VectorCompare (vec3_t v1, vec3_t v2)
  122. {
  123. int i;
  124. for (i=0 ; i<3 ; i++)
  125. if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
  126. return false;
  127. return true;
  128. }
  129. vec_t Q_rint (vec_t in)
  130. {
  131. return floor(in + 0.5);
  132. }
  133. void CrossProduct (const vec3_t v1, const vec3_t v2, vec3_t cross)
  134. {
  135. cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  136. cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  137. cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  138. }
  139. void _VectorMA (vec3_t va, double scale, vec3_t vb, vec3_t vc)
  140. {
  141. vc[0] = va[0] + scale*vb[0];
  142. vc[1] = va[1] + scale*vb[1];
  143. vc[2] = va[2] + scale*vb[2];
  144. }
  145. vec_t _DotProduct (vec3_t v1, vec3_t v2)
  146. {
  147. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  148. }
  149. void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
  150. {
  151. out[0] = va[0]-vb[0];
  152. out[1] = va[1]-vb[1];
  153. out[2] = va[2]-vb[2];
  154. }
  155. void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
  156. {
  157. out[0] = va[0]+vb[0];
  158. out[1] = va[1]+vb[1];
  159. out[2] = va[2]+vb[2];
  160. }
  161. void _VectorCopy (vec3_t in, vec3_t out)
  162. {
  163. out[0] = in[0];
  164. out[1] = in[1];
  165. out[2] = in[2];
  166. }
  167. void _VectorScale (vec3_t v, vec_t scale, vec3_t out)
  168. {
  169. out[0] = v[0] * scale;
  170. out[1] = v[1] * scale;
  171. out[2] = v[2] * scale;
  172. }
  173. vec_t VectorNormalize(vec3_t inout)
  174. {
  175. vec_t length, ilength;
  176. length = sqrt (inout[0]*inout[0] + inout[1]*inout[1] + inout[2]*inout[2]);
  177. if (length == 0)
  178. {
  179. VectorClear (inout);
  180. return 0;
  181. }
  182. ilength = 1.0/length;
  183. inout[0] = inout[0]*ilength;
  184. inout[1] = inout[1]*ilength;
  185. inout[2] = inout[2]*ilength;
  186. return length;
  187. }
  188. vec_t VectorNormalize2(const vec3_t in, vec3_t out)
  189. {
  190. vec_t length, ilength;
  191. length = sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
  192. if (length == 0)
  193. {
  194. VectorClear (out);
  195. return 0;
  196. }
  197. ilength = 1.0/length;
  198. out[0] = in[0]*ilength;
  199. out[1] = in[1]*ilength;
  200. out[2] = in[2]*ilength;
  201. return length;
  202. }
  203. vec_t ColorNormalize (vec3_t in, vec3_t out)
  204. {
  205. float max, scale;
  206. max = in[0];
  207. if (in[1] > max)
  208. max = in[1];
  209. if (in[2] > max)
  210. max = in[2];
  211. if (max == 0)
  212. return 0;
  213. scale = 1.0 / max;
  214. VectorScale (in, scale, out);
  215. return max;
  216. }
  217. void VectorInverse (vec3_t v)
  218. {
  219. v[0] = -v[0];
  220. v[1] = -v[1];
  221. v[2] = -v[2];
  222. }
  223. void ClearBounds(vec3_t mins, vec3_t maxs)
  224. {
  225. mins[0] = mins[1] = mins[2] = 99999;
  226. maxs[0] = maxs[1] = maxs[2] = -99999;
  227. }
  228. void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs)
  229. {
  230. int i;
  231. vec_t val;
  232. for (i=0 ; i<3 ; i++)
  233. {
  234. val = v[i];
  235. if (val < mins[i])
  236. mins[i] = val;
  237. if (val > maxs[i])
  238. maxs[i] = val;
  239. }
  240. }