/**************************************************************************/1/* math_funcs.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 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/**************************************************************************/2930#include "math_funcs.h"3132#include "core/error/error_macros.h"33#include "core/math/random_pcg.h"3435static RandomPCG default_rand;3637uint32_t Math::rand_from_seed(uint64_t *p_seed) {38RandomPCG rng = RandomPCG(*p_seed);39uint32_t r = rng.rand();40*p_seed = rng.get_seed();41return r;42}4344void Math::seed(uint64_t p_value) {45default_rand.seed(p_value);46}4748void Math::randomize() {49default_rand.randomize();50}5152uint32_t Math::rand() {53return default_rand.rand();54}5556double Math::randfn(double p_mean, double p_deviation) {57return default_rand.randfn(p_mean, p_deviation);58}5960int Math::step_decimals(double p_step) {61static const int maxn = 10;62static const double sd[maxn] = {630.9999, // somehow compensate for floating point error640.09999,650.009999,660.0009999,670.00009999,680.000009999,690.0000009999,700.00000009999,710.000000009999,720.000000000999973};7475double abs = Math::abs(p_step);76double decs = abs - (int)abs; // Strip away integer part77for (int i = 0; i < maxn; i++) {78if (decs >= sd[i]) {79return i;80}81}8283return 0;84}8586// Only meant for editor usage in float ranges, where a step of 087// means that decimal digits should not be limited in String::num.88int Math::range_step_decimals(double p_step) {89if (p_step < 0.0000000000001) {90return 16; // Max value hardcoded in String::num91}92return step_decimals(p_step);93}9495double Math::ease(double p_x, double p_c) {96if (p_x < 0) {97p_x = 0;98} else if (p_x > 1.0) {99p_x = 1.0;100}101if (p_c > 0) {102if (p_c < 1.0) {103return 1.0 - Math::pow(1.0 - p_x, 1.0 / p_c);104} else {105return Math::pow(p_x, p_c);106}107} else if (p_c < 0) {108//inout ease109110if (p_x < 0.5) {111return Math::pow(p_x * 2.0, -p_c) * 0.5;112} else {113return (1.0 - Math::pow(1.0 - (p_x - 0.5) * 2.0, -p_c)) * 0.5 + 0.5;114}115} else {116return 0; // no ease (raw)117}118}119120double Math::snapped(double p_value, double p_step) {121if (p_step != 0) {122p_value = Math::floor(p_value / p_step + 0.5) * p_step;123}124return p_value;125}126127uint32_t Math::larger_prime(uint32_t p_val) {128static const uint32_t primes[] = {1295,13013,13123,13247,13397,134193,135389,136769,1371543,1383079,1396151,14012289,14124593,14249157,14398317,144196613,145393241,146786433,1471572869,1483145739,1496291469,15012582917,15125165843,15250331653,153100663319,154201326611,155402653189,156805306457,1571610612741,1580,159};160161int idx = 0;162while (true) {163ERR_FAIL_COND_V(primes[idx] == 0, 0);164if (primes[idx] > p_val) {165return primes[idx];166}167idx++;168}169}170171double Math::random(double p_from, double p_to) {172return default_rand.random(p_from, p_to);173}174175float Math::random(float p_from, float p_to) {176return default_rand.random(p_from, p_to);177}178179int Math::random(int p_from, int p_to) {180return default_rand.random(p_from, p_to);181}182183184