Skip to content

Commit

Permalink
Derive PartialEq, Serialize, Deserialize and Reflect on primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
NiseVoid committed Jan 24, 2024
1 parent 143066d commit c93b6b3
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 23 deletions.
36 changes: 24 additions & 12 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl std::ops::Neg for Direction2d {
}

/// A circle primitive
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Circle {
/// The radius of the circle
pub radius: f32,
Expand Down Expand Up @@ -109,7 +110,8 @@ impl Circle {
}

/// An ellipse primitive
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Ellipse {
/// Half of the width and height of the ellipse.
///
Expand Down Expand Up @@ -154,7 +156,8 @@ impl Ellipse {

/// An unbounded plane in 2D space. It forms a separating surface through the origin,
/// stretching infinitely far
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Plane2d {
/// The normal of the plane. The plane will be placed perpendicular to this direction
pub normal: Direction2d,
Expand All @@ -178,7 +181,8 @@ impl Plane2d {
/// An infinite line along a direction in 2D space.
///
/// For a finite line: [`Segment2d`]
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Line2d {
/// The direction of the line. The line extends infinitely in both the given direction
/// and its opposite direction
Expand All @@ -188,7 +192,8 @@ impl Primitive2d for Line2d {}

/// A segment of a line along a direction in 2D space.
#[doc(alias = "LineSegment2d")]
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Segment2d {
/// The direction of the line segment
pub direction: Direction2d,
Expand Down Expand Up @@ -235,7 +240,8 @@ impl Segment2d {
/// A series of connected line segments in 2D space.
///
/// For a version without generics: [`BoxedPolyline2d`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
// #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Polyline2d<const N: usize> {
/// The vertices of the polyline
pub vertices: [Vec2; N],
Expand Down Expand Up @@ -264,7 +270,8 @@ impl<const N: usize> Polyline2d<N> {
/// in a `Box<[Vec2]>`.
///
/// For a version without alloc: [`Polyline2d`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct BoxedPolyline2d {
/// The vertices of the polyline
pub vertices: Box<[Vec2]>,
Expand All @@ -288,7 +295,8 @@ impl BoxedPolyline2d {
}

/// A triangle in 2D space
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Triangle2d {
/// The vertices of the triangle
pub vertices: [Vec2; 3],
Expand Down Expand Up @@ -361,7 +369,8 @@ impl Triangle2d {

/// A rectangle primitive
#[doc(alias = "Quad")]
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Rectangle {
/// Half of the width and height of the rectangle
pub half_size: Vec2,
Expand Down Expand Up @@ -394,7 +403,8 @@ impl Rectangle {
/// A polygon with N vertices.
///
/// For a version without generics: [`BoxedPolygon`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
// #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Polygon<const N: usize> {
/// The vertices of the `Polygon`
pub vertices: [Vec2; N],
Expand Down Expand Up @@ -423,7 +433,8 @@ impl<const N: usize> Polygon<N> {
/// in a `Box<[Vec2]>`.
///
/// For a version without alloc: [`Polygon`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct BoxedPolygon {
/// The vertices of the `BoxedPolygon`
pub vertices: Box<[Vec2]>,
Expand All @@ -447,7 +458,8 @@ impl BoxedPolygon {
}

/// A polygon where all vertices lie on a circle, equally far apart.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct RegularPolygon {
/// The circumcircle on which all vertices lie
pub circumcircle: Circle,
Expand Down
34 changes: 23 additions & 11 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl std::ops::Neg for Direction3d {
}

/// A sphere primitive
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Sphere {
/// The radius of the sphere
pub radius: f32,
Expand Down Expand Up @@ -114,7 +115,8 @@ impl Sphere {

/// An unbounded plane in 3D space. It forms a separating surface through the origin,
/// stretching infinitely far
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Plane3d {
/// The normal of the plane. The plane will be placed perpendicular to this direction
pub normal: Direction3d,
Expand All @@ -138,7 +140,8 @@ impl Plane3d {
/// An infinite line along a direction in 3D space.
///
/// For a finite line: [`Segment3d`]
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Line3d {
/// The direction of the line
pub direction: Direction3d,
Expand All @@ -147,7 +150,8 @@ impl Primitive3d for Line3d {}

/// A segment of a line along a direction in 3D space.
#[doc(alias = "LineSegment3d")]
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Segment3d {
/// The direction of the line
pub direction: Direction3d,
Expand Down Expand Up @@ -194,7 +198,8 @@ impl Segment3d {
/// A series of connected line segments in 3D space.
///
/// For a version without generics: [`BoxedPolyline3d`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
// #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Polyline3d<const N: usize> {
/// The vertices of the polyline
pub vertices: [Vec3; N],
Expand Down Expand Up @@ -223,7 +228,8 @@ impl<const N: usize> Polyline3d<N> {
/// in a `Box<[Vec3]>`.
///
/// For a version without alloc: [`Polyline3d`]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct BoxedPolyline3d {
/// The vertices of the polyline
pub vertices: Box<[Vec3]>,
Expand All @@ -247,7 +253,8 @@ impl BoxedPolyline3d {
}

/// A cuboid primitive, more commonly known as a box.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Cuboid {
/// Half of the width, height and depth of the cuboid
pub half_size: Vec3,
Expand Down Expand Up @@ -279,7 +286,8 @@ impl Cuboid {
}

/// A cylinder primitive
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Cylinder {
/// The radius of the cylinder
pub radius: f32,
Expand All @@ -300,7 +308,8 @@ impl Cylinder {

/// A capsule primitive.
/// A capsule is defined as a surface at a distance (radius) from a line
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Capsule {
/// The radius of the capsule
pub radius: f32,
Expand All @@ -321,7 +330,8 @@ impl Capsule {
}

/// A cone primitive.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Cone {
/// The radius of the base
pub radius: f32,
Expand All @@ -333,7 +343,8 @@ impl Primitive3d for Cone {}
/// A conical frustum primitive.
/// A conical frustum can be created
/// by slicing off a section of a cone.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct ConicalFrustum {
/// The radius of the top of the frustum
pub radius_top: f32,
Expand Down Expand Up @@ -364,6 +375,7 @@ pub enum TorusKind {

/// A torus primitive, often representing a ring or donut shape
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Torus {
/// The radius of the tube of the torus
#[doc(
Expand Down
109 changes: 109 additions & 0 deletions crates/bevy_reflect/src/impls/primitives2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate as bevy_reflect;
use crate::{ReflectDeserialize, ReflectSerialize};
use bevy_math::{primitives::*, Vec2};
use bevy_reflect_derive::{impl_reflect_struct, impl_reflect_value};

impl_reflect_value!(::bevy_math::primitives::Direction2d(
Debug,
PartialEq,
Serialize,
Deserialize
));

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct Circle {
radius: f32,
}
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct Ellipse {
pub half_size: Vec2,
}
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct Plane2d {
normal: Direction2d,
}
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct Line2d {
direction: Direction2d,
}
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct Segment2d {
direction: Direction2d,
half_length: f32,
}
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq)]
#[type_path = "bevy_math::primitives"]
struct Polyline2d<const N: usize> {
vertices: [Vec2; N],
}
);

// impl_reflect_struct!(
// #[reflect(Debug, PartialEq, Serialize, Deserialize)]
// #[type_path = "bevy_math::primitives"]
// struct BoxedPolyline2d {
// vertices: Box<[Vec2]>,
// }
// );

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct Triangle2d {
vertices: [Vec2; 3],
}
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct Rectangle {
half_size: Vec2,
}
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq)]
#[type_path = "bevy_math::primitives"]
struct Polygon<const N: usize> {
vertices: [Vec2; N],
}
);

// impl_reflect_struct!(
// #[reflect(Debug, PartialEq, Serialize, Deserialize)]
// #[type_path = "bevy_math::primitives"]
// struct BoxedPolygon {
// vertices: Box<[Vec2]>,
// }
// );

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize)]
#[type_path = "bevy_math::primitives"]
struct RegularPolygon {
circumcircle: Circle,
sides: usize,
}
);
Loading

0 comments on commit c93b6b3

Please sign in to comment.