| 1 |
use num::Zero; |
| 2 |
|
| 3 |
use alga::general::{ |
| 4 |
AbstractGroup, AbstractGroupAbelian, AbstractLoop, AbstractMagma, AbstractModule, |
| 5 |
AbstractMonoid, AbstractQuasigroup, AbstractSemigroup, Additive, Id, Identity, Module, |
| 6 |
Multiplicative, RealField, TwoSidedInverse, |
| 7 |
}; |
| 8 |
use alga::linear::{ |
| 9 |
AffineTransformation, DirectIsometry, FiniteDimVectorSpace, Isometry, NormedSpace, |
| 10 |
OrthogonalTransformation, ProjectiveTransformation, Rotation, Similarity, Transformation, |
| 11 |
VectorSpace, |
| 12 |
}; |
| 13 |
|
| 14 |
use crate::base::{Vector3, Vector4}; |
| 15 |
use crate::geometry::{Point3, Quaternion, UnitQuaternion}; |
| 16 |
|
| 17 |
impl<T: RealField + simba::scalar::RealField> Identity<Multiplicative> for Quaternion<T> { |
| 18 |
#[inline] |
| 19 |
fn identity() -> Self { |
| 20 |
Self::identity() |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
impl<T: RealField + simba::scalar::RealField> Identity<Additive> for Quaternion<T> { |
| 25 |
#[inline] |
| 26 |
fn identity() -> Self { |
| 27 |
Self::zero() |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl<T: RealField + simba::scalar::RealField> AbstractMagma<Multiplicative> for Quaternion<T> { |
| 32 |
#[inline] |
| 33 |
fn operate(&self, rhs: &Self) -> Self { |
| 34 |
self * rhs |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl<T: RealField + simba::scalar::RealField> AbstractMagma<Additive> for Quaternion<T> { |
| 39 |
#[inline] |
| 40 |
fn operate(&self, rhs: &Self) -> Self { |
| 41 |
self + rhs |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl<T: RealField + simba::scalar::RealField> TwoSidedInverse<Additive> for Quaternion<T> { |
| 46 |
#[inline] |
| 47 |
fn two_sided_inverse(&self) -> Self { |
| 48 |
-self |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
macro_rules! impl_structures( |
| 53 |
($Quaternion: ident; $($marker: ident<$operator: ident>),* $(,)*) => {$( |
| 54 |
impl<T: RealField + simba::scalar::RealField> $marker<$operator> for $Quaternion<T> { } |
| 55 |
)*} |
| 56 |
); |
| 57 |
|
| 58 |
impl_structures!( |
| 59 |
Quaternion; |
| 60 |
AbstractSemigroup<Multiplicative>, |
| 61 |
AbstractMonoid<Multiplicative>, |
| 62 |
|
| 63 |
AbstractSemigroup<Additive>, |
| 64 |
AbstractQuasigroup<Additive>, |
| 65 |
AbstractMonoid<Additive>, |
| 66 |
AbstractLoop<Additive>, |
| 67 |
AbstractGroup<Additive>, |
| 68 |
AbstractGroupAbelian<Additive> |
| 69 |
); |
| 70 |
|
| 71 |
/* |
| 72 |
* |
| 73 |
* Vector space. |
| 74 |
* |
| 75 |
*/ |
| 76 |
impl<T: RealField + simba::scalar::RealField> AbstractModule for Quaternion<T> { |
| 77 |
type AbstractRing = T; |
| 78 |
|
| 79 |
#[inline] |
| 80 |
fn multiply_by(&self, n: T) -> Self { |
| 81 |
self * n |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl<T: RealField + simba::scalar::RealField> Module for Quaternion<T> { |
| 86 |
type Ring = T; |
| 87 |
} |
| 88 |
|
| 89 |
impl<T: RealField + simba::scalar::RealField> VectorSpace for Quaternion<T> { |
| 90 |
type Field = T; |
| 91 |
} |
| 92 |
|
| 93 |
impl<T: RealField + simba::scalar::RealField> FiniteDimVectorSpace for Quaternion<T> { |
| 94 |
#[inline] |
| 95 |
fn dimension() -> usize { |
| 96 |
4 |
| 97 |
} |
| 98 |
|
| 99 |
#[inline] |
| 100 |
fn canonical_basis_element(i: usize) -> Self { |
| 101 |
Self::from(Vector4::canonical_basis_element(i)) |
| 102 |
} |
| 103 |
|
| 104 |
#[inline] |
| 105 |
fn dot(&self, other: &Self) -> T { |
| 106 |
self.coords.dot(&other.coords) |
| 107 |
} |
| 108 |
|
| 109 |
#[inline] |
| 110 |
unsafe fn component_unchecked(&self, i: usize) -> &T { |
| 111 |
unsafe { self.coords.component_unchecked(i) } |
| 112 |
} |
| 113 |
|
| 114 |
#[inline] |
| 115 |
unsafe fn component_unchecked_mut(&mut self, i: usize) -> &mut T { |
| 116 |
unsafe { self.coords.component_unchecked_mut(i) } |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
impl<T: RealField + simba::scalar::RealField> NormedSpace for Quaternion<T> { |
| 121 |
type RealField = T; |
| 122 |
type ComplexField = T; |
| 123 |
|
| 124 |
#[inline] |
| 125 |
fn norm_squared(&self) -> T { |
| 126 |
self.coords.norm_squared() |
| 127 |
} |
| 128 |
|
| 129 |
#[inline] |
| 130 |
fn norm(&self) -> T { |
| 131 |
self.as_vector().norm() |
| 132 |
} |
| 133 |
|
| 134 |
#[inline] |
| 135 |
fn normalize(&self) -> Self { |
| 136 |
let v = self.coords.normalize(); |
| 137 |
Self::from(v) |
| 138 |
} |
| 139 |
|
| 140 |
#[inline] |
| 141 |
fn normalize_mut(&mut self) -> T { |
| 142 |
self.coords.normalize_mut() |
| 143 |
} |
| 144 |
|
| 145 |
#[inline] |
| 146 |
fn try_normalize(&self, min_norm: T) -> Option<Self> { |
| 147 |
self.coords.try_normalize(min_norm).map(Self::from) |
| 148 |
} |
| 149 |
|
| 150 |
#[inline] |
| 151 |
fn try_normalize_mut(&mut self, min_norm: T) -> Option<T> { |
| 152 |
self.coords.try_normalize_mut(min_norm) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
* |
| 158 |
* Implementations for UnitQuaternion. |
| 159 |
* |
| 160 |
*/ |
| 161 |
impl<T: RealField + simba::scalar::RealField> Identity<Multiplicative> for UnitQuaternion<T> { |
| 162 |
#[inline] |
| 163 |
fn identity() -> Self { |
| 164 |
Self::identity() |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl<T: RealField + simba::scalar::RealField> AbstractMagma<Multiplicative> for UnitQuaternion<T> { |
| 169 |
#[inline] |
| 170 |
fn operate(&self, rhs: &Self) -> Self { |
| 171 |
self * rhs |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
impl<T: RealField + simba::scalar::RealField> TwoSidedInverse<Multiplicative> |
| 176 |
for UnitQuaternion<T> |
| 177 |
{ |
| 178 |
#[inline] |
| 179 |
fn two_sided_inverse(&self) -> Self { |
| 180 |
self.inverse() |
| 181 |
} |
| 182 |
|
| 183 |
#[inline] |
| 184 |
fn two_sided_inverse_mut(&mut self) { |
| 185 |
self.inverse_mut() |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl_structures!( |
| 190 |
UnitQuaternion; |
| 191 |
AbstractSemigroup<Multiplicative>, |
| 192 |
AbstractQuasigroup<Multiplicative>, |
| 193 |
AbstractMonoid<Multiplicative>, |
| 194 |
AbstractLoop<Multiplicative>, |
| 195 |
AbstractGroup<Multiplicative> |
| 196 |
); |
| 197 |
|
| 198 |
impl<T: RealField + simba::scalar::RealField> Transformation<Point3<T>> for UnitQuaternion<T> { |
| 199 |
#[inline] |
| 200 |
fn transform_point(&self, pt: &Point3<T>) -> Point3<T> { |
| 201 |
self.transform_point(pt) |
| 202 |
} |
| 203 |
|
| 204 |
#[inline] |
| 205 |
fn transform_vector(&self, v: &Vector3<T>) -> Vector3<T> { |
| 206 |
self.transform_vector(v) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
impl<T: RealField + simba::scalar::RealField> ProjectiveTransformation<Point3<T>> |
| 211 |
for UnitQuaternion<T> |
| 212 |
{ |
| 213 |
#[inline] |
| 214 |
fn inverse_transform_point(&self, pt: &Point3<T>) -> Point3<T> { |
| 215 |
self.inverse_transform_point(pt) |
| 216 |
} |
| 217 |
|
| 218 |
#[inline] |
| 219 |
fn inverse_transform_vector(&self, v: &Vector3<T>) -> Vector3<T> { |
| 220 |
self.inverse_transform_vector(v) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
impl<T: RealField + simba::scalar::RealField> AffineTransformation<Point3<T>> |
| 225 |
for UnitQuaternion<T> |
| 226 |
{ |
| 227 |
type Rotation = Self; |
| 228 |
type NonUniformScaling = Id; |
| 229 |
type Translation = Id; |
| 230 |
|
| 231 |
#[inline] |
| 232 |
fn decompose(&self) -> (Id, Self, Id, Self) { |
| 233 |
(Id::new(), *self, Id::new(), Self::identity()) |
| 234 |
} |
| 235 |
|
| 236 |
#[inline] |
| 237 |
fn append_translation(&self, _: &Self::Translation) -> Self { |
| 238 |
*self |
| 239 |
} |
| 240 |
|
| 241 |
#[inline] |
| 242 |
fn prepend_translation(&self, _: &Self::Translation) -> Self { |
| 243 |
*self |
| 244 |
} |
| 245 |
|
| 246 |
#[inline] |
| 247 |
fn append_rotation(&self, r: &Self::Rotation) -> Self { |
| 248 |
r * self |
| 249 |
} |
| 250 |
|
| 251 |
#[inline] |
| 252 |
fn prepend_rotation(&self, r: &Self::Rotation) -> Self { |
| 253 |
self * r |
| 254 |
} |
| 255 |
|
| 256 |
#[inline] |
| 257 |
fn append_scaling(&self, _: &Self::NonUniformScaling) -> Self { |
| 258 |
*self |
| 259 |
} |
| 260 |
|
| 261 |
#[inline] |
| 262 |
fn prepend_scaling(&self, _: &Self::NonUniformScaling) -> Self { |
| 263 |
*self |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
impl<T: RealField + simba::scalar::RealField> Similarity<Point3<T>> for UnitQuaternion<T> { |
| 268 |
type Scaling = Id; |
| 269 |
|
| 270 |
#[inline] |
| 271 |
fn translation(&self) -> Id { |
| 272 |
Id::new() |
| 273 |
} |
| 274 |
|
| 275 |
#[inline] |
| 276 |
fn rotation(&self) -> Self { |
| 277 |
*self |
| 278 |
} |
| 279 |
|
| 280 |
#[inline] |
| 281 |
fn scaling(&self) -> Id { |
| 282 |
Id::new() |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
macro_rules! marker_impl( |
| 287 |
($($Trait: ident),*) => {$( |
| 288 |
impl<T: RealField + simba::scalar::RealField> $Trait<Point3<T>> for UnitQuaternion<T> { } |
| 289 |
)*} |
| 290 |
); |
| 291 |
|
| 292 |
marker_impl!(Isometry, DirectIsometry, OrthogonalTransformation); |
| 293 |
|
| 294 |
impl<T: RealField + simba::scalar::RealField> Rotation<Point3<T>> for UnitQuaternion<T> { |
| 295 |
#[inline] |
| 296 |
fn powf(&self, n: T) -> Option<Self> { |
| 297 |
Some(self.powf(n)) |
| 298 |
} |
| 299 |
|
| 300 |
#[inline] |
| 301 |
fn rotation_between(a: &Vector3<T>, b: &Vector3<T>) -> Option<Self> { |
| 302 |
Self::rotation_between(a, b) |
| 303 |
} |
| 304 |
|
| 305 |
#[inline] |
| 306 |
fn scaled_rotation_between(a: &Vector3<T>, b: &Vector3<T>, s: T) -> Option<Self> { |
| 307 |
Self::scaled_rotation_between(a, b, s) |
| 308 |
} |
| 309 |
} |