Ported improvements to Vector2, Vector2i, Vector3, Vector3i, Vector4, and Vector4i from Godot4.

This commit is contained in:
Relintai 2022-08-15 16:13:21 +02:00
parent bdd94e5b26
commit 608ba7826a
15 changed files with 209 additions and 124 deletions

View File

@ -31,6 +31,8 @@
/*************************************************************************/ /*************************************************************************/
#include "core/math/rect2.h" #include "core/math/rect2.h"
#include "core/math/vector2.h"
#include "core/vector.h"
class Delaunay2D { class Delaunay2D {
public: public:

View File

@ -32,6 +32,8 @@
#include "core/math/vector2.h" #include "core/math/vector2.h"
#include "core/vector.h"
/* /*
http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
*/ */

View File

@ -30,6 +30,8 @@
#include "vector2.h" #include "vector2.h"
#include "core/ustring.h"
real_t Vector2::angle() const { real_t Vector2::angle() const {
return Math::atan2(y, x); return Math::atan2(y, x);
} }
@ -150,25 +152,6 @@ Vector2 Vector2::limit_length(const real_t p_len) const {
return v; 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 p0 = p_pre_a;
Vector2 p1 = *this;
Vector2 p2 = p_b;
Vector2 p3 = p_post_b;
real_t t = p_weight;
real_t t2 = t * t;
real_t t3 = t2 * t;
Vector2 out;
out = 0.5f *
((p1 * 2) +
(-p0 + p2) * t +
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3 * p1 - 3 * p2 + p3) * t3);
return out;
}
Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const { Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const {
Vector2 v = *this; Vector2 v = *this;
Vector2 vd = p_to - v; Vector2 vd = p_to - v;
@ -199,3 +182,6 @@ 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); return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
} }
Vector2::operator String() const {
return "(" + String::num_real(x) + ", " + String::num_real(y) + ")";
}

View File

