From 34da833e39b6fedb0c015cee291b75614cf4cdc3 Mon Sep 17 00:00:00 2001 From: Osspial Date: Sat, 20 Jan 2018 23:54:59 -0500 Subject: [PATCH 1/2] Add map functions to points and vectors --- src/point.rs | 54 +++++++++++++++++++++++++-------------------------- src/vector.rs | 7 +++++++ 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/point.rs b/src/point.rs index 337e67b..1291495 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,21 @@ impl Point3 { macro_rules! impl_point { ($PointN:ident { $($field:ident),+ }, $VectorN:ident, $n:expr) => { + impl $PointN { + /// Construct a new vector, using the provided values. + #[inline] + pub fn new($($field: S),+) -> $PointN { + $PointN { $($field: $field),+ } + } + + #[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 +310,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..4f111ac 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -101,6 +101,13 @@ macro_rules! impl_vector { pub fn new($($field: S),+) -> $VectorN { $VectorN { $($field: $field),+ } } + + #[inline] + pub fn map(self, mut f: F) -> $VectorN + where F: FnMut(S) -> U + { + $VectorN { $($field: f(self.$field)),+ } + } } /// The short constructor. From def53ca3719cbe5c78cec54aabee477538ec6981 Mon Sep 17 00:00:00 2001 From: Osspial Date: Fri, 16 Mar 2018 16:32:46 -0400 Subject: [PATCH 2/2] Fix some documentation --- src/point.rs | 4 +++- src/vector.rs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/point.rs b/src/point.rs index 1291495..df5a299 100644 --- a/src/point.rs +++ b/src/point.rs @@ -80,12 +80,14 @@ impl Point3 { macro_rules! impl_point { ($PointN:ident { $($field:ident),+ }, $VectorN:ident, $n:expr) => { impl $PointN { - /// Construct a new vector, using the provided values. + /// 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 diff --git a/src/vector.rs b/src/vector.rs index 4f111ac..108792a 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -102,6 +102,8 @@ macro_rules! impl_vector { $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