From 0a7e5d0f4ab997c55c9130caf04726a4cdf42083 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 23 Oct 2023 19:18:50 +0200 Subject: [PATCH] Make the HashMap work. Move the Mathp namespace into the Math class. Small tweaks. --- core/aabb.h | 16 +- core/containers/hash_map.h | 3 +- core/containers/hashfuncs.h | 35 ++- core/containers/list.h | 2 +- core/defs.h | 92 +++++++ core/math_funcs.h | 533 +++++++++++++++++++----------------- core/node_path.cpp | 8 +- core/node_path.h | 14 +- core/os/spin_lock.h | 50 ++++ core/projection.cpp | 4 +- core/projection.h | 2 +- core/rect2i.h | 11 +- core/rid.h | 10 +- core/string_name.cpp | 19 +- core/string_name.h | 5 +- core/tag_db.cpp | 10 +- core/tag_db.h | 4 - core/transform_2d.cpp | 14 +- core/ustring.cpp | 6 +- core/ustring.h | 1 + core/vector2.h | 36 +-- core/vector3.cpp | 6 +- core/vector3.h | 2 +- 23 files changed, 514 insertions(+), 369 deletions(-) create mode 100644 core/os/spin_lock.h diff --git a/core/aabb.h b/core/aabb.h index 5d7432c..5d285ae 100644 --- a/core/aabb.h +++ b/core/aabb.h @@ -1,3 +1,6 @@ +#ifndef AABB_H +#define AABB_H + /*************************************************************************/ /* aabb.h */ /*************************************************************************/ @@ -28,15 +31,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef AABB_H -#define AABB_H - #include "vector3.h" -#include - - - class Plane; class AABB { @@ -66,8 +62,8 @@ public: bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this AABB merge(const AABB &p_with) const; - void merge_with(const AABB &p_aabb); ///merge with another AABB - AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs + void merge_with(const AABB &p_aabb); /// merge with another AABB + AABB intersection(const AABB &p_aabb) const; /// get box where two intersect, empty if no intersection occurs bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const; bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const; bool smits_intersect_ray(const Vector3 &from, const Vector3 &p_dir, real_t t0, real_t t1) const; @@ -105,6 +101,4 @@ public: } }; - - #endif // RECT3_H diff --git a/core/containers/hash_map.h b/core/containers/hash_map.h index afcc353..c9d68b0 100644 --- a/core/containers/hash_map.h +++ b/core/containers/hash_map.h @@ -32,9 +32,10 @@ /**************************************************************************/ #include "core/containers/hashfuncs.h" +#include "core/containers/list.h" #include "core/containers/paged_allocator.h" #include "core/containers/pair.h" -#include "core/math/math_funcs.h" +#include "core/math_funcs.h" #include "core/os/memory.h" /** diff --git a/core/containers/hashfuncs.h b/core/containers/hashfuncs.h index 4751bc7..8d448ed 100644 --- a/core/containers/hashfuncs.h +++ b/core/containers/hashfuncs.h @@ -30,23 +30,22 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "core/math/aabb.h" -#include "core/math/math_defs.h" -#include "core/math/math_funcs.h" -#include "core/math/rect2.h" -#include "core/math/rect2i.h" -#include "core/math/vector2.h" -#include "core/math/vector2i.h" -#include "core/math/vector3.h" -#include "core/math/vector3i.h" -#include "core/math/vector4.h" -#include "core/math/vector4i.h" -#include "core/object/object_id.h" -#include "core/string/node_path.h" -#include "core/string/string_name.h" -#include "core/string/ustring.h" -#include "core/containers/rid.h" +#include "core/aabb.h" +#include "core/defs.h" +#include "core/math_funcs.h" +#include "core/node_path.h" +#include "core/rect2.h" +#include "core/rect2i.h" +#include "core/rid.h" +#include "core/string_name.h" #include "core/typedefs.h" +#include "core/ustring.h" +#include "core/vector2.h" +#include "core/vector2i.h" +#include "core/vector3.h" +#include "core/vector3i.h" +#include "core/vector4.h" +#include "core/vector4i.h" /** * Hashing functions @@ -322,7 +321,7 @@ struct HashMapHasherDefault { static _FORCE_INLINE_ uint32_t hash(const RID &p_rid) { return hash_one_uint64(p_rid.get_id()); } static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); } static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); } - //static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); } + // static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); } static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); } static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash_one_uint64(p_int); } @@ -334,7 +333,7 @@ struct HashMapHasherDefault { static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return hash_fmix32(p_int); } static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return hash_fmix32(p_int); } static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return hash_fmix32(p_int); } - + static _FORCE_INLINE_ uint32_t hash(const Vector2i &p_vec) { uint32_t h = hash_murmur3_one_32(p_vec.x); h = hash_murmur3_one_32(p_vec.y, h); diff --git a/core/containers/list.h b/core/containers/list.h index bee327f..8cb2f8e 100644 --- a/core/containers/list.h +++ b/core/containers/list.h @@ -30,7 +30,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "core/error/error_macros.h" +#include "core/defs.h" #include "core/os/memory.h" #include "core/containers/sort_array.h" diff --git a/core/defs.h b/core/defs.h index 84311d4..6948d0c 100644 --- a/core/defs.h +++ b/core/defs.h @@ -153,14 +153,26 @@ typedef float real_t; #define ERR_MSG_INDEX(index, size) (String("Index ") + #index + "=" + String::num_int64(index) + " out of size (" + #size + "=" + String::num_int64(size) + ")") #endif +#ifndef ERR_MSG_INDEX_MSG +#define ERR_MSG_INDEX_MSG(index, size, msg) (String("Index ") + #index + "=" + String::num_int64(index) + " out of size (" + #size + "=" + String::num_int64(size) + ")" + #msg) +#endif + #ifndef ERR_MSG_NULL #define ERR_MSG_NULL(param) (String("Parameter '") + #param + "' is null.") #endif +#ifndef ERR_MSG_NULL_MSG +#define ERR_MSG_NULL_MSG(param, msg) (String("Parameter '") + #param + "' is null." + #msg) +#endif + #ifndef ERR_MSG_COND #define ERR_MSG_COND(cond) (String("Condition '") + #cond + "' is true.") #endif +#ifndef ERR_MSG_COND_MSG +#define ERR_MSG_COND_MSG(cond, msg) (String("Condition '") + #cond + "' is true." + #msg) +#endif + #ifndef ERR_FAIL_INDEX #define ERR_FAIL_INDEX(index, size) \ do { \ @@ -171,6 +183,16 @@ typedef float real_t; } while (0) #endif +#ifndef ERR_FAIL_INDEX_MSG +#define ERR_FAIL_INDEX_MSG(index, size, m_msg) \ + do { \ + if (unlikely((index) < 0 || (index) >= (size))) { \ + ERR_PRINT(ERR_MSG_INDEX_MSG(index, size, m_msg)); \ + return; \ + } \ + } while (0) +#endif + #ifndef ERR_FAIL_INDEX_V #define ERR_FAIL_INDEX_V(index, size, ret) \ do { \ @@ -181,6 +203,16 @@ typedef float real_t; } while (0) #endif +#ifndef ERR_FAIL_INDEX_V_MSG +#define ERR_FAIL_INDEX_V_MSG(index, size, ret, m_msg) \ + do { \ + if (unlikely((index) < 0 || (index) >= (size))) { \ + ERR_PRINT(ERR_MSG_INDEX_MSG(index, size, m_msg)); \ + return ret; \ + } \ + } while (0) +#endif + #ifndef ERR_FAIL_UNSIGNED_INDEX_V #define ERR_FAIL_UNSIGNED_INDEX_V(index, size, ret) \ do { \ @@ -191,6 +223,16 @@ typedef float real_t; } while (0) #endif +#ifndef ERR_FAIL_UNSIGNED_INDEX_V_MSG +#define ERR_FAIL_UNSIGNED_INDEX_V_MSG(index, size, ret, m_msg) \ + do { \ + if (unlikely((index) >= (size))) { \ + ERR_PRINT(ERR_MSG_INDEX_MSG(index, size, m_msg)); \ + return ret; \ + } \ + } while (0) +#endif + #ifndef CRASH_BAD_INDEX #define CRASH_BAD_INDEX(index, size) \ do { \ @@ -211,6 +253,16 @@ typedef float real_t; } while (0) #endif +#ifndef ERR_FAIL_NULL_MSG +#define ERR_FAIL_NULL_MSG(param, m_msg) \ + do { \ + if (unlikely(!param)) { \ + ERR_PRINT(ERR_MSG_NULL_MSG(param, m_msg)); \ + return; \ + } \ + } while (0) +#endif + #ifndef ERR_FAIL_NULL_V #define ERR_FAIL_NULL_V(param, ret) \ do { \ @@ -221,6 +273,16 @@ typedef float real_t; } while (0) #endif +#ifndef ERR_FAIL_NULL_V_MSG +#define ERR_FAIL_NULL_V_MSG(param, ret, m_msg) \ + do { \ + if (unlikely(!param)) { \ + ERR_PRINT(ERR_MSG_NULL_MSG(param, m_msg)); \ + return ret; \ + } \ + } while (0) +#endif + #ifndef ERR_FAIL_COND #define ERR_FAIL_COND(cond) \ do { \ @@ -231,6 +293,16 @@ typedef float real_t; } while (0) #endif +#ifndef ERR_FAIL_COND_MSG +#define ERR_FAIL_COND_MSG(cond, m_msg) \ + do { \ + if (unlikely(cond)) { \ + ERR_PRINT(ERR_MSG_COND_MSG(cond, m_msg)); \ + return; \ + } \ + } while (0) +#endif + #ifndef CRASH_COND #define CRASH_COND(cond) \ do { \ @@ -241,6 +313,16 @@ typedef float real_t; } while (0) #endif +#ifndef CRASH_COND_MSG +#define CRASH_COND_MSG(cond, m_msg) \ + do { \ + if (unlikely(cond)) { \ + FATAL_PRINT(ERR_MSG_COND_MSG(cond, m_msg)); \ + GENERATE_TRAP; \ + } \ + } while (0) +#endif + #ifndef ERR_FAIL_COND_V #define ERR_FAIL_COND_V(cond, ret) \ do { \ @@ -251,6 +333,16 @@ typedef float real_t; } while (0) #endif +#ifndef ERR_FAIL_COND_V_MSG +#define ERR_FAIL_COND_V_MSG(cond, ret, m_msg) \ + do { \ + if (unlikely(cond)) { \ + ERR_PRINT(ERR_MSG_COND_MSG(cond, m_msg)); \ + return ret; \ + } \ + } while (0) +#endif + #ifndef ERR_CONTINUE #define ERR_CONTINUE(cond) \ { \ diff --git a/core/math_funcs.h b/core/math_funcs.h index 31f5198..b60ed6d 100644 --- a/core/math_funcs.h +++ b/core/math_funcs.h @@ -32,271 +32,310 @@ #define PANDEMONIUM_MATH_H #include "defs.h" + +#include +#include #include +class Math { +public: + // Functions reproduced as in Pandemonium's source code `math_funcs.h`. + // Some are overloads to automatically support changing real_t into either double or float in the way Pandemonium does. -namespace Mathp { - -// Functions reproduced as in Pandemonium's source code `math_funcs.h`. -// Some are overloads to automatically support changing real_t into either double or float in the way Pandemonium does. - -inline double fmod(double p_x, double p_y) { - return ::fmod(p_x, p_y); -} -inline float fmod(float p_x, float p_y) { - return ::fmodf(p_x, p_y); -} - -inline double floor(double p_x) { - return ::floor(p_x); -} -inline float floor(float p_x) { - return ::floorf(p_x); -} - -inline double exp(double p_x) { - return ::exp(p_x); -} -inline float exp(float p_x) { - return ::expf(p_x); -} - -inline double sin(double p_x) { - return ::sin(p_x); -} -inline float sin(float p_x) { - return ::sinf(p_x); -} - -inline double cos(double p_x) { - return ::cos(p_x); -} -inline float cos(float p_x) { - return ::cosf(p_x); -} - -inline double tan(double p_x) { - return ::tan(p_x); -} -inline float tan(float p_x) { - return ::tanf(p_x); -} - -inline double asin(double p_x) { - return ::asin(p_x); -} -inline float asin(float p_x) { - return ::asinf(p_x); -} - -inline double acos(double p_x) { - return ::acos(p_x); -} -inline float acos(float p_x) { - return ::acosf(p_x); -} - -inline double atan(double p_x) { - return ::atan(p_x); -} -inline float atan(float p_x) { - return ::atanf(p_x); -} - -inline double atan2(double p_y, double p_x) { - return ::atan2(p_y, p_x); -} -inline float atan2(float p_y, float p_x) { - return ::atan2f(p_y, p_x); -} - -inline double sqrt(double p_x) { - return ::sqrt(p_x); -} -inline float sqrt(float p_x) { - return ::sqrtf(p_x); -} - -inline float lerp(float minv, float maxv, float t) { - return minv + t * (maxv - minv); -} -inline double lerp(double minv, double maxv, double t) { - return minv + t * (maxv - minv); -} - -inline double lerp_angle(double p_from, double p_to, double p_weight) { - double difference = fmod(p_to - p_from, Math_TAU); - double distance = fmod(2.0 * difference, Math_TAU) - difference; - return p_from + distance * p_weight; -} -inline float lerp_angle(float p_from, float p_to, float p_weight) { - float difference = fmod(p_to - p_from, (float)Math_TAU); - float distance = fmod(2.0f * difference, (float)Math_TAU) - difference; - return p_from + distance * p_weight; -} - -template -inline T clamp(T x, T minv, T maxv) { - if (x < minv) { - return minv; + static _ALWAYS_INLINE_ double fmod(double p_x, double p_y) { + return ::fmod(p_x, p_y); } - if (x > maxv) { - return maxv; + static _ALWAYS_INLINE_ float fmod(float p_x, float p_y) { + return ::fmodf(p_x, p_y); } - return x; -} -template -inline T min(T a, T b) { - return a < b ? a : b; -} - -template -inline T max(T a, T b) { - return a > b ? a : b; -} - -template -inline T sign(T x) { - return static_cast(x < 0 ? -1 : 1); -} - -inline double deg2rad(double p_y) { - return p_y * Math_PI / 180.0; -} -inline float deg2rad(float p_y) { - return p_y * static_cast(Math_PI) / 180.f; -} - -inline double rad2deg(double p_y) { - return p_y * 180.0 / Math_PI; -} -inline float rad2deg(float p_y) { - return p_y * 180.f / static_cast(Math_PI); -} - -inline double inverse_lerp(double p_from, double p_to, double p_value) { - return (p_value - p_from) / (p_to - p_from); -} -inline float inverse_lerp(float p_from, float p_to, float p_value) { - return (p_value - p_from) / (p_to - p_from); -} - -inline double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) { - return Mathp::lerp(p_ostart, p_ostop, Mathp::inverse_lerp(p_istart, p_istop, p_value)); -} -inline float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) { - return Mathp::lerp(p_ostart, p_ostop, Mathp::inverse_lerp(p_istart, p_istop, p_value)); -} - -inline bool is_equal_approx(real_t a, real_t b) { - // Check for exact equality first, required to handle "infinity" values. - if (a == b) { - return true; + static _ALWAYS_INLINE_ double floor(double p_x) { + return ::floor(p_x); } - // Then check for approximate equality. - real_t tolerance = CMP_EPSILON * ABS(a); - if (tolerance < CMP_EPSILON) { - tolerance = CMP_EPSILON; + static _ALWAYS_INLINE_ float floor(float p_x) { + return ::floorf(p_x); } - return ABS(a - b) < tolerance; -} -inline bool is_equal_approx(real_t a, real_t b, real_t tolerance) { - // Check for exact equality first, required to handle "infinity" values. - if (a == b) { - return true; + static _ALWAYS_INLINE_ double exp(double p_x) { + return ::exp(p_x); } - // Then check for approximate equality. - return ABS(a - b) < tolerance; -} - -inline bool is_zero_approx(real_t s) { - return ABS(s) < CMP_EPSILON; -} - -inline double smoothstep(double p_from, double p_to, double p_weight) { - if (is_equal_approx(static_cast(p_from), static_cast(p_to))) { - return p_from; + static _ALWAYS_INLINE_ float exp(float p_x) { + return ::expf(p_x); } - double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0); - return x * x * (3.0 - 2.0 * x); -} -inline float smoothstep(float p_from, float p_to, float p_weight) { - if (is_equal_approx(p_from, p_to)) { - return p_from; + + static _ALWAYS_INLINE_ double sin(double p_x) { + return ::sin(p_x); } - float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f); - return x * x * (3.0f - 2.0f * x); -} - -inline double move_toward(double p_from, double p_to, double p_delta) { - return ABS(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta; -} - -inline float move_toward(float p_from, float p_to, float p_delta) { - return ABS(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta; -} - -inline double linear2db(double p_linear) { - return log(p_linear) * 8.6858896380650365530225783783321; -} -inline float linear2db(float p_linear) { - return log(p_linear) * 8.6858896380650365530225783783321f; -} - -inline double db2linear(double p_db) { - return exp(p_db * 0.11512925464970228420089957273422); -} -inline float db2linear(float p_db) { - return exp(p_db * 0.11512925464970228420089957273422f); -} - -inline double round(double p_val) { - return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5); -} -inline float round(float p_val) { - return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f); -} - -inline int64_t wrapi(int64_t value, int64_t min, int64_t max) { - int64_t range = max - min; - return range == 0 ? min : min + ((((value - min) % range) + range) % range); -} - -inline float wrapf(real_t value, real_t min, real_t max) { - const real_t range = max - min; - return is_zero_approx(range) ? min : value - (range * floor((value - min) / range)); -} - -inline float stepify(float p_value, float p_step) { - if (p_step != 0) { - p_value = floor(p_value / p_step + 0.5f) * p_step; + static _ALWAYS_INLINE_ float sin(float p_x) { + return ::sinf(p_x); } - return p_value; -} -inline double stepify(double p_value, double p_step) { - if (p_step != 0) { - p_value = floor(p_value / p_step + 0.5) * p_step; + + static _ALWAYS_INLINE_ double cos(double p_x) { + return ::cos(p_x); + } + static _ALWAYS_INLINE_ float cos(float p_x) { + return ::cosf(p_x); } - return p_value; -} -inline unsigned int next_power_of_2(unsigned int x) { - if (x == 0) - return 0; + static _ALWAYS_INLINE_ double tan(double p_x) { + return ::tan(p_x); + } + static _ALWAYS_INLINE_ float tan(float p_x) { + return ::tanf(p_x); + } - --x; - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; + static _ALWAYS_INLINE_ double asin(double p_x) { + return ::asin(p_x); + } + static _ALWAYS_INLINE_ float asin(float p_x) { + return ::asinf(p_x); + } - return ++x; -} + static _ALWAYS_INLINE_ double acos(double p_x) { + return ::acos(p_x); + } + static _ALWAYS_INLINE_ float acos(float p_x) { + return ::acosf(p_x); + } -} // namespace Math + static _ALWAYS_INLINE_ double atan(double p_x) { + return ::atan(p_x); + } + static _ALWAYS_INLINE_ float atan(float p_x) { + return ::atanf(p_x); + } + static _ALWAYS_INLINE_ double atan2(double p_y, double p_x) { + return ::atan2(p_y, p_x); + } + static _ALWAYS_INLINE_ float atan2(float p_y, float p_x) { + return ::atan2f(p_y, p_x); + } + + static _ALWAYS_INLINE_ double sqrt(double p_x) { + return ::sqrt(p_x); + } + static _ALWAYS_INLINE_ float sqrt(float p_x) { + return ::sqrtf(p_x); + } + + static _ALWAYS_INLINE_ float lerp(float minv, float maxv, float t) { + return minv + t * (maxv - minv); + } + static _ALWAYS_INLINE_ double lerp(double minv, double maxv, double t) { + return minv + t * (maxv - minv); + } + + static _ALWAYS_INLINE_ double lerp_angle(double p_from, double p_to, double p_weight) { + double difference = fmod(p_to - p_from, Math_TAU); + double distance = fmod(2.0 * difference, Math_TAU) - difference; + return p_from + distance * p_weight; + } + static _ALWAYS_INLINE_ float lerp_angle(float p_from, float p_to, float p_weight) { + float difference = fmod(p_to - p_from, (float)Math_TAU); + float distance = fmod(2.0f * difference, (float)Math_TAU) - difference; + return p_from + distance * p_weight; + } + + template + static _ALWAYS_INLINE_ T clamp(T x, T minv, T maxv) { + if (x < minv) { + return minv; + } + if (x > maxv) { + return maxv; + } + return x; + } + + template + static _ALWAYS_INLINE_ T min(T a, T b) { + return a < b ? a : b; + } + + template + static _ALWAYS_INLINE_ T max(T a, T b) { + return a > b ? a : b; + } + + template + static _ALWAYS_INLINE_ T sign(T x) { + return static_cast(x < 0 ? -1 : 1); + } + + static _ALWAYS_INLINE_ double deg2rad(double p_y) { + return p_y * Math_PI / 180.0; + } + static _ALWAYS_INLINE_ float deg2rad(float p_y) { + return p_y * static_cast(Math_PI) / 180.f; + } + + static _ALWAYS_INLINE_ double rad2deg(double p_y) { + return p_y * 180.0 / Math_PI; + } + static _ALWAYS_INLINE_ float rad2deg(float p_y) { + return p_y * 180.f / static_cast(Math_PI); + } + + static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) { + return (p_value - p_from) / (p_to - p_from); + } + static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) { + return (p_value - p_from) / (p_to - p_from); + } + + static _ALWAYS_INLINE_ double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) { + return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); + } + static _ALWAYS_INLINE_ float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) { + return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); + } + + static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) { + // Check for exact equality first, required to handle "infinity" values. + if (a == b) { + return true; + } + // Then check for approximate equality. + real_t tolerance = CMP_EPSILON * ABS(a); + if (tolerance < CMP_EPSILON) { + tolerance = CMP_EPSILON; + } + return ABS(a - b) < tolerance; + } + + static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b, real_t tolerance) { + // Check for exact equality first, required to handle "infinity" values. + if (a == b) { + return true; + } + // Then check for approximate equality. + return ABS(a - b) < tolerance; + } + + static _ALWAYS_INLINE_ bool is_zero_approx(real_t s) { + return ABS(s) < CMP_EPSILON; + } + + static _ALWAYS_INLINE_ double smoothstep(double p_from, double p_to, double p_weight) { + if (is_equal_approx(static_cast(p_from), static_cast(p_to))) { + return p_from; + } + double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0); + return x * x * (3.0 - 2.0 * x); + } + static _ALWAYS_INLINE_ float smoothstep(float p_from, float p_to, float p_weight) { + if (is_equal_approx(p_from, p_to)) { + return p_from; + } + float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f); + return x * x * (3.0f - 2.0f * x); + } + + static _ALWAYS_INLINE_ double move_toward(double p_from, double p_to, double p_delta) { + return ABS(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta; + } + + static _ALWAYS_INLINE_ float move_toward(float p_from, float p_to, float p_delta) { + return ABS(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta; + } + + static _ALWAYS_INLINE_ double linear2db(double p_linear) { + return log(p_linear) * 8.6858896380650365530225783783321; + } + static _ALWAYS_INLINE_ float linear2db(float p_linear) { + return log(p_linear) * 8.6858896380650365530225783783321f; + } + + static _ALWAYS_INLINE_ double db2linear(double p_db) { + return exp(p_db * 0.11512925464970228420089957273422); + } + static _ALWAYS_INLINE_ float db2linear(float p_db) { + return exp(p_db * 0.11512925464970228420089957273422f); + } + + static _ALWAYS_INLINE_ double round(double p_val) { + return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5); + } + static _ALWAYS_INLINE_ float round(float p_val) { + return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f); + } + + static _ALWAYS_INLINE_ int64_t wrapi(int64_t value, int64_t min, int64_t max) { + int64_t range = max - min; + return range == 0 ? min : min + ((((value - min) % range) + range) % range); + } + + static _ALWAYS_INLINE_ float wrapf(real_t value, real_t min, real_t max) { + const real_t range = max - min; + return is_zero_approx(range) ? min : value - (range * floor((value - min) / range)); + } + + static _ALWAYS_INLINE_ float stepify(float p_value, float p_step) { + if (p_step != 0) { + p_value = floor(p_value / p_step + 0.5f) * p_step; + } + return p_value; + } + static _ALWAYS_INLINE_ double stepify(double p_value, double p_step) { + if (p_step != 0) { + p_value = floor(p_value / p_step + 0.5) * p_step; + } + return p_value; + } + + static _ALWAYS_INLINE_ unsigned int next_power_of_2(unsigned int x) { + if (x == 0) + return 0; + + --x; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + + return ++x; + } + + static _ALWAYS_INLINE_ bool is_nan(double p_val) { +#ifdef _MSC_VER + return _isnan(p_val); +#elif defined(__GNUC__) && __GNUC__ < 6 + union { + uint64_t u; + double f; + } ieee754; + ieee754.f = p_val; + // (unsigned)(0x7ff0000000000001 >> 32) : 0x7ff00000 + return ((((unsigned)(ieee754.u >> 32) & 0x7fffffff) + ((unsigned)ieee754.u != 0)) > 0x7ff00000); +#else + return isnan(p_val); +#endif + } + + static _ALWAYS_INLINE_ bool is_nan(float p_val) { +#ifdef _MSC_VER + return _isnan(p_val); +#elif defined(__GNUC__) && __GNUC__ < 6 + union { + uint32_t u; + float f; + } ieee754; + ieee754.f = p_val; + // ----------------------------------- + // (single-precision floating-point) + // NaN : s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx + // : (> 0x7f800000) + // where, + // s : sign + // x : non-zero number + // ----------------------------------- + return ((ieee754.u & 0x7fffffff) > 0x7f800000); +#else + return isnan(p_val); +#endif + } +}; #endif // PANDEMONIUM_MATH_H diff --git a/core/node_path.cpp b/core/node_path.cpp index 1b030c2..3a5f287 100644 --- a/core/node_path.cpp +++ b/core/node_path.cpp @@ -34,8 +34,6 @@ #include - - NodePath::NodePath() { String from = ""; Pandemonium::api->pandemonium_node_path_new(&_node_path, (pandemonium_string *)&from); @@ -90,6 +88,10 @@ String NodePath::get_concatenated_subnames() const { return String(str); } +uint32_t NodePath::hash() const { + return Pandemonium::api->pandemonium_node_path_hash(&_node_path); +} + NodePath::operator String() const { pandemonium_string str = Pandemonium::api->pandemonium_node_path_as_string(&_node_path); return String(str); @@ -110,5 +112,3 @@ void NodePath::operator=(const NodePath &other) { NodePath::~NodePath() { Pandemonium::api->pandemonium_node_path_destroy(&_node_path); } - - diff --git a/core/node_path.h b/core/node_path.h index b839b7b..7228cd9 100644 --- a/core/node_path.h +++ b/core/node_path.h @@ -1,3 +1,7 @@ + +#ifndef NODEPATH_H +#define NODEPATH_H + /*************************************************************************/ /* NodePath.h */ /*************************************************************************/ @@ -28,13 +32,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef NODEPATH_H -#define NODEPATH_H - #include - - class String; class NodePath { @@ -70,15 +69,14 @@ public: String get_concatenated_subnames() const; + uint32_t hash() const; + operator String() const; void operator=(const NodePath &other); - bool operator==(const NodePath &other); ~NodePath(); }; - - #endif // NODEPATH_H diff --git a/core/os/spin_lock.h b/core/os/spin_lock.h new file mode 100644 index 0000000..dc9f07b --- /dev/null +++ b/core/os/spin_lock.h @@ -0,0 +1,50 @@ +#ifndef SPIN_LOCK_H +#define SPIN_LOCK_H +/*************************************************************************/ +/* spin_lock.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "core/defs.h" + +#include + +class SpinLock { + std::atomic_flag locked = ATOMIC_FLAG_INIT; + +public: + _ALWAYS_INLINE_ void lock() { + while (locked.test_and_set(std::memory_order_acquire)) { + ; + } + } + _ALWAYS_INLINE_ void unlock() { + locked.clear(std::memory_order_release); + } +}; +#endif // SPIN_LOCK_H diff --git a/core/projection.cpp b/core/projection.cpp index c3a2893..c393f4f 100644 --- a/core/projection.cpp +++ b/core/projection.cpp @@ -561,7 +561,7 @@ real_t Projection::get_fov() const { right_plane.normalize(); if ((matrix[8] == 0) && (matrix[9] == 0)) { - return Mathp::rad2deg(acos(ABS(right_plane.normal.x))) * 2.0; + return Math::rad2deg(acos(ABS(right_plane.normal.x))) * 2.0; } else { // our frustum is asymmetrical need to calculate the left planes angle separately.. Plane left_plane = Plane(matrix[3] + matrix[0], @@ -570,7 +570,7 @@ real_t Projection::get_fov() const { matrix[15] + matrix[12]); left_plane.normalize(); - return Mathp::rad2deg(acos(ABS(left_plane.normal.x))) + Mathp::rad2deg(acos(ABS(right_plane.normal.x))); + return Math::rad2deg(acos(ABS(left_plane.normal.x))) + Math::rad2deg(acos(ABS(right_plane.normal.x))); } } diff --git a/core/projection.h b/core/projection.h index 0c82b04..119f0b7 100644 --- a/core/projection.h +++ b/core/projection.h @@ -64,7 +64,7 @@ struct Projection { void set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, real_t p_near, real_t p_far, bool p_flip_fov = false); static real_t get_fovy(real_t p_fovx, real_t p_aspect) { - return Mathp::rad2deg(atan(p_aspect * tan(Mathp::deg2rad(p_fovx) * 0.5)) * 2.0); + return Math::rad2deg(atan(p_aspect * tan(Math::deg2rad(p_fovx) * 0.5)) * 2.0); } static inline double absd(double g) { diff --git a/core/rect2i.h b/core/rect2i.h index 2c152b5..fd0f892 100644 --- a/core/rect2i.h +++ b/core/rect2i.h @@ -1,3 +1,6 @@ +#ifndef RECT2I_H +#define RECT2I_H + /*************************************************************************/ /* rect2.h */ /*************************************************************************/ @@ -28,17 +31,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef RECT2I_H -#define RECT2I_H - #include "vector2i.h" #include - #include - - class String; typedef Vector2i Size2i; @@ -61,6 +58,4 @@ struct Rect2i { } }; - - #endif // RECT2_H diff --git a/core/rid.h b/core/rid.h index 73d8c28..95a6a37 100644 --- a/core/rid.h +++ b/core/rid.h @@ -1,3 +1,6 @@ +#ifndef RID_H +#define RID_H + /*************************************************************************/ /* rid.h */ /*************************************************************************/ @@ -28,13 +31,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef RID_H -#define RID_H - #include - - class Object; class RID { @@ -62,6 +60,4 @@ public: bool operator>=(const RID &p_other) const; }; - - #endif // RID_H diff --git a/core/string_name.cpp b/core/string_name.cpp index 1a2f861..22d09e9 100644 --- a/core/string_name.cpp +++ b/core/string_name.cpp @@ -31,8 +31,8 @@ #include "string_name.h" #include "array.h" -#include "pandemonium_global.h" #include "node_path.h" +#include "pandemonium_global.h" #include "pool_arrays.h" #include "variant.h" @@ -40,8 +40,6 @@ #include - - StringName::StringName() { Pandemonium::api->pandemonium_string_name_new(&_pandemonium_string_name); } @@ -58,6 +56,19 @@ StringName::~StringName() { Pandemonium::api->pandemonium_string_name_destroy(&_pandemonium_string_name); } +StringName::operator String() { + return String(Pandemonium::api->pandemonium_string_name_get_name(&_pandemonium_string_name)); +} + +uint32_t StringName::get_hash() { + return Pandemonium::api->pandemonium_string_name_get_hash(&_pandemonium_string_name); +} +uint32_t StringName::hash() const { + return Pandemonium::api->pandemonium_string_name_get_hash(&_pandemonium_string_name); +} +const void *StringName::get_data_unique_pointer() { + return &_pandemonium_string_name; +} bool StringName::operator==(const StringName &s) const { return Pandemonium::api->pandemonium_string_name_operator_equal(&_pandemonium_string_name, &s._pandemonium_string_name); @@ -83,5 +94,3 @@ bool StringName::operator>(const StringName &s) const { bool StringName::operator>=(const StringName &s) const { return !(*this < s); } - - diff --git a/core/string_name.h b/core/string_name.h index 4b24fcb..4f9c226 100644 --- a/core/string_name.h +++ b/core/string_name.h @@ -33,8 +33,6 @@ #include - - class String; class StringName { @@ -56,6 +54,7 @@ public: operator String(); uint32_t get_hash(); + uint32_t hash() const; const void *get_data_unique_pointer(); bool operator==(const StringName &s) const; @@ -67,6 +66,4 @@ public: bool operator>=(const StringName &s) const; }; - - #endif // STRING_H diff --git a/core/tag_db.cpp b/core/tag_db.cpp index fad8f02..2f1b450 100644 --- a/core/tag_db.cpp +++ b/core/tag_db.cpp @@ -30,15 +30,13 @@ #include "tag_db.h" -#include +#include "core/containers/hash_map.h" #include - - namespace _TagDB { -std::unordered_map parent_to; +HashMap parent_to; void register_type(size_t type_tag, size_t base_type_tag) { if (type_tag == base_type_tag) { @@ -48,7 +46,7 @@ void register_type(size_t type_tag, size_t base_type_tag) { } bool is_type_known(size_t type_tag) { - return parent_to.find(type_tag) != parent_to.end(); + return parent_to.find(type_tag) != NULL; } void register_global_type(const char *name, size_t type_tag, size_t base_type_tag) { @@ -74,5 +72,3 @@ bool is_type_compatible(size_t ask_tag, size_t have_tag) { } } // namespace _TagDB - - diff --git a/core/tag_db.h b/core/tag_db.h index d959569..bc51099 100644 --- a/core/tag_db.h +++ b/core/tag_db.h @@ -33,8 +33,6 @@ #include - - namespace _TagDB { void register_type(size_t type_tag, size_t base_type_tag); @@ -44,6 +42,4 @@ bool is_type_compatible(size_t type_tag, size_t base_type_tag); } // namespace _TagDB - - #endif // TAGDB_H diff --git a/core/transform_2d.cpp b/core/transform_2d.cpp index 820b38f..eb1d304 100644 --- a/core/transform_2d.cpp +++ b/core/transform_2d.cpp @@ -35,8 +35,6 @@ #include - - const Transform2D Transform2D::IDENTITY; const Transform2D Transform2D::FLIP_X = Transform2D(-1, 0, 0, 1, 0, 0); const Transform2D Transform2D::FLIP_Y = Transform2D(1, 0, 0, -1, 0, 0); @@ -291,7 +289,7 @@ real_t Transform2D::basis_determinant() const { } Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_c) const { - //extract parameters + // extract parameters Vector2 p1 = get_origin(); Vector2 p2 = p_transform.get_origin(); @@ -301,25 +299,25 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t Size2 s1 = get_scale(); Size2 s2 = p_transform.get_scale(); - //slerp rotation + // slerp rotation Vector2 v1(::cos(r1), ::sin(r1)); Vector2 v2(::cos(r2), ::sin(r2)); real_t dot = v1.dot(v2); - dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); //clamp dot to [-1,1] + dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); // clamp dot to [-1,1] Vector2 v; if (dot > 0.9995) { - v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues + v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); // linearly interpolate to avoid numerical precision issues } else { real_t angle = p_c * ::acos(dot); Vector2 v3 = (v2 - v1 * dot).normalized(); v = v1 * ::cos(angle) + v3 * ::sin(angle); } - //construct matrix + // construct matrix Transform2D res(::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c)); res.scale_basis(Vector2::linear_interpolate(s1, s2, p_c)); return res; @@ -328,5 +326,3 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t Transform2D::operator String() const { return String(String() + elements[0] + ", " + elements[1] + ", " + elements[2]); } - - diff --git a/core/ustring.cpp b/core/ustring.cpp index b8f7ba2..d0e5a7c 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -31,8 +31,8 @@ #include "ustring.h" #include "array.h" -#include "pandemonium_global.h" #include "node_path.h" +#include "pandemonium_global.h" #include "pool_arrays.h" #include "variant.h" @@ -40,8 +40,6 @@ #include - - CharString::~CharString() { Pandemonium::api->pandemonium_char_string_destroy(&_char_string); } @@ -530,5 +528,3 @@ String String::trim_suffix(const String &suffix) const { pandemonium_string s = Pandemonium::api->pandemonium_string_trim_suffix(&_pandemonium_string, &suffix._pandemonium_string); return String(s); } - - diff --git a/core/ustring.h b/core/ustring.h index 8a04dcf..c9e9c0b 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -73,6 +73,7 @@ class String { friend class NodePath; friend class Variant; friend class StringName; + explicit inline String(pandemonium_string contents) : _pandemonium_string(contents) {} diff --git a/core/vector2.h b/core/vector2.h index 049bec2..ab68bf3 100644 --- a/core/vector2.h +++ b/core/vector2.h @@ -37,8 +37,6 @@ #include - - class String; struct Vector2 { @@ -267,40 +265,32 @@ struct Vector2 { } inline Vector2 floor() const { - return Vector2(Mathp::floor(x), Mathp::floor(y)); + return Vector2(Math::floor(x), Math::floor(y)); } inline Vector2 snapped(const Vector2 &p_by) const { return Vector2( - Mathp::stepify(x, p_by.x), - Mathp::stepify(y, p_by.y)); + Math::stepify(x, p_by.x), + Math::stepify(y, p_by.y)); } inline real_t aspect() const { return width / height; } operator String() const; + + static Vector2 cartesian2polar(Vector2 v) { + return Vector2(Math::sqrt(v.x * v.x + v.y * v.y), Math::atan2(v.y, v.x)); + } + + static Vector2 polar2cartesian(Vector2 v) { + // x == radius + // y == angle + return Vector2(v.x * Math::cos(v.y), v.x * Math::sin(v.y)); + } }; inline Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) { return p_vec * p_scalar; } -namespace Mathp { - -// Convenience, since they exist in GDScript - -inline Vector2 cartesian2polar(Vector2 v) { - return Vector2(Mathp::sqrt(v.x * v.x + v.y * v.y), Mathp::atan2(v.y, v.x)); -} - -inline Vector2 polar2cartesian(Vector2 v) { - // x == radius - // y == angle - return Vector2(v.x * Mathp::cos(v.y), v.x * Mathp::sin(v.y)); -} - -} // namespace Math - - - #endif // VECTOR2_H diff --git a/core/vector3.cpp b/core/vector3.cpp index a53ea23..eb42c90 100644 --- a/core/vector3.cpp +++ b/core/vector3.cpp @@ -109,9 +109,9 @@ void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) { } void Vector3::snap(real_t p_val) { - x = Mathp::stepify(x, p_val); - y = Mathp::stepify(y, p_val); - z = Mathp::stepify(z, p_val); + x = Math::stepify(x, p_val); + y = Math::stepify(y, p_val); + z = Math::stepify(z, p_val); } Vector3::operator String() const { diff --git a/core/vector3.h b/core/vector3.h index 0f05595..34eac26 100644 --- a/core/vector3.h +++ b/core/vector3.h @@ -255,7 +255,7 @@ struct Vector3 { } inline real_t angle_to(const Vector3 &b) const { - return Mathp::atan2(cross(b).length(), dot(b)); + return Math::atan2(cross(b).length(), dot(b)); } inline Vector3 direction_to(const Vector3 &p_b) const {