Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement Rect2 functions #242

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions godot-core/src/builtin/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ impl Aabb {
/// Create a new `Aabb` with the first corner at `position` and opposite corner at `end`.
#[inline]
pub fn from_corners(position: Vector3, end: Vector3) -> Self {
Self {
position,
size: position + end,
}
// Cannot use floating point arithmetic in const functions.
Self::new(position, end - position)
}

/// Returns an AABB with equivalent position and size,
/// modified so that the most-negative corner is the origin and the size is positive.
/// Returns an AABB with the same geometry, with most-negative corner as `position` and non-negative `size`.
#[inline]
pub fn abs(&self) -> Self {
Aabb {
position: self.position + self.size.coord_min(Vector3::ZERO),
Expand Down Expand Up @@ -141,6 +139,7 @@ impl Aabb {
}

/// Returns true if the AABB has a volume, and false if the AABB is flat, linear, empty, or has a negative size.
#[inline]
pub fn has_volume(&self) -> bool {
self.size.x > 0.0 && self.size.y > 0.0 && self.size.z > 0.0
}
Expand Down Expand Up @@ -168,6 +167,7 @@ impl Aabb {
}

/// Returns `true` if this AABB is finite, by calling `@GlobalScope.is_finite` on each component.
#[inline]
pub fn is_finite(&self) -> bool {
self.position.is_finite() && self.size.is_finite()
}
Expand All @@ -187,6 +187,7 @@ impl Aabb {
}

/// Returns the normalized longest axis of the AABB.
#[inline]
pub fn longest_axis(&self) -> Vector3 {
match self.longest_axis_index() {
Vector3Axis::X => Vector3::RIGHT,
Expand All @@ -196,16 +197,19 @@ impl Aabb {
}

/// 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_index()
}

/// Returns the scalar length of the longest axis of the AABB.
#[inline]
pub fn longest_axis_size(&self) -> real {
self.size.x.max(self.size.y.max(self.size.z))
}

/// Returns the normalized shortest axis of the AABB.
#[inline]
pub fn shortest_axis(&self) -> Vector3 {
match self.shortest_axis_index() {
Vector3Axis::X => Vector3::RIGHT,
Expand All @@ -215,16 +219,19 @@ impl Aabb {
}

/// 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_index()
}

/// Returns the scalar length of the shortest axis of the AABB.
#[inline]
pub fn shortest_axis_size(&self) -> real {
self.size.x.min(self.size.y.min(self.size.z))
}

/// Returns the support point in a given direction. This is useful for collision detection algorithms.
#[inline]
pub fn support(&self, dir: Vector3) -> Vector3 {
let half_extents = self.size * 0.5;
let relative_center_point = self.position + half_extents;
Expand All @@ -238,9 +245,12 @@ impl Aabb {
half_extents * signs + relative_center_point
}

/// Returns `true` if the AABB overlaps with `b` (i.e. they have at least one point in common).
/// Checks whether two AABBs have at least one point in common.
///
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = false)`_
/// Also returns `true` if the AABBs only touch each other (share a point/edge/face).
/// See [`intersects_exclude_borders`][Self::intersects_exclude_borders] if you want to return `false` in that case.
///
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = true)`_
#[inline]
pub fn intersects(&self, b: &Aabb) -> bool {
let end = self.end();
Expand All @@ -253,9 +263,13 @@ impl Aabb {
&& self.position.z <= end_b.z
}

/// Returns `true` if the AABB overlaps with `b` (i.e. they have at least one inner point in common).
/// Checks whether two AABBs have at least one _inner_ point in common (not on the borders).
///
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = true)`_
/// Returns `false` if the AABBs only touch each other (share a point/edge/face).
/// See [`intersects`][Self::intersects] if you want to return `true` in that case.
///
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = false)`_
#[inline]
pub fn intersects_exclude_borders(&self, &b: &Aabb) -> bool {
let end = self.end();
let end_b = b.end();
Expand All @@ -269,6 +283,7 @@ impl Aabb {
}

/// Returns `true` if the AABB is on both sides of a plane.
#[inline]
pub fn intersects_plane(&self, plane: &Plane) -> bool {
// The set of the edges of the AABB.
let points = [
Expand Down Expand Up @@ -301,6 +316,7 @@ impl Aabb {
///
/// # Panics
/// If `self.size` is negative.
#[inline]
pub fn intersects_ray(&self, from: Vector3, dir: Vector3) -> bool {
self.assert_nonnegative();

Expand All @@ -320,6 +336,7 @@ impl Aabb {
///
/// # Panics
/// If `self.size` is negative.
#[inline]
pub fn intersects_segment(&self, from: Vector3, to: Vector3) -> bool {
self.assert_nonnegative();

Expand Down
177 changes: 165 additions & 12 deletions godot-core/src/builtin/rect2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use godot_ffi as sys;
use sys::{ffi_methods, GodotFfi};

use crate::builtin::math::ApproxEq;
use crate::builtin::{real, Rect2i, Vector2};
use crate::builtin::{real, Rect2i, RectSide, Vector2};

/// 2D axis-aligned bounding box.
///
Expand All @@ -35,6 +35,13 @@ impl Rect2 {
Self { position, size }
}

/// Create a new `Rect2` with the first corner at `position` and the opposite corner at `end`.
#[inline]
pub fn from_corners(position: Vector2, end: Vector2) -> Self {
// Cannot use floating point arithmetic in const functions.
Self::new(position, end - position)
}

/// Create a new `Rect2` from four reals representing position `(x,y)` and size `(width,height)`.
///
/// _Godot equivalent: `Rect2(float x, float y, float width, float height)`_
Expand All @@ -57,33 +64,180 @@ impl Rect2 {
}
}

/// Create a new `Rect2` with the first corner at `position` and the opposite corner at `end`.
/// Returns a rectangle with the same geometry, with top-left corner as `position` and non-negative size.
#[inline]
pub fn from_corners(position: Vector2, end: Vector2) -> Self {
pub fn abs(&self) -> Self {
Self {
position,
size: position + end,
position: self.position + self.size.coord_min(Vector2::ZERO),
size: self.size.abs(),
}
}

/// The end of the `Rect2` calculated as `position + size`.
/// Whether `self` covers at least the entire area of `b` (and possibly more).
#[inline]
pub fn encloses(&self, b: Rect2) -> bool {
let end = self.end();
let b_end = b.end();

b.position.x >= self.position.x
&& b.position.y >= self.position.y
&& b_end.x <= end.x
&& b_end.y <= end.y
}

/// Returns a copy of this rectangle expanded to include a given point.
///
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
/// to get a positive sized equivalent rectangle for expanding.
#[inline]
pub fn expand(&self, to: Vector2) -> Self {
self.merge(Rect2::new(to, Vector2::ZERO))
}

/// Returns a larger rectangle that contains this `Rect2` and `b`.
///
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
/// to get a positive sized equivalent rectangle for merging.
#[inline]
pub fn merge(&self, b: Self) -> Self {
let position = self.position.coord_min(b.position);
let end = self.end().coord_max(b.end());

Self::from_corners(position, end)
}

/// Returns the area of the rectangle.
#[inline]
pub fn area(&self) -> real {
juliohq marked this conversation as resolved.
Show resolved Hide resolved
self.size.x * self.size.y
}

/// Returns the center of the Rect2, which is equal to `position + (size / 2)`.
#[inline]
pub fn center(&self) -> Vector2 {
self.position + (self.size / 2.0)
}

/// Returns a copy of the Rect2 grown by the specified `amount` on all sides.
#[inline]
#[must_use]
pub fn grow(&self, amount: real) -> Self {
juliohq marked this conversation as resolved.
Show resolved Hide resolved
let position = self.position - Vector2::new(amount, amount);
let size = self.size + Vector2::new(amount, amount) * 2.0;

Self { position, size }
}

/// Returns a copy of the Rect2 grown by the specified amount on each side individually.
#[inline]
pub fn grow_individual(&self, left: real, top: real, right: real, bottom: real) -> Self {
Self::from_components(
self.position.x - left,
self.position.y - top,
self.size.x + left + right,
self.size.y + top + bottom,
)
}

/// Returns a copy of the `Rect2` grown by the specified `amount` on the specified `RectSide`.
///
/// _Godot equivalent: `Rect2.size` property_
#[doc(alias = "size")]
/// `amount` may be negative, but care must be taken: If the resulting `size` has
/// negative components the computation may be incorrect.
#[inline]
pub fn grow_side(&self, side: RectSide, amount: real) -> Self {
match side {
RectSide::Left => self.grow_individual(amount, 0.0, 0.0, 0.0),
RectSide::Top => self.grow_individual(0.0, amount, 0.0, 0.0),
RectSide::Right => self.grow_individual(0.0, 0.0, amount, 0.0),
RectSide::Bottom => self.grow_individual(0.0, 0.0, 0.0, amount),
}
}

/// Returns `true` if the Rect2 has area, and `false` if the Rect2 is linear, empty, or has a negative size. See also `get_area`.
#[inline]
pub fn has_area(&self) -> bool {
self.size.x > 0.0 && self.size.y > 0.0
}

/// Returns `true` if the Rect2 contains a point. By convention, the right and bottom edges of the Rect2 are considered exclusive, so points on these edges are not included.
///
/// Note: This method is not reliable for Rect2 with a negative size. Use `abs` to get a positive sized equivalent rectangle to check for contained points.
#[inline]
pub fn has_point(&self, point: Vector2) -> bool {
let point = point - self.position;

point.abs() == point && point.x < self.size.x && point.y < self.size.y
}

/// Returns the intersection of this Rect2 and `b`. If the rectangles do not intersect, an empty Rect2 is returned.
#[inline]
pub fn intersection(&self, b: Self) -> Option<Self> {
if !self.intersects(b) {
return None;
}

let mut rect = b;
rect.position = rect.position.coord_max(self.position);

let end = self.end();
let end_b = b.end();
rect.size = end.coord_min(end_b) - rect.position;

Some(rect)
}

/// Checks whether two rectangles have at least one point in common.
///
/// Also returns `true` if the rects only touch each other (share a point/edge).
/// See [`intersects_exclude_borders`][Self::intersects_exclude_borders] if you want to return `false` in that case.
///
/// _Godot equivalent: `Rect2.intersects(Rect2 b, bool include_borders = true)`_
#[inline]
pub fn intersects(&self, b: Self) -> bool {
let end = self.end();
let end_b = b.end();

self.position.x <= end_b.x
&& end.x >= b.position.x
&& self.position.y <= end_b.y
&& end.y >= b.position.y
}

/// Checks whether two rectangles have at least one _inner_ point in common (not on the borders).
///
/// Returns `false` if the rects only touch each other (share a point/edge).
/// See [`intersects`][Self::intersects] if you want to return `true` in that case.
///
/// _Godot equivalent: `Rect2.intersects(AABB b, bool include_borders = false)`_
#[inline]
pub fn intersects_exclude_borders(&self, b: Self) -> bool {
let end = self.end();
let end_b = b.end();

self.position.x < end_b.x
&& end.x > b.position.x
&& self.position.y < end_b.y
&& end.y > b.position.y
}

/// Returns `true` if this Rect2 is finite, by calling `@GlobalScope.is_finite` on each component.
#[inline]
pub fn is_finite(&self) -> bool {
self.position.is_finite() && self.size.is_finite()
}

/// The end of the `Rect2` calculated as `position + size`.
#[inline]
pub fn end(&self) -> Vector2 {
self.position + self.size
}

/// Set size based on desired end-point.
///
/// _Godot equivalent: `Rect2.size` property_
#[inline]
pub fn set_end(&mut self, end: Vector2) {
self.size = end - self.position
}

/* Add in when `Rect2::abs()` is implemented.
/// Assert that the size of the `Rect2` is not negative.
///
/// Certain functions will fail to give a correct result if the size is negative.
Expand All @@ -95,7 +249,6 @@ impl Rect2 {
self.size
);
}
*/
}

// SAFETY:
Expand Down
1 change: 1 addition & 0 deletions itest/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod packed_array_test;
mod plane_test;
mod projection_test;
mod quaternion_test;
mod rect2_test;
mod rect2i_test;
mod rid_test;
mod signal_test;
Expand Down
Loading