Skip to content

Scalable primitives #20094

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use thiserror::Error;
use super::{Measured2d, Primitive2d, WindingOrder};
use crate::{
ops::{self, FloatPow},
prelude::{ScaleNonUniform2d, ScaleUniform},
Dir2, InvalidDirectionError, Isometry2d, Ray2d, Rot2, Vec2,
};

Expand Down Expand Up @@ -93,6 +94,22 @@ impl Measured2d for Circle {
}
}

impl ScaleUniform for Circle {
fn scale_uniform(&self, scale: f32) -> Self {
Self::new(scale * self.radius)
}
}

impl ScaleNonUniform2d for Circle {
type Output = Ellipse;

fn scale(&self, scale: Vec2) -> Self::Output {
Ellipse {
half_size: scale * self.radius,
}
}
}

/// A primitive representing an arc between two points on a circle.
///
/// An arc has no area.
Expand Down Expand Up @@ -1833,6 +1850,24 @@ impl Measured2d for Rectangle {
}
}

impl ScaleUniform for Rectangle {
fn scale_uniform(&self, scale: f32) -> Self {
Self {
half_size: scale * self.half_size,
}
}
}

impl ScaleNonUniform2d for Rectangle {
type Output = Self;

fn scale(&self, scale: Vec2) -> Self::Output {
Self {
half_size: scale * self.half_size,
}
}
}

/// A polygon with N vertices.
///
/// For a version without generics: [`BoxedPolygon`]
Expand Down
25 changes: 25 additions & 0 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::f32::consts::{FRAC_PI_3, PI};
use super::{Circle, Measured2d, Measured3d, Primitive2d, Primitive3d};
use crate::{
ops::{self, FloatPow},
prelude::{ScaleNonUniform3d, ScaleUniform},
Dir3, InvalidDirectionError, Isometry3d, Mat3, Ray3d, Vec2, Vec3,
};

Expand Down Expand Up @@ -88,6 +89,12 @@ impl Measured3d for Sphere {
}
}

impl ScaleUniform for Sphere {
fn scale_uniform(&self, scale: f32) -> Self {
Self::new(scale * self.radius)
}
}

/// A bounded plane in 3D space. It forms a surface starting from the origin with a defined height and width.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -731,6 +738,24 @@ impl Measured3d for Cuboid {
}
}

impl ScaleUniform for Cuboid {
fn scale_uniform(&self, scale: f32) -> Self {
Self {
half_size: scale * self.half_size,
}
}
}

impl ScaleNonUniform3d for Cuboid {
type Output = Self;

fn scale(&self, scale: Vec3) -> Self::Output {
Self {
half_size: scale * self.half_size,
}
}
}

/// A cylinder primitive centered on the origin
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
Expand Down
27 changes: 27 additions & 0 deletions crates/bevy_math/src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod dim2;
pub use dim2::*;
mod dim3;
pub use dim3::*;
use glam::{Vec2, Vec3};
mod polygon;
#[cfg(feature = "serialize")]
mod serde;
Expand Down Expand Up @@ -48,3 +49,29 @@ pub trait Measured3d {
/// Get the volume of the shape
fn volume(&self) -> f32;
}

/// A trait for uniformly scaling shapes
pub trait ScaleUniform {
/// Scale this primitive by the provided factor.
fn scale_uniform(&self, scale: f32) -> Self;
}

/// A trait for scaling 2D shapes non-uniformly
pub trait ScaleNonUniform2d: ScaleUniform {
/// The type of the scaled primitive.
/// Most shapes will be the same when scaled but i.e. a [`Circle`] may become an [`Ellipse`] when scaled.
type Output;

/// Scale the primitive along the X- and Y-axis
fn scale(&self, scale: Vec2) -> Self::Output;
}

/// A trait for scaling 3D shapes non-uniformly
pub trait ScaleNonUniform3d: ScaleUniform {
/// The type of the scaled primitive.
/// Most shapes will be the same when scaled but i.e. a [`Sphere`] may become an `Ellipsoid` when scaled.
type Output;

/// Scale the primitive along the X-, Y- and Z-axis
fn scale(&self, scale: Vec3) -> Self::Output;
}
Loading