From c287f097b35d1494773f8a9df85e372d2c46c0b8 Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Sun, 18 Feb 2024 19:04:11 +0200 Subject: [PATCH] Implement `Mul` and `Mul` for `Rotation` --- src/components/rotation.rs | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/components/rotation.rs b/src/components/rotation.rs index 69b2d161..190bc726 100644 --- a/src/components/rotation.rs +++ b/src/components/rotation.rs @@ -231,6 +231,50 @@ impl SubAssign for Rotation { } } +impl core::ops::Mul for Rotation { + type Output = Vector; + + fn mul(self, vector: Vector) -> Self::Output { + let rotated = self.rotate(vector); + + // Make sure the result is normalized. + // This can fail for non-unit quaternions. + debug_assert!(rotated.is_normalized()); + + rotated + } +} + +impl core::ops::Mul for Rotation { + type Output = Dir; + + fn mul(self, direction: Dir) -> Self::Output { + Dir::new_unchecked(self.rotate(direction.adjust_precision()).f32()) + } +} + +impl core::ops::Mul for &Rotation { + type Output = Vector; + + fn mul(self, vector: Vector) -> Self::Output { + let rotated = self.rotate(vector); + + // Make sure the result is normalized. + // This can fail for non-unit quaternions. + debug_assert!(rotated.is_normalized()); + + rotated + } +} + +impl core::ops::Mul for &Rotation { + type Output = Dir; + + fn mul(self, direction: Dir) -> Self::Output { + Dir::new_unchecked(self.rotate(direction.adjust_precision()).f32()) + } +} + #[cfg(feature = "2d")] impl From for Scalar { fn from(rot: Rotation) -> Self {