Skip to content

Commit 3344326

Browse files
committed
Rect2, Aabb: {longest|shortest}_axis() as single method + new Axis::to_unit_vector(); rename get_* methods
1 parent dfdcc4c commit 3344326

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

gdnative-core/src/core_types/geom/aabb.rs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ impl Aabb {
5252
///
5353
/// This method corresponds to the [`get_area`] GDScript method.
5454
///
55-
/// [get_area]: https://docs.godotengine.org/en/stable/classes/class_aabb.html#class-aabb-method-get-area
55+
/// [`get_area`]: https://docs.godotengine.org/en/stable/classes/class_aabb.html#class-aabb-method-get-area
5656
#[inline]
57-
pub fn get_volume(self) -> f32 {
57+
pub fn volume(self) -> f32 {
5858
self.size.x * self.size.y * self.size.z
5959
}
6060

6161
/// Returns true if the bounding box is flat or empty. See also
62-
/// [`get_volume`][Self::get_volume].
62+
/// [`volume`][Self::volume].
6363
///
6464
/// This method corresponds to the [`has_no_area`] GDScript method.
6565
///
@@ -118,46 +118,40 @@ impl Aabb {
118118
}
119119
}
120120

121-
/// Returns the normalized longest axis of the bounding box.
122-
#[inline]
123-
pub fn get_longest_axis(self) -> Vector3 {
124-
self.size.max_axis().to_unit_vector()
125-
}
126-
127-
/// Returns the index of the longest axis of the bounding box.
121+
/// Returns the longest side of this AABB as an axis index and its length.
128122
///
129-
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
130-
#[inline]
131-
pub fn get_longest_axis_index(self) -> Axis {
132-
self.size.max_axis()
133-
}
134-
135-
/// Returns the scalar length of the longest axis of the bounding box.
123+
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
124+
/// To get the unit vector along the axis, use [`Axis::to_unit_vector()`].
125+
///
126+
/// If you want to emulate the separate GDScript methods, you can do this:
127+
/// ```no_run
128+
/// # let aabb: gdnative::core_types::Aabb = todo!();
129+
/// let (index, size) = aabb.longest_axis();
130+
/// let axis = index.to_unit_vector();
131+
/// ```
136132
#[inline]
137-
pub fn get_longest_axis_size(self) -> f32 {
133+
pub fn longest_axis(self) -> (Axis, f32) {
138134
let Vector3 { x, y, z } = self.size;
139-
x.max(y).max(z)
140-
}
141135

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

148-
/// Returns the index of the shortest axis of the bounding box.
139+
/// Returns the shortest side of this AABB as an axis index and its length.
149140
///
150-
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
151-
#[inline]
152-
pub fn get_shortest_axis_index(self) -> Axis {
153-
self.size.min_axis()
154-
}
155-
156-
/// Returns the scalar length of the shortest axis of the bounding box.
141+
/// If multiple axes have the same length, then the first in order X, Y, Z is returned.
142+
/// To get the unit vector along the axis, use [`Axis::to_unit_vector()`].
143+
///
144+
/// If you want to emulate the separate GDScript methods, you can do this:
145+
/// ```no_run
146+
/// # let aabb: gdnative::core_types::Aabb = todo!();
147+
/// let (index, size) = aabb.shortest_axis();
148+
/// let axis = index.to_unit_vector();
149+
/// ```
157150
#[inline]
158-
pub fn get_shortest_axis_size(self) -> f32 {
151+
pub fn shortest_axis(self) -> (Axis, f32) {
159152
let Vector3 { x, y, z } = self.size;
160-
x.min(y).min(z)
153+
154+
(self.size.min_axis(), x.min(y).min(z))
161155
}
162156

163157
/// Returns the support point in a given direction. This is useful for collision detection
@@ -382,15 +376,22 @@ mod tests {
382376
}
383377

384378
#[test]
385-
fn test_get_axis() {
379+
fn test_longest_shortest_axis() {
386380
let aabb = Aabb::new(Vector3::ZERO, Vector3::new(1.0, 2.0, 3.0));
387-
assert!(aabb.get_longest_axis().is_equal_approx(Vector3::BACK));
388-
assert_eq!(aabb.get_longest_axis_index(), Axis::Z);
389-
assert!(aabb.get_longest_axis_size().is_equal_approx(3.0));
390381

391-
assert!(aabb.get_shortest_axis().is_equal_approx(Vector3::RIGHT));
392-
assert_eq!(aabb.get_shortest_axis_index(), Axis::X);
393-
assert!(aabb.get_shortest_axis_size().is_equal_approx(1.0));
382+
let (longest_axis, longest_size) = aabb.longest_axis();
383+
let longest_vector = longest_axis.to_unit_vector();
384+
385+
assert!(longest_vector.is_equal_approx(Vector3::BACK));
386+
assert_eq!(longest_axis, Axis::Z);
387+
assert!(longest_size.is_equal_approx(3.0));
388+
389+
let (shortest_axis, shortest_size) = aabb.shortest_axis();
390+
let shortest_vector = shortest_axis.to_unit_vector();
391+
392+
assert!(shortest_vector.is_equal_approx(Vector3::RIGHT));
393+
assert_eq!(shortest_axis, Axis::X);
394+
assert!(shortest_size.is_equal_approx(1.0));
394395
}
395396

396397
#[test]

gdnative-core/src/core_types/geom/plane.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl Plane {
242242
fn ensure_normalized(self) {
243243
assert!(
244244
self.normal.is_normalized(),
245-
"Plane::normal {:?} does not have unit length",
245+
"Plane {:?} -- normal does not have unit length",
246246
self.normal
247247
);
248248
}

gdnative-core/src/core_types/geom/rect2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ impl Rect2 {
5757

5858
/// Returns the area of the rectangle. See also [`has_no_area`][Self::has_no_area].
5959
#[inline]
60-
pub fn get_area(self) -> f32 {
60+
pub fn area(self) -> f32 {
6161
self.size.x * self.size.y
6262
}
6363

64-
/// Returns true if the rectangle is flat or empty. See also [`get_area`][Self::get_area].
64+
/// Returns true if the rectangle is flat or empty. See also [`area`][Self::area].
6565
///
6666
/// Note: If the `Rect2` has a negative size and is not flat or empty, this method will return
6767
/// true. Use [`abs`][Self::abs] to make the size positive.
6868
///
6969
/// # Example
7070
///
71-
/// ```rust
71+
/// ```
7272
/// # use gdnative::prelude::*;
7373
/// # fn main() {
7474
/// let rect = Rect2::new(
@@ -198,7 +198,7 @@ impl Rect2 {
198198
///
199199
/// # Example
200200
///
201-
/// ```rust
201+
/// ```
202202
/// # use gdnative::prelude::*;
203203
/// # fn main() {
204204
/// let rect = Rect2::new(

gdnative-core/src/core_types/vector3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ pub enum Axis {
2424
}
2525

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

0 commit comments

Comments
 (0)