2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* math_funcs.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "math_funcs.h"
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "core/os/os.h"
|
2017-01-12 11:55:19 +00:00
|
|
|
|
2017-04-17 17:50:31 +00:00
|
|
|
pcg32_random_t Math::default_pcg = { 12047754176567800795ULL, PCG_DEFAULT_INC_64 };
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#define PHI 0x9e3779b9
|
|
|
|
|
2017-01-15 05:34:51 +00:00
|
|
|
// TODO: we should eventually expose pcg.inc too
|
|
|
|
uint32_t Math::rand_from_seed(uint64_t *seed) {
|
2017-03-05 15:44:50 +00:00
|
|
|
pcg32_random_t pcg = { *seed, PCG_DEFAULT_INC_64 };
|
2017-01-15 05:34:51 +00:00
|
|
|
uint32_t r = pcg32_random_r(&pcg);
|
|
|
|
*seed = pcg.state;
|
|
|
|
return r;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 05:34:51 +00:00
|
|
|
void Math::seed(uint64_t x) {
|
2017-03-05 15:44:50 +00:00
|
|
|
default_pcg.state = x;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Math::randomize() {
|
2017-04-17 17:50:31 +00:00
|
|
|
seed(OS::get_singleton()->get_ticks_usec() * default_pcg.state + PCG_DEFAULT_INC_64);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Math::rand() {
|
2017-01-15 05:34:51 +00:00
|
|
|
return pcg32_random_r(&default_pcg);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 20:24:34 +00:00
|
|
|
int Math::step_decimals(double p_step) {
|
2018-09-20 00:10:06 +00:00
|
|
|
static const int maxn = 10;
|
2017-03-05 15:44:50 +00:00
|
|
|
static const double sd[maxn] = {
|
2016-07-26 20:24:34 +00:00
|
|
|
0.9999, // somehow compensate for floating point error
|
|
|
|
0.09999,
|
|
|
|
0.009999,
|
|
|
|
0.0009999,
|
|
|
|
0.00009999,
|
|
|
|
0.000009999,
|
|
|
|
0.0000009999,
|
|
|
|
0.00000009999,
|
2018-09-20 00:10:06 +00:00
|
|
|
0.000000009999,
|
|
|
|
0.0000000009999
|
2016-07-26 20:24:34 +00:00
|
|
|
};
|
|
|
|
|
2018-09-20 00:10:06 +00:00
|
|
|
double abs = Math::abs(p_step);
|
|
|
|
double decs = abs - (int)abs; // Strip away integer part
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < maxn; i++) {
|
2018-09-20 00:10:06 +00:00
|
|
|
if (decs >= sd[i]) {
|
2016-07-26 20:24:34 +00:00
|
|
|
return i;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-09-20 00:10:06 +00:00
|
|
|
return 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double Math::dectime(double p_value, double p_amount, double p_step) {
|
2017-01-14 20:35:39 +00:00
|
|
|
double sgn = p_value < 0 ? -1.0 : 1.0;
|
|
|
|
double val = Math::abs(p_value);
|
2017-03-05 15:44:50 +00:00
|
|
|
val -= p_amount * p_step;
|
|
|
|
if (val < 0.0)
|
|
|
|
val = 0.0;
|
|
|
|
return val * sgn;
|
2017-01-14 20:35:39 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-14 20:35:39 +00:00
|
|
|
double Math::ease(double p_x, double p_c) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_x < 0)
|
|
|
|
p_x = 0;
|
|
|
|
else if (p_x > 1.0)
|
|
|
|
p_x = 1.0;
|
|
|
|
if (p_c > 0) {
|
|
|
|
if (p_c < 1.0) {
|
|
|
|
return 1.0 - Math::pow(1.0 - p_x, 1.0 / p_c);
|
2014-03-14 01:57:24 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return Math::pow(p_x, p_c);
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (p_c < 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
//inout ease
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_x < 0.5) {
|
|
|
|
return Math::pow(p_x * 2.0, -p_c) * 0.5;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return (1.0 - Math::pow(1.0 - (p_x - 0.5) * 2.0, -p_c)) * 0.5 + 0.5;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
return 0; // no ease (raw)
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double Math::stepify(double p_value, double p_step) {
|
|
|
|
if (p_step != 0) {
|
|
|
|
p_value = Math::floor(p_value / p_step + 0.5) * p_step;
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return p_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Math::larger_prime(uint32_t p_val) {
|
|
|
|
|
2015-05-05 22:56:59 +00:00
|
|
|
static const uint32_t primes[] = {
|
2014-02-10 01:10:30 +00:00
|
|
|
5,
|
|
|
|
13,
|
|
|
|
23,
|
|
|
|
47,
|
|
|
|
97,
|
|
|
|
193,
|
|
|
|
389,
|
|
|
|
769,
|
|
|
|
1543,
|
|
|
|
3079,
|
|
|
|
6151,
|
|
|
|
12289,
|
|
|
|
24593,
|
|
|
|
49157,
|
|
|
|
98317,
|
|
|
|
196613,
|
|
|
|
393241,
|
|
|
|
786433,
|
|
|
|
1572869,
|
|
|
|
3145739,
|
|
|
|
6291469,
|
|
|
|
12582917,
|
|
|
|
25165843,
|
|
|
|
50331653,
|
|
|
|
100663319,
|
|
|
|
201326611,
|
|
|
|
402653189,
|
|
|
|
805306457,
|
|
|
|
1610612741,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
while (true) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(primes[idx] == 0, 0);
|
|
|
|
if (primes[idx] > p_val)
|
2014-02-10 01:10:30 +00:00
|
|
|
return primes[idx];
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Math::random(double from, double to) {
|
|
|
|
unsigned int r = Math::rand();
|
2017-03-05 15:44:50 +00:00
|
|
|
double ret = (double)r / (double)RANDOM_MAX;
|
|
|
|
return (ret) * (to - from) + from;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 20:35:39 +00:00
|
|
|
float Math::random(float from, float to) {
|
|
|
|
unsigned int r = Math::rand();
|
2017-03-05 15:44:50 +00:00
|
|
|
float ret = (float)r / (float)RANDOM_MAX;
|
|
|
|
return (ret) * (to - from) + from;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|