Skip to content

Commit edd873b

Browse files
godot-core: builtin: vector: provide component-wise min/max under unambiguous name
1 parent 0502e2b commit edd873b

File tree

10 files changed

+46
-26
lines changed

10 files changed

+46
-26
lines changed

godot-core/src/builtin/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ mod real_mod {
212212
/// A 4-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
213213
pub type RVec4 = glam::Vec4;
214214

215-
/// A 2x2 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
215+
/// A 2x2 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
216216
pub type RMat2 = glam::Mat2;
217217
/// A 3x3 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
218218
pub type RMat3 = glam::Mat3;
@@ -279,7 +279,7 @@ mod real_mod {
279279
/// A 4-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
280280
pub type RVec4 = glam::DVec4;
281281

282-
/// A 2x2 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
282+
/// A 2x2 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
283283
pub type RMat2 = glam::DMat2;
284284
/// A 3x3 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
285285
pub type RMat3 = glam::DMat3;

godot-core/src/builtin/transform2d.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ impl Mul<Rect2> for Transform2D {
333333
let ya = self.b * rhs.position.y;
334334
let yb = self.b * rhs.end().y;
335335

336-
let position = Vector2::min(xa, xb) + Vector2::min(ya, yb) + self.origin;
337-
let end = Vector2::max(xa, xb) + Vector2::max(ya, yb) + self.origin;
336+
let position = Vector2::coord_min(xa, xb) + Vector2::coord_min(ya, yb) + self.origin;
337+
let end = Vector2::coord_max(xa, xb) + Vector2::coord_max(ya, yb) + self.origin;
338338
Rect2::new(position, end - position)
339339
}
340340
}

godot-core/src/builtin/transform3d.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,14 @@ impl Mul<Aabb> for Transform3D {
311311
let za = self.basis.col_c() * rhs.position.z;
312312
let zb = self.basis.col_c() * rhs.end().z;
313313

314-
let position =
315-
Vector3::min(xa, xb) + Vector3::min(ya, yb) + Vector3::min(za, zb) + self.origin;
316-
let end = Vector3::max(xa, xb) + Vector3::max(ya, yb) + Vector3::max(za, zb) + self.origin;
314+
let position = Vector3::coord_min(xa, xb)
315+
+ Vector3::coord_min(ya, yb)
316+
+ Vector3::coord_min(za, zb)
317+
+ self.origin;
318+
let end = Vector3::coord_max(xa, xb)
319+
+ Vector3::coord_max(ya, yb)
320+
+ Vector3::coord_max(za, zb)
321+
+ self.origin;
317322
Aabb::new(position, end - position)
318323
}
319324
}

godot-core/src/builtin/vector2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl fmt::Display for Vector2 {
305305
}
306306
}
307307

308-
impl_common_vector_fns!(Vector2, real);
308+
impl_common_vector_fns!(Vector2, real, (x, y));
309309
impl_float_vector_fns!(Vector2, real);
310310
impl_vector_operators!(Vector2, real, (x, y));
311311
impl_vector_index!(Vector2, real, (x, y), Vector2Axis, (X, Y));

godot-core/src/builtin/vector2i.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl fmt::Display for Vector2i {
8484
}
8585
}
8686

87-
impl_common_vector_fns!(Vector2i, i32);
87+
impl_common_vector_fns!(Vector2i, i32, (x, y));
8888
impl_vector_operators!(Vector2i, i32, (x, y));
8989
impl_vector_index!(Vector2i, i32, (x, y), Vector2iAxis, (X, Y));
9090

godot-core/src/builtin/vector3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl fmt::Display for Vector3 {
315315
}
316316
}
317317

318-
impl_common_vector_fns!(Vector3, real);
318+
impl_common_vector_fns!(Vector3, real, (x, y, z));
319319
impl_float_vector_fns!(Vector3, real);
320320
impl_vector_operators!(Vector3, real, (x, y, z));
321321
impl_vector_index!(Vector3, real, (x, y, z), Vector3Axis, (X, Y, Z));

