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

View File

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