home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / util / mbq319 / mathlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-01  |  2.6 KB  |  144 lines

  1. // mathlib.c -- math primitives
  2.  
  3. #include "mathlib.h"
  4. #include <math.h>
  5.  
  6. vec3_t vec3_origin = {0,0,0};
  7.  
  8. boolean VectorCompare (vec3_t v1, vec3_t v2)
  9. {
  10.     int        i;
  11.     
  12.     for (i=0 ; i<3 ; i++)
  13.         if (v1[i] != v2[i])
  14.             return false;
  15.             
  16.     return true;
  17. }
  18.  
  19.  
  20. void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
  21. {
  22.     cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  23.     cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  24.     cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  25. }
  26.  
  27. vec_t DotProduct (vec3_t v1, vec3_t v2)
  28. {
  29.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  30. }
  31.  
  32. void VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
  33. {
  34.     out[0] = va[0]-vb[0];
  35.     out[1] = va[1]-vb[1];
  36.     out[2] = va[2]-vb[2];
  37. }
  38.  
  39. void VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
  40. {
  41.     out[0] = va[0]+vb[0];
  42.     out[1] = va[1]+vb[1];
  43.     out[2] = va[2]+vb[2];
  44. }
  45.  
  46. void VectorCopy (vec3_t in, vec3_t out)
  47. {
  48.     out[0] = in[0];
  49.     out[1] = in[1];
  50.     out[2] = in[2];
  51. }
  52.  
  53. void VectorNormalize (vec3_t v)
  54. {
  55.     int        i;
  56.     float    length;
  57.     
  58.     length = 0;
  59.     for (i=0 ; i< 3 ; i++)
  60.         length += v[i]*v[i];
  61.     length = sqrt (length);
  62.  
  63.     for (i=0 ; i< 3 ; i++)
  64.         v[i] /= length;    
  65. }
  66.  
  67. void VectorInverse (vec3_t v)
  68. {
  69.     v[0] = -v[0];
  70.     v[1] = -v[1];
  71.     v[2] = -v[2];
  72. }
  73.  
  74. void VectorScale (vec3_t v, vec_t scale)
  75. {
  76.     v[0] *= scale;
  77.     v[1] *= scale;
  78.     v[2] *= scale;
  79. }
  80.  
  81. /*
  82. ===================
  83. PointOnPlaneSide
  84.  
  85. Returns 1 if the point is on the same side as the normal is pointing,
  86. -1 if on the back side, or 0 if coplanar
  87. ====================
  88. */
  89.  
  90. int    PointOnPlaneSide (vec3_t pt, plane_t *plane)
  91. {
  92.     vec3_t        temp;
  93.     vec_t        dot;
  94.     
  95.     VectorSubtract (pt,plane->origin,temp);
  96.     dot = DotProduct (temp,plane->normal);
  97.  
  98.     if (dot > 0)
  99.         return SIDE_FRONT;
  100.     if (dot < 0)
  101.         return SIDE_BACK;
  102.         
  103.     return SIDE_ON;        // coplanar
  104. }
  105.  
  106. int    PointOnPlaneSideFudge (vec3_t pt, plane_t *plane)
  107. {
  108.     vec3_t        temp;
  109.     vec_t        dot;
  110.     
  111.     VectorSubtract (pt,plane->origin,temp);
  112.     dot = DotProduct (temp,plane->normal);
  113.  
  114.     if (dot > -1 && dot < 1)
  115.         return SIDE_ON;    // coplanar
  116.         
  117.     if (dot > 0)
  118.         return SIDE_FRONT;
  119.     return SIDE_BACK;
  120. }
  121.  
  122.  
  123. /*
  124. ===================
  125. LineHitPlane
  126.  
  127. Calculates the intersection point of the line and plane
  128. ====================
  129. */
  130.  
  131. void LineHitPlane (line_t *line, plane_t *plane, vec3_t point)
  132. {
  133.     vec3_t        temp;
  134.     vec_t        partial,total;
  135.     
  136.     VectorSubtract (plane->origin,line->origin,temp);
  137.     partial = DotProduct (temp,plane->normal);
  138.     total = DotProduct (line->vector,plane->normal);
  139.  
  140.     point[0] = line->origin[0] + line->vector[0]*partial/total;
  141.     point[1] = line->origin[1] + line->vector[1]*partial/total;
  142.     point[2] = line->origin[2] + line->vector[2]*partial/total;
  143. }
  144.