math_funcs.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* math_funcs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "math_funcs.h"
  31. #include "core/error_macros.h"
  32. RandomPCG Math::default_rand(RandomPCG::DEFAULT_SEED, RandomPCG::DEFAULT_INC);
  33. #define PHI 0x9e3779b9
  34. uint32_t Math::rand_from_seed(uint64_t *seed) {
  35. RandomPCG rng = RandomPCG(*seed, RandomPCG::DEFAULT_INC);
  36. uint32_t r = rng.rand();
  37. *seed = rng.get_seed();
  38. return r;
  39. }
  40. void Math::seed(uint64_t x) {
  41. default_rand.seed(x);
  42. }
  43. void Math::randomize() {
  44. default_rand.randomize();
  45. }
  46. uint32_t Math::rand() {
  47. return default_rand.rand();
  48. }
  49. int Math::step_decimals(double p_step) {
  50. static const int maxn = 10;
  51. static const double sd[maxn] = {
  52. 0.9999, // somehow compensate for floating point error
  53. 0.09999,
  54. 0.009999,
  55. 0.0009999,
  56. 0.00009999,
  57. 0.000009999,
  58. 0.0000009999,
  59. 0.00000009999,
  60. 0.000000009999,
  61. 0.0000000009999
  62. };
  63. double abs = Math::abs(p_step);
  64. double decs = abs - (int)abs; // Strip away integer part
  65. for (int i = 0; i < maxn; i++) {
  66. if (decs >= sd[i]) {
  67. return i;
  68. }
  69. }
  70. return 0;
  71. }
  72. // Only meant for editor usage in float ranges, where a step of 0
  73. // means that decimal digits should not be limited in String::num.
  74. int Math::range_step_decimals(double p_step) {
  75. if (p_step < 0.0000000000001) {
  76. return 16; // Max value hardcoded in String::num
  77. }
  78. return step_decimals(p_step);
  79. }
  80. double Math::dectime(double p_value, double p_amount, double p_step) {
  81. double sgn = p_value < 0 ? -1.0 : 1.0;
  82. double val = Math::abs(p_value);
  83. val -= p_amount * p_step;
  84. if (val < 0.0)
  85. val = 0.0;
  86. return val * sgn;
  87. }
  88. double Math::ease(double p_x, double p_c) {
  89. if (p_x < 0)
  90. p_x = 0;
  91. else if (p_x > 1.0)
  92. p_x = 1.0;
  93. if (p_c > 0) {
  94. if (p_c < 1.0) {
  95. return 1.0 - Math::pow(1.0 - p_x, 1.0 / p_c);
  96. } else {
  97. return Math::pow(p_x, p_c);
  98. }
  99. } else if (p_c < 0) {
  100. //inout ease
  101. if (p_x < 0.5) {
  102. return Math::pow(p_x * 2.0, -p_c) * 0.5;
  103. } else {
  104. return (1.0 - Math::pow(1.0 - (p_x - 0.5) * 2.0, -p_c)) * 0.5 + 0.5;
  105. }
  106. } else
  107. return 0; // no ease (raw)
  108. }
  109. double Math::stepify(double p_value, double p_step) {
  110. if (p_step != 0) {
  111. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  112. }
  113. return p_value;
  114. }
  115. uint32_t Math::larger_prime(uint32_t p_val) {
  116. static const uint32_t primes[] = {
  117. 5,
  118. 13,
  119. 23,
  120. 47,
  121. 97,
  122. 193,
  123. 389,
  124. 769,
  125. 1543,
  126. 3079,
  127. 6151,
  128. 12289,
  129. 24593,
  130. 49157,
  131. 98317,
  132. 196613,
  133. 393241,
  134. 786433,
  135. 1572869,
  136. 3145739,
  137. 6291469,
  138. 12582917,
  139. 25165843,
  140. 50331653,
  141. 100663319,
  142. 201326611,
  143. 402653189,
  144. 805306457,
  145. 1610612741,
  146. 0,
  147. };
  148. int idx = 0;
  149. while (true) {
  150. ERR_FAIL_COND_V(primes[idx] == 0, 0);
  151. if (primes[idx] > p_val)
  152. return primes[idx];
  153. idx++;
  154. }
  155. }
  156. double Math::random(double from, double to) {
  157. return default_rand.random(from, to);
  158. }
  159. float Math::random(float from, float to) {
  160. return default_rand.random(from, to);
  161. }