Skip to content

Commit 0b6c29f

Browse files
committed
Merge pull request #89114 from AThousandShips/vec_elem_scalar
[Core] Add scalar versions of `Vector*` `min/max/clamp/snap(ped)`
2 parents a21824b + 308dbb8 commit 0b6c29f

File tree

73 files changed

+588
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+588
-105
lines changed

core/math/aabb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct _NO_DISCARD_ AABB {
101101
_FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */
102102

103103
_FORCE_INLINE_ AABB abs() const {
104-
return AABB(position + size.min(Vector3()), size.abs());
104+
return AABB(position + size.minf(0), size.abs());
105105
}
106106

107107
Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const;

core/math/delaunay_3d.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class Delaunay3D {
278278
}
279279

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

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

341341
for (int32_t x = from.x; x <= to.x; x++) {
342342
for (int32_t y = from.y; y <= to.y; y++) {

core/math/quick_hull.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_
5555
HashSet<Vector3> valid_cache;
5656

5757
for (int i = 0; i < p_points.size(); i++) {
58-
Vector3 sp = p_points[i].snapped(Vector3(0.0001, 0.0001, 0.0001));
58+
Vector3 sp = p_points[i].snappedf(0.0001);
5959
if (valid_cache.has(sp)) {
6060
valid_points.write[i] = false;
6161
} else {

core/math/rect2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ struct _NO_DISCARD_ Rect2 {
278278
}
279279

280280
_FORCE_INLINE_ Rect2 abs() const {
281-
return Rect2(position + size.min(Point2()), size.abs());
281+
return Rect2(position + size.minf(0), size.abs());
282282
}
283283

284284
_FORCE_INLINE_ Rect2 round() const {

core/math/rect2i.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ struct _NO_DISCARD_ Rect2i {
213213
}
214214

215215
_FORCE_INLINE_ Rect2i abs() const {
216-
return Rect2i(position + size.min(Point2i()), size.abs());
216+
return Rect2i(position + size.mini(0), size.abs());
217217
}
218218

219219
_FORCE_INLINE_ void set_end(const Vector2i &p_end) {

core/math/triangle_mesh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void TriangleMesh::create(const Vector<Vector3> &p_faces, const Vector<int32_t>
133133

134134
for (int j = 0; j < 3; j++) {
135135
int vidx = -1;
136-
Vector3 vs = v[j].snapped(Vector3(0.0001, 0.0001, 0.0001));
136+
Vector3 vs = v[j].snappedf(0.0001);
137137
HashMap<Vector3, int>::Iterator E = db.find(vs);
138138
if (E) {
139139
vidx = E->value;

core/math/vector2.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,24 @@ Vector2 Vector2::clamp(const Vector2 &p_min, const Vector2 &p_max) const {
135135
CLAMP(y, p_min.y, p_max.y));
136136
}
137137

138+
Vector2 Vector2::clampf(real_t p_min, real_t p_max) const {
139+
return Vector2(
140+
CLAMP(x, p_min, p_max),
141+
CLAMP(y, p_min, p_max));
142+
}
143+
138144
Vector2 Vector2::snapped(const Vector2 &p_step) const {
139145
return Vector2(
140146
Math::snapped(x, p_step.x),
141147
Math::snapped(y, p_step.y));
142148
}
143149

150+
Vector2 Vector2::snappedf(real_t p_step) const {
151+
return Vector2(
152+
Math::snapped(x, p_step),
153+
Math::snapped(y, p_step));
154+
}
155+
144156
Vector2 Vector2::limit_length(real_t p_len) const {
145157
const real_t l = length();
146158
Vector2 v = *this;

core/math/vector2.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,18 @@ struct _NO_DISCARD_ Vector2 {
8989
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
9090
}
9191

92+
Vector2 minf(real_t p_scalar) const {
93+
return Vector2(MIN(x, p_scalar), MIN(y, p_scalar));
94+
}
95+
9296
Vector2 max(const Vector2 &p_vector2) const {
9397
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
9498
}
9599

100+
Vector2 maxf(real_t p_scalar) const {
101+
return Vector2(MAX(x, p_scalar), MAX(y, p_scalar));
102+
}
103+
96104
real_t distance_to(const Vector2 &p_vector2) const;
97105
real_t distance_squared_to(const Vector2 &p_vector2) const;
98106
real_t angle_to(const Vector2 &p_vector2) const;
@@ -168,7 +176,9 @@ struct _NO_DISCARD_ Vector2 {
168176
Vector2 ceil() const;
169177
Vector2 round() const;
170178
Vector2 snapped(const Vector2 &p_by) const;
179+
Vector2 snappedf(real_t p_by) const;
171180
Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const;
181+
Vector2 clampf(real_t p_min, real_t p_max) const;
172182
real_t aspect() const { return width / height; }
173183

174184
operator String() const;

core/math/vector2i.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,24 @@ Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
3939
CLAMP(y, p_min.y, p_max.y));
4040
}
4141

42+
Vector2i Vector2i::clampi(int32_t p_min, int32_t p_max) const {
43+
return Vector2i(
44+
CLAMP(x, p_min, p_max),
45+
CLAMP(y, p_min, p_max));
46+
}
47+
4248
Vector2i Vector2i::snapped(const Vector2i &p_step) const {
4349
return Vector2i(
4450
Math::snapped(x, p_step.x),
4551
Math::snapped(y, p_step.y));
4652
}
4753

54+
Vector2i Vector2i::snappedi(int32_t p_step) const {
55+
return Vector2i(
56+
Math::snapped(x, p_step),
57+
Math::snapped(y, p_step));
58+
}
59+
4860
int64_t Vector2i::length_squared() const {
4961
return x * (int64_t)x + y * (int64_t)y;
5062
}

core/math/vector2i.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,18 @@ struct _NO_DISCARD_ Vector2i {
8181
return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
8282
}
8383

84+
Vector2i mini(int32_t p_scalar) const {
85+
return Vector2i(MIN(x, p_scalar), MIN(y, p_scalar));
86+
}
87+
8488
Vector2i max(const Vector2i &p_vector2i) const {
8589
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
8690
}
8791

92+
Vector2i maxi(int32_t p_scalar) const {
93+
return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
94+
}
95+
8896
double distance_to(const Vector2i &p_to) const {
8997
return (p_to - *this).length();
9098
}
@@ -127,7 +135,9 @@ struct _NO_DISCARD_ Vector2i {
127135
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
128136
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
129137
Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
138+
Vector2i clampi(int32_t p_min, int32_t p_max) const;
130139
Vector2i snapped(const Vector2i &p_step) const;
140+
Vector2i snappedi(int32_t p_step) const;
131141

132142
operator String() const;
133143
operator Vector2() const;

0 commit comments

Comments
 (0)