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

Normalization for Point2D and Point3D #185

Merged
merged 1 commit into from Apr 20, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Normalization for Point2D and Point3D

  • Loading branch information
kvark committed Apr 20, 2017
commit 6aaa5de1b5136fbea9064a37138706ae4b6e5752
@@ -1,6 +1,6 @@
[package]
name = "euclid"
version = "0.11.1"
version = "0.11.2"
authors = ["The Servo Project Developers"]
description = "Geometry primitives"
documentation = "https://docs.rs/euclid/"
@@ -110,6 +110,16 @@ where T: Copy + Mul<T, Output=T> + Add<T, Output=T> + Sub<T, Output=T> {
pub fn cross(self, other: TypedPoint2D<T, U>) -> T {
self.x * other.y - self.y * other.x
}

#[inline]
pub fn normalize(self) -> Self where T: Float + ApproxEq<T> {
let dot = self.dot(self);
if dot.approx_eq(&T::zero()) {
self
} else {
self / dot.sqrt()
}
}
}

impl<T: Copy + Add<T, Output=T>, U> Add for TypedPoint2D<T, U> {
@@ -386,6 +396,16 @@ impl<T: Mul<T, Output=T> +
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x)
}

#[inline]
pub fn normalize(self) -> Self where T: Float + ApproxEq<T> {
let dot = self.dot(self);
if dot.approx_eq(&T::zero()) {
self
} else {
self / dot.sqrt()
}
}
}

impl<T: Copy + Add<T, Output=T>, U> Add for TypedPoint3D<T, U> {
@@ -414,6 +434,22 @@ impl <T: Copy + Neg<Output=T>, U> Neg for TypedPoint3D<T, U> {
}
}

impl<T: Copy + Mul<T, Output=T>, U> Mul<T> for TypedPoint3D<T, U> {
type Output = Self;
#[inline]
fn mul(self, scale: T) -> Self {
Self::new(self.x * scale, self.y * scale, self.z * scale)
}
}

impl<T: Copy + Div<T, Output=T>, U> Div<T> for TypedPoint3D<T, U> {
type Output = Self;
#[inline]
fn div(self, scale: T) -> Self {
Self::new(self.x / scale, self.y / scale, self.z / scale)
}
}

impl<T: Float, U> TypedPoint3D<T, U> {
pub fn min(self, other: TypedPoint3D<T, U>) -> TypedPoint3D<T, U> {
TypedPoint3D::new(self.x.min(other.x),
@@ -800,6 +836,16 @@ mod point2d {
assert_eq!(r, -59.0);
}

#[test]
pub fn test_normalize() {
let p0: Point2D<f32> = Point2D::zero();
let p1: Point2D<f32> = Point2D::new(4.0, 0.0);
let p2: Point2D<f32> = Point2D::new(3.0, -4.0);
assert_eq!(p0.normalize(), p0);
assert_eq!(p1.normalize(), Point2D::new(1.0, 0.0));
assert_eq!(p2.normalize(), Point2D::new(0.6, -0.8));
}

#[test]
pub fn test_min() {
let p1 = Point2D::new(1.0, 3.0);
@@ -872,6 +918,16 @@ mod point3d {
assert_eq!(p3, Point3D::new(-51.0, 105.0, -59.0));
}

#[test]
pub fn test_normalize() {
let p0: Point3D<f32> = Point3D::zero();
let p1: Point3D<f32> = Point3D::new(0.0, -6.0, 0.0);
let p2: Point3D<f32> = Point3D::new(1.0, 2.0, -2.0);
assert_eq!(p0.normalize(), p0);
assert_eq!(p1.normalize(), Point3D::new(0.0, -1.0, 0.0));
assert_eq!(p2.normalize(), Point3D::new(1.0/3.0, 2.0/3.0, -2.0/3.0));
}

#[test]
pub fn test_min() {
let p1 = Point3D::new(1.0, 3.0, 5.0);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.