math_funcs.cpp 4.9 KB

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