From 213cc41f7d34c58c28fec7db157f31846b470c53 Mon Sep 17 00:00:00 2001 From: Dimitri Sabadie Date: Fri, 29 Jul 2016 15:56:04 +0200 Subject: [PATCH] Added exp(), log() and powf() for Quaternion / UnitQuaternion. --- src/structs/quaternion.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/structs/quaternion.rs b/src/structs/quaternion.rs index d785321a..52c7b26d 100644 --- a/src/structs/quaternion.rs +++ b/src/structs/quaternion.rs @@ -568,6 +568,33 @@ impl Arbitrary for UnitQuaternion { } } +impl Quaternion { + /// Compute the exponential of a quaternion. + /// + /// This function yields a `UnitQuaternion`. + pub fn exp(&self) -> UnitQuaternion { + let v = Vector3::new(self.i, self.j, self.k); + let n = v.norm(); + let z = v / n * n.sin(); + + UnitQuaternion::new_with_quaternion(Quaternion::new(n.cos(), z[0], z[1], z[2])) + } +} + +impl UnitQuaternion { + /// Compute the natural logarithm (a.k.a ln()) of a unit quaternion. + /// + /// Becareful, this function yields a `Quaternion`, losing the unit property. + pub fn log(&self) -> Quaternion { + let q = self.quaternion(); + (Quaternion { w: 0., .. *q }).normalize() * q.w.acos() + } + + /// Raise the quaternion to a given floating power. + pub fn powf(&self, n: f32) -> Self { + (self.log() * n).exp() + } +} pord_impl!(Quaternion, w, i, j, k); vec_axis_impl!(Quaternion, w, i, j, k);