Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transcendentals2 #554

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 44 additions & 7 deletions src/geometry/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ use base::{Matrix3, MatrixN, MatrixSlice, MatrixSliceMut, Unit, Vector3, Vector4

use geometry::Rotation;

/// Calculates the cardinal sine function
#[inline]
fn sinc<N: Real>(v: N) -> N {
if v == N::zero() {
N::zero()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
N::zero()
N::one()

}
else {
v.sin() / v
}
}

#[inline]
fn sinhc<N: Real>(v: N) -> N {
if v == N::zero() {
N::zero()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
N::zero()
N::one()

}
else {
v.sinh() / v
}
}

/// A quaternion. See the type alias `UnitQuaternion = Unit<Quaternion>` for a quaternion
/// that may be used as a rotation.
#[repr(C)]
Expand Down Expand Up @@ -124,6 +145,12 @@ impl<N: Real> Quaternion<N> {
Self::from(self.coords.normalize())
}

/// The imaginary part of this quaternion.
#[inline]
pub fn imag(&self) -> Vector3<N> {
self.coords.xyz()
}

/// The conjugate of this quaternion.
///
/// # Example
Expand All @@ -135,13 +162,7 @@ impl<N: Real> Quaternion<N> {
/// ```
#[inline]
pub fn conjugate(&self) -> Self {
let v = Vector4::new(
-self.coords[0],
-self.coords[1],
-self.coords[2],
self.coords[3],
);
Self::from(v)
Self::from_parts(self.w, -self.imag())
}

/// Inverts this quaternion if it is not zero.
Expand Down Expand Up @@ -506,6 +527,22 @@ impl<N: Real> Quaternion<N> {
pub fn normalize_mut(&mut self) -> N {
self.coords.normalize_mut()
}

/// Calculates quaternionic sin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Calculates quaternionic sin
/// Calculates the quaternionic cosinus.

#[inline]
pub fn cos(&self) -> Self {
let z = self.imag().magnitude();
let w = -self.w.sin() * sinhc(z);
Self::from_parts(self.w.cos() * z.cosh(), self.imag() * w)
}

/// Calculates quaternionic sin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Calculates quaternionic sin
/// Calculates the quaternionic sinus.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "sine" and "cosine" are the most common spellings of these in English.

#[inline]
pub fn sin(&self) -> Self {
let z = self.imag().magnitude();
let w = self.w.cos() * sinhc(z);
Self::from_parts(self.w.sin() * z.cosh(), self.imag() * w)
}
}

impl<N: Real + AbsDiffEq<Epsilon = N>> AbsDiffEq for Quaternion<N> {
Expand Down