Skip to content

Commit 7f2e5ca

Browse files
committed
Fix some vector refactor errors
1 parent b74431f commit 7f2e5ca

File tree

5 files changed

+57
-48
lines changed

5 files changed

+57
-48
lines changed

godot-core/src/builtin/aabb.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,18 @@ impl Aabb {
189189

190190
/// Returns the normalized longest axis of the AABB.
191191
#[inline]
192-
pub fn longest_axis(&self) -> Vector3 {
193-
match self.longest_axis_index() {
192+
pub fn longest_axis(&self) -> Option<Vector3> {
193+
self.longest_axis_index().map(|axis| match axis {
194194
Vector3Axis::X => Vector3::RIGHT,
195195
Vector3Axis::Y => Vector3::UP,
196196
Vector3Axis::Z => Vector3::BACK,
197-
}
197+
})
198198
}
199199

200200
/// Returns the index of the longest axis of the AABB (according to Vector3's AXIS_* constants).
201201
#[inline]
202-
pub fn longest_axis_index(&self) -> Vector3Axis {
203-
self.size.max_axis().unwrap_or(Vector3Axis::X)
202+
pub fn longest_axis_index(&self) -> Option<Vector3Axis> {
203+
self.size.max_axis()
204204
}
205205

206206
/// Returns the scalar length of the longest axis of the AABB.
@@ -211,18 +211,18 @@ impl Aabb {
211211

212212
/// Returns the normalized shortest axis of the AABB.
213213
#[inline]
214-
pub fn shortest_axis(&self) -> Vector3 {
215-
match self.shortest_axis_index() {
214+
pub fn shortest_axis(&self) -> Option<Vector3> {
215+
self.shortest_axis_index().map(|axis| match axis {
216216
Vector3Axis::X => Vector3::RIGHT,
217217
Vector3Axis::Y => Vector3::UP,
218218
Vector3Axis::Z => Vector3::BACK,
219-
}
219+
})
220220
}
221221

222222
/// Returns the index of the shortest axis of the AABB (according to Vector3::AXIS* enum).
223223
#[inline]
224-
pub fn shortest_axis_index(&self) -> Vector3Axis {
225-
self.size.min_axis().unwrap_or(Vector3Axis::Z)
224+
pub fn shortest_axis_index(&self) -> Option<Vector3Axis> {
225+
self.size.min_axis()
226226
}
227227

228228
/// Returns the scalar length of the shortest axis of the AABB.
@@ -436,12 +436,12 @@ mod test {
436436
size: Vector3::new(4.0, 6.0, 8.0),
437437
};
438438

439-
assert_eq!(aabb.shortest_axis(), Vector3::RIGHT);
440-
assert_eq!(aabb.longest_axis(), Vector3::BACK);
439+
assert_eq!(aabb.shortest_axis(), Some(Vector3::RIGHT));
440+
assert_eq!(aabb.longest_axis(), Some(Vector3::BACK));
441441
assert_eq!(aabb.shortest_axis_size(), 4.0);
442442
assert_eq!(aabb.longest_axis_size(), 8.0);
443-
assert_eq!(aabb.shortest_axis_index(), Vector3Axis::X);
444-
assert_eq!(aabb.longest_axis_index(), Vector3Axis::Z);
443+
assert_eq!(aabb.shortest_axis_index(), Some(Vector3Axis::X));
444+
assert_eq!(aabb.longest_axis_index(), Some(Vector3Axis::Z));
445445
}
446446

447447
#[test]

godot-core/src/builtin/math/glam_helpers.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ use crate::builtin::real;
2121
pub(crate) trait GlamConv {
2222
type Glam: GlamType<Mapped = Self>;
2323

24-
#[inline]
2524
fn to_glam(&self) -> Self::Glam {
2625
Self::Glam::from_front(self)
2726
}
2827

29-
#[inline]
3028
fn glam<F, R>(&self, unary_fn: F) -> R::Mapped
3129
where
3230
R: GlamType,
@@ -38,7 +36,6 @@ pub(crate) trait GlamConv {
3836
result.to_front()
3937
}
4038

41-
#[inline]
4239
fn glam2<F, P, R>(&self, rhs: &P, binary_fn: F) -> R::Mapped
4340
where
4441
P: GlamConv,

godot-core/src/builtin/vectors/vector2.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ impl Vector2 {
102102
///
103103
/// ```no_run
104104
/// use godot::prelude::*;
105-
/// use std::f32::consts::PI;
106105
///
107-
/// let a = Vector2::from_angle(0.0); // (1.0, 0.0)
108-
/// let b = Vector2::new(1.0, 0.0).angle(); // 0.0
109-
/// let c = Vector2::from_angle(PI / 2.0); // (0.0, 1.0)
106+
/// let a = Vector2::from_angle(0.0); // (1.0, 0.0)
107+
/// let b = Vector2::new(1.0, 0.0).angle(); // 0.0
108+
/// let c = Vector2::from_angle(real_consts::PI / 2.0); // (0.0, 1.0)
110109
/// ```
111110
#[inline]
112111
pub fn from_angle(angle: real) -> Self {

godot-core/src/builtin/vectors/vector3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl Vector3 {
5959
pub const MODEL_RIGHT: Self = Self::new(-1.0, 0.0, 0.0);
6060

6161
/// Unit vector pointing towards the top side (up) of imported 3D assets.
62-
pub const MODEL_UP: Self = Self::new(0.0, 1.0, 0.0);
62+
pub const MODEL_TOP: Self = Self::new(0.0, 1.0, 0.0);
6363

6464
/// Unit vector pointing towards the bottom side (down) of imported 3D assets.
6565
pub const MODEL_BOTTOM: Self = Self::new(0.0, -1.0, 0.0);
@@ -120,7 +120,7 @@ impl Vector3 {
120120
/// If vector is not normalized.
121121
#[inline]
122122
pub fn octahedron_encode(self) -> Vector2 {
123-
assert!(self.is_normalized());
123+
assert!(self.is_normalized(), "vector is not normalized!");
124124

125125
let mut n = self;
126126
n /= n.x.abs() + n.y.abs() + n.z.abs();
@@ -156,7 +156,7 @@ impl Vector3 {
156156
/// If `axis` is not normalized.
157157
#[inline]
158158
pub fn rotated(self, axis: Self, angle: real) -> Self {
159-
assert!(axis.is_normalized());
159+
assert!(axis.is_normalized(), "axis is not normalized!");
160160
Basis::from_axis_angle(axis, angle) * self
161161
}
162162

godot-core/src/builtin/vectors/vector_macros.rs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ macro_rules! impl_integer_vector_consts {
289289
$Vector:ty
290290
) => {
291291
impl $Vector {
292-
/// Min vector, a vector with all components equal to [`i32::MIN`]. Can be used as a negative integer equivalent of [`Vector2::INF`].
292+
/// Min vector, a vector with all components equal to [`i32::MIN`]. Can be used as a negative integer equivalent of `real::INF`.
293293
pub const MIN: Self = Self::splat(i32::MIN);
294294

295-
/// Max vector, a vector with all components equal to [`i32::MAX`]. Can be used as an integer equivalent of [`Vector2::INF`].
295+
/// Max vector, a vector with all components equal to [`i32::MAX`]. Can be used as an integer equivalent of `real::INF`.
296296
pub const MAX: Self = Self::splat(i32::MAX);
297297
}
298298
};
@@ -402,8 +402,8 @@ macro_rules! impl_vector_fns {
402402

403403
/// Returns a new vector with all components clamped between the components of `min` and `max`.
404404
///
405-
/// Panics
406-
/// Panics if `min` > `max`, `min` is NaN, or `max` is NaN.
405+
/// # Panics
406+
/// If `min` > `max`, `min` is NaN, or `max` is NaN.
407407
#[inline]
408408
pub fn clamp(self, min: Self, max: Self) -> Self {
409409
Self::from_glam(self.to_glam().clamp(min.to_glam(), max.to_glam()))
@@ -462,7 +462,9 @@ macro_rules! impl_float_vector_fns {
462462
}
463463

464464
/// Performs a cubic interpolation between this vector and `b` using `pre_a` and `post_b` as handles,
465-
/// and returns the result at position `weight`. `weight` is on the range of 0.0 to 1.0, representing the amount of interpolation.
465+
/// and returns the result at position `weight`.
466+
///
467+
/// `weight` is on the range of 0.0 to 1.0, representing the amount of interpolation.
466468
#[inline]
467469
pub fn cubic_interpolate(self, b: Self, pre_a: Self, post_b: Self, weight: real) -> Self {
468470
Self::new(
@@ -473,7 +475,9 @@ macro_rules! impl_float_vector_fns {
473475
}
474476

475477
/// Performs a cubic interpolation between this vector and `b` using `pre_a` and `post_b` as handles,
476-
/// and returns the result at position `weight`. `weight` is on the range of 0.0 to 1.0, representing the amount of interpolation.
478+
/// and returns the result at position `weight`.
479+
///
480+
/// `weight` is on the range of 0.0 to 1.0, representing the amount of interpolation.
477481
/// It can perform smoother interpolation than [`Self::cubic_interpolate`] by the time values.
478482
#[inline]
479483
#[allow(clippy::too_many_arguments)]
@@ -496,7 +500,9 @@ macro_rules! impl_float_vector_fns {
496500
)
497501
}
498502

499-
/// Returns the normalized vector pointing from this vector to `to`. This is equivalent to using `(b - a).normalized()`.
503+
/// Returns the normalized vector pointing from this vector to `to`.
504+
///
505+
/// This is equivalent to using `(b - a).normalized()`.
500506
#[inline]
501507
pub fn direction_to(self, to: Self) -> Self {
502508
(to - self).normalized()
@@ -541,13 +547,15 @@ macro_rules! impl_float_vector_fns {
541547
}
542548

543549
/// Returns `true` if this vector's values are approximately zero.
544-
/// This method is faster than using [`Self::is_equal_approx`] with one value as a zero vector.
550+
///
551+
/// This method is faster than using `approx_eq()` with one value as a zero vector.
545552
#[inline]
546553
pub fn is_zero_approx(self) -> bool {
547554
$( self.$comp.is_zero_approx() )&&*
548555
}
549556

550557
/// Returns the result of the linear interpolation between this vector and `to` by amount `weight`.
558+
///
551559
/// `weight` is on the range of `0.0` to `1.0`, representing the amount of interpolation.
552560
#[inline]
553561
pub fn lerp(self, other: Self, weight: real) -> Self {
@@ -559,19 +567,14 @@ macro_rules! impl_float_vector_fns {
559567
/// Returns the vector scaled to unit length. Equivalent to `self / self.length()`. See
560568
/// also `is_normalized()`.
561569
///
562-
/// If the vector is zero, the result is also zero.
570+
/// # Panics
571+
/// If called on a zero vector.
563572
#[inline]
564573
pub fn normalized(self) -> Self {
565-
// Copy Godot's implementation since it's faster than using glam's normalize_or_zero().
566-
if self == Self::ZERO {
567-
return self;
568-
}
569-
570-
let l = self.length();
574+
assert_ne!(self, Self::ZERO, "normalized() called on zero vector");
571575

572-
Self::new(
573-
$( self.$comp / l ),*
574-
)
576+
// Copy Godot's implementation since it's faster than using glam's normalize_or_zero().
577+
self / self.length()
575578
}
576579

577580
/// Returns a vector composed of the [`FloatExt::fposmod`] of this vector's components and `pmod`.
@@ -612,7 +615,7 @@ macro_rules! impl_float_vector_fns {
612615
impl $crate::builtin::math::ApproxEq for $Vector {
613616
/// Returns `true` if this vector and `to` are approximately equal.
614617
#[inline]
615-
#[doc(alias = "is_approx_eq")]
618+
#[doc(alias = "is_equal_approx")]
616619
fn approx_eq(&self, other: &Self) -> bool {
617620
$( self.$comp.approx_eq(&other.$comp) )&&*
618621
}
@@ -857,9 +860,13 @@ macro_rules! impl_vector2_vector3_fns {
857860
}
858861

859862
/// Returns a new vector "bounced off" from a plane defined by the given normal.
863+
///
864+
/// # Panics
865+
/// If `n` is not normalized.
860866
#[inline]
861-
pub fn bounce(self, normal: Self) -> Self {
862-
-self.reflect(normal)
867+
pub fn bounce(self, n: Self) -> Self {
868+
assert!(n.is_normalized(), "n is not normalized!");
869+
-self.reflect(n)
863870
}
864871

865872
/// Returns the vector with a maximum length by limiting its length to `length`.
@@ -884,14 +891,22 @@ macro_rules! impl_vector2_vector3_fns {
884891
}
885892

886893
/// Returns the result of reflecting the vector defined by the given direction vector `n`.
894+
///
895+
/// # Panics
896+
/// If `n` is not normalized.
887897
#[inline]
888898
pub fn reflect(self, n: Self) -> Self {
899+
assert!(n.is_normalized(), "n is not normalized!");
889900
2.0 * n * self.dot(n) - self
890901
}
891902

892903
/// Returns a new vector slid along a plane defined by the given normal.
904+
///
905+
/// # Panics
906+
/// If `n` is not normalized.
893907
#[inline]
894908
pub fn slide(self, n: Self) -> Self {
909+
assert!(n.is_normalized(), "n is not normalized!");
895910
self - n * self.dot(n)
896911
}
897912
}
@@ -911,9 +926,7 @@ macro_rules! impl_vector3_vector4_fns {
911926
#[inline]
912927
#[doc(alias = "inverse")]
913928
pub fn recip(self) -> Self {
914-
Self::new(
915-
$( 1.0 / self.$comp ),*
916-
)
929+
Self::from_glam(self.to_glam().recip())
917930
}
918931
}
919932
};

0 commit comments

Comments
 (0)