From 6fbbdca6c879312777ea06731926cb621fc352c8 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Thu, 11 Jun 2015 14:38:14 +1000 Subject: [PATCH] Add simple Point3D type and Point4D type, and associated matrix methods. --- src/lib.rs | 2 +- src/matrix.rs | 13 +++++- src/point.rs | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9de11f7..b234981 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ extern crate num as num_lib; pub use matrix::Matrix4; pub use matrix2d::Matrix2D; -pub use point::Point2D; +pub use point::{Point2D, Point3D, Point4D}; pub use rect::Rect; pub use side_offsets::SideOffsets2D; pub use side_offsets::SideOffsets2DSimdI32; diff --git a/src/matrix.rs b/src/matrix.rs index e3f3f74..800a596 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -8,10 +8,10 @@ // except according to those terms. use approxeq::ApproxEq; -use point::Point2D; +use point::{Point2D, Point4D}; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct Matrix4 { pub m11: f32, pub m12: f32, pub m13: f32, pub m14: f32, pub m21: f32, pub m22: f32, pub m23: f32, pub m24: f32, @@ -120,6 +120,15 @@ impl Matrix4 { p.x * self.m12 + p.y * self.m22 + self.m42) } + #[inline] + pub fn transform_point4d(&self, p: &Point4D) -> Point4D { + let x = p.x * self.m11 + p.y * self.m21 + p.z * self.m31 + self.m41; + let y = p.x * self.m12 + p.y * self.m22 + p.z * self.m32 + self.m42; + let z = p.x * self.m13 + p.y * self.m23 + p.z * self.m33 + self.m43; + let w = p.x * self.m14 + p.y * self.m24 + p.z * self.m34 + self.m44; + Point4D::new(x, y, z, w) + } + pub fn to_array(&self) -> [f32; 16] { [ self.m11, self.m12, self.m13, self.m14, diff --git a/src/point.rs b/src/point.rs index 5f2ae2a..465b7ce 100644 --- a/src/point.rs +++ b/src/point.rs @@ -137,3 +137,129 @@ impl Point2D> { self.cast().unwrap() } } + +#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)] +pub struct Point3D { + pub x: T, + pub y: T, + pub z: T, +} + +impl Point3D { + #[inline] + pub fn zero() -> Point3D { + Point3D { x: Zero::zero(), y: Zero::zero(), z: Zero::zero() } + } +} + +impl fmt::Debug for Point3D { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "({:?},{:?},{:?})", self.x, self.y, self.z) + } +} + +impl fmt::Display for Point3D { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "({},{},{})", self.x, self.y, self.z) + } +} + +impl Point3D { + #[inline] + pub fn new(x: T, y: T, z: T) -> Point3D { + Point3D {x: x, y: y, z: z} + } +} + +impl> Add for Point3D { + type Output = Point3D; + fn add(self, other: Point3D) -> Point3D { + Point3D::new(self.x + other.x, + self.y + other.y, + self.z + other.z) + } +} + +impl> Sub for Point3D { + type Output = Point3D; + fn sub(self, other: Point3D) -> Point3D { + Point3D::new(self.x - other.x, + self.y - other.y, + self.z - other.z) + } +} + +impl > Neg for Point3D { + type Output = Point3D; + #[inline] + fn neg(self) -> Point3D { + Point3D::new(-self.x, -self.y, -self.z) + } +} + +#[derive(Clone, Copy, RustcDecodable, RustcEncodable, Eq, Hash, PartialEq)] +pub struct Point4D { + pub x: T, + pub y: T, + pub z: T, + pub w: T, +} + +impl Point4D { + #[inline] + pub fn zero() -> Point4D { + Point4D { + x: Zero::zero(), + y: Zero::zero(), + z: Zero::zero(), + w: Zero::zero() + } + } +} + +impl fmt::Debug for Point4D { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "({:?},{:?},{:?},{:?})", self.x, self.y, self.z, self.w) + } +} + +impl fmt::Display for Point4D { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "({},{},{},{})", self.x, self.y, self.z, self.w) + } +} + +impl Point4D { + #[inline] + pub fn new(x: T, y: T, z: T, w: T) -> Point4D { + Point4D {x: x, y: y, z: z, w: w} + } +} + +impl> Add for Point4D { + type Output = Point4D; + fn add(self, other: Point4D) -> Point4D { + Point4D::new(self.x + other.x, + self.y + other.y, + self.z + other.z, + self.w + other.w) + } +} + +impl> Sub for Point4D { + type Output = Point4D; + fn sub(self, other: Point4D) -> Point4D { + Point4D::new(self.x - other.x, + self.y - other.y, + self.z - other.z, + self.w - other.w) + } +} + +impl > Neg for Point4D { + type Output = Point4D; + #[inline] + fn neg(self) -> Point4D { + Point4D::new(-self.x, -self.y, -self.z, -self.w) + } +}