Skip to content
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

Move Point out of cubic splines module and expand it #12747

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/bevy_color/src/laba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
impl_componentwise_point, Alpha, ClampColor, Hsla, Hsva, Hwba, LinearRgba, Luminance, Mix,
Oklaba, Srgba, StandardColor, Xyza,
impl_componentwise_vector_space, Alpha, ClampColor, Hsla, Hsva, Hwba, LinearRgba, Luminance,
Mix, Oklaba, Srgba, StandardColor, Xyza,
};
use bevy_reflect::prelude::*;

Expand Down Expand Up @@ -29,7 +29,7 @@ pub struct Laba {

impl StandardColor for Laba {}

impl_componentwise_point!(Laba, [lightness, a, b, alpha]);
impl_componentwise_vector_space!(Laba, [lightness, a, b, alpha]);

impl Laba {
/// Construct a new [`Laba`] color from components.
Expand Down
20 changes: 17 additions & 3 deletions crates/bevy_color/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ where
{
}

macro_rules! impl_componentwise_point {
macro_rules! impl_componentwise_vector_space {
($ty: ident, [$($element: ident),+]) => {
impl std::ops::Add<Self> for $ty {
type Output = Self;
Expand All @@ -181,6 +181,16 @@ macro_rules! impl_componentwise_point {
}
}

impl std::ops::Neg for $ty {
type Output = Self;

fn neg(self) -> Self::Output {
Self::Output {
$($element: -self.$element,)+
}
}
}

impl std::ops::Sub<Self> for $ty {
type Output = Self;

Expand Down Expand Up @@ -239,8 +249,12 @@ macro_rules! impl_componentwise_point {
}
}

impl bevy_math::cubic_splines::Point for $ty {}
impl bevy_math::VectorSpace for $ty {
const ZERO: Self = Self {
$($element: 0.0,)+
};
}
};
}

pub(crate) use impl_componentwise_point;
pub(crate) use impl_componentwise_vector_space;
6 changes: 3 additions & 3 deletions crates/bevy_color/src/linear_rgba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
color_difference::EuclideanDistance, impl_componentwise_point, Alpha, ClampColor, Luminance,
Mix, StandardColor,
color_difference::EuclideanDistance, impl_componentwise_vector_space, Alpha, ClampColor,
Luminance, Mix, StandardColor,
};
use bevy_math::Vec4;
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub struct LinearRgba {

impl StandardColor for LinearRgba {}

impl_componentwise_point!(LinearRgba, [red, green, blue, alpha]);
impl_componentwise_vector_space!(LinearRgba, [red, green, blue, alpha]);

impl LinearRgba {
/// A fully black color with full alpha.
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_color/src/oklaba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
color_difference::EuclideanDistance, impl_componentwise_point, Alpha, ClampColor, Hsla, Hsva,
Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba, StandardColor, Xyza,
color_difference::EuclideanDistance, impl_componentwise_vector_space, Alpha, ClampColor, Hsla,
Hsva, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba, StandardColor, Xyza,
};
use bevy_reflect::prelude::*;

Expand Down Expand Up @@ -29,7 +29,7 @@ pub struct Oklaba {

impl StandardColor for Oklaba {}

impl_componentwise_point!(Oklaba, [lightness, a, b, alpha]);
impl_componentwise_vector_space!(Oklaba, [lightness, a, b, alpha]);

impl Oklaba {
/// Construct a new [`Oklaba`] color from components.
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_color/src/srgba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::color_difference::EuclideanDistance;
use crate::{
impl_componentwise_point, Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor, Xyza,
impl_componentwise_vector_space, Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor,
Xyza,
};
use bevy_math::Vec4;
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -31,7 +32,7 @@ pub struct Srgba {

impl StandardColor for Srgba {}

impl_componentwise_point!(Srgba, [red, green, blue, alpha]);
impl_componentwise_vector_space!(Srgba, [red, green, blue, alpha]);

impl Srgba {
// The standard VGA colors, with alpha set to 1.0.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_color/src/xyza.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
impl_componentwise_point, Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor,
impl_componentwise_vector_space, Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor,
};
use bevy_reflect::prelude::*;

Expand Down Expand Up @@ -28,7 +28,7 @@ pub struct Xyza {

impl StandardColor for Xyza {}

impl_componentwise_point!(Xyza, [x, y, z, alpha]);
impl_componentwise_vector_space!(Xyza, [x, y, z, alpha]);

impl Xyza {
/// Construct a new [`Xyza`] color from components.
Expand Down
180 changes: 180 additions & 0 deletions crates/bevy_math/src/common_traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
use glam::{Quat, Vec2, Vec3, Vec3A, Vec4};
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Neg, Sub};

/// A type that supports the mathematical operations of a real vector space, irrespective of dimension.
/// In particular, this means that the implementing type supports:
/// - Scalar multiplication and division on the right by elements of `f32`
/// - Negation
/// - Addition and subtraction
/// - Zero
///
/// Within the limitations of floating point arithmetic, all the following are required to hold:
/// - (Associativity of addition) For all `u, v, w: Self`, `(u + v) + w == u + (v + w)`.
/// - (Commutativity of addition) For all `u, v: Self`, `u + v == v + u`.
/// - (Additive identity) For all `v: Self`, `v + Self::ZERO == v`.
/// - (Additive inverse) For all `v: Self`, `v - v == v + (-v) == Self::ZERO`.
/// - (Compatibility of multiplication) For all `a, b: f32`, `v: Self`, `v * (a * b) == (v * a) * b`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// - (Compatibility of multiplication) For all `a, b: f32`, `v: Self`, `v * (a * b) == (v * a) * b`.
/// - (Compatibility of scalar multiplication) For all `a, b: f32`, `v: Self`, `v * (a * b) == (v * a) * b`.

Worth calling this explicitly scalar multiplication to allow for possible future definition of vector multiplication in another trait.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote "multiplication" because really it's "compatibility of scalar multiplication with field multiplication", which was too many words. I'm also not sure how confusing it would be in the future anyway, since vector multiplication is almost always called something else anyway (e.g. inner product, dot product, etc.).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, that's fair. Until this trait is generic over the scalar field (probably never) I think that's specific enough language already.

/// - (Multiplicative identity) For all `v: Self`, `v * 1.0 == v`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// - (Multiplicative identity) For all `v: Self`, `v * 1.0 == v`.
/// - (Multiplicative scalar identity) For all `v: Self`, `v * 1.0 == v`.

/// - (Distributivity for vector addition) For all `a: f32`, `u, v: Self`, `(u + v) * a == u * a + v * a`.
/// - (Distributivity for scalar addition) For all `a, b: f32`, `v: Self`, `v * (a + b) == v * a + v * b`.
///
/// Note that, because implementing types use floating point arithmetic, they are not required to actually
/// implement `PartialEq` or `Eq`.
pub trait VectorSpace:
Mul<f32, Output = Self>
+ Div<f32, Output = Self>
+ Add<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Neg
+ Default
+ Debug
+ Clone
+ Copy
{
/// The zero vector, which is the identity of addition for the vector space type.
const ZERO: Self;

/// Perform vector space linear interpolation between this element and another, based
/// on the parameter `t`. When `t` is `0`, `self` is recovered. When `t` is `1`, `rhs`
/// is recovered.
///
/// Note that the value of `t` is not clamped by this function, so interpolating outside
/// of the interval `[0,1]` is allowed.
#[inline]
fn lerp(&self, rhs: Self, t: f32) -> Self {
*self * (1. - t) + rhs * t
}
}

// This is cursed and we should probably remove Quat from these.
impl VectorSpace for Quat {
const ZERO: Self = Quat::from_xyzw(0., 0., 0., 0.);
}

impl VectorSpace for Vec4 {
const ZERO: Self = Vec4::ZERO;
}

impl VectorSpace for Vec3 {
const ZERO: Self = Vec3::ZERO;
}

impl VectorSpace for Vec3A {
const ZERO: Self = Vec3A::ZERO;
}

impl VectorSpace for Vec2 {
const ZERO: Self = Vec2::ZERO;
}

impl VectorSpace for f32 {
const ZERO: Self = 0.0;
}

/// A type that supports the operations of a normed vector space; i.e. a norm operation in addition
/// to those of [`VectorSpace`]. Specifically, the implementor must guarantee that the following
/// relationships hold, within the limitations of floating point arithmetic:
/// - (Nonnegativity) For all `v: Self`, `v.norm() >= 0.0`.
/// - (Positive definiteness) For all `v: Self`, `v.norm() == 0.0` implies `v == Self::ZERO`.
/// - (Absolute homogeneity) For all `c: f32`, `v: Self`, `(v * c).norm() == v.norm() * c.abs()`.
/// - (Triangle inequality) For all `v, w: Self`, `(v + w).norm() <= v.norm() + w.norm()`.
///
/// Note that, because implementing types use floating point arithmetic, they are not required to actually
/// implement `PartialEq` or `Eq`.
pub trait NormedVectorSpace: VectorSpace {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mathematician in me also wants to see MetricSpace and InnerProductSpace traits with corresponding blanket impls, but I agree with Alice that we shouldn't add traits until the need arises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! I suspect it will come up before too long if someone tries to use NormedVectorSpace and ends up needing a dot product.

/// The size of this element. The return value should always be nonnegative.
fn norm(self) -> f32;

/// The squared norm of this element. Computing this is often faster than computing
/// [`NormedVectorSpace::norm`].
#[inline]
fn norm_squared(self) -> f32 {
self.norm() * self.norm()
}

/// The distance between this element and another, as determined by the norm.
#[inline]
fn distance(self, rhs: Self) -> f32 {
(rhs - self).norm()
}

/// The squared distance between this element and another, as determined by the norm. Note that
/// this is often faster to compute in practice than [`NormedVectorSpace::distance`].
#[inline]
fn distance_squared(self, rhs: Self) -> f32 {
(rhs - self).norm_squared()
}
}

impl NormedVectorSpace for Quat {
#[inline]
fn norm(self) -> f32 {
self.length()
}

#[inline]
fn norm_squared(self) -> f32 {
self.length_squared()
}
}

impl NormedVectorSpace for Vec4 {
#[inline]
fn norm(self) -> f32 {
self.length()
}

#[inline]
fn norm_squared(self) -> f32 {
self.length_squared()
}
}

impl NormedVectorSpace for Vec3 {
#[inline]
fn norm(self) -> f32 {
self.length()
}

#[inline]
fn norm_squared(self) -> f32 {
self.length_squared()
}
}

impl NormedVectorSpace for Vec3A {
#[inline]
fn norm(self) -> f32 {
self.length()
}

#[inline]
fn norm_squared(self) -> f32 {
self.length_squared()
}
}

impl NormedVectorSpace for Vec2 {
#[inline]
fn norm(self) -> f32 {
self.length()
}

#[inline]
fn norm_squared(self) -> f32 {
self.length_squared()
}
}

impl NormedVectorSpace for f32 {
#[inline]
fn norm(self) -> f32 {
self.abs()
}

#[inline]
fn norm_squared(self) -> f32 {
self * self
}
}