@ -32,7 +32,10 @@
/*************************************************************************/ /*************************************************************************/
#include "core/math/math_funcs.h" #include "core/math/math_funcs.h"
#include "core/ustring.h"
#include "core/error_macros.h"
class String;
struct _NO_DISCARD_CLASS_ Vector2 { struct _NO_DISCARD_CLASS_ Vector2 {
static const int AXIS_COUNT = 2; static const int AXIS_COUNT = 2;
@ -84,6 +87,15 @@ struct _NO_DISCARD_CLASS_ Vector2 {
real_t length() const; real_t length() const;
real_t length_squared() const; real_t length_squared() const;
Vector2 limit_length(const real_t p_len = 1.0) const;
Vector2 min(const Vector2 &p_vector2) const {
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
}
Vector2 max(const Vector2 &p_vector2) const {
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
}
real_t distance_to(const Vector2 &p_vector2) const; real_t distance_to(const Vector2 &p_vector2) const;
real_t distance_squared_to(const Vector2 &p_vector2) const; real_t distance_squared_to(const Vector2 &p_vector2) const;
@ -100,12 +112,13 @@ struct _NO_DISCARD_CLASS_ Vector2 {
Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const; Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
Vector2 clamped(real_t p_len) const; Vector2 clamped(real_t p_len) const;
Vector2 limit_length(const real_t p_len = 1.0) const;
_FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_weight); _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_weight);
_FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_to, real_t p_weight) const; _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector2 slerp(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 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
_FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const;
Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const; Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
Vector2 slide(const Vector2 &p_normal) const; Vector2 slide(const Vector2 &p_normal) const;
@ -153,7 +166,10 @@ struct _NO_DISCARD_CLASS_ Vector2 {
} }
Vector2 rotated(real_t p_by) const; Vector2 rotated(real_t p_by) const;
Vector2 tangent() const { _FORCE_INLINE_ Vector2 tangent() const {
return Vector2(y, -x);
}
_FORCE_INLINE_ Vector2 orthogonal() const {
return Vector2(y, -x); return Vector2(y, -x);
} }
@ -164,10 +180,7 @@ struct _NO_DISCARD_CLASS_ Vector2 {
Vector2 snapped(const Vector2 &p_by) const; Vector2 snapped(const Vector2 &p_by) const;
real_t aspect() const { return width / height; } real_t aspect() const { return width / height; }
//TODO operator String() const;
Vector2 orthogonal() { return Vector2(); }
operator String() const { return String::num(x) + ", " + String::num(y); }
_FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) { _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
x = p_x; x = p_x;
@ -252,6 +265,26 @@ Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
return rotated(theta * p_weight); return rotated(theta * p_weight);
} }
Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
Vector2 res = *this;
/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
}
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 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);
return res;
}
Vector2 Vector2::direction_to(const Vector2 &p_to) const { Vector2 Vector2::direction_to(const Vector2 &p_to) const {
Vector2 ret(p_to.x - x, p_to.y - y); Vector2 ret(p_to.x - x, p_to.y - y);
ret.normalize(); ret.normalize();

View File

@ -30,6 +30,23 @@
#include "vector2i.h" #include "vector2i.h"
#include "core/ustring.h"
Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
return Vector2i(
CLAMP(x, p_min.x, p_max.x),
CLAMP(y, p_min.y, p_max.y));
}
int64_t Vector2i::length_squared() const {
return x * (int64_t)x + y * (int64_t)y;
}
double Vector2i::length() const {
return Math::sqrt((double)length_squared());
}
Vector2i Vector2i::operator+(const Vector2i &p_v) const { Vector2i Vector2i::operator+(const Vector2i &p_v) const {
return Vector2i(x + p_v.x, y + p_v.y); return Vector2i(x + p_v.x, y + p_v.y);
} }
@ -80,3 +97,7 @@ bool Vector2i::operator==(const Vector2i &p_vec2) const {
bool Vector2i::operator!=(const Vector2i &p_vec2) const { bool Vector2i::operator!=(const Vector2i &p_vec2) const {
return x != p_vec2.x || y != p_vec2.y; return x != p_vec2.x || y != p_vec2.y;
} }
Vector2i::operator String() const {
return "(" + itos(x) + ", " + itos(y) + ")";
}

View File

@ -32,10 +32,12 @@
/*************************************************************************/ /*************************************************************************/
#include "core/math/math_funcs.h" #include "core/math/math_funcs.h"
#include "core/ustring.h" #include "core/error_macros.h"
#include "vector2.h" #include "vector2.h"
class String;
struct _NO_DISCARD_CLASS_ Vector2i { struct _NO_DISCARD_CLASS_ Vector2i {
enum Axis { enum Axis {
AXIS_X, AXIS_X,
@ -66,6 +68,26 @@ struct _NO_DISCARD_CLASS_ Vector2i {
return coord[p_idx]; return coord[p_idx];
} }
_FORCE_INLINE_ void set_all(int p_value) {
x = y = p_value;
}
_FORCE_INLINE_ int min_axis() const {
return x < y ? 0 : 1;
}
_FORCE_INLINE_ int max_axis() const {
return x < y ? 1 : 0;
}
Vector2i min(const Vector2i &p_vector2i) const {
return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
}
Vector2i max(const Vector2i &p_vector2i) const {
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
}
_FORCE_INLINE_ static Vector2i linear_interpolate(const Vector2i &p_a, const Vector2i &p_b, real_t p_weight); _FORCE_INLINE_ static Vector2i linear_interpolate(const Vector2i &p_a, const Vector2i &p_b, real_t p_weight);
_FORCE_INLINE_ Vector2i linear_interpolate(const Vector2i &p_to, real_t p_weight) const; _FORCE_INLINE_ Vector2i linear_interpolate(const Vector2i &p_to, real_t p_weight) const;
@ -93,13 +115,17 @@ struct _NO_DISCARD_CLASS_ Vector2i {
bool operator==(const Vector2i &p_vec2) const; bool operator==(const Vector2i &p_vec2) const;
bool operator!=(const Vector2i &p_vec2) const; bool operator!=(const Vector2i &p_vec2) const;
real_t get_aspect() const { return width / (real_t)height; } int64_t length_squared() const;
double length() const;
real_t aspect() const { return width / (real_t)height; }
Vector2i sign() const { return Vector2i(SGN(x), SGN(y)); }
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); } Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
Vector2 to_vector2() const { return Vector2(x, y); } Vector2 to_vector2() const { return Vector2(x, y); }
operator String() const { return String::num(x) + ", " + String::num(y); } operator String() const;
operator Vector2() const { return Vector2(x, y); } operator Vector2() const { return Vector2(x, y); }
inline Vector2i(const Vector2 &p_vec2) { inline Vector2i(const Vector2 &p_vec2) {

View File

@ -73,59 +73,6 @@ Vector3 Vector3::limit_length(const real_t p_len) const {
return v; return v;
} }
Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
Vector3 p0 = p_pre_a;
Vector3 p1 = *this;
Vector3 p2 = p_b;
Vector3 p3 = p_post_b;
{
//normalize
real_t ab = p0.distance_to(p1);
real_t bc = p1.distance_to(p2);
real_t cd = p2.distance_to(p3);
if (ab > 0) {
p0 = p1 + (p0 - p1) * (bc / ab);
}
if (cd > 0) {
p3 = p2 + (p3 - p2) * (bc / cd);
}
}
real_t t = p_weight;
real_t t2 = t * t;
real_t t3 = t2 * t;
Vector3 out;
out = 0.5f *
((p1 * 2) +
(-p0 + p2) * t +
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3 * p1 - 3 * p2 + p3) * t3);
return out;
}
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
Vector3 p0 = p_pre_a;
Vector3 p1 = *this;
Vector3 p2 = p_b;
Vector3 p3 = p_post_b;
real_t t = p_weight;
real_t t2 = t * t;
real_t t3 = t2 * t;
Vector3 out;
out = 0.5f *
((p1 * 2) +
(-p0 + p2) * t +
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3 * p1 - 3 * p2 + p3) * t3);
return out;
}
Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const { Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const {
Vector3 v = *this; Vector3 v = *this;
Vector3 vd = p_to - v; Vector3 vd = p_to - v;
@ -147,10 +94,17 @@ Basis Vector3::to_diagonal_matrix() const {
0, 0, z); 0, 0, z);
} }
Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const {
return Vector3(
CLAMP(x, p_min.x, p_max.x),
CLAMP(y, p_min.y, p_max.y),
CLAMP(z, p_min.z, p_max.z));
}
bool Vector3::is_equal_approx(const Vector3 &p_v) const { 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); 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 { Vector3::operator String() const {
return (rtos(x) + ", " + rtos(y) + ", " + rtos(z)); return "(" + String::num_real(x) + ", " + String::num_real(y) + ", " + String::num_real(z) + ")";
} }

