mathlib.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //amckern - 64bit - double to vec_t
  2. #pragma warning(disable:4244)
  3. #ifndef MATHLIB_H__
  4. #define MATHLIB_H__
  5. #if _MSC_VER >= 1000
  6. #pragma once
  7. #endif
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11. #ifdef STDC_HEADERS
  12. #include <math.h>
  13. #include <float.h>
  14. #endif
  15. #include <algorithm>
  16. #if !defined(max)
  17. #define max(a,b) (((a) > (b)) ? (a) : (b))
  18. #endif
  19. #if !defined(min)
  20. #define min(a,b) (((a) < (b)) ? (a) : (b))
  21. #endif
  22. #define Q_PI 3.14159265358979323846
  23. extern const vec3_t vec3_origin;
  24. #define NORMAL_EPSILON 0.00001
  25. #define ON_EPSILON 0.01
  26. #define EQUAL_EPSILON 0.001
  27. //
  28. // Vector Math
  29. //
  30. #define DotProduct(x,y) ( (x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2])
  31. #define CrossProduct(a, b, dest) \
  32. { \
  33. (dest)[0] = (a)[1] * (b)[2] - (a)[2] * (b)[1]; \
  34. (dest)[1] = (a)[2] * (b)[0] - (a)[0] * (b)[2]; \
  35. (dest)[2] = (a)[0] * (b)[1] - (a)[1] * (b)[0]; \
  36. }
  37. #define VectorMidpoint(a,b,c) { (c)[0]=((a)[0]+(b)[0])/2; (c)[1]=((a)[1]+(b)[1])/2; (c)[2]=((a)[2]+(b)[2])/2; }
  38. #define VectorFill(a,b) { (a)[0]=(b); (a)[1]=(b); (a)[2]=(b);}
  39. #define VectorAvg(a) ( ( (a)[0] + (a)[1] + (a)[2] ) / 3 )
  40. #define VectorSubtract(a,b,c) { (c)[0]=(a)[0]-(b)[0]; (c)[1]=(a)[1]-(b)[1]; (c)[2]=(a)[2]-(b)[2]; }
  41. #define VectorAdd(a,b,c) { (c)[0]=(a)[0]+(b)[0]; (c)[1]=(a)[1]+(b)[1]; (c)[2]=(a)[2]+(b)[2]; }
  42. #define VectorMultiply(a,b,c) { (c)[0]=(a)[0]*(b)[0]; (c)[1]=(a)[1]*(b)[1]; (c)[2]=(a)[2]*(b)[2]; }
  43. #define VectorDivide(a,b,c) { (c)[0]=(a)[0]/(b)[0]; (c)[1]=(a)[1]/(b)[1]; (c)[2]=(a)[2]/(b)[2]; }
  44. #define VectorSubtractVec(a,b,c) { (c)[0]=(a)[0]-(b); (c)[1]=(a)[1]-(b); (c)[2]=(a)[2]-(b); }
  45. #define VectorAddVec(a,b,c) { (c)[0]=(a)[0]+(b); (c)[1]=(a)[1]+(b); (c)[2]=(a)[2]+(b); }
  46. #define VecSubtractVector(a,b,c) { (c)[0]=(a)-(b)[0]; (c)[1]=(a)-(b)[1]; (c)[2]=(a)-(b)[2]; }
  47. #define VecAddVector(a,b,c) { (c)[0]=(a)+(b)[0]; (c)[1]=(a)[(b)[1]; (c)[2]=(a)+(b)[2]; }
  48. #define VectorMultiplyVec(a,b,c) { (c)[0]=(a)[0]*(b);(c)[1]=(a)[1]*(b);(c)[2]=(a)[2]*(b); }
  49. #define VectorDivideVec(a,b,c) { (c)[0]=(a)[0]/(b);(c)[1]=(a)[1]/(b);(c)[2]=(a)[2]/(b); }
  50. #define VectorScale(a,b,c) { (c)[0]=(a)[0]*(b);(c)[1]=(a)[1]*(b);(c)[2]=(a)[2]*(b); }
  51. #define VectorCopy(a,b) { (b)[0]=(a)[0]; (b)[1]=(a)[1]; (b)[2]=(a)[2]; }
  52. #define VectorClear(a) { (a)[0] = (a)[1] = (a)[2] = 0.0; }
  53. #define VectorMaximum(a) ( max( (a)[0], max( (a)[1], (a)[2] ) ) )
  54. #define VectorMinimum(a) ( min( (a)[0], min( (a)[1], (a)[2] ) ) )
  55. #define VectorInverse(a) \
  56. { \
  57. (a)[0] = -((a)[0]); \
  58. (a)[1] = -((a)[1]); \
  59. (a)[2] = -((a)[2]); \
  60. }
  61. #define VectorRound(a) floor((a) + 0.5)
  62. #define VectorMA(a, scale, b, dest) \
  63. { \
  64. (dest)[0] = (a)[0] + scale * (b)[0]; \
  65. (dest)[1] = (a)[1] + scale * (b)[1]; \
  66. (dest)[2] = (a)[2] + scale * (b)[2]; \
  67. }
  68. #define VectorLength(a) sqrt((double) ((double)((a)[0] * (a)[0]) + (double)( (a)[1] * (a)[1]) + (double)( (a)[2] * (a)[2])) )
  69. #define VectorCompareMinimum(a,b,c) { (c)[0] = min((a)[0], (b)[0]); (c)[1] = min((a)[1], (b)[1]); (c)[2] = min((a)[2], (b)[2]); }
  70. #define VectorCompareMaximum(a,b,c) { (c)[0] = max((a)[0], (b)[0]); (c)[1] = max((a)[1], (b)[1]); (c)[2] = max((a)[2], (b)[2]); }
  71. inline vec_t VectorNormalize(vec3_t v)
  72. {
  73. double length;
  74. length = DotProduct(v, v);
  75. length = sqrt(length);
  76. if (length < NORMAL_EPSILON)
  77. {
  78. VectorClear(v);
  79. return 0.0;
  80. }
  81. v[0] /= length;
  82. v[1] /= length;
  83. v[2] /= length;
  84. return length;
  85. }
  86. inline bool VectorCompare(const vec3_t v1, const vec3_t v2)
  87. {
  88. int i;
  89. for (i = 0; i < 3; i++)
  90. {
  91. if (fabs(v1[i] - v2[i]) > EQUAL_EPSILON)
  92. {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. //
  99. // Portable bit rotation
  100. //
  101. #ifdef SYSTEM_POSIX
  102. #undef rotl
  103. #undef rotr
  104. inline unsigned int rotl(unsigned value, unsigned int amt)
  105. {
  106. unsigned t1, t2;
  107. t1 = value >> ((sizeof(unsigned) * CHAR_BIT) - amt);
  108. t2 = value << amt;
  109. return (t1 | t2);
  110. }
  111. inline unsigned int rotr(unsigned value, unsigned int amt)
  112. {
  113. unsigned t1, t2;
  114. t1 = value << ((sizeof(unsigned) * CHAR_BIT) - amt);
  115. t2 = value >> amt;
  116. return (t1 | t2);
  117. }
  118. #endif
  119. //
  120. // Misc
  121. //
  122. inline bool isPointFinite(const vec_t* p)
  123. {
  124. if (finite(p[0]) && finite(p[1]) && finite(p[2]))
  125. {
  126. return true;
  127. }
  128. return false;
  129. }
  130. //
  131. // Planetype Math
  132. //
  133. typedef enum
  134. {
  135. plane_x = 0,
  136. plane_y,
  137. plane_z,
  138. plane_anyx,
  139. plane_anyy,
  140. plane_anyz
  141. }
  142. planetypes;
  143. #define last_axial plane_z
  144. inline planetypes PlaneTypeForNormal(vec3_t normal)
  145. {
  146. vec_t ax, ay, az;
  147. ax = fabs(normal[0]);
  148. if (ax == 1.0)
  149. {
  150. return plane_x;
  151. }
  152. ay = fabs(normal[1]);
  153. if (ay == 1.0)
  154. {
  155. return plane_y;
  156. }
  157. az = fabs(normal[2]);
  158. if (az == 1.0)
  159. {
  160. return plane_z;
  161. }
  162. if ((ax > ay) && (ax > az))
  163. {
  164. return plane_anyx;
  165. }
  166. if ((ay > ax) && (ay > az))
  167. {
  168. return plane_anyy;
  169. }
  170. return plane_anyz;
  171. }
  172. #endif //MATHLIB_H__