From 96d1c2d69c16847bc3623ff1cdc51feac9e23282 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 May 2023 13:26:52 +0200 Subject: [PATCH] Fix small inconsistencies in Vector3/3i/4/4i apis. --- core/math/vector3i.cpp | 4 ++++ core/math/vector3i.h | 2 ++ core/math/vector4.cpp | 13 ++++++++++++- core/math/vector4.h | 9 ++++++++- core/math/vector4i.cpp | 12 ++++++++++++ core/math/vector4i.h | 10 ++++++++++ core/variant/variant_call.cpp | 16 ++++++++++++++-- 7 files changed, 62 insertions(+), 4 deletions(-) diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp index 1d3deaaeb..90a4b22c1 100644 --- a/core/math/vector3i.cpp +++ b/core/math/vector3i.cpp @@ -58,6 +58,10 @@ Vector3i Vector3i::clamp(const Vector3i &p_min, const Vector3i &p_max) const { CLAMP(z, p_min.z, p_max.z)); } +Vector3 Vector3i::to_vector3() const { + return Vector3(x, y, z); +} + Vector3i::operator String() const { return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ")"; } diff --git a/core/math/vector3i.h b/core/math/vector3i.h index d11df5760..5764ece13 100644 --- a/core/math/vector3i.h +++ b/core/math/vector3i.h @@ -114,6 +114,8 @@ struct _NO_DISCARD_CLASS_ Vector3i { _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const; _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const; + Vector3 to_vector3() const; + operator String() const; operator Vector3() const; diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index b01da7932..7141ead24 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -87,6 +87,17 @@ bool Vector4::is_normalized() const { return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // Use less epsilon. } +Vector4 Vector4::limit_length(const real_t p_len) const { + const real_t l = length(); + Vector4 v = *this; + if (l > 0 && p_len < l) { + v /= l; + v *= p_len; + } + + return v; +} + real_t Vector4::distance_to(const Vector4 &p_to) const { return (p_to - *this).length(); } @@ -121,7 +132,7 @@ Vector4 Vector4::round() const { return Vector4(Math::round(x), Math::round(y), Math::round(z), Math::round(w)); } -Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const { +Vector4 Vector4::linear_interpolate(const Vector4 &p_to, const real_t p_weight) const { return Vector4( x + (p_weight * (p_to.x - x)), y + (p_weight * (p_to.y - y)), diff --git a/core/math/vector4.h b/core/math/vector4.h index 10065523e..0df8fcde7 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -76,6 +76,9 @@ struct _NO_DISCARD_CLASS_ Vector4 { void normalize(); Vector4 normalized() const; bool is_normalized() const; + Vector4 limit_length(const real_t p_len = 1.0) const; + + _FORCE_INLINE_ void zero(); real_t distance_to(const Vector4 &p_to) const; real_t distance_squared_to(const Vector4 &p_to) const; @@ -87,7 +90,7 @@ struct _NO_DISCARD_CLASS_ Vector4 { Vector4 ceil() const; Vector4 round() const; - Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const; + Vector4 linear_interpolate(const Vector4 &p_to, const real_t p_weight) const; Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const; Vector4 posmod(const real_t p_mod) const; @@ -163,6 +166,10 @@ real_t Vector4::length_squared() const { return dot(*this); } +void Vector4::zero() { + x = y = z = 0; +} + void Vector4::operator+=(const Vector4 &p_vec4) { x += p_vec4.x; y += p_vec4.y; diff --git a/core/math/vector4i.cpp b/core/math/vector4i.cpp index a5e3d6f24..ca2273a13 100644 --- a/core/math/vector4i.cpp +++ b/core/math/vector4i.cpp @@ -75,6 +75,18 @@ Vector4i Vector4i::clamp(const Vector4i &p_min, const Vector4i &p_max) const { CLAMP(w, p_min.w, p_max.w)); } +Vector4i Vector4i::linear_interpolate(const Vector4i &p_to, const real_t p_weight) const { + return Vector4i( + x + (p_weight * (p_to.x - x)), + y + (p_weight * (p_to.y - y)), + z + (p_weight * (p_to.z - z)), + w + (p_weight * (p_to.w - w))); +} + +Vector4 Vector4i::to_vector4() const { + return Vector4(x, y, z, w); +} + Vector4i::operator String() const { return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ", " + itos(w) + ")"; } diff --git a/core/math/vector4i.h b/core/math/vector4i.h index d90303f20..5024e4646 100644 --- a/core/math/vector4i.h +++ b/core/math/vector4i.h @@ -66,6 +66,8 @@ struct _NO_DISCARD_CLASS_ Vector4i { return coord[p_axis]; } + _FORCE_INLINE_ void set_all(const int32_t p_value); + void set_axis(const int p_axis, const int32_t p_value); int32_t get_axis(const int p_axis) const; @@ -81,6 +83,8 @@ struct _NO_DISCARD_CLASS_ Vector4i { _FORCE_INLINE_ Vector4i sign() const; Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const; + Vector4i linear_interpolate(const Vector4i &p_to, const real_t p_weight) const; + /* Operators */ _FORCE_INLINE_ Vector4i &operator+=(const Vector4i &p_v); @@ -110,6 +114,8 @@ struct _NO_DISCARD_CLASS_ Vector4i { _FORCE_INLINE_ bool operator>(const Vector4i &p_v) const; _FORCE_INLINE_ bool operator>=(const Vector4i &p_v) const; + Vector4 to_vector4() const; + operator String() const; operator Vector4() const; @@ -130,6 +136,10 @@ struct _NO_DISCARD_CLASS_ Vector4i { } }; +void Vector4i::set_all(const int32_t p_value) { + x = y = z = p_value; +} + int64_t Vector4i::length_squared() const { return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z + w * (int64_t)w; } diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index e1e7207b5..5d2fb1082 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -693,6 +693,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector3i, sign); VCALL_LOCALMEM2R(Vector3i, clamp); VCALL_LOCALMEM2R(Vector3i, linear_interpolate); + VCALL_LOCALMEM0R(Vector3i, to_vector3); VCALL_LOCALMEM1(Vector4, set_all); VCALL_LOCALMEM2(Vector4, set_axis); @@ -705,6 +706,8 @@ struct _VariantCall { VCALL_LOCALMEM0(Vector4, normalize); VCALL_LOCALMEM0R(Vector4, normalized); VCALL_LOCALMEM0R(Vector4, is_normalized); + VCALL_LOCALMEM1R(Vector4, limit_length); + VCALL_LOCALMEM0(Vector4, zero); VCALL_LOCALMEM1R(Vector4, distance_to); VCALL_LOCALMEM1R(Vector4, distance_squared_to); VCALL_LOCALMEM1R(Vector4, direction_to); @@ -713,7 +716,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector4, floor); VCALL_LOCALMEM0R(Vector4, ceil); VCALL_LOCALMEM0R(Vector4, round); - VCALL_LOCALMEM2R(Vector4, lerp); + VCALL_LOCALMEM2R(Vector4, linear_interpolate); VCALL_LOCALMEM4R(Vector4, cubic_interpolate); VCALL_LOCALMEM1R(Vector4, posmod); VCALL_LOCALMEM1R(Vector4, posmodv); @@ -723,6 +726,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector4, inverse); VCALL_LOCALMEM1R(Vector4, dot); + VCALL_LOCALMEM1(Vector4i, set_all); VCALL_LOCALMEM2(Vector4i, set_axis); VCALL_LOCALMEM1R(Vector4i, get_axis); VCALL_LOCALMEM0R(Vector4i, min_axis); @@ -733,6 +737,8 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector4i, abs); VCALL_LOCALMEM0R(Vector4i, sign); VCALL_LOCALMEM2R(Vector4i, clamp); + VCALL_LOCALMEM2R(Vector4i, linear_interpolate); + VCALL_LOCALMEM0R(Vector4i, to_vector4); VCALL_LOCALMEM1(Plane, set_normal); VCALL_LOCALMEM0R(Plane, get_normal); @@ -2784,6 +2790,7 @@ void register_variant_methods() { ADDFUNC0R(VECTOR3I, VECTOR3I, Vector3i, sign, varray()); ADDFUNC2R(VECTOR3I, VECTOR3I, Vector3i, clamp, VECTOR3I, "min", VECTOR3I, "max", varray()); ADDFUNC2R(VECTOR3I, VECTOR3I, Vector3i, linear_interpolate, VECTOR3I, "to", REAL, "weight", varray()); + ADDFUNC0R(VECTOR3I, VECTOR3, Vector3i, to_vector3, varray()); ADDFUNC1(VECTOR4, NIL, Vector4, set_all, REAL, "value", varray()); ADDFUNC2(VECTOR4, NIL, Vector4, set_axis, INT, "axis", REAL, "value", varray()); @@ -2796,6 +2803,8 @@ void register_variant_methods() { ADDFUNC0(VECTOR4, NIL, Vector4, normalize, varray()); ADDFUNC0R(VECTOR4, VECTOR4, Vector4, normalized, varray()); ADDFUNC0R(VECTOR4, BOOL, Vector4, is_normalized, varray()); + ADDFUNC1R(VECTOR4, VECTOR4, Vector4, limit_length, REAL, "len", varray(1.0)); + ADDFUNC0(VECTOR4, NIL, Vector4, zero, varray()); ADDFUNC1R(VECTOR4, REAL, Vector4, distance_to, VECTOR4, "b", varray()); ADDFUNC1R(VECTOR4, REAL, Vector4, distance_squared_to, VECTOR4, "b", varray()); ADDFUNC1R(VECTOR4, VECTOR4, Vector4, direction_to, VECTOR4, "b", varray()); @@ -2804,7 +2813,7 @@ void register_variant_methods() { ADDFUNC0R(VECTOR4, VECTOR4, Vector4, floor, varray()); ADDFUNC0R(VECTOR4, VECTOR4, Vector4, ceil, varray()); ADDFUNC0R(VECTOR4, VECTOR4, Vector4, round, varray()); - ADDFUNC2R(VECTOR4, VECTOR4, Vector4, lerp, VECTOR4, "to", REAL, "weight", varray()); + ADDFUNC2R(VECTOR4, VECTOR4, Vector4, linear_interpolate, VECTOR4, "to", REAL, "weight", varray()); ADDFUNC4R(VECTOR4, VECTOR4, Vector4, cubic_interpolate, VECTOR4, "b", VECTOR4, "pre_a", VECTOR4, "post_b", REAL, "weight", varray()); ADDFUNC1R(VECTOR4, VECTOR4, Vector4, posmod, REAL, "mod", varray()); ADDFUNC1R(VECTOR4, VECTOR4, Vector4, posmodv, VECTOR4, "modv", varray()); @@ -2814,6 +2823,7 @@ void register_variant_methods() { ADDFUNC0R(VECTOR4, VECTOR4, Vector4, inverse, varray()); ADDFUNC1R(VECTOR4, REAL, Vector4, dot, VECTOR4, "b", varray()); + ADDFUNC1(VECTOR4I, NIL, Vector4i, set_all, INT, "value", varray()); ADDFUNC2(VECTOR4I, NIL, Vector4i, set_axis, INT, "axis", INT, "value", varray()); ADDFUNC1R(VECTOR4I, INT, Vector4i, get_axis, INT, "axis", varray()); ADDFUNC0R(VECTOR4I, INT, Vector4i, min_axis, varray()); @@ -2824,6 +2834,8 @@ void register_variant_methods() { ADDFUNC0R(VECTOR4I, VECTOR4I, Vector4i, abs, varray()); ADDFUNC0R(VECTOR4I, VECTOR4I, Vector4i, sign, varray()); ADDFUNC2R(VECTOR4I, VECTOR4I, Vector4i, clamp, VECTOR4I, "min", VECTOR4I, "max", varray()); + ADDFUNC2R(VECTOR4I, VECTOR4I, Vector4i, linear_interpolate, VECTOR4I, "to", REAL, "weight", varray()); + ADDFUNC0R(VECTOR4I, VECTOR4, Vector4i, to_vector4, varray()); ADDFUNC1(PLANE, NIL, Plane, set_normal, VECTOR3, "normal", varray()); ADDFUNC0R(PLANE, VECTOR3, Plane, get_normal, varray());