MathUtils.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #undef min
  25. #undef max
  26. #include <cmath>
  27. #include "Assert.h"
  28. #include "Types.h"
  29. #define BIT(i) (1 << i)
  30. namespace crown
  31. {
  32. /// Math utilities.
  33. namespace math
  34. {
  35. // Constants
  36. const float PI = (float)3.1415926535897932;
  37. const float TWO_PI = PI * (float)2.0;
  38. const float HALF_PI = PI * (float)0.5;
  39. const float ONEFOURTH_PI = PI * (float)0.25;
  40. const float DEG_TO_RAD = PI / (float)180.0;
  41. const float RAD_TO_DEG = (float)1.0 / DEG_TO_RAD;
  42. const float FOUR_OVER_THREE = (float)(4.0 / 3.0);
  43. const float FOUR_OVER_THREE_TIMES_PI = FOUR_OVER_THREE * PI;
  44. const float ONE_OVER_THREE = (float)(1.0 / 3.0);
  45. const float ONE_OVER_FOUR = (float)(1.0 / 4.0);
  46. const float ONE_OVER_255 = (float)(1.0 / 255.0);
  47. const float FLOAT_PRECISION = (float)1.0e-7f;
  48. const double DOUBLE_PRECISION = (float)1.0e-9;
  49. //-----------------------------------------------------------------------------
  50. inline bool equals(float a, float b, float precision = FLOAT_PRECISION)
  51. {
  52. return ((b <= (a + precision)) && (b >= (a - precision)));
  53. }
  54. //-----------------------------------------------------------------------------
  55. inline bool equals(double a, double b, double precision = DOUBLE_PRECISION)
  56. {
  57. return ((b <= (a + precision)) && (b >= (a - precision)));
  58. }
  59. //-----------------------------------------------------------------------------
  60. inline bool test_bitmask(int32_t value, int32_t bitmask)
  61. {
  62. return (value & bitmask) == bitmask;
  63. }
  64. //-----------------------------------------------------------------------------
  65. inline int32_t set_bitmask(int32_t value, int32_t bitmask)
  66. {
  67. return value | bitmask;
  68. }
  69. //-----------------------------------------------------------------------------
  70. inline int32_t unset_bitmask(int32_t value, int32_t bitmask)
  71. {
  72. return value & (~bitmask);
  73. }
  74. //-----------------------------------------------------------------------------
  75. template <typename T>
  76. inline T min(const T& a, const T& b)
  77. {
  78. return a < b ? a : b;
  79. }
  80. //-----------------------------------------------------------------------------
  81. template <typename T>
  82. inline T min(const T& a, const T& b, const T& c)
  83. {
  84. return math::min(math::min(a, b), math::min(a, c));
  85. }
  86. //-----------------------------------------------------------------------------
  87. template <typename T>
  88. inline T min(const T& a, const T& b, const T& c, const T& d)
  89. {
  90. return math::min(math::min(a, b, c), math::min(a, b, d), math::min(a, c, d));
  91. }
  92. //-----------------------------------------------------------------------------
  93. template <typename T>
  94. inline T max(const T& a, const T& b)
  95. {
  96. return a < b ? b : a;
  97. }
  98. //-----------------------------------------------------------------------------
  99. template <typename T>
  100. inline T max(const T& a, const T& b, const T& c)
  101. {
  102. return math::max(math::max(a, b), math::max(a, c));
  103. }
  104. //-----------------------------------------------------------------------------
  105. template <typename T>
  106. inline T max(const T& a, const T& b, const T& c, const T& d)
  107. {
  108. return math::max(math::max(a, b, c), math::max(a, b, d), math::max(a, c, d));
  109. }
  110. //-----------------------------------------------------------------------------
  111. template <typename T>
  112. inline T avg(const T& a, const T& b)
  113. {
  114. return (a + b) * 0.5;
  115. }
  116. //-----------------------------------------------------------------------------
  117. template <typename T>
  118. inline T avg(const T& a, const T& b, const T& c)
  119. {
  120. return (a + b + c) * ONE_OVER_THREE;
  121. }
  122. //-----------------------------------------------------------------------------
  123. template <typename T>
  124. inline T avg(const T& a, const T& b, const T& c, const T& d)
  125. {
  126. return (a + b + c + d) * ONE_OVER_FOUR;
  127. }
  128. //-----------------------------------------------------------------------------
  129. template <typename T>
  130. inline T clamp_to_range(const T& min, const T& max, const T& value)
  131. {
  132. CE_ASSERT(min < max, "Min must be < max");
  133. if (value > max)
  134. {
  135. return max;
  136. }
  137. if (value < min)
  138. {
  139. return min;
  140. }
  141. return value;
  142. }
  143. //-----------------------------------------------------------------------------
  144. template <typename T>
  145. inline void swap(T& a, T& b)
  146. {
  147. T tmp = a;
  148. a = b;
  149. b = tmp;
  150. }
  151. //-----------------------------------------------------------------------------
  152. inline float deg_to_rad(float deg)
  153. {
  154. return deg * DEG_TO_RAD;
  155. }
  156. //-----------------------------------------------------------------------------
  157. inline float rad_to_deg(float rad)
  158. {
  159. return rad * RAD_TO_DEG;
  160. }
  161. //-----------------------------------------------------------------------------
  162. inline uint32_t next_pow_2(uint32_t x)
  163. {
  164. x--;
  165. x = (x >> 1) | x;
  166. x = (x >> 2) | x;
  167. x = (x >> 4) | x;
  168. x = (x >> 8) | x;
  169. x = (x >> 16) | x;
  170. return ++x;
  171. }
  172. //-----------------------------------------------------------------------------
  173. inline bool is_pow_2(uint32_t x)
  174. {
  175. return !(x & (x - 1)) && x;
  176. }
  177. //-----------------------------------------------------------------------------
  178. inline float ceil(float x)
  179. {
  180. return ceilf(x);
  181. }
  182. //-----------------------------------------------------------------------------
  183. inline float floor(float x)
  184. {
  185. return floorf(x);
  186. }
  187. //-----------------------------------------------------------------------------
  188. inline float sqrt(float x)
  189. {
  190. return sqrtf(x);
  191. }
  192. //-----------------------------------------------------------------------------
  193. inline float inv_sqrt(float x)
  194. {
  195. return 1.0 / sqrt(x);
  196. }
  197. //-----------------------------------------------------------------------------
  198. inline float sin(float x)
  199. {
  200. return sinf(x);
  201. }
  202. //-----------------------------------------------------------------------------
  203. inline float cos(float x)
  204. {
  205. return cosf(x);
  206. }
  207. //-----------------------------------------------------------------------------
  208. inline float asin(float x)
  209. {
  210. return asinf(x);
  211. }
  212. //-----------------------------------------------------------------------------
  213. inline float acos(float x)
  214. {
  215. return acosf(x);
  216. }
  217. //-----------------------------------------------------------------------------
  218. inline float tan(float x)
  219. {
  220. return tanf(x);
  221. }
  222. //-----------------------------------------------------------------------------
  223. inline float atan2(float y, float x)
  224. {
  225. return atan2f(y, x);
  226. }
  227. //-----------------------------------------------------------------------------
  228. inline float abs(float x)
  229. {
  230. return fabs(x);
  231. }
  232. //-----------------------------------------------------------------------------
  233. inline float fmod(float n, float d)
  234. {
  235. return ::fmod(n, d);
  236. }
  237. //-----------------------------------------------------------------------------
  238. inline bool solve_quadratic_equation(float a, float b, float c, float& x1, float& x2)
  239. {
  240. float delta = (b * b) - (4.0 * a * c);
  241. // If the equation has no float solutions
  242. if (delta < 0.0)
  243. {
  244. return false;
  245. }
  246. x1 = (-b + sqrt(delta)) / (2.0 * a);
  247. x2 = (-b - sqrt(delta)) / (2.0 * a);
  248. if (x2 > x1)
  249. {
  250. swap(x1, x2);
  251. }
  252. return true;
  253. }
  254. } // namespace math
  255. } // namespace crown