View File

@ -100,8 +100,9 @@ struct _NO_DISCARD_CLASS_ Vector3 {
_FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_to, real_t p_weight) const; _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector3 slerp(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 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
Vector3 cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const; _FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const;
Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const; Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
_FORCE_INLINE_ Vector3 cross(const Vector3 &p_b) const; _FORCE_INLINE_ Vector3 cross(const Vector3 &p_b) const;
@ -114,6 +115,7 @@ struct _NO_DISCARD_CLASS_ Vector3 {
_FORCE_INLINE_ Vector3 sign() const; _FORCE_INLINE_ Vector3 sign() const;
_FORCE_INLINE_ Vector3 ceil() const; _FORCE_INLINE_ Vector3 ceil() const;
_FORCE_INLINE_ Vector3 round() const; _FORCE_INLINE_ Vector3 round() const;
Vector3 clamp(const Vector3 &p_min, const Vector3 &p_max) const;
_FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const; _FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
_FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const; _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
@ -132,6 +134,7 @@ struct _NO_DISCARD_CLASS_ Vector3 {
bool is_equal_approx(const Vector3 &p_v) const; bool is_equal_approx(const Vector3 &p_v) const;
inline bool is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const; inline bool is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const;
inline bool is_equal_approxt(const Vector3 &p_v, real_t p_tolerance) const;
/* Operators */ /* Operators */
@ -208,9 +211,47 @@ Vector3 Vector3::linear_interpolate(const Vector3 &p_to, real_t p_weight) const
z + (p_weight * (p_to.z - z))); 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); // This method seems more complicated than it really is, since we write out
return rotated(cross(p_to).normalized(), theta * p_weight); // the internals of some methods for efficiency (mainly, checking length).
real_t start_length_sq = length_squared();
real_t end_length_sq = p_to.length_squared();
if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
// Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
return linear_interpolate(p_to, p_weight);
}
Vector3 axis = cross(p_to);
real_t axis_length_sq = axis.length_squared();
if (unlikely(axis_length_sq == 0.0f)) {
// Colinear vectors have no rotation axis or angle between them, so the best we can do is lerp.
return linear_interpolate(p_to, p_weight);
}
axis /= Math::sqrt(axis_length_sq);
real_t start_length = Math::sqrt(start_length_sq);
real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
real_t angle = angle_to(p_to);
return rotated(axis, angle * p_weight) * (result_length / start_length);
}
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 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);
return res;
}
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
Vector3 res = *this;
/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
} }
real_t Vector3::distance_to(const Vector3 &p_to) const { real_t Vector3::distance_to(const Vector3 &p_to) const {
@ -458,4 +499,8 @@ bool Vector3::is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const {
return Math::is_equal_approx(x, p_v.x, p_tolerance) && Math::is_equal_approx(y, p_v.y, p_tolerance) && Math::is_equal_approx(z, p_v.z, p_tolerance); return Math::is_equal_approx(x, p_v.x, p_tolerance) && Math::is_equal_approx(y, p_v.y, p_tolerance) && Math::is_equal_approx(z, p_v.z, p_tolerance);
} }
bool Vector3::is_equal_approxt(const Vector3 &p_v, real_t p_tolerance) const {
return Math::is_equal_approx(x, p_v.x, p_tolerance) && Math::is_equal_approx(y, p_v.y, p_tolerance) && Math::is_equal_approx(z, p_v.z, p_tolerance);
}
#endif // VECTOR3_H #endif // VECTOR3_H

View File

@ -43,11 +43,11 @@ int32_t Vector3i::get_axis(const int p_axis) const {
return operator[](p_axis); return operator[](p_axis);
} }
Vector3i::Axis Vector3i::min_axis_index() const { Vector3i::Axis Vector3i::min_axis() const {
return x < y ? (x < z ? Vector3i::AXIS_X : Vector3i::AXIS_Z) : (y < z ? Vector3i::AXIS_Y : Vector3i::AXIS_Z); return x < y ? (x < z ? Vector3i::AXIS_X : Vector3i::AXIS_Z) : (y < z ? Vector3i::AXIS_Y : Vector3i::AXIS_Z);
} }
Vector3i::Axis Vector3i::max_axis_index() const { Vector3i::Axis Vector3i::max_axis() const {
return x < y ? (y < z ? Vector3i::AXIS_Z : Vector3i::AXIS_Y) : (x < z ? Vector3i::AXIS_Z : Vector3i::AXIS_X); return x < y ? (y < z ? Vector3i::AXIS_Z : Vector3i::AXIS_Y) : (x < z ? Vector3i::AXIS_Z : Vector3i::AXIS_X);
} }