godot-core/src/builtin/vector3i.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl fmt::Display for Vector3i {
9393
}
9494
}
9595

96-
impl_common_vector_fns!(Vector3i, i32);
96+
impl_common_vector_fns!(Vector3i, i32, (x, y, z));
9797
impl_vector_operators!(Vector3i, i32, (x, y, z));
9898
impl_vector_index!(Vector3i, i32, (x, y, z), Vector3iAxis, (X, Y, Z));
9999

godot-core/src/builtin/vector4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct Vector4 {
3838

3939
impl_vector_operators!(Vector4, real, (x, y, z, w));
4040
impl_vector_index!(Vector4, real, (x, y, z, w), Vector4Axis, (X, Y, Z, W));
41-
impl_common_vector_fns!(Vector4, real);
41+
impl_common_vector_fns!(Vector4, real, (x, y, z, w));
4242
impl_float_vector_fns!(Vector4, real);
4343

4444
impl Vector4 {

godot-core/src/builtin/vector4i.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Vector4i {
3434

3535
impl_vector_operators!(Vector4i, i32, (x, y, z, w));
3636
impl_vector_index!(Vector4i, i32, (x, y, z, w), Vector4iAxis, (X, Y, Z, W));
37-
impl_common_vector_fns!(Vector4i, i32);
37+
impl_common_vector_fns!(Vector4i, i32, (x, y, z, w));
3838

3939
impl Vector4i {
4040
/// Returns a `Vector4i` with the given components.

godot-core/src/builtin/vector_macros.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ macro_rules! impl_common_vector_fns {
229229
// Name of the vector type.
230230
$Vector:ty,
231231
// Type of target component, for example `real`.
232-
$Scalar:ty
232+
$Scalar:ty,
233+
// Names of the components, with parentheses, for example `(x, y)`.
234+
($($components:ident),*)
233235
) => {
234236
impl $Vector {
235237
/// Returns a new vector with all components in absolute values (i.e. positive or
@@ -238,6 +240,31 @@ macro_rules! impl_common_vector_fns {
238240
pub fn abs(self) -> Self {
239241
Self::from_glam(self.to_glam().abs())
240242
}
243+
244+
/// Returns a new vector containing the minimum of the two vectors, component-wise.
245+
#[inline]
246+
pub fn coord_min(self, other: Self) -> Self {
247+
self.coord_operation(other, <$Scalar>::min)
248+
}
249+
250+
/// Returns a new vector containing the maximum of the two vectors, component-wise.
251+
#[inline]
252+
pub fn coord_max(self, other: Self) -> Self {
253+
self.coord_operation(other, <$Scalar>::max)
254+
}
255+
256+
/// Applies a component-wise internal binary operation to the two vectors.
257+
#[inline]
258+
fn coord_operation<F>(self, other: Self, f: F) -> Self
259+
where
260+
F: Fn($Scalar, $Scalar) -> $Scalar
261+
{
262+
Self {
263+
$(
264+
$components: f(self.$components, other.$components)
265+
),+
266+
}
267+
}
241268
}
242269
};
243270
}
@@ -266,18 +293,6 @@ macro_rules! impl_float_vector_fns {
266293
pub fn normalized(self) -> Self {
267294
Self::from_glam(self.to_glam().normalize_or_zero())
268295
}
269-
270-
/// Returns a vector containing the minimum values for each element of `self` and `other`.
271-
#[inline]
272-
pub fn min(self, other: Self) -> Self {
273-
self.glam2(&other, |a, b| a.min(b))
274-
}
275-
276-
/// Returns a vector containing the maximum values for each element of `self` and `other`.
277-
#[inline]
278-
pub fn max(self, other: Self) -> Self {
279-
self.glam2(&other, |a, b| a.max(b))
280-
}
281296
}
282297
};
283298
}

0 commit comments

Comments
 (0)