Skip to content

Commit

Permalink
Rect2, Aabb: longest/shortest axis accessible via single method + new…
Browse files Browse the repository at this point in the history
… Axis::to_unit_vector()
  • Loading branch information
Bromeon committed Jul 15, 2022
1 parent dfdcc4c commit 7df8463
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
75 changes: 38 additions & 37 deletions gdnative-core/src/core_types/geom/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,46 +118,40 @@ impl Aabb {
}
}

/// Returns the normalized longest axis of the bounding box.
#[inline]
pub fn get_longest_axis(self) -> Vector3 {
self.size.max_axis().to_unit_vector()
}

/// Returns the index of the longest axis of the bounding box.
/// Returns the longest side of this AABB as an axis index and its length.
///
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
#[inline]
pub fn get_longest_axis_index(self) -> Axis {
self.size.max_axis()
}

/// Returns the scalar length of the longest axis of the bounding box.
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
/// To get the unit vector along the axis, use [`Axis::to_unit_vector()`].
///
/// If you want to emulate the separate GDScript methods, you can do this:
/// ```no_run
/// # let aabb: gdnative::core_types::Aabb = todo!();
/// let (index, size) = aabb.get_longest_axis();
/// let axis = index.to_unit_vector();
/// ```
#[inline]
pub fn get_longest_axis_size(self) -> f32 {
pub fn get_longest_axis(self) -> (Axis, f32) {
let Vector3 { x, y, z } = self.size;
x.max(y).max(z)
}

/// Returns the normalized shortest axis of the bounding box.
#[inline]
pub fn get_shortest_axis(self) -> Vector3 {
self.size.min_axis().to_unit_vector()
(self.size.max_axis(), x.max(y).max(z))
}

/// Returns the index of the shortest axis of the bounding box.
/// Returns the shortest side of this AABB as an axis index and its length.
///
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
#[inline]
pub fn get_shortest_axis_index(self) -> Axis {
self.size.min_axis()
}

/// Returns the scalar length of the shortest axis of the bounding box.
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
/// To get the unit vector along the axis, use [`Axis::to_unit_vector()`].
///
/// If you want to emulate the separate GDScript methods, you can do this:
/// ```no_run
/// # let aabb: gdnative::core_types::Aabb = todo!();
/// let (index, size) = aabb.get_shortest_axis();
/// let axis = index.to_unit_vector();
/// ```
#[inline]
pub fn get_shortest_axis_size(self) -> f32 {
pub fn get_shortest_axis(self) -> (Axis, f32) {
let Vector3 { x, y, z } = self.size;
x.min(y).min(z)

(self.size.min_axis(), x.min(y).min(z))
}

/// Returns the support point in a given direction. This is useful for collision detection
Expand Down Expand Up @@ -384,13 +378,20 @@ mod tests {
#[test]
fn test_get_axis() {
let aabb = Aabb::new(Vector3::ZERO, Vector3::new(1.0, 2.0, 3.0));
assert!(aabb.get_longest_axis().is_equal_approx(Vector3::BACK));
assert_eq!(aabb.get_longest_axis_index(), Axis::Z);
assert!(aabb.get_longest_axis_size().is_equal_approx(3.0));

assert!(aabb.get_shortest_axis().is_equal_approx(Vector3::RIGHT));
assert_eq!(aabb.get_shortest_axis_index(), Axis::X);
assert!(aabb.get_shortest_axis_size().is_equal_approx(1.0));
let (longest_axis, longest_size) = aabb.get_longest_axis();
let longest_vector = longest_axis.to_unit_vector();

assert!(longest_vector.is_equal_approx(Vector3::BACK));
assert_eq!(longest_axis, Axis::Z);
assert!(longest_size.is_equal_approx(3.0));

let (shortest_axis, shortest_size) = aabb.get_shortest_axis();
let shortest_vector = shortest_axis.to_unit_vector();

assert!(shortest_vector.is_equal_approx(Vector3::RIGHT));
assert_eq!(shortest_axis, Axis::X);
assert!(shortest_size.is_equal_approx(1.0));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion gdnative-core/src/core_types/geom/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl Plane {
fn ensure_normalized(self) {
assert!(
self.normal.is_normalized(),
"Plane::normal {:?} does not have unit length",
"Plane {:?} -- normal does not have unit length",
self.normal
);
}
Expand Down
6 changes: 3 additions & 3 deletions gdnative-core/src/core_types/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub enum Axis {
}

impl Axis {
/// Returns this axis as a vector.
// Before making public, consider also Vector3::from_unit_axis() or so.
pub(crate) fn to_unit_vector(self) -> Vector3 {
/// Returns this axis as a vector of length 1, with only one component set.
#[inline]
pub fn to_unit_vector(self) -> Vector3 {
match self {
Axis::X => Vector3::RIGHT,
Axis::Y => Vector3::UP,
Expand Down

0 comments on commit 7df8463

Please sign in to comment.