math_funcs.cpp 4.8 KB

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