Math.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*************************************************************************/
  2. /* Math.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_MATH_H
  31. #define GODOT_MATH_H
  32. #include "Defs.hpp"
  33. #include <cmath>
  34. namespace godot {
  35. namespace Math {
  36. // Functions reproduced as in Godot's source code `math_funcs.h`.
  37. // Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
  38. inline double fmod(double p_x, double p_y) {
  39. return ::fmod(p_x, p_y);
  40. }
  41. inline float fmod(float p_x, float p_y) {
  42. return ::fmodf(p_x, p_y);
  43. }
  44. inline double floor(double p_x) {
  45. return ::floor(p_x);
  46. }
  47. inline float floor(float p_x) {
  48. return ::floorf(p_x);
  49. }
  50. inline double exp(double p_x) {
  51. return ::exp(p_x);
  52. }
  53. inline float exp(float p_x) {
  54. return ::expf(p_x);
  55. }
  56. inline double sin(double p_x) {
  57. return ::sin(p_x);
  58. }
  59. inline float sin(float p_x) {
  60. return ::sinf(p_x);
  61. }
  62. inline double cos(double p_x) {
  63. return ::cos(p_x);
  64. }
  65. inline float cos(float p_x) {
  66. return ::cosf(p_x);
  67. }
  68. inline double tan(double p_x) {
  69. return ::tan(p_x);
  70. }
  71. inline float tan(float p_x) {
  72. return ::tanf(p_x);
  73. }
  74. inline double asin(double p_x) {
  75. return ::asin(p_x);
  76. }
  77. inline float asin(float p_x) {
  78. return ::asinf(p_x);
  79. }
  80. inline double acos(double p_x) {
  81. return ::acos(p_x);
  82. }
  83. inline float acos(float p_x) {
  84. return ::acosf(p_x);
  85. }
  86. inline double atan(double p_x) {
  87. return ::atan(p_x);
  88. }
  89. inline float atan(float p_x) {
  90. return ::atanf(p_x);
  91. }
  92. inline double atan2(double p_y, double p_x) {
  93. return ::atan2(p_y, p_x);
  94. }
  95. inline float atan2(float p_y, float p_x) {
  96. return ::atan2f(p_y, p_x);
  97. }
  98. inline double sqrt(double p_x) {
  99. return ::sqrt(p_x);
  100. }
  101. inline float sqrt(float p_x) {
  102. return ::sqrtf(p_x);
  103. }
  104. inline float lerp(float minv, float maxv, float t) {
  105. return minv + t * (maxv - minv);
  106. }
  107. inline double lerp(double minv, double maxv, double t) {
  108. return minv + t * (maxv - minv);
  109. }
  110. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  111. double difference = fmod(p_to - p_from, Math_TAU);
  112. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  113. return p_from + distance * p_weight;
  114. }
  115. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  116. float difference = fmod(p_to - p_from, (float)Math_TAU);
  117. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  118. return p_from + distance * p_weight;
  119. }
  120. template <typename T>
  121. inline T clamp(T x, T minv, T maxv) {
  122. if (x < minv) {
  123. return minv;
  124. }
  125. if (x > maxv) {
  126. return maxv;
  127. }
  128. return x;
  129. }
  130. template <typename T>
  131. inline T min(T a, T b) {
  132. return a < b ? a : b;
  133. }
  134. template <typename T>
  135. inline T max(T a, T b) {
  136. return a > b ? a : b;
  137. }
  138. template <typename T>
  139. inline T sign(T x) {
  140. return static_cast<T>(x < 0 ? -1 : 1);
  141. }
  142. inline double deg2rad(double p_y) {
  143. return p_y * Math_PI / 180.0;
  144. }
  145. inline float deg2rad(float p_y) {
  146. return p_y * static_cast<float>(Math_PI) / 180.f;
  147. }
  148. inline double rad2deg(double p_y) {
  149. return p_y * 180.0 / Math_PI;
  150. }
  151. inline float rad2deg(float p_y) {
  152. return p_y * 180.f / static_cast<float>(Math_PI);
  153. }
  154. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  155. return (p_value - p_from) / (p_to - p_from);
  156. }
  157. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  158. return (p_value - p_from) / (p_to - p_from);
  159. }
  160. inline double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  161. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  162. }
  163. inline float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  164. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  165. }
  166. inline bool is_equal_approx(real_t a, real_t b) {
  167. // Check for exact equality first, required to handle "infinity" values.
  168. if (a == b) {
  169. return true;
  170. }
  171. // Then check for approximate equality.
  172. real_t tolerance = CMP_EPSILON * std::abs(a);
  173. if (tolerance < CMP_EPSILON) {
  174. tolerance = CMP_EPSILON;
  175. }
  176. return std::abs(a - b) < tolerance;
  177. }
  178. inline bool is_equal_approx(real_t a, real_t b, real_t tolerance) {
  179. // Check for exact equality first, required to handle "infinity" values.
  180. if (a == b) {
  181. return true;
  182. }
  183. // Then check for approximate equality.
  184. return std::abs(a - b) < tolerance;
  185. }
  186. inline bool is_zero_approx(real_t s) {
  187. return std::abs(s) < CMP_EPSILON;
  188. }
  189. inline double smoothstep(double p_from, double p_to, double p_weight) {
  190. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  191. return p_from;
  192. }
  193. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  194. return x * x * (3.0 - 2.0 * x);
  195. }
  196. inline float smoothstep(float p_from, float p_to, float p_weight) {
  197. if (is_equal_approx(p_from, p_to)) {
  198. return p_from;
  199. }
  200. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  201. return x * x * (3.0f - 2.0f * x);
  202. }
  203. inline double move_toward(double p_from, double p_to, double p_delta) {
  204. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  205. }
  206. inline float move_toward(float p_from, float p_to, float p_delta) {
  207. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  208. }
  209. inline double linear2db(double p_linear) {
  210. return log(p_linear) * 8.6858896380650365530225783783321;
  211. }
  212. inline float linear2db(float p_linear) {
  213. return log(p_linear) * 8.6858896380650365530225783783321f;
  214. }
  215. inline double db2linear(double p_db) {
  216. return exp(p_db * 0.11512925464970228420089957273422);
  217. }
  218. inline float db2linear(float p_db) {
  219. return exp(p_db * 0.11512925464970228420089957273422f);
  220. }
  221. inline double round(double p_val) {
  222. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  223. }
  224. inline float round(float p_val) {
  225. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  226. }
  227. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  228. int64_t range = max - min;
  229. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  230. }
  231. inline float wrapf(real_t value, real_t min, real_t max) {
  232. const real_t range = max - min;
  233. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  234. }
  235. inline float stepify(float p_value, float p_step) {
  236. if (p_step != 0) {
  237. p_value = floor(p_value / p_step + 0.5f) * p_step;
  238. }
  239. return p_value;
  240. }
  241. inline double stepify(double p_value, double p_step) {
  242. if (p_step != 0) {
  243. p_value = floor(p_value / p_step + 0.5) * p_step;
  244. }
  245. return p_value;
  246. }
  247. inline unsigned int next_power_of_2(unsigned int x) {
  248. if (x == 0)
  249. return 0;
  250. --x;
  251. x |= x >> 1;
  252. x |= x >> 2;
  253. x |= x >> 4;
  254. x |= x >> 8;
  255. x |= x >> 16;
  256. return ++x;
  257. }
  258. } // namespace Math
  259. } // namespace godot
  260. #endif // GODOT_MATH_H