home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / readmap / mathlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-29  |  3.8 KB  |  149 lines

  1. /*-------------------------------------------------------------------------
  2. File    : mathlib.c
  3. Author  : John Carmack (id Software) with modifications by C. Newham
  4. Version : 1.0
  5. Date    : 96/08/29
  6.  
  7. Description
  8. -----------
  9. Contains primatives for vector maths.
  10.  
  11. Comments
  12. --------
  13. This C version is an almost verbatim copy from Carmack's mathlib.c
  14. provided with the QuakeEd source.
  15.  
  16. CrossProduct has been modified because I found it was giving Not a Number
  17. (NaN) results for some cases of -0. Machine used: Pentium 133,
  18. Linux 1.2.13, GCC 2.7.0.  As I'm no great expert on IEEE floating point
  19. (or maths in general :) I'd appreciate any answers as to why this
  20. problem occurs (or am I seeing things?).  cam@iinet.com.au.
  21. -------------------------------------------------------------------------*/
  22.  
  23. #include "mathlib.h"
  24. #include "readmap.h"
  25. #include <math.h>
  26.  
  27.  
  28. vec3_t vec3_origin = {0,0,0};
  29.  
  30.  
  31. /*--------------------------------------------------------
  32. --------------------------------------------------------*/
  33. double VectorLength(vec3_t v)
  34. {
  35.     int        i;
  36.     double    length;
  37.     
  38.     length = 0;
  39.     for (i=0 ; i< 3 ; i++)
  40.         length += v[i]*v[i];
  41.     length = sqrt (length);        /* FIXME */
  42.  
  43.     return length;
  44. }
  45.  
  46. /*--------------------------------------------------------
  47. --------------------------------------------------------*/
  48. void VectorMA (vec3_t va, double scale, vec3_t vb, vec3_t vc)
  49. {
  50.     vc[0] = va[0] + scale*vb[0];
  51.     vc[1] = va[1] + scale*vb[1];
  52.     vc[2] = va[2] + scale*vb[2];
  53. }
  54.  
  55. /*--------------------------------------------------------
  56. --------------------------------------------------------*/
  57. int VectorCompare (vec3_t v1, vec3_t v2)
  58. {
  59.     int        i;
  60.     
  61.     for (i=0 ; i<3 ; i++)
  62.         if (v1[i] != v2[i])
  63.             return FALSE;
  64.             
  65.     return TRUE;
  66. }
  67.  
  68.  
  69. /*--------------------------------------------------------
  70. Calculates the cross product of two vectors. The cross
  71. product returns a vector perpendicular to two non-parallel
  72. vectors.
  73. --------------------------------------------------------*/
  74. void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
  75. {
  76. /* needed the checks for < MINFLOAT because it can be -0.0
  77.    which returned NaN (Not a Number) - bizarre behaviour IMHO. */
  78.  
  79.     cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  80.         if (abs(cross[0]) < MINFLOAT)
  81.           cross[0] = 0;
  82.     cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  83.         if (abs(cross[1]) < MINFLOAT)
  84.           cross[1] = 0;
  85.     cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  86.         if (abs(cross[2]) < MINFLOAT)
  87.           cross[2] = 0;
  88. }
  89.  
  90. /*--------------------------------------------------------
  91. --------------------------------------------------------*/
  92. vec_t DotProduct (vec3_t v1, vec3_t v2)
  93. {
  94.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  95. }
  96.  
  97. /*--------------------------------------------------------
  98. --------------------------------------------------------*/
  99. void VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
  100. {
  101.     out[0] = va[0]-vb[0];
  102.     out[1] = va[1]-vb[1];
  103.     out[2] = va[2]-vb[2];
  104. }
  105.  
  106. /*--------------------------------------------------------
  107. --------------------------------------------------------*/
  108. void VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
  109. {
  110.     out[0] = va[0]+vb[0];
  111.     out[1] = va[1]+vb[1];
  112.     out[2] = va[2]+vb[2];
  113. }
  114.  
  115. /*--------------------------------------------------------
  116. --------------------------------------------------------*/
  117. void VectorCopy (vec3_t in, vec3_t out)
  118. {
  119.     out[0] = in[0];
  120.     out[1] = in[1];
  121.     out[2] = in[2];
  122. }
  123.  
  124. /*--------------------------------------------------------
  125. --------------------------------------------------------*/
  126. void VectorNormalize (vec3_t v)
  127. {
  128.     int        i;
  129.     float    length;
  130.     
  131.     length = 0;
  132.     for (i=0 ; i< 3 ; i++)
  133.         length += v[i]*v[i];
  134.     length = sqrt (length);
  135.  
  136.     for (i=0 ; i< 3 ; i++)
  137.         v[i] /= length;    
  138. }
  139.  
  140. /*--------------------------------------------------------
  141. --------------------------------------------------------*/
  142. void VectorScale (vec3_t v, vec_t scale, vec3_t out)
  143. {
  144.     out[0] = v[0] * scale;
  145.     out[1] = v[1] * scale;
  146.     out[2] = v[2] * scale;
  147. }
  148.  
  149.