| 1 |
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 |
// option. This file may not be copied, modified, or distributed |
| 8 |
// except according to those terms. |
| 9 |
|
| 10 |
use crate::approxeq::ApproxEq; |
| 11 |
use crate::trig::Trig; |
| 12 |
use crate::{point2, point3, vec3, Angle, Point2D, Point3D, Vector2D, Vector3D}; |
| 13 |
use crate::{Transform2D, Transform3D, UnknownUnit}; |
| 14 |
|
| 15 |
use core::cmp::{Eq, PartialEq}; |
| 16 |
use core::fmt; |
| 17 |
use core::hash::Hash; |
| 18 |
use core::marker::PhantomData; |
| 19 |
use core::ops::{Add, Mul, Neg, Sub}; |
| 20 |
|
| 21 |
#[cfg(feature = "bytemuck")] |
| 22 |
use bytemuck::{Pod, Zeroable}; |
| 23 |
use num_traits::real::Real; |
| 24 |
use num_traits::{NumCast, One, Zero}; |
| 25 |
#[cfg(feature = "serde")] |
| 26 |
use serde::{Deserialize, Serialize}; |
| 27 |
|
| 28 |
/// A transform that can represent rotations in 2d, represented as an angle in radians. |
| 29 |
#[repr(C)] |
| 30 |
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 31 |
#[cfg_attr( |
| 32 |
feature = "serde", |
| 33 |
serde(bound( |
| 34 |
serialize = "T: serde::Serialize", |
| 35 |
deserialize = "T: serde::Deserialize<'de>" |
| 36 |
)) |
| 37 |
)] |
| 38 |
pub struct Rotation2D<T, Src, Dst> { |
| 39 |
/// Angle in radians |
| 40 |
pub angle: T, |
| 41 |
#[doc(hidden)] |
| 42 |
pub _unit: PhantomData<(Src, Dst)>, |
| 43 |
} |
| 44 |
|
| 45 |
impl<T: Copy, Src, Dst> Copy for Rotation2D<T, Src, Dst> {} |
| 46 |
|
| 47 |
impl<T: Clone, Src, Dst> Clone for Rotation2D<T, Src, Dst> { |
| 48 |
fn clone(&self) -> Self { |
| 49 |
Rotation2D { |
| 50 |
angle: self.angle.clone(), |
| 51 |
_unit: PhantomData, |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl<T, Src, Dst> Eq for Rotation2D<T, Src, Dst> where T: Eq {} |
| 57 |
|
| 58 |
impl<T, Src, Dst> PartialEq for Rotation2D<T, Src, Dst> |
| 59 |
where |
| 60 |
T: PartialEq, |
| 61 |
{ |
| 62 |
fn eq(&self, other: &Self) -> bool { |
| 63 |
self.angle == other.angle |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl<T, Src, Dst> Hash for Rotation2D<T, Src, Dst> |
| 68 |
where |
| 69 |
T: Hash, |
| 70 |
{ |
| 71 |
fn hash<H: core::hash::Hasher>(&self, h: &mut H) { |
| 72 |
self.angle.hash(h); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[cfg(feature = "bytemuck")] |
| 77 |
unsafe impl<T: Zeroable, Src, Dst> Zeroable for Rotation2D<T, Src, Dst> {} |
| 78 |
|
| 79 |
#[cfg(feature = "bytemuck")] |
| 80 |
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Rotation2D<T, Src, Dst> {} |
| 81 |
|
| 82 |
impl<T, Src, Dst> Rotation2D<T, Src, Dst> { |
| 83 |
/// Creates a rotation from an angle in radians. |
| 84 |
#[inline] |
| 85 |
pub fn new(angle: Angle<T>) -> Self { |
| 86 |
Rotation2D { |
| 87 |
angle: angle.radians, |
| 88 |
_unit: PhantomData, |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/// Creates a rotation from an angle in radians. |
| 93 |
pub fn radians(angle: T) -> Self { |
| 94 |
Self::new(Angle::radians(angle)) |
| 95 |
} |
| 96 |
|
| 97 |
/// Creates the identity rotation. |
| 98 |
#[inline] |
| 99 |
pub fn identity() -> Self |
| 100 |
where |
| 101 |
T: Zero, |
| 102 |
{ |
| 103 |
Self::radians(T::zero()) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl<T: Copy, Src, Dst> Rotation2D<T, Src, Dst> { |
| 108 |
/// Cast the unit, preserving the numeric value. |
| 109 |
/// |
| 110 |
/// # Example |
| 111 |
/// |
| 112 |
/// ```rust |
| 113 |
/// # use euclid::Rotation2D; |
| 114 |
/// enum Local {} |
| 115 |
/// enum World {} |
| 116 |
/// |
| 117 |
/// enum Local2 {} |
| 118 |
/// enum World2 {} |
| 119 |
/// |
| 120 |
/// let to_world: Rotation2D<_, Local, World> = Rotation2D::radians(42); |
| 121 |
/// |
| 122 |
/// assert_eq!(to_world.angle, to_world.cast_unit::<Local2, World2>().angle); |
| 123 |
/// ``` |
| 124 |
#[inline] |
| 125 |
pub fn cast_unit<Src2, Dst2>(&self) -> Rotation2D<T, Src2, Dst2> { |
| 126 |
Rotation2D { |
| 127 |
angle: self.angle, |
| 128 |
_unit: PhantomData, |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/// Drop the units, preserving only the numeric value. |
| 133 |
/// |
| 134 |
/// # Example |
| 135 |
/// |
| 136 |
/// ```rust |
| 137 |
/// # use euclid::Rotation2D; |
| 138 |
/// enum Local {} |
| 139 |
/// enum World {} |
| 140 |
/// |
| 141 |
/// let to_world: Rotation2D<_, Local, World> = Rotation2D::radians(42); |
| 142 |
/// |
| 143 |
/// assert_eq!(to_world.angle, to_world.to_untyped().angle); |
| 144 |
/// ``` |
| 145 |
#[inline] |
| 146 |
pub fn to_untyped(&self) -> Rotation2D<T, UnknownUnit, UnknownUnit> { |
| 147 |
self.cast_unit() |
| 148 |
} |
| 149 |
|
| 150 |
/// Tag a unitless value with units. |
| 151 |
/// |
| 152 |
/// # Example |
| 153 |
/// |
| 154 |
/// ```rust |
| 155 |
/// # use euclid::Rotation2D; |
| 156 |
/// use euclid::UnknownUnit; |
| 157 |
/// enum Local {} |
| 158 |
/// enum World {} |
| 159 |
/// |
| 160 |
/// let rot: Rotation2D<_, UnknownUnit, UnknownUnit> = Rotation2D::radians(42); |
| 161 |
/// |
| 162 |
/// assert_eq!(rot.angle, Rotation2D::<_, Local, World>::from_untyped(&rot).angle); |
| 163 |
/// ``` |
| 164 |
#[inline] |
| 165 |
pub fn from_untyped(r: &Rotation2D<T, UnknownUnit, UnknownUnit>) -> Self { |
| 166 |
r.cast_unit() |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl<T, Src, Dst> Rotation2D<T, Src, Dst> |
| 171 |
where |
| 172 |
T: Copy, |
| 173 |
{ |
| 174 |
/// Returns self.angle as a strongly typed `Angle<T>`. |
| 175 |
pub fn get_angle(&self) -> Angle<T> { |
| 176 |
Angle::radians(self.angle) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
impl<T: Real, Src, Dst> Rotation2D<T, Src, Dst> { |
| 181 |
/// Creates a 3d rotation (around the z axis) from this 2d rotation. |
| 182 |
#[inline] |
| 183 |
pub fn to_3d(&self) -> Rotation3D<T, Src, Dst> { |
| 184 |
Rotation3D::around_z(self.get_angle()) |
| 185 |
} |
| 186 |
|
| 187 |
/// Returns the inverse of this rotation. |
| 188 |
#[inline] |
| 189 |
pub fn inverse(&self) -> Rotation2D<T, Dst, Src> { |
| 190 |
Rotation2D::radians(-self.angle) |
| 191 |
} |
| 192 |
|
| 193 |
/// Returns a rotation representing the other rotation followed by this rotation. |
| 194 |
#[inline] |
| 195 |
pub fn then<NewSrc>(&self, other: &Rotation2D<T, NewSrc, Src>) -> Rotation2D<T, NewSrc, Dst> { |
| 196 |
Rotation2D::radians(self.angle + other.angle) |
| 197 |
} |
| 198 |
|
| 199 |
/// Returns the given 2d point transformed by this rotation. |
| 200 |
/// |
| 201 |
/// The input point must be use the unit Src, and the returned point has the unit Dst. |
| 202 |
#[inline] |
| 203 |
pub fn transform_point(&self, point: Point2D<T, Src>) -> Point2D<T, Dst> { |
| 204 |
let (sin, cos) = Real::sin_cos(self.angle); |
| 205 |
point2(point.x * cos - point.y * sin, point.y * cos + point.x * sin) |
| 206 |
} |
| 207 |
|
| 208 |
/// Returns the given 2d vector transformed by this rotation. |
| 209 |
/// |
| 210 |
/// The input point must be use the unit Src, and the returned point has the unit Dst. |
| 211 |
#[inline] |
| 212 |
pub fn transform_vector(&self, vector: Vector2D<T, Src>) -> Vector2D<T, Dst> { |
| 213 |
self.transform_point(vector.to_point()).to_vector() |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl<T, Src, Dst> Rotation2D<T, Src, Dst> |
| 218 |
where |
| 219 |
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Zero + Trig, |
| 220 |
{ |
| 221 |
/// Returns the matrix representation of this rotation. |
| 222 |
#[inline] |
| 223 |
pub fn to_transform(&self) -> Transform2D<T, Src, Dst> { |
| 224 |
Transform2D::rotation(self.get_angle()) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/// A transform that can represent rotations in 3d, represented as a quaternion. |
| 229 |
/// |
| 230 |
/// Most methods expect the quaternion to be normalized. |
| 231 |
/// When in doubt, use `unit_quaternion` instead of `quaternion` to create |
| 232 |
/// a rotation as the former will ensure that its result is normalized. |
| 233 |
/// |
| 234 |
/// Some people use the `x, y, z, w` (or `w, x, y, z`) notations. The equivalence is |
| 235 |
/// as follows: `x -> i`, `y -> j`, `z -> k`, `w -> r`. |
| 236 |
/// The memory layout of this type corresponds to the `x, y, z, w` notation |
| 237 |
#[repr(C)] |
| 238 |
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 239 |
#[cfg_attr( |
| 240 |
feature = "serde", |
| 241 |
serde(bound( |
| 242 |
serialize = "T: serde::Serialize", |
| 243 |
deserialize = "T: serde::Deserialize<'de>" |
| 244 |
)) |
| 245 |
)] |
| 246 |
pub struct Rotation3D<T, Src, Dst> { |
| 247 |
/// Component multiplied by the imaginary number `i`. |
| 248 |
pub i: T, |
| 249 |
/// Component multiplied by the imaginary number `j`. |
| 250 |
pub j: T, |
| 251 |
/// Component multiplied by the imaginary number `k`. |
| 252 |
pub k: T, |
| 253 |
/// The real part. |
| 254 |
pub r: T, |
| 255 |
#[doc(hidden)] |
| 256 |
pub _unit: PhantomData<(Src, Dst)>, |
| 257 |
} |
| 258 |
|
| 259 |
impl<T: Copy, Src, Dst> Copy for Rotation3D<T, Src, Dst> {} |
| 260 |
|
| 261 |
impl<T: Clone, Src, Dst> Clone for Rotation3D<T, Src, Dst> { |
| 262 |
fn clone(&self) -> Self { |
| 263 |
Rotation3D { |
| 264 |
i: self.i.clone(), |
| 265 |
j: self.j.clone(), |
| 266 |
k: self.k.clone(), |
| 267 |
r: self.r.clone(), |
| 268 |
_unit: PhantomData, |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
impl<T, Src, Dst> Eq for Rotation3D<T, Src, Dst> where T: Eq {} |
| 274 |
|
| 275 |
impl<T, Src, Dst> PartialEq for Rotation3D<T, Src, Dst> |
| 276 |
where |
| 277 |
T: PartialEq, |
| 278 |
{ |
| 279 |
fn eq(&self, other: &Self) -> bool { |
| 280 |
self.i == other.i && self.j == other.j && self.k == other.k && self.r == other.r |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
impl<T, Src, Dst> Hash for Rotation3D<T, Src, Dst> |
| 285 |
where |
| 286 |
T: Hash, |
| 287 |
{ |
| 288 |
fn hash<H: core::hash::Hasher>(&self, h: &mut H) { |
| 289 |
self.i.hash(h); |
| 290 |
self.j.hash(h); |
| 291 |
self.k.hash(h); |
| 292 |
self.r.hash(h); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
#[cfg(feature = "bytemuck")] |
| 297 |
unsafe impl<T: Zeroable, Src, Dst> Zeroable for Rotation3D<T, Src, Dst> {} |
| 298 |
|
| 299 |
#[cfg(feature = "bytemuck")] |
| 300 |
unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Rotation3D<T, Src, Dst> {} |
| 301 |
|
| 302 |
impl<T, Src, Dst> Rotation3D<T, Src, Dst> { |
| 303 |
/// Creates a rotation around from a quaternion representation. |
| 304 |
/// |
| 305 |
/// The parameters are a, b, c and r compose the quaternion `a*i + b*j + c*k + r` |
| 306 |
/// where `a`, `b` and `c` describe the vector part and the last parameter `r` is |
| 307 |
/// the real part. |
| 308 |
/// |
| 309 |
/// The resulting quaternion is not necessarily normalized. See [`unit_quaternion`]. |
| 310 |
/// |
| 311 |
/// [`unit_quaternion`]: #method.unit_quaternion |
| 312 |
#[inline] |
| 313 |
pub fn quaternion(a: T, b: T, c: T, r: T) -> Self { |
| 314 |
Rotation3D { |
| 315 |
i: a, |
| 316 |
j: b, |
| 317 |
k: c, |
| 318 |
r, |
| 319 |
_unit: PhantomData, |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/// Creates the identity rotation. |
| 324 |
#[inline] |
| 325 |
pub fn identity() -> Self |
| 326 |
where |
| 327 |
T: Zero + One, |
| 328 |
{ |
| 329 |
Self::quaternion(T::zero(), T::zero(), T::zero(), T::one()) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
impl<T, Src, Dst> Rotation3D<T, Src, Dst> |
| 334 |
where |
| 335 |
T: Copy, |
| 336 |
{ |
| 337 |
/// Returns the vector part (i, j, k) of this quaternion. |
| 338 |
#[inline] |
| 339 |
pub fn vector_part(&self) -> Vector3D<T, UnknownUnit> { |
| 340 |
vec3(self.i, self.j, self.k) |
| 341 |
} |
| 342 |
|
| 343 |
/// Cast the unit, preserving the numeric value. |
| 344 |
/// |
| 345 |
/// # Example |
| 346 |
/// |
| 347 |
/// ```rust |
| 348 |
/// # use euclid::Rotation3D; |
| 349 |
/// enum Local {} |
| 350 |
/// enum World {} |
| 351 |
/// |
| 352 |
/// enum Local2 {} |
| 353 |
/// enum World2 {} |
| 354 |
/// |
| 355 |
/// let to_world: Rotation3D<_, Local, World> = Rotation3D::quaternion(1, 2, 3, 4); |
| 356 |
/// |
| 357 |
/// assert_eq!(to_world.i, to_world.cast_unit::<Local2, World2>().i); |
| 358 |
/// assert_eq!(to_world.j, to_world.cast_unit::<Local2, World2>().j); |
| 359 |
/// assert_eq!(to_world.k, to_world.cast_unit::<Local2, World2>().k); |
| 360 |
/// assert_eq!(to_world.r, to_world.cast_unit::<Local2, World2>().r); |
| 361 |
/// ``` |
| 362 |
#[inline] |
| 363 |
pub fn cast_unit<Src2, Dst2>(&self) -> Rotation3D<T, Src2, Dst2> { |
| 364 |
Rotation3D { |
| 365 |
i: self.i, |
| 366 |
j: self.j, |
| 367 |
k: self.k, |
| 368 |
r: self.r, |
| 369 |
_unit: PhantomData, |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/// Drop the units, preserving only the numeric value. |
| 374 |
/// |
| 375 |
/// # Example |
| 376 |
/// |
| 377 |
/// ```rust |
| 378 |
/// # use euclid::Rotation3D; |
| 379 |
/// enum Local {} |
| 380 |
/// enum World {} |
| 381 |
/// |
| 382 |
/// let to_world: Rotation3D<_, Local, World> = Rotation3D::quaternion(1, 2, 3, 4); |
| 383 |
/// |
| 384 |
/// assert_eq!(to_world.i, to_world.to_untyped().i); |
| 385 |
/// assert_eq!(to_world.j, to_world.to_untyped().j); |
| 386 |
/// assert_eq!(to_world.k, to_world.to_untyped().k); |
| 387 |
/// assert_eq!(to_world.r, to_world.to_untyped().r); |
| 388 |
/// ``` |
| 389 |
#[inline] |
| 390 |
pub fn to_untyped(&self) -> Rotation3D<T, UnknownUnit, UnknownUnit> { |
| 391 |
self.cast_unit() |
| 392 |
} |
| 393 |
|
| 394 |
/// Tag a unitless value with units. |
| 395 |
/// |
| 396 |
/// # Example |
| 397 |
/// |
| 398 |
/// ```rust |
| 399 |
/// # use euclid::Rotation3D; |
| 400 |
/// use euclid::UnknownUnit; |
| 401 |
/// enum Local {} |
| 402 |
/// enum World {} |
| 403 |
/// |
| 404 |
/// let rot: Rotation3D<_, UnknownUnit, UnknownUnit> = Rotation3D::quaternion(1, 2, 3, 4); |
| 405 |
/// |
| 406 |
/// assert_eq!(rot.i, Rotation3D::<_, Local, World>::from_untyped(&rot).i); |
| 407 |
/// assert_eq!(rot.j, Rotation3D::<_, Local, World>::from_untyped(&rot).j); |
| 408 |
/// assert_eq!(rot.k, Rotation3D::<_, Local, World>::from_untyped(&rot).k); |
| 409 |
/// assert_eq!(rot.r, Rotation3D::<_, Local, World>::from_untyped(&rot).r); |
| 410 |
/// ``` |
| 411 |
#[inline] |
| 412 |
pub fn from_untyped(r: &Rotation3D<T, UnknownUnit, UnknownUnit>) -> Self { |
| 413 |
r.cast_unit() |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
impl<T, Src, Dst> Rotation3D<T, Src, Dst> |
| 418 |
where |
| 419 |
T: Real, |
| 420 |
{ |
| 421 |
/// Creates a rotation around from a quaternion representation and normalizes it. |
| 422 |
/// |
| 423 |
/// The parameters are a, b, c and r compose the quaternion `a*i + b*j + c*k + r` |
| 424 |
/// before normalization, where `a`, `b` and `c` describe the vector part and the |
| 425 |
/// last parameter `r` is the real part. |
| 426 |
#[inline] |
| 427 |
pub fn unit_quaternion(i: T, j: T, k: T, r: T) -> Self { |
| 428 |
Self::quaternion(i, j, k, r).normalize() |
| 429 |
} |
| 430 |
|
| 431 |
/// Creates a rotation around a given axis. |
| 432 |
pub fn around_axis(axis: Vector3D<T, Src>, angle: Angle<T>) -> Self { |
| 433 |
let axis = axis.normalize(); |
| 434 |
let two = T::one() + T::one(); |
| 435 |
let (sin, cos) = Angle::sin_cos(angle / two); |
| 436 |
Self::quaternion(axis.x * sin, axis.y * sin, axis.z * sin, cos) |
| 437 |
} |
| 438 |
|
| 439 |
/// Creates a rotation around the x axis. |
| 440 |
pub fn around_x(angle: Angle<T>) -> Self { |
| 441 |
let zero = Zero::zero(); |
| 442 |
let two = T::one() + T::one(); |
| 443 |
let (sin, cos) = Angle::sin_cos(angle / two); |
| 444 |
Self::quaternion(sin, zero, zero, cos) |
| 445 |
} |
| 446 |
|
| 447 |
/// Creates a rotation around the y axis. |
| 448 |
pub fn around_y(angle: Angle<T>) -> Self { |
| 449 |
let zero = Zero::zero(); |
| 450 |
let two = T::one() + T::one(); |
| 451 |
let (sin, cos) = Angle::sin_cos(angle / two); |
| 452 |
Self::quaternion(zero, sin, zero, cos) |
| 453 |
} |
| 454 |
|
| 455 |
/// Creates a rotation around the z axis. |
| 456 |
pub fn around_z(angle: Angle<T>) -> Self { |
| 457 |
let zero = Zero::zero(); |
| 458 |
let two = T::one() + T::one(); |
| 459 |
let (sin, cos) = Angle::sin_cos(angle / two); |
| 460 |
Self::quaternion(zero, zero, sin, cos) |
| 461 |
} |
| 462 |
|
| 463 |
/// Creates a rotation from Euler angles. |
| 464 |
/// |
| 465 |
/// The rotations are applied in roll then pitch then yaw order. |
| 466 |
/// |
| 467 |
/// - Roll (also called bank) is a rotation around the x axis. |
| 468 |
/// - Pitch (also called bearing) is a rotation around the y axis. |
| 469 |
/// - Yaw (also called heading) is a rotation around the z axis. |
| 470 |
pub fn euler(roll: Angle<T>, pitch: Angle<T>, yaw: Angle<T>) -> Self { |
| 471 |
let half = T::one() / (T::one() + T::one()); |
| 472 |
|
| 473 |
let (sy, cy) = Real::sin_cos(half * yaw.get()); |
| 474 |
let (sp, cp) = Real::sin_cos(half * pitch.get()); |
| 475 |
let (sr, cr) = Real::sin_cos(half * roll.get()); |
| 476 |
|
| 477 |
Self::quaternion( |
| 478 |
cy * sr * cp - sy * cr * sp, |
| 479 |
cy * cr * sp + sy * sr * cp, |
| 480 |
sy * cr * cp - cy * sr * sp, |
| 481 |
cy * cr * cp + sy * sr * sp, |
| 482 |
) |
| 483 |
} |
| 484 |
|
| 485 |
/// Returns the inverse of this rotation. |
| 486 |
#[inline] |
| 487 |
pub fn inverse(&self) -> Rotation3D<T, Dst, Src> { |
| 488 |
Rotation3D::quaternion(-self.i, -self.j, -self.k, self.r) |
| 489 |
} |
| 490 |
|
| 491 |
/// Computes the norm of this quaternion. |
| 492 |
#[inline] |
| 493 |
pub fn norm(&self) -> T { |
| 494 |
self.square_norm().sqrt() |
| 495 |
} |
| 496 |
|
| 497 |
/// Computes the squared norm of this quaternion. |
| 498 |
#[inline] |
| 499 |
pub fn square_norm(&self) -> T { |
| 500 |
self.i * self.i + self.j * self.j + self.k * self.k + self.r * self.r |
| 501 |
} |
| 502 |
|
| 503 |
/// Returns a [unit quaternion] from this one. |
| 504 |
/// |
| 505 |
/// [unit quaternion]: https://en.wikipedia.org/wiki/Quaternion#Unit_quaternion |
| 506 |
#[inline] |
| 507 |
pub fn normalize(&self) -> Self { |
| 508 |
self.mul(T::one() / self.norm()) |
| 509 |
} |
| 510 |
|
| 511 |
/// Returns `true` if [norm] of this quaternion is (approximately) one. |
| 512 |
/// |
| 513 |
/// [norm]: #method.norm |
| 514 |
#[inline] |
| 515 |
pub fn is_normalized(&self) -> bool |
| 516 |
where |
| 517 |
T: ApproxEq<T>, |
| 518 |
{ |
| 519 |
let eps = NumCast::from(1.0e-5).unwrap(); |
| 520 |
self.square_norm().approx_eq_eps(&T::one(), &eps) |
| 521 |
} |
| 522 |
|
| 523 |
/// Spherical linear interpolation between this rotation and another rotation. |
| 524 |
/// |
| 525 |
/// `t` is expected to be between zero and one. |
| 526 |
pub fn slerp(&self, other: &Self, t: T) -> Self |
| 527 |
where |
| 528 |
T: ApproxEq<T>, |
| 529 |
{ |
| 530 |
debug_assert!(self.is_normalized()); |
| 531 |
debug_assert!(other.is_normalized()); |
| 532 |
|
| 533 |
let r1 = *self; |
| 534 |
let mut r2 = *other; |
| 535 |
|
| 536 |
let mut dot = r1.i * r2.i + r1.j * r2.j + r1.k * r2.k + r1.r * r2.r; |
| 537 |
|
| 538 |
let one = T::one(); |
| 539 |
|
| 540 |
if dot.approx_eq(&T::one()) { |
| 541 |
// If the inputs are too close, linearly interpolate to avoid precision issues. |
| 542 |
return r1.lerp(&r2, t); |
| 543 |
} |
| 544 |
|
| 545 |
// If the dot product is negative, the quaternions |
| 546 |
// have opposite handed-ness and slerp won't take |
| 547 |
// the shorter path. Fix by reversing one quaternion. |
| 548 |
if dot < T::zero() { |
| 549 |
r2 = r2.mul(-T::one()); |
| 550 |
dot = -dot; |
| 551 |
} |
| 552 |
|
| 553 |
// For robustness, stay within the domain of acos. |
| 554 |
dot = Real::min(dot, one); |
| 555 |
|
| 556 |
// Angle between r1 and the result. |
| 557 |
let theta = Real::acos(dot) * t; |
| 558 |
|
| 559 |
// r1 and r3 form an orthonormal basis. |
| 560 |
let r3 = r2.sub(r1.mul(dot)).normalize(); |
| 561 |
let (sin, cos) = Real::sin_cos(theta); |
| 562 |
r1.mul(cos).add(r3.mul(sin)) |
| 563 |
} |
| 564 |
|
| 565 |
/// Basic Linear interpolation between this rotation and another rotation. |
| 566 |
#[inline] |
| 567 |
pub fn lerp(&self, other: &Self, t: T) -> Self { |
| 568 |
let one_t = T::one() - t; |
| 569 |
self.mul(one_t).add(other.mul(t)).normalize() |
| 570 |
} |
| 571 |
|
| 572 |
/// Returns the given 3d point transformed by this rotation. |
| 573 |
/// |
| 574 |
/// The input point must be use the unit Src, and the returned point has the unit Dst. |
| 575 |
pub fn transform_point3d(&self, point: Point3D<T, Src>) -> Point3D<T, Dst> |
| 576 |
where |
| 577 |
T: ApproxEq<T>, |
| 578 |
{ |
| 579 |
debug_assert!(self.is_normalized()); |
| 580 |
|
| 581 |
let two = T::one() + T::one(); |
| 582 |
let cross = self.vector_part().cross(point.to_vector().to_untyped()) * two; |
| 583 |
|
| 584 |
point3( |
| 585 |
point.x + self.r * cross.x + self.j * cross.z - self.k * cross.y, |
| 586 |
point.y + self.r * cross.y + self.k * cross.x - self.i * cross.z, |
| 587 |
point.z + self.r * cross.z + self.i * cross.y - self.j * cross.x, |
| 588 |
) |
| 589 |
} |
| 590 |
|
| 591 |
/// Returns the given 2d point transformed by this rotation then projected on the xy plane. |
| 592 |
/// |
| 593 |
/// The input point must be use the unit Src, and the returned point has the unit Dst. |
| 594 |
#[inline] |
| 595 |
pub fn transform_point2d(&self, point: Point2D<T, Src>) -> Point2D<T, Dst> |
| 596 |
where |
| 597 |
T: ApproxEq<T>, |
| 598 |
{ |
| 599 |
self.transform_point3d(point.to_3d()).xy() |
| 600 |
} |
| 601 |
|
| 602 |
/// Returns the given 3d vector transformed by this rotation. |
| 603 |
/// |
| 604 |
/// The input vector must be use the unit Src, and the returned point has the unit Dst. |
| 605 |
#[inline] |
| 606 |
pub fn transform_vector3d(&self, vector: Vector3D<T, Src>) -> Vector3D<T, Dst> |
| 607 |
where |
| 608 |
T: ApproxEq<T>, |
| 609 |
{ |
| 610 |
self.transform_point3d(vector.to_point()).to_vector() |
| 611 |
} |
| 612 |
|
| 613 |
/// Returns the given 2d vector transformed by this rotation then projected on the xy plane. |
| 614 |
/// |
| 615 |
/// The input vector must be use the unit Src, and the returned point has the unit Dst. |
| 616 |
#[inline] |
| 617 |
pub fn transform_vector2d(&self, vector: Vector2D<T, Src>) -> Vector2D<T, Dst> |
| 618 |
where |
| 619 |
T: ApproxEq<T>, |
| 620 |
{ |
| 621 |
self.transform_vector3d(vector.to_3d()).xy() |
| 622 |
} |
| 623 |
|
| 624 |
/// Returns the matrix representation of this rotation. |
| 625 |
#[inline] |
| 626 |
#[rustfmt::skip] |
| 627 |
pub fn to_transform(&self) -> Transform3D<T, Src, Dst> |
| 628 |
where |
| 629 |
T: ApproxEq<T>, |
| 630 |
{ |
| 631 |
debug_assert!(self.is_normalized()); |
| 632 |
|
| 633 |
let i2 = self.i + self.i; |
| 634 |
let j2 = self.j + self.j; |
| 635 |
let k2 = self.k + self.k; |
| 636 |
let ii = self.i * i2; |
| 637 |
let ij = self.i * j2; |
| 638 |
let ik = self.i * k2; |
| 639 |
let jj = self.j * j2; |
| 640 |
let jk = self.j * k2; |
| 641 |
let kk = self.k * k2; |
| 642 |
let ri = self.r * i2; |
| 643 |
let rj = self.r * j2; |
| 644 |
let rk = self.r * k2; |
| 645 |
|
| 646 |
let one = T::one(); |
| 647 |
let zero = T::zero(); |
| 648 |
|
| 649 |
let m11 = one - (jj + kk); |
| 650 |
let m12 = ij + rk; |
| 651 |
let m13 = ik - rj; |
| 652 |
|
| 653 |
let m21 = ij - rk; |
| 654 |
let m22 = one - (ii + kk); |
| 655 |
let m23 = jk + ri; |
| 656 |
|
| 657 |
let m31 = ik + rj; |
| 658 |
let m32 = jk - ri; |
| 659 |
let m33 = one - (ii + jj); |
| 660 |
|
| 661 |
Transform3D::new( |
| 662 |
m11, m12, m13, zero, |
| 663 |
m21, m22, m23, zero, |
| 664 |
m31, m32, m33, zero, |
| 665 |
zero, zero, zero, one, |
| 666 |
) |
| 667 |
} |
| 668 |
|
| 669 |
/// Returns a rotation representing this rotation followed by the other rotation. |
| 670 |
#[inline] |
| 671 |
pub fn then<NewDst>(&self, other: &Rotation3D<T, Dst, NewDst>) -> Rotation3D<T, Src, NewDst> |
| 672 |
where |
| 673 |
T: ApproxEq<T>, |
| 674 |
{ |
| 675 |
debug_assert!(self.is_normalized()); |
| 676 |
Rotation3D::quaternion( |
| 677 |
other.i * self.r + other.r * self.i + other.j * self.k - other.k * self.j, |
| 678 |
other.j * self.r + other.r * self.j + other.k * self.i - other.i * self.k, |
| 679 |
other.k * self.r + other.r * self.k + other.i * self.j - other.j * self.i, |
| 680 |
other.r * self.r - other.i * self.i - other.j * self.j - other.k * self.k, |
| 681 |
) |
| 682 |
} |
| 683 |
|
| 684 |
// add, sub and mul are used internally for intermediate computation but aren't public |
| 685 |
// because they don't carry real semantic meanings (I think?). |
| 686 |
|
| 687 |
#[inline] |
| 688 |
fn add(&self, other: Self) -> Self { |
| 689 |
Self::quaternion( |
| 690 |
self.i + other.i, |
| 691 |
self.j + other.j, |
| 692 |
self.k + other.k, |
| 693 |
self.r + other.r, |
| 694 |
) |
| 695 |
} |
| 696 |
|
| 697 |
#[inline] |
| 698 |
fn sub(&self, other: Self) -> Self { |
| 699 |
Self::quaternion( |
| 700 |
self.i - other.i, |
| 701 |
self.j - other.j, |
| 702 |
self.k - other.k, |
| 703 |
self.r - other.r, |
| 704 |
) |
| 705 |
} |
| 706 |
|
| 707 |
#[inline] |
| 708 |
fn mul(&self, factor: T) -> Self { |
| 709 |
Self::quaternion( |
| 710 |
self.i * factor, |
| 711 |
self.j * factor, |
| 712 |
self.k * factor, |
| 713 |
self.r * factor, |
| 714 |
) |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
impl<T: fmt::Debug, Src, Dst> fmt::Debug for Rotation3D<T, Src, Dst> { |
| 719 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 720 |
write!( |
| 721 |
f, |
| 722 |
"Quat({:?}*i + {:?}*j + {:?}*k + {:?})", |
| 723 |
self.i, self.j, self.k, self.r |
| 724 |
) |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
impl<T, Src, Dst> ApproxEq<T> for Rotation3D<T, Src, Dst> |
| 729 |
where |
| 730 |
T: Copy + Neg<Output = T> + ApproxEq<T>, |
| 731 |
{ |
| 732 |
fn approx_epsilon() -> T { |
| 733 |
T::approx_epsilon() |
| 734 |
} |
| 735 |
|
| 736 |
fn approx_eq_eps(&self, other: &Self, eps: &T) -> bool { |
| 737 |
(self.i.approx_eq_eps(&other.i, eps) |
| 738 |
&& self.j.approx_eq_eps(&other.j, eps) |
| 739 |
&& self.k.approx_eq_eps(&other.k, eps) |
| 740 |
&& self.r.approx_eq_eps(&other.r, eps)) |
| 741 |
|| (self.i.approx_eq_eps(&-other.i, eps) |
| 742 |
&& self.j.approx_eq_eps(&-other.j, eps) |
| 743 |
&& self.k.approx_eq_eps(&-other.k, eps) |
| 744 |
&& self.r.approx_eq_eps(&-other.r, eps)) |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
#[test] |
| 749 |
fn simple_rotation_2d() { |
| 750 |
use crate::default::Rotation2D; |
| 751 |
use core::f32::consts::{FRAC_PI_2, PI}; |
| 752 |
|
| 753 |
let ri = Rotation2D::identity(); |
| 754 |
let r90 = Rotation2D::radians(FRAC_PI_2); |
| 755 |
let rm90 = Rotation2D::radians(-FRAC_PI_2); |
| 756 |
let r180 = Rotation2D::radians(PI); |
| 757 |
|
| 758 |
assert!(ri |
| 759 |
.transform_point(point2(1.0, 2.0)) |
| 760 |
.approx_eq(&point2(1.0, 2.0))); |
| 761 |
assert!(r90 |
| 762 |
.transform_point(point2(1.0, 2.0)) |
| 763 |
.approx_eq(&point2(-2.0, 1.0))); |
| 764 |
assert!(rm90 |
| 765 |
.transform_point(point2(1.0, 2.0)) |
| 766 |
.approx_eq(&point2(2.0, -1.0))); |
| 767 |
assert!(r180 |
| 768 |
.transform_point(point2(1.0, 2.0)) |
| 769 |
.approx_eq(&point2(-1.0, -2.0))); |
| 770 |
|
| 771 |
assert!(r90 |
| 772 |
.inverse() |
| 773 |
.inverse() |
| 774 |
.transform_point(point2(1.0, 2.0)) |
| 775 |
.approx_eq(&r90.transform_point(point2(1.0, 2.0)))); |
| 776 |
} |
| 777 |
|
| 778 |
#[test] |
| 779 |
fn simple_rotation_3d_in_2d() { |
| 780 |
use crate::default::Rotation3D; |
| 781 |
use core::f32::consts::{FRAC_PI_2, PI}; |
| 782 |
|
| 783 |
let ri = Rotation3D::identity(); |
| 784 |
let r90 = Rotation3D::around_z(Angle::radians(FRAC_PI_2)); |
| 785 |
let rm90 = Rotation3D::around_z(Angle::radians(-FRAC_PI_2)); |
| 786 |
let r180 = Rotation3D::around_z(Angle::radians(PI)); |
| 787 |
|
| 788 |
assert!(ri |
| 789 |
.transform_point2d(point2(1.0, 2.0)) |
| 790 |
.approx_eq(&point2(1.0, 2.0))); |
| 791 |
assert!(r90 |
| 792 |
.transform_point2d(point2(1.0, 2.0)) |
| 793 |
.approx_eq(&point2(-2.0, 1.0))); |
| 794 |
assert!(rm90 |
| 795 |
.transform_point2d(point2(1.0, 2.0)) |
| 796 |
.approx_eq(&point2(2.0, -1.0))); |
| 797 |
assert!(r180 |
| 798 |
.transform_point2d(point2(1.0, 2.0)) |
| 799 |
.approx_eq(&point2(-1.0, -2.0))); |
| 800 |
|
| 801 |
assert!(r90 |
| 802 |
.inverse() |
| 803 |
.inverse() |
| 804 |
.transform_point2d(point2(1.0, 2.0)) |
| 805 |
.approx_eq(&r90.transform_point2d(point2(1.0, 2.0)))); |
| 806 |
} |
| 807 |
|
| 808 |
#[test] |
| 809 |
fn pre_post() { |
| 810 |
use crate::default::Rotation3D; |
| 811 |
use core::f32::consts::FRAC_PI_2; |
| 812 |
|
| 813 |
let r1 = Rotation3D::around_x(Angle::radians(FRAC_PI_2)); |
| 814 |
let r2 = Rotation3D::around_y(Angle::radians(FRAC_PI_2)); |
| 815 |
let r3 = Rotation3D::around_z(Angle::radians(FRAC_PI_2)); |
| 816 |
|
| 817 |
let t1 = r1.to_transform(); |
| 818 |
let t2 = r2.to_transform(); |
| 819 |
let t3 = r3.to_transform(); |
| 820 |
|
| 821 |
let p = point3(1.0, 2.0, 3.0); |
| 822 |
|
| 823 |
// Check that the order of transformations is correct (corresponds to what |
| 824 |
// we do in Transform3D). |
| 825 |
let p1 = r1.then(&r2).then(&r3).transform_point3d(p); |
| 826 |
let p2 = t1.then(&t2).then(&t3).transform_point3d(p); |
| 827 |
|
| 828 |
assert!(p1.approx_eq(&p2.unwrap())); |
| 829 |
|
| 830 |
// Check that changing the order indeed matters. |
| 831 |
let p3 = t3.then(&t1).then(&t2).transform_point3d(p); |
| 832 |
assert!(!p1.approx_eq(&p3.unwrap())); |
| 833 |
} |
| 834 |
|
| 835 |
#[test] |
| 836 |
fn to_transform3d() { |
| 837 |
use crate::default::Rotation3D; |
| 838 |
|
| 839 |
use core::f32::consts::{FRAC_PI_2, PI}; |
| 840 |
let rotations = [ |
| 841 |
Rotation3D::identity(), |
| 842 |
Rotation3D::around_x(Angle::radians(FRAC_PI_2)), |
| 843 |
Rotation3D::around_x(Angle::radians(-FRAC_PI_2)), |
| 844 |
Rotation3D::around_x(Angle::radians(PI)), |
| 845 |
Rotation3D::around_y(Angle::radians(FRAC_PI_2)), |
| 846 |
Rotation3D::around_y(Angle::radians(-FRAC_PI_2)), |
| 847 |
Rotation3D::around_y(Angle::radians(PI)), |
| 848 |
Rotation3D::around_z(Angle::radians(FRAC_PI_2)), |
| 849 |
Rotation3D::around_z(Angle::radians(-FRAC_PI_2)), |
| 850 |
Rotation3D::around_z(Angle::radians(PI)), |
| 851 |
]; |
| 852 |
|
| 853 |
let points = [ |
| 854 |
point3(0.0, 0.0, 0.0), |
| 855 |
point3(1.0, 2.0, 3.0), |
| 856 |
point3(-5.0, 3.0, -1.0), |
| 857 |
point3(-0.5, -1.0, 1.5), |
| 858 |
]; |
| 859 |
|
| 860 |
for rotation in &rotations { |
| 861 |
for &point in &points { |
| 862 |
let p1 = rotation.transform_point3d(point); |
| 863 |
let p2 = rotation.to_transform().transform_point3d(point); |
| 864 |
assert!(p1.approx_eq(&p2.unwrap())); |
| 865 |
} |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
#[test] |
| 870 |
fn slerp() { |
| 871 |
use crate::default::Rotation3D; |
| 872 |
|
| 873 |
let q1 = Rotation3D::quaternion(1.0, 0.0, 0.0, 0.0); |
| 874 |
let q2 = Rotation3D::quaternion(0.0, 1.0, 0.0, 0.0); |
| 875 |
let q3 = Rotation3D::quaternion(0.0, 0.0, -1.0, 0.0); |
| 876 |
|
| 877 |
// The values below can be obtained with a python program: |
| 878 |
// import numpy |
| 879 |
// import quaternion |
| 880 |
// q1 = numpy.quaternion(1, 0, 0, 0) |
| 881 |
// q2 = numpy.quaternion(0, 1, 0, 0) |
| 882 |
// quaternion.slerp_evaluate(q1, q2, 0.2) |
| 883 |
|
| 884 |
assert!(q1.slerp(&q2, 0.0).approx_eq(&q1)); |
| 885 |
assert!(q1.slerp(&q2, 0.2).approx_eq(&Rotation3D::quaternion( |
| 886 |
0.951056516295154, |
| 887 |
0.309016994374947, |
| 888 |
0.0, |
| 889 |
0.0 |
| 890 |
))); |
| 891 |
assert!(q1.slerp(&q2, 0.4).approx_eq(&Rotation3D::quaternion( |
| 892 |
0.809016994374947, |
| 893 |
0.587785252292473, |
| 894 |
0.0, |
| 895 |
0.0 |
| 896 |
))); |
| 897 |
assert!(q1.slerp(&q2, 0.6).approx_eq(&Rotation3D::quaternion( |
| 898 |
0.587785252292473, |
| 899 |
0.809016994374947, |
| 900 |
0.0, |
| 901 |
0.0 |
| 902 |
))); |
| 903 |
assert!(q1.slerp(&q2, 0.8).approx_eq(&Rotation3D::quaternion( |
| 904 |
0.309016994374947, |
| 905 |
0.951056516295154, |
| 906 |
0.0, |
| 907 |
0.0 |
| 908 |
))); |
| 909 |
assert!(q1.slerp(&q2, 1.0).approx_eq(&q2)); |
| 910 |
|
| 911 |
assert!(q1.slerp(&q3, 0.0).approx_eq(&q1)); |
| 912 |
assert!(q1.slerp(&q3, 0.2).approx_eq(&Rotation3D::quaternion( |
| 913 |
0.951056516295154, |
| 914 |
0.0, |
| 915 |
-0.309016994374947, |
| 916 |
0.0 |
| 917 |
))); |
| 918 |
assert!(q1.slerp(&q3, 0.4).approx_eq(&Rotation3D::quaternion( |
| 919 |
0.809016994374947, |
| 920 |
0.0, |
| 921 |
-0.587785252292473, |
| 922 |
0.0 |
| 923 |
))); |
| 924 |
assert!(q1.slerp(&q3, 0.6).approx_eq(&Rotation3D::quaternion( |
| 925 |
0.587785252292473, |
| 926 |
0.0, |
| 927 |
-0.809016994374947, |
| 928 |
0.0 |
| 929 |
))); |
| 930 |
assert!(q1.slerp(&q3, 0.8).approx_eq(&Rotation3D::quaternion( |
| 931 |
0.309016994374947, |
| 932 |
0.0, |
| 933 |
-0.951056516295154, |
| 934 |
0.0 |
| 935 |
))); |
| 936 |
assert!(q1.slerp(&q3, 1.0).approx_eq(&q3)); |
| 937 |
} |
| 938 |
|
| 939 |
#[test] |
| 940 |
fn around_axis() { |
| 941 |
use crate::default::Rotation3D; |
| 942 |
use core::f32::consts::{FRAC_PI_2, PI}; |
| 943 |
|
| 944 |
// Two sort of trivial cases: |
| 945 |
let r1 = Rotation3D::around_axis(vec3(1.0, 1.0, 0.0), Angle::radians(PI)); |
| 946 |
let r2 = Rotation3D::around_axis(vec3(1.0, 1.0, 0.0), Angle::radians(FRAC_PI_2)); |
| 947 |
assert!(r1 |
| 948 |
.transform_point3d(point3(1.0, 2.0, 0.0)) |
| 949 |
.approx_eq(&point3(2.0, 1.0, 0.0))); |
| 950 |
assert!(r2 |
| 951 |
.transform_point3d(point3(1.0, 0.0, 0.0)) |
| 952 |
.approx_eq(&point3(0.5, 0.5, -0.5.sqrt()))); |
| 953 |
|
| 954 |
// A more arbitrary test (made up with numpy): |
| 955 |
let r3 = Rotation3D::around_axis(vec3(0.5, 1.0, 2.0), Angle::radians(2.291288)); |
| 956 |
assert!(r3 |
| 957 |
.transform_point3d(point3(1.0, 0.0, 0.0)) |
| 958 |
.approx_eq(&point3(-0.58071821, 0.81401868, -0.01182979))); |
| 959 |
} |
| 960 |
|
| 961 |
#[test] |
| 962 |
fn from_euler() { |
| 963 |
use crate::default::Rotation3D; |
| 964 |
use core::f32::consts::FRAC_PI_2; |
| 965 |
|
| 966 |
// First test simple separate yaw pitch and roll rotations, because it is easy to come |
| 967 |
// up with the corresponding quaternion. |
| 968 |
// Since several quaternions can represent the same transformation we compare the result |
| 969 |
// of transforming a point rather than the values of each quaternions. |
| 970 |
let p = point3(1.0, 2.0, 3.0); |
| 971 |
|
| 972 |
let angle = Angle::radians(FRAC_PI_2); |
| 973 |
let zero = Angle::radians(0.0); |
| 974 |
|
| 975 |
// roll |
| 976 |
let roll_re = Rotation3D::euler(angle, zero, zero); |
| 977 |
let roll_rq = Rotation3D::around_x(angle); |
| 978 |
let roll_pe = roll_re.transform_point3d(p); |
| 979 |
let roll_pq = roll_rq.transform_point3d(p); |
| 980 |
|
| 981 |
// pitch |
| 982 |
let pitch_re = Rotation3D::euler(zero, angle, zero); |
| 983 |
let pitch_rq = Rotation3D::around_y(angle); |
| 984 |
let pitch_pe = pitch_re.transform_point3d(p); |
| 985 |
let pitch_pq = pitch_rq.transform_point3d(p); |
| 986 |
|
| 987 |
// yaw |
| 988 |
let yaw_re = Rotation3D::euler(zero, zero, angle); |
| 989 |
let yaw_rq = Rotation3D::around_z(angle); |
| 990 |
let yaw_pe = yaw_re.transform_point3d(p); |
| 991 |
let yaw_pq = yaw_rq.transform_point3d(p); |
| 992 |
|
| 993 |
assert!(roll_pe.approx_eq(&roll_pq)); |
| 994 |
assert!(pitch_pe.approx_eq(&pitch_pq)); |
| 995 |
assert!(yaw_pe.approx_eq(&yaw_pq)); |
| 996 |
|
| 997 |
// Now check that the yaw pitch and roll transformations when combined are applied in |
| 998 |
// the proper order: roll -> pitch -> yaw. |
| 999 |
let ypr_e = Rotation3D::euler(angle, angle, angle); |
| 1000 |
let ypr_q = roll_rq.then(&pitch_rq).then(&yaw_rq); |
| 1001 |
let ypr_pe = ypr_e.transform_point3d(p); |
| 1002 |
let ypr_pq = ypr_q.transform_point3d(p); |
| 1003 |
|
| 1004 |
assert!(ypr_pe.approx_eq(&ypr_pq)); |
| 1005 |
} |