Add map functions to points and vectors

This commit is contained in:
Osspial 2018-01-20 23:54:59 -05:00
parent fec4e8363d
commit 34da833e39
2 changed files with 34 additions and 27 deletions

View file

@ -64,33 +64,6 @@ pub struct Point3<S> {
pub z: S,
}
impl<S: BaseNum> Point1<S> {
#[inline]
pub fn new(x: S) -> Point1<S> {
Point1 { x: x }
}
impl_swizzle_functions!(Point1, Point2, Point3, S, x);
}
impl<S: BaseNum> Point2<S> {
#[inline]
pub fn new(x: S, y: S) -> Point2<S> {
Point2 { x: x, y: y }
}
impl_swizzle_functions!(Point1, Point2, Point3, S, xy);
}
impl<S: BaseNum> Point3<S> {
#[inline]
pub fn new(x: S, y: S, z: S) -> Point3<S> {
Point3 { x: x, y: y, z: z }
}
impl_swizzle_functions!(Point1, Point2, Point3, S, xyz);
}
impl<S: BaseNum> Point3<S> {
#[inline]
pub fn from_homogeneous(v: Vector4<S>) -> Point3<S> {
@ -106,6 +79,21 @@ impl<S: BaseNum> Point3<S> {
macro_rules! impl_point {
($PointN:ident { $($field:ident),+ }, $VectorN:ident, $n:expr) => {
impl<S> $PointN<S> {
/// Construct a new vector, using the provided values.
#[inline]
pub fn new($($field: S),+) -> $PointN<S> {
$PointN { $($field: $field),+ }
}
#[inline]
pub fn map<U, F>(self, mut f: F) -> $PointN<U>
where F: FnMut(S) -> U
{
$PointN { $($field: f(self.$field)),+ }
}
}
impl<S: BaseNum> Array for $PointN<S> {
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<S: Copy> Point1<S> {
impl_swizzle_functions!(Point1, Point2, Point3, S, x);
}
impl<S: Copy> Point2<S> {
impl_swizzle_functions!(Point1, Point2, Point3, S, xy);
}
impl<S: Copy> Point3<S> {
impl_swizzle_functions!(Point1, Point2, Point3, S, xyz);
}
impl_fixed_array_conversions!(Point1<S> { x: 0 }, 1);
impl_fixed_array_conversions!(Point2<S> { x: 0, y: 1 }, 2);
impl_fixed_array_conversions!(Point3<S> { x: 0, y: 1, z: 2 }, 3);

View file

@ -101,6 +101,13 @@ macro_rules! impl_vector {
pub fn new($($field: S),+) -> $VectorN<S> {
$VectorN { $($field: $field),+ }
}
#[inline]
pub fn map<U, F>(self, mut f: F) -> $VectorN<U>
where F: FnMut(S) -> U
{
$VectorN { $($field: f(self.$field)),+ }
}
}
/// The short constructor.