Debian Codegraph @

core/math/math_funcs.cpp

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