gim_math.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef GIM_MATH_H_INCLUDED
  2. #define GIM_MATH_H_INCLUDED
  3. /*! \file gim_math.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. -----------------------------------------------------------------------------
  8. This source file is part of GIMPACT Library.
  9. For the latest info, see http://gimpact.sourceforge.net/
  10. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  11. email: projectileman@yahoo.com
  12. This library is free software; you can redistribute it and/or
  13. modify it under the terms of EITHER:
  14. (1) The GNU Lesser General Public License as published by the Free
  15. Software Foundation; either version 2.1 of the License, or (at
  16. your option) any later version. The text of the GNU Lesser
  17. General Public License is included with this library in the
  18. file GIMPACT-LICENSE-LGPL.TXT.
  19. (2) The BSD-style license that is included with this library in
  20. the file GIMPACT-LICENSE-BSD.TXT.
  21. (3) The zlib/libpng license that is included with this library in
  22. the file GIMPACT-LICENSE-ZLIB.TXT.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  26. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  27. -----------------------------------------------------------------------------
  28. */
  29. #include "bullet/LinearMath/btScalar.h"
  30. #define GREAL btScalar
  31. #define GREAL2 double
  32. #define GINT int
  33. #define GUINT unsigned int
  34. #define GSHORT short
  35. #define GUSHORT unsigned short
  36. #define GINT64 long long
  37. #define GUINT64 unsigned long long
  38. #define G_PI 3.14159265358979f
  39. #define G_HALF_PI 1.5707963f
  40. //267948966
  41. #define G_TWO_PI 6.28318530f
  42. //71795864
  43. #define G_ROOT3 1.73205f
  44. #define G_ROOT2 1.41421f
  45. #define G_UINT_INFINITY 0xffffffff //!< A very very high value
  46. #define G_REAL_INFINITY FLT_MAX
  47. #define G_SIGN_BITMASK 0x80000000
  48. #define G_EPSILON SIMD_EPSILON
  49. enum GIM_SCALAR_TYPES
  50. {
  51. G_STYPE_REAL =0,
  52. G_STYPE_REAL2,
  53. G_STYPE_SHORT,
  54. G_STYPE_USHORT,
  55. G_STYPE_INT,
  56. G_STYPE_UINT,
  57. G_STYPE_INT64,
  58. G_STYPE_UINT64
  59. };
  60. #define G_DEGTORAD(X) ((X)*3.1415926f/180.0f)
  61. #define G_RADTODEG(X) ((X)*180.0f/3.1415926f)
  62. //! Integer representation of a floating-point value.
  63. #define GIM_IR(x) ((GUINT&)(x))
  64. //! Signed integer representation of a floating-point value.
  65. #define GIM_SIR(x) ((GINT&)(x))
  66. //! Absolute integer representation of a floating-point value
  67. #define GIM_AIR(x) (GIM_IR(x)&0x7fffffff)
  68. //! Floating-point representation of an integer value.
  69. #define GIM_FR(x) ((GREAL&)(x))
  70. #define GIM_MAX(a,b) (a<b?b:a)
  71. #define GIM_MIN(a,b) (a>b?b:a)
  72. #define GIM_MAX3(a,b,c) GIM_MAX(a,GIM_MAX(b,c))
  73. #define GIM_MIN3(a,b,c) GIM_MIN(a,GIM_MIN(b,c))
  74. #define GIM_IS_ZERO(value) (value < G_EPSILON && value > -G_EPSILON)
  75. #define GIM_IS_NEGATIVE(value) (value <= -G_EPSILON)
  76. #define GIM_IS_POSISITVE(value) (value >= G_EPSILON)
  77. #define GIM_NEAR_EQUAL(v1,v2) GIM_IS_ZERO((v1-v2))
  78. ///returns a clamped number
  79. #define GIM_CLAMP(number,minval,maxval) (number<minval?minval:(number>maxval?maxval:number))
  80. #define GIM_GREATER(x, y) btFabs(x) > (y)
  81. ///Swap numbers
  82. #define GIM_SWAP_NUMBERS(a,b){ \
  83. a = a+b; \
  84. b = a-b; \
  85. a = a-b; \
  86. }\
  87. #define GIM_INV_SQRT(va,isva)\
  88. {\
  89. if(va<=0.0000001f)\
  90. {\
  91. isva = G_REAL_INFINITY;\
  92. }\
  93. else\
  94. {\
  95. GREAL _x = va * 0.5f;\
  96. GUINT _y = 0x5f3759df - ( GIM_IR(va) >> 1);\
  97. isva = GIM_FR(_y);\
  98. isva = isva * ( 1.5f - ( _x * isva * isva ) );\
  99. }\
  100. }\
  101. #define GIM_SQRT(va,sva)\
  102. {\
  103. GIM_INV_SQRT(va,sva);\
  104. sva = 1.0f/sva;\
  105. }\
  106. //! Computes 1.0f / sqrtf(x). Comes from Quake3. See http://www.magic-software.com/3DGEDInvSqrt.html
  107. inline GREAL gim_inv_sqrt(GREAL f)
  108. {
  109. GREAL r;
  110. GIM_INV_SQRT(f,r);
  111. return r;
  112. }
  113. inline GREAL gim_sqrt(GREAL f)
  114. {
  115. GREAL r;
  116. GIM_SQRT(f,r);
  117. return r;
  118. }
  119. #endif // GIM_MATH_H_INCLUDED