math_funcs.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "math_funcs.h"
  30. #include "core/os/os.h"
  31. pcg32_random_t Math::default_pcg = {1, PCG_DEFAULT_INC_64};
  32. #define PHI 0x9e3779b9
  33. #if 0
  34. static uint32_t Q[4096];
  35. #endif
  36. // TODO: we should eventually expose pcg.inc too
  37. uint32_t Math::rand_from_seed(uint64_t *seed) {
  38. pcg32_random_t pcg = {*seed, PCG_DEFAULT_INC_64};
  39. uint32_t r = pcg32_random_r(&pcg);
  40. *seed = pcg.state;
  41. return r;
  42. }
  43. void Math::seed(uint64_t x) {
  44. default_pcg.state=x;
  45. }
  46. void Math::randomize() {
  47. OS::Time time = OS::get_singleton()->get_time();
  48. seed(OS::get_singleton()->get_ticks_usec()*(time.hour+1)*(time.min+1)*(time.sec+1)*rand()); // TODO: can be simplified.
  49. }
  50. uint32_t Math::rand() {
  51. return pcg32_random_r(&default_pcg);
  52. }
  53. double Math::randf() {
  54. return (double)rand() / (double)Math::RANDOM_MAX;
  55. }
  56. double Math::round(double p_val) {
  57. if (p_val>=0) {
  58. return ::floor(p_val+0.5);
  59. } else {
  60. p_val=-p_val;
  61. return -::floor(p_val+0.5);
  62. }
  63. }
  64. double Math::dectime(double p_value,double p_amount, double p_step) {
  65. float sgn = p_value < 0 ? -1.0 : 1.0;
  66. float val = absf(p_value);
  67. val-=p_amount*p_step;
  68. if (val<0.0)
  69. val=0.0;
  70. return val*sgn;
  71. }
  72. int Math::step_decimals(double p_step) {
  73. static const int maxn=9;
  74. static const double sd[maxn]={
  75. 0.9999, // somehow compensate for floating point error
  76. 0.09999,
  77. 0.009999,
  78. 0.0009999,
  79. 0.00009999,
  80. 0.000009999,
  81. 0.0000009999,
  82. 0.00000009999,
  83. 0.000000009999
  84. };
  85. double as=absf(p_step);
  86. for(int i=0;i<maxn;i++) {
  87. if (as>=sd[i]) {
  88. return i;
  89. }
  90. }
  91. return maxn;
  92. }
  93. double Math::ease(double p_x, double p_c) {
  94. if (p_x<0)
  95. p_x=0;
  96. else if (p_x>1.0)
  97. p_x=1.0;
  98. if (p_c>0) {
  99. if (p_c<1.0) {
  100. return 1.0-Math::pow(1.0-p_x,1.0/p_c);
  101. } else {
  102. return Math::pow(p_x,p_c);
  103. }
  104. } else if (p_c<0) {
  105. //inout ease
  106. if (p_x<0.5) {
  107. return Math::pow(p_x*2.0,-p_c)*0.5;
  108. } else {
  109. return (1.0-Math::pow(1.0-(p_x-0.5)*2.0,-p_c))*0.5+0.5;
  110. }
  111. } else
  112. return 0; // no ease (raw)
  113. }
  114. double Math::stepify(double p_value,double p_step) {
  115. if (p_step!=0) {
  116. p_value=floor( p_value / p_step + 0.5 ) * p_step;
  117. }
  118. return p_value;
  119. }
  120. uint32_t Math::larger_prime(uint32_t p_val) {
  121. static const uint32_t primes[] = {
  122. 5,
  123. 13,
  124. 23,
  125. 47,
  126. 97,
  127. 193,
  128. 389,
  129. 769,
  130. 1543,
  131. 3079,
  132. 6151,
  133. 12289,
  134. 24593,
  135. 49157,
  136. 98317,
  137. 196613,
  138. 393241,
  139. 786433,
  140. 1572869,
  141. 3145739,
  142. 6291469,
  143. 12582917,
  144. 25165843,
  145. 50331653,
  146. 100663319,
  147. 201326611,
  148. 402653189,
  149. 805306457,
  150. 1610612741,
  151. 0,
  152. };
  153. int idx=0;
  154. while (true) {
  155. ERR_FAIL_COND_V(primes[idx]==0,0);
  156. if (primes[idx]>p_val)
  157. return primes[idx];
  158. idx++;
  159. }
  160. return 0;
  161. }
  162. double Math::random(double from, double to) {
  163. unsigned int r = Math::rand();
  164. double ret = (double)r/(double)RANDOM_MAX;
  165. return (ret)*(to-from) + from;
  166. }
  167. double Math::pow(double x, double y) {
  168. return ::pow(x,y);
  169. }
  170. double Math::log(double x) {
  171. return ::log(x);
  172. }
  173. double Math::exp(double x) {
  174. return ::exp(x);
  175. }