math_funcs.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "float.h"
  32. #include "pcg.h"
  33. pcg32_random_t Math::default_pcg = {1, PCG_DEFAULT_INC_64};
  34. #define PHI 0x9e3779b9
  35. #if 0
  36. static uint32_t Q[4096];
  37. #endif
  38. // TODO: we should eventually expose pcg.inc too
  39. uint32_t Math::rand_from_seed(uint64_t *seed) {
  40. pcg32_random_t pcg = {*seed, PCG_DEFAULT_INC_64};
  41. uint32_t r = pcg32_random_r(&pcg);
  42. *seed = pcg.state;
  43. return r;
  44. }
  45. void Math::seed(uint64_t x) {
  46. default_pcg.state=x;
  47. }
  48. void Math::randomize() {
  49. OS::Time time = OS::get_singleton()->get_time();
  50. seed(OS::get_singleton()->get_ticks_usec()*(time.hour+1)*(time.min+1)*(time.sec+1)*rand()); // TODO: can be simplified.
  51. }
  52. uint32_t Math::rand() {
  53. return pcg32_random_r(&default_pcg);
  54. }
  55. int Math::step_decimals(double p_step) {
  56. static const int maxn=9;
  57. static const double sd[maxn]={
  58. 0.9999, // somehow compensate for floating point error
  59. 0.09999,
  60. 0.009999,
  61. 0.0009999,
  62. 0.00009999,
  63. 0.000009999,
  64. 0.0000009999,
  65. 0.00000009999,
  66. 0.000000009999
  67. };
  68. double as=Math::abs(p_step);
  69. for(int i=0;i<maxn;i++) {
  70. if (as>=sd[i]) {
  71. return i;
  72. }
  73. }
  74. return maxn;
  75. }
  76. double Math::dectime(double p_value,double p_amount, double p_step) {
  77. double sgn = p_value < 0 ? -1.0 : 1.0;
  78. double val = Math::abs(p_value);
  79. val-=p_amount*p_step;
  80. if (val<0.0)
  81. val=0.0;
  82. return val*sgn;
  83. }
  84. double Math::ease(double p_x, double p_c) {
  85. if (p_x<0)
  86. p_x=0;
  87. else if (p_x>1.0)
  88. p_x=1.0;
  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. double Math::stepify(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. idx++;
  150. }
  151. return 0;
  152. }
  153. double Math::random(double from, double to) {
  154. unsigned int r = Math::rand();
  155. double ret = (double)r/(double)RANDOM_MAX;
  156. return (ret)*(to-from) + from;
  157. }
  158. float Math::random(float from, float to) {
  159. unsigned int r = Math::rand();
  160. float ret = (float)r/(float)RANDOM_MAX;
  161. return (ret)*(to-from) + from;
  162. }