Merge pull request #63463 from KoBeWi/Vector5

Add some missing Vector4 methods
This commit is contained in:
Rémi Verschelde 2022-07-27 10:45:39 +02:00 committed by GitHub
commit 1c57d90e85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 148 additions and 6 deletions

View file

@ -50,7 +50,7 @@ Vector4 Vector4::normalized() const {
}
bool Vector4::is_normalized() const {
return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon
return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // Use less epsilon.
}
Vector4 Vector4::abs() const {
@ -61,6 +61,26 @@ Vector4 Vector4::sign() const {
return Vector4(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
}
Vector4 Vector4::floor() const {
return Vector4(Math::floor(x), Math::floor(y), Math::floor(z), Math::floor(w));
}
Vector4 Vector4::ceil() const {
return Vector4(Math::ceil(x), Math::ceil(y), Math::ceil(z), Math::ceil(w));
}
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 {
return Vector4(
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 Vector4::inverse() const {
return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);
}

View file

@ -54,11 +54,13 @@ struct _NO_DISCARD_ Vector4 {
real_t components[4] = { 0, 0, 0, 0 };
};
_FORCE_INLINE_ real_t &operator[](int idx) {
return components[idx];
_FORCE_INLINE_ real_t &operator[](const int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return components[p_axis];
}
_FORCE_INLINE_ const real_t &operator[](int idx) const {
return components[idx];
_FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return components[p_axis];
}
_FORCE_INLINE_ real_t length_squared() const;
bool is_equal_approx(const Vector4 &p_vec4) const;
@ -66,8 +68,13 @@ struct _NO_DISCARD_ Vector4 {
void normalize();
Vector4 normalized() const;
bool is_normalized() 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::Axis min_axis_index() const;
Vector4::Axis max_axis_index() const;

View file

@ -1731,8 +1731,12 @@ static void _register_variant_builtin_methods() {
bind_method(Vector4, max_axis_index, sarray(), varray());
bind_method(Vector4, length, sarray(), varray());
bind_method(Vector4, length_squared, sarray(), varray());
bind_method(Vector4, sign, sarray(), varray());
bind_method(Vector4, abs, sarray(), varray());
bind_method(Vector4, sign, sarray(), varray());
bind_method(Vector4, floor, sarray(), varray());
bind_method(Vector4, ceil, sarray(), varray());
bind_method(Vector4, round, sarray(), varray());
bind_method(Vector4, lerp, sarray("to", "weight"), varray());
bind_method(Vector4, clamp, sarray("min", "max"), varray());
bind_method(Vector4, normalized, sarray(), varray());
bind_method(Vector4, is_normalized, sarray(), varray());

View file

@ -819,6 +819,8 @@ INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector2, double, real_t, 2)
INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector2i, int64_t, int32_t, 2)
INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector3, double, real_t, 3)
INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector3i, int64_t, int32_t, 3)
INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector4, double, real_t, 4)
INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector4i, int64_t, int32_t, 4)
INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Quaternion, double, real_t, 4)
INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Color, double, float, 4)
@ -883,6 +885,8 @@ void register_indexed_setters_getters() {
REGISTER_INDEXED_MEMBER(Vector2i);
REGISTER_INDEXED_MEMBER(Vector3);
REGISTER_INDEXED_MEMBER(Vector3i);
REGISTER_INDEXED_MEMBER(Vector4);
REGISTER_INDEXED_MEMBER(Vector4i);
REGISTER_INDEXED_MEMBER(Quaternion);
REGISTER_INDEXED_MEMBER(Color);
REGISTER_INDEXED_MEMBER(Transform2D);

View file

@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="Vector4" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Vector used for 4D math using floating point coordinates.
</brief_description>
<description>
4-element structure that can be used to represent any quadruplet of numeric values.
It uses floating-point coordinates. See [Vector4i] for its integer counterpart.
[b]Note:[/b] In a boolean context, a Vector4 will evaluate to [code]false[/code] if it's equal to [code]Vector4(0, 0, 0, 0)[/code]. Otherwise, a Vector4 will always evaluate to [code]true[/code].
</description>
<tutorials>
</tutorials>
@ -10,18 +14,21 @@
<constructor name="Vector4">
<return type="Vector4" />
<description>
Constructs a default-initialized [Vector4] with all components set to [code]0[/code].
</description>
</constructor>
<constructor name="Vector4">
<return type="Vector4" />
<argument index="0" name="from" type="Vector4" />
<description>
Constructs a [Vector4] as a copy of the given [Vector4].
</description>
</constructor>
<constructor name="Vector4">
<return type="Vector4" />
<argument index="0" name="from" type="Vector4i" />
<description>
Constructs a new [Vector4] from [Vector4i].
</description>
</constructor>
<constructor name="Vector4">
@ -31,6 +38,7 @@
<argument index="2" name="z" type="float" />
<argument index="3" name="w" type="float" />
<description>
Returns a [Vector4] with the given components.
</description>
</constructor>
</constructors>
@ -38,6 +46,13 @@
<method name="abs" qualifiers="const">
<return type="Vector4" />
<description>
Returns a new vector with all components in absolute values (i.e. positive).
</description>
</method>
<method name="ceil" qualifiers="const">
<return type="Vector4" />
<description>
Returns a new vector with all components rounded up (towards positive infinity).
</description>
</method>
<method name="clamp" qualifiers="const">
@ -45,85 +60,127 @@
<argument index="0" name="min" type="Vector4" />
<argument index="1" name="max" type="Vector4" />
<description>
Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
</description>
</method>
<method name="dot" qualifiers="const">
<return type="float" />
<argument index="0" name="with" type="Vector4" />
<description>
Returns the dot product of this vector and [code]with[/code].
</description>
</method>
<method name="floor" qualifiers="const">
<return type="Vector4" />
<description>
Returns a new vector with all components rounded down (towards negative infinity).
</description>
</method>
<method name="inverse" qualifiers="const">
<return type="Vector4" />
<description>
Returns the inverse of the vector. This is the same as [code]Vector4(1.0 / v.x, 1.0 / v.y, 1.0 / v.z, 1.0 / v.w)[/code].
</description>
</method>
<method name="is_equal_approx" qualifiers="const">
<return type="bool" />
<argument index="0" name="with" type="Vector4" />
<description>
Returns [code]true[/code] if this vector and [code]v[/code] are approximately equal, by running [method @GlobalScope.is_equal_approx] on each component.
</description>
</method>
<method name="is_normalized" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if the vector is normalized, i.e. its length is equal to 1.
</description>
</method>
<method name="length" qualifiers="const">
<return type="float" />
<description>
Returns the length (magnitude) of this vector.
</description>
</method>
<method name="length_squared" qualifiers="const">
<return type="float" />
<description>
Returns the squared length (squared magnitude) of this vector. This method runs faster than [method length].
</description>
</method>
<method name="lerp" qualifiers="const">
<return type="Vector4" />
<argument index="0" name="to" type="Vector4" />
<argument index="1" name="weight" type="float" />
<description>
Returns the result of the linear interpolation between this vector and [code]to[/code] by amount [code]weight[/code]. [code]weight[/code] is on the range of [code]0.0[/code] to [code]1.0[/code], representing the amount of interpolation.
</description>
</method>
<method name="max_axis_index" qualifiers="const">
<return type="int" />
<description>
Returns the axis of the vector's highest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_X].
</description>
</method>
<method name="min_axis_index" qualifiers="const">
<return type="int" />
<description>
Returns the axis of the vector's lowest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_W].
</description>
</method>
<method name="normalized" qualifiers="const">
<return type="Vector4" />
<description>
Returns the vector scaled to unit length. Equivalent to [code]v / v.length()[/code].
</description>
</method>
<method name="round" qualifiers="const">
<return type="Vector4" />
<description>
Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
</description>
</method>
<method name="sign" qualifiers="const">
<return type="Vector4" />
<description>
Returns a new vector with each component set to one or negative one, depending on the signs of the components, or zero if the component is zero, by calling [method @GlobalScope.sign] on each component.
</description>
</method>
</methods>
<members>
<member name="w" type="float" setter="" getter="" default="0.0">
The vector's W component. Also accessible by using the index position [code][3][/code].
</member>
<member name="x" type="float" setter="" getter="" default="0.0">
The vector's X component. Also accessible by using the index position [code][0][/code].
</member>
<member name="y" type="float" setter="" getter="" default="0.0">
The vector's Y component. Also accessible by using the index position [code][1][/code].
</member>
<member name="z" type="float" setter="" getter="" default="0.0">
The vector's Z component. Also accessible by using the index position [code][2][/code].
</member>
</members>
<constants>
<constant name="AXIS_X" value="0">
Enumerated value for the X axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="AXIS_Y" value="1">
Enumerated value for the Y axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="AXIS_Z" value="2">
Enumerated value for the Z axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="AXIS_W" value="3">
Enumerated value for the W axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="ZERO" value="Vector4(0, 0, 0)">
Zero vector, a vector with all components set to [code]0[/code].
</constant>
<constant name="ONE" value="Vector4(1, 1, 1)">
One vector, a vector with all components set to [code]1[/code].
</constant>
<constant name="INF" value="Vector4(inf, inf, inf)">
Infinity vector, a vector with all components set to [constant @GDScript.INF].
</constant>
</constants>
<operators>
@ -131,100 +188,144 @@
<return type="bool" />
<argument index="0" name="right" type="Vector4" />
<description>
Returns [code]true[/code] if the vectors are not equal.
[b]Note:[/b] Due to floating-point precision errors, consider using [method is_equal_approx] instead, which is more reliable.
</description>
</operator>
<operator name="operator *">
<return type="Vector4" />
<argument index="0" name="right" type="Projection" />
<description>
Inversely transforms (multiplies) the [Vector4] by the given [Projection] matrix.
</description>
</operator>
<operator name="operator *">
<return type="Vector4" />
<argument index="0" name="right" type="Vector4" />
<description>
Multiplies each component of the [Vector4] by the components of the given [Vector4].
[codeblock]
print(Vector4(10, 20, 30, 40) * Vector4(3, 4, 5, 6)) # Prints "(30, 80, 150, 240)"
[/codeblock]
</description>
</operator>
<operator name="operator *">
<return type="Vector4" />
<argument index="0" name="right" type="float" />
<description>
Multiplies each component of the [Vector4] by the given [float].
[codeblock]
print(Vector4(10, 20, 30, 40) * 2) # Prints "(20, 40, 60, 80)"
[/codeblock]
</description>
</operator>
<operator name="operator *">
<return type="Vector4" />
<argument index="0" name="right" type="int" />
<description>
Multiplies each component of the [Vector4] by the given [int].
</description>
</operator>
<operator name="operator +">
<return type="Vector4" />
<argument index="0" name="right" type="Vector4" />
<description>
Adds each component of the [Vector4] by the components of the given [Vector4].
[codeblock]
print(Vector4(10, 20, 30, 40) + Vector4(3, 4, 5, 6)) # Prints "(13, 24, 35, 46)"
[/codeblock]
</description>
</operator>
<operator name="operator -">
<return type="Vector4" />
<argument index="0" name="right" type="Vector4" />
<description>
Subtracts each component of the [Vector4] by the components of the given [Vector4].
[codeblock]
print(Vector4(10, 20, 30, 40) - Vector4(3, 4, 5, 6)) # Prints "(7, 16, 25, 34)"
[/codeblock]
</description>
</operator>
<operator name="operator /">
<return type="Vector4" />
<argument index="0" name="right" type="Vector4" />
<description>
Divides each component of the [Vector4] by the components of the given [Vector4].
[codeblock]
print(Vector4(10, 20, 30, 40) / Vector4(2, 5, 3, 4)) # Prints "(5, 4, 10, 10)"
[/codeblock]
</description>
</operator>
<operator name="operator /">
<return type="Vector4" />
<argument index="0" name="right" type="float" />
<description>
Divides each component of the [Vector4] by the given [float].
[codeblock]
print(Vector4(10, 20, 30, 40) / 2 # Prints "(5, 10, 15, 20)"
[/codeblock]
</description>
</operator>
<operator name="operator /">
<return type="Vector4" />
<argument index="0" name="right" type="int" />
<description>
Divides each component of the [Vector4] by the given [int].
</description>
</operator>
<operator name="operator &lt;">
<return type="bool" />
<argument index="0" name="right" type="Vector4" />
<description>
Compares two [Vector4] vectors by first checking if the X value of the left vector is less than the X value of the [code]right[/code] vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.
</description>
</operator>
<operator name="operator &lt;=">
<return type="bool" />
<argument index="0" name="right" type="Vector4" />
<description>
Compares two [Vector4] vectors by first checking if the X value of the left vector is less than or equal to the X value of the [code]right[/code] vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.
</description>
</operator>
<operator name="operator ==">
<return type="bool" />
<argument index="0" name="right" type="Vector4" />
<description>
Returns [code]true[/code] if the vectors are exactly equal.
[b]Note:[/b] Due to floating-point precision errors, consider using [method is_equal_approx] instead, which is more reliable.
</description>
</operator>
<operator name="operator &gt;">
<return type="bool" />
<argument index="0" name="right" type="Vector4" />
<description>
Compares two [Vector4] vectors by first checking if the X value of the left vector is greater than the X value of the [code]right[/code] vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.
</description>
</operator>
<operator name="operator &gt;=">
<return type="bool" />
<argument index="0" name="right" type="Vector4" />
<description>
Access vector components using their index. [code]v[0][/code] is equivalent to [code]v.x[/code], [code]v[1][/code] is equivalent to [code]v.y[/code], and [code]v[2][/code] is equivalent to [code]v.z[/code].
</description>
</operator>
<operator name="operator []">
<return type="float" />
<argument index="0" name="index" type="int" />
<description>
Access vector components using their index. [code]v[0][/code] is equivalent to [code]v.x[/code], [code]v[1][/code] is equivalent to [code]v.y[/code], [code]v[2][/code] is equivalent to [code]v.z[/code], and [code]v[3][/code] is equivalent to [code]v.w[/code].
</description>
</operator>
<operator name="operator unary+">
<return type="Vector4" />
<description>
Returns the same value as if the [code]+[/code] was not there. Unary [code]+[/code] does nothing, but sometimes it can make your code more readable.
</description>
</operator>
<operator name="operator unary-">
<return type="Vector4" />
<description>
Returns the negative value of the [Vector4]. This is the same as writing [code]Vector4(-v.x, -v.y, -v.z, -v.w)[/code]. This operation flips the direction of the vector while keeping the same magnitude. With floats, the number zero can be either positive or negative.
</description>
</operator>
</operators>

View file

@ -194,6 +194,12 @@
<description>
</description>
</operator>
<operator name="operator []">
<return type="int" />
<argument index="0" name="index" type="int" />
<description>
</description>
</operator>
<operator name="operator unary+">
<return type="Vector4i" />
<description>