diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2019-10-14 16:33:45 -0400 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2019-10-14 16:46:54 -0400 |
commit | 86922ff70ba533b376a6680f965f542894e8c614 (patch) | |
tree | b99bc39067ce17ec257ad44199becdc61b69d3c2 | |
parent | 1fed266bf5452b30376db62495f4985f6975f2c1 (diff) |
Make is_equal_approx separate for structures
This commit adds exposed behavior for C#
31 files changed, 118 insertions, 16 deletions
diff --git a/core/color.cpp b/core/color.cpp index a54a3115cc..a6ad50b745 100644 --- a/core/color.cpp +++ b/core/color.cpp @@ -214,6 +214,11 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) { } } +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; diff --git a/core/color.h b/core/color.h index 8fb78d1ced..b34a82ef19 100644 --- a/core/color.h +++ b/core/color.h @@ -86,6 +86,8 @@ struct Color { void operator/=(const Color &p_color); void operator/=(const real_t &rvalue); + bool is_equal_approx(const Color &p_color) const; + void invert(); void contrast(); Color inverted() const; diff --git a/core/math/aabb.cpp b/core/math/aabb.cpp index a4eb1fe2a5..27da061274 100644 --- a/core/math/aabb.cpp +++ b/core/math/aabb.cpp @@ -69,6 +69,11 @@ void AABB::merge_with(const AABB &p_aabb) { 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; diff --git a/core/math/aabb.h b/core/math/aabb.h index 52e5ed3626..c3ce33c6f4 100644 --- a/core/math/aabb.h +++ b/core/math/aabb.h @@ -64,6 +64,7 @@ public: bool operator==(const AABB &p_rval) const; bool operator!=(const AABB &p_rval) const; + bool is_equal_approx(const AABB &p_aabb) 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 0a491010e2..d77501c0f6 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -106,17 +106,17 @@ Basis Basis::orthonormalized() const { } bool Basis::is_orthogonal() const { - Basis id; + Basis identity; Basis m = (*this) * transposed(); - return is_equal_approx(id, m); + return m.is_equal_approx(identity); } bool Basis::is_diagonal() const { return ( - Math::is_equal_approx(elements[0][1], 0) && Math::is_equal_approx(elements[0][2], 0) && - Math::is_equal_approx(elements[1][0], 0) && Math::is_equal_approx(elements[1][2], 0) && - Math::is_equal_approx(elements[2][0], 0) && Math::is_equal_approx(elements[2][1], 0)); + 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 { @@ -557,16 +557,9 @@ void Basis::set_euler_yxz(const Vector3 &p_euler) { *this = ymat * xmat * zmat; } -bool Basis::is_equal_approx(const Basis &a, const Basis &b, real_t p_epsilon) const { +bool Basis::is_equal_approx(const Basis &p_basis) const { - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (!Math::is_equal_approx(a.elements[i][j], b.elements[i][j], p_epsilon)) - return false; - } - } - - return true; + 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::is_equal_approx_ratio(const Basis &a, const Basis &b, real_t p_epsilon) const { diff --git a/core/math/basis.h b/core/math/basis.h index 4be4ea4cd3..9b2e38b3d3 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -127,7 +127,9 @@ public: return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2]; } - bool is_equal_approx(const Basis &a, const Basis &b, real_t p_epsilon = CMP_EPSILON) const; + bool is_equal_approx(const Basis &p_basis) const; + // TODO: Break compatibility in 4.0 by getting rid of this so that it's only an instance method. See also TODO in variant_call.cpp + bool is_equal_approx(const Basis &a, const Basis &b) const { return a.is_equal_approx(b); } bool is_equal_approx_ratio(const Basis &a, const Basis &b, real_t p_epsilon = UNIT_EPSILON) const; bool operator==(const Basis &p_matrix) const; diff --git a/core/math/plane.cpp b/core/math/plane.cpp index b01853c4ac..7c501e9f9d 100644 --- a/core/math/plane.cpp +++ b/core/math/plane.cpp @@ -161,6 +161,11 @@ bool Plane::is_almost_like(const Plane &p_plane) const { return (normal.dot(p_plane.normal) > _PLANE_EQ_DOT_EPSILON && Math::absd(d - p_plane.d) < _PLANE_EQ_D_EPSILON); } +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() + ", " + rtos(d); diff --git a/core/math/plane.h b/core/math/plane.h index ec817edd2c..10802de383 100644 --- a/core/math/plane.h +++ b/core/math/plane.h @@ -69,6 +69,7 @@ public: Plane operator-() const { return Plane(-normal, -d); } bool is_almost_like(const Plane &p_plane) const; + bool is_equal_approx(const Plane &p_plane) const; _FORCE_INLINE_ bool operator==(const Plane &p_plane) const; _FORCE_INLINE_ bool operator!=(const Plane &p_plane) const; diff --git a/core/math/quat.cpp b/core/math/quat.cpp index 1a67be7384..a4f91844b9 100644 --- a/core/math/quat.cpp +++ b/core/math/quat.cpp @@ -121,6 +121,11 @@ Quat Quat::operator*(const Quat &q) const { return r; } +bool Quat::is_equal_approx(const Quat &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 Quat::length() const { return Math::sqrt(length_squared()); diff --git a/core/math/quat.h b/core/math/quat.h index 3d6602e466..27885f4152 100644 --- a/core/math/quat.h +++ b/core/math/quat.h @@ -43,6 +43,7 @@ public: real_t x, y, z, w; _FORCE_INLINE_ real_t length_squared() const; + bool is_equal_approx(const Quat &p_quat) const; real_t length() const; void normalize(); Quat normalized() const; diff --git a/core/math/rect2.cpp b/core/math/rect2.cpp index fea128afbd..e31776672e 100644 --- a/core/math/rect2.cpp +++ b/core/math/rect2.cpp @@ -30,6 +30,11 @@ #include "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D +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; diff --git a/core/math/rect2.h b/core/math/rect2.h index f58756ee40..71221ffb1b 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -153,6 +153,7 @@ struct Rect2 { 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; } diff --git a/core/math/transform.cpp b/core/math/transform.cpp index 4056975da8..5dcc6ab9f0 100644 --- a/core/math/transform.cpp +++ b/core/math/transform.cpp @@ -182,6 +182,11 @@ Transform Transform::orthonormalized() const { return _copy; } +bool Transform::is_equal_approx(const Transform &p_transform) const { + + return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin); +} + bool Transform::operator==(const Transform &p_transform) const { return (basis == p_transform.basis && origin == p_transform.origin); diff --git a/core/math/transform.h b/core/math/transform.h index 90e2b07583..da65a183cf 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -70,6 +70,7 @@ public: void orthonormalize(); Transform orthonormalized() const; + bool is_equal_approx(const Transform &p_transform) const; bool operator==(const Transform &p_transform) const; bool operator!=(const Transform &p_transform) const; diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index 1d0387bd45..a1c0814637 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -147,6 +147,7 @@ void Transform2D::orthonormalize() { elements[0] = x; elements[1] = y; } + Transform2D Transform2D::orthonormalized() const { Transform2D on = *this; @@ -154,6 +155,11 @@ Transform2D Transform2D::orthonormalized() const { 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++) { diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index e8b44ab197..0ec39a1765 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -96,6 +96,7 @@ struct Transform2D { 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; diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index 972bccc0ac..fbedeb8eb2 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -203,6 +203,10 @@ Vector2 Vector2::reflect(const Vector2 &p_normal) const { 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); +} + /* Vector2i */ Vector2i Vector2i::operator+(const Vector2i &p_v) const { diff --git a/core/math/vector2.h b/core/math/vector2.h index 1a73831891..39fec15ce6 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -92,6 +92,8 @@ struct Vector2 { 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; diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index ebc1599820..e3211c8fb1 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -149,6 +149,11 @@ Basis Vector3::to_diagonal_matrix() const { 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 (rtos(x) + ", " + rtos(y) + ", " + rtos(z)); diff --git a/core/math/vector3.h b/core/math/vector3.h index de1743d88f..ac42238ed4 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -119,6 +119,8 @@ struct Vector3 { _FORCE_INLINE_ Vector3 bounce(const Vector3 &p_normal) const; _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_normal) const; + bool is_equal_approx(const Vector3 &p_v) const; + /* Operators */ _FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 40b944744b..8a45d8f6a0 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -816,7 +816,7 @@ struct _VariantCall { VCALL_PTR0R(Basis, get_orthogonal_index); VCALL_PTR0R(Basis, orthonormalized); VCALL_PTR2R(Basis, slerp); - VCALL_PTR2R(Basis, is_equal_approx); + VCALL_PTR2R(Basis, is_equal_approx); // TODO: Break compatibility in 4.0 to change this to an instance method (a.is_equal_approx(b) as VCALL_PTR1R) for consistency. VCALL_PTR0R(Basis, get_rotation_quat); VCALL_PTR0R(Transform, inverse); diff --git a/modules/mono/glue/Managed/Files/AABB.cs b/modules/mono/glue/Managed/Files/AABB.cs index 98a73382f4..6a4f785551 100644 --- a/modules/mono/glue/Managed/Files/AABB.cs +++ b/modules/mono/glue/Managed/Files/AABB.cs @@ -458,6 +458,11 @@ namespace Godot return _position == other._position && _size == other._size; } + public bool IsEqualApprox(AABB other) + { + return _position.IsEqualApprox(other._position) && _size.IsEqualApprox(other._size); + } + public override int GetHashCode() { return _position.GetHashCode() ^ _size.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Basis.cs b/modules/mono/glue/Managed/Files/Basis.cs index 0eb76e9c63..c5e62b77c8 100644 --- a/modules/mono/glue/Managed/Files/Basis.cs +++ b/modules/mono/glue/Managed/Files/Basis.cs @@ -654,6 +654,11 @@ namespace Godot return Row0.Equals(other.Row0) && Row1.Equals(other.Row1) && Row2.Equals(other.Row2); } + public bool IsEqualApprox(Basis other) + { + return Row0.IsEqualApprox(other.Row0) && Row1.IsEqualApprox(other.Row1) && Row2.IsEqualApprox(other.Row2); + } + public override int GetHashCode() { return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Color.cs b/modules/mono/glue/Managed/Files/Color.cs index 3a52a1a13b..f44634f4e5 100644 --- a/modules/mono/glue/Managed/Files/Color.cs +++ b/modules/mono/glue/Managed/Files/Color.cs @@ -664,6 +664,11 @@ namespace Godot return Mathf.IsEqualApprox(r, other.r) && Mathf.IsEqualApprox(g, other.g) && Mathf.IsEqualApprox(b, other.b) && Mathf.IsEqualApprox(a, other.a); } + public bool IsEqualApprox(Color other) + { + return Mathf.IsEqualApprox(r, other.r) && Mathf.IsEqualApprox(g, other.g) && Mathf.IsEqualApprox(b, other.b) && Mathf.IsEqualApprox(a, other.a); + } + public override int GetHashCode() { return r.GetHashCode() ^ g.GetHashCode() ^ b.GetHashCode() ^ a.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Plane.cs b/modules/mono/glue/Managed/Files/Plane.cs index a13161d2e6..e7ad68ab49 100644 --- a/modules/mono/glue/Managed/Files/Plane.cs +++ b/modules/mono/glue/Managed/Files/Plane.cs @@ -206,6 +206,11 @@ namespace Godot return _normal == other._normal && Mathf.IsEqualApprox(D, other.D); } + public bool IsEqualApprox(Plane other) + { + return _normal.IsEqualApprox(other._normal) && Mathf.IsEqualApprox(D, other.D); + } + public override int GetHashCode() { return _normal.GetHashCode() ^ D.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Quat.cs b/modules/mono/glue/Managed/Files/Quat.cs index 845c7c730e..52f0305b16 100644 --- a/modules/mono/glue/Managed/Files/Quat.cs +++ b/modules/mono/glue/Managed/Files/Quat.cs @@ -366,6 +366,11 @@ namespace Godot return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y) && Mathf.IsEqualApprox(z, other.z) && Mathf.IsEqualApprox(w, other.w); } + public bool IsEqualApprox(Quat other) + { + return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y) && Mathf.IsEqualApprox(z, other.z) && Mathf.IsEqualApprox(w, other.w); + } + public override int GetHashCode() { return y.GetHashCode() ^ x.GetHashCode() ^ z.GetHashCode() ^ w.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Rect2.cs b/modules/mono/glue/Managed/Files/Rect2.cs index 99542d0c0a..91e614dc7b 100644 --- a/modules/mono/glue/Managed/Files/Rect2.cs +++ b/modules/mono/glue/Managed/Files/Rect2.cs @@ -231,6 +231,11 @@ namespace Godot return _position.Equals(other._position) && _size.Equals(other._size); } + public bool IsEqualApprox(Rect2 other) + { + return _position.IsEqualApprox(other._position) && _size.IsEqualApprox(other.Size); + } + public override int GetHashCode() { return _position.GetHashCode() ^ _size.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Transform.cs b/modules/mono/glue/Managed/Files/Transform.cs index cc4d26158d..0b84050f07 100644 --- a/modules/mono/glue/Managed/Files/Transform.cs +++ b/modules/mono/glue/Managed/Files/Transform.cs @@ -185,6 +185,11 @@ namespace Godot return basis.Equals(other.basis) && origin.Equals(other.origin); } + public bool IsEqualApprox(Transform other) + { + return basis.IsEqualApprox(other.basis) && origin.IsEqualApprox(other.origin); + } + public override int GetHashCode() { return basis.GetHashCode() ^ origin.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Transform2D.cs b/modules/mono/glue/Managed/Files/Transform2D.cs index 814332dc07..77ea3e5830 100644 --- a/modules/mono/glue/Managed/Files/Transform2D.cs +++ b/modules/mono/glue/Managed/Files/Transform2D.cs @@ -357,6 +357,11 @@ namespace Godot return x.Equals(other.x) && y.Equals(other.y) && origin.Equals(other.origin); } + public bool IsEqualApprox(Transform2D other) + { + return x.IsEqualApprox(other.x) && y.IsEqualApprox(other.y) && origin.IsEqualApprox(other.origin); + } + public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode() ^ origin.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Vector2.cs b/modules/mono/glue/Managed/Files/Vector2.cs index 0daa94057e..70d6cd382a 100644 --- a/modules/mono/glue/Managed/Files/Vector2.cs +++ b/modules/mono/glue/Managed/Files/Vector2.cs @@ -458,6 +458,11 @@ namespace Godot return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y); } + public bool IsEqualApprox(Vector2 other) + { + return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y); + } + public override int GetHashCode() { return y.GetHashCode() ^ x.GetHashCode(); diff --git a/modules/mono/glue/Managed/Files/Vector3.cs b/modules/mono/glue/Managed/Files/Vector3.cs index 9076dbd3b0..7b1080c266 100644 --- a/modules/mono/glue/Managed/Files/Vector3.cs +++ b/modules/mono/glue/Managed/Files/Vector3.cs @@ -516,6 +516,11 @@ namespace Godot return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y) && Mathf.IsEqualApprox(z, other.z); } + public bool IsEqualApprox(Vector3 other) + { + return Mathf.IsEqualApprox(x, other.x) && Mathf.IsEqualApprox(y, other.y) && Mathf.IsEqualApprox(z, other.z); + } + public override int GetHashCode() { return y.GetHashCode() ^ x.GetHashCode() ^ z.GetHashCode(); |