Skip to content

Commit

Permalink
Merge pull request #89114 from AThousandShips/vec_elem_scalar
Browse files Browse the repository at this point in the history
[Core] Add scalar versions of `Vector*` `min/max/clamp/snap(ped)`
  • Loading branch information
akien-mga committed May 2, 2024
2 parents a21824b + 308dbb8 commit 0b6c29f
Show file tree
Hide file tree
Showing 73 changed files with 588 additions and 105 deletions.
2 changes: 1 addition & 1 deletion core/math/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct _NO_DISCARD_ AABB {
_FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */

_FORCE_INLINE_ AABB abs() const {
return AABB(position + size.min(Vector3()), size.abs());
return AABB(position + size.minf(0), size.abs());
}

Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const;
Expand Down
6 changes: 3 additions & 3 deletions core/math/delaunay_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class Delaunay3D {
}

Vector3i grid_pos = Vector3i(points[i] * proportions * ACCEL_GRID_SIZE);
grid_pos = grid_pos.clamp(Vector3i(), Vector3i(ACCEL_GRID_SIZE - 1, ACCEL_GRID_SIZE - 1, ACCEL_GRID_SIZE - 1));
grid_pos = grid_pos.clampi(0, ACCEL_GRID_SIZE - 1);

for (List<Simplex *>::Element *E = acceleration_grid[grid_pos.x][grid_pos.y][grid_pos.z].front(); E;) {
List<Simplex *>::Element *N = E->next(); //may be deleted
Expand Down Expand Up @@ -335,8 +335,8 @@ class Delaunay3D {
Vector3 extents = Vector3(radius2, radius2, radius2);
Vector3i from = Vector3i((center - extents) * proportions * ACCEL_GRID_SIZE);
Vector3i to = Vector3i((center + extents) * proportions * ACCEL_GRID_SIZE);
from = from.clamp(Vector3i(), Vector3i(ACCEL_GRID_SIZE - 1, ACCEL_GRID_SIZE - 1, ACCEL_GRID_SIZE - 1));
to = to.clamp(Vector3i(), Vector3i(ACCEL_GRID_SIZE - 1, ACCEL_GRID_SIZE - 1, ACCEL_GRID_SIZE - 1));
from = from.clampi(0, ACCEL_GRID_SIZE - 1);
to = to.clampi(0, ACCEL_GRID_SIZE - 1);

for (int32_t x = from.x; x <= to.x; x++) {
for (int32_t y = from.y; y <= to.y; y++) {
Expand Down
2 changes: 1 addition & 1 deletion core/math/quick_hull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_
HashSet<Vector3> valid_cache;

for (int i = 0; i < p_points.size(); i++) {
Vector3 sp = p_points[i].snapped(Vector3(0.0001, 0.0001, 0.0001));
Vector3 sp = p_points[i].snappedf(0.0001);
if (valid_cache.has(sp)) {
valid_points.write[i] = false;
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/math/rect2.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ struct _NO_DISCARD_ Rect2 {
}

_FORCE_INLINE_ Rect2 abs() const {
return Rect2(position + size.min(Point2()), size.abs());
return Rect2(position + size.minf(0), size.abs());
}

_FORCE_INLINE_ Rect2 round() const {
Expand Down
2 changes: 1 addition & 1 deletion core/math/rect2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ struct _NO_DISCARD_ Rect2i {
}

_FORCE_INLINE_ Rect2i abs() const {
return Rect2i(position + size.min(Point2i()), size.abs());
return Rect2i(position + size.mini(0), size.abs());
}

_FORCE_INLINE_ void set_end(const Vector2i &p_end) {
Expand Down
2 changes: 1 addition & 1 deletion core/math/triangle_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void TriangleMesh::create(const Vector<Vector3> &p_faces, const Vector<int32_t>

for (int j = 0; j < 3; j++) {
int vidx = -1;
Vector3 vs = v[j].snapped(Vector3(0.0001, 0.0001, 0.0001));
Vector3 vs = v[j].snappedf(0.0001);
HashMap<Vector3, int>::Iterator E = db.find(vs);
if (E) {
vidx = E->value;
Expand Down
12 changes: 12 additions & 0 deletions core/math/vector2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,24 @@ Vector2 Vector2::clamp(const Vector2 &p_min, const Vector2 &p_max) const {
CLAMP(y, p_min.y, p_max.y));
}

Vector2 Vector2::clampf(real_t p_min, real_t p_max) const {
return Vector2(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max));
}

Vector2 Vector2::snapped(const Vector2 &p_step) const {
return Vector2(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y));
}

Vector2 Vector2::snappedf(real_t p_step) const {
return Vector2(
Math::snapped(x, p_step),
Math::snapped(y, p_step));
}

Vector2 Vector2::limit_length(real_t p_len) const {
const real_t l = length();
Vector2 v = *this;
Expand Down
10 changes: 10 additions & 0 deletions core/math/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,18 @@ struct _NO_DISCARD_ Vector2 {
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
}

Vector2 minf(real_t p_scalar) const {
return Vector2(MIN(x, p_scalar), MIN(y, p_scalar));
}

Vector2 max(const Vector2 &p_vector2) const {
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
}

Vector2 maxf(real_t p_scalar) const {
return Vector2(MAX(x, p_scalar), MAX(y, p_scalar));
}

real_t distance_to(const Vector2 &p_vector2) const;
real_t distance_squared_to(const Vector2 &p_vector2) const;
real_t angle_to(const Vector2 &p_vector2) const;
Expand Down Expand Up @@ -168,7 +176,9 @@ struct _NO_DISCARD_ Vector2 {
Vector2 ceil() const;
Vector2 round() const;
Vector2 snapped(const Vector2 &p_by) const;
Vector2 snappedf(real_t p_by) const;
Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const;
Vector2 clampf(real_t p_min, real_t p_max) const;
real_t aspect() const { return width / height; }

operator String() const;
Expand Down
12 changes: 12 additions & 0 deletions core/math/vector2i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
CLAMP(y, p_min.y, p_max.y));
}

Vector2i Vector2i::clampi(int32_t p_min, int32_t p_max) const {
return Vector2i(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max));
}

Vector2i Vector2i::snapped(const Vector2i &p_step) const {
return Vector2i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y));
}

Vector2i Vector2i::snappedi(int32_t p_step) const {
return Vector2i(
Math::snapped(x, p_step),
Math::snapped(y, p_step));
}

int64_t Vector2i::length_squared() const {
return x * (int64_t)x + y * (int64_t)y;
}
Expand Down
10 changes: 10 additions & 0 deletions core/math/vector2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,18 @@ struct _NO_DISCARD_ Vector2i {
return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
}

Vector2i mini(int32_t p_scalar) const {
return Vector2i(MIN(x, p_scalar), MIN(y, p_scalar));
}

Vector2i max(const Vector2i &p_vector2i) const {
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
}

Vector2i maxi(int32_t p_scalar) const {
return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
}

double distance_to(const Vector2i &p_to) const {
return (p_to - *this).length();
}
Expand Down Expand Up @@ -127,7 +135,9 @@ struct _NO_DISCARD_ Vector2i {
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
Vector2i clampi(int32_t p_min, int32_t p_max) const;
Vector2i snapped(const Vector2i &p_step) const;
Vector2i snappedi(int32_t p_step) const;

operator String() const;
operator Vector2() const;
Expand Down
19 changes: 19 additions & 0 deletions core/math/vector3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const {
CLAMP(z, p_min.z, p_max.z));
}

Vector3 Vector3::clampf(real_t p_min, real_t p_max) const {
return Vector3(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max),
CLAMP(z, p_min, p_max));
}

void Vector3::snap(const Vector3 &p_step) {
x = Math::snapped(x, p_step.x);
y = Math::snapped(y, p_step.y);
Expand All @@ -64,6 +71,18 @@ Vector3 Vector3::snapped(const Vector3 &p_step) const {
return v;
}

void Vector3::snapf(real_t p_step) {
x = Math::snapped(x, p_step);
y = Math::snapped(y, p_step);
z = Math::snapped(z, p_step);
}

Vector3 Vector3::snappedf(real_t p_step) const {
Vector3 v = *this;
v.snapf(p_step);
return v;
}

Vector3 Vector3::limit_length(real_t p_len) const {
const real_t l = length();
Vector3 v = *this;
Expand Down
11 changes: 11 additions & 0 deletions core/math/vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ struct _NO_DISCARD_ Vector3 {
return Vector3(MIN(x, p_vector3.x), MIN(y, p_vector3.y), MIN(z, p_vector3.z));
}

Vector3 minf(real_t p_scalar) const {
return Vector3(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
}

Vector3 max(const Vector3 &p_vector3) const {
return Vector3(MAX(x, p_vector3.x), MAX(y, p_vector3.y), MAX(z, p_vector3.z));
}

Vector3 maxf(real_t p_scalar) const {
return Vector3(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
}

_FORCE_INLINE_ real_t length() const;
_FORCE_INLINE_ real_t length_squared() const;

Expand All @@ -96,7 +104,9 @@ struct _NO_DISCARD_ Vector3 {
_FORCE_INLINE_ void zero();

void snap(const Vector3 &p_step);
void snapf(real_t p_step);
Vector3 snapped(const Vector3 &p_step) const;
Vector3 snappedf(real_t p_step) const;

void rotate(const Vector3 &p_axis, real_t p_angle);
Vector3 rotated(const Vector3 &p_axis, real_t p_angle) const;
Expand Down Expand Up @@ -127,6 +137,7 @@ struct _NO_DISCARD_ Vector3 {
_FORCE_INLINE_ Vector3 ceil() const;
_FORCE_INLINE_ Vector3 round() const;
Vector3 clamp(const Vector3 &p_min, const Vector3 &p_max) const;
Vector3 clampf(real_t p_min, real_t p_max) const;

_FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
_FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
Expand Down
14 changes: 14 additions & 0 deletions core/math/vector3i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,27 @@ Vector3i Vector3i::clamp(const Vector3i &p_min, const Vector3i &p_max) const {
CLAMP(z, p_min.z, p_max.z));
}

Vector3i Vector3i::clampi(int32_t p_min, int32_t p_max) const {
return Vector3i(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max),
CLAMP(z, p_min, p_max));
}

Vector3i Vector3i::snapped(const Vector3i &p_step) const {
return Vector3i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y),
Math::snapped(z, p_step.z));
}

Vector3i Vector3i::snappedi(int32_t p_step) const {
return Vector3i(
Math::snapped(x, p_step),
Math::snapped(y, p_step),
Math::snapped(z, p_step));
}

Vector3i::operator String() const {
return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ")";
}
Expand Down
10 changes: 10 additions & 0 deletions core/math/vector3i.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ struct _NO_DISCARD_ Vector3i {
return Vector3i(MIN(x, p_vector3i.x), MIN(y, p_vector3i.y), MIN(z, p_vector3i.z));
}

Vector3i mini(int32_t p_scalar) const {
return Vector3i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
}

Vector3i max(const Vector3i &p_vector3i) const {
return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z));
}

Vector3i maxi(int32_t p_scalar) const {
return Vector3i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
}

_FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const;

Expand All @@ -85,7 +93,9 @@ struct _NO_DISCARD_ Vector3i {
_FORCE_INLINE_ Vector3i abs() const;
_FORCE_INLINE_ Vector3i sign() const;
Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
Vector3i clampi(int32_t p_min, int32_t p_max) const;
Vector3i snapped(const Vector3i &p_step) const;
Vector3i snappedi(int32_t p_step) const;

_FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;
Expand Down
21 changes: 21 additions & 0 deletions core/math/vector4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,25 @@ void Vector4::snap(const Vector4 &p_step) {
w = Math::snapped(w, p_step.w);
}

void Vector4::snapf(real_t p_step) {
x = Math::snapped(x, p_step);
y = Math::snapped(y, p_step);
z = Math::snapped(z, p_step);
w = Math::snapped(w, p_step);
}

Vector4 Vector4::snapped(const Vector4 &p_step) const {
Vector4 v = *this;
v.snap(p_step);
return v;
}

Vector4 Vector4::snappedf(real_t p_step) const {
Vector4 v = *this;
v.snapf(p_step);
return v;
}

Vector4 Vector4::inverse() const {
return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);
}
Expand All @@ -189,6 +202,14 @@ Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const {
CLAMP(w, p_min.w, p_max.w));
}

Vector4 Vector4::clampf(real_t p_min, real_t p_max) const {
return Vector4(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max),
CLAMP(z, p_min, p_max),
CLAMP(w, p_min, p_max));
}

Vector4::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
}
Expand Down
11 changes: 11 additions & 0 deletions core/math/vector4.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ struct _NO_DISCARD_ Vector4 {
return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w));
}

Vector4 minf(real_t p_scalar) const {
return Vector4(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
}

Vector4 max(const Vector4 &p_vector4) const {
return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w));
}

Vector4 maxf(real_t p_scalar) const {
return Vector4(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
}

_FORCE_INLINE_ real_t length_squared() const;
bool is_equal_approx(const Vector4 &p_vec4) const;
bool is_zero_approx() const;
Expand All @@ -101,8 +109,11 @@ struct _NO_DISCARD_ Vector4 {
Vector4 posmod(real_t p_mod) const;
Vector4 posmodv(const Vector4 &p_modv) const;
void snap(const Vector4 &p_step);
void snapf(real_t p_step);
Vector4 snapped(const Vector4 &p_step) const;
Vector4 snappedf(real_t p_step) const;
Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const;
Vector4 clampf(real_t p_min, real_t p_max) const;

Vector4 inverse() const;
_FORCE_INLINE_ real_t dot(const Vector4 &p_vec4) const;
Expand Down
Loading

0 comments on commit 0b6c29f

Please sign in to comment.