
# Objective Now that #13432 has been merged, it's important we update our reflected types to properly opt into this feature. If we do not, then this could cause issues for users downstream who want to make use of reflection-based cloning. ## Solution This PR is broken into 4 commits: 1. Add `#[reflect(Clone)]` on all types marked `#[reflect(opaque)]` that are also `Clone`. This is mandatory as these types would otherwise cause the cloning operation to fail for any type that contains it at any depth. 2. Update the reflection example to suggest adding `#[reflect(Clone)]` on opaque types. 3. Add `#[reflect(clone)]` attributes on all fields marked `#[reflect(ignore)]` that are also `Clone`. This prevents the ignored field from causing the cloning operation to fail. Note that some of the types that contain these fields are also `Clone`, and thus can be marked `#[reflect(Clone)]`. This makes the `#[reflect(clone)]` attribute redundant. However, I think it's safer to keep it marked in the case that the `Clone` impl/derive is ever removed. I'm open to removing them, though, if people disagree. 4. Finally, I added `#[reflect(Clone)]` on all types that are also `Clone`. While not strictly necessary, it enables us to reduce the generated output since we can just call `Clone::clone` directly instead of calling `PartialReflect::reflect_clone` on each variant/field. It also means we benefit from any optimizations or customizations made in the `Clone` impl, including directly dereferencing `Copy` values and increasing reference counters. Along with that change I also took the liberty of adding any missing registrations that I saw could be applied to the type as well, such as `Default`, `PartialEq`, and `Hash`. There were hundreds of these to edit, though, so it's possible I missed quite a few. That last commit is **_massive_**. There were nearly 700 types to update. So it's recommended to review the first three before moving onto that last one. Additionally, I can break the last commit off into its own PR or into smaller PRs, but I figured this would be the easiest way of doing it (and in a timely manner since I unfortunately don't have as much time as I used to for code contributions). ## Testing You can test locally with a `cargo check`: ``` cargo check --workspace --all-features ```
191 lines
5.5 KiB
Rust
191 lines
5.5 KiB
Rust
use crate::{
|
|
ops,
|
|
primitives::{InfinitePlane3d, Plane2d},
|
|
Dir2, Dir3, Vec2, Vec3,
|
|
};
|
|
|
|
#[cfg(feature = "bevy_reflect")]
|
|
use bevy_reflect::Reflect;
|
|
#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
|
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
|
|
|
/// An infinite half-line starting at `origin` and going in `direction` in 2D space.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
|
#[cfg_attr(
|
|
feature = "bevy_reflect",
|
|
derive(Reflect),
|
|
reflect(Debug, PartialEq, Clone)
|
|
)]
|
|
#[cfg_attr(
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
|
reflect(Deserialize, Serialize)
|
|
)]
|
|
pub struct Ray2d {
|
|
/// The origin of the ray.
|
|
pub origin: Vec2,
|
|
/// The direction of the ray.
|
|
pub direction: Dir2,
|
|
}
|
|
|
|
impl Ray2d {
|
|
/// Create a new `Ray2d` from a given origin and direction
|
|
#[inline]
|
|
pub const fn new(origin: Vec2, direction: Dir2) -> Self {
|
|
Self { origin, direction }
|
|
}
|
|
|
|
/// Get a point at a given distance along the ray
|
|
#[inline]
|
|
pub fn get_point(&self, distance: f32) -> Vec2 {
|
|
self.origin + *self.direction * distance
|
|
}
|
|
|
|
/// Get the distance to a plane if the ray intersects it
|
|
#[inline]
|
|
pub fn intersect_plane(&self, plane_origin: Vec2, plane: Plane2d) -> Option<f32> {
|
|
let denominator = plane.normal.dot(*self.direction);
|
|
if ops::abs(denominator) > f32::EPSILON {
|
|
let distance = (plane_origin - self.origin).dot(*plane.normal) / denominator;
|
|
if distance > f32::EPSILON {
|
|
return Some(distance);
|
|
}
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
/// An infinite half-line starting at `origin` and going in `direction` in 3D space.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
|
#[cfg_attr(
|
|
feature = "bevy_reflect",
|
|
derive(Reflect),
|
|
reflect(Debug, PartialEq, Clone)
|
|
)]
|
|
#[cfg_attr(
|
|
all(feature = "serialize", feature = "bevy_reflect"),
|
|
reflect(Deserialize, Serialize)
|
|
)]
|
|
pub struct Ray3d {
|
|
/// The origin of the ray.
|
|
pub origin: Vec3,
|
|
/// The direction of the ray.
|
|
pub direction: Dir3,
|
|
}
|
|
|
|
impl Ray3d {
|
|
/// Create a new `Ray3d` from a given origin and direction
|
|
#[inline]
|
|
pub const fn new(origin: Vec3, direction: Dir3) -> Self {
|
|
Self { origin, direction }
|
|
}
|
|
|
|
/// Get a point at a given distance along the ray
|
|
#[inline]
|
|
pub fn get_point(&self, distance: f32) -> Vec3 {
|
|
self.origin + *self.direction * distance
|
|
}
|
|
|
|
/// Get the distance to a plane if the ray intersects it
|
|
#[inline]
|
|
pub fn intersect_plane(&self, plane_origin: Vec3, plane: InfinitePlane3d) -> Option<f32> {
|
|
let denominator = plane.normal.dot(*self.direction);
|
|
if ops::abs(denominator) > f32::EPSILON {
|
|
let distance = (plane_origin - self.origin).dot(*plane.normal) / denominator;
|
|
if distance > f32::EPSILON {
|
|
return Some(distance);
|
|
}
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn intersect_plane_2d() {
|
|
let ray = Ray2d::new(Vec2::ZERO, Dir2::Y);
|
|
|
|
// Orthogonal, and test that an inverse plane_normal has the same result
|
|
assert_eq!(
|
|
ray.intersect_plane(Vec2::Y, Plane2d::new(Vec2::Y)),
|
|
Some(1.0)
|
|
);
|
|
assert_eq!(
|
|
ray.intersect_plane(Vec2::Y, Plane2d::new(Vec2::NEG_Y)),
|
|
Some(1.0)
|
|
);
|
|
assert!(ray
|
|
.intersect_plane(Vec2::NEG_Y, Plane2d::new(Vec2::Y))
|
|
.is_none());
|
|
assert!(ray
|
|
.intersect_plane(Vec2::NEG_Y, Plane2d::new(Vec2::NEG_Y))
|
|
.is_none());
|
|
|
|
// Diagonal
|
|
assert_eq!(
|
|
ray.intersect_plane(Vec2::Y, Plane2d::new(Vec2::ONE)),
|
|
Some(1.0)
|
|
);
|
|
assert!(ray
|
|
.intersect_plane(Vec2::NEG_Y, Plane2d::new(Vec2::ONE))
|
|
.is_none());
|
|
|
|
// Parallel
|
|
assert!(ray
|
|
.intersect_plane(Vec2::X, Plane2d::new(Vec2::X))
|
|
.is_none());
|
|
|
|
// Parallel with simulated rounding error
|
|
assert!(ray
|
|
.intersect_plane(Vec2::X, Plane2d::new(Vec2::X + Vec2::Y * f32::EPSILON))
|
|
.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn intersect_plane_3d() {
|
|
let ray = Ray3d::new(Vec3::ZERO, Dir3::Z);
|
|
|
|
// Orthogonal, and test that an inverse plane_normal has the same result
|
|
assert_eq!(
|
|
ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::Z)),
|
|
Some(1.0)
|
|
);
|
|
assert_eq!(
|
|
ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::NEG_Z)),
|
|
Some(1.0)
|
|
);
|
|
assert!(ray
|
|
.intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::Z))
|
|
.is_none());
|
|
assert!(ray
|
|
.intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::NEG_Z))
|
|
.is_none());
|
|
|
|
// Diagonal
|
|
assert_eq!(
|
|
ray.intersect_plane(Vec3::Z, InfinitePlane3d::new(Vec3::ONE)),
|
|
Some(1.0)
|
|
);
|
|
assert!(ray
|
|
.intersect_plane(Vec3::NEG_Z, InfinitePlane3d::new(Vec3::ONE))
|
|
.is_none());
|
|
|
|
// Parallel
|
|
assert!(ray
|
|
.intersect_plane(Vec3::X, InfinitePlane3d::new(Vec3::X))
|
|
.is_none());
|
|
|
|
// Parallel with simulated rounding error
|
|
assert!(ray
|
|
.intersect_plane(
|
|
Vec3::X,
|
|
InfinitePlane3d::new(Vec3::X + Vec3::Z * f32::EPSILON)
|
|
)
|
|
.is_none());
|
|
}
|
|
}
|