Skip to content

Commit

Permalink
Add some derives
Browse files Browse the repository at this point in the history
  • Loading branch information
NiseVoid committed Nov 9, 2023
1 parent f681821 commit 2795df6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
26 changes: 23 additions & 3 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::Primitive2d;
use crate::Vec2;

/// A normalized vector pointing in a direction in 2D space
#[derive(Clone, Copy, Debug)]
pub struct Direction2d(Vec2);

impl From<Vec2> for Direction2d {
Expand All @@ -17,20 +18,27 @@ impl Direction2d {
}
}

impl std::ops::Deref for Direction2d {
type Target = Vec2;
fn deref(&self) -> &Self::Target {
&self.0
}
}

/// An infinite half-line pointing in a direction in 2D space
#[derive(Clone, Copy, Debug)]
pub struct Ray2d(pub Direction2d);

/// An alias for [Rectangle]
pub type Quad = Rectangle;

/// A circle primitive
#[derive(Clone, Copy, Debug)]
pub struct Circle {
/// The radius of the circle
pub radius: f32,
}
impl Primitive2d for Circle {}

/// An unbounded plane in 2D space
#[derive(Clone, Copy, Debug)]
pub struct Plane2d {
/// The direction in which the plane points
pub normal: Direction2d,
Expand All @@ -39,13 +47,15 @@ impl Primitive2d for Plane2d {}

/// An infinite line along a direction in 2D space.
/// For a finite line: [LineSegment2d]
#[derive(Clone, Copy, Debug)]
pub struct Line2d {
/// The direction of the line
pub direction: Direction2d,
}
impl Primitive2d for Line2d {}

/// A section of a line along a direction in 2D space.
#[derive(Clone, Debug)]
pub struct LineSegment2d {
/// The direction of the line
pub direction: Direction2d,
Expand All @@ -58,6 +68,7 @@ impl Primitive2d for LineSegment2d {}

/// A line alone a path of N vertices in 2D space.
/// For a version without generics: [BoxedPolyline2d]
#[derive(Clone, Debug)]
pub struct Polyline2d<const N: usize> {
/// The vertices of the polyline
pub vertices: [Vec2; N],
Expand All @@ -66,20 +77,23 @@ impl<const N: usize> Primitive2d for Polyline2d<N> {}

/// A line alone a path of vertices in 2D space.
/// For a version without alloc: [Polyline2d]
#[derive(Clone, Debug)]
pub struct BoxedPolyline2d {
/// The vertices of the polyline
pub vertices: Box<[Vec2]>,
}
impl Primitive2d for BoxedPolyline2d {}

/// A triangle primitive
#[derive(Clone, Debug)]
pub struct Triangle {
/// The vertices of the triangle
pub vertcies: [Vec2; 3],
}
impl Primitive2d for Triangle {}

/// A rectangle primitive
#[derive(Clone, Copy, Debug)]
pub struct Rectangle {
/// The half width of the rectangle
pub half_width: f32,
Expand All @@ -88,8 +102,12 @@ pub struct Rectangle {
}
impl Primitive2d for Rectangle {}

/// An alias for [Rectangle]
pub type Quad = Rectangle;

/// A polygon with N vertices
/// For a version without generics: [BoxedPolygon]
#[derive(Clone, Debug)]
pub struct Polygon<const N: usize> {
/// The vertices of the polygon
pub vertices: [Vec2; N],
Expand All @@ -98,13 +116,15 @@ impl<const N: usize> Primitive2d for Polygon<N> {}

/// A polygon with a variable number of vertices
/// For a version without alloc: [Polygon]
#[derive(Clone, Debug)]
pub struct BoxedPolygon {
/// The vertices of the polygon
pub vertices: Box<[Vec2]>,
}
impl Primitive2d for BoxedPolygon {}

/// A polygon where all vertices lie on a circumscribed circle, equally far apart
#[derive(Clone, Copy, Debug)]
pub struct RegularPolygon {
/// The circumcircle on which all vertices lie
pub circumcircle: Circle,
Expand Down
18 changes: 18 additions & 0 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::Primitive3d;
use crate::Vec3;

/// A normalized vector pointing in a direction in 3D space
#[derive(Clone, Copy, Debug)]
pub struct Direction3d(Vec3);

impl From<Vec3> for Direction3d {
Expand All @@ -17,17 +18,27 @@ impl Direction3d {
}
}

impl std::ops::Deref for Direction3d {
type Target = Vec3;
fn deref(&self) -> &Self::Target {
&self.0
}
}

/// An infinite half-line pointing in a direction in 3D space
#[derive(Clone, Copy, Debug)]
pub struct Ray3d(pub Direction3d);

/// A sphere primitive
#[derive(Clone, Copy, Debug)]
pub struct Sphere {
/// The radius of the sphere
pub radius: f32,
}
impl Primitive3d for Sphere {}

/// An unbounded plane in 3D space
#[derive(Clone, Copy, Debug)]
pub struct Plane3d {
/// The direction in which the plane points
pub normal: Direction3d,
Expand All @@ -36,13 +47,15 @@ impl Primitive3d for Plane3d {}

/// An infinite line along a direction in 3D space.
/// For a finite line: [LineSegment3d]
#[derive(Clone, Copy, Debug)]
pub struct Line3d {
/// The direction of the line
pub direction: Direction3d,
}
impl Primitive3d for Line3d {}

/// A section of a line along a direction in 3D space.
#[derive(Clone, Debug)]
pub struct LineSegment3d {
/// The direction of the line
pub direction: Direction3d,
Expand All @@ -55,6 +68,7 @@ impl Primitive3d for LineSegment3d {}

/// A line alone a path of N vertices in 3D space.
/// For a version without generics: [BoxedPolyline3d]
#[derive(Clone, Debug)]
pub struct Polyline3d<const N: usize> {
/// The vertices of the polyline
pub vertices: [Vec3; N],
Expand All @@ -63,20 +77,23 @@ impl<const N: usize> Primitive3d for Polyline3d<N> {}

/// A line alone a path of vertices in 3D space.
/// For a version without alloc: [Polyline3d]
#[derive(Clone, Debug)]
pub struct BoxedPolyline3d {
/// The vertices of the polyline
pub vertices: Box<[Vec3]>,
}
impl Primitive3d for BoxedPolyline3d {}

/// A cuboid primitive, more commonly known as a box.
#[derive(Clone, Copy, Debug)]
pub struct Cuboid {
/// Half of the width, height and depth of the cuboid
pub half_extents: Vec3,
}
impl Primitive3d for Cuboid {}

/// A cylinder primitive
#[derive(Clone, Copy, Debug)]
pub struct Cylinder {
/// The radius of the cylinder
pub radius: f32,
Expand All @@ -86,6 +103,7 @@ pub struct Cylinder {
impl Primitive3d for Cylinder {}

/// A capsule primitive
#[derive(Clone, Copy, Debug)]
pub struct Capsule {
/// The radius of the capsule
pub radius: f32,
Expand Down

0 comments on commit 2795df6

Please sign in to comment.