math_funcs.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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-2016 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 <math.h>
  32. #include "float.h"
  33. uint32_t Math::default_seed=1;
  34. #define PHI 0x9e3779b9
  35. #if 0
  36. static uint32_t Q[4096];
  37. #endif
  38. uint32_t Math::rand_from_seed(uint32_t *seed) {
  39. // Xorshift31 PRNG
  40. if ( *seed == 0 ) *seed = Math::RANDOM_MAX;
  41. (*seed) ^= (*seed) << 13;
  42. (*seed) ^= (*seed) >> 17;
  43. (*seed) ^= (*seed) << 5;
  44. return (*seed) & Math::RANDOM_MAX;
  45. }
  46. void Math::seed(uint32_t x) {
  47. default_seed=x;
  48. }
  49. void Math::randomize() {
  50. OS::Time time = OS::get_singleton()->get_time();
  51. seed(OS::get_singleton()->get_ticks_usec()*(time.hour+1)*(time.min+1)*(time.sec+1)*rand()); /* *OS::get_singleton()->get_time().sec); // windows doesn't have get_time(), returns always 0 */
  52. }
  53. uint32_t Math::rand() {
  54. return rand_from_seed(&default_seed);
  55. }
  56. double Math::randf() {
  57. return (double)rand() / (double)Math::RANDOM_MAX;
  58. }
  59. double Math::sin(double p_x) {
  60. return ::sin(p_x);
  61. }
  62. double Math::cos(double p_x) {
  63. return ::cos(p_x);
  64. }
  65. double Math::tan(double p_x) {
  66. return ::tan(p_x);
  67. }
  68. double Math::sinh(double p_x) {
  69. return ::sinh(p_x);
  70. }
  71. double Math::cosh(double p_x) {
  72. return ::cosh(p_x);
  73. }
  74. double Math::tanh(double p_x) {
  75. return ::tanh(p_x);
  76. }
  77. double Math::deg2rad(double p_y) {
  78. return p_y*Math_PI/180.0;
  79. }
  80. double Math::rad2deg(double p_y) {
  81. return p_y*180.0/Math_PI;
  82. }
  83. double Math::round(double p_val) {
  84. if (p_val>=0) {
  85. return ::floor(p_val+0.5);
  86. } else {
  87. p_val=-p_val;
  88. return -::floor(p_val+0.5);
  89. }
  90. }
  91. double Math::asin(double p_x) {
  92. return ::asin(p_x);
  93. }
  94. double Math::acos(double p_x) {
  95. return ::acos(p_x);
  96. }
  97. double Math::atan(double p_x) {
  98. return ::atan(p_x);
  99. }
  100. double Math::dectime(double p_value,double p_amount, double p_step) {
  101. float sgn = p_value < 0 ? -1.0 : 1.0;
  102. float val = absf(p_value);
  103. val-=p_amount*p_step;
  104. if (val<0.0)
  105. val=0.0;
  106. return val*sgn;
  107. }
  108. double Math::atan2(double p_y, double p_x) {
  109. return ::atan2(p_y,p_x);
  110. }
  111. double Math::sqrt(double p_x) {
  112. return ::sqrt(p_x);
  113. }
  114. double Math::fmod(double p_x,double p_y) {
  115. return ::fmod(p_x,p_y);
  116. }
  117. double Math::fposmod(double p_x,double p_y) {
  118. if (p_x>=0) {
  119. return Math::fmod(p_x,p_y);
  120. } else {
  121. return p_y-Math::fmod(-p_x,p_y);
  122. }
  123. }
  124. double Math::floor(double p_x) {
  125. return ::floor(p_x);
  126. }
  127. double Math::ceil(double p_x) {
  128. return ::ceil(p_x);
  129. }
  130. int Math::step_decimals(double p_step) {
  131. static const int maxn=9;
  132. static const double sd[maxn]={
  133. 0.9999, // somehow compensate for floating point error
  134. 0.09999,
  135. 0.009999,
  136. 0.0009999,
  137. 0.00009999,
  138. 0.000009999,
  139. 0.0000009999,
  140. 0.00000009999,
  141. 0.000000009999
  142. };
  143. double as=absf(p_step);
  144. for(int i=0;i<maxn;i++) {
  145. if (as>=sd[i]) {
  146. return i;
  147. }
  148. }
  149. return maxn;
  150. }
  151. double Math::ease(double p_x, double p_c) {
  152. if (p_x<0)
  153. p_x=0;
  154. else if (p_x>1.0)
  155. p_x=1.0;
  156. if (p_c>0) {
  157. if (p_c<1.0) {
  158. return 1.0-Math::pow(1.0-p_x,1.0/p_c);
  159. } else {
  160. return Math::pow(p_x,p_c);
  161. }
  162. } else if (p_c<0) {
  163. //inout ease
  164. if (p_x<0.5) {
  165. return Math::pow(p_x*2.0,-p_c)*0.5;
  166. } else {
  167. return (1.0-Math::pow(1.0-(p_x-0.5)*2.0,-p_c))*0.5+0.5;
  168. }
  169. } else
  170. return 0; // no ease (raw)
  171. }
  172. double Math::stepify(double p_value,double p_step) {
  173. if (p_step!=0) {
  174. p_value=floor( p_value / p_step + 0.5 ) * p_step;
  175. }
  176. return p_value;
  177. }
  178. bool Math::is_nan(double p_val) {
  179. return (p_val!=p_val);
  180. }
  181. bool Math::is_inf(double p_val) {
  182. #ifdef _MSC_VER
  183. return !_finite(p_val);
  184. #else
  185. return isinf(p_val);
  186. #endif
  187. }
  188. uint32_t Math::larger_prime(uint32_t p_val) {
  189. static const uint32_t primes[] = {
  190. 5,
  191. 13,
  192. 23,
  193. 47,
  194. 97,
  195. 193,
  196. 389,
  197. 769,
  198. 1543,
  199. 3079,
  200. 6151,
  201. 12289,
  202. 24593,
  203. 49157,
  204. 98317,
  205. 196613,
  206. 393241,
  207. 786433,
  208. 1572869,
  209. 3145739,
  210. 6291469,
  211. 12582917,
  212. 25165843,
  213. 50331653,
  214. 100663319,
  215. 201326611,
  216. 402653189,
  217. 805306457,
  218. 1610612741,
  219. 0,
  220. };
  221. int idx=0;
  222. while (true) {
  223. ERR_FAIL_COND_V(primes[idx]==0,0);
  224. if (primes[idx]>p_val)
  225. return primes[idx];
  226. idx++;
  227. }
  228. return 0;
  229. }
  230. double Math::random(double from, double to) {
  231. unsigned int r = Math::rand();
  232. double ret = (double)r/(double)RANDOM_MAX;
  233. return (ret)*(to-from) + from;
  234. }
  235. double Math::pow(double x, double y) {
  236. return ::pow(x,y);
  237. }
  238. double Math::log(double x) {
  239. return ::log(x);
  240. }
  241. double Math::exp(double x) {
  242. return ::exp(x);
  243. }