diff --git a/src/point.rs b/src/point.rs index 337e67b..df5a299 100644 --- a/src/point.rs +++ b/src/point.rs @@ -64,33 +64,6 @@ pub struct Point3 { pub z: S, } -impl Point1 { - #[inline] - pub fn new(x: S) -> Point1 { - Point1 { x: x } - } - - impl_swizzle_functions!(Point1, Point2, Point3, S, x); -} - -impl Point2 { - #[inline] - pub fn new(x: S, y: S) -> Point2 { - Point2 { x: x, y: y } - } - - impl_swizzle_functions!(Point1, Point2, Point3, S, xy); -} - -impl Point3 { - #[inline] - pub fn new(x: S, y: S, z: S) -> Point3 { - Point3 { x: x, y: y, z: z } - } - - impl_swizzle_functions!(Point1, Point2, Point3, S, xyz); -} - impl Point3 { #[inline] pub fn from_homogeneous(v: Vector4) -> Point3 { @@ -106,6 +79,23 @@ impl Point3 { macro_rules! impl_point { ($PointN:ident { $($field:ident),+ }, $VectorN:ident, $n:expr) => { + impl $PointN { + /// Construct a new point, using the provided values. + #[inline] + pub fn new($($field: S),+) -> $PointN { + $PointN { $($field: $field),+ } + } + + /// Perform the given operation on each field in the point, returning a new point + /// constructed from the operations. + #[inline] + pub fn map(self, mut f: F) -> $PointN + where F: FnMut(S) -> U + { + $PointN { $($field: f(self.$field)),+ } + } + } + impl Array for $PointN { type Element = S; @@ -322,6 +312,18 @@ impl_point!(Point1 { x }, Vector1, 1); impl_point!(Point2 { x, y }, Vector2, 2); impl_point!(Point3 { x, y, z }, Vector3, 3); +impl Point1 { + impl_swizzle_functions!(Point1, Point2, Point3, S, x); +} + +impl Point2 { + impl_swizzle_functions!(Point1, Point2, Point3, S, xy); +} + +impl Point3 { + impl_swizzle_functions!(Point1, Point2, Point3, S, xyz); +} + impl_fixed_array_conversions!(Point1 { x: 0 }, 1); impl_fixed_array_conversions!(Point2 { x: 0, y: 1 }, 2); impl_fixed_array_conversions!(Point3 { x: 0, y: 1, z: 2 }, 3); diff --git a/src/vector.rs b/src/vector.rs index 8dfcb09..108792a 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -101,6 +101,15 @@ macro_rules! impl_vector { pub fn new($($field: S),+) -> $VectorN { $VectorN { $($field: $field),+ } } + + /// Perform the given operation on each field in the vector, returning a new point + /// constructed from the operations. + #[inline] + pub fn map(self, mut f: F) -> $VectorN + where F: FnMut(S) -> U + { + $VectorN { $($field: f(self.$field)),+ } + } } /// The short constructor.