Skip to content

Commit

Permalink
Expose max_axis_index and max_axis_index for Vector2(i)
Browse files Browse the repository at this point in the history
Some cleanup with Vector3(i)'s methods so that it is consistent with Vector2, for example it returns enums internally (GDScript still gets ints).
  • Loading branch information
aaronfranke committed Dec 3, 2021
1 parent 543462e commit 24f5788
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 78 deletions.
4 changes: 2 additions & 2 deletions core/math/bvh_split.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void _split_leaf_sort_groups_simple(int &num_a, int &num_b, uint16_t *group_a, u

int order[Point::AXIS_COUNT];

order[0] = size.min_axis();
order[Point::AXIS_COUNT - 1] = size.max_axis();
order[0] = size.min_axis_index();
order[Point::AXIS_COUNT - 1] = size.max_axis_index();

static_assert(Point::AXIS_COUNT <= 3);
if (Point::AXIS_COUNT == 3) {
Expand Down
14 changes: 7 additions & 7 deletions core/math/convex_hull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,9 @@ class ConvexHullInternal {
PagedAllocator<Face> face_pool;
LocalVector<Vertex *> original_vertices;
int32_t merge_stamp = 0;
int32_t min_axis = 0;
int32_t med_axis = 0;
int32_t max_axis = 0;
Vector3::Axis min_axis = Vector3::Axis::AXIS_X;
Vector3::Axis med_axis = Vector3::Axis::AXIS_X;
Vector3::Axis max_axis = Vector3::Axis::AXIS_X;
int32_t used_edge_pairs = 0;
int32_t max_used_edge_pairs = 0;

Expand Down Expand Up @@ -1585,12 +1585,12 @@ void ConvexHullInternal::compute(const Vector3 *p_coords, int32_t p_count) {
}

Vector3 s = aabb.size;
max_axis = s.max_axis();
min_axis = s.min_axis();
max_axis = s.max_axis_index();
min_axis = s.min_axis_index();
if (min_axis == max_axis) {
min_axis = (max_axis + 1) % 3;
min_axis = Vector3::Axis((max_axis + 1) % 3);
}
med_axis = 3 - max_axis - min_axis;
med_axis = Vector3::Axis(3 - max_axis - min_axis);

s /= real_t(10216);
if (((med_axis + 1) % 3) != max_axis) {
Expand Down
16 changes: 8 additions & 8 deletions core/math/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ struct Vector2 {
x = y = p_value;
}

_FORCE_INLINE_ int min_axis() const {
return x < y ? 0 : 1;
_FORCE_INLINE_ Vector2::Axis min_axis_index() const {
return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y;
}

_FORCE_INLINE_ int max_axis() const {
return x < y ? 1 : 0;
_FORCE_INLINE_ Vector2::Axis max_axis_index() const {
return x < y ? Vector2::AXIS_Y : Vector2::AXIS_X;
}

void normalize();
Expand Down Expand Up @@ -301,12 +301,12 @@ struct Vector2i {
return p_idx ? y : x;
}

_FORCE_INLINE_ int min_axis() const {
return x < y ? 0 : 1;
_FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
return x < y ? Vector2i::AXIS_X : Vector2i::AXIS_Y;
}

_FORCE_INLINE_ int max_axis() const {
return x < y ? 1 : 0;
_FORCE_INLINE_ Vector2i::Axis max_axis_index() const {
return x < y ? Vector2i::AXIS_Y : Vector2i::AXIS_X;
}

Vector2i min(const Vector2i &p_vector2i) const {
Expand Down
8 changes: 4 additions & 4 deletions core/math/vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ struct Vector3 {
x = y = z = p_value;
}

_FORCE_INLINE_ int min_axis() const {
return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
_FORCE_INLINE_ Vector3::Axis min_axis_index() const {
return x < y ? (x < z ? Vector3::AXIS_X : Vector3::AXIS_Z) : (y < z ? Vector3::AXIS_Y : Vector3::AXIS_Z);
}

_FORCE_INLINE_ int max_axis() const {
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
_FORCE_INLINE_ Vector3::Axis max_axis_index() const {
return x < y ? (y < z ? Vector3::AXIS_Z : Vector3::AXIS_Y) : (x < z ? Vector3::AXIS_Z : Vector3::AXIS_X);
}

_FORCE_INLINE_ real_t length() const;
Expand Down
8 changes: 4 additions & 4 deletions core/math/vector3i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ int32_t Vector3i::get_axis(const int p_axis) const {
return operator[](p_axis);
}

int Vector3i::min_axis() const {
return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
Vector3i::Axis Vector3i::min_axis_index() const {
return x < y ? (x < z ? Vector3i::AXIS_X : Vector3i::AXIS_Z) : (y < z ? Vector3i::AXIS_Y : Vector3i::AXIS_Z);
}

int Vector3i::max_axis() const {
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
Vector3i::Axis Vector3i::max_axis_index() const {
return x < y ? (y < z ? Vector3i::AXIS_Z : Vector3i::AXIS_Y) : (x < z ? Vector3i::AXIS_Z : Vector3i::AXIS_X);
}

Vector3i Vector3i::clamp(const Vector3i &p_min, const Vector3i &p_max) const {
Expand Down
4 changes: 2 additions & 2 deletions core/math/vector3i.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ struct Vector3i {
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;
Vector3i::Axis min_axis_index() const;
Vector3i::Axis max_axis_index() const;

_FORCE_INLINE_ void zero();

Expand Down
3 changes: 3 additions & 0 deletions core/variant/binder_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ struct VariantCaster<const T &> {
// Object enum casts must go here
VARIANT_ENUM_CAST(Object::ConnectFlags);

VARIANT_ENUM_CAST(Vector2::Axis);
VARIANT_ENUM_CAST(Vector2i::Axis);
VARIANT_ENUM_CAST(Vector3::Axis);
VARIANT_ENUM_CAST(Vector3i::Axis);
VARIANT_ENUM_CAST(Basis::EulerOrder);

VARIANT_ENUM_CAST(Error);
Expand Down
12 changes: 8 additions & 4 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector2, lerp, sarray("to", "weight"), varray());
bind_method(Vector2, slerp, sarray("to", "weight"), varray());
bind_method(Vector2, cubic_interpolate, sarray("b", "pre_a", "post_b", "weight"), varray());
bind_method(Vector2, max_axis_index, sarray(), varray());
bind_method(Vector2, min_axis_index, sarray(), varray());
bind_method(Vector2, move_toward, sarray("to", "delta"), varray());
bind_method(Vector2, rotated, sarray("phi"), varray());
bind_method(Vector2, orthogonal, sarray(), varray());
Expand All @@ -1508,6 +1510,8 @@ static void _register_variant_builtin_methods() {
/* Vector2i */

bind_method(Vector2i, aspect, sarray(), varray());
bind_method(Vector2i, max_axis_index, sarray(), varray());
bind_method(Vector2i, min_axis_index, sarray(), varray());
bind_method(Vector2i, sign, sarray(), varray());
bind_method(Vector2i, abs, sarray(), varray());
bind_method(Vector2i, clamp, sarray("min", "max"), varray());
Expand Down Expand Up @@ -1547,8 +1551,8 @@ static void _register_variant_builtin_methods() {

/* Vector3 */

bind_method(Vector3, min_axis, sarray(), varray());
bind_method(Vector3, max_axis, sarray(), varray());
bind_method(Vector3, min_axis_index, sarray(), varray());
bind_method(Vector3, max_axis_index, sarray(), varray());
bind_method(Vector3, angle_to, sarray("to"), varray());
bind_method(Vector3, signed_angle_to, sarray("to", "axis"), varray());
bind_method(Vector3, direction_to, sarray("to"), varray());
Expand Down Expand Up @@ -1587,8 +1591,8 @@ static void _register_variant_builtin_methods() {

/* Vector3i */

bind_method(Vector3i, min_axis, sarray(), varray());
bind_method(Vector3i, max_axis, sarray(), varray());
bind_method(Vector3i, min_axis_index, sarray(), varray());
bind_method(Vector3i, max_axis_index, sarray(), varray());
bind_method(Vector3i, sign, sarray(), varray());
bind_method(Vector3i, abs, sarray(), varray());
bind_method(Vector3i, clamp, sarray("min", "max"), varray());
Expand Down
10 changes: 10 additions & 0 deletions core/variant/variant_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,14 @@ VARIANT_ACCESSOR_NUMBER(uint32_t)
VARIANT_ACCESSOR_NUMBER(int64_t)
VARIANT_ACCESSOR_NUMBER(uint64_t)
VARIANT_ACCESSOR_NUMBER(char32_t)

// Bind enums to allow using them as return types.
VARIANT_ACCESSOR_NUMBER(Error)
VARIANT_ACCESSOR_NUMBER(Side)
VARIANT_ACCESSOR_NUMBER(Vector2::Axis)
VARIANT_ACCESSOR_NUMBER(Vector2i::Axis)
VARIANT_ACCESSOR_NUMBER(Vector3::Axis)
VARIANT_ACCESSOR_NUMBER(Vector3i::Axis)

template <>
struct VariantInternalAccessor<Basis::EulerOrder> {
Expand Down Expand Up @@ -1020,6 +1026,10 @@ INITIALIZER_INT(int64_t)
INITIALIZER_INT(char32_t)
INITIALIZER_INT(Error)
INITIALIZER_INT(ObjectID)
INITIALIZER_INT(Vector2::Axis)
INITIALIZER_INT(Vector2i::Axis)
INITIALIZER_INT(Vector3::Axis)
INITIALIZER_INT(Vector3i::Axis)

template <>
struct VariantInitializer<double> {
Expand Down
16 changes: 14 additions & 2 deletions doc/classes/Vector2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@
Returns the vector with a maximum length by limiting its length to [code]length[/code].
</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_Y].
</description>
</method>
<method name="move_toward" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="to" type="Vector2" />
Expand Down Expand Up @@ -315,10 +327,10 @@
</members>
<constants>
<constant name="AXIS_X" value="0">
Enumerated value for the X axis.
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.
Enumerated value for the Y axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="ZERO" value="Vector2(0, 0)">
Zero vector, a vector with all components set to [code]0[/code].
Expand Down
16 changes: 14 additions & 2 deletions doc/classes/Vector2i.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@
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="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_Y].
</description>
</method>
<method name="sign" qualifiers="const">
<return type="Vector2i" />
<description>
Expand All @@ -81,10 +93,10 @@
</members>
<constants>
<constant name="AXIS_X" value="0">
Enumerated value for the X axis.
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.
Enumerated value for the Y axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="ZERO" value="Vector2i(0, 0)">
Zero vector, a vector with all components set to [code]0[/code].
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/Vector3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,16 @@
Returns the vector with a maximum length by limiting its length to [code]length[/code].
</description>
</method>
<method name="max_axis" qualifiers="const">
<method name="max_axis_index" qualifiers="const">
<return type="int" />
<description>
Returns the axis of the vector's largest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_X].
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" qualifiers="const">
<method name="min_axis_index" qualifiers="const">
<return type="int" />
<description>
Returns the axis of the vector's smallest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_Z].
Returns the axis of the vector's lowest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_Z].
</description>
</method>
<method name="move_toward" qualifiers="const">
Expand Down Expand Up @@ -321,13 +321,13 @@
</members>
<constants>
<constant name="AXIS_X" value="0">
Enumerated value for the X axis. Returned by [method max_axis] and [method min_axis].
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] and [method min_axis].
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] and [method min_axis].
Enumerated value for the Z axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="ZERO" value="Vector3(0, 0, 0)">
Zero vector, a vector with all components set to [code]0[/code].
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/Vector3i.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@
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="max_axis" qualifiers="const">
<method name="max_axis_index" qualifiers="const">
<return type="int" />
<description>
Returns the axis of the vector's largest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_X].
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" qualifiers="const">
<method name="min_axis_index" qualifiers="const">
<return type="int" />
<description>
Returns the axis of the vector's smallest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_Z].
Returns the axis of the vector's lowest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_Z].
</description>
</method>
<method name="sign" qualifiers="const">
Expand All @@ -90,13 +90,13 @@
</members>
<constants>
<constant name="AXIS_X" value="0">
Enumerated value for the X axis.
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.
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.
Enumerated value for the Z axis. Returned by [method max_axis_index] and [method min_axis_index].
</constant>
<constant name="ZERO" value="Vector3i(0, 0, 0)">
Zero vector, a vector with all components set to [code]0[/code].
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/tiles/tile_map_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3786,7 +3786,7 @@ void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
}

// Draw the warning icon.
int min_axis = missing_tile_texture->get_size().min_axis();
Vector2::Axis min_axis = missing_tile_texture->get_size().min_axis_index();
Vector2 icon_size;
icon_size[min_axis] = tile_set->get_tile_size()[min_axis] / 3;
icon_size[(min_axis + 1) % 2] = (icon_size[min_axis] * missing_tile_texture->get_size()[(min_axis + 1) % 2] / missing_tile_texture->get_size()[min_axis]);
Expand Down
14 changes: 7 additions & 7 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct Vector2 : IEquatable<Vector2>
{
/// <summary>
/// Enumerated index values for the axes.
/// Returned by <see cref="MaxAxis"/> and <see cref="MinAxis"/>.
/// Returned by <see cref="MaxAxisIndex"/> and <see cref="MinAxisIndex"/>.
/// </summary>
public enum Axis
{
Expand Down Expand Up @@ -365,21 +365,21 @@ public Vector2 LimitLength(real_t length = 1.0f)
}

/// <summary>
/// Returns the axis of the vector's largest value. See <see cref="Axis"/>.
/// Returns the axis of the vector's highest value. See <see cref="Axis"/>.
/// If both components are equal, this method returns <see cref="Axis.X"/>.
/// </summary>
/// <returns>The index of the largest axis.</returns>
public Axis MaxAxis()
/// <returns>The index of the highest axis.</returns>
public Axis MaxAxisIndex()
{
return x < y ? Axis.Y : Axis.X;
}

/// <summary>
/// Returns the axis of the vector's smallest value. See <see cref="Axis"/>.
/// Returns the axis of the vector's lowest value. See <see cref="Axis"/>.
/// If both components are equal, this method returns <see cref="Axis.Y"/>.
/// </summary>
/// <returns>The index of the smallest axis.</returns>
public Axis MinAxis()
/// <returns>The index of the lowest axis.</returns>
public Axis MinAxisIndex()
{
return x < y ? Axis.X : Axis.Y;
}
Expand Down
Loading

0 comments on commit 24f5788

Please sign in to comment.