math_utils.h 3.4 KB

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