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

Implement Add/Sub of Translation* for Point* #507

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::length::Length;
use crate::num::*;
use crate::scale::Scale;
use crate::size::{Size2D, Size3D};
use crate::translation::{Translation2D, Translation3D};
use crate::vector::{vec2, vec3, Vector2D, Vector3D};
use core::cmp::{Eq, PartialEq};
use core::fmt;
Expand Down Expand Up @@ -560,6 +561,15 @@ impl<T: Copy + Add<T, Output = T>, U> AddAssign<Vector2D<T, U>> for Point2D<T, U
}
}

impl<T: Copy + Add, Src, Dst> Add<Translation2D<T, Src, Dst>> for Point2D<T, Src> {
type Output = Point2D<T::Output, Dst>;

#[inline]
fn add(self, other: Translation2D<T, Src, Dst>) -> Self::Output {
other.transform_point(self)
}
}

impl<T: Sub, U> Sub for Point2D<T, U> {
type Output = Vector2D<T::Output, U>;

Expand Down Expand Up @@ -602,6 +612,15 @@ impl<T: Copy + Sub<T, Output = T>, U> SubAssign<Vector2D<T, U>> for Point2D<T, U
}
}

impl<T: Copy + Sub, Src, Dst> Sub<Translation2D<T, Src, Dst>> for Point2D<T, Dst> {
type Output = Point2D<T::Output, Src>;

#[inline]
fn sub(self, other: Translation2D<T, Src, Dst>) -> Self::Output {
other.transform_point_inv(self)
}
}

impl<T: Copy + Mul, U> Mul<T> for Point2D<T, U> {
type Output = Point2D<T::Output, U>;

Expand Down Expand Up @@ -1337,6 +1356,15 @@ impl<T: Copy + Add<T, Output = T>, U> AddAssign<Vector3D<T, U>> for Point3D<T, U
}
}

impl<T: Copy + Add, Src, Dst> Add<Translation3D<T, Src, Dst>> for Point3D<T, Src> {
type Output = Point3D<T::Output, Dst>;

#[inline]
fn add(self, other: Translation3D<T, Src, Dst>) -> Self::Output {
other.transform_point3d(&self)
}
}

impl<T: Sub, U> Sub for Point3D<T, U> {
type Output = Vector3D<T::Output, U>;

Expand Down Expand Up @@ -1384,6 +1412,15 @@ impl<T: Copy + Sub<T, Output = T>, U> SubAssign<Vector3D<T, U>> for Point3D<T, U
}
}

impl<T: Copy + Sub, Src, Dst> Sub<Translation3D<T, Src, Dst>> for Point3D<T, Dst> {
type Output = Point3D<T::Output, Src>;

#[inline]
fn sub(self, other: Translation3D<T, Src, Dst>) -> Self::Output {
other.transform_point3d_inv(&self)
}
}

impl<T: Copy + Mul, U> Mul<T> for Point3D<T, U> {
type Output = Point3D<T::Output, U>;

Expand Down
18 changes: 18 additions & 0 deletions src/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ impl<T: Copy, Src, Dst> Translation2D<T, Src, Dst> {
point2(p.x + self.x, p.y + self.y)
}

/// Translate a point by inverse and cast its unit.
#[inline]
pub(crate) fn transform_point_inv(&self, p: Point2D<T, Dst>) -> Point2D<T::Output, Src>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add _inv methods like this. You could relax the requirements on inverse or simply do the math manually in sub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are pub(crate) since I wasn't sure they made sense in the public API or what to name them. Having them alongside the transformations in the opposite direction make it easier to see that they're correct, but the body of the function could definitely be moved directly into the Sub implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the body of the function could definitely be moved directly into the Sub implementation.

This would be my preference.

where
T: Sub,
{
point2(p.x - self.x, p.y - self.y)
}

/// Translate a rectangle and cast its unit.
#[inline]
pub fn transform_rect(&self, r: &Rect<T, Src>) -> Rect<T::Output, Dst>
Expand Down Expand Up @@ -525,6 +534,15 @@ impl<T: Copy, Src, Dst> Translation3D<T, Src, Dst> {
point3(p.x + self.x, p.y + self.y, p.z + self.z)
}

/// Translate a point by inverse and cast its unit.
#[inline]
pub(crate) fn transform_point3d_inv(&self, p: &Point3D<T, Dst>) -> Point3D<T::Output, Src>
where
T: Sub,
{
point3(p.x - self.x, p.y - self.y, p.z - self.z)
}

/// Translate a point and cast its unit.
#[inline]
pub fn transform_point2d(&self, p: &Point2D<T, Src>) -> Point2D<T::Output, Dst>
Expand Down