diff options
author | Haoyu Qiu <timothyqiu32@gmail.com> | 2022-08-11 16:12:27 +0800 |
---|---|---|
committer | Haoyu Qiu <timothyqiu32@gmail.com> | 2022-10-08 13:25:08 +0800 |
commit | 5da515773d8edec988b7523ea97cdfd54c3fd16c (patch) | |
tree | 1107edc8f9bcff011b9bf789cf0e54bc9d46c28d /core | |
parent | 18177828ad97286ca88cbdcfb763c967858d051b (diff) |
Add `is_finite` method for checking built-in types
Diffstat (limited to 'core')
-rw-r--r-- | core/math/aabb.cpp | 4 | ||||
-rw-r--r-- | core/math/aabb.h | 1 | ||||
-rw-r--r-- | core/math/basis.cpp | 4 | ||||
-rw-r--r-- | core/math/basis.h | 1 | ||||
-rw-r--r-- | core/math/math_funcs.h | 3 | ||||
-rw-r--r-- | core/math/plane.cpp | 4 | ||||
-rw-r--r-- | core/math/plane.h | 1 | ||||
-rw-r--r-- | core/math/quaternion.cpp | 4 | ||||
-rw-r--r-- | core/math/quaternion.h | 1 | ||||
-rw-r--r-- | core/math/rect2.cpp | 4 | ||||
-rw-r--r-- | core/math/rect2.h | 1 | ||||
-rw-r--r-- | core/math/transform_2d.cpp | 4 | ||||
-rw-r--r-- | core/math/transform_2d.h | 1 | ||||
-rw-r--r-- | core/math/transform_3d.cpp | 4 | ||||
-rw-r--r-- | core/math/transform_3d.h | 1 | ||||
-rw-r--r-- | core/math/vector2.cpp | 4 | ||||
-rw-r--r-- | core/math/vector2.h | 1 | ||||
-rw-r--r-- | core/math/vector3.cpp | 4 | ||||
-rw-r--r-- | core/math/vector3.h | 1 | ||||
-rw-r--r-- | core/math/vector4.cpp | 4 | ||||
-rw-r--r-- | core/math/vector4.h | 1 | ||||
-rw-r--r-- | core/string/ustring.cpp | 12 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 10 | ||||
-rw-r--r-- | core/variant/variant_utility.cpp | 5 |
24 files changed, 74 insertions, 6 deletions
diff --git a/core/math/aabb.cpp b/core/math/aabb.cpp index 026f179445..fcf245d2ad 100644 --- a/core/math/aabb.cpp +++ b/core/math/aabb.cpp @@ -76,6 +76,10 @@ 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); } +bool AABB::is_finite() const { + return position.is_finite() && size.is_finite(); +} + AABB AABB::intersection(const AABB &p_aabb) const { #ifdef MATH_CHECKS if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) { diff --git a/core/math/aabb.h b/core/math/aabb.h index b9f777c6cf..9d5837ad37 100644 --- a/core/math/aabb.h +++ b/core/math/aabb.h @@ -63,6 +63,7 @@ struct _NO_DISCARD_ AABB { bool operator!=(const AABB &p_rval) const; bool is_equal_approx(const AABB &p_aabb) const; + bool is_finite() const; _FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap _FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap _FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 845686f339..9b8188eed8 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -691,6 +691,10 @@ bool Basis::is_equal_approx(const Basis &p_basis) const { return rows[0].is_equal_approx(p_basis.rows[0]) && rows[1].is_equal_approx(p_basis.rows[1]) && rows[2].is_equal_approx(p_basis.rows[2]); } +bool Basis::is_finite() const { + return rows[0].is_finite() && rows[1].is_finite() && rows[2].is_finite(); +} + bool Basis::operator==(const Basis &p_matrix) const { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { diff --git a/core/math/basis.h b/core/math/basis.h index cc2924f5ff..69bef5a7be 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -134,6 +134,7 @@ struct _NO_DISCARD_ Basis { } bool is_equal_approx(const Basis &p_basis) const; + bool is_finite() const; bool operator==(const Basis &p_matrix) const; bool operator!=(const Basis &p_matrix) const; diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 7fa674a23d..0af529ad98 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -184,6 +184,9 @@ public: #endif } + static _ALWAYS_INLINE_ bool is_finite(double p_val) { return isfinite(p_val); } + static _ALWAYS_INLINE_ bool is_finite(float p_val) { return isfinite(p_val); } + static _ALWAYS_INLINE_ double abs(double g) { return absd(g); } static _ALWAYS_INLINE_ float abs(float g) { return absf(g); } static _ALWAYS_INLINE_ int abs(int g) { return g > 0 ? g : -g; } diff --git a/core/math/plane.cpp b/core/math/plane.cpp index 3b2eab4ae2..a5d2fe5628 100644 --- a/core/math/plane.cpp +++ b/core/math/plane.cpp @@ -176,6 +176,10 @@ 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); } +bool Plane::is_finite() const { + return normal.is_finite() && Math::is_finite(d); +} + Plane::operator String() const { return "[N: " + normal.operator String() + ", D: " + String::num_real(d, false) + "]"; } diff --git a/core/math/plane.h b/core/math/plane.h index 73babfa496..77da59fb27 100644 --- a/core/math/plane.h +++ b/core/math/plane.h @@ -74,6 +74,7 @@ struct _NO_DISCARD_ Plane { 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; + bool is_finite() const; _FORCE_INLINE_ bool operator==(const Plane &p_plane) const; _FORCE_INLINE_ bool operator!=(const Plane &p_plane) const; diff --git a/core/math/quaternion.cpp b/core/math/quaternion.cpp index 4a8d29e402..6a5f29f3d8 100644 --- a/core/math/quaternion.cpp +++ b/core/math/quaternion.cpp @@ -79,6 +79,10 @@ bool Quaternion::is_equal_approx(const Quaternion &p_quaternion) const { return Math::is_equal_approx(x, p_quaternion.x) && Math::is_equal_approx(y, p_quaternion.y) && Math::is_equal_approx(z, p_quaternion.z) && Math::is_equal_approx(w, p_quaternion.w); } +bool Quaternion::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w); +} + real_t Quaternion::length() const { return Math::sqrt(length_squared()); } diff --git a/core/math/quaternion.h b/core/math/quaternion.h index 178cfaca70..7aa400aa8c 100644 --- a/core/math/quaternion.h +++ b/core/math/quaternion.h @@ -55,6 +55,7 @@ struct _NO_DISCARD_ Quaternion { } _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Quaternion &p_quaternion) const; + bool is_finite() const; real_t length() const; void normalize(); Quaternion normalized() const; diff --git a/core/math/rect2.cpp b/core/math/rect2.cpp index 9e78ead816..facf4eb3c4 100644 --- a/core/math/rect2.cpp +++ b/core/math/rect2.cpp @@ -38,6 +38,10 @@ 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::is_finite() const { + return position.is_finite() && size.is_finite(); +} + bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const { #ifdef MATH_CHECKS if (unlikely(size.x < 0 || size.y < 0)) { diff --git a/core/math/rect2.h b/core/math/rect2.h index 50dd2dc1df..9863405d8e 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -207,6 +207,7 @@ struct _NO_DISCARD_ Rect2 { } bool is_equal_approx(const Rect2 &p_rect) const; + bool is_finite() 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; } diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index 2bfefe979f..548a82d254 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -168,6 +168,10 @@ bool Transform2D::is_equal_approx(const Transform2D &p_transform) const { return columns[0].is_equal_approx(p_transform.columns[0]) && columns[1].is_equal_approx(p_transform.columns[1]) && columns[2].is_equal_approx(p_transform.columns[2]); } +bool Transform2D::is_finite() const { + return columns[0].is_finite() && columns[1].is_finite() && columns[2].is_finite(); +} + Transform2D Transform2D::looking_at(const Vector2 &p_target) const { Transform2D return_trans = Transform2D(get_rotation(), get_origin()); Vector2 target_position = affine_inverse().xform(p_target); diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index f23f32867a..2b11f36535 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -98,6 +98,7 @@ struct _NO_DISCARD_ Transform2D { void orthonormalize(); Transform2D orthonormalized() const; bool is_equal_approx(const Transform2D &p_transform) const; + bool is_finite() const; Transform2D looking_at(const Vector2 &p_target) const; diff --git a/core/math/transform_3d.cpp b/core/math/transform_3d.cpp index 6741ef4034..3285cbd664 100644 --- a/core/math/transform_3d.cpp +++ b/core/math/transform_3d.cpp @@ -174,6 +174,10 @@ 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::is_finite() const { + return basis.is_finite() && origin.is_finite(); +} + bool Transform3D::operator==(const Transform3D &p_transform) const { return (basis == p_transform.basis && origin == p_transform.origin); } diff --git a/core/math/transform_3d.h b/core/math/transform_3d.h index 44d6d826f3..cb347aa1c1 100644 --- a/core/math/transform_3d.h +++ b/core/math/transform_3d.h @@ -75,6 +75,7 @@ struct _NO_DISCARD_ Transform3D { void orthogonalize(); Transform3D orthogonalized() const; bool is_equal_approx(const Transform3D &p_transform) const; + bool is_finite() const; bool operator==(const Transform3D &p_transform) const; bool operator!=(const Transform3D &p_transform) const; diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index 56dbba393a..5366587126 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -186,6 +186,10 @@ bool Vector2::is_zero_approx() const { return Math::is_zero_approx(x) && Math::is_zero_approx(y); } +bool Vector2::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y); +} + Vector2::operator String() const { return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")"; } diff --git a/core/math/vector2.h b/core/math/vector2.h index 75364f72f0..5775d8e735 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -121,6 +121,7 @@ struct _NO_DISCARD_ Vector2 { bool is_equal_approx(const Vector2 &p_v) const; bool is_zero_approx() const; + bool is_finite() const; Vector2 operator+(const Vector2 &p_v) const; void operator+=(const Vector2 &p_v); diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index 55ba509144..b106200c4a 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -139,6 +139,10 @@ bool Vector3::is_zero_approx() const { return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z); } +bool Vector3::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z); +} + Vector3::operator String() const { return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")"; } diff --git a/core/math/vector3.h b/core/math/vector3.h index 62e810fb4d..19771eb312 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -136,6 +136,7 @@ struct _NO_DISCARD_ Vector3 { bool is_equal_approx(const Vector3 &p_v) const; bool is_zero_approx() const; + bool is_finite() const; /* Operators */ diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index 9fd980aaff..3b189f7ed4 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -64,6 +64,10 @@ bool Vector4::is_zero_approx() const { return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z) && Math::is_zero_approx(w); } +bool Vector4::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w); +} + real_t Vector4::length() const { return Math::sqrt(length_squared()); } diff --git a/core/math/vector4.h b/core/math/vector4.h index ac7b6c3aee..7c4bdc1788 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -71,6 +71,7 @@ struct _NO_DISCARD_ Vector4 { _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Vector4 &p_vec4) const; bool is_zero_approx() const; + bool is_finite() const; real_t length() const; void normalize(); Vector4 normalized() const; diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 6218c21cde..e5e7ff357c 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -4651,10 +4651,10 @@ String String::sprintf(const Array &values, bool *error) const { double value = values[value_index]; bool is_negative = (value < 0); String str = String::num(ABS(value), min_decimals); - bool not_numeric = isinf(value) || isnan(value); + const bool is_finite = Math::is_finite(value); // Pad decimals out. - if (!not_numeric) { + if (is_finite) { str = str.pad_decimals(min_decimals); } @@ -4662,7 +4662,7 @@ String String::sprintf(const Array &values, bool *error) const { // Padding. Leave room for sign later if required. int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars; - String pad_char = (pad_with_zeros && !not_numeric) ? String("0") : String(" "); // Never pad NaN or inf with zeros + String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros if (left_justified) { str = str.rpad(pad_chars_count, pad_char); } else { @@ -4713,10 +4713,10 @@ String String::sprintf(const Array &values, bool *error) const { for (int i = 0; i < count; i++) { double val = vec[i]; String number_str = String::num(ABS(val), min_decimals); - bool not_numeric = isinf(val) || isnan(val); + const bool is_finite = Math::is_finite(val); // Pad decimals out. - if (!not_numeric) { + if (is_finite) { number_str = number_str.pad_decimals(min_decimals); } @@ -4724,7 +4724,7 @@ String String::sprintf(const Array &values, bool *error) const { // Padding. Leave room for sign later if required. int pad_chars_count = val < 0 ? min_chars - 1 : min_chars; - String pad_char = (pad_with_zeros && !not_numeric) ? String("0") : String(" "); // Never pad NaN or inf with zeros + String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros if (left_justified) { number_str = number_str.rpad(pad_chars_count, pad_char); } else { diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 1831f7b72a..900e3d8e77 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1606,6 +1606,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector2, is_normalized, sarray(), varray()); bind_method(Vector2, is_equal_approx, sarray("to"), varray()); bind_method(Vector2, is_zero_approx, sarray(), varray()); + bind_method(Vector2, is_finite, sarray(), varray()); bind_method(Vector2, posmod, sarray("mod"), varray()); bind_method(Vector2, posmodv, sarray("modv"), varray()); bind_method(Vector2, project, sarray("b"), varray()); @@ -1653,6 +1654,7 @@ static void _register_variant_builtin_methods() { bind_method(Rect2, has_area, sarray(), varray()); bind_method(Rect2, has_point, sarray("point"), varray()); bind_method(Rect2, is_equal_approx, sarray("rect"), varray()); + bind_method(Rect2, is_finite, sarray(), varray()); bind_method(Rect2, intersects, sarray("b", "include_borders"), varray(false)); bind_method(Rect2, encloses, sarray("b"), varray()); bind_method(Rect2, intersection, sarray("b"), varray()); @@ -1695,6 +1697,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector3, is_normalized, sarray(), varray()); bind_method(Vector3, is_equal_approx, sarray("to"), varray()); bind_method(Vector3, is_zero_approx, sarray(), varray()); + bind_method(Vector3, is_finite, sarray(), varray()); bind_method(Vector3, inverse, sarray(), varray()); bind_method(Vector3, clamp, sarray("min", "max"), varray()); bind_method(Vector3, snapped, sarray("step"), varray()); @@ -1759,6 +1762,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector4, inverse, sarray(), varray()); bind_method(Vector4, is_equal_approx, sarray("with"), varray()); bind_method(Vector4, is_zero_approx, sarray(), varray()); + bind_method(Vector4, is_finite, sarray(), varray()); /* Vector4i */ @@ -1775,6 +1779,7 @@ static void _register_variant_builtin_methods() { bind_method(Plane, normalized, sarray(), varray()); bind_method(Plane, center, sarray(), varray()); bind_method(Plane, is_equal_approx, sarray("to_plane"), varray()); + bind_method(Plane, is_finite, sarray(), varray()); bind_method(Plane, is_point_over, sarray("point"), varray()); bind_method(Plane, distance_to, sarray("point"), varray()); bind_method(Plane, has_point, sarray("point", "tolerance"), varray(CMP_EPSILON)); @@ -1790,6 +1795,7 @@ static void _register_variant_builtin_methods() { bind_method(Quaternion, normalized, sarray(), varray()); bind_method(Quaternion, is_normalized, sarray(), varray()); bind_method(Quaternion, is_equal_approx, sarray("to"), varray()); + bind_method(Quaternion, is_finite, sarray(), varray()); bind_method(Quaternion, inverse, sarray(), varray()); bind_method(Quaternion, log, sarray(), varray()); bind_method(Quaternion, exp, sarray(), varray()); @@ -1909,6 +1915,7 @@ static void _register_variant_builtin_methods() { bind_method(Transform2D, basis_xform_inv, sarray("v"), varray()); bind_method(Transform2D, interpolate_with, sarray("xform", "weight"), varray()); bind_method(Transform2D, is_equal_approx, sarray("xform"), varray()); + bind_method(Transform2D, is_finite, sarray(), varray()); bind_method(Transform2D, set_rotation, sarray("rotation"), varray()); bind_method(Transform2D, set_scale, sarray("scale"), varray()); bind_method(Transform2D, set_skew, sarray("skew"), varray()); @@ -1929,6 +1936,7 @@ static void _register_variant_builtin_methods() { bind_method(Basis, tdotz, sarray("with"), varray()); bind_method(Basis, slerp, sarray("to", "weight"), varray()); bind_method(Basis, is_equal_approx, sarray("b"), varray()); + bind_method(Basis, is_finite, sarray(), varray()); bind_method(Basis, get_rotation_quaternion, sarray(), varray()); bind_static_method(Basis, looking_at, sarray("target", "up"), varray(Vector3(0, 1, 0))); bind_static_method(Basis, from_scale, sarray("scale"), varray()); @@ -1943,6 +1951,7 @@ static void _register_variant_builtin_methods() { bind_method(AABB, has_surface, sarray(), varray()); bind_method(AABB, has_point, sarray("point"), varray()); bind_method(AABB, is_equal_approx, sarray("aabb"), varray()); + bind_method(AABB, is_finite, sarray(), varray()); bind_method(AABB, intersects, sarray("with"), varray()); bind_method(AABB, encloses, sarray("with"), varray()); bind_method(AABB, intersects_plane, sarray("plane"), varray()); @@ -1975,6 +1984,7 @@ static void _register_variant_builtin_methods() { bind_method(Transform3D, looking_at, sarray("target", "up"), varray(Vector3(0, 1, 0))); bind_method(Transform3D, interpolate_with, sarray("xform", "weight"), varray()); bind_method(Transform3D, is_equal_approx, sarray("xform"), varray()); + bind_method(Transform3D, is_finite, sarray(), varray()); /* Projection */ diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index 670b66d53e..3843c32bcc 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -310,6 +310,10 @@ struct VariantUtilityFunctions { return Math::is_zero_approx(x); } + static inline bool is_finite(double x) { + return Math::is_finite(x); + } + static inline double ease(float x, float curve) { return Math::ease(x, curve); } @@ -1420,6 +1424,7 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(is_zero_approx, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(is_finite, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(ease, sarray("x", "curve"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); |