View File

@ -51,7 +51,7 @@ struct _NO_DISCARD_CLASS_ Vector3i {
int32_t z; int32_t z;
}; };
int32_t coord[3] = { 0 }; int32_t coord[3];
}; };
_FORCE_INLINE_ const int32_t &operator[](const int p_axis) const { _FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
@ -67,8 +67,8 @@ struct _NO_DISCARD_CLASS_ Vector3i {
void set_axis(const int p_axis, 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; int32_t get_axis(const int p_axis) const;
Vector3i::Axis min_axis_index() const; Vector3i::Axis min_axis() const;
Vector3i::Axis max_axis_index() const; Vector3i::Axis max_axis() const;
_FORCE_INLINE_ int64_t length_squared() const; _FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const; _FORCE_INLINE_ double length() const;
@ -113,7 +113,11 @@ struct _NO_DISCARD_CLASS_ Vector3i {
operator String() const; operator String() const;
operator Vector3() const; operator Vector3() const;
_FORCE_INLINE_ Vector3i() {} _FORCE_INLINE_ Vector3i() {
x = 0;
y = 0;
z = 0;
}
_FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const 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; x = p_x;
y = p_y; y = p_y;

View File

@ -43,7 +43,7 @@ real_t Vector4::get_axis(const int p_axis) const {
return operator[](p_axis); return operator[](p_axis);
} }
Vector4::Axis Vector4::min_axis_index() const { Vector4::Axis Vector4::min_axis() const {
uint32_t min_index = 0; uint32_t min_index = 0;
real_t min_value = x; real_t min_value = x;
for (uint32_t i = 1; i < 4; i++) { for (uint32_t i = 1; i < 4; i++) {
@ -55,7 +55,7 @@ Vector4::Axis Vector4::min_axis_index() const {
return Vector4::Axis(min_index); return Vector4::Axis(min_index);
} }
Vector4::Axis Vector4::max_axis_index() const { Vector4::Axis Vector4::max_axis() const {
uint32_t max_index = 0; uint32_t max_index = 0;
real_t max_value = x; real_t max_value = x;
for (uint32_t i = 1; i < 4; i++) { for (uint32_t i = 1; i < 4; i++) {
@ -97,6 +97,10 @@ Vector4 Vector4::direction_to(const Vector4 &p_to) const {
return ret; return ret;
} }
real_t Vector4::distance_squared_to(const Vector4 &p_to) const {
return (p_to - *this).length_squared();
}
Vector4 Vector4::abs() const { Vector4 Vector4::abs() const {
return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w)); return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
} }
@ -125,7 +129,6 @@ Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const {
w + (p_weight * (p_to.w - w))); w + (p_weight * (p_to.w - 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 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; Vector4 res = *this;
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight); res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
@ -134,7 +137,6 @@ Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, c
res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight); res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight);
return res; return res;
} }
*/
Vector4 Vector4::posmod(const real_t p_mod) const { 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)); return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod));
@ -144,22 +146,18 @@ 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)); 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) { void Vector4::snap(const Vector4 &p_step) {
x = Math::snapped(x, p_step.x); x = Math::stepify(x, p_step.x);
y = Math::snapped(y, p_step.y); y = Math::stepify(y, p_step.y);
z = Math::snapped(z, p_step.z); z = Math::stepify(z, p_step.z);
w = Math::snapped(w, p_step.w); w = Math::stepify(w, p_step.w);
} }
*/
/*
Vector4 Vector4::snapped(const Vector4 &p_step) const { Vector4 Vector4::snapped(const Vector4 &p_step) const {
Vector4 v = *this; Vector4 v = *this;
v.snap(p_step); v.snap(p_step);
return v; return v;
} }
*/
Vector4 Vector4::inverse() const { Vector4 Vector4::inverse() const {
return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w); return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);

