diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-08-07 16:07:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-07 16:07:36 +0200 |
commit | 446fdc5b71370182439e382e6c71914c3612c734 (patch) | |
tree | f7223a639cf71adee7187f9db41c77e1b81f2904 /core | |
parent | 64aeae66a4fc5b0175d241c94564536ebefab907 (diff) | |
parent | 36061c5dca6c45429509f9fe01e8d9d20841d0e6 (diff) |
Merge pull request #64027 from Geometror/add-vector4-tests
Diffstat (limited to 'core')
-rw-r--r-- | core/math/vector4.cpp | 94 | ||||
-rw-r--r-- | core/math/vector4.h | 25 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 6 |
3 files changed, 100 insertions, 25 deletions
diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index cc0d0dcf72..4697c311b4 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -33,6 +33,40 @@ #include "core/math/basis.h" #include "core/string/print_string.h" +void Vector4::set_axis(const int p_axis, const real_t p_value) { + ERR_FAIL_INDEX(p_axis, 4); + components[p_axis] = p_value; +} + +real_t Vector4::get_axis(const int p_axis) const { + ERR_FAIL_INDEX_V(p_axis, 4, 0); + return operator[](p_axis); +} + +Vector4::Axis Vector4::min_axis_index() const { + uint32_t min_index = 0; + real_t min_value = x; + for (uint32_t i = 1; i < 4; i++) { + if (operator[](i) <= min_value) { + min_index = i; + min_value = operator[](i); + } + } + return Vector4::Axis(min_index); +} + +Vector4::Axis Vector4::max_axis_index() const { + uint32_t max_index = 0; + real_t max_value = x; + for (uint32_t i = 1; i < 4; i++) { + if (operator[](i) > max_value) { + max_index = i; + max_value = operator[](i); + } + } + return Vector4::Axis(max_index); +} + bool Vector4::is_equal_approx(const Vector4 &p_vec4) const { return Math::is_equal_approx(x, p_vec4.x) && Math::is_equal_approx(y, p_vec4.y) && Math::is_equal_approx(z, p_vec4.z) && Math::is_equal_approx(w, p_vec4.w); } @@ -53,6 +87,16 @@ bool Vector4::is_normalized() const { return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // Use less epsilon. } +real_t Vector4::distance_to(const Vector4 &p_to) const { + return (p_to - *this).length(); +} + +Vector4 Vector4::direction_to(const Vector4 &p_to) const { + Vector4 ret(p_to.x - x, p_to.y - y, p_to.z - z, p_to.w - w); + ret.normalize(); + return ret; +} + Vector4 Vector4::abs() const { return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w)); } @@ -81,32 +125,38 @@ Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const { w + (p_weight * (p_to.w - w))); } -Vector4 Vector4::inverse() const { - return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w); +Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const { + Vector4 res = *this; + res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight); + res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight); + res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight); + res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight); + return res; } -Vector4::Axis Vector4::min_axis_index() const { - uint32_t min_index = 0; - real_t min_value = x; - for (uint32_t i = 1; i < 4; i++) { - if (operator[](i) <= min_value) { - min_index = i; - min_value = operator[](i); - } - } - return Vector4::Axis(min_index); +Vector4 Vector4::posmod(const real_t p_mod) const { + return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod)); } -Vector4::Axis Vector4::max_axis_index() const { - uint32_t max_index = 0; - real_t max_value = x; - for (uint32_t i = 1; i < 4; i++) { - if (operator[](i) > max_value) { - max_index = i; - max_value = operator[](i); - } - } - return Vector4::Axis(max_index); +Vector4 Vector4::posmodv(const Vector4 &p_modv) const { + return Vector4(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z), Math::fposmod(w, p_modv.w)); +} + +void Vector4::snap(const Vector4 &p_step) { + x = Math::snapped(x, p_step.x); + y = Math::snapped(y, p_step.y); + z = Math::snapped(z, p_step.z); + w = Math::snapped(w, p_step.w); +} + +Vector4 Vector4::snapped(const Vector4 &p_step) const { + Vector4 v = *this; + v.snap(p_step); + return v; +} + +Vector4 Vector4::inverse() const { + return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w); } Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const { diff --git a/core/math/vector4.h b/core/math/vector4.h index 37ddb509d6..373a6a1218 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -62,6 +62,15 @@ struct _NO_DISCARD_ Vector4 { DEV_ASSERT((unsigned int)p_axis < 4); return components[p_axis]; } + + _FORCE_INLINE_ void set_all(const real_t p_value); + + void set_axis(const int p_axis, const real_t p_value); + real_t get_axis(const int p_axis) const; + + Vector4::Axis min_axis_index() const; + Vector4::Axis max_axis_index() const; + _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Vector4 &p_vec4) const; real_t length() const; @@ -69,15 +78,21 @@ struct _NO_DISCARD_ Vector4 { Vector4 normalized() const; bool is_normalized() const; + real_t distance_to(const Vector4 &p_to) const; + Vector4 direction_to(const Vector4 &p_to) const; + Vector4 abs() const; Vector4 sign() const; Vector4 floor() const; Vector4 ceil() const; Vector4 round() const; Vector4 lerp(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::Axis min_axis_index() const; - Vector4::Axis max_axis_index() const; + Vector4 posmod(const real_t p_mod) const; + Vector4 posmodv(const Vector4 &p_modv) const; + void snap(const Vector4 &p_step); + Vector4 snapped(const Vector4 &p_step) const; Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const; Vector4 inverse() const; @@ -130,6 +145,10 @@ struct _NO_DISCARD_ Vector4 { } }; +void Vector4::set_all(const real_t p_value) { + x = y = z = p_value; +} + real_t Vector4::dot(const Vector4 &p_vec4) const { return x * p_vec4.x + y * p_vec4.y + z * p_vec4.z + w * p_vec4.w; } @@ -193,7 +212,7 @@ Vector4 Vector4::operator/(const Vector4 &p_vec4) const { } Vector4 Vector4::operator-() const { - return Vector4(x, y, z, w); + return Vector4(-x, -y, -z, -w); } Vector4 Vector4::operator*(const real_t &s) const { diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index d1f1b83457..5c298f9b3b 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1737,9 +1737,15 @@ static void _register_variant_builtin_methods() { bind_method(Vector4, ceil, sarray(), varray()); bind_method(Vector4, round, sarray(), varray()); bind_method(Vector4, lerp, sarray("to", "weight"), varray()); + bind_method(Vector4, cubic_interpolate, sarray("b", "pre_a", "post_b", "weight"), varray()); + bind_method(Vector4, posmod, sarray("mod"), varray()); + bind_method(Vector4, posmodv, sarray("modv"), varray()); + bind_method(Vector4, snapped, sarray("step"), varray()); bind_method(Vector4, clamp, sarray("min", "max"), varray()); bind_method(Vector4, normalized, sarray(), varray()); bind_method(Vector4, is_normalized, sarray(), varray()); + bind_method(Vector4, direction_to, sarray("to"), varray()); + bind_method(Vector4, distance_to, sarray("to"), varray()); bind_method(Vector4, dot, sarray("with"), varray()); bind_method(Vector4, inverse, sarray(), varray()); bind_method(Vector4, is_equal_approx, sarray("with"), varray()); |