home *** CD-ROM | disk | FTP | other *** search
/ Qu-ake / Qu-ake.iso / qu_ke / editor / 017 / MATHLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-12  |  4.2 KB  |  154 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);        
  42.  
  43.     return length;
  44. }
  45.  
  46. /*--------------------------------------------------------
  47. --------------------------------------------------------*/
  48. void VectorMA (vec3_t va, float scale, vec3_t vb, vec3_t vc) //double
  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. // Cross[i] is a float value and abs is looking to get the
  79. // absolute of an int. You can take out the MINFLOAT check
  80. // 11-12-96
  81.  
  82.     cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  83.         if ((abs((int)cross[0])) < MINFLOAT)
  84.           cross[0] = 0;
  85.     cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  86.         if ((abs((int)cross[1])) < MINFLOAT)
  87.           cross[1] = 0;
  88.     cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  89.         if ((abs((int)cross[2])) < MINFLOAT)
  90.           cross[2] = 0;              
  91.     
  92. }
  93.  
  94. /*--------------------------------------------------------
  95. --------------------------------------------------------*/
  96. vec_t DotProduct (vec3_t v1, vec3_t v2)
  97. {
  98.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  99. }
  100.  
  101. /*--------------------------------------------------------
  102. --------------------------------------------------------*/
  103. void VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
  104. {
  105.     out[0] = va[0]-vb[0];
  106.     out[1] = va[1]-vb[1];
  107.     out[2] = va[2]-vb[2];
  108. }
  109.  
  110. /*--------------------------------------------------------
  111. --------------------------------------------------------*/
  112. void VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
  113. {
  114.     out[0] = va[0]+vb[0];
  115.     out[1] = va[1]+vb[1];
  116.     out[2] = va[2]+vb[2];
  117. }
  118.  
  119. /*--------------------------------------------------------
  120. --------------------------------------------------------*/
  121. void VectorCopy (vec3_t in, vec3_t out)
  122. {
  123.     out[0] = in[0];
  124.     out[1] = in[1];
  125.     out[2] = in[2];
  126. }
  127.  
  128. /*--------------------------------------------------------
  129. --------------------------------------------------------*/
  130. void VectorNormalize (vec3_t v)
  131. {
  132.     int        i;  
  133.     long double length;
  134.     //float    length;  this should probably be looked at 11-12-96
  135.     
  136.     length = 0;
  137.     for (i=0 ; i< 3 ; i++)
  138.         length += v[i]*v[i];
  139.     length =(float) sqrt ((float)length);
  140.  
  141.     for (i=0 ; i< 3 ; i++)
  142.         v[i] /=(float) length;    
  143. }
  144.  
  145. /*--------------------------------------------------------
  146. --------------------------------------------------------*/
  147. void VectorScale (vec3_t v, vec_t scale, vec3_t out)
  148. {
  149.     out[0] = v[0] * scale;
  150.     out[1] = v[1] * scale;
  151.     out[2] = v[2] * scale;
  152. }
  153.  
  154.