Skip to content

Commit d7fc20c

Browse files
authored
Implemented Reflect for (almost) all bevy_math types (#13537)
# Objective Fixes #13535. ## Solution I implemented `Reflect` for close to all math types now, except for some types that it would cause issues (like some boxed types). ## Testing - Everything seems to still build, will await CI though. --- ## Changelog - Made close to all math types implement `Reflect`.
1 parent cef31ff commit d7fc20c

12 files changed

Lines changed: 96 additions & 1 deletion

File tree

crates/bevy_math/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ rand = { version = "0.8", features = [
2020
], default-features = false, optional = true }
2121
smallvec = { version = "1.11" }
2222

23-
bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", optional = true }
23+
bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
24+
"glam",
25+
], optional = true }
2426

2527
[dev-dependencies]
2628
approx = "0.5"

crates/bevy_math/src/affine3.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use glam::{Affine3A, Mat3, Vec3, Vec3Swizzles, Vec4};
22

3+
#[cfg(feature = "bevy_reflect")]
4+
use bevy_reflect::Reflect;
5+
36
/// Reduced-size version of `glam::Affine3A` for use when storage has
47
/// significant performance impact. Convert to `glam::Affine3A` to do
58
/// non-trivial calculations.
9+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
610
pub struct Affine3 {
711
/// Scaling, rotation, shears, and other non-translation affine transforms
812
pub matrix3: Mat3,

crates/bevy_math/src/aspect_ratio.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
33
use crate::Vec2;
44

5+
#[cfg(feature = "bevy_reflect")]
6+
use bevy_reflect::Reflect;
7+
58
/// An `AspectRatio` is the ratio of width to height.
69
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
10+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
711
pub struct AspectRatio(f32);
812

913
impl AspectRatio {

crates/bevy_math/src/bounding/bounded2d/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ mod primitive_impls;
33
use super::{BoundingVolume, IntersectsVolume};
44
use crate::prelude::{Mat2, Rotation2d, Vec2};
55

6+
#[cfg(feature = "bevy_reflect")]
7+
use bevy_reflect::Reflect;
8+
69
/// Computes the geometric center of the given set of points.
710
#[inline(always)]
811
fn point_cloud_2d_center(points: &[Vec2]) -> Vec2 {
@@ -29,6 +32,7 @@ pub trait Bounded2d {
2932
/// A 2D axis-aligned bounding box, or bounding rectangle
3033
#[doc(alias = "BoundingRectangle")]
3134
#[derive(Clone, Copy, Debug)]
35+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
3236
pub struct Aabb2d {
3337
/// The minimum, conventionally bottom-left, point of the box
3438
pub min: Vec2,
@@ -449,6 +453,7 @@ use crate::primitives::Circle;
449453

450454
/// A bounding circle
451455
#[derive(Clone, Copy, Debug)]
456+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
452457
pub struct BoundingCircle {
453458
/// The center of the bounding circle
454459
pub center: Vec2,

crates/bevy_math/src/bounding/bounded3d/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use glam::Mat3;
55
use super::{BoundingVolume, IntersectsVolume};
66
use crate::{Quat, Vec3, Vec3A};
77

8+
#[cfg(feature = "bevy_reflect")]
9+
use bevy_reflect::Reflect;
10+
811
/// Computes the geometric center of the given set of points.
912
#[inline(always)]
1013
fn point_cloud_3d_center(points: impl Iterator<Item = impl Into<Vec3A>>) -> Vec3A {
@@ -29,6 +32,7 @@ pub trait Bounded3d {
2932

3033
/// A 3D axis-aligned bounding box
3134
#[derive(Clone, Copy, Debug)]
35+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
3236
pub struct Aabb3d {
3337
/// The minimum point of the box
3438
pub min: Vec3A,
@@ -448,6 +452,7 @@ use crate::primitives::Sphere;
448452

449453
/// A bounding sphere
450454
#[derive(Clone, Copy, Debug)]
455+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
451456
pub struct BoundingSphere {
452457
/// The center of the bounding sphere
453458
pub center: Vec3A,

crates/bevy_math/src/bounding/raycast2d.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use super::{Aabb2d, BoundingCircle, IntersectsVolume};
22
use crate::{Dir2, Ray2d, Vec2};
33

4+
#[cfg(feature = "bevy_reflect")]
5+
use bevy_reflect::Reflect;
6+
47
/// A raycast intersection test for 2D bounding volumes
58
#[derive(Clone, Debug)]
9+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
610
pub struct RayCast2d {
711
/// The ray for the test
812
pub ray: Ray2d,
@@ -100,6 +104,7 @@ impl IntersectsVolume<BoundingCircle> for RayCast2d {
100104

101105
/// An intersection test that casts an [`Aabb2d`] along a ray.
102106
#[derive(Clone, Debug)]
107+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
103108
pub struct AabbCast2d {
104109
/// The ray along which to cast the bounding volume
105110
pub ray: RayCast2d,
@@ -137,6 +142,7 @@ impl IntersectsVolume<Aabb2d> for AabbCast2d {
137142

138143
/// An intersection test that casts a [`BoundingCircle`] along a ray.
139144
#[derive(Clone, Debug)]
145+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
140146
pub struct BoundingCircleCast {
141147
/// The ray along which to cast the bounding volume
142148
pub ray: RayCast2d,

crates/bevy_math/src/bounding/raycast3d.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use super::{Aabb3d, BoundingSphere, IntersectsVolume};
22
use crate::{Dir3A, Ray3d, Vec3A};
33

4+
#[cfg(feature = "bevy_reflect")]
5+
use bevy_reflect::Reflect;
6+
47
/// A raycast intersection test for 3D bounding volumes
58
#[derive(Clone, Debug)]
9+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
610
pub struct RayCast3d {
711
/// The origin of the ray.
812
pub origin: Vec3A,
@@ -95,6 +99,7 @@ impl IntersectsVolume<BoundingSphere> for RayCast3d {
9599

96100
/// An intersection test that casts an [`Aabb3d`] along a ray.
97101
#[derive(Clone, Debug)]
102+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
98103
pub struct AabbCast3d {
99104
/// The ray along which to cast the bounding volume
100105
pub ray: RayCast3d,
@@ -137,6 +142,7 @@ impl IntersectsVolume<Aabb3d> for AabbCast3d {
137142

138143
/// An intersection test that casts a [`BoundingSphere`] along a ray.
139144
#[derive(Clone, Debug)]
145+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
140146
pub struct BoundingSphereCast {
141147
/// The ray along which to cast the bounding volume
142148
pub ray: RayCast3d,

crates/bevy_math/src/cubic_splines.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ impl<P: VectorSpace> RationalGenerator<P> for CubicNurbs<P> {
607607
/// ### Continuity
608608
/// The curve is C0 continuous, meaning it has no holes or jumps.
609609
#[derive(Clone, Debug)]
610+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
610611
pub struct LinearSpline<P: VectorSpace> {
611612
/// The control points of the NURBS
612613
pub points: Vec<P>,

crates/bevy_math/src/float_ord.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::{
44
ops::Neg,
55
};
66

7+
#[cfg(feature = "bevy_reflect")]
8+
use bevy_reflect::Reflect;
9+
710
/// A wrapper for floats that implements [`Ord`], [`Eq`], and [`Hash`] traits.
811
///
912
/// This is a work around for the fact that the IEEE 754-2008 standard,
@@ -14,6 +17,11 @@ use std::{
1417
/// Wrapping a float with `FloatOrd` breaks conformance with the standard
1518
/// by sorting `NaN` as less than all other numbers and equal to any other `NaN`.
1619
#[derive(Debug, Copy, Clone)]
20+
#[cfg_attr(
21+
feature = "bevy_reflect",
22+
derive(Reflect),
23+
reflect(Debug, PartialEq, Hash)
24+
)]
1725
pub struct FloatOrd(pub f32);
1826

1927
impl PartialOrd for FloatOrd {

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ impl Measured2d for Circle {
9898
#[derive(Clone, Copy, Debug, PartialEq)]
9999
#[doc(alias("CircularArc", "CircleArc"))]
100100
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
101+
#[cfg_attr(
102+
feature = "bevy_reflect",
103+
derive(Reflect),
104+
reflect(Debug, PartialEq, Default)
105+
)]
106+
#[cfg_attr(
107+
all(feature = "serialize", feature = "bevy_reflect"),
108+
reflect(Serialize, Deserialize)
109+
)]
101110
pub struct Arc2d {
102111
/// The radius of the circle
103112
pub radius: f32,
@@ -256,6 +265,15 @@ impl Arc2d {
256265
/// We recommend normalizing circular sectors to have an angle in [0, 2π].
257266
#[derive(Clone, Copy, Debug, PartialEq)]
258267
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
268+
#[cfg_attr(
269+
feature = "bevy_reflect",
270+
derive(Reflect),
271+
reflect(Debug, PartialEq, Default)
272+
)]
273+
#[cfg_attr(
274+
all(feature = "serialize", feature = "bevy_reflect"),
275+
reflect(Serialize, Deserialize)
276+
)]
259277
pub struct CircularSector {
260278
/// The arc defining the sector
261279
#[cfg_attr(feature = "serialize", serde(flatten))]
@@ -386,6 +404,15 @@ impl CircularSector {
386404
/// We recommend normalizing circular segments to have an angle in [0, 2π].
387405
#[derive(Clone, Copy, Debug, PartialEq)]
388406
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
407+
#[cfg_attr(
408+
feature = "bevy_reflect",
409+
derive(Reflect),
410+
reflect(Debug, PartialEq, Default)
411+
)]
412+
#[cfg_attr(
413+
all(feature = "serialize", feature = "bevy_reflect"),
414+
reflect(Serialize, Deserialize)
415+
)]
389416
pub struct CircularSegment {
390417
/// The arc defining the segment
391418
#[cfg_attr(feature = "serialize", serde(flatten))]
@@ -1217,6 +1244,10 @@ impl Segment2d {
12171244
#[derive(Clone, Debug, PartialEq)]
12181245
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
12191246
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
1247+
#[cfg_attr(
1248+
all(feature = "serialize", feature = "bevy_reflect"),
1249+
reflect(Serialize, Deserialize)
1250+
)]
12201251
pub struct Polyline2d<const N: usize> {
12211252
/// The vertices of the polyline
12221253
#[cfg_attr(feature = "serialize", serde(with = "super::serde::array"))]
@@ -1538,6 +1569,10 @@ impl Measured2d for Rectangle {
15381569
#[derive(Clone, Debug, PartialEq)]
15391570
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
15401571
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
1572+
#[cfg_attr(
1573+
all(feature = "serialize", feature = "bevy_reflect"),
1574+
reflect(Serialize, Deserialize)
1575+
)]
15411576
pub struct Polygon<const N: usize> {
15421577
/// The vertices of the `Polygon`
15431578
#[cfg_attr(feature = "serialize", serde(with = "super::serde::array"))]

0 commit comments

Comments
 (0)