Cleaned up exp(), log() and powf() for Quaternion. Also, they’re for Quaternion only as we cannot define them on UnitQuaternion.

This commit is contained in:
Dimitri Sabadie 2016-07-29 17:51:35 +02:00
parent 213cc41f7d
commit 988d9272d2
1 changed files with 14 additions and 14 deletions

View File

@ -570,24 +570,24 @@ impl<N: Arbitrary + BaseFloat> Arbitrary for UnitQuaternion<N> {
impl Quaternion<f32> { impl Quaternion<f32> {
/// Compute the exponential of a quaternion. /// Compute the exponential of a quaternion.
/// pub fn exp(&self) -> Self {
/// This function yields a `UnitQuaternion<f32>`. let v = *self.vector();
pub fn exp(&self) -> UnitQuaternion<f32> { let nn = v.norm_squared();
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])) if nn.is_zero() {
::one()
} else {
let n = nn.sqrt();
let nv = v / n * n.sin();
Quaternion::new(n.cos(), nv.x, nv.y, nv.z)
}
} }
}
impl UnitQuaternion<f32> { /// Compute the natural logarithm (a.k.a ln()) of a quaternion.
/// Compute the natural logarithm (a.k.a ln()) of a unit quaternion.
/// ///
/// Becareful, this function yields a `Quaternion<f32>`, losing the unit property. /// Becareful, this function yields a `Quaternion<f32>`, losing the unit property.
pub fn log(&self) -> Quaternion<f32> { pub fn log(&self) -> Self {
let q = self.quaternion(); (Quaternion { w: 0., .. *self }).normalize() * self.w.acos()
(Quaternion { w: 0., .. *q }).normalize() * q.w.acos()
} }
/// Raise the quaternion to a given floating power. /// Raise the quaternion to a given floating power.