mathlib.h 6.0 KB

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