From 54d5f49d7e33ec273b0f5799404bd7c7bd032062 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Mon, 25 Sep 2023 12:21:42 -0700 Subject: [PATCH] Implement `Add/Sub` of `Translation*` for `Point*` Like https://github.com/servo/euclid/pull/506, these exist for the vector types. There doesn't seem to be any reason there shouldn't also work for translation types. The existing `translate` methods could be used for subtraction with `inv`, but that requires a complicated bound involving `Inv` and `Add` instead of a `Sub` bound. --- src/point.rs | 37 +++++++++++++++++++++++++++++++++++++ src/translation.rs | 18 ++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/point.rs b/src/point.rs index b3fcd8e..4b10a55 100644 --- a/src/point.rs +++ b/src/point.rs @@ -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; @@ -560,6 +561,15 @@ impl, U> AddAssign> for Point2D Add> for Point2D { + type Output = Point2D; + + #[inline] + fn add(self, other: Translation2D) -> Self::Output { + other.transform_point(self) + } +} + impl Sub for Point2D { type Output = Vector2D; @@ -602,6 +612,15 @@ impl, U> SubAssign> for Point2D Sub> for Point2D { + type Output = Point2D; + + #[inline] + fn sub(self, other: Translation2D) -> Self::Output { + other.transform_point_inv(self) + } +} + impl Mul for Point2D { type Output = Point2D; @@ -1337,6 +1356,15 @@ impl, U> AddAssign> for Point3D Add> for Point3D { + type Output = Point3D; + + #[inline] + fn add(self, other: Translation3D) -> Self::Output { + other.transform_point3d(&self) + } +} + impl Sub for Point3D { type Output = Vector3D; @@ -1384,6 +1412,15 @@ impl, U> SubAssign> for Point3D Sub> for Point3D { + type Output = Point3D; + + #[inline] + fn sub(self, other: Translation3D) -> Self::Output { + other.transform_point3d_inv(&self) + } +} + impl Mul for Point3D { type Output = Point3D; diff --git a/src/translation.rs b/src/translation.rs index 75e3fd2..9e44b17 100644 --- a/src/translation.rs +++ b/src/translation.rs @@ -220,6 +220,15 @@ impl Translation2D { 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) -> Point2D + 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) -> Rect @@ -525,6 +534,15 @@ impl Translation3D { 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) -> Point3D + 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) -> Point2D