math_funcs.cpp 4.9 KB

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