math_funcs.cpp 4.9 KB

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