Skip to content

Commit 9d93265

Browse files
committed
Fix some vector refactor errors
1 parent b74431f commit 9d93265

File tree

5 files changed

+46
-42
lines changed

5 files changed

+46
-42
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: 26 additions & 18 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()))
@@ -541,7 +541,8 @@ macro_rules! impl_float_vector_fns {
541541
}
542542

543543
/// 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.
544+
///
545+
/// This method is faster than using `approx_eq()` with one value as a zero vector.
545546
#[inline]
546547
pub fn is_zero_approx(self) -> bool {
547548
$( self.$comp.is_zero_approx() )&&*
@@ -559,19 +560,14 @@ macro_rules! impl_float_vector_fns {
559560
/// Returns the vector scaled to unit length. Equivalent to `self / self.length()`. See
560561
/// also `is_normalized()`.
561562
///
562-
/// If the vector is zero, the result is also zero.
563+
/// # Panics
564+
/// If called on a zero vector.
563565
#[inline]
564566
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-
}
567+
assert_ne!(self, Self::ZERO, "normalized() called on zero vector!");
569568

570-
let l = self.length();
571-
572-
Self::new(
573-
$( self.$comp / l ),*
574-
)
569+
// Copy Godot's implementation since it's faster than using glam's normalize_or_zero().
570+
self / self.length()
575571
}
576572

577573
/// Returns a vector composed of the [`FloatExt::fposmod`] of this vector's components and `pmod`.
@@ -612,7 +608,7 @@ macro_rules! impl_float_vector_fns {
612608
impl $crate::builtin::math::ApproxEq for $Vector {
613609
/// Returns `true` if this vector and `to` are approximately equal.
614610
#[inline]
615-
#[doc(alias = "is_approx_eq")]
611+
#[doc(alias = "is_equal_approx")]
616612
fn approx_eq(&self, other: &Self) -> bool {
617613
$( self.$comp.approx_eq(&other.$comp) )&&*
618614
}
@@ -857,9 +853,13 @@ macro_rules! impl_vector2_vector3_fns {
857853
}
858854

859855
/// Returns a new vector "bounced off" from a plane defined by the given normal.
856+
///
857+
/// # Panics
858+
/// If `n` is not normalized.
860859
#[inline]
861-
pub fn bounce(self, normal: Self) -> Self {
862-
-self.reflect(normal)
860+
pub fn bounce(self, n: Self) -> Self {
861+
assert!(n.is_normalized(), "n is not normalized!");
862+
-self.reflect(n)
863863
}
864864

865865
/// Returns the vector with a maximum length by limiting its length to `length`.
@@ -884,14 +884,22 @@ macro_rules! impl_vector2_vector3_fns {
884884
}
885885

886886
/// Returns the result of reflecting the vector defined by the given direction vector `n`.
887+
///
888+
/// # Panics
889+
/// If `n` is not normalized.
887890
#[inline]
888891
pub fn reflect(self, n: Self) -> Self {
892+
assert!(n.is_normalized(), "n is not normalized!");
889893
2.0 * n * self.dot(n) - self
890894
}
891895

892896
/// Returns a new vector slid along a plane defined by the given normal.
897+
///
898+
/// # Panics
899+
/// If `n` is not normalized.
893900
#[inline]
894901
pub fn slide(self, n: Self) -> Self {
902+
assert!(n.is_normalized(), "n is not normalized!");
895903
self - n * self.dot(n)
896904
}
897905
}

0 commit comments

Comments
 (0)