2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* math_funcs.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:45:00 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef MATH_FUNCS_H
|
|
|
|
#define MATH_FUNCS_H
|
|
|
|
|
|
|
|
#include "math_defs.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "typedefs.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#ifndef NO_MATH_H
|
|
|
|
#include "math.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class Math {
|
|
|
|
|
|
|
|
static uint32_t default_seed;
|
2017-03-19 00:36:26 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
2017-03-19 00:36:26 +01:00
|
|
|
Math(){}; // useless to instance
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
enum {
|
2017-03-19 00:36:26 +01:00
|
|
|
RANDOM_MAX = 2147483647L
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static double sin(double p_x);
|
|
|
|
static double cos(double p_x);
|
|
|
|
static double tan(double p_x);
|
|
|
|
static double sinh(double p_x);
|
|
|
|
static double cosh(double p_x);
|
|
|
|
static double tanh(double p_x);
|
|
|
|
static double asin(double p_x);
|
|
|
|
static double acos(double p_x);
|
|
|
|
static double atan(double p_x);
|
|
|
|
static double atan2(double p_y, double p_x);
|
|
|
|
static double deg2rad(double p_y);
|
|
|
|
static double rad2deg(double p_y);
|
|
|
|
static double sqrt(double p_x);
|
2017-03-19 00:36:26 +01:00
|
|
|
static double fmod(double p_x, double p_y);
|
|
|
|
static double fposmod(double p_x, double p_y);
|
2014-02-10 02:10:30 +01:00
|
|
|
static uint32_t rand_from_seed(uint32_t *seed);
|
|
|
|
static double floor(double p_x);
|
|
|
|
static double ceil(double p_x);
|
|
|
|
static double ease(double p_x, double p_c);
|
2016-07-26 22:24:34 +02:00
|
|
|
static int step_decimals(double p_step);
|
2017-03-19 00:36:26 +01:00
|
|
|
static double stepify(double p_value, double p_step);
|
|
|
|
static void seed(uint32_t x = 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
static void randomize();
|
|
|
|
static uint32_t larger_prime(uint32_t p_val);
|
2017-03-19 00:36:26 +01:00
|
|
|
static double dectime(double p_value, double p_amount, double p_step);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
static inline double linear2db(double p_linear) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return Math::log(p_linear) * 8.6858896380650365530225783783321;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2015-09-26 16:47:59 +02:00
|
|
|
static inline double db2linear(double p_db) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return Math::exp(p_db * 0.11512925464970228420089957273422);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_nan(double p_val);
|
|
|
|
static bool is_inf(double p_val);
|
|
|
|
|
|
|
|
static uint32_t rand();
|
|
|
|
static double randf();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
static double round(double p_val);
|
|
|
|
|
|
|
|
static double random(double from, double to);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
static _FORCE_INLINE_ real_t abs(real_t g) {
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
#ifdef REAL_T_IS_DOUBLE
|
|
|
|
|
|
|
|
return absd(g);
|
2014-02-10 02:10:30 +01:00
|
|
|
#else
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
return absf(g);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ float absf(float g) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
uint32_t i;
|
|
|
|
} u;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
u.f = g;
|
|
|
|
u.i &= 2147483647u;
|
2014-02-10 02:10:30 +01:00
|
|
|
return u.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ double absd(double g) {
|
|
|
|
|
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
uint64_t i;
|
|
|
|
} u;
|
2017-03-19 00:36:26 +01:00
|
|
|
u.d = g;
|
|
|
|
u.i &= (uint64_t)9223372036854775807ll;
|
2014-02-10 02:10:30 +01:00
|
|
|
return u.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
//this function should be as fast as possible and rounding mode should not matter
|
|
|
|
static _FORCE_INLINE_ int fast_ftoi(float a) {
|
|
|
|
|
|
|
|
static int b;
|
|
|
|
|
2014-12-20 19:30:06 +01:00
|
|
|
#if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
|
2017-03-19 00:36:26 +01:00
|
|
|
b = (int)((a > 0.0f) ? (a + 0.5f) : (a - 0.5f));
|
2014-12-15 19:42:58 +01:00
|
|
|
|
|
|
|
#elif defined(_MSC_VER) && _MSC_VER < 1800
|
2017-03-19 00:36:26 +01:00
|
|
|
__asm fld a __asm fistp b
|
2014-02-10 02:10:30 +01:00
|
|
|
/*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
|
|
|
|
// use AT&T inline assembly style, document that
|
|
|
|
// we use memory as output (=m) and input (m)
|
|
|
|
__asm__ __volatile__ (
|
|
|
|
"flds %1 \n\t"
|
|
|
|
"fistpl %0 \n\t"
|
|
|
|
: "=m" (b)
|
|
|
|
: "m" (a));*/
|
2014-12-15 19:42:58 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#else
|
2017-03-19 00:36:26 +01:00
|
|
|
b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
2017-03-19 00:36:26 +01:00
|
|
|
return b;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
|
|
|
|
#else
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ float lerp(float a, float b, float c) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return a + (b - a) * c;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static double pow(double x, double y);
|
|
|
|
static double log(double x);
|
|
|
|
static double exp(double x);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define Math_PI 3.14159265358979323846
|
|
|
|
#define Math_SQRT12 0.7071067811865475244008443621048490
|
|
|
|
|
|
|
|
#endif // MATH_FUNCS_H
|