nalgebra/src/geometry/quaternion_alga.rs

308 lines
6.8 KiB
Rust
Raw Normal View History

use num::Zero;
2018-10-22 13:00:10 +08:00
use alga::general::{
AbstractGroup, AbstractGroupAbelian, AbstractLoop, AbstractMagma, AbstractModule,
2019-02-03 22:45:25 +08:00
AbstractMonoid, AbstractQuasigroup, AbstractSemigroup, Additive, Id, Identity, TwoSidedInverse, Module,
2019-03-25 18:21:41 +08:00
Multiplicative, RealField,
2018-10-22 13:00:10 +08:00
};
use alga::linear::{
AffineTransformation, DirectIsometry, FiniteDimVectorSpace, Isometry, NormedSpace,
OrthogonalTransformation, ProjectiveTransformation, Rotation, Similarity, Transformation,
VectorSpace,
};
2019-03-23 21:29:07 +08:00
use crate::base::{Vector3, Vector4};
use crate::geometry::{Point3, Quaternion, UnitQuaternion};
2019-03-25 18:21:41 +08:00
impl<N: RealField> Identity<Multiplicative> for Quaternion<N> {
#[inline]
fn identity() -> Self {
Self::identity()
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> Identity<Additive> for Quaternion<N> {
#[inline]
fn identity() -> Self {
Self::zero()
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> AbstractMagma<Multiplicative> for Quaternion<N> {
#[inline]
fn operate(&self, rhs: &Self) -> Self {
self * rhs
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> AbstractMagma<Additive> for Quaternion<N> {
#[inline]
fn operate(&self, rhs: &Self) -> Self {
self + rhs
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> TwoSidedInverse<Additive> for Quaternion<N> {
#[inline]
2019-02-03 22:45:25 +08:00
fn two_sided_inverse(&self) -> Self {
-self
}
}
macro_rules! impl_structures(
($Quaternion: ident; $($marker: ident<$operator: ident>),* $(,)*) => {$(
2019-03-25 18:21:41 +08:00
impl<N: RealField> $marker<$operator> for $Quaternion<N> { }
)*}
);
impl_structures!(
Quaternion;
AbstractSemigroup<Multiplicative>,
AbstractMonoid<Multiplicative>,
AbstractSemigroup<Additive>,
AbstractQuasigroup<Additive>,
AbstractMonoid<Additive>,
AbstractLoop<Additive>,
AbstractGroup<Additive>,
AbstractGroupAbelian<Additive>
);
/*
*
* Vector space.
*
*/
2019-03-25 18:21:41 +08:00
impl<N: RealField> AbstractModule for Quaternion<N> {
type AbstractRing = N;
#[inline]
fn multiply_by(&self, n: N) -> Self {
self * n
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> Module for Quaternion<N> {
type Ring = N;
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> VectorSpace for Quaternion<N> {
type Field = N;
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> FiniteDimVectorSpace for Quaternion<N> {
#[inline]
fn dimension() -> usize {
4
}
#[inline]
fn canonical_basis_element(i: usize) -> Self {
Self::from(Vector4::canonical_basis_element(i))
}
#[inline]
fn dot(&self, other: &Self) -> N {
self.coords.dot(&other.coords)
}
#[inline]
unsafe fn component_unchecked(&self, i: usize) -> &N {
self.coords.component_unchecked(i)
}
#[inline]
unsafe fn component_unchecked_mut(&mut self, i: usize) -> &mut N {
self.coords.component_unchecked_mut(i)
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> NormedSpace for Quaternion<N> {
type RealField = N;
2019-03-25 18:19:36 +08:00
type ComplexField = N;
#[inline]
fn norm_squared(&self) -> N {
self.coords.norm_squared()
}
#[inline]
fn norm(&self) -> N {
self.as_vector().norm()
}
#[inline]
fn normalize(&self) -> Self {
let v = self.coords.normalize();
Self::from(v)
}
#[inline]
fn normalize_mut(&mut self) -> N {
self.coords.normalize_mut()
}
#[inline]
fn try_normalize(&self, min_norm: N) -> Option<Self> {
if let Some(v) = self.coords.try_normalize(min_norm) {
Some(Self::from(v))
2018-02-02 19:26:35 +08:00
} else {
None
}
}
#[inline]
fn try_normalize_mut(&mut self, min_norm: N) -> Option<N> {
self.coords.try_normalize_mut(min_norm)
}
}
/*
*
* Implementations for UnitQuaternion.
*
*/
2019-03-25 18:21:41 +08:00
impl<N: RealField> Identity<Multiplicative> for UnitQuaternion<N> {
#[inline]
fn identity() -> Self {
Self::identity()
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> AbstractMagma<Multiplicative> for UnitQuaternion<N> {
#[inline]
fn operate(&self, rhs: &Self) -> Self {
self * rhs
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> TwoSidedInverse<Multiplicative> for UnitQuaternion<N> {
#[inline]
2019-02-03 22:45:25 +08:00
fn two_sided_inverse(&self) -> Self {
self.inverse()
}
#[inline]
2019-02-03 22:45:25 +08:00
fn two_sided_inverse_mut(&mut self) {
self.inverse_mut()
}
}
impl_structures!(
UnitQuaternion;
AbstractSemigroup<Multiplicative>,
AbstractQuasigroup<Multiplicative>,
AbstractMonoid<Multiplicative>,
AbstractLoop<Multiplicative>,
AbstractGroup<Multiplicative>
);
2019-03-25 18:21:41 +08:00
impl<N: RealField> Transformation<Point3<N>> for UnitQuaternion<N> {
#[inline]
fn transform_point(&self, pt: &Point3<N>) -> Point3<N> {
self.transform_point(pt)
}
#[inline]
fn transform_vector(&self, v: &Vector3<N>) -> Vector3<N> {
self.transform_vector(v)
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> ProjectiveTransformation<Point3<N>> for UnitQuaternion<N> {
#[inline]
fn inverse_transform_point(&self, pt: &Point3<N>) -> Point3<N> {
self.inverse_transform_point(pt)
}
#[inline]
fn inverse_transform_vector(&self, v: &Vector3<N>) -> Vector3<N> {
self.inverse_transform_vector(v)
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> AffineTransformation<Point3<N>> for UnitQuaternion<N> {
2018-02-02 19:26:35 +08:00
type Rotation = Self;
type NonUniformScaling = Id;
2018-02-02 19:26:35 +08:00
type Translation = Id;
#[inline]
fn decompose(&self) -> (Id, Self, Id, Self) {
(Id::new(), self.clone(), Id::new(), Self::identity())
}
#[inline]
fn append_translation(&self, _: &Self::Translation) -> Self {
self.clone()
}
#[inline]
fn prepend_translation(&self, _: &Self::Translation) -> Self {
self.clone()
}
#[inline]
fn append_rotation(&self, r: &Self::Rotation) -> Self {
r * self
}
#[inline]
fn prepend_rotation(&self, r: &Self::Rotation) -> Self {
self * r
}
#[inline]
fn append_scaling(&self, _: &Self::NonUniformScaling) -> Self {
self.clone()
}
#[inline]
fn prepend_scaling(&self, _: &Self::NonUniformScaling) -> Self {
self.clone()
}
}
2019-03-25 18:21:41 +08:00
impl<N: RealField> Similarity<Point3<N>> for UnitQuaternion<N> {
2018-02-02 19:26:35 +08:00
type Scaling = Id;
#[inline]
fn translation(&self) -> Id {
Id::new()
}
#[inline]
fn rotation(&self) -> Self {
self.clone()
}
#[inline]
fn scaling(&self) -> Id {
Id::new()
}
}
macro_rules! marker_impl(
($($Trait: ident),*) => {$(
2019-03-25 18:21:41 +08:00
impl<N: RealField> $Trait<Point3<N>> for UnitQuaternion<N> { }
)*}
);
marker_impl!(Isometry, DirectIsometry, OrthogonalTransformation);
2019-03-25 18:21:41 +08:00
impl<N: RealField> Rotation<Point3<N>> for UnitQuaternion<N> {
#[inline]
fn powf(&self, n: N) -> Option<Self> {
Some(self.powf(n))
}
#[inline]
fn rotation_between(a: &Vector3<N>, b: &Vector3<N>) -> Option<Self> {
Self::rotation_between(a, b)
}
#[inline]
fn scaled_rotation_between(a: &Vector3<N>, b: &Vector3<N>, s: N) -> Option<Self> {
Self::scaled_rotation_between(a, b, s)
}
}