mathlib.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools 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 2 Tools 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 Quake 2 Tools source code; 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 "cmdlib.h"
  20. #include "mathlib.h"
  21. vec3_t vec3_origin = {0.0f,0.0f,0.0f};
  22. float VectorLength(vec3_t v)
  23. {
  24. int i;
  25. float length;
  26. length = 0.0f;
  27. for (i=0 ; i< 3 ; i++)
  28. length += v[i]*v[i];
  29. length = (float)sqrt (length);
  30. return length;
  31. }
  32. qboolean VectorCompare (vec3_t v1, vec3_t v2)
  33. {
  34. int i;
  35. for (i=0 ; i<3 ; i++)
  36. if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
  37. return false;
  38. return true;
  39. }
  40. vec_t Q_rint (vec_t in)
  41. {
  42. return (float)floor (in + 0.5);
  43. }
  44. void VectorMA (vec3_t va, float scale, vec3_t vb, vec3_t vc)
  45. {
  46. vc[0] = va[0] + scale*vb[0];
  47. vc[1] = va[1] + scale*vb[1];
  48. vc[2] = va[2] + scale*vb[2];
  49. }
  50. void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
  51. {
  52. cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  53. cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  54. cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  55. }
  56. vec_t _DotProduct (vec3_t v1, vec3_t v2)
  57. {
  58. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  59. }
  60. void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
  61. {
  62. out[0] = va[0]-vb[0];
  63. out[1] = va[1]-vb[1];
  64. out[2] = va[2]-vb[2];
  65. }
  66. void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
  67. {
  68. out[0] = va[0]+vb[0];
  69. out[1] = va[1]+vb[1];
  70. out[2] = va[2]+vb[2];
  71. }
  72. void _VectorCopy (vec3_t in, vec3_t out)
  73. {
  74. out[0] = in[0];
  75. out[1] = in[1];
  76. out[2] = in[2];
  77. }
  78. vec_t VectorNormalize (vec3_t v)
  79. {
  80. int i;
  81. float length;
  82. length = 0.0f;
  83. for (i=0 ; i< 3 ; i++)
  84. length += v[i]*v[i];
  85. length = (float)sqrt (length);
  86. if (length == 0)
  87. return (vec_t)0;
  88. for (i=0 ; i< 3 ; i++)
  89. v[i] /= length;
  90. return length;
  91. }
  92. void VectorInverse (vec3_t v)
  93. {
  94. v[0] = -v[0];
  95. v[1] = -v[1];
  96. v[2] = -v[2];
  97. }
  98. void VectorScale (vec3_t v, vec_t scale, vec3_t out)
  99. {
  100. out[0] = v[0] * scale;
  101. out[1] = v[1] * scale;
  102. out[2] = v[2] * scale;
  103. }