From a8d12b5a613400024eaeea37266cf7cf4bca3724 Mon Sep 17 00:00:00 2001 From: Ricard Rovira Cubeles Date: Sun, 15 Aug 2021 16:45:37 +0200 Subject: Add constant to vector function parameters that don't actually modify their input. Add more overloads of vector multiplication, required by templates to compile with float=64. --- core/math/vector2.cpp | 4 +-- core/math/vector2.h | 30 ++++++++--------- core/math/vector3.cpp | 14 ++++---- core/math/vector3.h | 60 ++++++++++++++++++++------------- core/math/vector3i.cpp | 4 +-- core/math/vector3i.h | 50 ++++++++++++++++----------- modules/mobile_vr/mobile_vr_interface.h | 8 ++--- 7 files changed, 97 insertions(+), 73 deletions(-) diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index eb3301f5d0..54abc1b7f2 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -102,7 +102,7 @@ Vector2 Vector2::round() const { return Vector2(Math::round(x), Math::round(y)); } -Vector2 Vector2::rotated(real_t p_by) const { +Vector2 Vector2::rotated(const real_t p_by) const { real_t sine = Math::sin(p_by); real_t cosi = Math::cos(p_by); return Vector2( @@ -145,7 +145,7 @@ Vector2 Vector2::limit_length(const real_t p_len) const { return v; } -Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const { +Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const { Vector2 p0 = p_pre_a; Vector2 p1 = *this; Vector2 p2 = p_b; diff --git a/core/math/vector2.h b/core/math/vector2.h index 4d9f3126e9..330b4741b1 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -66,7 +66,7 @@ struct Vector2 { return p_idx ? y : x; } - _FORCE_INLINE_ void set_all(real_t p_value) { + _FORCE_INLINE_ void set_all(const real_t p_value) { x = y = p_value; } @@ -106,11 +106,11 @@ struct Vector2 { Vector2 posmodv(const Vector2 &p_modv) const; Vector2 project(const Vector2 &p_to) const; - Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const; + Vector2 plane_project(const real_t p_d, const Vector2 &p_vec) const; - _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, real_t p_weight) const; - _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const; - Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const; + _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const; + _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const; + Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const; Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const; Vector2 slide(const Vector2 &p_normal) const; @@ -152,7 +152,7 @@ struct Vector2 { return Vector2(Math::abs(x), Math::abs(y)); } - Vector2 rotated(real_t p_by) const; + Vector2 rotated(const real_t p_by) const; Vector2 orthogonal() const { return Vector2(y, -x); } @@ -168,29 +168,29 @@ struct Vector2 { operator String() const; _FORCE_INLINE_ Vector2() {} - _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) { + _FORCE_INLINE_ Vector2(const real_t p_x, const real_t p_y) { x = p_x; y = p_y; } }; -_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const { +_FORCE_INLINE_ Vector2 Vector2::plane_project(const real_t p_d, const Vector2 &p_vec) const { return p_vec - *this * (dot(p_vec) - p_d); } -_FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2 &p_vec) { +_FORCE_INLINE_ Vector2 operator*(const float p_scalar, const Vector2 &p_vec) { return p_vec * p_scalar; } -_FORCE_INLINE_ Vector2 operator*(double p_scalar, const Vector2 &p_vec) { +_FORCE_INLINE_ Vector2 operator*(const double p_scalar, const Vector2 &p_vec) { return p_vec * p_scalar; } -_FORCE_INLINE_ Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) { +_FORCE_INLINE_ Vector2 operator*(const int32_t p_scalar, const Vector2 &p_vec) { return p_vec * p_scalar; } -_FORCE_INLINE_ Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) { +_FORCE_INLINE_ Vector2 operator*(const int64_t p_scalar, const Vector2 &p_vec) { return p_vec * p_scalar; } @@ -250,7 +250,7 @@ _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const { return x != p_vec2.x || y != p_vec2.y; } -Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const { +Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const { Vector2 res = *this; res.x += (p_weight * (p_to.x - x)); @@ -259,7 +259,7 @@ Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const { return res; } -Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const { +Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const { #ifdef MATH_CHECKS ERR_FAIL_COND_V_MSG(!is_normalized(), Vector2(), "The start Vector2 must be normalized."); #endif @@ -357,7 +357,7 @@ struct Vector2i { x = (int32_t)p_vec2.x; y = (int32_t)p_vec2.y; } - inline Vector2i(int32_t p_x, int32_t p_y) { + inline Vector2i(const int32_t p_x, const int32_t p_y) { x = p_x; y = p_y; } diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index 3d59064af6..401c3ccd9c 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -32,22 +32,22 @@ #include "core/math/basis.h" -void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) { +void Vector3::rotate(const Vector3 &p_axis, const real_t p_phi) { *this = Basis(p_axis, p_phi).xform(*this); } -Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const { +Vector3 Vector3::rotated(const Vector3 &p_axis, const real_t p_phi) const { Vector3 r = *this; r.rotate(p_axis, p_phi); return r; } -void Vector3::set_axis(int p_axis, real_t p_value) { +void Vector3::set_axis(const int p_axis, const real_t p_value) { ERR_FAIL_INDEX(p_axis, 3); coord[p_axis] = p_value; } -real_t Vector3::get_axis(int p_axis) const { +real_t Vector3::get_axis(const int p_axis) const { ERR_FAIL_INDEX_V(p_axis, 3, 0); return operator[](p_axis); } @@ -59,13 +59,13 @@ Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const { CLAMP(z, p_min.z, p_max.z)); } -void Vector3::snap(Vector3 p_step) { +void Vector3::snap(const Vector3 p_step) { x = Math::snapped(x, p_step.x); y = Math::snapped(y, p_step.y); z = Math::snapped(z, p_step.z); } -Vector3 Vector3::snapped(Vector3 p_step) const { +Vector3 Vector3::snapped(const Vector3 p_step) const { Vector3 v = *this; v.snap(p_step); return v; @@ -82,7 +82,7 @@ Vector3 Vector3::limit_length(const real_t p_len) const { return v; } -Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const { +Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const { Vector3 p0 = p_pre_a; Vector3 p1 = *this; Vector3 p2 = p_b; diff --git a/core/math/vector3.h b/core/math/vector3.h index d8d3cd3cc0..6a4c42f41b 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -56,18 +56,18 @@ struct Vector3 { real_t coord[3] = { 0 }; }; - _FORCE_INLINE_ const real_t &operator[](int p_axis) const { + _FORCE_INLINE_ const real_t &operator[](const int p_axis) const { return coord[p_axis]; } - _FORCE_INLINE_ real_t &operator[](int p_axis) { + _FORCE_INLINE_ real_t &operator[](const int p_axis) { return coord[p_axis]; } - void set_axis(int p_axis, real_t p_value); - real_t get_axis(int p_axis) const; + void set_axis(const int p_axis, const real_t p_value); + real_t get_axis(const int p_axis) const; - _FORCE_INLINE_ void set_all(real_t p_value) { + _FORCE_INLINE_ void set_all(const real_t p_value) { x = y = z = p_value; } @@ -90,17 +90,17 @@ struct Vector3 { _FORCE_INLINE_ void zero(); - void snap(Vector3 p_val); - Vector3 snapped(Vector3 p_val) const; + void snap(const Vector3 p_val); + Vector3 snapped(const Vector3 p_val) const; - void rotate(const Vector3 &p_axis, real_t p_phi); - Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const; + void rotate(const Vector3 &p_axis, const real_t p_phi); + Vector3 rotated(const Vector3 &p_axis, const real_t p_phi) const; /* Static Methods between 2 vector3s */ - _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, real_t p_weight) const; - _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, real_t p_weight) const; - Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const; + _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const; + _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const real_t p_weight) const; + Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const; Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const; _FORCE_INLINE_ Vector3 cross(const Vector3 &p_b) const; @@ -143,10 +143,10 @@ struct Vector3 { _FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v); _FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const; - _FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar); - _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const; - _FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar); - _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const; + _FORCE_INLINE_ Vector3 &operator*=(const real_t p_scalar); + _FORCE_INLINE_ Vector3 operator*(const real_t p_scalar) const; + _FORCE_INLINE_ Vector3 &operator/=(const real_t p_scalar); + _FORCE_INLINE_ Vector3 operator/(const real_t p_scalar) const; _FORCE_INLINE_ Vector3 operator-() const; @@ -168,7 +168,7 @@ struct Vector3 { y = p_ivec.y; z = p_ivec.z; } - _FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) { + _FORCE_INLINE_ Vector3(const real_t p_x, const real_t p_y, const real_t p_z) { x = p_x; y = p_y; z = p_z; @@ -208,14 +208,14 @@ Vector3 Vector3::round() const { return Vector3(Math::round(x), Math::round(y), Math::round(z)); } -Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const { +Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const { return Vector3( x + (p_weight * (p_to.x - x)), y + (p_weight * (p_to.y - y)), z + (p_weight * (p_to.z - z))); } -Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const { +Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const { real_t theta = angle_to(p_to); return rotated(cross(p_to).normalized(), theta * p_weight); } @@ -303,29 +303,41 @@ Vector3 Vector3::operator/(const Vector3 &p_v) const { return Vector3(x / p_v.x, y / p_v.y, z / p_v.z); } -Vector3 &Vector3::operator*=(real_t p_scalar) { +Vector3 &Vector3::operator*=(const real_t p_scalar) { x *= p_scalar; y *= p_scalar; z *= p_scalar; return *this; } -_FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) { +_FORCE_INLINE_ Vector3 operator*(const float p_scalar, const Vector3 &p_vec) { return p_vec * p_scalar; } -Vector3 Vector3::operator*(real_t p_scalar) const { +_FORCE_INLINE_ Vector3 operator*(const double p_scalar, const Vector3 &p_vec) { + return p_vec * p_scalar; +} + +_FORCE_INLINE_ Vector3 operator*(const int32_t p_scalar, const Vector3 &p_vec) { + return p_vec * p_scalar; +} + +_FORCE_INLINE_ Vector3 operator*(const int64_t p_scalar, const Vector3 &p_vec) { + return p_vec * p_scalar; +} + +Vector3 Vector3::operator*(const real_t p_scalar) const { return Vector3(x * p_scalar, y * p_scalar, z * p_scalar); } -Vector3 &Vector3::operator/=(real_t p_scalar) { +Vector3 &Vector3::operator/=(const real_t p_scalar) { x /= p_scalar; y /= p_scalar; z /= p_scalar; return *this; } -Vector3 Vector3::operator/(real_t p_scalar) const { +Vector3 Vector3::operator/(const real_t p_scalar) const { return Vector3(x / p_scalar, y / p_scalar, z / p_scalar); } diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp index 2de1e4e331..d3a57af77c 100644 --- a/core/math/vector3i.cpp +++ b/core/math/vector3i.cpp @@ -30,12 +30,12 @@ #include "vector3i.h" -void Vector3i::set_axis(int p_axis, int32_t p_value) { +void Vector3i::set_axis(const int p_axis, const int32_t p_value) { ERR_FAIL_INDEX(p_axis, 3); coord[p_axis] = p_value; } -int32_t Vector3i::get_axis(int p_axis) const { +int32_t Vector3i::get_axis(const int p_axis) const { ERR_FAIL_INDEX_V(p_axis, 3, 0); return operator[](p_axis); } diff --git a/core/math/vector3i.h b/core/math/vector3i.h index 37c7c1c368..9308d09045 100644 --- a/core/math/vector3i.h +++ b/core/math/vector3i.h @@ -51,16 +51,16 @@ struct Vector3i { int32_t coord[3] = { 0 }; }; - _FORCE_INLINE_ const int32_t &operator[](int p_axis) const { + _FORCE_INLINE_ const int32_t &operator[](const int p_axis) const { return coord[p_axis]; } - _FORCE_INLINE_ int32_t &operator[](int p_axis) { + _FORCE_INLINE_ int32_t &operator[](const int p_axis) { return coord[p_axis]; } - void set_axis(int p_axis, int32_t p_value); - int32_t get_axis(int p_axis) const; + void set_axis(const int p_axis, const int32_t p_value); + int32_t get_axis(const int p_axis) const; int min_axis() const; int max_axis() const; @@ -84,12 +84,12 @@ struct Vector3i { _FORCE_INLINE_ Vector3i &operator%=(const Vector3i &p_v); _FORCE_INLINE_ Vector3i operator%(const Vector3i &p_v) const; - _FORCE_INLINE_ Vector3i &operator*=(int32_t p_scalar); - _FORCE_INLINE_ Vector3i operator*(int32_t p_scalar) const; - _FORCE_INLINE_ Vector3i &operator/=(int32_t p_scalar); - _FORCE_INLINE_ Vector3i operator/(int32_t p_scalar) const; - _FORCE_INLINE_ Vector3i &operator%=(int32_t p_scalar); - _FORCE_INLINE_ Vector3i operator%(int32_t p_scalar) const; + _FORCE_INLINE_ Vector3i &operator*=(const int32_t p_scalar); + _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const; + _FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar); + _FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) const; + _FORCE_INLINE_ Vector3i &operator%=(const int32_t p_scalar); + _FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const; _FORCE_INLINE_ Vector3i operator-() const; @@ -103,7 +103,7 @@ struct Vector3i { operator String() const; _FORCE_INLINE_ Vector3i() {} - _FORCE_INLINE_ Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) { + _FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) { x = p_x; y = p_y; z = p_z; @@ -175,40 +175,52 @@ Vector3i Vector3i::operator%(const Vector3i &p_v) const { return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z); } -Vector3i &Vector3i::operator*=(int32_t p_scalar) { +Vector3i &Vector3i::operator*=(const int32_t p_scalar) { x *= p_scalar; y *= p_scalar; z *= p_scalar; return *this; } -_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar, const Vector3i &p_vec) { - return p_vec * p_scalar; +_FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) { + return p_vector * p_scalar; } -Vector3i Vector3i::operator*(int32_t p_scalar) const { +_FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) { + return p_vector * p_scalar; +} + +_FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) { + return p_vector * p_scalar; +} + +_FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) { + return p_vector * p_scalar; +} + +Vector3i Vector3i::operator*(const int32_t p_scalar) const { return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar); } -Vector3i &Vector3i::operator/=(int32_t p_scalar) { +Vector3i &Vector3i::operator/=(const int32_t p_scalar) { x /= p_scalar; y /= p_scalar; z /= p_scalar; return *this; } -Vector3i Vector3i::operator/(int32_t p_scalar) const { +Vector3i Vector3i::operator/(const int32_t p_scalar) const { return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar); } -Vector3i &Vector3i::operator%=(int32_t p_scalar) { +Vector3i &Vector3i::operator%=(const int32_t p_scalar) { x %= p_scalar; y %= p_scalar; z %= p_scalar; return *this; } -Vector3i Vector3i::operator%(int32_t p_scalar) const { +Vector3i Vector3i::operator%(const int32_t p_scalar) const { return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar); } diff --git a/modules/mobile_vr/mobile_vr_interface.h b/modules/mobile_vr/mobile_vr_interface.h index 29ce0f92c8..0c05dc1ebb 100644 --- a/modules/mobile_vr/mobile_vr_interface.h +++ b/modules/mobile_vr/mobile_vr_interface.h @@ -86,20 +86,20 @@ private: Vector3 mag_next_max; ///@TODO a few support functions for trackers, most are math related and should likely be moved elsewhere - float floor_decimals(float p_value, float p_decimals) { + float floor_decimals(const float p_value, const float p_decimals) { float power_of_10 = pow(10.0f, p_decimals); return floor(p_value * power_of_10) / power_of_10; }; - Vector3 floor_decimals(const Vector3 &p_vector, float p_decimals) { + Vector3 floor_decimals(const Vector3 &p_vector, const float p_decimals) { return Vector3(floor_decimals(p_vector.x, p_decimals), floor_decimals(p_vector.y, p_decimals), floor_decimals(p_vector.z, p_decimals)); }; - Vector3 low_pass(const Vector3 &p_vector, const Vector3 &p_last_vector, float p_factor) { + Vector3 low_pass(const Vector3 &p_vector, const Vector3 &p_last_vector, const float p_factor) { return p_vector + (p_factor * (p_last_vector - p_vector)); }; - Vector3 scrub(const Vector3 &p_vector, const Vector3 &p_last_vector, float p_decimals, float p_factor) { + Vector3 scrub(const Vector3 &p_vector, const Vector3 &p_last_vector, const float p_decimals, const float p_factor) { return low_pass(floor_decimals(p_vector, p_decimals), p_last_vector, p_factor); }; -- cgit v1.2.3