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

Derive Default for all math types #1029

Merged
merged 4 commits into from
Sep 2, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/fj-math/src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use parry3d_f64::bounding_volume::BoundingVolume as _;
use super::{Point, Vector};

/// An axis-aligned bounding box (AABB)
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(C)]
pub struct Aabb<const D: usize> {
/// The minimum coordinates of the AABB
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{Point, Scalar, Vector};
///
/// The dimensionality of the circle is defined by the const generic `D`
/// parameter.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Circle<const D: usize> {
center: Point<D>,
a: Vector<D>,
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{Point, Scalar, Triangle, Vector};
///
/// The dimensionality of the line is defined by the const generic `D`
/// parameter.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(C)]
pub struct Line<const D: usize> {
origin: Point<D>,
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
///
/// The dimensionality of the point is defined by the const generic `D`
/// parameter.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Copy, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(C)]
pub struct Point<const D: usize> {
/// The coordinates of the point
Expand Down
10 changes: 2 additions & 8 deletions crates/fj-math/src/poly_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{Point, Segment};
///
/// The dimensionality of the polygonal chain is defined by the const generic
/// `D` parameter.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(C)]
pub struct PolyChain<const D: usize> {
points: Vec<Point<D>>,
Expand All @@ -13,7 +13,7 @@ pub struct PolyChain<const D: usize> {
impl<const D: usize> PolyChain<D> {
/// Create an empty `PolyChain`
pub fn new() -> Self {
Self { points: Vec::new() }
Self::default()
}

/// Construct a polygonal chain from a number of points
Expand Down Expand Up @@ -73,12 +73,6 @@ impl<const D: usize> PolyChain<D> {
}
}

impl<const D: usize> Default for PolyChain<D> {
fn default() -> Self {
Self::new()
}
}

impl<P, Ps, const D: usize> From<Ps> for PolyChain<D>
where
P: Into<Point<D>>,
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use decorum::R64;
/// value is not NaN. This allows `Scalar` to provide implementations of [`Eq`],
/// [`Ord`], and [`Hash`], enabling `Scalar` (and types built on top of it), to
/// be used as keys in hash maps, hash sets, and similar types.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub struct Scalar(f64);

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::Point;
///
/// The dimensionality of the segment is defined by the const generic `D`
/// parameter.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Copy, Eq, Default, PartialEq, Hash, Ord, PartialOrd)]
#[repr(C)]
pub struct Segment<const D: usize> {
points: [Point<D>; 2],
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{Aabb, Point, Segment, Triangle, Vector};

/// An affine transform
#[repr(C)]
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Transform(nalgebra::Transform<f64, nalgebra::TAffine, 3>);

impl Transform {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{Point, Scalar};
///
/// The dimensionality of the triangle is defined by the const generic `D`
/// parameter.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(C)]
pub struct Triangle<const D: usize> {
points: [Point<D>; 3],
Expand Down
7 changes: 7 additions & 0 deletions crates/fj-math/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ impl ops::DerefMut for Vector<3> {
}
}

impl<const D: usize> Default for Vector<D> {
fn default() -> Self {
let components = [Scalar::default(); D];
Self { components }
}
}

impl<const D: usize> From<[Scalar; D]> for Vector<D> {
fn from(components: [Scalar; D]) -> Self {
Self { components }
Expand Down