From c77ae051d56a55abfba5f65f872da953feb1d216 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Wed, 20 Dec 2023 10:42:39 -0600 Subject: [PATCH] Add and expose Basis/Transform2D/3D division by float operator --- core/math/basis.h | 14 ++++++++++++++ core/math/transform_2d.cpp | 12 ++++++++++++ core/math/transform_2d.h | 2 ++ core/math/transform_3d.cpp | 11 +++++++++++ core/math/transform_3d.h | 2 ++ core/variant/variant_op.cpp | 9 +++++++++ doc/classes/Basis.xml | 14 ++++++++++++++ doc/classes/Transform2D.xml | 14 ++++++++++++++ doc/classes/Transform3D.xml | 14 ++++++++++++++ 9 files changed, 92 insertions(+) diff --git a/core/math/basis.h b/core/math/basis.h index b4d971464e4f..e3094114e84e 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -136,6 +136,8 @@ struct _NO_DISCARD_ Basis { _FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const; _FORCE_INLINE_ void operator*=(const real_t p_val); _FORCE_INLINE_ Basis operator*(const real_t p_val) const; + _FORCE_INLINE_ void operator/=(const real_t p_val); + _FORCE_INLINE_ Basis operator/(const real_t p_val) const; bool is_orthogonal() const; bool is_orthonormal() const; @@ -289,6 +291,18 @@ _FORCE_INLINE_ Basis Basis::operator*(const real_t p_val) const { return ret; } +_FORCE_INLINE_ void Basis::operator/=(const real_t p_val) { + rows[0] /= p_val; + rows[1] /= p_val; + rows[2] /= p_val; +} + +_FORCE_INLINE_ Basis Basis::operator/(const real_t p_val) const { + Basis ret(*this); + ret /= p_val; + return ret; +} + Vector3 Basis::xform(const Vector3 &p_vector) const { return Vector3( rows[0].dot(p_vector), diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index bc4682fd901d..a22d075b644a 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -295,6 +295,18 @@ Transform2D Transform2D::operator*(const real_t p_val) const { return ret; } +void Transform2D::operator/=(const real_t p_val) { + columns[0] /= p_val; + columns[1] /= p_val; + columns[2] /= p_val; +} + +Transform2D Transform2D::operator/(const real_t p_val) const { + Transform2D ret(*this); + ret /= p_val; + return ret; +} + Transform2D::operator String() const { return "[X: " + columns[0].operator String() + ", Y: " + columns[1].operator String() + diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index dd1a33c5d5fd..9ff925f66f5f 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -109,6 +109,8 @@ struct _NO_DISCARD_ Transform2D { Transform2D operator*(const Transform2D &p_transform) const; void operator*=(const real_t p_val); Transform2D operator*(const real_t p_val) const; + void operator/=(const real_t p_val); + Transform2D operator/(const real_t p_val) const; Transform2D interpolate_with(const Transform2D &p_transform, const real_t p_c) const; diff --git a/core/math/transform_3d.cpp b/core/math/transform_3d.cpp index cdc94676c9f5..20713349d7d8 100644 --- a/core/math/transform_3d.cpp +++ b/core/math/transform_3d.cpp @@ -208,6 +208,17 @@ Transform3D Transform3D::operator*(const real_t p_val) const { return ret; } +void Transform3D::operator/=(const real_t p_val) { + basis /= p_val; + origin /= p_val; +} + +Transform3D Transform3D::operator/(const real_t p_val) const { + Transform3D ret(*this); + ret /= p_val; + return ret; +} + Transform3D::operator String() const { return "[X: " + basis.get_column(0).operator String() + ", Y: " + basis.get_column(1).operator String() + diff --git a/core/math/transform_3d.h b/core/math/transform_3d.h index 70141a3dbe3f..d1ec34d53f34 100644 --- a/core/math/transform_3d.h +++ b/core/math/transform_3d.h @@ -104,6 +104,8 @@ struct _NO_DISCARD_ Transform3D { Transform3D operator*(const Transform3D &p_transform) const; void operator*=(const real_t p_val); Transform3D operator*(const real_t p_val) const; + void operator/=(const real_t p_val); + Transform3D operator/(const real_t p_val) const; Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const; diff --git a/core/variant/variant_op.cpp b/core/variant/variant_op.cpp index aed83ac010d6..4f9c38dc4c8d 100644 --- a/core/variant/variant_op.cpp +++ b/core/variant/variant_op.cpp @@ -412,6 +412,15 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_DIVIDE, Variant::VECTOR4I, Variant::FLOAT); register_op>(Variant::OP_DIVIDE, Variant::VECTOR4I, Variant::INT); + register_op>(Variant::OP_DIVIDE, Variant::TRANSFORM2D, Variant::INT); + register_op>(Variant::OP_DIVIDE, Variant::TRANSFORM2D, Variant::FLOAT); + + register_op>(Variant::OP_DIVIDE, Variant::TRANSFORM3D, Variant::INT); + register_op>(Variant::OP_DIVIDE, Variant::TRANSFORM3D, Variant::FLOAT); + + register_op>(Variant::OP_DIVIDE, Variant::BASIS, Variant::INT); + register_op>(Variant::OP_DIVIDE, Variant::BASIS, Variant::FLOAT); + register_op>(Variant::OP_DIVIDE, Variant::QUATERNION, Variant::FLOAT); register_op>(Variant::OP_DIVIDE, Variant::QUATERNION, Variant::INT); diff --git a/doc/classes/Basis.xml b/doc/classes/Basis.xml index f98c207a6e03..b4fb9107b09b 100644 --- a/doc/classes/Basis.xml +++ b/doc/classes/Basis.xml @@ -256,6 +256,20 @@ This operator multiplies all components of the [Basis], which scales it uniformly. + + + + + This operator divides all components of the [Basis], which inversely scales it uniformly. + + + + + + + This operator divides all components of the [Basis], which inversely scales it uniformly. + + diff --git a/doc/classes/Transform2D.xml b/doc/classes/Transform2D.xml index aee70f6b5986..158846035990 100644 --- a/doc/classes/Transform2D.xml +++ b/doc/classes/Transform2D.xml @@ -286,6 +286,20 @@ This operator multiplies all components of the [Transform2D], including the [member origin] vector, which scales it uniformly. + + + + + This operator divides all components of the [Transform2D], including the [member origin] vector, which inversely scales it uniformly. + + + + + + + This operator divides all components of the [Transform2D], including the [member origin] vector, which inversely scales it uniformly. + + diff --git a/doc/classes/Transform3D.xml b/doc/classes/Transform3D.xml index 85da629d7051..a467ac12f2de 100644 --- a/doc/classes/Transform3D.xml +++ b/doc/classes/Transform3D.xml @@ -245,6 +245,20 @@ This operator multiplies all components of the [Transform3D], including the [member origin] vector, which scales it uniformly. + + + + + This operator divides all components of the [Transform3D], including the [member origin] vector, which inversely scales it uniformly. + + + + + + + This operator divides all components of the [Transform3D], including the [member origin] vector, which inversely scales it uniformly. + +