Skip to content

Commit

Permalink
[Math] Add is_finite methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed Apr 11, 2024
1 parent b021245 commit d389171
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/godot_cpp/core/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ inline bool is_inf(double p_val) {
return std::isinf(p_val);
}

inline bool is_finite(float p_val) {
return std::isfinite(p_val);
}

inline bool is_finite(double p_val) {
return std::isfinite(p_val);
}

inline bool is_equal_approx(float a, float b) {
// Check for exact equality first, required to handle "infinity" values.
if (a == b) {
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/aabb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/basis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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;
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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;
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/rect2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,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; }
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/transform2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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;

Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/transform3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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;
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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);
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ struct _NO_DISCARD_ Vector3 {

bool is_equal_approx(const Vector3 &p_v) const;
bool is_zero_approx() const;
bool is_finite() const;

/* Operators */

Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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;
Expand Down
4 changes: 4 additions & 0 deletions src/variant/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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)) {
Expand Down
4 changes: 4 additions & 0 deletions src/variant/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,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++) {
Expand Down
4 changes: 4 additions & 0 deletions src/variant/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,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) + "]";
}
Expand Down
4 changes: 4 additions & 0 deletions src/variant/quaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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());
}
Expand Down
4 changes: 4 additions & 0 deletions src/variant/rect2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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)) {
Expand Down
4 changes: 4 additions & 0 deletions src/variant/transform2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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);
Expand Down
4 changes: 4 additions & 0 deletions src/variant/transform3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/variant/vector2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,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) + ")";
}
Expand Down
4 changes: 4 additions & 0 deletions src/variant/vector3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,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) + ")";
}
Expand Down
4 changes: 4 additions & 0 deletions src/variant/vector4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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());
}
Expand Down

0 comments on commit d389171

Please sign in to comment.