Skip to content

Commit

Permalink
Fix some vector refactor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joriskleiber committed Jun 2, 2024
1 parent b74431f commit ba217ea
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
28 changes: 14 additions & 14 deletions godot-core/src/builtin/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,18 @@ impl Aabb {

/// Returns the normalized longest axis of the AABB.
#[inline]
pub fn longest_axis(&self) -> Vector3 {
match self.longest_axis_index() {
pub fn longest_axis(&self) -> Option<Vector3> {
self.longest_axis_index().map(|axis| match axis {
Vector3Axis::X => Vector3::RIGHT,
Vector3Axis::Y => Vector3::UP,
Vector3Axis::Z => Vector3::BACK,
}
})
}

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

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

/// Returns the normalized shortest axis of the AABB.
#[inline]
pub fn shortest_axis(&self) -> Vector3 {
match self.shortest_axis_index() {
pub fn shortest_axis(&self) -> Option<Vector3> {
self.shortest_axis_index().map(|axis| match axis {
Vector3Axis::X => Vector3::RIGHT,
Vector3Axis::Y => Vector3::UP,
Vector3Axis::Z => Vector3::BACK,
}
})
}

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

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

assert_eq!(aabb.shortest_axis(), Vector3::RIGHT);
assert_eq!(aabb.longest_axis(), Vector3::BACK);
assert_eq!(aabb.shortest_axis(), Some(Vector3::RIGHT));
assert_eq!(aabb.longest_axis(), Some(Vector3::BACK));
assert_eq!(aabb.shortest_axis_size(), 4.0);
assert_eq!(aabb.longest_axis_size(), 8.0);
assert_eq!(aabb.shortest_axis_index(), Vector3Axis::X);
assert_eq!(aabb.longest_axis_index(), Vector3Axis::Z);
assert_eq!(aabb.shortest_axis_index(), Some(Vector3Axis::X));
assert_eq!(aabb.longest_axis_index(), Some(Vector3Axis::Z));
}

#[test]
Expand Down
3 changes: 0 additions & 3 deletions godot-core/src/builtin/math/glam_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ use crate::builtin::real;
pub(crate) trait GlamConv {
type Glam: GlamType<Mapped = Self>;

#[inline]
fn to_glam(&self) -> Self::Glam {
Self::Glam::from_front(self)
}

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

#[inline]
fn glam2<F, P, R>(&self, rhs: &P, binary_fn: F) -> R::Mapped
where
P: GlamConv,
Expand Down
7 changes: 3 additions & 4 deletions godot-core/src/builtin/vectors/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ impl Vector2 {
///
/// ```no_run
/// use godot::prelude::*;
/// use std::f32::consts::PI;
///
/// let a = Vector2::from_angle(0.0); // (1.0, 0.0)
/// let b = Vector2::new(1.0, 0.0).angle(); // 0.0
/// let c = Vector2::from_angle(PI / 2.0); // (0.0, 1.0)
/// let a = Vector2::from_angle(0.0); // (1.0, 0.0)
/// let b = Vector2::new(1.0, 0.0).angle(); // 0.0
/// let c = Vector2::from_angle(real_consts::PI / 2.0); // (0.0, 1.0)
/// ```
#[inline]
pub fn from_angle(angle: real) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions godot-core/src/builtin/vectors/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Vector3 {
pub const MODEL_RIGHT: Self = Self::new(-1.0, 0.0, 0.0);

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

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

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

Expand Down
40 changes: 24 additions & 16 deletions godot-core/src/builtin/vectors/vector_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ macro_rules! impl_vector_fns {

/// Returns a new vector with all components clamped between the components of `min` and `max`.
///
/// Panics
/// Panics if `min` > `max`, `min` is NaN, or `max` is NaN.
/// # Panics
/// If `min` > `max`, `min` is NaN, or `max` is NaN.
#[inline]
pub fn clamp(self, min: Self, max: Self) -> Self {
Self::from_glam(self.to_glam().clamp(min.to_glam(), max.to_glam()))
Expand Down Expand Up @@ -541,7 +541,8 @@ macro_rules! impl_float_vector_fns {
}

/// Returns `true` if this vector's values are approximately zero.
/// This method is faster than using [`Self::is_equal_approx`] with one value as a zero vector.
///
/// This method is faster than using `approx_eq()` with one value as a zero vector.
#[inline]
pub fn is_zero_approx(self) -> bool {
$( self.$comp.is_zero_approx() )&&*
Expand All @@ -559,19 +560,14 @@ macro_rules! impl_float_vector_fns {
/// Returns the vector scaled to unit length. Equivalent to `self / self.length()`. See
/// also `is_normalized()`.
///
/// If the vector is zero, the result is also zero.
/// # Panics
/// If called on a zero vector.
#[inline]
pub fn normalized(self) -> Self {
// Copy Godot's implementation since it's faster than using glam's normalize_or_zero().
if self == Self::ZERO {
return self;
}
assert_ne!(self, Self::ZERO, "normalized() called on zero vector!");

let l = self.length();

Self::new(
$( self.$comp / l ),*
)
// Copy Godot's implementation since it's faster than using glam's normalize_or_zero().
self / self.length()
}

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

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

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

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

/// Returns a new vector slid along a plane defined by the given normal.
///
/// # Panics
/// If `n` is not normalized.
#[inline]
pub fn slide(self, n: Self) -> Self {
assert!(n.is_normalized(), "n is not normalized!");
self - n * self.dot(n)
}
}
Expand Down

0 comments on commit ba217ea

Please sign in to comment.