-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Add basic impls of Rect2
, Rect2i
, Aabb
, Plane
#180
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use godot_ffi as sys; | ||
use sys::{ffi_methods, GodotFfi}; | ||
|
||
use super::Vector3; | ||
|
||
/// Axis-aligned bounding box in 3D space. | ||
/// | ||
/// `Aabb` consists of a position, a size, and several utility functions. It is typically used for | ||
/// fast overlap tests. | ||
/// | ||
/// Currently most methods are only available through [`InnerAabb`](super::inner::InnerAabb). | ||
/// | ||
/// The 2D counterpart to `Aabb` is [`Rect2`](super::Rect2). | ||
#[derive(Default, Copy, Clone, PartialEq, Debug)] | ||
#[repr(C)] | ||
pub struct Aabb { | ||
pub position: Vector3, | ||
pub size: Vector3, | ||
} | ||
|
||
impl Aabb { | ||
/// Create a new `Aabb` from a position and a size. | ||
/// | ||
/// _Godot equivalent: `Aabb(Vector3 position, Vector3 size)`_ | ||
#[inline] | ||
pub const fn new(position: Vector3, size: Vector3) -> Self { | ||
Self { position, size } | ||
} | ||
|
||
/// 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, | ||
} | ||
} | ||
|
||
/// The end of the `Aabb` calculated as `position + size`. | ||
/// | ||
/// _Godot equivalent: `Aabb.size` property_ | ||
#[inline] | ||
pub fn end(&self) -> Vector3 { | ||
self.position + self.size | ||
} | ||
|
||
/// Set size based on desired end-point. | ||
/// | ||
/// _Godot equivalent: `Aabb.size` property_ | ||
#[inline] | ||
pub fn set_end(&mut self, end: Vector3) { | ||
self.size = end - self.position | ||
} | ||
|
||
/// Returns `true` if the two `Aabb`s are approximately equal, by calling `is_equal_approx` on | ||
/// `position` and `size`. | ||
/// | ||
/// _Godot equivalent: `Aabb.is_equal_approx()`_ | ||
#[inline] | ||
pub fn is_equal_approx(&self, other: &Self) -> bool { | ||
self.position.is_equal_approx(other.position) && self.size.is_equal_approx(other.size) | ||
} | ||
|
||
/* Add in when `Aabb::abs()` is implemented. | ||
/// Assert that the size of the `Aabb` is not negative. | ||
/// | ||
/// Certain functions will fail to give a correct result if the size is negative. | ||
#[inline] | ||
pub fn assert_nonnegative(&self) { | ||
assert!( | ||
self.size.x >= 0.0 && self.size.y >= 0.0 && self.size.z >= 0.0, | ||
"size {:?} is negative", | ||
self.size | ||
); | ||
} | ||
*/ | ||
} | ||
|
||
impl GodotFfi for Aabb { | ||
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use std::ops::Neg; | ||
|
||
use godot_ffi as sys; | ||
use sys::{ffi_methods, GodotFfi}; | ||
|
||
use super::{is_equal_approx, real, Vector3}; | ||
|
||
/// 3D plane in [Hessian normal form](https://mathworld.wolfram.com/HessianNormalForm.html). | ||
/// | ||
/// The Hessian form defines all points `point` which satisfy the equation | ||
/// `dot(normal, point) + d == 0`, where `normal` is the normal vector and `d` | ||
/// the distance from the origin. | ||
/// | ||
/// Currently most methods are only available through [`InnerPlane`](super::inner::InnerPlane). | ||
/// | ||
/// Note: almost all methods on `Plane` require that the `normal` vector have | ||
/// unit length and will panic if this invariant is violated. This is not separately | ||
/// annotated for each method. | ||
#[derive(Copy, Clone, PartialEq, Debug)] | ||
#[repr(C)] | ||
pub struct Plane { | ||
pub normal: Vector3, | ||
pub d: real, | ||
} | ||
|
||
impl Plane { | ||
/// Creates a new `Plane` from the `normal` and the distance from the origin `d`. | ||
/// | ||
/// # Panics | ||
/// In contrast to construction via `Plane { normal, d }`, this verifies that `normal` has unit length, and will | ||
/// panic if this is not the case. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 normal, float d)`_ | ||
#[inline] | ||
pub fn new(unit_normal: Vector3, d: real) -> Self { | ||
let plane = Self { | ||
normal: unit_normal, | ||
d, | ||
}; | ||
plane.assert_normalized(); | ||
plane | ||
} | ||
|
||
/// Create a new `Plane` through the origin from a normal. | ||
/// | ||
/// # Panics | ||
/// See [`Self::new()`]. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 normal)`_ | ||
#[inline] | ||
pub fn from_normal_at_origin(normal: Vector3) -> Self { | ||
Self::new(normal, 0.0) | ||
} | ||
|
||
/// Create a new `Plane` from a normal and a point in the plane. | ||
/// | ||
/// # Panics | ||
/// See [`Self::new()`]. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 normal, Vector3 point)`_ | ||
#[inline] | ||
pub fn from_point_normal(point: Vector3, normal: Vector3) -> Self { | ||
Self::new(normal, normal.dot(point)) | ||
} | ||
|
||
/// Creates a new `Plane` from normal and origin distance. | ||
/// | ||
/// `nx`, `ny`, `nz` are used for the `normal` vector. | ||
/// `d` is the distance from the origin. | ||
/// | ||
/// # Panics | ||
/// See [`Self::new()`]. | ||
/// | ||
/// _Godot equivalent: `Plane(float a, float b, float c, float d)`_ | ||
#[inline] | ||
pub fn from_components(nx: real, ny: real, nz: real, d: real) -> Self { | ||
Self::new(Vector3::new(nx, ny, nz), d) | ||
} | ||
|
||
/// Creates a new `Plane` from three points, given in clockwise order. | ||
/// | ||
/// # Panics | ||
/// Will panic if all three points are colinear. | ||
/// | ||
/// _Godot equivalent: `Plane(Vector3 point1, Vector3 point2, Vector3 point3)`_ | ||
#[inline] | ||
pub fn from_points(a: Vector3, b: Vector3, c: Vector3) -> Self { | ||
let normal = (a - c).cross(a - b); | ||
assert_ne!( | ||
normal, | ||
Vector3::ZERO, | ||
"points {a}, {b}, {c} are all colinear" | ||
); | ||
let normal = normal.normalized(); | ||
Self { | ||
normal, | ||
d: normal.dot(a), | ||
} | ||
} | ||
|
||
/// Returns `true` if the two `Plane`s are approximately equal, by calling `is_equal_approx` on | ||
/// `normal` and `d` or on `-normal` and `-d`. | ||
/// | ||
/// _Godot equivalent: `Plane.is_equal_approx()`_ | ||
#[inline] | ||
pub fn is_equal_approx(&self, other: &Self) -> bool { | ||
(self.normal.is_equal_approx(other.normal) && is_equal_approx(self.d, other.d)) | ||
|| (self.normal.is_equal_approx(-other.normal) && is_equal_approx(self.d, -other.d)) | ||
} | ||
|
||
#[inline] | ||
fn assert_normalized(self) { | ||
assert!( | ||
self.normal.is_normalized(), | ||
"normal {:?} is not normalized", | ||
self.normal | ||
); | ||
} | ||
} | ||
|
||
impl Neg for Plane { | ||
type Output = Plane; | ||
|
||
/// Returns the negative value of the plane by flipping both the normal and the distance value. Meaning | ||
/// it creates a plane that is in the same place, but facing the opposite direction. | ||
fn neg(self) -> Self::Output { | ||
Self::new(-self.normal, -self.d) | ||
} | ||
} | ||
|
||
impl GodotFfi for Plane { | ||
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
/// Tests that none of the constructors panic for some simple planes. | ||
#[test] | ||
fn construction_succeeds() { | ||
let vec = Vector3::new(1.0, 2.0, 3.0).normalized(); | ||
let Vector3 { x, y, z } = vec; | ||
let _ = Plane::new(vec, 5.0); | ||
let _ = Plane::from_normal_at_origin(vec); | ||
let _ = Plane::from_point_normal(Vector3::new(10.0, 20.0, 30.0), vec); | ||
let _ = Plane::from_components(x, y, z, 5.0); | ||
let _ = Plane::from_points( | ||
Vector3::new(1.0, 2.0, 3.0), | ||
Vector3::new(2.0, 3.0, 1.0), | ||
Vector3::new(3.0, 2.0, 1.0), | ||
); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn new_unnormalized_panics() { | ||
let _ = Plane::new(Vector3::new(1.0, 2.0, 3.0), 5.0); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn from_points_colinear_panics() { | ||
let _ = Plane::from_points( | ||
Vector3::new(0.0, 0.0, 0.0), | ||
Vector3::new(0.0, 0.0, 1.0), | ||
Vector3::new(0.0, 0.0, 2.0), | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, I think I now understood why Godot begins with
normal
-- because all the constructors do, andnormal
(as opposed topoint
) is a direct member/property ofPlane
.So I'm not sure which is better... It's probably fine as it is now.