diff --git a/binding_generator.py b/binding_generator.py index 24135aa5..48b34aea 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -19,6 +19,9 @@ def print_file_list(api_filepath, output_dir, headers=False, sources=False): if is_pod_type(builtin_class["name"]): continue + if is_included_type(builtin_class["name"]): + continue + header_filename = include_gen_folder / "variant" / (camel_to_snake(builtin_class["name"]) + ".hpp") source_filename = source_gen_folder / "variant" / (camel_to_snake(builtin_class["name"]) + ".cpp") if headers: @@ -112,6 +115,8 @@ def generate_builtin_bindings(api, output_dir, build_config): for builtin_api in api["builtin_classes"]: if is_pod_type(builtin_api["name"]): continue + if is_included_type(builtin_api["name"]): + continue size = builtin_sizes[builtin_api["name"]] @@ -413,6 +418,19 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl result.append("bool operator!=(const wchar_t *p_str) const;") result.append("bool operator!=(const char16_t *p_str) const;") result.append("bool operator!=(const char32_t *p_str) const;") + result.append(f'\tconst char32_t &operator[](int p_index) const;') + result.append(f'\tchar32_t &operator[](int p_index);') + + if is_packed_array(class_name): + return_type = correct_type(builtin_api["indexing_return_type"]) + if class_name == "PackedByteArray": + return_type = 'uint8_t' + elif class_name == "PackedInt32Array": + return_type = 'int32_t' + elif class_name == "PackedFloat32Array": + return_type = 'float' + result.append(f'\tconst ' + return_type + f' &operator[](int p_index) const;') + result.append(f'\t' + return_type + f' &operator[](int p_index);') result.append("};") @@ -430,7 +448,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl result.append("String operator+(const wchar_t *p_chr, const String &p_str);") result.append("String operator+(const char16_t *p_chr, const String &p_str);") result.append("String operator+(const char32_t *p_chr, const String &p_str);") - + result.append("") result.append("} // namespace godot") @@ -1497,6 +1515,42 @@ def is_pod_type(type_name): "uint64_t", ] +def is_included_type(type_name): + """ + Those are types for which we already have a class file implemented. + """ + return type_name in [ + "AABB", + "Basis", + "Color", + "Plane", + "Quaternion", + "Rect2", + "Rect2i", + "Transform2D", + "Transform3D", + "Vector2", + "Vector2i", + "Vector3", + "Vector3i", + ] + +def is_packed_array(type_name): + """ + Those are types for which we add our extra packed array functions. + """ + return type_name in [ + "PackedByteArray", + "PackedColorArray", + "PackedFloat32Array", + "PackedFloat64Array", + "PackedInt32Array", + "PackedInt64Array", + "PackedStringArray", + "PackedVector2Array", + "PackedVector3Array", + ] + def is_enum(type_name): return type_name.startswith("enum::") diff --git a/godot-headers-temp/godot/gdnative_interface.h b/godot-headers-temp/godot/gdnative_interface.h index 3a5b0442..63f4b091 100644 --- a/godot-headers-temp/godot/gdnative_interface.h +++ b/godot-headers-temp/godot/gdnative_interface.h @@ -387,6 +387,32 @@ typedef struct { char32_t *(*string_operator_index)(GDNativeStringPtr p_self, GDNativeInt p_index); const char32_t *(*string_operator_index_const)(const GDNativeStringPtr p_self, GDNativeInt p_index); + /* Packed array functions */ + + uint8_t *(*packed_byte_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedByteArray + const uint8_t *(*packed_byte_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedByteArray + + GDNativeTypePtr (*packed_color_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedColorArray, returns Color ptr + GDNativeTypePtr (*packed_color_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedColorArray, returns Color ptr + + float *(*packed_float32_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedFloat32Array + const float *(*packed_float32_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedFloat32Array + double *(*packed_float64_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedFloat64Array + const double *(*packed_float64_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedFloat64Array + + int32_t *(*packed_int32_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedInt32Array + const int32_t *(*packed_int32_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedInt32Array + int64_t *(*packed_int64_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedInt32Array + const int64_t *(*packed_int64_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedInt32Array + + GDNativeStringPtr (*packed_string_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedStringArray + GDNativeStringPtr (*packed_string_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedStringArray + + GDNativeTypePtr (*packed_vector2_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedVector2Array, returns Vector2 ptr + GDNativeTypePtr (*packed_vector2_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedVector2Array, returns Vector2 ptr + GDNativeTypePtr (*packed_vector3_array_operator_index)(GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedVector3Array, returns Vector3 ptr + GDNativeTypePtr (*packed_vector3_array_operator_index_const)(const GDNativeTypePtr p_self, GDNativeInt p_index); // p_self should be a PackedVector3Array, returns Vector3 ptr + /* OBJECT */ void (*object_method_bind_call)(const GDNativeMethodBindPtr p_method_bind, GDNativeObjectPtr p_instance, const GDNativeVariantPtr *p_args, GDNativeInt p_arg_count, GDNativeVariantPtr r_ret, GDNativeCallError *r_error); diff --git a/include/godot_cpp/core/defs.hpp b/include/godot_cpp/core/defs.hpp index a103985f..21edc9f1 100644 --- a/include/godot_cpp/core/defs.hpp +++ b/include/godot_cpp/core/defs.hpp @@ -92,6 +92,23 @@ #define unlikely(x) x #endif +#ifdef REAL_T_IS_DOUBLE +typedef double real_t; +#else +typedef float real_t; +#endif + +// Generic swap template. +#ifndef SWAP +#define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y)) +template +inline void __swap_tmpl(T &x, T &y) { + T aux = x; + x = y; + y = aux; +} +#endif // SWAP + // Home-made index sequence trick, so it can be used everywhere without the costly include of std::tuple. // https://stackoverflow.com/questions/15014096/c-index-of-type-during-variadic-template-expansion template diff --git a/include/godot_cpp/core/math.hpp b/include/godot_cpp/core/math.hpp new file mode 100644 index 00000000..13949018 --- /dev/null +++ b/include/godot_cpp/core/math.hpp @@ -0,0 +1,424 @@ +#ifndef GODOT_MATH_H +#define GODOT_MATH_H + +#include + +#include + +#include + +namespace godot { +namespace Math { + +// This epsilon should match the one used by Godot for consistency. +// Using `f` when `real_t` is float. +#define CMP_EPSILON 0.00001f +#define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON) + +// This epsilon is for values related to a unit size (scalar or vector len). +#ifdef PRECISE_MATH_CHECKS +#define UNIT_EPSILON 0.00001 +#else +// Tolerate some more floating point error normally. +#define UNIT_EPSILON 0.001 +#endif + +#define Math_SQRT12 0.7071067811865475244008443621048490 +#define Math_SQRT2 1.4142135623730950488016887242 +#define Math_LN2 0.6931471805599453094172321215 +#define Math_PI 3.1415926535897932384626433833 +#define Math_TAU 6.2831853071795864769252867666 +#define Math_E 2.7182818284590452353602874714 +#define Math_INF INFINITY +#define Math_NAN NAN + +// Functions reproduced as in Godot's source code `math_funcs.h`. +// Some are overloads to automatically support changing real_t into either double or float in the way Godot 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 fposmod(double p_x, double p_y) { + double value = Math::fmod(p_x, p_y); + if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) { + value += p_y; + } + value += 0.0; + return value; +} +inline float fposmod(float p_x, float p_y) { + float value = Math::fmod(p_x, p_y); + if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) { + value += p_y; + } + value += 0.0; + return value; +} + +inline float fposmodp(float p_x, float p_y) { + float value = Math::fmod(p_x, p_y); + if (value < 0) { + value += p_y; + } + value += 0.0; + return value; +} +inline double fposmodp(double p_x, double p_y) { + double value = Math::fmod(p_x, p_y); + if (value < 0) { + value += p_y; + } + value += 0.0; + return value; +} + +inline double floor(double p_x) { + return ::floor(p_x); +} +inline float floor(float p_x) { + return ::floorf(p_x); +} + +inline double ceil(double p_x) { + return ::ceil(p_x); +} +inline float ceil(float p_x) { + return ::ceilf(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 sinh(double p_x) { + return ::sinh(p_x); +} +inline float sinh(float p_x) { + return ::sinhf(p_x); +} + +inline float sinc(float p_x) { + return p_x == 0 ? 1 : ::sin(p_x) / p_x; +} +inline double sinc(double p_x) { + return p_x == 0 ? 1 : ::sin(p_x) / p_x; +} + +inline float sincn(float p_x) { + return sinc(Math_PI * p_x); +} +inline double sincn(double p_x) { + return sinc(Math_PI * p_x); +} + +inline double cosh(double p_x) { + return ::cosh(p_x); +} +inline float cosh(float p_x) { + return ::coshf(p_x); +} + +inline double tanh(double p_x) { + return ::tanh(p_x); +} +inline float tanh(float p_x) { + return ::tanhf(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 double pow(double p_x, double p_y) { + return ::pow(p_x, p_y); +} +inline float pow(float p_x, float p_y) { + return ::powf(p_x, p_y); +} + +inline double log(double p_x) { + return ::log(p_x); +} +inline float log(float p_x) { + return ::logf(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; + } + if (x > maxv) { + return maxv; + } + 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); +} + +template +inline T abs(T x) { + return std::abs(x); +} + +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 Math::lerp(p_ostart, p_ostop, Math::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 Math::lerp(p_ostart, p_ostop, Math::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; + } + // Then check for approximate equality. + real_t tolerance = CMP_EPSILON * std::abs(a); + if (tolerance < CMP_EPSILON) { + tolerance = CMP_EPSILON; + } + return std::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; + } + // Then check for approximate equality. + return std::abs(a - b) < tolerance; +} + +inline bool is_zero_approx(real_t s) { + return std::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; + } + 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; + } + 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 std::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 std::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; + } + 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; + } + return p_value; +} + +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; +} + +// This function should be as fast as possible and rounding mode should not matter. +inline int fast_ftoi(float a) { + static int b; + +#if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone? + b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5)); + +#elif defined(_MSC_VER) && _MSC_VER < 1800 + __asm fld a __asm fistp b + /*#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));*/ + +#else + b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint +#endif + return b; +} + +inline double snapped(double p_value, double p_step) { + if (p_step != 0) { + p_value = Math::floor(p_value / p_step + 0.5) * p_step; + } + return p_value; +} + +} // namespace Math +} // namespace godot + +#endif // GODOT_MATH_H diff --git a/include/godot_cpp/variant/aabb.hpp b/include/godot_cpp/variant/aabb.hpp new file mode 100644 index 00000000..446821bb --- /dev/null +++ b/include/godot_cpp/variant/aabb.hpp @@ -0,0 +1,430 @@ +#ifndef GODOT_AABB_HPP +#define GODOT_AABB_HPP + +#include +#include +#include +#include + +/** + * AABB / AABB (Axis Aligned Bounding Box) + * This is implemented by a point (position) and the box size + */ + +namespace godot { + +class AABB { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + Vector3 position; + Vector3 size; + + real_t get_area() const; /// get area + inline bool has_no_area() const { + return (size.x <= 0 || size.y <= 0 || size.z <= 0); + } + + inline bool has_no_surface() const { + return (size.x <= 0 && size.y <= 0 && size.z <= 0); + } + + const Vector3 &get_position() const { return position; } + void set_position(const Vector3 &p_pos) { position = p_pos; } + const Vector3 &get_size() const { return size; } + void set_size(const Vector3 &p_size) { size = p_size; } + + bool operator==(const AABB &p_rval) const; + bool operator!=(const AABB &p_rval) const; + + bool is_equal_approx(const AABB &p_aabb) const; + inline bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap + inline bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap + inline 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 + 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; + inline bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const; + + inline bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const; + inline bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const; + bool intersects_plane(const Plane &p_plane) const; + + inline bool has_point(const Vector3 &p_point) const; + inline Vector3 get_support(const Vector3 &p_normal) const; + + Vector3 get_longest_axis() const; + int get_longest_axis_index() const; + inline real_t get_longest_axis_size() const; + + Vector3 get_shortest_axis() const; + int get_shortest_axis_index() const; + inline real_t get_shortest_axis_size() const; + + AABB grow(real_t p_by) const; + inline void grow_by(real_t p_amount); + + void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const; + inline Vector3 get_endpoint(int p_point) const; + + AABB expand(const Vector3 &p_vector) const; + inline void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const; + inline void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */ + + inline AABB abs() const { + return AABB(Vector3(position.x + Math::min(size.x, (real_t)0), position.y + Math::min(size.y, (real_t)0), position.z + Math::min(size.z, (real_t)0)), size.abs()); + } + + inline void quantize(real_t p_unit); + inline AABB quantized(real_t p_unit) const; + + inline void set_end(const Vector3 &p_end) { + size = p_end - position; + } + + inline Vector3 get_end() const { + return position + size; + } + + operator String() const; + + inline AABB() {} + inline AABB(const Vector3 &p_pos, const Vector3 &p_size) : + position(p_pos), + size(p_size) { + } +}; + +inline bool AABB::intersects(const AABB &p_aabb) const { + if (position.x >= (p_aabb.position.x + p_aabb.size.x)) { + return false; + } + if ((position.x + size.x) <= p_aabb.position.x) { + return false; + } + if (position.y >= (p_aabb.position.y + p_aabb.size.y)) { + return false; + } + if ((position.y + size.y) <= p_aabb.position.y) { + return false; + } + if (position.z >= (p_aabb.position.z + p_aabb.size.z)) { + return false; + } + if ((position.z + size.z) <= p_aabb.position.z) { + return false; + } + + return true; +} + +inline bool AABB::intersects_inclusive(const AABB &p_aabb) const { + if (position.x > (p_aabb.position.x + p_aabb.size.x)) { + return false; + } + if ((position.x + size.x) < p_aabb.position.x) { + return false; + } + if (position.y > (p_aabb.position.y + p_aabb.size.y)) { + return false; + } + if ((position.y + size.y) < p_aabb.position.y) { + return false; + } + if (position.z > (p_aabb.position.z + p_aabb.size.z)) { + return false; + } + if ((position.z + size.z) < p_aabb.position.z) { + return false; + } + + return true; +} + +inline bool AABB::encloses(const AABB &p_aabb) const { + Vector3 src_min = position; + Vector3 src_max = position + size; + Vector3 dst_min = p_aabb.position; + Vector3 dst_max = p_aabb.position + p_aabb.size; + + return ( + (src_min.x <= dst_min.x) && + (src_max.x > dst_max.x) && + (src_min.y <= dst_min.y) && + (src_max.y > dst_max.y) && + (src_min.z <= dst_min.z) && + (src_max.z > dst_max.z)); +} + +Vector3 AABB::get_support(const Vector3 &p_normal) const { + Vector3 half_extents = size * 0.5; + Vector3 ofs = position + half_extents; + + return Vector3( + (p_normal.x > 0) ? half_extents.x : -half_extents.x, + (p_normal.y > 0) ? half_extents.y : -half_extents.y, + (p_normal.z > 0) ? half_extents.z : -half_extents.z) + + ofs; +} + +Vector3 AABB::get_endpoint(int p_point) const { + switch (p_point) { + case 0: + return Vector3(position.x, position.y, position.z); + case 1: + return Vector3(position.x, position.y, position.z + size.z); + case 2: + return Vector3(position.x, position.y + size.y, position.z); + case 3: + return Vector3(position.x, position.y + size.y, position.z + size.z); + case 4: + return Vector3(position.x + size.x, position.y, position.z); + case 5: + return Vector3(position.x + size.x, position.y, position.z + size.z); + case 6: + return Vector3(position.x + size.x, position.y + size.y, position.z); + case 7: + return Vector3(position.x + size.x, position.y + size.y, position.z + size.z); + } + + ERR_FAIL_V(Vector3()); +} + +bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const { + Vector3 half_extents = size * 0.5; + Vector3 ofs = position + half_extents; + + for (int i = 0; i < p_plane_count; i++) { + const Plane &p = p_planes[i]; + Vector3 point( + (p.normal.x > 0) ? -half_extents.x : half_extents.x, + (p.normal.y > 0) ? -half_extents.y : half_extents.y, + (p.normal.z > 0) ? -half_extents.z : half_extents.z); + point += ofs; + if (p.is_point_over(point)) { + return false; + } + } + + // Make sure all points in the shape aren't fully separated from the AABB on + // each axis. + int bad_point_counts_positive[3] = { 0 }; + int bad_point_counts_negative[3] = { 0 }; + + for (int k = 0; k < 3; k++) { + for (int i = 0; i < p_point_count; i++) { + if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) { + bad_point_counts_positive[k]++; + } + if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) { + bad_point_counts_negative[k]++; + } + } + + if (bad_point_counts_negative[k] == p_point_count) { + return false; + } + if (bad_point_counts_positive[k] == p_point_count) { + return false; + } + } + + return true; +} + +bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const { + Vector3 half_extents = size * 0.5; + Vector3 ofs = position + half_extents; + + for (int i = 0; i < p_plane_count; i++) { + const Plane &p = p_planes[i]; + Vector3 point( + (p.normal.x < 0) ? -half_extents.x : half_extents.x, + (p.normal.y < 0) ? -half_extents.y : half_extents.y, + (p.normal.z < 0) ? -half_extents.z : half_extents.z); + point += ofs; + if (p.is_point_over(point)) { + return false; + } + } + + return true; +} + +bool AABB::has_point(const Vector3 &p_point) const { + if (p_point.x < position.x) { + return false; + } + if (p_point.y < position.y) { + return false; + } + if (p_point.z < position.z) { + return false; + } + if (p_point.x > position.x + size.x) { + return false; + } + if (p_point.y > position.y + size.y) { + return false; + } + if (p_point.z > position.z + size.z) { + return false; + } + + return true; +} + +inline void AABB::expand_to(const Vector3 &p_vector) { + Vector3 begin = position; + Vector3 end = position + size; + + if (p_vector.x < begin.x) { + begin.x = p_vector.x; + } + if (p_vector.y < begin.y) { + begin.y = p_vector.y; + } + if (p_vector.z < begin.z) { + begin.z = p_vector.z; + } + + if (p_vector.x > end.x) { + end.x = p_vector.x; + } + if (p_vector.y > end.y) { + end.y = p_vector.y; + } + if (p_vector.z > end.z) { + end.z = p_vector.z; + } + + position = begin; + size = end - begin; +} + +void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const { + Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5); + Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z); + + real_t length = p_plane.normal.abs().dot(half_extents); + real_t distance = p_plane.distance_to(center); + r_min = distance - length; + r_max = distance + length; +} + +inline real_t AABB::get_longest_axis_size() const { + real_t max_size = size.x; + + if (size.y > max_size) { + max_size = size.y; + } + + if (size.z > max_size) { + max_size = size.z; + } + + return max_size; +} + +inline real_t AABB::get_shortest_axis_size() const { + real_t max_size = size.x; + + if (size.y < max_size) { + max_size = size.y; + } + + if (size.z < max_size) { + max_size = size.z; + } + + return max_size; +} + +bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const { + real_t divx = 1.0 / p_dir.x; + real_t divy = 1.0 / p_dir.y; + real_t divz = 1.0 / p_dir.z; + + Vector3 upbound = position + size; + real_t tmin, tmax, tymin, tymax, tzmin, tzmax; + if (p_dir.x >= 0) { + tmin = (position.x - p_from.x) * divx; + tmax = (upbound.x - p_from.x) * divx; + } else { + tmin = (upbound.x - p_from.x) * divx; + tmax = (position.x - p_from.x) * divx; + } + if (p_dir.y >= 0) { + tymin = (position.y - p_from.y) * divy; + tymax = (upbound.y - p_from.y) * divy; + } else { + tymin = (upbound.y - p_from.y) * divy; + tymax = (position.y - p_from.y) * divy; + } + if ((tmin > tymax) || (tymin > tmax)) { + return false; + } + if (tymin > tmin) { + tmin = tymin; + } + if (tymax < tmax) { + tmax = tymax; + } + if (p_dir.z >= 0) { + tzmin = (position.z - p_from.z) * divz; + tzmax = (upbound.z - p_from.z) * divz; + } else { + tzmin = (upbound.z - p_from.z) * divz; + tzmax = (position.z - p_from.z) * divz; + } + if ((tmin > tzmax) || (tzmin > tmax)) { + return false; + } + if (tzmin > tmin) { + tmin = tzmin; + } + if (tzmax < tmax) { + tmax = tzmax; + } + return ((tmin < t1) && (tmax > t0)); +} + +void AABB::grow_by(real_t p_amount) { + position.x -= p_amount; + position.y -= p_amount; + position.z -= p_amount; + size.x += 2.0 * p_amount; + size.y += 2.0 * p_amount; + size.z += 2.0 * p_amount; +} + +void AABB::quantize(real_t p_unit) { + size += position; + + position.x -= Math::fposmodp(position.x, p_unit); + position.y -= Math::fposmodp(position.y, p_unit); + position.z -= Math::fposmodp(position.z, p_unit); + + size.x -= Math::fposmodp(size.x, p_unit); + size.y -= Math::fposmodp(size.y, p_unit); + size.z -= Math::fposmodp(size.z, p_unit); + + size.x += p_unit; + size.y += p_unit; + size.z += p_unit; + + size -= position; +} + +AABB AABB::quantized(real_t p_unit) const { + AABB ret = *this; + ret.quantize(p_unit); + return ret; +} + +} // namespace godot + +#endif // GODOT_AABB_HPP diff --git a/include/godot_cpp/variant/basis.hpp b/include/godot_cpp/variant/basis.hpp new file mode 100644 index 00000000..b14bba0a --- /dev/null +++ b/include/godot_cpp/variant/basis.hpp @@ -0,0 +1,310 @@ +#ifndef GODOT_BASIS_HPP +#define GODOT_BASIS_HPP + +#include +#include +#include + +namespace godot { + +class Basis { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + Vector3 elements[3] = { + Vector3(1, 0, 0), + Vector3(0, 1, 0), + Vector3(0, 0, 1) + }; + + inline const Vector3 &operator[](int axis) const { + return elements[axis]; + } + inline Vector3 &operator[](int axis) { + return elements[axis]; + } + + void invert(); + void transpose(); + + Basis inverse() const; + Basis transposed() const; + + inline real_t determinant() const; + + void from_z(const Vector3 &p_z); + + inline Vector3 get_axis(int p_axis) const { + // get actual basis axis (elements is transposed for performance) + return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]); + } + inline void set_axis(int p_axis, const Vector3 &p_value) { + // get actual basis axis (elements is transposed for performance) + elements[0][p_axis] = p_value.x; + elements[1][p_axis] = p_value.y; + elements[2][p_axis] = p_value.z; + } + + void rotate(const Vector3 &p_axis, real_t p_phi); + Basis rotated(const Vector3 &p_axis, real_t p_phi) const; + + void rotate_local(const Vector3 &p_axis, real_t p_phi); + Basis rotated_local(const Vector3 &p_axis, real_t p_phi) const; + + void rotate(const Vector3 &p_euler); + Basis rotated(const Vector3 &p_euler) const; + + void rotate(const Quaternion &p_quat); + Basis rotated(const Quaternion &p_quat) const; + + Vector3 get_rotation_euler() const; + void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const; + void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const; + Quaternion get_rotation_quat() const; + Vector3 get_rotation() const { return get_rotation_euler(); }; + + Vector3 rotref_posscale_decomposition(Basis &rotref) const; + + Vector3 get_euler_xyz() const; + void set_euler_xyz(const Vector3 &p_euler); + + Vector3 get_euler_xzy() const; + void set_euler_xzy(const Vector3 &p_euler); + + Vector3 get_euler_yzx() const; + void set_euler_yzx(const Vector3 &p_euler); + + Vector3 get_euler_yxz() const; + void set_euler_yxz(const Vector3 &p_euler); + + Vector3 get_euler_zxy() const; + void set_euler_zxy(const Vector3 &p_euler); + + Vector3 get_euler_zyx() const; + void set_euler_zyx(const Vector3 &p_euler); + + Quaternion get_quat() const; + void set_quat(const Quaternion &p_quat); + + Vector3 get_euler() const { return get_euler_yxz(); } + void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); } + + void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const; + void set_axis_angle(const Vector3 &p_axis, real_t p_phi); + + void scale(const Vector3 &p_scale); + Basis scaled(const Vector3 &p_scale) const; + + void scale_local(const Vector3 &p_scale); + Basis scaled_local(const Vector3 &p_scale) const; + + void make_scale_uniform(); + float get_uniform_scale() const; + + Vector3 get_scale() const; + Vector3 get_scale_abs() const; + Vector3 get_scale_local() const; + + void set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale); + void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale); + void set_quat_scale(const Quaternion &p_quat, const Vector3 &p_scale); + + // transposed dot products + inline real_t tdotx(const Vector3 &v) const { + return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2]; + } + inline real_t tdoty(const Vector3 &v) const { + return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2]; + } + inline real_t tdotz(const Vector3 &v) const { + return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2]; + } + + bool is_equal_approx(const Basis &p_basis) const; + + bool operator==(const Basis &p_matrix) const; + bool operator!=(const Basis &p_matrix) const; + + inline Vector3 xform(const Vector3 &p_vector) const; + inline Vector3 xform_inv(const Vector3 &p_vector) const; + inline void operator*=(const Basis &p_matrix); + inline Basis operator*(const Basis &p_matrix) const; + inline void operator+=(const Basis &p_matrix); + inline Basis operator+(const Basis &p_matrix) const; + inline void operator-=(const Basis &p_matrix); + inline Basis operator-(const Basis &p_matrix) const; + inline void operator*=(real_t p_val); + inline Basis operator*(real_t p_val) const; + + int get_orthogonal_index() const; + void set_orthogonal_index(int p_index); + + void set_diagonal(const Vector3 &p_diag); + + bool is_orthogonal() const; + bool is_diagonal() const; + bool is_rotation() const; + + Basis slerp(const Basis &p_to, const real_t &p_weight) const; + void rotate_sh(real_t *p_values); + + operator String() const; + + /* create / set */ + + inline void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { + elements[0][0] = xx; + elements[0][1] = xy; + elements[0][2] = xz; + elements[1][0] = yx; + elements[1][1] = yy; + elements[1][2] = yz; + elements[2][0] = zx; + elements[2][1] = zy; + elements[2][2] = zz; + } + inline void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) { + set_axis(0, p_x); + set_axis(1, p_y); + set_axis(2, p_z); + } + inline Vector3 get_column(int i) const { + return Vector3(elements[0][i], elements[1][i], elements[2][i]); + } + + inline Vector3 get_row(int i) const { + return Vector3(elements[i][0], elements[i][1], elements[i][2]); + } + inline Vector3 get_main_diagonal() const { + return Vector3(elements[0][0], elements[1][1], elements[2][2]); + } + + inline void set_row(int i, const Vector3 &p_row) { + elements[i][0] = p_row.x; + elements[i][1] = p_row.y; + elements[i][2] = p_row.z; + } + + inline void set_zero() { + elements[0].zero(); + elements[1].zero(); + elements[2].zero(); + } + + inline Basis transpose_xform(const Basis &m) const { + return Basis( + elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x, + elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y, + elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z, + elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x, + elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y, + elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z, + elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x, + elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y, + elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z); + } + Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { + set(xx, xy, xz, yx, yy, yz, zx, zy, zz); + } + + void orthonormalize(); + Basis orthonormalized() const; + +#ifdef MATH_CHECKS + bool is_symmetric() const; +#endif + Basis diagonalize(); + + operator Quaternion() const { return get_quat(); } + + Basis(const Quaternion &p_quat) { set_quat(p_quat); }; + Basis(const Quaternion &p_quat, const Vector3 &p_scale) { set_quat_scale(p_quat, p_scale); } + + Basis(const Vector3 &p_euler) { set_euler(p_euler); } + Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); } + + Basis(const Vector3 &p_axis, real_t p_phi) { set_axis_angle(p_axis, p_phi); } + Basis(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_phi, p_scale); } + + inline Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) { + elements[0] = row0; + elements[1] = row1; + elements[2] = row2; + } + + inline Basis() {} +}; + +inline void Basis::operator*=(const Basis &p_matrix) { + set( + p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), + p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), + p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2])); +} + +inline Basis Basis::operator*(const Basis &p_matrix) const { + return Basis( + p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), + p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), + p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2])); +} + +inline void Basis::operator+=(const Basis &p_matrix) { + elements[0] += p_matrix.elements[0]; + elements[1] += p_matrix.elements[1]; + elements[2] += p_matrix.elements[2]; +} + +inline Basis Basis::operator+(const Basis &p_matrix) const { + Basis ret(*this); + ret += p_matrix; + return ret; +} + +inline void Basis::operator-=(const Basis &p_matrix) { + elements[0] -= p_matrix.elements[0]; + elements[1] -= p_matrix.elements[1]; + elements[2] -= p_matrix.elements[2]; +} + +inline Basis Basis::operator-(const Basis &p_matrix) const { + Basis ret(*this); + ret -= p_matrix; + return ret; +} + +inline void Basis::operator*=(real_t p_val) { + elements[0] *= p_val; + elements[1] *= p_val; + elements[2] *= p_val; +} + +inline Basis Basis::operator*(real_t p_val) const { + Basis ret(*this); + ret *= p_val; + return ret; +} + +Vector3 Basis::xform(const Vector3 &p_vector) const { + return Vector3( + elements[0].dot(p_vector), + elements[1].dot(p_vector), + elements[2].dot(p_vector)); +} + +Vector3 Basis::xform_inv(const Vector3 &p_vector) const { + return Vector3( + (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z), + (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z), + (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z)); +} + +real_t Basis::determinant() const { + return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) - + elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) + + elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]); +} + +} // namespace godot + +#endif // GODOT_BASIS_HPP diff --git a/include/godot_cpp/variant/color.hpp b/include/godot_cpp/variant/color.hpp new file mode 100644 index 00000000..07dc054c --- /dev/null +++ b/include/godot_cpp/variant/color.hpp @@ -0,0 +1,257 @@ +#ifndef GODOT_COLOR_HPP +#define GODOT_COLOR_HPP + +#include + +namespace godot { + +class String; + +class Color { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + union { + struct { + float r; + float g; + float b; + float a; + }; + float components[4] = { 0, 0, 0, 1.0 }; + }; + + uint32_t to_rgba32() const; + uint32_t to_argb32() const; + uint32_t to_abgr32() const; + uint64_t to_rgba64() const; + uint64_t to_argb64() const; + uint64_t to_abgr64() const; + float get_h() const; + float get_s() const; + float get_v() const; + void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0); + + inline float &operator[](int p_idx) { + return components[p_idx]; + } + inline const float &operator[](int p_idx) const { + return components[p_idx]; + } + + bool operator==(const Color &p_color) const { + return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a); + } + bool operator!=(const Color &p_color) const { + return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a); + } + + Color operator+(const Color &p_color) const; + void operator+=(const Color &p_color); + + Color operator-() const; + Color operator-(const Color &p_color) const; + void operator-=(const Color &p_color); + + Color operator*(const Color &p_color) const; + Color operator*(float p_scalar) const; + void operator*=(const Color &p_color); + void operator*=(float p_scalar); + + Color operator/(const Color &p_color) const; + Color operator/(float p_scalar) const; + void operator/=(const Color &p_color); + void operator/=(float p_scalar); + + bool is_equal_approx(const Color &p_color) const; + + void invert(); + Color inverted() const; + + inline Color lerp(const Color &p_to, float p_weight) const { + Color res = *this; + + res.r += (p_weight * (p_to.r - r)); + res.g += (p_weight * (p_to.g - g)); + res.b += (p_weight * (p_to.b - b)); + res.a += (p_weight * (p_to.a - a)); + + return res; + } + + inline Color darkened(float p_amount) const { + Color res = *this; + res.r = res.r * (1.0f - p_amount); + res.g = res.g * (1.0f - p_amount); + res.b = res.b * (1.0f - p_amount); + return res; + } + + inline Color lightened(float p_amount) const { + Color res = *this; + res.r = res.r + (1.0f - res.r) * p_amount; + res.g = res.g + (1.0f - res.g) * p_amount; + res.b = res.b + (1.0f - res.b) * p_amount; + return res; + } + + inline uint32_t to_rgbe9995() const { + const float pow2to9 = 512.0f; + const float B = 15.0f; + const float N = 9.0f; + + float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f) + + float cRed = Math::max(0.0f, Math::min(sharedexp, r)); + float cGreen = Math::max(0.0f, Math::min(sharedexp, g)); + float cBlue = Math::max(0.0f, Math::min(sharedexp, b)); + + float cMax = Math::max(cRed, Math::max(cGreen, cBlue)); + + float expp = Math::max(-B - 1.0f, Math::floor(Math::log(cMax) / (float)Math_LN2)) + 1.0f + B; + + float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f); + + float exps = expp + 1.0f; + + if (0.0 <= sMax && sMax < pow2to9) { + exps = expp; + } + + float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f); + float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f); + float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f); + + return (uint32_t(Math::fast_ftoi(sRed)) & 0x1FF) | ((uint32_t(Math::fast_ftoi(sGreen)) & 0x1FF) << 9) | ((uint32_t(Math::fast_ftoi(sBlue)) & 0x1FF) << 18) | ((uint32_t(Math::fast_ftoi(exps)) & 0x1F) << 27); + } + + inline Color blend(const Color &p_over) const { + Color res; + float sa = 1.0 - p_over.a; + res.a = a * sa + p_over.a; + if (res.a == 0) { + return Color(0, 0, 0, 0); + } else { + res.r = (r * a * sa + p_over.r * p_over.a) / res.a; + res.g = (g * a * sa + p_over.g * p_over.a) / res.a; + res.b = (b * a * sa + p_over.b * p_over.a) / res.a; + } + return res; + } + + inline Color to_linear() const { + return Color( + r < 0.04045 ? r * (1.0 / 12.92) : Math::pow((r + 0.055) * (1.0 / (1 + 0.055)), 2.4), + g < 0.04045 ? g * (1.0 / 12.92) : Math::pow((g + 0.055) * (1.0 / (1 + 0.055)), 2.4), + b < 0.04045 ? b * (1.0 / 12.92) : Math::pow((b + 0.055) * (1.0 / (1 + 0.055)), 2.4), + a); + } + inline Color to_srgb() const { + return Color( + r < 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055, + g < 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055, + b < 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a); + } + + static Color hex(uint32_t p_hex); + static Color hex64(uint64_t p_hex); + static Color html(const String &p_rgba); + static bool html_is_valid(const String &p_color); + static Color named(const String &p_name); + static Color named(const String &p_name, const Color &p_default); + static int find_named_color(const String &p_name); + static int get_named_color_count(); + static String get_named_color_name(int p_idx); + static Color get_named_color(int p_idx); + static Color from_string(const String &p_string, const Color &p_default); + String to_html(bool p_alpha = true) const; + static Color from_hsv(float p_h, float p_s, float p_v, float p_a); + static Color from_rgbe9995(uint32_t p_rgbe); + + inline bool operator<(const Color &p_color) const; //used in set keys + operator String() const; + + // For the binder. + inline void set_r8(int32_t r8) { r = (Math::clamp(r8, 0, 255) / 255.0); } + inline int32_t get_r8() const { return int32_t(Math::clamp(r * 255.0, 0.0, 255.0)); } + inline void set_g8(int32_t g8) { g = (Math::clamp(g8, 0, 255) / 255.0); } + inline int32_t get_g8() const { return int32_t(Math::clamp(g * 255.0, 0.0, 255.0)); } + inline void set_b8(int32_t b8) { b = (Math::clamp(b8, 0, 255) / 255.0); } + inline int32_t get_b8() const { return int32_t(Math::clamp(b * 255.0, 0.0, 255.0)); } + inline void set_a8(int32_t a8) { a = (Math::clamp(a8, 0, 255) / 255.0); } + inline int32_t get_a8() const { return int32_t(Math::clamp(a * 255.0, 0.0, 255.0)); } + + inline void set_h(float p_h) { set_hsv(p_h, get_s(), get_v()); } + inline void set_s(float p_s) { set_hsv(get_h(), p_s, get_v()); } + inline void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v); } + + inline Color() {} + + /** + * RGBA construct parameters. + * Alpha is not optional as otherwise we can't bind the RGB version for scripting. + */ + inline Color(float p_r, float p_g, float p_b, float p_a) { + r = p_r; + g = p_g; + b = p_b; + a = p_a; + } + + /** + * RGB construct parameters. + */ + inline Color(float p_r, float p_g, float p_b) { + r = p_r; + g = p_g; + b = p_b; + a = 1.0; + } + + /** + * Construct a Color from another Color, but with the specified alpha value. + */ + inline Color(const Color &p_c, float p_a) { + r = p_c.r; + g = p_c.g; + b = p_c.b; + a = p_a; + } + + Color(const String &p_code) { + if (html_is_valid(p_code)) { + *this = html(p_code); + } else { + *this = named(p_code); + } + } + + Color(const String &p_code, float p_a) { + *this = Color(p_code); + a = p_a; + } +}; + +bool Color::operator<(const Color &p_color) const { + if (r == p_color.r) { + if (g == p_color.g) { + if (b == p_color.b) { + return (a < p_color.a); + } else { + return (b < p_color.b); + } + } else { + return g < p_color.g; + } + } else { + return r < p_color.r; + } +} + +inline Color operator*(float p_scalar, const Color &p_color) { + return p_color * p_scalar; +} + +} // namespace godot + +#endif // GODOT_COLOR_HPP diff --git a/include/godot_cpp/variant/color_names.inc.hpp b/include/godot_cpp/variant/color_names.inc.hpp new file mode 100644 index 00000000..13664574 --- /dev/null +++ b/include/godot_cpp/variant/color_names.inc.hpp @@ -0,0 +1,158 @@ +namespace godot { + +struct NamedColor { + const char *name; + Color color; +}; + +static NamedColor named_colors[] = { + { "aliceblue", Color(0.94, 0.97, 1.00) }, + { "antiquewhite", Color(0.98, 0.92, 0.84) }, + { "aqua", Color(0.00, 1.00, 1.00) }, + { "aquamarine", Color(0.50, 1.00, 0.83) }, + { "azure", Color(0.94, 1.00, 1.00) }, + { "beige", Color(0.96, 0.96, 0.86) }, + { "bisque", Color(1.00, 0.89, 0.77) }, + { "black", Color(0.00, 0.00, 0.00) }, + { "blanchedalmond", Color(1.00, 0.92, 0.80) }, + { "blue", Color(0.00, 0.00, 1.00) }, + { "blueviolet", Color(0.54, 0.17, 0.89) }, + { "brown", Color(0.65, 0.16, 0.16) }, + { "burlywood", Color(0.87, 0.72, 0.53) }, + { "cadetblue", Color(0.37, 0.62, 0.63) }, + { "chartreuse", Color(0.50, 1.00, 0.00) }, + { "chocolate", Color(0.82, 0.41, 0.12) }, + { "coral", Color(1.00, 0.50, 0.31) }, + { "cornflower", Color(0.39, 0.58, 0.93) }, + { "cornsilk", Color(1.00, 0.97, 0.86) }, + { "crimson", Color(0.86, 0.08, 0.24) }, + { "cyan", Color(0.00, 1.00, 1.00) }, + { "darkblue", Color(0.00, 0.00, 0.55) }, + { "darkcyan", Color(0.00, 0.55, 0.55) }, + { "darkgoldenrod", Color(0.72, 0.53, 0.04) }, + { "darkgray", Color(0.66, 0.66, 0.66) }, + { "darkgreen", Color(0.00, 0.39, 0.00) }, + { "darkkhaki", Color(0.74, 0.72, 0.42) }, + { "darkmagenta", Color(0.55, 0.00, 0.55) }, + { "darkolivegreen", Color(0.33, 0.42, 0.18) }, + { "darkorange", Color(1.00, 0.55, 0.00) }, + { "darkorchid", Color(0.60, 0.20, 0.80) }, + { "darkred", Color(0.55, 0.00, 0.00) }, + { "darksalmon", Color(0.91, 0.59, 0.48) }, + { "darkseagreen", Color(0.56, 0.74, 0.56) }, + { "darkslateblue", Color(0.28, 0.24, 0.55) }, + { "darkslategray", Color(0.18, 0.31, 0.31) }, + { "darkturquoise", Color(0.00, 0.81, 0.82) }, + { "darkviolet", Color(0.58, 0.00, 0.83) }, + { "deeppink", Color(1.00, 0.08, 0.58) }, + { "deepskyblue", Color(0.00, 0.75, 1.00) }, + { "dimgray", Color(0.41, 0.41, 0.41) }, + { "dodgerblue", Color(0.12, 0.56, 1.00) }, + { "firebrick", Color(0.70, 0.13, 0.13) }, + { "floralwhite", Color(1.00, 0.98, 0.94) }, + { "forestgreen", Color(0.13, 0.55, 0.13) }, + { "fuchsia", Color(1.00, 0.00, 1.00) }, + { "gainsboro", Color(0.86, 0.86, 0.86) }, + { "ghostwhite", Color(0.97, 0.97, 1.00) }, + { "gold", Color(1.00, 0.84, 0.00) }, + { "goldenrod", Color(0.85, 0.65, 0.13) }, + { "gray", Color(0.75, 0.75, 0.75) }, + { "green", Color(0.00, 1.00, 0.00) }, + { "greenyellow", Color(0.68, 1.00, 0.18) }, + { "honeydew", Color(0.94, 1.00, 0.94) }, + { "hotpink", Color(1.00, 0.41, 0.71) }, + { "indianred", Color(0.80, 0.36, 0.36) }, + { "indigo", Color(0.29, 0.00, 0.51) }, + { "ivory", Color(1.00, 1.00, 0.94) }, + { "khaki", Color(0.94, 0.90, 0.55) }, + { "lavender", Color(0.90, 0.90, 0.98) }, + { "lavenderblush", Color(1.00, 0.94, 0.96) }, + { "lawngreen", Color(0.49, 0.99, 0.00) }, + { "lemonchiffon", Color(1.00, 0.98, 0.80) }, + { "lightblue", Color(0.68, 0.85, 0.90) }, + { "lightcoral", Color(0.94, 0.50, 0.50) }, + { "lightcyan", Color(0.88, 1.00, 1.00) }, + { "lightgoldenrod", Color(0.98, 0.98, 0.82) }, + { "lightgray", Color(0.83, 0.83, 0.83) }, + { "lightgreen", Color(0.56, 0.93, 0.56) }, + { "lightpink", Color(1.00, 0.71, 0.76) }, + { "lightsalmon", Color(1.00, 0.63, 0.48) }, + { "lightseagreen", Color(0.13, 0.70, 0.67) }, + { "lightskyblue", Color(0.53, 0.81, 0.98) }, + { "lightslategray", Color(0.47, 0.53, 0.60) }, + { "lightsteelblue", Color(0.69, 0.77, 0.87) }, + { "lightyellow", Color(1.00, 1.00, 0.88) }, + { "lime", Color(0.00, 1.00, 0.00) }, + { "limegreen", Color(0.20, 0.80, 0.20) }, + { "linen", Color(0.98, 0.94, 0.90) }, + { "magenta", Color(1.00, 0.00, 1.00) }, + { "maroon", Color(0.69, 0.19, 0.38) }, + { "mediumaquamarine", Color(0.40, 0.80, 0.67) }, + { "mediumblue", Color(0.00, 0.00, 0.80) }, + { "mediumorchid", Color(0.73, 0.33, 0.83) }, + { "mediumpurple", Color(0.58, 0.44, 0.86) }, + { "mediumseagreen", Color(0.24, 0.70, 0.44) }, + { "mediumslateblue", Color(0.48, 0.41, 0.93) }, + { "mediumspringgreen", Color(0.00, 0.98, 0.60) }, + { "mediumturquoise", Color(0.28, 0.82, 0.80) }, + { "mediumvioletred", Color(0.78, 0.08, 0.52) }, + { "midnightblue", Color(0.10, 0.10, 0.44) }, + { "mintcream", Color(0.96, 1.00, 0.98) }, + { "mistyrose", Color(1.00, 0.89, 0.88) }, + { "moccasin", Color(1.00, 0.89, 0.71) }, + { "navajowhite", Color(1.00, 0.87, 0.68) }, + { "navyblue", Color(0.00, 0.00, 0.50) }, + { "oldlace", Color(0.99, 0.96, 0.90) }, + { "olive", Color(0.50, 0.50, 0.00) }, + { "olivedrab", Color(0.42, 0.56, 0.14) }, + { "orange", Color(1.00, 0.65, 0.00) }, + { "orangered", Color(1.00, 0.27, 0.00) }, + { "orchid", Color(0.85, 0.44, 0.84) }, + { "palegoldenrod", Color(0.93, 0.91, 0.67) }, + { "palegreen", Color(0.60, 0.98, 0.60) }, + { "paleturquoise", Color(0.69, 0.93, 0.93) }, + { "palevioletred", Color(0.86, 0.44, 0.58) }, + { "papayawhip", Color(1.00, 0.94, 0.84) }, + { "peachpuff", Color(1.00, 0.85, 0.73) }, + { "peru", Color(0.80, 0.52, 0.25) }, + { "pink", Color(1.00, 0.75, 0.80) }, + { "plum", Color(0.87, 0.63, 0.87) }, + { "powderblue", Color(0.69, 0.88, 0.90) }, + { "purple", Color(0.63, 0.13, 0.94) }, + { "rebeccapurple", Color(0.40, 0.20, 0.60) }, + { "red", Color(1.00, 0.00, 0.00) }, + { "rosybrown", Color(0.74, 0.56, 0.56) }, + { "royalblue", Color(0.25, 0.41, 0.88) }, + { "saddlebrown", Color(0.55, 0.27, 0.07) }, + { "salmon", Color(0.98, 0.50, 0.45) }, + { "sandybrown", Color(0.96, 0.64, 0.38) }, + { "seagreen", Color(0.18, 0.55, 0.34) }, + { "seashell", Color(1.00, 0.96, 0.93) }, + { "sienna", Color(0.63, 0.32, 0.18) }, + { "silver", Color(0.75, 0.75, 0.75) }, + { "skyblue", Color(0.53, 0.81, 0.92) }, + { "slateblue", Color(0.42, 0.35, 0.80) }, + { "slategray", Color(0.44, 0.50, 0.56) }, + { "snow", Color(1.00, 0.98, 0.98) }, + { "springgreen", Color(0.00, 1.00, 0.50) }, + { "steelblue", Color(0.27, 0.51, 0.71) }, + { "tan", Color(0.82, 0.71, 0.55) }, + { "teal", Color(0.00, 0.50, 0.50) }, + { "thistle", Color(0.85, 0.75, 0.85) }, + { "tomato", Color(1.00, 0.39, 0.28) }, + { "transparent", Color(1.00, 1.00, 1.00, 0.00) }, + { "turquoise", Color(0.25, 0.88, 0.82) }, + { "violet", Color(0.93, 0.51, 0.93) }, + { "webgray", Color(0.50, 0.50, 0.50) }, + { "webgreen", Color(0.00, 0.50, 0.00) }, + { "webmaroon", Color(0.50, 0.00, 0.00) }, + { "webpurple", Color(0.50, 0.00, 0.50) }, + { "wheat", Color(0.96, 0.87, 0.70) }, + { "white", Color(1.00, 1.00, 1.00) }, + { "whitesmoke", Color(0.96, 0.96, 0.96) }, + { "yellow", Color(1.00, 1.00, 0.00) }, + { "yellowgreen", Color(0.60, 0.80, 0.20) }, + { nullptr, Color() }, +}; + +} // namespace godot diff --git a/include/godot_cpp/variant/plane.hpp b/include/godot_cpp/variant/plane.hpp new file mode 100644 index 00000000..6bf7f99b --- /dev/null +++ b/include/godot_cpp/variant/plane.hpp @@ -0,0 +1,106 @@ +#ifndef GODOT_PLANE_HPP +#define GODOT_PLANE_HPP + +#include +#include +#include + +namespace godot { + +class Plane { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + Vector3 normal; + real_t d = 0; + + void set_normal(const Vector3 &p_normal); + inline Vector3 get_normal() const { return normal; }; ///Point is coplanar, CMP_EPSILON for precision + + void normalize(); + Plane normalized() const; + + /* Plane-Point operations */ + + inline Vector3 center() const { return normal * d; } + Vector3 get_any_perpendicular_normal() const; + + inline bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane + inline real_t distance_to(const Vector3 &p_point) const; + inline bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const; + + /* intersections */ + + bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = nullptr) const; + bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const; + bool intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const; + + inline Vector3 project(const Vector3 &p_point) const { + return p_point - normal * distance_to(p_point); + } + + /* misc */ + + Plane operator-() const { return Plane(-normal, -d); } + bool is_equal_approx(const Plane &p_plane) const; + bool is_equal_approx_any_side(const Plane &p_plane) const; + + inline bool operator==(const Plane &p_plane) const; + inline bool operator!=(const Plane &p_plane) const; + operator String() const; + + inline Plane() {} + inline Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) : + normal(p_a, p_b, p_c), + d(p_d) {} + + inline Plane(const Vector3 &p_normal, real_t p_d); + inline Plane(const Vector3 &p_point, const Vector3 &p_normal); + inline Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE); +}; + +bool Plane::is_point_over(const Vector3 &p_point) const { + return (normal.dot(p_point) > d); +} + +real_t Plane::distance_to(const Vector3 &p_point) const { + return (normal.dot(p_point) - d); +} + +bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const { + real_t dist = normal.dot(p_point) - d; + dist = Math::abs(dist); + return (dist <= _epsilon); +} + +Plane::Plane(const Vector3 &p_normal, real_t p_d) : + normal(p_normal), + d(p_d) { +} + +Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) : + normal(p_normal), + d(p_normal.dot(p_point)) { +} + +Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) { + if (p_dir == CLOCKWISE) { + normal = (p_point1 - p_point3).cross(p_point1 - p_point2); + } else { + normal = (p_point1 - p_point2).cross(p_point1 - p_point3); + } + + normal.normalize(); + d = normal.dot(p_point1); +} + +bool Plane::operator==(const Plane &p_plane) const { + return normal == p_plane.normal && d == p_plane.d; +} + +bool Plane::operator!=(const Plane &p_plane) const { + return normal != p_plane.normal || d != p_plane.d; +} +} // namespace godot + +#endif // GODOT_PLANE_HPP diff --git a/include/godot_cpp/variant/quaternion.hpp b/include/godot_cpp/variant/quaternion.hpp new file mode 100644 index 00000000..a113ee0b --- /dev/null +++ b/include/godot_cpp/variant/quaternion.hpp @@ -0,0 +1,212 @@ +#ifndef GODOT_QUAT_HPP +#define GODOT_QUAT_HPP + +#include +#include + +namespace godot { + +class Quaternion { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + union { + struct { + real_t x; + real_t y; + real_t z; + real_t w; + }; + real_t components[4] = { 0, 0, 0, 1.0 }; + }; + + inline real_t &operator[](int idx) { + return components[idx]; + } + inline const real_t &operator[](int idx) const { + return components[idx]; + } + inline real_t length_squared() const; + bool is_equal_approx(const Quaternion &p_quat) const; + real_t length() const; + void normalize(); + Quaternion normalized() const; + bool is_normalized() const; + Quaternion inverse() const; + inline real_t dot(const Quaternion &p_q) const; + + Vector3 get_euler_xyz() const; + Vector3 get_euler_yxz() const; + Vector3 get_euler() const { return get_euler_yxz(); }; + + Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const; + Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const; + Quaternion cubic_slerp(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const; + + inline void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { + r_angle = 2 * Math::acos(w); + real_t r = ((real_t)1) / Math::sqrt(1 - w * w); + r_axis.x = x * r; + r_axis.y = y * r; + r_axis.z = z * r; + } + + void operator*=(const Quaternion &p_q); + Quaternion operator*(const Quaternion &p_q) const; + + Quaternion operator*(const Vector3 &v) const { + return Quaternion(w * v.x + y * v.z - z * v.y, + w * v.y + z * v.x - x * v.z, + w * v.z + x * v.y - y * v.x, + -x * v.x - y * v.y - z * v.z); + } + + inline Vector3 xform(const Vector3 &v) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_normalized(), v); +#endif + Vector3 u(x, y, z); + Vector3 uv = u.cross(v); + return v + ((uv * w) + u.cross(uv)) * ((real_t)2); + } + + inline Vector3 xform_inv(const Vector3 &v) const { + return inverse().xform(v); + } + + inline void operator+=(const Quaternion &p_q); + inline void operator-=(const Quaternion &p_q); + inline void operator*=(const real_t &s); + inline void operator/=(const real_t &s); + inline Quaternion operator+(const Quaternion &q2) const; + inline Quaternion operator-(const Quaternion &q2) const; + inline Quaternion operator-() const; + inline Quaternion operator*(const real_t &s) const; + inline Quaternion operator/(const real_t &s) const; + + inline bool operator==(const Quaternion &p_quat) const; + inline bool operator!=(const Quaternion &p_quat) const; + + operator String() const; + + inline Quaternion() {} + + inline Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) : + x(p_x), + y(p_y), + z(p_z), + w(p_w) { + } + + Quaternion(const Vector3 &p_axis, real_t p_angle); + + Quaternion(const Vector3 &p_euler); + + Quaternion(const Quaternion &p_q) : + x(p_q.x), + y(p_q.y), + z(p_q.z), + w(p_q.w) { + } + + Quaternion &operator=(const Quaternion &p_q) { + x = p_q.x; + y = p_q.y; + z = p_q.z; + w = p_q.w; + return *this; + } + + Quaternion(const Vector3 &v0, const Vector3 &v1) // shortest arc + { + Vector3 c = v0.cross(v1); + real_t d = v0.dot(v1); + + if (d < -1.0 + CMP_EPSILON) { + x = 0; + y = 1; + z = 0; + w = 0; + } else { + real_t s = Math::sqrt((1.0 + d) * 2.0); + real_t rs = 1.0 / s; + + x = c.x * rs; + y = c.y * rs; + z = c.z * rs; + w = s * 0.5; + } + } +}; + +real_t Quaternion::dot(const Quaternion &p_q) const { + return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w; +} + +real_t Quaternion::length_squared() const { + return dot(*this); +} + +void Quaternion::operator+=(const Quaternion &p_q) { + x += p_q.x; + y += p_q.y; + z += p_q.z; + w += p_q.w; +} + +void Quaternion::operator-=(const Quaternion &p_q) { + x -= p_q.x; + y -= p_q.y; + z -= p_q.z; + w -= p_q.w; +} + +void Quaternion::operator*=(const real_t &s) { + x *= s; + y *= s; + z *= s; + w *= s; +} + +void Quaternion::operator/=(const real_t &s) { + *this *= 1.0 / s; +} + +Quaternion Quaternion::operator+(const Quaternion &q2) const { + const Quaternion &q1 = *this; + return Quaternion(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w); +} + +Quaternion Quaternion::operator-(const Quaternion &q2) const { + const Quaternion &q1 = *this; + return Quaternion(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w); +} + +Quaternion Quaternion::operator-() const { + const Quaternion &q2 = *this; + return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w); +} + +Quaternion Quaternion::operator*(const real_t &s) const { + return Quaternion(x * s, y * s, z * s, w * s); +} + +Quaternion Quaternion::operator/(const real_t &s) const { + return *this * (1.0 / s); +} + +bool Quaternion::operator==(const Quaternion &p_quat) const { + return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w; +} + +bool Quaternion::operator!=(const Quaternion &p_quat) const { + return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w; +} + +inline Quaternion operator*(const real_t &p_real, const Quaternion &p_quat) { + return p_quat * p_real; +} + +} // namespace godot + +#endif // GODOT_QUAT_HPP diff --git a/include/godot_cpp/variant/rect2.hpp b/include/godot_cpp/variant/rect2.hpp new file mode 100644 index 00000000..8ace5f3b --- /dev/null +++ b/include/godot_cpp/variant/rect2.hpp @@ -0,0 +1,312 @@ + +#ifndef GODOT_RECT2_HPP +#define GODOT_RECT2_HPP + +#include +#include +#include + +namespace godot { + +struct Transform2D; + +class Rect2 { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + Point2 position; + Size2 size; + + const Vector2 &get_position() const { return position; } + void set_position(const Vector2 &p_pos) { position = p_pos; } + const Vector2 &get_size() const { return size; } + void set_size(const Vector2 &p_size) { size = p_size; } + + real_t get_area() const { return size.width * size.height; } + + inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const { + if (p_include_borders) { + if (position.x > (p_rect.position.x + p_rect.size.width)) { + return false; + } + if ((position.x + size.width) < p_rect.position.x) { + return false; + } + if (position.y > (p_rect.position.y + p_rect.size.height)) { + return false; + } + if ((position.y + size.height) < p_rect.position.y) { + return false; + } + } else { + if (position.x >= (p_rect.position.x + p_rect.size.width)) { + return false; + } + if ((position.x + size.width) <= p_rect.position.x) { + return false; + } + if (position.y >= (p_rect.position.y + p_rect.size.height)) { + return false; + } + if ((position.y + size.height) <= p_rect.position.y) { + return false; + } + } + + return true; + } + + inline real_t distance_to(const Vector2 &p_point) const { + real_t dist = 0.0; + bool inside = true; + + if (p_point.x < position.x) { + real_t d = position.x - p_point.x; + dist = d; + inside = false; + } + if (p_point.y < position.y) { + real_t d = position.y - p_point.y; + dist = inside ? d : Math::min(dist, d); + inside = false; + } + if (p_point.x >= (position.x + size.x)) { + real_t d = p_point.x - (position.x + size.x); + dist = inside ? d : Math::min(dist, d); + inside = false; + } + if (p_point.y >= (position.y + size.y)) { + real_t d = p_point.y - (position.y + size.y); + dist = inside ? d : Math::min(dist, d); + inside = false; + } + + if (inside) { + return 0; + } else { + return dist; + } + } + + bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const; + + bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const; + + inline bool encloses(const Rect2 &p_rect) const { + return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && + ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) && + ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y)); + } + + inline bool has_no_area() const { + return (size.x <= 0 || size.y <= 0); + } + + // Returns the instersection between two Rect2s or an empty Rect2 if there is no intersection + inline Rect2 intersection(const Rect2 &p_rect) const { + Rect2 new_rect = p_rect; + + if (!intersects(new_rect)) { + return Rect2(); + } + + new_rect.position.x = Math::max(p_rect.position.x, position.x); + new_rect.position.y = Math::max(p_rect.position.y, position.y); + + Point2 p_rect_end = p_rect.position + p_rect.size; + Point2 end = position + size; + + new_rect.size.x = Math::min(p_rect_end.x, end.x) - new_rect.position.x; + new_rect.size.y = Math::min(p_rect_end.y, end.y) - new_rect.position.y; + + return new_rect; + } + + inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect + + Rect2 new_rect; + + new_rect.position.x = Math::min(p_rect.position.x, position.x); + new_rect.position.y = Math::min(p_rect.position.y, position.y); + + new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x); + new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y); + + new_rect.size = new_rect.size - new_rect.position; //make relative again + + return new_rect; + } + inline bool has_point(const Point2 &p_point) const { + if (p_point.x < position.x) { + return false; + } + if (p_point.y < position.y) { + return false; + } + + if (p_point.x >= (position.x + size.x)) { + return false; + } + if (p_point.y >= (position.y + size.y)) { + return false; + } + + return true; + } + bool is_equal_approx(const Rect2 &p_rect) const; + + bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; } + bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; } + + inline Rect2 grow(real_t p_amount) const { + Rect2 g = *this; + g.position.x -= p_amount; + g.position.y -= p_amount; + g.size.width += p_amount * 2; + g.size.height += p_amount * 2; + return g; + } + + inline Rect2 grow_side(Side p_side, real_t p_amount) const { + Rect2 g = *this; + g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0, + (SIDE_TOP == p_side) ? p_amount : 0, + (SIDE_RIGHT == p_side) ? p_amount : 0, + (SIDE_BOTTOM == p_side) ? p_amount : 0); + return g; + } + + inline Rect2 grow_side_bind(uint32_t p_side, real_t p_amount) const { + return grow_side(Side(p_side), p_amount); + } + + inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const { + Rect2 g = *this; + g.position.x -= p_left; + g.position.y -= p_top; + g.size.width += p_left + p_right; + g.size.height += p_top + p_bottom; + + return g; + } + + inline Rect2 expand(const Vector2 &p_vector) const { + Rect2 r = *this; + r.expand_to(p_vector); + return r; + } + + inline void expand_to(const Vector2 &p_vector) { //in place function for speed + + Vector2 begin = position; + Vector2 end = position + size; + + if (p_vector.x < begin.x) { + begin.x = p_vector.x; + } + if (p_vector.y < begin.y) { + begin.y = p_vector.y; + } + + if (p_vector.x > end.x) { + end.x = p_vector.x; + } + if (p_vector.y > end.y) { + end.y = p_vector.y; + } + + position = begin; + size = end - begin; + } + + inline Rect2 abs() const { + return Rect2(Point2(position.x + Math::min(size.x, (real_t)0), position.y + Math::min(size.y, (real_t)0)), size.abs()); + } + + Vector2 get_support(const Vector2 &p_normal) const { + Vector2 half_extents = size * 0.5; + Vector2 ofs = position + half_extents; + return Vector2( + (p_normal.x > 0) ? -half_extents.x : half_extents.x, + (p_normal.y > 0) ? -half_extents.y : half_extents.y) + + ofs; + } + + inline bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const { + Vector2 center = position + size * 0.5; + int side_plus = 0; + int side_minus = 0; + Vector2 end = position + size; + + int i_f = p_point_count - 1; + for (int i = 0; i < p_point_count; i++) { + const Vector2 &a = p_points[i_f]; + const Vector2 &b = p_points[i]; + i_f = i; + + Vector2 r = (b - a); + float l = r.length(); + if (l == 0.0) { + continue; + } + + //check inside + Vector2 tg = r.orthogonal(); + float s = tg.dot(center) - tg.dot(a); + if (s < 0.0) { + side_plus++; + } else { + side_minus++; + } + + //check ray box + r /= l; + Vector2 ir(1.0 / r.x, 1.0 / r.y); + + // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner + // r.org is origin of ray + Vector2 t13 = (position - a) * ir; + Vector2 t24 = (end - a) * ir; + + float tmin = Math::max(Math::min(t13.x, t24.x), Math::min(t13.y, t24.y)); + float tmax = Math::min(Math::max(t13.x, t24.x), Math::max(t13.y, t24.y)); + + // if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us + if (tmax < 0 || tmin > tmax || tmin >= l) { + continue; + } + + return true; + } + + if (side_plus * side_minus == 0) { + return true; //all inside + } else { + return false; + } + } + + inline void set_end(const Vector2 &p_end) { + size = p_end - position; + } + + inline Vector2 get_end() const { + return position + size; + } + + operator String() const; + + Rect2() {} + Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) : + position(Point2(p_x, p_y)), + size(Size2(p_width, p_height)) { + } + Rect2(const Point2 &p_pos, const Size2 &p_size) : + position(p_pos), + size(p_size) { + } +}; + +} // namespace godot + +#endif // GODOT_RECT2_HPP diff --git a/include/godot_cpp/variant/rect2i.hpp b/include/godot_cpp/variant/rect2i.hpp new file mode 100644 index 00000000..54601ab8 --- /dev/null +++ b/include/godot_cpp/variant/rect2i.hpp @@ -0,0 +1,198 @@ +#ifndef GODOT_RECT2I_HPP +#define GODOT_RECT2I_HPP + +#include +#include + +namespace godot { + +class Rect2i { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + Point2i position; + Size2i size; + + const Point2i &get_position() const { return position; } + void set_position(const Point2i &p_position) { position = p_position; } + const Size2i &get_size() const { return size; } + void set_size(const Size2i &p_size) { size = p_size; } + + int get_area() const { return size.width * size.height; } + + inline bool intersects(const Rect2i &p_rect) const { + if (position.x > (p_rect.position.x + p_rect.size.width)) { + return false; + } + if ((position.x + size.width) < p_rect.position.x) { + return false; + } + if (position.y > (p_rect.position.y + p_rect.size.height)) { + return false; + } + if ((position.y + size.height) < p_rect.position.y) { + return false; + } + + return true; + } + + inline bool encloses(const Rect2i &p_rect) const { + return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && + ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) && + ((p_rect.position.y + p_rect.size.y) < (position.y + size.y)); + } + + inline bool has_no_area() const { + return (size.x <= 0 || size.y <= 0); + } + + // Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection + inline Rect2i intersection(const Rect2i &p_rect) const { + Rect2i new_rect = p_rect; + + if (!intersects(new_rect)) { + return Rect2i(); + } + + new_rect.position.x = Math::max(p_rect.position.x, position.x); + new_rect.position.y = Math::max(p_rect.position.y, position.y); + + Point2i p_rect_end = p_rect.position + p_rect.size; + Point2i end = position + size; + + new_rect.size.x = (int)(Math::min(p_rect_end.x, end.x) - new_rect.position.x); + new_rect.size.y = (int)(Math::min(p_rect_end.y, end.y) - new_rect.position.y); + + return new_rect; + } + + inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect + + Rect2i new_rect; + + new_rect.position.x = Math::min(p_rect.position.x, position.x); + new_rect.position.y = Math::min(p_rect.position.y, position.y); + + new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x); + new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y); + + new_rect.size = new_rect.size - new_rect.position; //make relative again + + return new_rect; + } + bool has_point(const Point2i &p_point) const { + if (p_point.x < position.x) { + return false; + } + if (p_point.y < position.y) { + return false; + } + + if (p_point.x >= (position.x + size.x)) { + return false; + } + if (p_point.y >= (position.y + size.y)) { + return false; + } + + return true; + } + + bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; } + bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; } + + Rect2i grow(int p_amount) const { + Rect2i g = *this; + g.position.x -= p_amount; + g.position.y -= p_amount; + g.size.width += p_amount * 2; + g.size.height += p_amount * 2; + return g; + } + + inline Rect2i grow_side(Side p_side, int p_amount) const { + Rect2i g = *this; + g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0, + (SIDE_TOP == p_side) ? p_amount : 0, + (SIDE_RIGHT == p_side) ? p_amount : 0, + (SIDE_BOTTOM == p_side) ? p_amount : 0); + return g; + } + + inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const { + return grow_side(Side(p_side), p_amount); + } + + inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const { + Rect2i g = *this; + g.position.x -= p_left; + g.position.y -= p_top; + g.size.width += p_left + p_right; + g.size.height += p_top + p_bottom; + + return g; + } + + inline Rect2i expand(const Vector2i &p_vector) const { + Rect2i r = *this; + r.expand_to(p_vector); + return r; + } + + inline void expand_to(const Point2i &p_vector) { + Point2i begin = position; + Point2i end = position + size; + + if (p_vector.x < begin.x) { + begin.x = p_vector.x; + } + if (p_vector.y < begin.y) { + begin.y = p_vector.y; + } + + if (p_vector.x > end.x) { + end.x = p_vector.x; + } + if (p_vector.y > end.y) { + end.y = p_vector.y; + } + + position = begin; + size = end - begin; + } + + inline Rect2i abs() const { + return Rect2i(Point2i(position.x + Math::min(size.x, 0), position.y + Math::min(size.y, 0)), size.abs()); + } + + inline void set_end(const Vector2i &p_end) { + size = p_end - position; + } + + inline Vector2i get_end() const { + return position + size; + } + + operator String() const { return String(position) + ", " + String(size); } + + operator Rect2() const { return Rect2(position, size); } + + Rect2i() {} + Rect2i(const Rect2 &p_r2) : + position(p_r2.position), + size(p_r2.size) { + } + Rect2i(int p_x, int p_y, int p_width, int p_height) : + position(Point2i(p_x, p_y)), + size(Size2i(p_width, p_height)) { + } + Rect2i(const Point2i &p_pos, const Size2i &p_size) : + position(p_pos), + size(p_size) { + } +}; + +} // namespace godot + +#endif // GODOT_RECT2I_HPP diff --git a/include/godot_cpp/variant/transform2d.hpp b/include/godot_cpp/variant/transform2d.hpp new file mode 100644 index 00000000..e41857b8 --- /dev/null +++ b/include/godot_cpp/variant/transform2d.hpp @@ -0,0 +1,216 @@ +#ifndef GODOT_TRANSFORM2D_HPP +#define GODOT_TRANSFORM2D_HPP + +#include +#include +#include +#include +#include + +namespace godot { + +class Transform2D { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper": + // M = (elements[0][0] elements[1][0]) + // (elements[0][1] elements[1][1]) + // This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as elements[i]. + // Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[1][0] here. + // This requires additional care when working with explicit indices. + // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading. + + // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down, + // and angle is measure from +X to +Y in a clockwise-fashion. + + Vector2 elements[3]; + + inline real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; } + inline real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; } + + const Vector2 &operator[](int p_idx) const { return elements[p_idx]; } + Vector2 &operator[](int p_idx) { return elements[p_idx]; } + + inline Vector2 get_axis(int p_axis) const { + ERR_FAIL_INDEX_V(p_axis, 3, Vector2()); + return elements[p_axis]; + } + inline void set_axis(int p_axis, const Vector2 &p_vec) { + ERR_FAIL_INDEX(p_axis, 3); + elements[p_axis] = p_vec; + } + + void invert(); + Transform2D inverse() const; + + void affine_invert(); + Transform2D affine_inverse() const; + + void set_rotation(real_t p_rot); + real_t get_rotation() const; + real_t get_skew() const; + void set_skew(float p_angle); + inline void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale); + inline void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew); + void rotate(real_t p_phi); + + void scale(const Size2 &p_scale); + void scale_basis(const Size2 &p_scale); + void translate(real_t p_tx, real_t p_ty); + void translate(const Vector2 &p_translation); + + real_t basis_determinant() const; + + Size2 get_scale() const; + void set_scale(const Size2 &p_scale); + + inline const Vector2 &get_origin() const { return elements[2]; } + inline void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; } + + Transform2D scaled(const Size2 &p_scale) const; + Transform2D basis_scaled(const Size2 &p_scale) const; + Transform2D translated(const Vector2 &p_offset) const; + Transform2D rotated(real_t p_phi) const; + + Transform2D untranslated() const; + + void orthonormalize(); + Transform2D orthonormalized() const; + bool is_equal_approx(const Transform2D &p_transform) const; + + bool operator==(const Transform2D &p_transform) const; + bool operator!=(const Transform2D &p_transform) const; + + void operator*=(const Transform2D &p_transform); + Transform2D operator*(const Transform2D &p_transform) const; + + Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const; + + inline Vector2 basis_xform(const Vector2 &p_vec) const; + inline Vector2 basis_xform_inv(const Vector2 &p_vec) const; + inline Vector2 xform(const Vector2 &p_vec) const; + inline Vector2 xform_inv(const Vector2 &p_vec) const; + inline Rect2 xform(const Rect2 &p_rect) const; + inline Rect2 xform_inv(const Rect2 &p_rect) const; + inline PackedVector2Array xform(const PackedVector2Array &p_array) const; + inline PackedVector2Array xform_inv(const PackedVector2Array &p_array) const; + + operator String() const; + + Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) { + elements[0][0] = xx; + elements[0][1] = xy; + elements[1][0] = yx; + elements[1][1] = yy; + elements[2][0] = ox; + elements[2][1] = oy; + } + + Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) { + elements[0] = p_x; + elements[1] = p_y; + elements[2] = p_origin; + } + + Transform2D(real_t p_rot, const Vector2 &p_pos); + Transform2D() { + elements[0][0] = 1.0; + elements[1][1] = 1.0; + } +}; + +Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const { + return Vector2( + tdotx(p_vec), + tdoty(p_vec)); +} + +Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const { + return Vector2( + elements[0].dot(p_vec), + elements[1].dot(p_vec)); +} + +Vector2 Transform2D::xform(const Vector2 &p_vec) const { + return Vector2( + tdotx(p_vec), + tdoty(p_vec)) + + elements[2]; +} + +Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const { + Vector2 v = p_vec - elements[2]; + + return Vector2( + elements[0].dot(v), + elements[1].dot(v)); +} + +Rect2 Transform2D::xform(const Rect2 &p_rect) const { + Vector2 x = elements[0] * p_rect.size.x; + Vector2 y = elements[1] * p_rect.size.y; + Vector2 pos = xform(p_rect.position); + + Rect2 new_rect; + new_rect.position = pos; + new_rect.expand_to(pos + x); + new_rect.expand_to(pos + y); + new_rect.expand_to(pos + x + y); + return new_rect; +} + +void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) { + elements[0][0] = Math::cos(p_rot) * p_scale.x; + elements[1][1] = Math::cos(p_rot) * p_scale.y; + elements[1][0] = -Math::sin(p_rot) * p_scale.y; + elements[0][1] = Math::sin(p_rot) * p_scale.x; +} + +void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew) { + elements[0][0] = Math::cos(p_rot) * p_scale.x; + elements[1][1] = Math::cos(p_rot + p_skew) * p_scale.y; + elements[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y; + elements[0][1] = Math::sin(p_rot) * p_scale.x; +} + +Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const { + Vector2 ends[4] = { + xform_inv(p_rect.position), + xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)), + xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)), + xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)) + }; + + Rect2 new_rect; + new_rect.position = ends[0]; + new_rect.expand_to(ends[1]); + new_rect.expand_to(ends[2]); + new_rect.expand_to(ends[3]); + + return new_rect; +} + +PackedVector2Array Transform2D::xform(const PackedVector2Array &p_array) const { + PackedVector2Array array; + array.resize(p_array.size()); + + for (int i = 0; i < p_array.size(); ++i) { + array[i] = xform(p_array[i]); + } + return array; +} + +PackedVector2Array Transform2D::xform_inv(const PackedVector2Array &p_array) const { + PackedVector2Array array; + array.resize(p_array.size()); + + for (int i = 0; i < p_array.size(); ++i) { + array[i] = xform_inv(p_array[i]); + } + return array; +} + +} // namespace godot + +#endif // GODOT_TRANSFORM2D_HPP diff --git a/include/godot_cpp/variant/transform3d.hpp b/include/godot_cpp/variant/transform3d.hpp new file mode 100644 index 00000000..7a7f6254 --- /dev/null +++ b/include/godot_cpp/variant/transform3d.hpp @@ -0,0 +1,204 @@ +#ifndef GODOT_TRANSFORM3D_HPP +#define GODOT_TRANSFORM3D_HPP + +#include +#include +#include +#include +#include + +namespace godot { + +class Transform3D { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + Basis basis; + Vector3 origin; + + void invert(); + Transform3D inverse() const; + + void affine_invert(); + Transform3D affine_inverse() const; + + Transform3D rotated(const Vector3 &p_axis, real_t p_phi) const; + + void rotate(const Vector3 &p_axis, real_t p_phi); + void rotate_basis(const Vector3 &p_axis, real_t p_phi); + + void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)); + Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)) const; + + void scale(const Vector3 &p_scale); + Transform3D scaled(const Vector3 &p_scale) const; + void scale_basis(const Vector3 &p_scale); + void translate(real_t p_tx, real_t p_ty, real_t p_tz); + void translate(const Vector3 &p_translation); + Transform3D translated(const Vector3 &p_translation) const; + + const Basis &get_basis() const { return basis; } + void set_basis(const Basis &p_basis) { basis = p_basis; } + + const Vector3 &get_origin() const { return origin; } + void set_origin(const Vector3 &p_origin) { origin = p_origin; } + + void orthonormalize(); + Transform3D orthonormalized() const; + bool is_equal_approx(const Transform3D &p_transform) const; + + bool operator==(const Transform3D &p_transform) const; + bool operator!=(const Transform3D &p_transform) const; + + inline Vector3 xform(const Vector3 &p_vector) const; + inline Vector3 xform_inv(const Vector3 &p_vector) const; + + inline Plane xform(const Plane &p_plane) const; + inline Plane xform_inv(const Plane &p_plane) const; + + inline AABB xform(const AABB &p_aabb) const; + inline AABB xform_inv(const AABB &p_aabb) const; + + inline PackedVector3Array xform(const PackedVector3Array &p_array) const; + inline PackedVector3Array xform_inv(const PackedVector3Array &p_array) const; + + void operator*=(const Transform3D &p_transform); + Transform3D operator*(const Transform3D &p_transform) const; + + Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const; + + inline Transform3D inverse_xform(const Transform3D &t) const { + Vector3 v = t.origin - origin; + return Transform3D(basis.transpose_xform(t.basis), + basis.xform(v)); + } + + void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t tx, real_t ty, real_t tz) { + basis.set(xx, xy, xz, yx, yy, yz, zx, zy, zz); + origin.x = tx; + origin.y = ty; + origin.z = tz; + } + + operator String() const; + + Transform3D() {} + Transform3D(const Basis &p_basis, const Vector3 &p_origin = Vector3()); + Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin); + Transform3D(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t ox, real_t oy, real_t oz); +}; + +inline Vector3 Transform3D::xform(const Vector3 &p_vector) const { + return Vector3( + basis[0].dot(p_vector) + origin.x, + basis[1].dot(p_vector) + origin.y, + basis[2].dot(p_vector) + origin.z); +} + +inline Vector3 Transform3D::xform_inv(const Vector3 &p_vector) const { + Vector3 v = p_vector - origin; + + return Vector3( + (basis.elements[0][0] * v.x) + (basis.elements[1][0] * v.y) + (basis.elements[2][0] * v.z), + (basis.elements[0][1] * v.x) + (basis.elements[1][1] * v.y) + (basis.elements[2][1] * v.z), + (basis.elements[0][2] * v.x) + (basis.elements[1][2] * v.y) + (basis.elements[2][2] * v.z)); +} + +inline Plane Transform3D::xform(const Plane &p_plane) const { + Vector3 point = p_plane.normal * p_plane.d; + Vector3 point_dir = point + p_plane.normal; + point = xform(point); + point_dir = xform(point_dir); + + Vector3 normal = point_dir - point; + normal.normalize(); + real_t d = normal.dot(point); + + return Plane(normal, d); +} + +inline Plane Transform3D::xform_inv(const Plane &p_plane) const { + Vector3 point = p_plane.normal * p_plane.d; + Vector3 point_dir = point + p_plane.normal; + point = xform_inv(point); + point_dir = xform_inv(point_dir); + + Vector3 normal = point_dir - point; + normal.normalize(); + real_t d = normal.dot(point); + + return Plane(normal, d); +} + +inline AABB Transform3D::xform(const AABB &p_aabb) const { + /* http://dev.theomader.com/transform-bounding-boxes/ */ + Vector3 min = p_aabb.position; + Vector3 max = p_aabb.position + p_aabb.size; + Vector3 tmin, tmax; + for (int i = 0; i < 3; i++) { + tmin[i] = tmax[i] = origin[i]; + for (int j = 0; j < 3; j++) { + real_t e = basis[i][j] * min[j]; + real_t f = basis[i][j] * max[j]; + if (e < f) { + tmin[i] += e; + tmax[i] += f; + } else { + tmin[i] += f; + tmax[i] += e; + } + } + } + AABB r_aabb; + r_aabb.position = tmin; + r_aabb.size = tmax - tmin; + return r_aabb; +} + +inline AABB Transform3D::xform_inv(const AABB &p_aabb) const { + /* define vertices */ + Vector3 vertices[8] = { + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z), + Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z), + Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z), + Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z) + }; + + AABB ret; + + ret.position = xform_inv(vertices[0]); + + for (int i = 1; i < 8; i++) { + ret.expand_to(xform_inv(vertices[i])); + } + + return ret; +} + +PackedVector3Array Transform3D::xform(const PackedVector3Array &p_array) const { + PackedVector3Array array; + array.resize(p_array.size()); + + for (int i = 0; i < p_array.size(); ++i) { + array[i] = xform(p_array[i]); + } + return array; +} + +PackedVector3Array Transform3D::xform_inv(const PackedVector3Array &p_array) const { + PackedVector3Array array; + array.resize(p_array.size()); + + for (int i = 0; i < p_array.size(); ++i) { + array[i] = xform_inv(p_array[i]); + } + return array; +} + +} // namespace godot + +#endif // GODOT_TRANSFORM_HPP diff --git a/include/godot_cpp/variant/vector2.hpp b/include/godot_cpp/variant/vector2.hpp new file mode 100644 index 00000000..51ece07e --- /dev/null +++ b/include/godot_cpp/variant/vector2.hpp @@ -0,0 +1,236 @@ +#ifndef GODOT_VECTOR2_HPP +#define GODOT_VECTOR2_HPP + +#include +#include + +namespace godot { + +class Vector2i; + +class Vector2 { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + enum Axis { + AXIS_X, + AXIS_Y, + }; + + union { + real_t x = 0; + real_t width; + }; + union { + real_t y = 0; + real_t height; + }; + + inline real_t &operator[](int p_idx) { + return p_idx ? y : x; + } + inline const real_t &operator[](int p_idx) const { + return p_idx ? y : x; + } + + void normalize(); + Vector2 normalized() const; + bool is_normalized() const; + + real_t length() const; + real_t length_squared() const; + + Vector2 min(const Vector2 &p_vector2) const { + return Vector2(Math::min(x, p_vector2.x), Math::min(y, p_vector2.y)); + } + + Vector2 max(const Vector2 &p_vector2) const { + return Vector2(Math::max(x, p_vector2.x), Math::max(y, p_vector2.y)); + } + + real_t distance_to(const Vector2 &p_vector2) const; + real_t distance_squared_to(const Vector2 &p_vector2) const; + real_t angle_to(const Vector2 &p_vector2) const; + real_t angle_to_point(const Vector2 &p_vector2) const; + inline Vector2 direction_to(const Vector2 &p_to) const; + + real_t dot(const Vector2 &p_other) const; + real_t cross(const Vector2 &p_other) const; + Vector2 posmod(const real_t p_mod) const; + Vector2 posmodv(const Vector2 &p_modv) const; + Vector2 project(const Vector2 &p_to) const; + + Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const; + + Vector2 clamped(real_t p_len) const; + + inline Vector2 lerp(const Vector2 &p_to, real_t p_weight) const; + inline Vector2 slerp(const Vector2 &p_to, real_t p_weight) const; + Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const; + Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const; + + Vector2 slide(const Vector2 &p_normal) const; + Vector2 bounce(const Vector2 &p_normal) const; + Vector2 reflect(const Vector2 &p_normal) const; + + bool is_equal_approx(const Vector2 &p_v) const; + + Vector2 operator+(const Vector2 &p_v) const; + void operator+=(const Vector2 &p_v); + Vector2 operator-(const Vector2 &p_v) const; + void operator-=(const Vector2 &p_v); + Vector2 operator*(const Vector2 &p_v1) const; + + Vector2 operator*(const real_t &rvalue) const; + void operator*=(const real_t &rvalue); + void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; } + + Vector2 operator/(const Vector2 &p_v1) const; + + Vector2 operator/(const real_t &rvalue) const; + + void operator/=(const real_t &rvalue); + void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; } + + Vector2 operator-() const; + + bool operator==(const Vector2 &p_vec2) const; + bool operator!=(const Vector2 &p_vec2) const; + + bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); } + bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); } + bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); } + bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); } + + real_t angle() const; + + inline Vector2 abs() const { + return Vector2(Math::abs(x), Math::abs(y)); + } + + Vector2 rotated(real_t p_by) const; + Vector2 orthogonal() const { + return Vector2(y, -x); + } + + Vector2 sign() const; + Vector2 floor() const; + Vector2 ceil() const; + Vector2 round() const; + Vector2 snapped(const Vector2 &p_by) const; + real_t aspect() const { return width / height; } + + operator String() const; + + inline Vector2() {} + inline Vector2(real_t p_x, real_t p_y) { + x = p_x; + y = p_y; + } +}; + +inline Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const { + return p_vec - *this * (dot(p_vec) - p_d); +} + +inline Vector2 operator*(float p_scalar, const Vector2 &p_vec) { + return p_vec * p_scalar; +} + +inline Vector2 operator*(double p_scalar, const Vector2 &p_vec) { + return p_vec * p_scalar; +} + +inline Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) { + return p_vec * p_scalar; +} + +inline Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) { + return p_vec * p_scalar; +} + +inline Vector2 Vector2::operator+(const Vector2 &p_v) const { + return Vector2(x + p_v.x, y + p_v.y); +} + +inline void Vector2::operator+=(const Vector2 &p_v) { + x += p_v.x; + y += p_v.y; +} + +inline Vector2 Vector2::operator-(const Vector2 &p_v) const { + return Vector2(x - p_v.x, y - p_v.y); +} + +inline void Vector2::operator-=(const Vector2 &p_v) { + x -= p_v.x; + y -= p_v.y; +} + +inline Vector2 Vector2::operator*(const Vector2 &p_v1) const { + return Vector2(x * p_v1.x, y * p_v1.y); +} + +inline Vector2 Vector2::operator*(const real_t &rvalue) const { + return Vector2(x * rvalue, y * rvalue); +} + +inline void Vector2::operator*=(const real_t &rvalue) { + x *= rvalue; + y *= rvalue; +} + +inline Vector2 Vector2::operator/(const Vector2 &p_v1) const { + return Vector2(x / p_v1.x, y / p_v1.y); +} + +inline Vector2 Vector2::operator/(const real_t &rvalue) const { + return Vector2(x / rvalue, y / rvalue); +} + +inline void Vector2::operator/=(const real_t &rvalue) { + x /= rvalue; + y /= rvalue; +} + +inline Vector2 Vector2::operator-() const { + return Vector2(-x, -y); +} + +inline bool Vector2::operator==(const Vector2 &p_vec2) const { + return x == p_vec2.x && y == p_vec2.y; +} + +inline bool Vector2::operator!=(const Vector2 &p_vec2) const { + return x != p_vec2.x || y != p_vec2.y; +} + +Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const { + Vector2 res = *this; + + res.x += (p_weight * (p_to.x - x)); + res.y += (p_weight * (p_to.y - y)); + + return res; +} + +Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_normalized(), Vector2()); +#endif + real_t theta = angle_to(p_to); + return rotated(theta * p_weight); +} + +Vector2 Vector2::direction_to(const Vector2 &p_to) const { + Vector2 ret(p_to.x - x, p_to.y - y); + ret.normalize(); + return ret; +} + +typedef Vector2 Size2; +typedef Vector2 Point2; + +} // namespace godot + +#endif // GODOT_VECTOR2_HPP diff --git a/include/godot_cpp/variant/vector2i.hpp b/include/godot_cpp/variant/vector2i.hpp new file mode 100644 index 00000000..e9493adf --- /dev/null +++ b/include/godot_cpp/variant/vector2i.hpp @@ -0,0 +1,102 @@ +#ifndef GODOT_VECTOR2I_HPP +#define GODOT_VECTOR2I_HPP + +#include +#include +#include + +namespace godot { + +class Vector2i { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + enum Axis { + AXIS_X, + AXIS_Y, + }; + + union { + int32_t x = 0; + int32_t width; + }; + union { + int32_t y = 0; + int32_t height; + }; + + inline int32_t &operator[](int p_idx) { + return p_idx ? y : x; + } + inline const int32_t &operator[](int p_idx) const { + return p_idx ? y : x; + } + + Vector2i operator+(const Vector2i &p_v) const; + void operator+=(const Vector2i &p_v); + Vector2i operator-(const Vector2i &p_v) const; + void operator-=(const Vector2i &p_v); + Vector2i operator*(const Vector2i &p_v1) const; + + Vector2i operator*(const int32_t &rvalue) const; + void operator*=(const int32_t &rvalue); + + Vector2i operator/(const Vector2i &p_v1) const; + Vector2i operator/(const int32_t &rvalue) const; + void operator/=(const int32_t &rvalue); + + Vector2i operator%(const Vector2i &p_v1) const; + Vector2i operator%(const int32_t &rvalue) const; + void operator%=(const int32_t &rvalue); + + Vector2i operator-() const; + bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); } + bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); } + + bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); } + bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); } + + bool operator==(const Vector2i &p_vec2) const; + bool operator!=(const Vector2i &p_vec2) const; + + real_t aspect() const { return width / (real_t)height; } + Vector2i sign() const { return Vector2i(Math::sign(x), Math::sign(y)); } + Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); } + + operator String() const; + + operator Vector2() const { return Vector2(x, y); } + + inline Vector2i() {} + inline Vector2i(const Vector2 &p_vec2) { + x = (int32_t)p_vec2.x; + y = (int32_t)p_vec2.y; + } + inline Vector2i(int32_t p_x, int32_t p_y) { + x = p_x; + y = p_y; + } +}; + +inline Vector2i operator*(const int32_t &p_scalar, const Vector2i &p_vector) { + return p_vector * p_scalar; +} + +inline Vector2i operator*(const int64_t &p_scalar, const Vector2i &p_vector) { + return p_vector * p_scalar; +} + +inline Vector2i operator*(const float &p_scalar, const Vector2i &p_vector) { + return p_vector * p_scalar; +} + +inline Vector2i operator*(const double &p_scalar, const Vector2i &p_vector) { + return p_vector * p_scalar; +} + +typedef Vector2i Size2i; +typedef Vector2i Point2i; + +} // namespace godot + +#endif // GODOT_VECTOR2I_HPP diff --git a/include/godot_cpp/variant/vector3.hpp b/include/godot_cpp/variant/vector3.hpp new file mode 100644 index 00000000..fb081743 --- /dev/null +++ b/include/godot_cpp/variant/vector3.hpp @@ -0,0 +1,408 @@ +#ifndef GODOT_VECTOR3_HPP +#define GODOT_VECTOR3_HPP + +#include +#include + +namespace godot { + +class Basis; +class Vector3i; + +class Vector3 { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + enum Axis { + AXIS_X, + AXIS_Y, + AXIS_Z, + }; + + union { + struct { + real_t x; + real_t y; + real_t z; + }; + + real_t coord[3] = { 0 }; + }; + + inline const real_t &operator[](int p_axis) const { + return coord[p_axis]; + } + + inline real_t &operator[](int p_axis) { + return coord[p_axis]; + } + + void set_axis(int p_axis, real_t p_value); + real_t get_axis(int p_axis) const; + + int min_axis() const; + int max_axis() const; + + inline real_t length() const; + inline real_t length_squared() const; + + inline void normalize(); + inline Vector3 normalized() const; + inline bool is_normalized() const; + inline Vector3 inverse() const; + + inline void zero(); + + void snap(Vector3 p_val); + Vector3 snapped(Vector3 p_val) const; + + void rotate(const Vector3 &p_axis, real_t p_phi); + Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const; + + /* Static Methods between 2 vector3s */ + + inline Vector3 lerp(const Vector3 &p_to, real_t p_weight) const; + inline Vector3 slerp(const Vector3 &p_to, real_t p_weight) const; + Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const; + Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const; + + inline Vector3 cross(const Vector3 &p_b) const; + inline real_t dot(const Vector3 &p_b) const; + Basis outer(const Vector3 &p_b) const; + Basis to_diagonal_matrix() const; + + inline Vector3 abs() const; + inline Vector3 floor() const; + inline Vector3 sign() const; + inline Vector3 ceil() const; + inline Vector3 round() const; + + inline real_t distance_to(const Vector3 &p_to) const; + inline real_t distance_squared_to(const Vector3 &p_to) const; + + inline Vector3 posmod(const real_t p_mod) const; + inline Vector3 posmodv(const Vector3 &p_modv) const; + inline Vector3 project(const Vector3 &p_to) const; + + inline real_t angle_to(const Vector3 &p_to) const; + inline Vector3 direction_to(const Vector3 &p_to) const; + + inline Vector3 slide(const Vector3 &p_normal) const; + inline Vector3 bounce(const Vector3 &p_normal) const; + inline Vector3 reflect(const Vector3 &p_normal) const; + + bool is_equal_approx(const Vector3 &p_v) const; + + /* Operators */ + + inline Vector3 &operator+=(const Vector3 &p_v); + inline Vector3 operator+(const Vector3 &p_v) const; + inline Vector3 &operator-=(const Vector3 &p_v); + inline Vector3 operator-(const Vector3 &p_v) const; + inline Vector3 &operator*=(const Vector3 &p_v); + inline Vector3 operator*(const Vector3 &p_v) const; + inline Vector3 &operator/=(const Vector3 &p_v); + inline Vector3 operator/(const Vector3 &p_v) const; + + inline Vector3 &operator*=(real_t p_scalar); + inline Vector3 operator*(real_t p_scalar) const; + inline Vector3 &operator/=(real_t p_scalar); + inline Vector3 operator/(real_t p_scalar) const; + + inline Vector3 operator-() const; + + inline bool operator==(const Vector3 &p_v) const; + inline bool operator!=(const Vector3 &p_v) const; + inline bool operator<(const Vector3 &p_v) const; + inline bool operator<=(const Vector3 &p_v) const; + inline bool operator>(const Vector3 &p_v) const; + inline bool operator>=(const Vector3 &p_v) const; + + operator String() const; + operator Vector3i() const; + + inline Vector3() {} + inline Vector3(real_t p_x, real_t p_y, real_t p_z) { + x = p_x; + y = p_y; + z = p_z; + } + Vector3(const Vector3i &p_ivec); +}; + +Vector3 Vector3::cross(const Vector3 &p_b) const { + Vector3 ret( + (y * p_b.z) - (z * p_b.y), + (z * p_b.x) - (x * p_b.z), + (x * p_b.y) - (y * p_b.x)); + + return ret; +} + +real_t Vector3::dot(const Vector3 &p_b) const { + return x * p_b.x + y * p_b.y + z * p_b.z; +} + +Vector3 Vector3::abs() const { + return Vector3(Math::abs(x), Math::abs(y), Math::abs(z)); +} + +Vector3 Vector3::sign() const { + return Vector3(Math::sign(x), Math::sign(y), Math::sign(z)); +} + +Vector3 Vector3::floor() const { + return Vector3(Math::floor(x), Math::floor(y), Math::floor(z)); +} + +Vector3 Vector3::ceil() const { + return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z)); +} + +Vector3 Vector3::round() const { + return Vector3(Math::round(x), Math::round(y), Math::round(z)); +} + +Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const { + return Vector3( + x + (p_weight * (p_to.x - x)), + y + (p_weight * (p_to.y - y)), + z + (p_weight * (p_to.z - z))); +} + +Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const { + real_t theta = angle_to(p_to); + return rotated(cross(p_to).normalized(), theta * p_weight); +} + +real_t Vector3::distance_to(const Vector3 &p_to) const { + return (p_to - *this).length(); +} + +real_t Vector3::distance_squared_to(const Vector3 &p_to) const { + return (p_to - *this).length_squared(); +} + +Vector3 Vector3::posmod(const real_t p_mod) const { + return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod)); +} + +Vector3 Vector3::posmodv(const Vector3 &p_modv) const { + return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z)); +} + +Vector3 Vector3::project(const Vector3 &p_to) const { + return p_to * (dot(p_to) / p_to.length_squared()); +} + +real_t Vector3::angle_to(const Vector3 &p_to) const { + return Math::atan2(cross(p_to).length(), dot(p_to)); +} + +Vector3 Vector3::direction_to(const Vector3 &p_to) const { + Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z); + ret.normalize(); + return ret; +} + +/* Operators */ + +Vector3 &Vector3::operator+=(const Vector3 &p_v) { + x += p_v.x; + y += p_v.y; + z += p_v.z; + return *this; +} + +Vector3 Vector3::operator+(const Vector3 &p_v) const { + return Vector3(x + p_v.x, y + p_v.y, z + p_v.z); +} + +Vector3 &Vector3::operator-=(const Vector3 &p_v) { + x -= p_v.x; + y -= p_v.y; + z -= p_v.z; + return *this; +} + +Vector3 Vector3::operator-(const Vector3 &p_v) const { + return Vector3(x - p_v.x, y - p_v.y, z - p_v.z); +} + +Vector3 &Vector3::operator*=(const Vector3 &p_v) { + x *= p_v.x; + y *= p_v.y; + z *= p_v.z; + return *this; +} + +Vector3 Vector3::operator*(const Vector3 &p_v) const { + return Vector3(x * p_v.x, y * p_v.y, z * p_v.z); +} + +Vector3 &Vector3::operator/=(const Vector3 &p_v) { + x /= p_v.x; + y /= p_v.y; + z /= p_v.z; + return *this; +} + +Vector3 Vector3::operator/(const Vector3 &p_v) const { + return Vector3(x / p_v.x, y / p_v.y, z / p_v.z); +} + +Vector3 &Vector3::operator*=(real_t p_scalar) { + x *= p_scalar; + y *= p_scalar; + z *= p_scalar; + return *this; +} + +inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) { + return p_vec * p_scalar; +} + +Vector3 Vector3::operator*(real_t p_scalar) const { + return Vector3(x * p_scalar, y * p_scalar, z * p_scalar); +} + +Vector3 &Vector3::operator/=(real_t p_scalar) { + x /= p_scalar; + y /= p_scalar; + z /= p_scalar; + return *this; +} + +Vector3 Vector3::operator/(real_t p_scalar) const { + return Vector3(x / p_scalar, y / p_scalar, z / p_scalar); +} + +Vector3 Vector3::operator-() const { + return Vector3(-x, -y, -z); +} + +bool Vector3::operator==(const Vector3 &p_v) const { + return x == p_v.x && y == p_v.y && z == p_v.z; +} + +bool Vector3::operator!=(const Vector3 &p_v) const { + return x != p_v.x || y != p_v.y || z != p_v.z; +} + +bool Vector3::operator<(const Vector3 &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z < p_v.z; + } + return y < p_v.y; + } + return x < p_v.x; +} + +bool Vector3::operator>(const Vector3 &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z > p_v.z; + } + return y > p_v.y; + } + return x > p_v.x; +} + +bool Vector3::operator<=(const Vector3 &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z <= p_v.z; + } + return y < p_v.y; + } + return x < p_v.x; +} + +bool Vector3::operator>=(const Vector3 &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z >= p_v.z; + } + return y > p_v.y; + } + return x > p_v.x; +} + +inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) { + return p_a.cross(p_b); +} + +inline real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) { + return p_a.dot(p_b); +} + +real_t Vector3::length() const { + real_t x2 = x * x; + real_t y2 = y * y; + real_t z2 = z * z; + + return Math::sqrt(x2 + y2 + z2); +} + +real_t Vector3::length_squared() const { + real_t x2 = x * x; + real_t y2 = y * y; + real_t z2 = z * z; + + return x2 + y2 + z2; +} + +void Vector3::normalize() { + real_t lengthsq = length_squared(); + if (lengthsq == 0) { + x = y = z = 0; + } else { + real_t length = Math::sqrt(lengthsq); + x /= length; + y /= length; + z /= length; + } +} + +Vector3 Vector3::normalized() const { + Vector3 v = *this; + v.normalize(); + return v; +} + +bool Vector3::is_normalized() const { + // use length_squared() instead of length() to avoid sqrt(), makes it more stringent. + return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); +} + +Vector3 Vector3::inverse() const { + return Vector3(1.0 / x, 1.0 / y, 1.0 / z); +} + +void Vector3::zero() { + x = y = z = 0; +} + +// slide returns the component of the vector along the given plane, specified by its normal vector. +Vector3 Vector3::slide(const Vector3 &p_normal) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector3()); +#endif + return *this - p_normal * this->dot(p_normal); +} + +Vector3 Vector3::bounce(const Vector3 &p_normal) const { + return -reflect(p_normal); +} + +Vector3 Vector3::reflect(const Vector3 &p_normal) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector3()); +#endif + return 2.0 * p_normal * this->dot(p_normal) - *this; +} + +} // namespace godot + +#endif // GODOT_VECTOR3_HPP diff --git a/include/godot_cpp/variant/vector3i.hpp b/include/godot_cpp/variant/vector3i.hpp new file mode 100644 index 00000000..09a8b69e --- /dev/null +++ b/include/godot_cpp/variant/vector3i.hpp @@ -0,0 +1,255 @@ +#ifndef GODOT_VECTOR3I_HPP +#define GODOT_VECTOR3I_HPP + +#include +#include + +namespace godot { + +class Vector3i { +public: + _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; } + + enum Axis { + AXIS_X, + AXIS_Y, + AXIS_Z, + }; + + union { + struct { + int32_t x; + int32_t y; + int32_t z; + }; + + int32_t coord[3] = { 0 }; + }; + + inline const int32_t &operator[](int p_axis) const { + return coord[p_axis]; + } + + inline int32_t &operator[](int p_axis) { + return coord[p_axis]; + } + + void set_axis(int p_axis, int32_t p_value); + int32_t get_axis(int p_axis) const; + + int min_axis() const; + int max_axis() const; + + inline void zero(); + + inline Vector3i abs() const; + inline Vector3i sign() const; + + /* Operators */ + + inline Vector3i &operator+=(const Vector3i &p_v); + inline Vector3i operator+(const Vector3i &p_v) const; + inline Vector3i &operator-=(const Vector3i &p_v); + inline Vector3i operator-(const Vector3i &p_v) const; + inline Vector3i &operator*=(const Vector3i &p_v); + inline Vector3i operator*(const Vector3i &p_v) const; + inline Vector3i &operator/=(const Vector3i &p_v); + inline Vector3i operator/(const Vector3i &p_v) const; + inline Vector3i &operator%=(const Vector3i &p_v); + inline Vector3i operator%(const Vector3i &p_v) const; + + inline Vector3i &operator*=(int32_t p_scalar); + inline Vector3i operator*(int32_t p_scalar) const; + inline Vector3i &operator/=(int32_t p_scalar); + inline Vector3i operator/(int32_t p_scalar) const; + inline Vector3i &operator%=(int32_t p_scalar); + inline Vector3i operator%(int32_t p_scalar) const; + + inline Vector3i operator-() const; + + inline bool operator==(const Vector3i &p_v) const; + inline bool operator!=(const Vector3i &p_v) const; + inline bool operator<(const Vector3i &p_v) const; + inline bool operator<=(const Vector3i &p_v) const; + inline bool operator>(const Vector3i &p_v) const; + inline bool operator>=(const Vector3i &p_v) const; + + operator String() const; + + inline Vector3i() {} + inline Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) { + x = p_x; + y = p_y; + z = p_z; + } +}; + +Vector3i Vector3i::abs() const { + return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z)); +} + +Vector3i Vector3i::sign() const { + return Vector3i(Math::sign(x), Math::sign(y), Math::sign(z)); +} + +/* Operators */ + +Vector3i &Vector3i::operator+=(const Vector3i &p_v) { + x += p_v.x; + y += p_v.y; + z += p_v.z; + return *this; +} + +Vector3i Vector3i::operator+(const Vector3i &p_v) const { + return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z); +} + +Vector3i &Vector3i::operator-=(const Vector3i &p_v) { + x -= p_v.x; + y -= p_v.y; + z -= p_v.z; + return *this; +} + +Vector3i Vector3i::operator-(const Vector3i &p_v) const { + return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z); +} + +Vector3i &Vector3i::operator*=(const Vector3i &p_v) { + x *= p_v.x; + y *= p_v.y; + z *= p_v.z; + return *this; +} + +Vector3i Vector3i::operator*(const Vector3i &p_v) const { + return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z); +} + +Vector3i &Vector3i::operator/=(const Vector3i &p_v) { + x /= p_v.x; + y /= p_v.y; + z /= p_v.z; + return *this; +} + +Vector3i Vector3i::operator/(const Vector3i &p_v) const { + return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z); +} + +Vector3i &Vector3i::operator%=(const Vector3i &p_v) { + x %= p_v.x; + y %= p_v.y; + z %= p_v.z; + return *this; +} + +Vector3i Vector3i::operator%(const Vector3i &p_v) const { + return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z); +} + +Vector3i &Vector3i::operator*=(int32_t p_scalar) { + x *= p_scalar; + y *= p_scalar; + z *= p_scalar; + return *this; +} + +inline Vector3i operator*(int32_t p_scalar, const Vector3i &p_vec) { + return p_vec * p_scalar; +} + +Vector3i Vector3i::operator*(int32_t p_scalar) const { + return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar); +} + +Vector3i &Vector3i::operator/=(int32_t p_scalar) { + x /= p_scalar; + y /= p_scalar; + z /= p_scalar; + return *this; +} + +Vector3i Vector3i::operator/(int32_t p_scalar) const { + return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar); +} + +Vector3i &Vector3i::operator%=(int32_t p_scalar) { + x %= p_scalar; + y %= p_scalar; + z %= p_scalar; + return *this; +} + +Vector3i Vector3i::operator%(int32_t p_scalar) const { + return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar); +} + +Vector3i Vector3i::operator-() const { + return Vector3i(-x, -y, -z); +} + +bool Vector3i::operator==(const Vector3i &p_v) const { + return (x == p_v.x && y == p_v.y && z == p_v.z); +} + +bool Vector3i::operator!=(const Vector3i &p_v) const { + return (x != p_v.x || y != p_v.y || z != p_v.z); +} + +bool Vector3i::operator<(const Vector3i &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z < p_v.z; + } else { + return y < p_v.y; + } + } else { + return x < p_v.x; + } +} + +bool Vector3i::operator>(const Vector3i &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z > p_v.z; + } else { + return y > p_v.y; + } + } else { + return x > p_v.x; + } +} + +bool Vector3i::operator<=(const Vector3i &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z <= p_v.z; + } else { + return y < p_v.y; + } + } else { + return x < p_v.x; + } +} + +bool Vector3i::operator>=(const Vector3i &p_v) const { + if (x == p_v.x) { + if (y == p_v.y) { + return z >= p_v.z; + } else { + return y > p_v.y; + } + } else { + return x > p_v.x; + } +} + +void Vector3i::zero() { + x = y = z = 0; +} + +} // namespace godot + +#endif // GODOT_VECTOR3I_HPP diff --git a/src/variant/aabb.cpp b/src/variant/aabb.cpp new file mode 100644 index 00000000..586ec5b0 --- /dev/null +++ b/src/variant/aabb.cpp @@ -0,0 +1,355 @@ +#include + +#include +#include + +namespace godot { + +real_t AABB::get_area() const { + return size.x * size.y * size.z; +} + +bool AABB::operator==(const AABB &p_rval) const { + return ((position == p_rval.position) && (size == p_rval.size)); +} + +bool AABB::operator!=(const AABB &p_rval) const { + return ((position != p_rval.position) || (size != p_rval.size)); +} + +void AABB::merge_with(const AABB &p_aabb) { + Vector3 beg_1, beg_2; + Vector3 end_1, end_2; + Vector3 min, max; + + beg_1 = position; + beg_2 = p_aabb.position; + end_1 = Vector3(size.x, size.y, size.z) + beg_1; + end_2 = Vector3(p_aabb.size.x, p_aabb.size.y, p_aabb.size.z) + beg_2; + + min.x = (beg_1.x < beg_2.x) ? beg_1.x : beg_2.x; + min.y = (beg_1.y < beg_2.y) ? beg_1.y : beg_2.y; + min.z = (beg_1.z < beg_2.z) ? beg_1.z : beg_2.z; + + max.x = (end_1.x > end_2.x) ? end_1.x : end_2.x; + max.y = (end_1.y > end_2.y) ? end_1.y : end_2.y; + max.z = (end_1.z > end_2.z) ? end_1.z : end_2.z; + + position = min; + size = max - min; +} + +bool AABB::is_equal_approx(const AABB &p_aabb) const { + return position.is_equal_approx(p_aabb.position) && size.is_equal_approx(p_aabb.size); +} + +AABB AABB::intersection(const AABB &p_aabb) const { + Vector3 src_min = position; + Vector3 src_max = position + size; + Vector3 dst_min = p_aabb.position; + Vector3 dst_max = p_aabb.position + p_aabb.size; + + Vector3 min, max; + + if (src_min.x > dst_max.x || src_max.x < dst_min.x) { + return AABB(); + } else { + min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x; + max.x = (src_max.x < dst_max.x) ? src_max.x : dst_max.x; + } + + if (src_min.y > dst_max.y || src_max.y < dst_min.y) { + return AABB(); + } else { + min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y; + max.y = (src_max.y < dst_max.y) ? src_max.y : dst_max.y; + } + + if (src_min.z > dst_max.z || src_max.z < dst_min.z) { + return AABB(); + } else { + min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z; + max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z; + } + + return AABB(min, max - min); +} + +bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const { + Vector3 c1, c2; + Vector3 end = position + size; + real_t near = -1e20; + real_t far = 1e20; + int axis = 0; + + for (int i = 0; i < 3; i++) { + if (p_dir[i] == 0) { + if ((p_from[i] < position[i]) || (p_from[i] > end[i])) { + return false; + } + } else { // ray not parallel to planes in this direction + c1[i] = (position[i] - p_from[i]) / p_dir[i]; + c2[i] = (end[i] - p_from[i]) / p_dir[i]; + + if (c1[i] > c2[i]) { + SWAP(c1, c2); + } + if (c1[i] > near) { + near = c1[i]; + axis = i; + } + if (c2[i] < far) { + far = c2[i]; + } + if ((near > far) || (far < 0)) { + return false; + } + } + } + + if (r_clip) { + *r_clip = c1; + } + if (r_normal) { + *r_normal = Vector3(); + (*r_normal)[axis] = p_dir[axis] ? -1 : 1; + } + + return true; +} + +bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const { + real_t min = 0, max = 1; + int axis = 0; + real_t sign = 0; + + for (int i = 0; i < 3; i++) { + real_t seg_from = p_from[i]; + real_t seg_to = p_to[i]; + real_t box_begin = position[i]; + real_t box_end = box_begin + size[i]; + real_t cmin, cmax; + real_t csign; + + if (seg_from < seg_to) { + if (seg_from > box_end || seg_to < box_begin) { + return false; + } + real_t length = seg_to - seg_from; + cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0; + cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1; + csign = -1.0; + + } else { + if (seg_to > box_end || seg_from < box_begin) { + return false; + } + real_t length = seg_to - seg_from; + cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0; + cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1; + csign = 1.0; + } + + if (cmin > min) { + min = cmin; + axis = i; + sign = csign; + } + if (cmax < max) { + max = cmax; + } + if (max < min) { + return false; + } + } + + Vector3 rel = p_to - p_from; + + if (r_normal) { + Vector3 normal; + normal[axis] = sign; + *r_normal = normal; + } + + if (r_clip) { + *r_clip = p_from + rel * min; + } + + return true; +} + +bool AABB::intersects_plane(const Plane &p_plane) const { + Vector3 points[8] = { + Vector3(position.x, position.y, position.z), + Vector3(position.x, position.y, position.z + size.z), + Vector3(position.x, position.y + size.y, position.z), + Vector3(position.x, position.y + size.y, position.z + size.z), + Vector3(position.x + size.x, position.y, position.z), + Vector3(position.x + size.x, position.y, position.z + size.z), + Vector3(position.x + size.x, position.y + size.y, position.z), + Vector3(position.x + size.x, position.y + size.y, position.z + size.z), + }; + + bool over = false; + bool under = false; + + for (int i = 0; i < 8; i++) { + if (p_plane.distance_to(points[i]) > 0) { + over = true; + } else { + under = true; + } + } + + return under && over; +} + +Vector3 AABB::get_longest_axis() const { + Vector3 axis(1, 0, 0); + real_t max_size = size.x; + + if (size.y > max_size) { + axis = Vector3(0, 1, 0); + max_size = size.y; + } + + if (size.z > max_size) { + axis = Vector3(0, 0, 1); + } + + return axis; +} + +int AABB::get_longest_axis_index() const { + int axis = 0; + real_t max_size = size.x; + + if (size.y > max_size) { + axis = 1; + max_size = size.y; + } + + if (size.z > max_size) { + axis = 2; + } + + return axis; +} + +Vector3 AABB::get_shortest_axis() const { + Vector3 axis(1, 0, 0); + real_t max_size = size.x; + + if (size.y < max_size) { + axis = Vector3(0, 1, 0); + max_size = size.y; + } + + if (size.z < max_size) { + axis = Vector3(0, 0, 1); + } + + return axis; +} + +int AABB::get_shortest_axis_index() const { + int axis = 0; + real_t max_size = size.x; + + if (size.y < max_size) { + axis = 1; + max_size = size.y; + } + + if (size.z < max_size) { + axis = 2; + } + + return axis; +} + +AABB AABB::merge(const AABB &p_with) const { + AABB aabb = *this; + aabb.merge_with(p_with); + return aabb; +} + +AABB AABB::expand(const Vector3 &p_vector) const { + AABB aabb = *this; + aabb.expand_to(p_vector); + return aabb; +} + +AABB AABB::grow(real_t p_by) const { + AABB aabb = *this; + aabb.grow_by(p_by); + return aabb; +} + +void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { + ERR_FAIL_INDEX(p_edge, 12); + switch (p_edge) { + case 0: { + r_from = Vector3(position.x + size.x, position.y, position.z); + r_to = Vector3(position.x, position.y, position.z); + } break; + case 1: { + r_from = Vector3(position.x + size.x, position.y, position.z + size.z); + r_to = Vector3(position.x + size.x, position.y, position.z); + } break; + case 2: { + r_from = Vector3(position.x, position.y, position.z + size.z); + r_to = Vector3(position.x + size.x, position.y, position.z + size.z); + + } break; + case 3: { + r_from = Vector3(position.x, position.y, position.z); + r_to = Vector3(position.x, position.y, position.z + size.z); + + } break; + case 4: { + r_from = Vector3(position.x, position.y + size.y, position.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z); + } break; + case 5: { + r_from = Vector3(position.x + size.x, position.y + size.y, position.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); + } break; + case 6: { + r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); + r_to = Vector3(position.x, position.y + size.y, position.z + size.z); + + } break; + case 7: { + r_from = Vector3(position.x, position.y + size.y, position.z + size.z); + r_to = Vector3(position.x, position.y + size.y, position.z); + + } break; + case 8: { + r_from = Vector3(position.x, position.y, position.z + size.z); + r_to = Vector3(position.x, position.y + size.y, position.z + size.z); + + } break; + case 9: { + r_from = Vector3(position.x, position.y, position.z); + r_to = Vector3(position.x, position.y + size.y, position.z); + + } break; + case 10: { + r_from = Vector3(position.x + size.x, position.y, position.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z); + + } break; + case 11: { + r_from = Vector3(position.x + size.x, position.y, position.z + size.z); + r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); + + } break; + } +} + +AABB::operator String() const { + return position.operator String() + " - " + size.operator String(); +} + +} // namespace godot diff --git a/src/variant/basis.cpp b/src/variant/basis.cpp new file mode 100644 index 00000000..6fa7de95 --- /dev/null +++ b/src/variant/basis.cpp @@ -0,0 +1,1113 @@ +#include +#include +#include + +#define cofac(row1, col1, row2, col2) \ + (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1]) + +namespace godot { + +void Basis::from_z(const Vector3 &p_z) { + if (Math::abs(p_z.z) > Math_SQRT12) { + // choose p in y-z plane + real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2]; + real_t k = 1.0 / Math::sqrt(a); + elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k); + elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]); + } else { + // choose p in x-y plane + real_t a = p_z.x * p_z.x + p_z.y * p_z.y; + real_t k = 1.0 / Math::sqrt(a); + elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0); + elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k); + } + elements[2] = p_z; +} + +void Basis::invert() { + real_t co[3] = { + cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1) + }; + real_t det = elements[0][0] * co[0] + + elements[0][1] * co[1] + + elements[0][2] * co[2]; +#ifdef MATH_CHECKS + ERR_FAIL_COND(det == 0); +#endif + real_t s = 1.0 / det; + + set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s, + co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s, + co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s); +} + +void Basis::orthonormalize() { + // Gram-Schmidt Process + + Vector3 x = get_axis(0); + Vector3 y = get_axis(1); + Vector3 z = get_axis(2); + + x.normalize(); + y = (y - x * (x.dot(y))); + y.normalize(); + z = (z - x * (x.dot(z)) - y * (y.dot(z))); + z.normalize(); + + set_axis(0, x); + set_axis(1, y); + set_axis(2, z); +} + +Basis Basis::orthonormalized() const { + Basis c = *this; + c.orthonormalize(); + return c; +} + +bool Basis::is_orthogonal() const { + Basis identity; + Basis m = (*this) * transposed(); + + return m.is_equal_approx(identity); +} + +bool Basis::is_diagonal() const { + return ( + Math::is_zero_approx(elements[0][1]) && Math::is_zero_approx(elements[0][2]) && + Math::is_zero_approx(elements[1][0]) && Math::is_zero_approx(elements[1][2]) && + Math::is_zero_approx(elements[2][0]) && Math::is_zero_approx(elements[2][1])); +} + +bool Basis::is_rotation() const { + return Math::is_equal_approx(determinant(), 1, UNIT_EPSILON) && is_orthogonal(); +} + +#ifdef MATH_CHECKS +// This method is only used once, in diagonalize. If it's desired elsewhere, feel free to remove the #ifdef. +bool Basis::is_symmetric() const { + if (!Math::is_equal_approx(elements[0][1], elements[1][0])) { + return false; + } + if (!Math::is_equal_approx(elements[0][2], elements[2][0])) { + return false; + } + if (!Math::is_equal_approx(elements[1][2], elements[2][1])) { + return false; + } + + return true; +} +#endif + +Basis Basis::diagonalize() { +//NOTE: only implemented for symmetric matrices +//with the Jacobi iterative method method +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_symmetric(), Basis()); +#endif + const int ite_max = 1024; + + real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2]; + + int ite = 0; + Basis acc_rot; + while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) { + real_t el01_2 = elements[0][1] * elements[0][1]; + real_t el02_2 = elements[0][2] * elements[0][2]; + real_t el12_2 = elements[1][2] * elements[1][2]; + // Find the pivot element + int i, j; + if (el01_2 > el02_2) { + if (el12_2 > el01_2) { + i = 1; + j = 2; + } else { + i = 0; + j = 1; + } + } else { + if (el12_2 > el02_2) { + i = 1; + j = 2; + } else { + i = 0; + j = 2; + } + } + + // Compute the rotation angle + real_t angle; + if (Math::is_equal_approx(elements[j][j], elements[i][i])) { + angle = Math_PI / 4; + } else { + angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i])); + } + + // Compute the rotation matrix + Basis rot; + rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle); + rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle)); + + // Update the off matrix norm + off_matrix_norm_2 -= elements[i][j] * elements[i][j]; + + // Apply the rotation + *this = rot * *this * rot.transposed(); + acc_rot = rot * acc_rot; + } + + return acc_rot; +} + +Basis Basis::inverse() const { + Basis inv = *this; + inv.invert(); + return inv; +} + +void Basis::transpose() { + SWAP(elements[0][1], elements[1][0]); + SWAP(elements[0][2], elements[2][0]); + SWAP(elements[1][2], elements[2][1]); +} + +Basis Basis::transposed() const { + Basis tr = *this; + tr.transpose(); + return tr; +} + +// Multiplies the matrix from left by the scaling matrix: M -> S.M +// See the comment for Basis::rotated for further explanation. +void Basis::scale(const Vector3 &p_scale) { + elements[0][0] *= p_scale.x; + elements[0][1] *= p_scale.x; + elements[0][2] *= p_scale.x; + elements[1][0] *= p_scale.y; + elements[1][1] *= p_scale.y; + elements[1][2] *= p_scale.y; + elements[2][0] *= p_scale.z; + elements[2][1] *= p_scale.z; + elements[2][2] *= p_scale.z; +} + +Basis Basis::scaled(const Vector3 &p_scale) const { + Basis m = *this; + m.scale(p_scale); + return m; +} + +void Basis::scale_local(const Vector3 &p_scale) { + // performs a scaling in object-local coordinate system: + // M -> (M.S.Minv).M = M.S. + *this = scaled_local(p_scale); +} + +float Basis::get_uniform_scale() const { + return (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0; +} + +void Basis::make_scale_uniform() { + float l = (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0; + for (int i = 0; i < 3; i++) { + elements[i].normalize(); + elements[i] *= l; + } +} + +Basis Basis::scaled_local(const Vector3 &p_scale) const { + Basis b; + b.set_diagonal(p_scale); + + return (*this) * b; +} + +Vector3 Basis::get_scale_abs() const { + return Vector3( + Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), + Vector3(elements[0][1], elements[1][1], elements[2][1]).length(), + Vector3(elements[0][2], elements[1][2], elements[2][2]).length()); +} + +Vector3 Basis::get_scale_local() const { + real_t det_sign = Math::sign(determinant()); + return det_sign * Vector3(elements[0].length(), elements[1].length(), elements[2].length()); +} + +// get_scale works with get_rotation, use get_scale_abs if you need to enforce positive signature. +Vector3 Basis::get_scale() const { + // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S. + // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and + // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal). + // + // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition + // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where + // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix, + // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P, + // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique. + // Therefore, we are going to do this decomposition by sticking to a particular convention. + // This may lead to confusion for some users though. + // + // The convention we use here is to absorb the sign flip into the scaling matrix. + // The same convention is also used in other similar functions such as get_rotation_axis_angle, get_rotation, ... + // + // A proper way to get rid of this issue would be to store the scaling values (or at least their signs) + // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the + // matrix elements. + // + // The rotation part of this decomposition is returned by get_rotation* functions. + real_t det_sign = Math::sign(determinant()); + return det_sign * Vector3( + Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), + Vector3(elements[0][1], elements[1][1], elements[2][1]).length(), + Vector3(elements[0][2], elements[1][2], elements[2][2]).length()); +} + +// Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S. +// Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3. +// This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so. +Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(determinant() == 0, Vector3()); + + Basis m = transposed() * (*this); + ERR_FAIL_COND_V(!m.is_diagonal(), Vector3()); +#endif + Vector3 scale = get_scale(); + Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale + rotref = (*this) * inv_scale; + +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3()); +#endif + return scale.abs(); +} + +// Multiplies the matrix from left by the rotation matrix: M -> R.M +// Note that this does *not* rotate the matrix itself. +// +// The main use of Basis is as Transform3D.basis, which is used a the transformation matrix +// of 3D object. Rotate here refers to rotation of the object (which is R * (*this)), +// not the matrix itself (which is R * (*this) * R.transposed()). +Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const { + return Basis(p_axis, p_phi) * (*this); +} + +void Basis::rotate(const Vector3 &p_axis, real_t p_phi) { + *this = rotated(p_axis, p_phi); +} + +void Basis::rotate_local(const Vector3 &p_axis, real_t p_phi) { + // performs a rotation in object-local coordinate system: + // M -> (M.R.Minv).M = M.R. + *this = rotated_local(p_axis, p_phi); +} + +Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_phi) const { + return (*this) * Basis(p_axis, p_phi); +} + +Basis Basis::rotated(const Vector3 &p_euler) const { + return Basis(p_euler) * (*this); +} + +void Basis::rotate(const Vector3 &p_euler) { + *this = rotated(p_euler); +} + +Basis Basis::rotated(const Quaternion &p_quat) const { + return Basis(p_quat) * (*this); +} + +void Basis::rotate(const Quaternion &p_quat) { + *this = rotated(p_quat); +} + +Vector3 Basis::get_rotation_euler() const { + // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, + // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). + // See the comment in get_scale() for further information. + Basis m = orthonormalized(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + return m.get_euler(); +} + +Quaternion Basis::get_rotation_quat() const { + // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, + // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). + // See the comment in get_scale() for further information. + Basis m = orthonormalized(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + return m.get_quat(); +} + +void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const { + // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, + // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). + // See the comment in get_scale() for further information. + Basis m = orthonormalized(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + m.get_axis_angle(p_axis, p_angle); +} + +void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const { + // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, + // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). + // See the comment in get_scale() for further information. + Basis m = transposed(); + m.orthonormalize(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + m.get_axis_angle(p_axis, p_angle); + p_angle = -p_angle; +} + +// get_euler_xyz returns a vector containing the Euler angles in the format +// (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last +// (following the convention they are commonly defined in the literature). +// +// The current implementation uses XYZ convention (Z is the first rotation), +// so euler.z is the angle of the (first) rotation around Z axis and so on, +// +// And thus, assuming the matrix is a rotation matrix, this function returns +// the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates +// around the z-axis by a and so on. +Vector3 Basis::get_euler_xyz() const { + // Euler angles in XYZ convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cy*cz -cy*sz sy + // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx + // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy + + Vector3 euler; + real_t sy = elements[0][2]; + if (sy < (1.0 - CMP_EPSILON)) { + if (sy > -(1.0 - CMP_EPSILON)) { + // is this a pure Y rotation? + if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) { + // return the simplest form (human friendlier in editor and scripts) + euler.x = 0; + euler.y = atan2(elements[0][2], elements[0][0]); + euler.z = 0; + } else { + euler.x = Math::atan2(-elements[1][2], elements[2][2]); + euler.y = Math::asin(sy); + euler.z = Math::atan2(-elements[0][1], elements[0][0]); + } + } else { + euler.x = Math::atan2(elements[2][1], elements[1][1]); + euler.y = -Math_PI / 2.0; + euler.z = 0.0; + } + } else { + euler.x = Math::atan2(elements[2][1], elements[1][1]); + euler.y = Math_PI / 2.0; + euler.z = 0.0; + } + return euler; +} + +// set_euler_xyz expects a vector containing the Euler angles in the format +// (ax,ay,az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// The current implementation uses XYZ convention (Z is the first rotation). +void Basis::set_euler_xyz(const Vector3 &p_euler) { + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + //optimizer will optimize away all this anyway + *this = xmat * (ymat * zmat); +} + +Vector3 Basis::get_euler_xzy() const { + // Euler angles in XZY convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cz*cy -sz cz*sy + // sx*sy+cx*cy*sz cx*cz cx*sz*sy-cy*sx + // cy*sx*sz cz*sx cx*cy+sx*sz*sy + + Vector3 euler; + real_t sz = elements[0][1]; + if (sz < (1.0 - CMP_EPSILON)) { + if (sz > -(1.0 - CMP_EPSILON)) { + euler.x = Math::atan2(elements[2][1], elements[1][1]); + euler.y = Math::atan2(elements[0][2], elements[0][0]); + euler.z = Math::asin(-sz); + } else { + // It's -1 + euler.x = -Math::atan2(elements[1][2], elements[2][2]); + euler.y = 0.0; + euler.z = Math_PI / 2.0; + } + } else { + // It's 1 + euler.x = -Math::atan2(elements[1][2], elements[2][2]); + euler.y = 0.0; + euler.z = -Math_PI / 2.0; + } + return euler; +} + +void Basis::set_euler_xzy(const Vector3 &p_euler) { + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + *this = xmat * zmat * ymat; +} + +Vector3 Basis::get_euler_yzx() const { + // Euler angles in YZX convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cy*cz sy*sx-cy*cx*sz cx*sy+cy*sz*sx + // sz cz*cx -cz*sx + // -cz*sy cy*sx+cx*sy*sz cy*cx-sy*sz*sx + + Vector3 euler; + real_t sz = elements[1][0]; + if (sz < (1.0 - CMP_EPSILON)) { + if (sz > -(1.0 - CMP_EPSILON)) { + euler.x = Math::atan2(-elements[1][2], elements[1][1]); + euler.y = Math::atan2(-elements[2][0], elements[0][0]); + euler.z = Math::asin(sz); + } else { + // It's -1 + euler.x = Math::atan2(elements[2][1], elements[2][2]); + euler.y = 0.0; + euler.z = -Math_PI / 2.0; + } + } else { + // It's 1 + euler.x = Math::atan2(elements[2][1], elements[2][2]); + euler.y = 0.0; + euler.z = Math_PI / 2.0; + } + return euler; +} + +void Basis::set_euler_yzx(const Vector3 &p_euler) { + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + *this = ymat * zmat * xmat; +} + +// get_euler_yxz returns a vector containing the Euler angles in the YXZ convention, +// as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned +// as the x, y, and z components of a Vector3 respectively. +Vector3 Basis::get_euler_yxz() const { + // Euler angles in YXZ convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy + // cx*sz cx*cz -sx + // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx + + Vector3 euler; + + real_t m12 = elements[1][2]; + + if (m12 < (1 - CMP_EPSILON)) { + if (m12 > -(1 - CMP_EPSILON)) { + // is this a pure X rotation? + if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) { + // return the simplest form (human friendlier in editor and scripts) + euler.x = atan2(-m12, elements[1][1]); + euler.y = 0; + euler.z = 0; + } else { + euler.x = asin(-m12); + euler.y = atan2(elements[0][2], elements[2][2]); + euler.z = atan2(elements[1][0], elements[1][1]); + } + } else { // m12 == -1 + euler.x = Math_PI * 0.5; + euler.y = atan2(elements[0][1], elements[0][0]); + euler.z = 0; + } + } else { // m12 == 1 + euler.x = -Math_PI * 0.5; + euler.y = -atan2(elements[0][1], elements[0][0]); + euler.z = 0; + } + + return euler; +} + +// set_euler_yxz expects a vector containing the Euler angles in the format +// (ax,ay,az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// The current implementation uses YXZ convention (Z is the first rotation). +void Basis::set_euler_yxz(const Vector3 &p_euler) { + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + //optimizer will optimize away all this anyway + *this = ymat * xmat * zmat; +} + +Vector3 Basis::get_euler_zxy() const { + // Euler angles in ZXY convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cz*cy-sz*sx*sy -cx*sz cz*sy+cy*sz*sx + // cy*sz+cz*sx*sy cz*cx sz*sy-cz*cy*sx + // -cx*sy sx cx*cy + Vector3 euler; + real_t sx = elements[2][1]; + if (sx < (1.0 - CMP_EPSILON)) { + if (sx > -(1.0 - CMP_EPSILON)) { + euler.x = Math::asin(sx); + euler.y = Math::atan2(-elements[2][0], elements[2][2]); + euler.z = Math::atan2(-elements[0][1], elements[1][1]); + } else { + // It's -1 + euler.x = -Math_PI / 2.0; + euler.y = Math::atan2(elements[0][2], elements[0][0]); + euler.z = 0; + } + } else { + // It's 1 + euler.x = Math_PI / 2.0; + euler.y = Math::atan2(elements[0][2], elements[0][0]); + euler.z = 0; + } + return euler; +} + +void Basis::set_euler_zxy(const Vector3 &p_euler) { + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + *this = zmat * xmat * ymat; +} + +Vector3 Basis::get_euler_zyx() const { + // Euler angles in ZYX convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cz*cy cz*sy*sx-cx*sz sz*sx+cz*cx*cy + // cy*sz cz*cx+sz*sy*sx cx*sz*sy-cz*sx + // -sy cy*sx cy*cx + Vector3 euler; + real_t sy = elements[2][0]; + if (sy < (1.0 - CMP_EPSILON)) { + if (sy > -(1.0 - CMP_EPSILON)) { + euler.x = Math::atan2(elements[2][1], elements[2][2]); + euler.y = Math::asin(-sy); + euler.z = Math::atan2(elements[1][0], elements[0][0]); + } else { + // It's -1 + euler.x = 0; + euler.y = Math_PI / 2.0; + euler.z = -Math::atan2(elements[0][1], elements[1][1]); + } + } else { + // It's 1 + euler.x = 0; + euler.y = -Math_PI / 2.0; + euler.z = -Math::atan2(elements[0][1], elements[1][1]); + } + return euler; +} + +void Basis::set_euler_zyx(const Vector3 &p_euler) { + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + *this = zmat * ymat * xmat; +} + +bool Basis::is_equal_approx(const Basis &p_basis) const { + return elements[0].is_equal_approx(p_basis.elements[0]) && elements[1].is_equal_approx(p_basis.elements[1]) && elements[2].is_equal_approx(p_basis.elements[2]); +} + +bool Basis::operator==(const Basis &p_matrix) const { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (elements[i][j] != p_matrix.elements[i][j]) { + return false; + } + } + } + + return true; +} + +bool Basis::operator!=(const Basis &p_matrix) const { + return (!(*this == p_matrix)); +} + +Basis::operator String() const { + String mtx; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (i != 0 || j != 0) { + mtx = mtx + ", "; + } + + mtx = mtx + String::num(elements[j][i]); //matrix is stored transposed for performance, so print it transposed + } + } + + return mtx; +} + +Quaternion Basis::get_quat() const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_rotation(), Quaternion()); +#endif + /* Allow getting a quaternion from an unnormalized transform */ + Basis m = *this; + real_t trace = m.elements[0][0] + m.elements[1][1] + m.elements[2][2]; + real_t temp[4]; + + if (trace > 0.0) { + real_t s = Math::sqrt(trace + 1.0); + temp[3] = (s * 0.5); + s = 0.5 / s; + + temp[0] = ((m.elements[2][1] - m.elements[1][2]) * s); + temp[1] = ((m.elements[0][2] - m.elements[2][0]) * s); + temp[2] = ((m.elements[1][0] - m.elements[0][1]) * s); + } else { + int i = m.elements[0][0] < m.elements[1][1] ? + (m.elements[1][1] < m.elements[2][2] ? 2 : 1) : + (m.elements[0][0] < m.elements[2][2] ? 2 : 0); + int j = (i + 1) % 3; + int k = (i + 2) % 3; + + real_t s = Math::sqrt(m.elements[i][i] - m.elements[j][j] - m.elements[k][k] + 1.0); + temp[i] = s * 0.5; + s = 0.5 / s; + + temp[3] = (m.elements[k][j] - m.elements[j][k]) * s; + temp[j] = (m.elements[j][i] + m.elements[i][j]) * s; + temp[k] = (m.elements[k][i] + m.elements[i][k]) * s; + } + + return Quaternion(temp[0], temp[1], temp[2], temp[3]); +} + +static const Basis _ortho_bases[24] = { + Basis(1, 0, 0, 0, 1, 0, 0, 0, 1), + Basis(0, -1, 0, 1, 0, 0, 0, 0, 1), + Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1), + Basis(0, 1, 0, -1, 0, 0, 0, 0, 1), + Basis(1, 0, 0, 0, 0, -1, 0, 1, 0), + Basis(0, 0, 1, 1, 0, 0, 0, 1, 0), + Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0), + Basis(0, 0, -1, -1, 0, 0, 0, 1, 0), + Basis(1, 0, 0, 0, -1, 0, 0, 0, -1), + Basis(0, 1, 0, 1, 0, 0, 0, 0, -1), + Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1), + Basis(0, -1, 0, -1, 0, 0, 0, 0, -1), + Basis(1, 0, 0, 0, 0, 1, 0, -1, 0), + Basis(0, 0, -1, 1, 0, 0, 0, -1, 0), + Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0), + Basis(0, 0, 1, -1, 0, 0, 0, -1, 0), + Basis(0, 0, 1, 0, 1, 0, -1, 0, 0), + Basis(0, -1, 0, 0, 0, 1, -1, 0, 0), + Basis(0, 0, -1, 0, -1, 0, -1, 0, 0), + Basis(0, 1, 0, 0, 0, -1, -1, 0, 0), + Basis(0, 0, 1, 0, -1, 0, 1, 0, 0), + Basis(0, 1, 0, 0, 0, 1, 1, 0, 0), + Basis(0, 0, -1, 0, 1, 0, 1, 0, 0), + Basis(0, -1, 0, 0, 0, -1, 1, 0, 0) +}; + +int Basis::get_orthogonal_index() const { + //could be sped up if i come up with a way + Basis orth = *this; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + real_t v = orth[i][j]; + if (v > 0.5) { + v = 1.0; + } else if (v < -0.5) { + v = -1.0; + } else { + v = 0; + } + + orth[i][j] = v; + } + } + + for (int i = 0; i < 24; i++) { + if (_ortho_bases[i] == orth) { + return i; + } + } + + return 0; +} + +void Basis::set_orthogonal_index(int p_index) { + //there only exist 24 orthogonal bases in r3 + ERR_FAIL_INDEX(p_index, 24); + + *this = _ortho_bases[p_index]; +} + +void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { + /* checking this is a bad idea, because obtaining from scaled transform is a valid use case +#ifdef MATH_CHECKS + ERR_FAIL_COND(!is_rotation()); +#endif +*/ + real_t angle, x, y, z; // variables for result + real_t epsilon = 0.01; // margin to allow for rounding errors + real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees + + if ((Math::abs(elements[1][0] - elements[0][1]) < epsilon) && (Math::abs(elements[2][0] - elements[0][2]) < epsilon) && (Math::abs(elements[2][1] - elements[1][2]) < epsilon)) { + // singularity found + // first check for identity matrix which must have +1 for all terms + // in leading diagonaland zero in other terms + if ((Math::abs(elements[1][0] + elements[0][1]) < epsilon2) && (Math::abs(elements[2][0] + elements[0][2]) < epsilon2) && (Math::abs(elements[2][1] + elements[1][2]) < epsilon2) && (Math::abs(elements[0][0] + elements[1][1] + elements[2][2] - 3) < epsilon2)) { + // this singularity is identity matrix so angle = 0 + r_axis = Vector3(0, 1, 0); + r_angle = 0; + return; + } + // otherwise this singularity is angle = 180 + angle = Math_PI; + real_t xx = (elements[0][0] + 1) / 2; + real_t yy = (elements[1][1] + 1) / 2; + real_t zz = (elements[2][2] + 1) / 2; + real_t xy = (elements[1][0] + elements[0][1]) / 4; + real_t xz = (elements[2][0] + elements[0][2]) / 4; + real_t yz = (elements[2][1] + elements[1][2]) / 4; + if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term + if (xx < epsilon) { + x = 0; + y = Math_SQRT12; + z = Math_SQRT12; + } else { + x = Math::sqrt(xx); + y = xy / x; + z = xz / x; + } + } else if (yy > zz) { // elements[1][1] is the largest diagonal term + if (yy < epsilon) { + x = Math_SQRT12; + y = 0; + z = Math_SQRT12; + } else { + y = Math::sqrt(yy); + x = xy / y; + z = yz / y; + } + } else { // elements[2][2] is the largest diagonal term so base result on this + if (zz < epsilon) { + x = Math_SQRT12; + y = Math_SQRT12; + z = 0; + } else { + z = Math::sqrt(zz); + x = xz / z; + y = yz / z; + } + } + r_axis = Vector3(x, y, z); + r_angle = angle; + return; + } + // as we have reached here there are no singularities so we can handle normally + real_t s = Math::sqrt((elements[1][2] - elements[2][1]) * (elements[1][2] - elements[2][1]) + (elements[2][0] - elements[0][2]) * (elements[2][0] - elements[0][2]) + (elements[0][1] - elements[1][0]) * (elements[0][1] - elements[1][0])); // s=|axis||sin(angle)|, used to normalise + + angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2); + if (angle < 0) { + s = -s; + } + x = (elements[2][1] - elements[1][2]) / s; + y = (elements[0][2] - elements[2][0]) / s; + z = (elements[1][0] - elements[0][1]) / s; + + r_axis = Vector3(x, y, z); + r_angle = angle; +} + +void Basis::set_quat(const Quaternion &p_quat) { + real_t d = p_quat.length_squared(); + real_t s = 2.0 / d; + real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s; + real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs; + real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs; + real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs; + set(1.0 - (yy + zz), xy - wz, xz + wy, + xy + wz, 1.0 - (xx + zz), yz - wx, + xz - wy, yz + wx, 1.0 - (xx + yy)); +} + +void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) { +// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle +#ifdef MATH_CHECKS + ERR_FAIL_COND(!p_axis.is_normalized()); +#endif + Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z); + real_t cosine = Math::cos(p_phi); + elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x); + elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y); + elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z); + + real_t sine = Math::sin(p_phi); + real_t t = 1 - cosine; + + real_t xyzt = p_axis.x * p_axis.y * t; + real_t zyxs = p_axis.z * sine; + elements[0][1] = xyzt - zyxs; + elements[1][0] = xyzt + zyxs; + + xyzt = p_axis.x * p_axis.z * t; + zyxs = p_axis.y * sine; + elements[0][2] = xyzt + zyxs; + elements[2][0] = xyzt - zyxs; + + xyzt = p_axis.y * p_axis.z * t; + zyxs = p_axis.x * sine; + elements[1][2] = xyzt - zyxs; + elements[2][1] = xyzt + zyxs; +} + +void Basis::set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { + set_diagonal(p_scale); + rotate(p_axis, p_phi); +} + +void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale) { + set_diagonal(p_scale); + rotate(p_euler); +} + +void Basis::set_quat_scale(const Quaternion &p_quat, const Vector3 &p_scale) { + set_diagonal(p_scale); + rotate(p_quat); +} + +void Basis::set_diagonal(const Vector3 &p_diag) { + elements[0][0] = p_diag.x; + elements[0][1] = 0; + elements[0][2] = 0; + + elements[1][0] = 0; + elements[1][1] = p_diag.y; + elements[1][2] = 0; + + elements[2][0] = 0; + elements[2][1] = 0; + elements[2][2] = p_diag.z; +} + +Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const { + //consider scale + Quaternion from(*this); + Quaternion to(p_to); + + Basis b(from.slerp(to, p_weight)); + b.elements[0] *= Math::lerp(elements[0].length(), p_to.elements[0].length(), p_weight); + b.elements[1] *= Math::lerp(elements[1].length(), p_to.elements[1].length(), p_weight); + b.elements[2] *= Math::lerp(elements[2].length(), p_to.elements[2].length(), p_weight); + + return b; +} + +void Basis::rotate_sh(real_t *p_values) { + // code by John Hable + // http://filmicworlds.com/blog/simple-and-fast-spherical-harmonic-rotation/ + // this code is Public Domain + + const static real_t s_c3 = 0.94617469575; // (3*sqrt(5))/(4*sqrt(pi)) + const static real_t s_c4 = -0.31539156525; // (-sqrt(5))/(4*sqrt(pi)) + const static real_t s_c5 = 0.54627421529; // (sqrt(15))/(4*sqrt(pi)) + + const static real_t s_c_scale = 1.0 / 0.91529123286551084; + const static real_t s_c_scale_inv = 0.91529123286551084; + + const static real_t s_rc2 = 1.5853309190550713 * s_c_scale; + const static real_t s_c4_div_c3 = s_c4 / s_c3; + const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * 2.0; + + const static real_t s_scale_dst2 = s_c3 * s_c_scale_inv; + const static real_t s_scale_dst4 = s_c5 * s_c_scale_inv; + + real_t src[9] = { p_values[0], p_values[1], p_values[2], p_values[3], p_values[4], p_values[5], p_values[6], p_values[7], p_values[8] }; + + real_t m00 = elements[0][0]; + real_t m01 = elements[0][1]; + real_t m02 = elements[0][2]; + real_t m10 = elements[1][0]; + real_t m11 = elements[1][1]; + real_t m12 = elements[1][2]; + real_t m20 = elements[2][0]; + real_t m21 = elements[2][1]; + real_t m22 = elements[2][2]; + + p_values[0] = src[0]; + p_values[1] = m11 * src[1] - m12 * src[2] + m10 * src[3]; + p_values[2] = -m21 * src[1] + m22 * src[2] - m20 * src[3]; + p_values[3] = m01 * src[1] - m02 * src[2] + m00 * src[3]; + + real_t sh0 = src[7] + src[8] + src[8] - src[5]; + real_t sh1 = src[4] + s_rc2 * src[6] + src[7] + src[8]; + real_t sh2 = src[4]; + real_t sh3 = -src[7]; + real_t sh4 = -src[5]; + + // Rotations. R0 and R1 just use the raw matrix columns + real_t r2x = m00 + m01; + real_t r2y = m10 + m11; + real_t r2z = m20 + m21; + + real_t r3x = m00 + m02; + real_t r3y = m10 + m12; + real_t r3z = m20 + m22; + + real_t r4x = m01 + m02; + real_t r4y = m11 + m12; + real_t r4z = m21 + m22; + + // dense matrix multiplication one column at a time + + // column 0 + real_t sh0_x = sh0 * m00; + real_t sh0_y = sh0 * m10; + real_t d0 = sh0_x * m10; + real_t d1 = sh0_y * m20; + real_t d2 = sh0 * (m20 * m20 + s_c4_div_c3); + real_t d3 = sh0_x * m20; + real_t d4 = sh0_x * m00 - sh0_y * m10; + + // column 1 + real_t sh1_x = sh1 * m02; + real_t sh1_y = sh1 * m12; + d0 += sh1_x * m12; + d1 += sh1_y * m22; + d2 += sh1 * (m22 * m22 + s_c4_div_c3); + d3 += sh1_x * m22; + d4 += sh1_x * m02 - sh1_y * m12; + + // column 2 + real_t sh2_x = sh2 * r2x; + real_t sh2_y = sh2 * r2y; + d0 += sh2_x * r2y; + d1 += sh2_y * r2z; + d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2); + d3 += sh2_x * r2z; + d4 += sh2_x * r2x - sh2_y * r2y; + + // column 3 + real_t sh3_x = sh3 * r3x; + real_t sh3_y = sh3 * r3y; + d0 += sh3_x * r3y; + d1 += sh3_y * r3z; + d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2); + d3 += sh3_x * r3z; + d4 += sh3_x * r3x - sh3_y * r3y; + + // column 4 + real_t sh4_x = sh4 * r4x; + real_t sh4_y = sh4 * r4y; + d0 += sh4_x * r4y; + d1 += sh4_y * r4z; + d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2); + d3 += sh4_x * r4z; + d4 += sh4_x * r4x - sh4_y * r4y; + + // extra multipliers + p_values[4] = d0; + p_values[5] = -d1; + p_values[6] = d2 * s_scale_dst2; + p_values[7] = -d3; + p_values[8] = d4 * s_scale_dst4; +} + +} // namespace godot diff --git a/src/variant/char_string.cpp b/src/variant/char_string.cpp index 19c99b40..72f4a6c4 100644 --- a/src/variant/char_string.cpp +++ b/src/variant/char_string.cpp @@ -192,6 +192,14 @@ bool String::operator!=(const char32_t *p_str) const { return *this != String(p_str); } +const char32_t &String::operator[](int p_index) const { + return *internal::interface->string_operator_index_const((GDNativeStringPtr) this, p_index); +} + +char32_t &String::operator[](int p_index) { + return *internal::interface->string_operator_index((GDNativeStringPtr) this, p_index); +} + bool operator==(const char *p_chr, const String &p_str) { return p_str == String(p_chr); } diff --git a/src/variant/color.cpp b/src/variant/color.cpp new file mode 100644 index 00000000..6af55a64 --- /dev/null +++ b/src/variant/color.cpp @@ -0,0 +1,532 @@ +#include +#include +#include +#include + +namespace godot { + +uint32_t Color::to_argb32() const { + uint32_t c = (uint8_t)Math::round(a * 255); + c <<= 8; + c |= (uint8_t)Math::round(r * 255); + c <<= 8; + c |= (uint8_t)Math::round(g * 255); + c <<= 8; + c |= (uint8_t)Math::round(b * 255); + + return c; +} + +uint32_t Color::to_abgr32() const { + uint32_t c = (uint8_t)Math::round(a * 255); + c <<= 8; + c |= (uint8_t)Math::round(b * 255); + c <<= 8; + c |= (uint8_t)Math::round(g * 255); + c <<= 8; + c |= (uint8_t)Math::round(r * 255); + + return c; +} + +uint32_t Color::to_rgba32() const { + uint32_t c = (uint8_t)Math::round(r * 255); + c <<= 8; + c |= (uint8_t)Math::round(g * 255); + c <<= 8; + c |= (uint8_t)Math::round(b * 255); + c <<= 8; + c |= (uint8_t)Math::round(a * 255); + + return c; +} + +uint64_t Color::to_abgr64() const { + uint64_t c = (uint16_t)Math::round(a * 65535); + c <<= 16; + c |= (uint16_t)Math::round(b * 65535); + c <<= 16; + c |= (uint16_t)Math::round(g * 65535); + c <<= 16; + c |= (uint16_t)Math::round(r * 65535); + + return c; +} + +uint64_t Color::to_argb64() const { + uint64_t c = (uint16_t)Math::round(a * 65535); + c <<= 16; + c |= (uint16_t)Math::round(r * 65535); + c <<= 16; + c |= (uint16_t)Math::round(g * 65535); + c <<= 16; + c |= (uint16_t)Math::round(b * 65535); + + return c; +} + +uint64_t Color::to_rgba64() const { + uint64_t c = (uint16_t)Math::round(r * 65535); + c <<= 16; + c |= (uint16_t)Math::round(g * 65535); + c <<= 16; + c |= (uint16_t)Math::round(b * 65535); + c <<= 16; + c |= (uint16_t)Math::round(a * 65535); + + return c; +} + +float Color::get_h() const { + float min = Math::min(r, g); + min = Math::min(min, b); + float max = Math::max(r, g); + max = Math::max(max, b); + + float delta = max - min; + + if (delta == 0) { + return 0; + } + + float h; + if (r == max) { + h = (g - b) / delta; // between yellow & magenta + } else if (g == max) { + h = 2 + (b - r) / delta; // between cyan & yellow + } else { + h = 4 + (r - g) / delta; // between magenta & cyan + } + + h /= 6.0; + if (h < 0) { + h += 1.0; + } + + return h; +} + +float Color::get_s() const { + float min = Math::min(r, g); + min = Math::min(min, b); + float max = Math::max(r, g); + max = Math::max(max, b); + + float delta = max - min; + + return (max != 0) ? (delta / max) : 0; +} + +float Color::get_v() const { + float max = Math::max(r, g); + max = Math::max(max, b); + return max; +} + +void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) { + int i; + float f, p, q, t; + a = p_alpha; + + if (p_s == 0) { + // Achromatic (grey) + r = g = b = p_v; + return; + } + + p_h *= 6.0; + p_h = Math::fmod(p_h, 6); + i = Math::floor(p_h); + + f = p_h - i; + p = p_v * (1 - p_s); + q = p_v * (1 - p_s * f); + t = p_v * (1 - p_s * (1 - f)); + + switch (i) { + case 0: // Red is the dominant color + r = p_v; + g = t; + b = p; + break; + case 1: // Green is the dominant color + r = q; + g = p_v; + b = p; + break; + case 2: + r = p; + g = p_v; + b = t; + break; + case 3: // Blue is the dominant color + r = p; + g = q; + b = p_v; + break; + case 4: + r = t; + g = p; + b = p_v; + break; + default: // (5) Red is the dominant color + r = p_v; + g = p; + b = q; + break; + } +} + +bool Color::is_equal_approx(const Color &p_color) const { + return Math::is_equal_approx(r, p_color.r) && Math::is_equal_approx(g, p_color.g) && Math::is_equal_approx(b, p_color.b) && Math::is_equal_approx(a, p_color.a); +} + +void Color::invert() { + r = 1.0 - r; + g = 1.0 - g; + b = 1.0 - b; +} + +Color Color::hex(uint32_t p_hex) { + float a = (p_hex & 0xFF) / 255.0; + p_hex >>= 8; + float b = (p_hex & 0xFF) / 255.0; + p_hex >>= 8; + float g = (p_hex & 0xFF) / 255.0; + p_hex >>= 8; + float r = (p_hex & 0xFF) / 255.0; + + return Color(r, g, b, a); +} + +Color Color::hex64(uint64_t p_hex) { + float a = (p_hex & 0xFFFF) / 65535.0; + p_hex >>= 16; + float b = (p_hex & 0xFFFF) / 65535.0; + p_hex >>= 16; + float g = (p_hex & 0xFFFF) / 65535.0; + p_hex >>= 16; + float r = (p_hex & 0xFFFF) / 65535.0; + + return Color(r, g, b, a); +} + +Color Color::from_rgbe9995(uint32_t p_rgbe) { + float r = p_rgbe & 0x1ff; + float g = (p_rgbe >> 9) & 0x1ff; + float b = (p_rgbe >> 18) & 0x1ff; + float e = (p_rgbe >> 27); + float m = Math::pow(2, e - 15.0 - 9.0); + + float rd = r * m; + float gd = g * m; + float bd = b * m; + + return Color(rd, gd, bd, 1.0f); +} + +static int _parse_col4(const String &p_str, int p_ofs) { + char character = p_str[p_ofs]; + + if (character >= '0' && character <= '9') { + return character - '0'; + } else if (character >= 'a' && character <= 'f') { + return character + (10 - 'a'); + } else if (character >= 'A' && character <= 'F') { + return character + (10 - 'A'); + } + return -1; +} + +static int _parse_col8(const String &p_str, int p_ofs) { + return _parse_col4(p_str, p_ofs) * 16 + _parse_col4(p_str, p_ofs + 1); +} + +Color Color::inverted() const { + Color c = *this; + c.invert(); + return c; +} + +Color Color::html(const String &p_rgba) { + String color = p_rgba; + if (color.length() == 0) { + return Color(); + } + if (color[0] == '#') { + color = color.substr(1); + } + + // If enabled, use 1 hex digit per channel instead of 2. + // Other sizes aren't in the HTML/CSS spec but we could add them if desired. + bool is_shorthand = color.length() < 5; + bool alpha = false; + + if (color.length() == 8) { + alpha = true; + } else if (color.length() == 6) { + alpha = false; + } else if (color.length() == 4) { + alpha = true; + } else if (color.length() == 3) { + alpha = false; + } else { + ERR_FAIL_V(Color()); + } + + float r, g, b, a = 1.0; + if (is_shorthand) { + r = _parse_col4(color, 0) / 15.0; + g = _parse_col4(color, 1) / 15.0; + b = _parse_col4(color, 2) / 15.0; + if (alpha) { + a = _parse_col4(color, 3) / 15.0; + } + } else { + r = _parse_col8(color, 0) / 255.0; + g = _parse_col8(color, 2) / 255.0; + b = _parse_col8(color, 4) / 255.0; + if (alpha) { + a = _parse_col8(color, 6) / 255.0; + } + } + ERR_FAIL_COND_V(r < 0, Color()); + ERR_FAIL_COND_V(g < 0, Color()); + ERR_FAIL_COND_V(b < 0, Color()); + ERR_FAIL_COND_V(a < 0, Color()); + + return Color(r, g, b, a); +} + +bool Color::html_is_valid(const String &p_color) { + String color = p_color; + + if (color.length() == 0) { + return false; + } + if (color[0] == '#') { + color = color.substr(1); + } + + // Check if the amount of hex digits is valid. + int len = color.length(); + if (!(len == 3 || len == 4 || len == 6 || len == 8)) { + return false; + } + + // Check if each hex digit is valid. + for (int i = 0; i < len; i++) { + if (_parse_col4(color, i) == -1) { + return false; + } + } + + return true; +} + +Color Color::named(const String &p_name) { + int idx = find_named_color(p_name); + if (idx == -1) { + ERR_FAIL_V(Color()); + return Color(); + } + return get_named_color(idx); +} + +Color Color::named(const String &p_name, const Color &p_default) { + int idx = find_named_color(p_name); + if (idx == -1) { + return p_default; + } + return get_named_color(idx); +} + +int Color::find_named_color(const String &p_name) { + String name = p_name; + // Normalize name + name = name.replace(" ", ""); + name = name.replace("-", ""); + name = name.replace("_", ""); + name = name.replace("'", ""); + name = name.replace(".", ""); + name = name.to_lower(); + + int idx = 0; + while (named_colors[idx].name != nullptr) { + if (name == String(named_colors[idx].name)) { + return idx; + } + idx++; + } + + return -1; +} + +int Color::get_named_color_count() { + int idx = 0; + while (named_colors[idx].name != nullptr) { + idx++; + } + return idx; +} + +String Color::get_named_color_name(int p_idx) { + return named_colors[p_idx].name; +} + +Color Color::get_named_color(int p_idx) { + return named_colors[p_idx].color; +} + +// For a version that errors on invalid values instead of returning +// a default color, use the Color(String) constructor instead. +Color Color::from_string(const String &p_string, const Color &p_default) { + if (html_is_valid(p_string)) { + return html(p_string); + } else { + return named(p_string, p_default); + } +} + +String _to_hex(float p_val) { + int v = Math::round(p_val * 255); + v = Math::clamp(v, 0, 255); + String ret; + + for (int i = 0; i < 2; i++) { + char32_t c[2] = { 0, 0 }; + int lv = v & 0xF; + if (lv < 10) { + c[0] = '0' + lv; + } else { + c[0] = 'a' + lv - 10; + } + + v >>= 4; + String cs = (const char32_t *)c; + ret = cs + ret; + } + + return ret; +} + +String Color::to_html(bool p_alpha) const { + String txt; + txt = txt + _to_hex(g); + txt = txt + _to_hex(b); + txt = txt + _to_hex(r); + if (p_alpha) { + txt = txt + _to_hex(a); + } + return txt; +} + +Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) { + Color result; + result.set_hsv(p_h, p_s, p_v, p_a); + return result; +} + +Color::operator String() const { + return String::num(r, 3) + ", " + String::num(g, 3) + ", " + String::num(b, 3) + ", " + String::num(a, 3); +} + +Color Color::operator+(const Color &p_color) const { + return Color( + r + p_color.r, + g + p_color.g, + b + p_color.b, + a + p_color.a); +} + +void Color::operator+=(const Color &p_color) { + r = r + p_color.r; + g = g + p_color.g; + b = b + p_color.b; + a = a + p_color.a; +} + +Color Color::operator-(const Color &p_color) const { + return Color( + r - p_color.r, + g - p_color.g, + b - p_color.b, + a - p_color.a); +} + +void Color::operator-=(const Color &p_color) { + r = r - p_color.r; + g = g - p_color.g; + b = b - p_color.b; + a = a - p_color.a; +} + +Color Color::operator*(const Color &p_color) const { + return Color( + r * p_color.r, + g * p_color.g, + b * p_color.b, + a * p_color.a); +} + +Color Color::operator*(float p_scalar) const { + return Color( + r * p_scalar, + g * p_scalar, + b * p_scalar, + a * p_scalar); +} + +void Color::operator*=(const Color &p_color) { + r = r * p_color.r; + g = g * p_color.g; + b = b * p_color.b; + a = a * p_color.a; +} + +void Color::operator*=(float p_scalar) { + r = r * p_scalar; + g = g * p_scalar; + b = b * p_scalar; + a = a * p_scalar; +} + +Color Color::operator/(const Color &p_color) const { + return Color( + r / p_color.r, + g / p_color.g, + b / p_color.b, + a / p_color.a); +} + +Color Color::operator/(float p_scalar) const { + return Color( + r / p_scalar, + g / p_scalar, + b / p_scalar, + a / p_scalar); +} + +void Color::operator/=(const Color &p_color) { + r = r / p_color.r; + g = g / p_color.g; + b = b / p_color.b; + a = a / p_color.a; +} + +void Color::operator/=(float p_scalar) { + r = r / p_scalar; + g = g / p_scalar; + b = b / p_scalar; + a = a / p_scalar; +} + +Color Color::operator-() const { + return Color( + 1.0 - r, + 1.0 - g, + 1.0 - b, + 1.0 - a); +} + +} // namespace godot diff --git a/src/variant/packed_arrays.cpp b/src/variant/packed_arrays.cpp new file mode 100644 index 00000000..e3cfeab8 --- /dev/null +++ b/src/variant/packed_arrays.cpp @@ -0,0 +1,97 @@ +// extra functions for packed arrays + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace godot { + +const uint8_t &PackedByteArray::operator[](int p_index) const { + return *internal::interface->packed_byte_array_operator_index_const((GDNativeTypePtr *)this, p_index); +} + +uint8_t &PackedByteArray::operator[](int p_index) { + return *internal::interface->packed_byte_array_operator_index((GDNativeTypePtr *)this, p_index); +} + +const Color &PackedColorArray::operator[](int p_index) const { + const Color *color = (const Color *) internal::interface->packed_color_array_operator_index_const((GDNativeTypePtr *)this, p_index); + return *color; +} + +Color &PackedColorArray::operator[](int p_index) { + Color *color = (Color *) internal::interface->packed_color_array_operator_index((GDNativeTypePtr *)this, p_index); + return *color; +} + +const float &PackedFloat32Array::operator[](int p_index) const { + return *internal::interface->packed_float32_array_operator_index_const((GDNativeTypePtr *)this, p_index); +} + +float &PackedFloat32Array::operator[](int p_index) { + return *internal::interface->packed_float32_array_operator_index((GDNativeTypePtr *)this, p_index); +} + +const double &PackedFloat64Array::operator[](int p_index) const { + return *internal::interface->packed_float64_array_operator_index_const((GDNativeTypePtr *)this, p_index); +} + +double &PackedFloat64Array::operator[](int p_index) { + return *internal::interface->packed_float64_array_operator_index((GDNativeTypePtr *)this, p_index); +} + +const int32_t &PackedInt32Array::operator[](int p_index) const { + return *internal::interface->packed_int32_array_operator_index_const((GDNativeTypePtr *)this, p_index); +} + +int32_t &PackedInt32Array::operator[](int p_index) { + return *internal::interface->packed_int32_array_operator_index((GDNativeTypePtr *)this, p_index); +} + +const int64_t &PackedInt64Array::operator[](int p_index) const { + return *internal::interface->packed_int64_array_operator_index_const((GDNativeTypePtr *)this, p_index); +} + +int64_t &PackedInt64Array::operator[](int p_index) { + return *internal::interface->packed_int64_array_operator_index((GDNativeTypePtr *)this, p_index); +} + +const String &PackedStringArray::operator[](int p_index) const { + const String *string = (const String *) internal::interface->packed_string_array_operator_index_const((GDNativeTypePtr *)this, p_index); + return *string; +} + +String &PackedStringArray::operator[](int p_index) { + String *string = (String *) internal::interface->packed_string_array_operator_index((GDNativeTypePtr *)this, p_index); + return *string; +} + +const Vector2 &PackedVector2Array::operator[](int p_index) const { + const Vector2 *vec = (const Vector2 *) internal::interface->packed_vector2_array_operator_index_const((GDNativeTypePtr *)this, p_index); + return *vec; +} + +Vector2 &PackedVector2Array::operator[](int p_index) { + Vector2 *vec = (Vector2 *) internal::interface->packed_vector2_array_operator_index((GDNativeTypePtr *)this, p_index); + return *vec; +} + +const Vector3 &PackedVector3Array::operator[](int p_index) const { + const Vector3 *vec = (const Vector3 *) internal::interface->packed_vector3_array_operator_index_const((GDNativeTypePtr *)this, p_index); + return *vec; +} + +Vector3 &PackedVector3Array::operator[](int p_index) { + Vector3 *vec = (Vector3 *) internal::interface->packed_vector3_array_operator_index((GDNativeTypePtr *)this, p_index); + return *vec; +} + +} // namespace godot diff --git a/src/variant/plane.cpp b/src/variant/plane.cpp new file mode 100644 index 00000000..aa9be343 --- /dev/null +++ b/src/variant/plane.cpp @@ -0,0 +1,127 @@ +#include + +#include + +namespace godot { + +void Plane::set_normal(const Vector3 &p_normal) { + normal = p_normal; +} + +void Plane::normalize() { + real_t l = normal.length(); + if (l == 0) { + *this = Plane(0, 0, 0, 0); + return; + } + normal /= l; + d /= l; +} + +Plane Plane::normalized() const { + Plane p = *this; + p.normalize(); + return p; +} + +Vector3 Plane::get_any_perpendicular_normal() const { + static const Vector3 p1 = Vector3(1, 0, 0); + static const Vector3 p2 = Vector3(0, 1, 0); + Vector3 p; + + if (Math::abs(normal.dot(p1)) > 0.99) { // if too similar to p1 + p = p2; // use p2 + } else { + p = p1; // use p1 + } + + p -= normal * normal.dot(p); + p.normalize(); + + return p; +} + +/* intersections */ + +bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const { + const Plane &p_plane0 = *this; + Vector3 normal0 = p_plane0.normal; + Vector3 normal1 = p_plane1.normal; + Vector3 normal2 = p_plane2.normal; + + real_t denom = vec3_cross(normal0, normal1).dot(normal2); + + if (Math::is_zero_approx(denom)) { + return false; + } + + if (r_result) { + *r_result = ((vec3_cross(normal1, normal2) * p_plane0.d) + + (vec3_cross(normal2, normal0) * p_plane1.d) + + (vec3_cross(normal0, normal1) * p_plane2.d)) / + denom; + } + + return true; +} + +bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const { + Vector3 segment = p_dir; + real_t den = normal.dot(segment); + + //printf("den is %i\n",den); + if (Math::is_zero_approx(den)) { + return false; + } + + real_t dist = (normal.dot(p_from) - d) / den; + //printf("dist is %i\n",dist); + + if (dist > CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist + + return false; + } + + dist = -dist; + *p_intersection = p_from + segment * dist; + + return true; +} + +bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const { + Vector3 segment = p_begin - p_end; + real_t den = normal.dot(segment); + + //printf("den is %i\n",den); + if (Math::is_zero_approx(den)) { + return false; + } + + real_t dist = (normal.dot(p_begin) - d) / den; + //printf("dist is %i\n",dist); + + if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) { + return false; + } + + dist = -dist; + *p_intersection = p_begin + segment * dist; + + return true; +} + +/* misc */ + +bool Plane::is_equal_approx_any_side(const Plane &p_plane) const { + return (normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d)) || (normal.is_equal_approx(-p_plane.normal) && Math::is_equal_approx(d, -p_plane.d)); +} + +bool Plane::is_equal_approx(const Plane &p_plane) const { + return normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d); +} + +Plane::operator String() const { + return normal.operator String() + ", " + String::num(d,3); +} + +} // namespace godot diff --git a/src/variant/quaternion.cpp b/src/variant/quaternion.cpp new file mode 100644 index 00000000..5ceaf237 --- /dev/null +++ b/src/variant/quaternion.cpp @@ -0,0 +1,203 @@ +#include + +#include +#include + +namespace godot { + +// get_euler_xyz returns a vector containing the Euler angles in the format +// (ax,ay,az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// This implementation uses XYZ convention (Z is the first rotation). +Vector3 Quaternion::get_euler_xyz() const { + Basis m(*this); + return m.get_euler_xyz(); +} + +// get_euler_yxz returns a vector containing the Euler angles in the format +// (ax,ay,az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// This implementation uses YXZ convention (Z is the first rotation). +Vector3 Quaternion::get_euler_yxz() const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_normalized(), Vector3(0, 0, 0)); +#endif + Basis m(*this); + return m.get_euler_yxz(); +} + +void Quaternion::operator*=(const Quaternion &p_q) { + x = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y; + y = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z; + z = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x; + w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z; +} + +Quaternion Quaternion::operator*(const Quaternion &p_q) const { + Quaternion r = *this; + r *= p_q; + return r; +} + +bool Quaternion::is_equal_approx(const Quaternion &p_quat) const { + return Math::is_equal_approx(x, p_quat.x) && Math::is_equal_approx(y, p_quat.y) && Math::is_equal_approx(z, p_quat.z) && Math::is_equal_approx(w, p_quat.w); +} + +real_t Quaternion::length() const { + return Math::sqrt(length_squared()); +} + +void Quaternion::normalize() { + *this /= length(); +} + +Quaternion Quaternion::normalized() const { + return *this / length(); +} + +bool Quaternion::is_normalized() const { + return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); //use less epsilon +} + +Quaternion Quaternion::inverse() const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_normalized(), Quaternion()); +#endif + return Quaternion(-x, -y, -z, w); +} + +Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_normalized(), Quaternion()); + ERR_FAIL_COND_V(!p_to.is_normalized(), Quaternion()); +#endif + Quaternion to1; + real_t omega, cosom, sinom, scale0, scale1; + + // calc cosine + cosom = dot(p_to); + + // adjust signs (if necessary) + if (cosom < 0.0) { + cosom = -cosom; + to1.x = -p_to.x; + to1.y = -p_to.y; + to1.z = -p_to.z; + to1.w = -p_to.w; + } else { + to1.x = p_to.x; + to1.y = p_to.y; + to1.z = p_to.z; + to1.w = p_to.w; + } + + // calculate coefficients + + if ((1.0 - cosom) > CMP_EPSILON) { + // standard case (slerp) + omega = Math::acos(cosom); + sinom = Math::sin(omega); + scale0 = Math::sin((1.0 - p_weight) * omega) / sinom; + scale1 = Math::sin(p_weight * omega) / sinom; + } else { + // "from" and "to" quaternions are very close + // ... so we can do a linear interpolation + scale0 = 1.0 - p_weight; + scale1 = p_weight; + } + // calculate final values + return Quaternion( + scale0 * x + scale1 * to1.x, + scale0 * y + scale1 * to1.y, + scale0 * z + scale1 * to1.z, + scale0 * w + scale1 * to1.w); +} + +Quaternion Quaternion::slerpni(const Quaternion &p_to, const real_t &p_weight) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_normalized(), Quaternion()); + ERR_FAIL_COND_V(!p_to.is_normalized(), Quaternion()); +#endif + const Quaternion &from = *this; + + real_t dot = from.dot(p_to); + + if (Math::abs(dot) > 0.9999) { + return from; + } + + real_t theta = Math::acos(dot), + sinT = 1.0 / Math::sin(theta), + newFactor = Math::sin(p_weight * theta) * sinT, + invFactor = Math::sin((1.0 - p_weight) * theta) * sinT; + + return Quaternion(invFactor * from.x + newFactor * p_to.x, + invFactor * from.y + newFactor * p_to.y, + invFactor * from.z + newFactor * p_to.z, + invFactor * from.w + newFactor * p_to.w); +} + +Quaternion Quaternion::cubic_slerp(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_normalized(), Quaternion()); + ERR_FAIL_COND_V(!p_b.is_normalized(), Quaternion()); +#endif + //the only way to do slerp :| + real_t t2 = (1.0 - p_weight) * p_weight * 2; + Quaternion sp = this->slerp(p_b, p_weight); + Quaternion sq = p_pre_a.slerpni(p_post_b, p_weight); + return sp.slerpni(sq, t2); +} + +Quaternion::operator String() const { + return String::num(x, 5) + ", " + String::num(y, 5) + ", " + String::num(z, 5) + ", " + String::num(w, 5); +} + +Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) { +#ifdef MATH_CHECKS + ERR_FAIL_COND(!p_axis.is_normalized()); +#endif + real_t d = p_axis.length(); + if (d == 0) { + x = 0; + y = 0; + z = 0; + w = 0; + } else { + real_t sin_angle = Math::sin(p_angle * 0.5); + real_t cos_angle = Math::cos(p_angle * 0.5); + real_t s = sin_angle / d; + x = p_axis.x * s; + y = p_axis.y * s; + z = p_axis.z * s; + w = cos_angle; + } +} + +// Euler constructor expects a vector containing the Euler angles in the format +// (ax, ay, az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// This implementation uses YXZ convention (Z is the first rotation). +Quaternion::Quaternion(const Vector3 &p_euler) { + real_t half_a1 = p_euler.y * 0.5; + real_t half_a2 = p_euler.x * 0.5; + real_t half_a3 = p_euler.z * 0.5; + + // R = Y(a1).X(a2).Z(a3) convention for Euler angles. + // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6) + // a3 is the angle of the first rotation, following the notation in this reference. + + real_t cos_a1 = Math::cos(half_a1); + real_t sin_a1 = Math::sin(half_a1); + real_t cos_a2 = Math::cos(half_a2); + real_t sin_a2 = Math::sin(half_a2); + real_t cos_a3 = Math::cos(half_a3); + real_t sin_a3 = Math::sin(half_a3); + + x = sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3; + y = sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3; + z = -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3; + w = sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3; +} + +} // namespace godot diff --git a/src/variant/rect2.cpp b/src/variant/rect2.cpp new file mode 100644 index 00000000..e737f616 --- /dev/null +++ b/src/variant/rect2.cpp @@ -0,0 +1,241 @@ +#include + +#include + +namespace godot { + +bool Rect2::is_equal_approx(const Rect2 &p_rect) const { + return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size); +} + +bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const { + real_t min = 0, max = 1; + int axis = 0; + real_t sign = 0; + + for (int i = 0; i < 2; i++) { + real_t seg_from = p_from[i]; + real_t seg_to = p_to[i]; + real_t box_begin = position[i]; + real_t box_end = box_begin + size[i]; + real_t cmin, cmax; + real_t csign; + + if (seg_from < seg_to) { + if (seg_from > box_end || seg_to < box_begin) { + return false; + } + real_t length = seg_to - seg_from; + cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0; + cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1; + csign = -1.0; + + } else { + if (seg_to > box_end || seg_from < box_begin) { + return false; + } + real_t length = seg_to - seg_from; + cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0; + cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1; + csign = 1.0; + } + + if (cmin > min) { + min = cmin; + axis = i; + sign = csign; + } + if (cmax < max) { + max = cmax; + } + if (max < min) { + return false; + } + } + + Vector2 rel = p_to - p_from; + + if (r_normal) { + Vector2 normal; + normal[axis] = sign; + *r_normal = normal; + } + + if (r_pos) { + *r_pos = p_from + rel * min; + } + + return true; +} + +bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const { + //SAT intersection between local and transformed rect2 + + Vector2 xf_points[4] = { + p_xform.xform(p_rect.position), + p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)), + p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)), + p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)), + }; + + real_t low_limit; + + //base rect2 first (faster) + + if (xf_points[0].y > position.y) { + goto next1; + } + if (xf_points[1].y > position.y) { + goto next1; + } + if (xf_points[2].y > position.y) { + goto next1; + } + if (xf_points[3].y > position.y) { + goto next1; + } + + return false; + +next1: + + low_limit = position.y + size.y; + + if (xf_points[0].y < low_limit) { + goto next2; + } + if (xf_points[1].y < low_limit) { + goto next2; + } + if (xf_points[2].y < low_limit) { + goto next2; + } + if (xf_points[3].y < low_limit) { + goto next2; + } + + return false; + +next2: + + if (xf_points[0].x > position.x) { + goto next3; + } + if (xf_points[1].x > position.x) { + goto next3; + } + if (xf_points[2].x > position.x) { + goto next3; + } + if (xf_points[3].x > position.x) { + goto next3; + } + + return false; + +next3: + + low_limit = position.x + size.x; + + if (xf_points[0].x < low_limit) { + goto next4; + } + if (xf_points[1].x < low_limit) { + goto next4; + } + if (xf_points[2].x < low_limit) { + goto next4; + } + if (xf_points[3].x < low_limit) { + goto next4; + } + + return false; + +next4: + + Vector2 xf_points2[4] = { + position, + Vector2(position.x + size.x, position.y), + Vector2(position.x, position.y + size.y), + Vector2(position.x + size.x, position.y + size.y), + }; + + real_t maxa = p_xform.elements[0].dot(xf_points2[0]); + real_t mina = maxa; + + real_t dp = p_xform.elements[0].dot(xf_points2[1]); + maxa = Math::max(dp, maxa); + mina = Math::min(dp, mina); + + dp = p_xform.elements[0].dot(xf_points2[2]); + maxa = Math::max(dp, maxa); + mina = Math::min(dp, mina); + + dp = p_xform.elements[0].dot(xf_points2[3]); + maxa = Math::max(dp, maxa); + mina = Math::min(dp, mina); + + real_t maxb = p_xform.elements[0].dot(xf_points[0]); + real_t minb = maxb; + + dp = p_xform.elements[0].dot(xf_points[1]); + maxb = Math::max(dp, maxb); + minb = Math::min(dp, minb); + + dp = p_xform.elements[0].dot(xf_points[2]); + maxb = Math::max(dp, maxb); + minb = Math::min(dp, minb); + + dp = p_xform.elements[0].dot(xf_points[3]); + maxb = Math::max(dp, maxb); + minb = Math::min(dp, minb); + + if (mina > maxb) { + return false; + } + if (minb > maxa) { + return false; + } + + maxa = p_xform.elements[1].dot(xf_points2[0]); + mina = maxa; + + dp = p_xform.elements[1].dot(xf_points2[1]); + maxa = Math::max(dp, maxa); + mina = Math::min(dp, mina); + + dp = p_xform.elements[1].dot(xf_points2[2]); + maxa = Math::max(dp, maxa); + mina = Math::min(dp, mina); + + dp = p_xform.elements[1].dot(xf_points2[3]); + maxa = Math::max(dp, maxa); + mina = Math::min(dp, mina); + + maxb = p_xform.elements[1].dot(xf_points[0]); + minb = maxb; + + dp = p_xform.elements[1].dot(xf_points[1]); + maxb = Math::max(dp, maxb); + minb = Math::min(dp, minb); + + dp = p_xform.elements[1].dot(xf_points[2]); + maxb = Math::max(dp, maxb); + minb = Math::min(dp, minb); + + dp = p_xform.elements[1].dot(xf_points[3]); + maxb = Math::max(dp, maxb); + minb = Math::min(dp, minb); + + if (mina > maxb) { + return false; + } + if (minb > maxa) { + return false; + } + + return true; +} + +} // namespace godot diff --git a/src/variant/rect2i.cpp b/src/variant/rect2i.cpp new file mode 100644 index 00000000..ee9b723d --- /dev/null +++ b/src/variant/rect2i.cpp @@ -0,0 +1,3 @@ +#include + +// No implementation left. This is here to add the header as a compiled unit. diff --git a/src/variant/transform2d.cpp b/src/variant/transform2d.cpp new file mode 100644 index 00000000..4a4a6d53 --- /dev/null +++ b/src/variant/transform2d.cpp @@ -0,0 +1,248 @@ +#include + +namespace godot { + +void Transform2D::invert() { + // FIXME: this function assumes the basis is a rotation matrix, with no scaling. + // Transform2D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that. + SWAP(elements[0][1], elements[1][0]); + elements[2] = basis_xform(-elements[2]); +} + +Transform2D Transform2D::inverse() const { + Transform2D inv = *this; + inv.invert(); + return inv; +} + +void Transform2D::affine_invert() { + real_t det = basis_determinant(); +#ifdef MATH_CHECKS + ERR_FAIL_COND(det == 0); +#endif + real_t idet = 1.0 / det; + + SWAP(elements[0][0], elements[1][1]); + elements[0] *= Vector2(idet, -idet); + elements[1] *= Vector2(-idet, idet); + + elements[2] = basis_xform(-elements[2]); +} + +Transform2D Transform2D::affine_inverse() const { + Transform2D inv = *this; + inv.affine_invert(); + return inv; +} + +void Transform2D::rotate(real_t p_phi) { + *this = Transform2D(p_phi, Vector2()) * (*this); +} + +real_t Transform2D::get_skew() const { + real_t det = basis_determinant(); + return Math::acos(elements[0].normalized().dot(Math::sign(det) * elements[1].normalized())) - Math_PI * 0.5; +} + +void Transform2D::set_skew(float p_angle) { + real_t det = basis_determinant(); + elements[1] = Math::sign(det) * elements[0].rotated((Math_PI * 0.5 + p_angle)).normalized() * elements[1].length(); +} + +real_t Transform2D::get_rotation() const { + return Math::atan2(elements[0].y, elements[0].x); +} + +void Transform2D::set_rotation(real_t p_rot) { + Size2 scale = get_scale(); + real_t cr = Math::cos(p_rot); + real_t sr = Math::sin(p_rot); + elements[0][0] = cr; + elements[0][1] = sr; + elements[1][0] = -sr; + elements[1][1] = cr; + set_scale(scale); +} + +Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) { + real_t cr = Math::cos(p_rot); + real_t sr = Math::sin(p_rot); + elements[0][0] = cr; + elements[0][1] = sr; + elements[1][0] = -sr; + elements[1][1] = cr; + elements[2] = p_pos; +} + +Size2 Transform2D::get_scale() const { + real_t det_sign = Math::sign(basis_determinant()); + return Size2(elements[0].length(), det_sign * elements[1].length()); +} + +void Transform2D::set_scale(const Size2 &p_scale) { + elements[0].normalize(); + elements[1].normalize(); + elements[0] *= p_scale.x; + elements[1] *= p_scale.y; +} + +void Transform2D::scale(const Size2 &p_scale) { + scale_basis(p_scale); + elements[2] *= p_scale; +} + +void Transform2D::scale_basis(const Size2 &p_scale) { + elements[0][0] *= p_scale.x; + elements[0][1] *= p_scale.y; + elements[1][0] *= p_scale.x; + elements[1][1] *= p_scale.y; +} + +void Transform2D::translate(real_t p_tx, real_t p_ty) { + translate(Vector2(p_tx, p_ty)); +} + +void Transform2D::translate(const Vector2 &p_translation) { + elements[2] += basis_xform(p_translation); +} + +void Transform2D::orthonormalize() { + // Gram-Schmidt Process + + Vector2 x = elements[0]; + Vector2 y = elements[1]; + + x.normalize(); + y = (y - x * (x.dot(y))); + y.normalize(); + + elements[0] = x; + elements[1] = y; +} + +Transform2D Transform2D::orthonormalized() const { + Transform2D on = *this; + on.orthonormalize(); + return on; +} + +bool Transform2D::is_equal_approx(const Transform2D &p_transform) const { + return elements[0].is_equal_approx(p_transform.elements[0]) && elements[1].is_equal_approx(p_transform.elements[1]) && elements[2].is_equal_approx(p_transform.elements[2]); +} + +bool Transform2D::operator==(const Transform2D &p_transform) const { + for (int i = 0; i < 3; i++) { + if (elements[i] != p_transform.elements[i]) { + return false; + } + } + + return true; +} + +bool Transform2D::operator!=(const Transform2D &p_transform) const { + for (int i = 0; i < 3; i++) { + if (elements[i] != p_transform.elements[i]) { + return true; + } + } + + return false; +} + +void Transform2D::operator*=(const Transform2D &p_transform) { + elements[2] = xform(p_transform.elements[2]); + + real_t x0, x1, y0, y1; + + x0 = tdotx(p_transform.elements[0]); + x1 = tdoty(p_transform.elements[0]); + y0 = tdotx(p_transform.elements[1]); + y1 = tdoty(p_transform.elements[1]); + + elements[0][0] = x0; + elements[0][1] = x1; + elements[1][0] = y0; + elements[1][1] = y1; +} + +Transform2D Transform2D::operator*(const Transform2D &p_transform) const { + Transform2D t = *this; + t *= p_transform; + return t; +} + +Transform2D Transform2D::scaled(const Size2 &p_scale) const { + Transform2D copy = *this; + copy.scale(p_scale); + return copy; +} + +Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const { + Transform2D copy = *this; + copy.scale_basis(p_scale); + return copy; +} + +Transform2D Transform2D::untranslated() const { + Transform2D copy = *this; + copy.elements[2] = Vector2(); + return copy; +} + +Transform2D Transform2D::translated(const Vector2 &p_offset) const { + Transform2D copy = *this; + copy.translate(p_offset); + return copy; +} + +Transform2D Transform2D::rotated(real_t p_phi) const { + Transform2D copy = *this; + copy.rotate(p_phi); + return copy; +} + +real_t Transform2D::basis_determinant() const { + return elements[0].x * elements[1].y - elements[0].y * elements[1].x; +} + +Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_c) const { + //extract parameters + Vector2 p1 = get_origin(); + Vector2 p2 = p_transform.get_origin(); + + real_t r1 = get_rotation(); + real_t r2 = p_transform.get_rotation(); + + Size2 s1 = get_scale(); + Size2 s2 = p_transform.get_scale(); + + //slerp rotation + Vector2 v1(Math::cos(r1), Math::sin(r1)); + Vector2 v2(Math::cos(r2), Math::sin(r2)); + + real_t dot = v1.dot(v2); + + dot = Math::clamp(dot, (real_t)-1.0, (real_t)1.0); + + Vector2 v; + + if (dot > 0.9995) { + v = v1.lerp(v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues + } else { + real_t angle = p_c * Math::acos(dot); + Vector2 v3 = (v2 - v1 * dot).normalized(); + v = v1 * Math::cos(angle) + v3 * Math::sin(angle); + } + + //construct matrix + Transform2D res(Math::atan2(v.y, v.x), p1.lerp(p2, p_c)); + res.scale_basis(s1.lerp(s2, p_c)); + return res; +} + +Transform2D::operator String() const { + return elements[0].operator String() + ", " + elements[1].operator String() + ", " + elements[2].operator String(); +} + +} // namespace godot diff --git a/src/variant/transform3d.cpp b/src/variant/transform3d.cpp new file mode 100644 index 00000000..10b927ce --- /dev/null +++ b/src/variant/transform3d.cpp @@ -0,0 +1,185 @@ +#include + +#include + +namespace godot { + +void Transform3D::affine_invert() { + basis.invert(); + origin = basis.xform(-origin); +} + +Transform3D Transform3D::affine_inverse() const { + Transform3D ret = *this; + ret.affine_invert(); + return ret; +} + +void Transform3D::invert() { + basis.transpose(); + origin = basis.xform(-origin); +} + +Transform3D Transform3D::inverse() const { + // FIXME: this function assumes the basis is a rotation matrix, with no scaling. + // Transform3D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that. + Transform3D ret = *this; + ret.invert(); + return ret; +} + +void Transform3D::rotate(const Vector3 &p_axis, real_t p_phi) { + *this = rotated(p_axis, p_phi); +} + +Transform3D Transform3D::rotated(const Vector3 &p_axis, real_t p_phi) const { + return Transform3D(Basis(p_axis, p_phi), Vector3()) * (*this); +} + +void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_phi) { + basis.rotate(p_axis, p_phi); +} + +Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up) const { + Transform3D t = *this; + t.set_look_at(origin, p_target, p_up); + return t; +} + +void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) { +#ifdef MATH_CHECKS + ERR_FAIL_COND(p_eye == p_target); + ERR_FAIL_COND(p_up.length() == 0); +#endif + // RefCounted: MESA source code + Vector3 v_x, v_y, v_z; + + /* Make rotation matrix */ + + /* Z vector */ + v_z = p_eye - p_target; + + v_z.normalize(); + + v_y = p_up; + + v_x = v_y.cross(v_z); +#ifdef MATH_CHECKS + ERR_FAIL_COND(v_x.length() == 0); +#endif + + /* Recompute Y = Z cross X */ + v_y = v_z.cross(v_x); + + v_x.normalize(); + v_y.normalize(); + + basis.set(v_x, v_y, v_z); + + origin = p_eye; +} + +Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const { + /* not sure if very "efficient" but good enough? */ + + Vector3 src_scale = basis.get_scale(); + Quaternion src_rot = basis.get_rotation_quat(); + Vector3 src_loc = origin; + + Vector3 dst_scale = p_transform.basis.get_scale(); + Quaternion dst_rot = p_transform.basis.get_rotation_quat(); + Vector3 dst_loc = p_transform.origin; + + Transform3D interp; + interp.basis.set_quat_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c)); + interp.origin = src_loc.lerp(dst_loc, p_c); + + return interp; +} + +void Transform3D::scale(const Vector3 &p_scale) { + basis.scale(p_scale); + origin *= p_scale; +} + +Transform3D Transform3D::scaled(const Vector3 &p_scale) const { + Transform3D t = *this; + t.scale(p_scale); + return t; +} + +void Transform3D::scale_basis(const Vector3 &p_scale) { + basis.scale(p_scale); +} + +void Transform3D::translate(real_t p_tx, real_t p_ty, real_t p_tz) { + translate(Vector3(p_tx, p_ty, p_tz)); +} + +void Transform3D::translate(const Vector3 &p_translation) { + for (int i = 0; i < 3; i++) { + origin[i] += basis[i].dot(p_translation); + } +} + +Transform3D Transform3D::translated(const Vector3 &p_translation) const { + Transform3D t = *this; + t.translate(p_translation); + return t; +} + +void Transform3D::orthonormalize() { + basis.orthonormalize(); +} + +Transform3D Transform3D::orthonormalized() const { + Transform3D _copy = *this; + _copy.orthonormalize(); + return _copy; +} + +bool Transform3D::is_equal_approx(const Transform3D &p_transform) const { + return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin); +} + +bool Transform3D::operator==(const Transform3D &p_transform) const { + return (basis == p_transform.basis && origin == p_transform.origin); +} + +bool Transform3D::operator!=(const Transform3D &p_transform) const { + return (basis != p_transform.basis || origin != p_transform.origin); +} + +void Transform3D::operator*=(const Transform3D &p_transform) { + origin = xform(p_transform.origin); + basis *= p_transform.basis; +} + +Transform3D Transform3D::operator*(const Transform3D &p_transform) const { + Transform3D t = *this; + t *= p_transform; + return t; +} + +Transform3D::operator String() const { + return basis.operator String() + " - " + origin.operator String(); +} + +Transform3D::Transform3D(const Basis &p_basis, const Vector3 &p_origin) : + basis(p_basis), + origin(p_origin) { +} + +Transform3D::Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) : + origin(p_origin) { + basis.set_axis(0, p_x); + basis.set_axis(1, p_y); + basis.set_axis(2, p_z); +} + +Transform3D::Transform3D(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t ox, real_t oy, real_t oz) { + basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz); + origin = Vector3(ox, oy, oz); +} + +} // namespace godot diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index fa1d54cf..dc8ce6e2 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -50,19 +50,6 @@ void Variant::init_bindings() { } String::init_bindings(); - Vector2::init_bindings(); - Vector2i::init_bindings(); - Rect2::init_bindings(); - Rect2i::init_bindings(); - Vector3::init_bindings(); - Vector3i::init_bindings(); - Transform2D::init_bindings(); - Plane::init_bindings(); - Quaternion::init_bindings(); - AABB::init_bindings(); - Basis::init_bindings(); - Transform3D::init_bindings(); - Color::init_bindings(); StringName::init_bindings(); NodePath::init_bindings(); RID::init_bindings(); diff --git a/src/variant/vector2.cpp b/src/variant/vector2.cpp new file mode 100644 index 00000000..a91bdde3 --- /dev/null +++ b/src/variant/vector2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + +namespace godot { + +Vector2::operator String() const { + return String::num(x, 5) + ", " + String::num(y, 5); +} + +real_t Vector2::angle() const { + return Math::atan2(y, x); +} + +real_t Vector2::length() const { + return Math::sqrt(x * x + y * y); +} + +real_t Vector2::length_squared() const { + return x * x + y * y; +} + +void Vector2::normalize() { + real_t l = x * x + y * y; + if (l != 0) { + l = Math::sqrt(l); + x /= l; + y /= l; + } +} + +Vector2 Vector2::normalized() const { + Vector2 v = *this; + v.normalize(); + return v; +} + +bool Vector2::is_normalized() const { + // use length_squared() instead of length() to avoid sqrt(), makes it more stringent. + return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); +} + +real_t Vector2::distance_to(const Vector2 &p_vector2) const { + return Math::sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y)); +} + +real_t Vector2::distance_squared_to(const Vector2 &p_vector2) const { + return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y); +} + +real_t Vector2::angle_to(const Vector2 &p_vector2) const { + return Math::atan2(cross(p_vector2), dot(p_vector2)); +} + +real_t Vector2::angle_to_point(const Vector2 &p_vector2) const { + return Math::atan2(y - p_vector2.y, x - p_vector2.x); +} + +real_t Vector2::dot(const Vector2 &p_other) const { + return x * p_other.x + y * p_other.y; +} + +real_t Vector2::cross(const Vector2 &p_other) const { + return x * p_other.y - y * p_other.x; +} + +Vector2 Vector2::sign() const { + return Vector2(Math::sign(x), Math::sign(y)); +} + +Vector2 Vector2::floor() const { + return Vector2(Math::floor(x), Math::floor(y)); +} + +Vector2 Vector2::ceil() const { + return Vector2(Math::ceil(x), Math::ceil(y)); +} + +Vector2 Vector2::round() const { + return Vector2(Math::round(x), Math::round(y)); +} + +Vector2 Vector2::rotated(real_t p_by) const { + real_t sine = Math::sin(p_by); + real_t cosi = Math::cos(p_by); + return Vector2( + x * cosi - y * sine, + x * sine + y * cosi); +} + +Vector2 Vector2::posmod(const real_t p_mod) const { + return Vector2(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod)); +} + +Vector2 Vector2::posmodv(const Vector2 &p_modv) const { + return Vector2(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y)); +} + +Vector2 Vector2::project(const Vector2 &p_to) const { + return p_to * (dot(p_to) / p_to.length_squared()); +} + +Vector2 Vector2::snapped(const Vector2 &p_step) const { + return Vector2( + Math::snapped(x, p_step.x), + Math::snapped(y, p_step.y)); +} + +Vector2 Vector2::clamped(real_t p_len) const { + real_t l = length(); + Vector2 v = *this; + if (l > 0 && p_len < l) { + v /= l; + v *= p_len; + } + + return v; +} + +Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const { + Vector2 p0 = p_pre_a; + Vector2 p1 = *this; + Vector2 p2 = p_b; + Vector2 p3 = p_post_b; + + real_t t = p_weight; + real_t t2 = t * t; + real_t t3 = t2 * t; + + Vector2 out; + out = 0.5 * ((p1 * 2.0) + + (-p0 + p2) * t + + (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 + + (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3); + return out; +} + +Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const { + Vector2 v = *this; + Vector2 vd = p_to - v; + real_t len = vd.length(); + return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta; +} + +// slide returns the component of the vector along the given plane, specified by its normal vector. +Vector2 Vector2::slide(const Vector2 &p_normal) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector2()); +#endif + return *this - p_normal * this->dot(p_normal); +} + +Vector2 Vector2::bounce(const Vector2 &p_normal) const { + return -reflect(p_normal); +} + +Vector2 Vector2::reflect(const Vector2 &p_normal) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector2()); +#endif + return 2.0 * p_normal * this->dot(p_normal) - *this; +} + +bool Vector2::is_equal_approx(const Vector2 &p_v) const { + return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y); +} + +} // namespace godot diff --git a/src/variant/vector2i.cpp b/src/variant/vector2i.cpp new file mode 100644 index 00000000..ccd524a8 --- /dev/null +++ b/src/variant/vector2i.cpp @@ -0,0 +1,80 @@ +#include +#include +#include + +namespace godot { + +Vector2i::operator String() const { + return String::num(x, 0) + ", " + String::num(y, 0); +} + +Vector2i Vector2i::operator+(const Vector2i &p_v) const { + return Vector2i(x + p_v.x, y + p_v.y); +} + +void Vector2i::operator+=(const Vector2i &p_v) { + x += p_v.x; + y += p_v.y; +} + +Vector2i Vector2i::operator-(const Vector2i &p_v) const { + return Vector2i(x - p_v.x, y - p_v.y); +} + +void Vector2i::operator-=(const Vector2i &p_v) { + x -= p_v.x; + y -= p_v.y; +} + +Vector2i Vector2i::operator*(const Vector2i &p_v1) const { + return Vector2i(x * p_v1.x, y * p_v1.y); +} + +Vector2i Vector2i::operator*(const int32_t &rvalue) const { + return Vector2i(x * rvalue, y * rvalue); +} + +void Vector2i::operator*=(const int32_t &rvalue) { + x *= rvalue; + y *= rvalue; +} + +Vector2i Vector2i::operator/(const Vector2i &p_v1) const { + return Vector2i(x / p_v1.x, y / p_v1.y); +} + +Vector2i Vector2i::operator/(const int32_t &rvalue) const { + return Vector2i(x / rvalue, y / rvalue); +} + +void Vector2i::operator/=(const int32_t &rvalue) { + x /= rvalue; + y /= rvalue; +} + +Vector2i Vector2i::operator%(const Vector2i &p_v1) const { + return Vector2i(x % p_v1.x, y % p_v1.y); +} + +Vector2i Vector2i::operator%(const int32_t &rvalue) const { + return Vector2i(x % rvalue, y % rvalue); +} + +void Vector2i::operator%=(const int32_t &rvalue) { + x %= rvalue; + y %= rvalue; +} + +Vector2i Vector2i::operator-() const { + return Vector2i(-x, -y); +} + +bool Vector2i::operator==(const Vector2i &p_vec2) const { + return x == p_vec2.x && y == p_vec2.y; +} + +bool Vector2i::operator!=(const Vector2i &p_vec2) const { + return x != p_vec2.x || y != p_vec2.y; +} + +} // namespace godot diff --git a/src/variant/vector3.cpp b/src/variant/vector3.cpp new file mode 100644 index 00000000..b835e03f --- /dev/null +++ b/src/variant/vector3.cpp @@ -0,0 +1,94 @@ +#include +#include +#include + +namespace godot { + +void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) { + *this = Basis(p_axis, p_phi).xform(*this); +} + +Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const { + Vector3 r = *this; + r.rotate(p_axis, p_phi); + return r; +} + +void Vector3::set_axis(int p_axis, real_t p_value) { + ERR_FAIL_INDEX(p_axis, 3); + coord[p_axis] = p_value; +} + +real_t Vector3::get_axis(int p_axis) const { + ERR_FAIL_INDEX_V(p_axis, 3, 0); + return operator[](p_axis); +} + +int Vector3::min_axis() const { + return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2); +} + +int Vector3::max_axis() const { + return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0); +} + +void Vector3::snap(Vector3 p_step) { + x = Math::snapped(x, p_step.x); + y = Math::snapped(y, p_step.y); + z = Math::snapped(z, p_step.z); +} + +Vector3 Vector3::snapped(Vector3 p_step) const { + Vector3 v = *this; + v.snap(p_step); + return v; +} + +Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const { + Vector3 p0 = p_pre_a; + Vector3 p1 = *this; + Vector3 p2 = p_b; + Vector3 p3 = p_post_b; + + real_t t = p_weight; + real_t t2 = t * t; + real_t t3 = t2 * t; + + Vector3 out; + out = 0.5 * ((p1 * 2.0) + + (-p0 + p2) * t + + (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 + + (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3); + return out; +} + +Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const { + Vector3 v = *this; + Vector3 vd = p_to - v; + real_t len = vd.length(); + return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta; +} + +Basis Vector3::outer(const Vector3 &p_b) const { + Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z); + Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z); + Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z); + + return Basis(row0, row1, row2); +} + +Basis Vector3::to_diagonal_matrix() const { + return Basis(x, 0, 0, + 0, y, 0, + 0, 0, z); +} + +bool Vector3::is_equal_approx(const Vector3 &p_v) const { + return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z); +} + +Vector3::operator String() const { + return (String::num(x, 5) + ", " + String::num(y, 5) + ", " + String::num(z, 5)); +} + +} // namespace godot diff --git a/src/variant/vector3i.cpp b/src/variant/vector3i.cpp new file mode 100644 index 00000000..d80c8ea4 --- /dev/null +++ b/src/variant/vector3i.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +namespace godot { + +void Vector3i::set_axis(int p_axis, int32_t p_value) { + ERR_FAIL_INDEX(p_axis, 3); + coord[p_axis] = p_value; +} + +int32_t Vector3i::get_axis(int p_axis) const { + ERR_FAIL_INDEX_V(p_axis, 3, 0); + return operator[](p_axis); +} + +int Vector3i::min_axis() const { + return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2); +} + +int Vector3i::max_axis() const { + return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0); +} + +Vector3i::operator String() const { + return (String::num(x, 0) + ", " + String::num(y, 0) + ", " + String::num(z, 5)); +} + +} // namespace godot