math_utils.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "assert.h"
  25. #include "types.h"
  26. #include <math.h>
  27. namespace crown
  28. {
  29. /// Math utilities.
  30. ///
  31. /// @ingroup Math
  32. namespace math
  33. {
  34. // Constants
  35. const float PI = 3.1415926535897932f;
  36. const float TWO_PI = PI * 2.0f;
  37. const float HALF_PI = PI * 0.5f;
  38. const float FLOAT_PRECISION = 1.0e-7f;
  39. const double DOUBLE_PRECISION = 1.0e-9;
  40. inline bool equals(float a, float b, float precision = FLOAT_PRECISION)
  41. {
  42. return ((b <= (a + precision)) && (b >= (a - precision)));
  43. }
  44. inline bool equals(double a, double b, double precision = DOUBLE_PRECISION)
  45. {
  46. return ((b <= (a + precision)) && (b >= (a - precision)));
  47. }
  48. template <typename T>
  49. inline T min(const T& a, const T& b)
  50. {
  51. return a < b ? a : b;
  52. }
  53. template <typename T>
  54. inline T max(const T& a, const T& b)
  55. {
  56. return a < b ? b : a;
  57. }
  58. template <typename T>
  59. inline T clamp(const T& min, const T& max, const T& val)
  60. {
  61. CE_ASSERT(min < max, "Min must be < max");
  62. return val > max ? max : val < min ? min : val;
  63. }
  64. template <typename T>
  65. inline void swap(T& a, T& b)
  66. {
  67. T tmp = a;
  68. a = b;
  69. b = tmp;
  70. }
  71. inline float to_rad(float deg)
  72. {
  73. return deg * float(PI / 180.0);
  74. }
  75. inline float to_deg(float rad)
  76. {
  77. return rad * float(180.0 / PI);
  78. }
  79. inline uint32_t next_pow_2(uint32_t x)
  80. {
  81. x--;
  82. x = (x >> 1) | x;
  83. x = (x >> 2) | x;
  84. x = (x >> 4) | x;
  85. x = (x >> 8) | x;
  86. x = (x >> 16) | x;
  87. return ++x;
  88. }
  89. inline bool is_pow_2(uint32_t x)
  90. {
  91. return !(x & (x - 1)) && x;
  92. }
  93. inline float ceil(float x)
  94. {
  95. return ceilf(x);
  96. }
  97. inline float floor(float x)
  98. {
  99. return floorf(x);
  100. }
  101. inline float sqrt(float x)
  102. {
  103. return sqrtf(x);
  104. }
  105. inline float inv_sqrt(float x)
  106. {
  107. return 1.0f / sqrt(x);
  108. }
  109. inline float sin(float x)
  110. {
  111. return sinf(x);
  112. }
  113. inline float cos(float x)
  114. {
  115. return cosf(x);
  116. }
  117. inline float asin(float x)
  118. {
  119. return asinf(x);
  120. }
  121. inline float acos(float x)
  122. {
  123. return acosf(x);
  124. }
  125. inline float tan(float x)
  126. {
  127. return tanf(x);
  128. }
  129. inline float atan2(float y, float x)
  130. {
  131. return atan2f(y, x);
  132. }
  133. inline float abs(float x)
  134. {
  135. return fabs(x);
  136. }
  137. inline float fmod(float n, float d)
  138. {
  139. return ::fmod(n, d);
  140. }
  141. /// Returns the linear interpolated value between @a p0 and @a p1 at time @a t
  142. template <typename T>
  143. inline T linear(const T& p0, const T& p1, float t)
  144. {
  145. return p0 + (t * (p1 - p0));
  146. }
  147. /// Returns the cosine interpolated value between @a p0 and @a p1 at time @a t
  148. template <typename T>
  149. inline T cosine(const T& p0, const T& p1, float t)
  150. {
  151. float f = t * math::PI;
  152. float g = (1.0 - math::cos(f)) * 0.5;
  153. return p0 + (g * (p1 - p0));
  154. }
  155. /// Returns the cubic interpolated value between @a p0 and @a p1 at time @a t
  156. template <typename T>
  157. inline T cubic(const T& p0, const T& p1, float t)
  158. {
  159. float tt = t * t;
  160. float ttt = tt * t;
  161. return p0 * (2.0 * ttt - 3.0 * tt + 1.0) + p1 * (3.0 * tt - 2.0 * ttt);
  162. }
  163. /// Bezier interpolation
  164. template <typename T>
  165. inline T bezier(const T& p0, const T& p1, const T& p2, const T& p3, float t)
  166. {
  167. float u = 1.0 - t;
  168. float tt = t * t ;
  169. float uu = u * u;
  170. float uuu = uu * u;
  171. float ttt = tt * t;
  172. T tmp = (uuu * p0) +
  173. (3 * uu * t * p1) +
  174. (3 * u * tt * p2) +
  175. (ttt * p3);
  176. return tmp;
  177. }
  178. /// Catmull-Rom interpolation
  179. template <typename T>
  180. inline T catmull_rom(const T& p0, const T& p1, const T& p2, const T& p3, float t)
  181. {
  182. float tt = t * t;
  183. float ttt = tt * t;
  184. T tmp = (2.0 * p1) +
  185. ((-p0 + p2) * t) +
  186. (((2.0 * p0) - (5.0 * p1) + (4.0 * p2) - p3) * tt) +
  187. ((-p0 + (3.0 * p1) + (-3.0 * p2) + p3) * ttt);
  188. return tmp * 0.5;
  189. }
  190. } // namespace math
  191. } // namespace crown