View File

@ -51,7 +51,7 @@ struct _NO_DISCARD_CLASS_ Vector4 {
real_t z; real_t z;
real_t w; real_t w;
}; };
real_t components[4] = { 0, 0, 0, 0 }; real_t components[4];
}; };
_FORCE_INLINE_ real_t &operator[](const int p_axis) { _FORCE_INLINE_ real_t &operator[](const int p_axis) {
@ -68,8 +68,8 @@ struct _NO_DISCARD_CLASS_ Vector4 {
void set_axis(const int p_axis, 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; real_t get_axis(const int p_axis) const;
Vector4::Axis min_axis_index() const; Vector4::Axis min_axis() const;
Vector4::Axis max_axis_index() const; Vector4::Axis max_axis() const;
_FORCE_INLINE_ real_t length_squared() const; _FORCE_INLINE_ real_t length_squared() const;
bool is_equal_approx(const Vector4 &p_vec4) const; bool is_equal_approx(const Vector4 &p_vec4) const;
@ -79,6 +79,7 @@ struct _NO_DISCARD_CLASS_ Vector4 {
bool is_normalized() const; bool is_normalized() const;
real_t distance_to(const Vector4 &p_to) const; real_t distance_to(const Vector4 &p_to) const;
real_t distance_squared_to(const Vector4 &p_to) const;
Vector4 direction_to(const Vector4 &p_to) const; Vector4 direction_to(const Vector4 &p_to) const;
Vector4 abs() const; Vector4 abs() const;
@ -86,13 +87,14 @@ struct _NO_DISCARD_CLASS_ Vector4 {
Vector4 floor() const; Vector4 floor() const;
Vector4 ceil() const; Vector4 ceil() const;
Vector4 round() const; Vector4 round() const;
Vector4 lerp(const Vector4 &p_to, const real_t p_weight) 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 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; Vector4 posmod(const real_t p_mod) const;
Vector4 posmodv(const Vector4 &p_modv) const; Vector4 posmodv(const Vector4 &p_modv) const;
//void snap(const Vector4 &p_step); void snap(const Vector4 &p_step);
//Vector4 snapped(const Vector4 &p_step) const; Vector4 snapped(const Vector4 &p_step) const;
Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const; Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const;
Vector4 inverse() const; Vector4 inverse() const;
@ -121,7 +123,12 @@ struct _NO_DISCARD_CLASS_ Vector4 {
operator String() const; operator String() const;
_FORCE_INLINE_ Vector4() {} _FORCE_INLINE_ Vector4() {
x = 0;
y = 0;
z = 0;
w = 0;
}
_FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) : _FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
x(p_x), x(p_x),

View File

@ -43,7 +43,7 @@ int32_t Vector4i::get_axis(const int p_axis) const {
return operator[](p_axis); return operator[](p_axis);
} }
Vector4i::Axis Vector4i::min_axis_index() const { Vector4i::Axis Vector4i::min_axis() const {
uint32_t min_index = 0; uint32_t min_index = 0;
int32_t min_value = x; int32_t min_value = x;
for (uint32_t i = 1; i < 4; i++) { for (uint32_t i = 1; i < 4; i++) {
@ -55,7 +55,7 @@ Vector4i::Axis Vector4i::min_axis_index() const {
return Vector4i::Axis(min_index); return Vector4i::Axis(min_index);
} }
Vector4i::Axis Vector4i::max_axis_index() const { Vector4i::Axis Vector4i::max_axis() const {
uint32_t max_index = 0; uint32_t max_index = 0;
int32_t max_value = x; int32_t max_value = x;
for (uint32_t i = 1; i < 4; i++) { for (uint32_t i = 1; i < 4; i++) {

View File

@ -53,7 +53,7 @@ struct _NO_DISCARD_CLASS_ Vector4i {
int32_t w; int32_t w;
}; };
int32_t coord[4] = { 0 }; int32_t coord[4];
}; };
_FORCE_INLINE_ const int32_t &operator[](const int p_axis) const { _FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
@ -69,8 +69,8 @@ struct _NO_DISCARD_CLASS_ Vector4i {
void set_axis(const int p_axis, 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; int32_t get_axis(const int p_axis) const;
Vector4i::Axis min_axis_index() const; Vector4i::Axis min_axis() const;
Vector4i::Axis max_axis_index() const; Vector4i::Axis max_axis() const;
_FORCE_INLINE_ int64_t length_squared() const; _FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const; _FORCE_INLINE_ double length() const;
@ -113,8 +113,15 @@ struct _NO_DISCARD_CLASS_ Vector4i {
operator String() const; operator String() const;
operator Vector4() const; operator Vector4() const;
_FORCE_INLINE_ Vector4i() {} _FORCE_INLINE_ Vector4i() {
x = 0;
y = 0;
z = 0;
w = 0;
}
Vector4i(const Vector4 &p_vec4); Vector4i(const Vector4 &p_vec4);
_FORCE_INLINE_ Vector4i(const int32_t p_x, const int32_t p_y, const int32_t p_z, const int32_t p_w) { _FORCE_INLINE_ Vector4i(const int32_t p_x, const int32_t p_y, const int32_t p_z, const int32_t p_w) {
x = p_x; x = p_x;
y = p_y; y = p_y;

View File

@ -554,8 +554,8 @@ struct _VariantCall {
VCALL_LOCALMEM1R(Vector3, limit_length); VCALL_LOCALMEM1R(Vector3, limit_length);
VCALL_LOCALMEM0R(Vector3, sign); VCALL_LOCALMEM0R(Vector3, sign);
VCALL_LOCALMEM0R(Vector3i, min_axis_index); VCALL_LOCALMEM0R(Vector3i, min_axis);
VCALL_LOCALMEM0R(Vector3i, max_axis_index); VCALL_LOCALMEM0R(Vector3i, max_axis);
VCALL_LOCALMEM0R(Vector3i, length); VCALL_LOCALMEM0R(Vector3i, length);
VCALL_LOCALMEM0R(Vector3i, length_squared); VCALL_LOCALMEM0R(Vector3i, length_squared);
VCALL_LOCALMEM2R(Vector3i, linear_interpolate); VCALL_LOCALMEM2R(Vector3i, linear_interpolate);
@ -2358,8 +2358,8 @@ void register_variant_methods() {
ADDFUNC1R(VECTOR3, VECTOR3, Vector3, limit_length, REAL, "length", varray(1.0)); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, limit_length, REAL, "length", varray(1.0));
ADDFUNC0R(VECTOR3, VECTOR3, Vector3, sign, varray()); ADDFUNC0R(VECTOR3, VECTOR3, Vector3, sign, varray());
ADDFUNC0R(VECTOR3I, INT, Vector3i, min_axis_index, varray()); ADDFUNC0R(VECTOR3I, INT, Vector3i, min_axis, varray());
ADDFUNC0R(VECTOR3I, INT, Vector3i, max_axis_index, varray()); ADDFUNC0R(VECTOR3I, INT, Vector3i, max_axis, varray());
ADDFUNC0R(VECTOR3I, REAL, Vector3, length, varray()); ADDFUNC0R(VECTOR3I, REAL, Vector3, length, varray());
ADDFUNC0R(VECTOR3I, REAL, Vector3, length_squared, varray()); ADDFUNC0R(VECTOR3I, REAL, Vector3, length_squared, varray());
ADDFUNC2R(VECTOR3I, VECTOR3I, Vector3, linear_interpolate, VECTOR3I, "to", REAL, "weight", varray()); ADDFUNC2R(VECTOR3I, VECTOR3I, Vector3, linear_interpolate, VECTOR3I, "to", REAL, "weight", varray());