| 1 | /**************************************************************************/ |
| 2 | /* math_funcs.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "math_funcs.h" |
| 32 | |
| 33 | #include "core/error/error_macros.h" |
| 34 | |
| 35 | RandomPCG Math::default_rand(RandomPCG::DEFAULT_SEED, RandomPCG::DEFAULT_INC); |
| 36 | |
| 37 | uint32_t Math::rand_from_seed(uint64_t *seed) { |
| 38 | RandomPCG rng = RandomPCG(*seed, RandomPCG::DEFAULT_INC); |
| 39 | uint32_t r = rng.rand(); |
| 40 | *seed = rng.get_seed(); |
| 41 | return r; |
| 42 | } |
| 43 | |
| 44 | void Math::seed(uint64_t x) { |
| 45 | default_rand.seed(x); |
| 46 | } |
| 47 | |
| 48 | void Math::randomize() { |
| 49 | default_rand.randomize(); |
| 50 | } |
| 51 | |
| 52 | uint32_t Math::rand() { |
| 53 | return default_rand.rand(); |
| 54 | } |
| 55 | |
| 56 | double Math::randfn(double mean, double deviation) { |
| 57 | return default_rand.randfn(mean, deviation); |
| 58 | } |
| 59 | |
| 60 | int Math::step_decimals(double p_step) { |
| 61 | static const int maxn = 10; |
| 62 | static const double sd[maxn] = { |
| 63 | 0.9999, // somehow compensate for floating point error |
| 64 | 0.09999, |
| 65 | 0.009999, |
| 66 | 0.0009999, |
| 67 | 0.00009999, |
| 68 | 0.000009999, |
| 69 | 0.0000009999, |
| 70 | 0.00000009999, |
| 71 | 0.000000009999, |
| 72 | 0.0000000009999 |
| 73 | }; |
| 74 | |
| 75 | double abs = Math::abs(p_step); |
| 76 | double decs = abs - (int)abs; // Strip away integer part |
| 77 | for (int i = 0; i < maxn; i++) { |
| 78 | if (decs >= sd[i]) { |
| 79 | return i; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | // Only meant for editor usage in float ranges, where a step of 0 |
| 87 | // means that decimal digits should not be limited in String::num. |
| 88 | int Math::range_step_decimals(double p_step) { |
| 89 | if (p_step < 0.0000000000001) { |
| 90 | return 16; // Max value hardcoded in String::num |
| 91 | } |
| 92 | return step_decimals(p_step); |
| 93 | } |
| 94 | |
| 95 | double Math::ease(double p_x, double p_c) { |
| 96 | if (p_x < 0) { |
| 97 | p_x = 0; |
| 98 | } else if (p_x > 1.0) { |
| 99 | p_x = 1.0; |
| 100 | } |
| 101 | if (p_c > 0) { |
| 102 | if (p_c < 1.0) { |
| 103 | return 1.0 - Math::pow(1.0 - p_x, 1.0 / p_c); |
| 104 | } else { |
| 105 | return Math::pow(p_x, p_c); |
| 106 | } |
| 107 | } else if (p_c < 0) { |
| 108 | //inout ease |
| 109 | |
| 110 | if (p_x < 0.5) { |
| 111 | return Math::pow(p_x * 2.0, -p_c) * 0.5; |
| 112 | } else { |
| 113 | return (1.0 - Math::pow(1.0 - (p_x - 0.5) * 2.0, -p_c)) * 0.5 + 0.5; |
| 114 | } |
| 115 | } else { |
| 116 | return 0; // no ease (raw) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | double Math::snapped(double p_value, double p_step) { |
| 121 | if (p_step != 0) { |
| 122 | p_value = Math::floor(p_value / p_step + 0.5) * p_step; |
| 123 | } |
| 124 | return p_value; |
| 125 | } |
| 126 | |
| 127 | uint32_t Math::larger_prime(uint32_t p_val) { |
| 128 | static const uint32_t primes[] = { |
| 129 | 5, |
| 130 | 13, |
| 131 | 23, |
| 132 | 47, |
| 133 | 97, |
| 134 | 193, |
| 135 | 389, |
| 136 | 769, |
| 137 | 1543, |
| 138 | 3079, |
| 139 | 6151, |
| 140 | 12289, |
| 141 | 24593, |
| 142 | 49157, |
| 143 | 98317, |
| 144 | 196613, |
| 145 | 393241, |
| 146 | 786433, |
| 147 | 1572869, |
| 148 | 3145739, |
| 149 | 6291469, |
| 150 | 12582917, |
| 151 | 25165843, |
| 152 | 50331653, |
| 153 | 100663319, |
| 154 | 201326611, |
| 155 | 402653189, |
| 156 | 805306457, |
| 157 | 1610612741, |
| 158 | 0, |
| 159 | }; |
| 160 | |
| 161 | int idx = 0; |
| 162 | while (true) { |
| 163 | ERR_FAIL_COND_V(primes[idx] == 0, 0); |
| 164 | if (primes[idx] > p_val) { |
| 165 | return primes[idx]; |
| 166 | } |
| 167 | idx++; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | double Math::random(double from, double to) { |
| 172 | return default_rand.random(from, to); |
| 173 | } |
| 174 | |
| 175 | float Math::random(float from, float to) { |
| 176 | return default_rand.random(from, to); |
| 177 | } |
| 178 | |
| 179 | int Math::random(int from, int to) { |
| 180 | return default_rand.random(from, to); |
| 181 | } |
| 182 | |