math_funcs.cpp 4.6 KB

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