| 1 |
// Generated from quat.rs.tera template. Edit the template, not the generated file. |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
coresimd::*, |
| 5 |
euler::{EulerRot, FromEuler, ToEuler}, |
| 6 |
f32::math, |
| 7 |
DQuat, Mat3, Mat3A, Mat4, Vec2, Vec3, Vec3A, Vec4, |
| 8 |
}; |
| 9 |
|
| 10 |
use core::simd::*; |
| 11 |
|
| 12 |
use core::fmt; |
| 13 |
use core::iter::{Product, Sum}; |
| 14 |
use core::ops::{ |
| 15 |
Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign, |
| 16 |
}; |
| 17 |
|
| 18 |
/// Creates a quaternion from `x`, `y`, `z` and `w` values. |
| 19 |
/// |
| 20 |
/// This should generally not be called manually unless you know what you are doing. Use |
| 21 |
/// one of the other constructors instead such as `identity` or `from_axis_angle`. |
| 22 |
#[inline] |
| 23 |
#[must_use] |
| 24 |
pub const fn quat(x: f32, y: f32, z: f32, w: f32) -> Quat { |
| 25 |
Quat::from_xyzw(x, y, z, w) |
| 26 |
} |
| 27 |
|
| 28 |
/// A quaternion representing an orientation. |
| 29 |
/// |
| 30 |
/// This quaternion is intended to be of unit length but may denormalize due to |
| 31 |
/// floating point "error creep" which can occur when successive quaternion |
| 32 |
/// operations are applied. |
| 33 |
/// |
| 34 |
/// SIMD vector types are used for storage on supported platforms. |
| 35 |
/// |
| 36 |
/// This type is 16 byte aligned. |
| 37 |
#[derive(Clone, Copy)] |
| 38 |
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))] |
| 39 |
#[repr(transparent)] |
| 40 |
pub struct Quat(pub(crate) f32x4); |
| 41 |
|
| 42 |
impl Quat { |
| 43 |
/// All zeros. |
| 44 |
const ZERO: Self = Self::from_array([0.0; 4]); |
| 45 |
|
| 46 |
/// The identity quaternion. Corresponds to no rotation. |
| 47 |
pub const IDENTITY: Self = Self::from_xyzw(0.0, 0.0, 0.0, 1.0); |
| 48 |
|
| 49 |
/// All NANs. |
| 50 |
pub const NAN: Self = Self::from_array([f32::NAN; 4]); |
| 51 |
|
| 52 |
/// Creates a new rotation quaternion. |
| 53 |
/// |
| 54 |
/// This should generally not be called manually unless you know what you are doing. |
| 55 |
/// Use one of the other constructors instead such as `identity` or `from_axis_angle`. |
| 56 |
/// |
| 57 |
/// `from_xyzw` is mostly used by unit tests and `serde` deserialization. |
| 58 |
/// |
| 59 |
/// # Preconditions |
| 60 |
/// |
| 61 |
/// This function does not check if the input is normalized, it is up to the user to |
| 62 |
/// provide normalized input or to normalized the resulting quaternion. |
| 63 |
#[inline(always)] |
| 64 |
#[must_use] |
| 65 |
pub const fn from_xyzw(x: f32, y: f32, z: f32, w: f32) -> Self { |
| 66 |
Self(f32x4::from_array([x, y, z, w])) |
| 67 |
} |
| 68 |
|
| 69 |
/// Creates a rotation quaternion from an array. |
| 70 |
/// |
| 71 |
/// # Preconditions |
| 72 |
/// |
| 73 |
/// This function does not check if the input is normalized, it is up to the user to |
| 74 |
/// provide normalized input or to normalized the resulting quaternion. |
| 75 |
#[inline] |
| 76 |
#[must_use] |
| 77 |
pub const fn from_array(a: [f32; 4]) -> Self { |
| 78 |
Self(f32x4::from_array(a)) |
| 79 |
} |
| 80 |
|
| 81 |
/// Creates a new rotation quaternion from a 4D vector. |
| 82 |
/// |
| 83 |
/// # Preconditions |
| 84 |
/// |
| 85 |
/// This function does not check if the input is normalized, it is up to the user to |
| 86 |
/// provide normalized input or to normalized the resulting quaternion. |
| 87 |
#[inline] |
| 88 |
#[must_use] |
| 89 |
pub const fn from_vec4(v: Vec4) -> Self { |
| 90 |
Self(v.0) |
| 91 |
} |
| 92 |
|
| 93 |
/// Creates a rotation quaternion from a slice. |
| 94 |
/// |
| 95 |
/// # Preconditions |
| 96 |
/// |
| 97 |
/// This function does not check if the input is normalized, it is up to the user to |
| 98 |
/// provide normalized input or to normalized the resulting quaternion. |
| 99 |
/// |
| 100 |
/// # Panics |
| 101 |
/// |
| 102 |
/// Panics if `slice` length is less than 4. |
| 103 |
#[inline] |
| 104 |
#[must_use] |
| 105 |
pub fn from_slice(slice: &[f32]) -> Self { |
| 106 |
Self::from_xyzw(slice[0], slice[1], slice[2], slice[3]) |
| 107 |
} |
| 108 |
|
| 109 |
/// Writes the quaternion to an unaligned slice. |
| 110 |
/// |
| 111 |
/// # Panics |
| 112 |
/// |
| 113 |
/// Panics if `slice` length is less than 4. |
| 114 |
#[inline] |
| 115 |
pub fn write_to_slice(self, slice: &mut [f32]) { |
| 116 |
slice[0] = self.x; |
| 117 |
slice[1] = self.y; |
| 118 |
slice[2] = self.z; |
| 119 |
slice[3] = self.w; |
| 120 |
} |
| 121 |
|
| 122 |
/// Create a quaternion for a normalized rotation `axis` and `angle` (in radians). |
| 123 |
/// |
| 124 |
/// The axis must be a unit vector. |
| 125 |
/// |
| 126 |
/// # Panics |
| 127 |
/// |
| 128 |
/// Will panic if `axis` is not normalized when `glam_assert` is enabled. |
| 129 |
#[inline] |
| 130 |
#[must_use] |
| 131 |
pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self { |
| 132 |
glam_assert!(axis.is_normalized()); |
| 133 |
let (s, c) = math::sin_cos(angle * 0.5); |
| 134 |
let v = axis * s; |
| 135 |
Self::from_xyzw(v.x, v.y, v.z, c) |
| 136 |
} |
| 137 |
|
| 138 |
/// Create a quaternion that rotates `v.length()` radians around `v.normalize()`. |
| 139 |
/// |
| 140 |
/// `from_scaled_axis(Vec3::ZERO)` results in the identity quaternion. |
| 141 |
#[inline] |
| 142 |
#[must_use] |
| 143 |
pub fn from_scaled_axis(v: Vec3) -> Self { |
| 144 |
let length = v.length(); |
| 145 |
if length == 0.0 { |
| 146 |
Self::IDENTITY |
| 147 |
} else { |
| 148 |
Self::from_axis_angle(v / length, length) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/// Creates a quaternion from the `angle` (in radians) around the x axis. |
| 153 |
#[inline] |
| 154 |
#[must_use] |
| 155 |
pub fn from_rotation_x(angle: f32) -> Self { |
| 156 |
let (s, c) = math::sin_cos(angle * 0.5); |
| 157 |
Self::from_xyzw(s, 0.0, 0.0, c) |
| 158 |
} |
| 159 |
|
| 160 |
/// Creates a quaternion from the `angle` (in radians) around the y axis. |
| 161 |
#[inline] |
| 162 |
#[must_use] |
| 163 |
pub fn from_rotation_y(angle: f32) -> Self { |
| 164 |
let (s, c) = math::sin_cos(angle * 0.5); |
| 165 |
Self::from_xyzw(0.0, s, 0.0, c) |
| 166 |
} |
| 167 |
|
| 168 |
/// Creates a quaternion from the `angle` (in radians) around the z axis. |
| 169 |
#[inline] |
| 170 |
#[must_use] |
| 171 |
pub fn from_rotation_z(angle: f32) -> Self { |
| 172 |
let (s, c) = math::sin_cos(angle * 0.5); |
| 173 |
Self::from_xyzw(0.0, 0.0, s, c) |
| 174 |
} |
| 175 |
|
| 176 |
/// Creates a quaternion from the given Euler rotation sequence and the angles (in radians). |
| 177 |
#[inline] |
| 178 |
#[must_use] |
| 179 |
pub fn from_euler(euler: EulerRot, a: f32, b: f32, c: f32) -> Self { |
| 180 |
Self::from_euler_angles(euler, a, b, c) |
| 181 |
} |
| 182 |
|
| 183 |
/// From the columns of a 3x3 rotation matrix. |
| 184 |
/// |
| 185 |
/// Note if the input axes contain scales, shears, or other non-rotation transformations then |
| 186 |
/// the output of this function is ill-defined. |
| 187 |
/// |
| 188 |
/// # Panics |
| 189 |
/// |
| 190 |
/// Will panic if any axis is not normalized when `glam_assert` is enabled. |
| 191 |
#[inline] |
| 192 |
#[must_use] |
| 193 |
pub(crate) fn from_rotation_axes(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self { |
| 194 |
glam_assert!(x_axis.is_normalized() && y_axis.is_normalized() && z_axis.is_normalized()); |
| 195 |
// Based on https://github.com/microsoft/DirectXMath `XMQuaternionRotationMatrix` |
| 196 |
let (m00, m01, m02) = x_axis.into(); |
| 197 |
let (m10, m11, m12) = y_axis.into(); |
| 198 |
let (m20, m21, m22) = z_axis.into(); |
| 199 |
if m22 <= 0.0 { |
| 200 |
// x^2 + y^2 >= z^2 + w^2 |
| 201 |
let dif10 = m11 - m00; |
| 202 |
let omm22 = 1.0 - m22; |
| 203 |
if dif10 <= 0.0 { |
| 204 |
// x^2 >= y^2 |
| 205 |
let four_xsq = omm22 - dif10; |
| 206 |
let inv4x = 0.5 / math::sqrt(four_xsq); |
| 207 |
Self::from_xyzw( |
| 208 |
four_xsq * inv4x, |
| 209 |
(m01 + m10) * inv4x, |
| 210 |
(m02 + m20) * inv4x, |
| 211 |
(m12 - m21) * inv4x, |
| 212 |
) |
| 213 |
} else { |
| 214 |
// y^2 >= x^2 |
| 215 |
let four_ysq = omm22 + dif10; |
| 216 |
let inv4y = 0.5 / math::sqrt(four_ysq); |
| 217 |
Self::from_xyzw( |
| 218 |
(m01 + m10) * inv4y, |
| 219 |
four_ysq * inv4y, |
| 220 |
(m12 + m21) * inv4y, |
| 221 |
(m20 - m02) * inv4y, |
| 222 |
) |
| 223 |
} |
| 224 |
} else { |
| 225 |
// z^2 + w^2 >= x^2 + y^2 |
| 226 |
let sum10 = m11 + m00; |
| 227 |
let opm22 = 1.0 + m22; |
| 228 |
if sum10 <= 0.0 { |
| 229 |
// z^2 >= w^2 |
| 230 |
let four_zsq = opm22 - sum10; |
| 231 |
let inv4z = 0.5 / math::sqrt(four_zsq); |
| 232 |
Self::from_xyzw( |
| 233 |
(m02 + m20) * inv4z, |
| 234 |
(m12 + m21) * inv4z, |
| 235 |
four_zsq * inv4z, |
| 236 |
(m01 - m10) * inv4z, |
| 237 |
) |
| 238 |
} else { |
| 239 |
// w^2 >= z^2 |
| 240 |
let four_wsq = opm22 + sum10; |
| 241 |
let inv4w = 0.5 / math::sqrt(four_wsq); |
| 242 |
Self::from_xyzw( |
| 243 |
(m12 - m21) * inv4w, |
| 244 |
(m20 - m02) * inv4w, |
| 245 |
(m01 - m10) * inv4w, |
| 246 |
four_wsq * inv4w, |
| 247 |
) |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/// Creates a quaternion from a 3x3 rotation matrix. |
| 253 |
/// |
| 254 |
/// Note if the input matrix contain scales, shears, or other non-rotation transformations then |
| 255 |
/// the resulting quaternion will be ill-defined. |
| 256 |
/// |
| 257 |
/// # Panics |
| 258 |
/// |
| 259 |
/// Will panic if any input matrix column is not normalized when `glam_assert` is enabled. |
| 260 |
#[inline] |
| 261 |
#[must_use] |
| 262 |
pub fn from_mat3(mat: &Mat3) -> Self { |
| 263 |
Self::from_rotation_axes(mat.x_axis, mat.y_axis, mat.z_axis) |
| 264 |
} |
| 265 |
|
| 266 |
/// Creates a quaternion from a 3x3 SIMD aligned rotation matrix. |
| 267 |
/// |
| 268 |
/// Note if the input matrix contain scales, shears, or other non-rotation transformations then |
| 269 |
/// the resulting quaternion will be ill-defined. |
| 270 |
/// |
| 271 |
/// # Panics |
| 272 |
/// |
| 273 |
/// Will panic if any input matrix column is not normalized when `glam_assert` is enabled. |
| 274 |
#[inline] |
| 275 |
#[must_use] |
| 276 |
pub fn from_mat3a(mat: &Mat3A) -> Self { |
| 277 |
Self::from_rotation_axes(mat.x_axis.into(), mat.y_axis.into(), mat.z_axis.into()) |
| 278 |
} |
| 279 |
|
| 280 |
/// Creates a quaternion from the upper 3x3 rotation matrix inside a homogeneous 4x4 matrix. |
| 281 |
/// |
| 282 |
/// Note if the upper 3x3 matrix contain scales, shears, or other non-rotation transformations |
| 283 |
/// then the resulting quaternion will be ill-defined. |
| 284 |
/// |
| 285 |
/// # Panics |
| 286 |
/// |
| 287 |
/// Will panic if any column of the upper 3x3 rotation matrix is not normalized when |
| 288 |
/// `glam_assert` is enabled. |
| 289 |
#[inline] |
| 290 |
#[must_use] |
| 291 |
pub fn from_mat4(mat: &Mat4) -> Self { |
| 292 |
Self::from_rotation_axes( |
| 293 |
mat.x_axis.truncate(), |
| 294 |
mat.y_axis.truncate(), |
| 295 |
mat.z_axis.truncate(), |
| 296 |
) |
| 297 |
} |
| 298 |
|
| 299 |
/// Gets the minimal rotation for transforming `from` to `to`. The rotation is in the |
| 300 |
/// plane spanned by the two vectors. Will rotate at most 180 degrees. |
| 301 |
/// |
| 302 |
/// The inputs must be unit vectors. |
| 303 |
/// |
| 304 |
/// `from_rotation_arc(from, to) * from ≈ to`. |
| 305 |
/// |
| 306 |
/// For near-singular cases (from≈to and from≈-to) the current implementation |
| 307 |
/// is only accurate to about 0.001 (for `f32`). |
| 308 |
/// |
| 309 |
/// # Panics |
| 310 |
/// |
| 311 |
/// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled. |
| 312 |
#[must_use] |
| 313 |
pub fn from_rotation_arc(from: Vec3, to: Vec3) -> Self { |
| 314 |
glam_assert!(from.is_normalized()); |
| 315 |
glam_assert!(to.is_normalized()); |
| 316 |
|
| 317 |
const ONE_MINUS_EPS: f32 = 1.0 - 2.0 * f32::EPSILON; |
| 318 |
let dot = from.dot(to); |
| 319 |
if dot > ONE_MINUS_EPS { |
| 320 |
// 0° singularity: from ≈ to |
| 321 |
Self::IDENTITY |
| 322 |
} else if dot < -ONE_MINUS_EPS { |
| 323 |
// 180° singularity: from ≈ -to |
| 324 |
use core::f32::consts::PI; // half a turn = 𝛕/2 = 180° |
| 325 |
Self::from_axis_angle(from.any_orthonormal_vector(), PI) |
| 326 |
} else { |
| 327 |
let c = from.cross(to); |
| 328 |
Self::from_xyzw(c.x, c.y, c.z, 1.0 + dot).normalize() |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/// Gets the minimal rotation for transforming `from` to either `to` or `-to`. This means |
| 333 |
/// that the resulting quaternion will rotate `from` so that it is colinear with `to`. |
| 334 |
/// |
| 335 |
/// The rotation is in the plane spanned by the two vectors. Will rotate at most 90 |
| 336 |
/// degrees. |
| 337 |
/// |
| 338 |
/// The inputs must be unit vectors. |
| 339 |
/// |
| 340 |
/// `to.dot(from_rotation_arc_colinear(from, to) * from).abs() ≈ 1`. |
| 341 |
/// |
| 342 |
/// # Panics |
| 343 |
/// |
| 344 |
/// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled. |
| 345 |
#[inline] |
| 346 |
#[must_use] |
| 347 |
pub fn from_rotation_arc_colinear(from: Vec3, to: Vec3) -> Self { |
| 348 |
if from.dot(to) < 0.0 { |
| 349 |
Self::from_rotation_arc(from, -to) |
| 350 |
} else { |
| 351 |
Self::from_rotation_arc(from, to) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/// Gets the minimal rotation for transforming `from` to `to`. The resulting rotation is |
| 356 |
/// around the z axis. Will rotate at most 180 degrees. |
| 357 |
/// |
| 358 |
/// The inputs must be unit vectors. |
| 359 |
/// |
| 360 |
/// `from_rotation_arc_2d(from, to) * from ≈ to`. |
| 361 |
/// |
| 362 |
/// For near-singular cases (from≈to and from≈-to) the current implementation |
| 363 |
/// is only accurate to about 0.001 (for `f32`). |
| 364 |
/// |
| 365 |
/// # Panics |
| 366 |
/// |
| 367 |
/// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled. |
| 368 |
#[must_use] |
| 369 |
pub fn from_rotation_arc_2d(from: Vec2, to: Vec2) -> Self { |
| 370 |
glam_assert!(from.is_normalized()); |
| 371 |
glam_assert!(to.is_normalized()); |
| 372 |
|
| 373 |
const ONE_MINUS_EPSILON: f32 = 1.0 - 2.0 * f32::EPSILON; |
| 374 |
let dot = from.dot(to); |
| 375 |
if dot > ONE_MINUS_EPSILON { |
| 376 |
// 0° singularity: from ≈ to |
| 377 |
Self::IDENTITY |
| 378 |
} else if dot < -ONE_MINUS_EPSILON { |
| 379 |
// 180° singularity: from ≈ -to |
| 380 |
const COS_FRAC_PI_2: f32 = 0.0; |
| 381 |
const SIN_FRAC_PI_2: f32 = 1.0; |
| 382 |
// rotation around z by PI radians |
| 383 |
Self::from_xyzw(0.0, 0.0, SIN_FRAC_PI_2, COS_FRAC_PI_2) |
| 384 |
} else { |
| 385 |
// vector3 cross where z=0 |
| 386 |
let z = from.x * to.y - to.x * from.y; |
| 387 |
let w = 1.0 + dot; |
| 388 |
// calculate length with x=0 and y=0 to normalize |
| 389 |
let len_rcp = 1.0 / math::sqrt(z * z + w * w); |
| 390 |
Self::from_xyzw(0.0, 0.0, z * len_rcp, w * len_rcp) |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
/// Creates a quaterion rotation from a facing direction and an up direction. |
| 395 |
/// |
| 396 |
/// For a left-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 397 |
/// |
| 398 |
/// # Panics |
| 399 |
/// |
| 400 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 401 |
#[inline] |
| 402 |
#[must_use] |
| 403 |
pub fn look_to_lh(dir: Vec3, up: Vec3) -> Self { |
| 404 |
Self::look_to_rh(-dir, up) |
| 405 |
} |
| 406 |
|
| 407 |
/// Creates a quaterion rotation from facing direction and an up direction. |
| 408 |
/// |
| 409 |
/// For a right-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 410 |
/// |
| 411 |
/// # Panics |
| 412 |
/// |
| 413 |
/// Will panic if `dir` and `up` are not normalized when `glam_assert` is enabled. |
| 414 |
#[inline] |
| 415 |
#[must_use] |
| 416 |
pub fn look_to_rh(dir: Vec3, up: Vec3) -> Self { |
| 417 |
glam_assert!(dir.is_normalized()); |
| 418 |
glam_assert!(up.is_normalized()); |
| 419 |
let f = dir; |
| 420 |
let s = f.cross(up).normalize(); |
| 421 |
let u = s.cross(f); |
| 422 |
|
| 423 |
Self::from_rotation_axes( |
| 424 |
Vec3::new(s.x, u.x, -f.x), |
| 425 |
Vec3::new(s.y, u.y, -f.y), |
| 426 |
Vec3::new(s.z, u.z, -f.z), |
| 427 |
) |
| 428 |
} |
| 429 |
|
| 430 |
/// Creates a left-handed view matrix using a camera position, a focal point, and an up |
| 431 |
/// direction. |
| 432 |
/// |
| 433 |
/// For a left-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 434 |
/// |
| 435 |
/// # Panics |
| 436 |
/// |
| 437 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 438 |
#[inline] |
| 439 |
#[must_use] |
| 440 |
pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self { |
| 441 |
Self::look_to_lh(center.sub(eye).normalize(), up) |
| 442 |
} |
| 443 |
|
| 444 |
/// Creates a right-handed view matrix using a camera position, an up direction, and a focal |
| 445 |
/// point. |
| 446 |
/// |
| 447 |
/// For a right-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 448 |
/// |
| 449 |
/// # Panics |
| 450 |
/// |
| 451 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 452 |
#[inline] |
| 453 |
#[must_use] |
| 454 |
pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self { |
| 455 |
Self::look_to_rh(center.sub(eye).normalize(), up) |
| 456 |
} |
| 457 |
|
| 458 |
/// Returns the rotation axis (normalized) and angle (in radians) of `self`. |
| 459 |
#[inline] |
| 460 |
#[must_use] |
| 461 |
pub fn to_axis_angle(self) -> (Vec3, f32) { |
| 462 |
const EPSILON: f32 = 1.0e-8; |
| 463 |
let v = Vec3::new(self.x, self.y, self.z); |
| 464 |
let length = v.length(); |
| 465 |
if length >= EPSILON { |
| 466 |
let angle = 2.0 * math::atan2(length, self.w); |
| 467 |
let axis = v / length; |
| 468 |
(axis, angle) |
| 469 |
} else { |
| 470 |
(Vec3::X, 0.0) |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/// Returns the rotation axis scaled by the rotation in radians. |
| 475 |
#[inline] |
| 476 |
#[must_use] |
| 477 |
pub fn to_scaled_axis(self) -> Vec3 { |
| 478 |
let (axis, angle) = self.to_axis_angle(); |
| 479 |
axis * angle |
| 480 |
} |
| 481 |
|
| 482 |
/// Returns the rotation angles for the given euler rotation sequence. |
| 483 |
#[inline] |
| 484 |
#[must_use] |
| 485 |
pub fn to_euler(self, order: EulerRot) -> (f32, f32, f32) { |
| 486 |
self.to_euler_angles(order) |
| 487 |
} |
| 488 |
|
| 489 |
/// `[x, y, z, w]` |
| 490 |
#[inline] |
| 491 |
#[must_use] |
| 492 |
pub fn to_array(&self) -> [f32; 4] { |
| 493 |
[self.x, self.y, self.z, self.w] |
| 494 |
} |
| 495 |
|
| 496 |
/// Returns the vector part of the quaternion. |
| 497 |
#[inline] |
| 498 |
#[must_use] |
| 499 |
pub fn xyz(self) -> Vec3 { |
| 500 |
Vec3::new(self.x, self.y, self.z) |
| 501 |
} |
| 502 |
|
| 503 |
/// Returns the quaternion conjugate of `self`. For a unit quaternion the |
| 504 |
/// conjugate is also the inverse. |
| 505 |
#[inline] |
| 506 |
#[must_use] |
| 507 |
pub fn conjugate(self) -> Self { |
| 508 |
const SIGN: f32x4 = f32x4::from_array([-1.0, -1.0, -1.0, 1.0]); |
| 509 |
Self(self.0.mul(SIGN)) |
| 510 |
} |
| 511 |
|
| 512 |
/// Returns the inverse of a normalized quaternion. |
| 513 |
/// |
| 514 |
/// Typically quaternion inverse returns the conjugate of a normalized quaternion. |
| 515 |
/// Because `self` is assumed to already be unit length this method *does not* normalize |
| 516 |
/// before returning the conjugate. |
| 517 |
/// |
| 518 |
/// # Panics |
| 519 |
/// |
| 520 |
/// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 521 |
#[inline] |
| 522 |
#[must_use] |
| 523 |
pub fn inverse(self) -> Self { |
| 524 |
glam_assert!(self.is_normalized()); |
| 525 |
self.conjugate() |
| 526 |
} |
| 527 |
|
| 528 |
/// Computes the dot product of `self` and `rhs`. The dot product is |
| 529 |
/// equal to the cosine of the angle between two quaternion rotations. |
| 530 |
#[inline] |
| 531 |
#[must_use] |
| 532 |
pub fn dot(self, rhs: Self) -> f32 { |
| 533 |
Vec4::from(self).dot(Vec4::from(rhs)) |
| 534 |
} |
| 535 |
|
| 536 |
/// Computes the length of `self`. |
| 537 |
#[doc(alias = "magnitude")] |
| 538 |
#[inline] |
| 539 |
#[must_use] |
| 540 |
pub fn length(self) -> f32 { |
| 541 |
Vec4::from(self).length() |
| 542 |
} |
| 543 |
|
| 544 |
/// Computes the squared length of `self`. |
| 545 |
/// |
| 546 |
/// This is generally faster than `length()` as it avoids a square |
| 547 |
/// root operation. |
| 548 |
#[doc(alias = "magnitude2")] |
| 549 |
#[inline] |
| 550 |
#[must_use] |
| 551 |
pub fn length_squared(self) -> f32 { |
| 552 |
Vec4::from(self).length_squared() |
| 553 |
} |
| 554 |
|
| 555 |
/// Computes `1.0 / length()`. |
| 556 |
/// |
| 557 |
/// For valid results, `self` must _not_ be of length zero. |
| 558 |
#[inline] |
| 559 |
#[must_use] |
| 560 |
pub fn length_recip(self) -> f32 { |
| 561 |
Vec4::from(self).length_recip() |
| 562 |
} |
| 563 |
|
| 564 |
/// Returns `self` normalized to length 1.0. |
| 565 |
/// |
| 566 |
/// For valid results, `self` must _not_ be of length zero. |
| 567 |
/// |
| 568 |
/// Panics |
| 569 |
/// |
| 570 |
/// Will panic if `self` is zero length when `glam_assert` is enabled. |
| 571 |
#[inline] |
| 572 |
#[must_use] |
| 573 |
pub fn normalize(self) -> Self { |
| 574 |
Self::from_vec4(Vec4::from(self).normalize()) |
| 575 |
} |
| 576 |
|
| 577 |
/// Returns `true` if, and only if, all elements are finite. |
| 578 |
/// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 579 |
#[inline] |
| 580 |
#[must_use] |
| 581 |
pub fn is_finite(self) -> bool { |
| 582 |
Vec4::from(self).is_finite() |
| 583 |
} |
| 584 |
|
| 585 |
/// Returns `true` if any elements are `NAN`. |
| 586 |
#[inline] |
| 587 |
#[must_use] |
| 588 |
pub fn is_nan(self) -> bool { |
| 589 |
Vec4::from(self).is_nan() |
| 590 |
} |
| 591 |
|
| 592 |
/// Returns whether `self` of length `1.0` or not. |
| 593 |
/// |
| 594 |
/// Uses a precision threshold of `1e-6`. |
| 595 |
#[inline] |
| 596 |
#[must_use] |
| 597 |
pub fn is_normalized(self) -> bool { |
| 598 |
Vec4::from(self).is_normalized() |
| 599 |
} |
| 600 |
|
| 601 |
#[inline] |
| 602 |
#[must_use] |
| 603 |
pub fn is_near_identity(self) -> bool { |
| 604 |
// Based on https://github.com/nfrechette/rtm `rtm::quat_near_identity` |
| 605 |
// Because of floating point precision, we cannot represent very small rotations. |
| 606 |
// The closest f32 to 1.0 that is not 1.0 itself yields: |
| 607 |
// 0.99999994.acos() * 2.0 = 0.000690533954 rad |
| 608 |
// |
| 609 |
// An error threshold of 1.e-6 is used by default. |
| 610 |
// (1.0 - 1.e-6).acos() * 2.0 = 0.00284714461 rad |
| 611 |
// (1.0 - 1.e-7).acos() * 2.0 = 0.00097656250 rad |
| 612 |
// |
| 613 |
// We don't really care about the angle value itself, only if it's close to 0. |
| 614 |
// This will happen whenever quat.w is close to 1.0. |
| 615 |
// If the quat.w is close to -1.0, the angle will be near 2*PI which is close to |
| 616 |
// a negative 0 rotation. By forcing quat.w to be positive, we'll end up with |
| 617 |
// the shortest path. |
| 618 |
// |
| 619 |
// For f64 we're using a threshhold of |
| 620 |
// (1.0 - 1e-14).acos() * 2.0 |
| 621 |
const THRESHOLD_ANGLE: f32 = 0.002_847_144_6; |
| 622 |
let positive_w_angle = math::acos_approx(math::abs(self.w)) * 2.0; |
| 623 |
positive_w_angle < THRESHOLD_ANGLE |
| 624 |
} |
| 625 |
|
| 626 |
/// Returns the angle (in radians) for the minimal rotation |
| 627 |
/// for transforming this quaternion into another. |
| 628 |
/// |
| 629 |
/// Both quaternions must be normalized. |
| 630 |
/// |
| 631 |
/// # Panics |
| 632 |
/// |
| 633 |
/// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled. |
| 634 |
#[inline] |
| 635 |
#[must_use] |
| 636 |
pub fn angle_between(self, rhs: Self) -> f32 { |
| 637 |
glam_assert!(self.is_normalized() && rhs.is_normalized()); |
| 638 |
math::acos_approx(math::abs(self.dot(rhs))) * 2.0 |
| 639 |
} |
| 640 |
|
| 641 |
/// Rotates towards `rhs` up to `max_angle` (in radians). |
| 642 |
/// |
| 643 |
/// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to |
| 644 |
/// `self.angle_between(rhs)`, the result will be equal to `rhs`. If `max_angle` is negative, |
| 645 |
/// rotates towards the exact opposite of `rhs`. Will not go past the target. |
| 646 |
/// |
| 647 |
/// Both quaternions must be normalized. |
| 648 |
/// |
| 649 |
/// # Panics |
| 650 |
/// |
| 651 |
/// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled. |
| 652 |
#[inline] |
| 653 |
#[must_use] |
| 654 |
pub fn rotate_towards(&self, rhs: Self, max_angle: f32) -> Self { |
| 655 |
glam_assert!(self.is_normalized() && rhs.is_normalized()); |
| 656 |
let angle = self.angle_between(rhs); |
| 657 |
if angle <= 1e-4 { |
| 658 |
return rhs; |
| 659 |
} |
| 660 |
let s = (max_angle / angle).clamp(-1.0, 1.0); |
| 661 |
self.slerp(rhs, s) |
| 662 |
} |
| 663 |
|
| 664 |
/// Returns true if the absolute difference of all elements between `self` and `rhs` |
| 665 |
/// is less than or equal to `max_abs_diff`. |
| 666 |
/// |
| 667 |
/// This can be used to compare if two quaternions contain similar elements. It works |
| 668 |
/// best when comparing with a known value. The `max_abs_diff` that should be used used |
| 669 |
/// depends on the values being compared against. |
| 670 |
/// |
| 671 |
/// For more see |
| 672 |
/// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 673 |
#[inline] |
| 674 |
#[must_use] |
| 675 |
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool { |
| 676 |
Vec4::from(self).abs_diff_eq(Vec4::from(rhs), max_abs_diff) |
| 677 |
} |
| 678 |
|
| 679 |
#[inline(always)] |
| 680 |
#[must_use] |
| 681 |
fn lerp_impl(self, end: Self, s: f32) -> Self { |
| 682 |
(self * (1.0 - s) + end * s).normalize() |
| 683 |
} |
| 684 |
|
| 685 |
/// Performs a linear interpolation between `self` and `rhs` based on |
| 686 |
/// the value `s`. |
| 687 |
/// |
| 688 |
/// When `s` is `0.0`, the result will be equal to `self`. When `s` |
| 689 |
/// is `1.0`, the result will be equal to `rhs`. |
| 690 |
/// |
| 691 |
/// # Panics |
| 692 |
/// |
| 693 |
/// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled. |
| 694 |
#[doc(alias = "mix")] |
| 695 |
#[inline] |
| 696 |
#[must_use] |
| 697 |
pub fn lerp(self, end: Self, s: f32) -> Self { |
| 698 |
glam_assert!(self.is_normalized()); |
| 699 |
glam_assert!(end.is_normalized()); |
| 700 |
|
| 701 |
const NEG_ZERO: f32x4 = f32x4::from_array([-0.0; 4]); |
| 702 |
let dot = dot4_into_f32x4(self.0, end.0); |
| 703 |
// Calculate the bias, if the dot product is positive or zero, there is no bias |
| 704 |
// but if it is negative, we want to flip the 'end' rotation XYZW components |
| 705 |
let bias = f32x4_bitand(dot, NEG_ZERO); |
| 706 |
self.lerp_impl(Self(f32x4_bitxor(end.0, bias)), s) |
| 707 |
} |
| 708 |
|
| 709 |
/// Performs a spherical linear interpolation between `self` and `end` |
| 710 |
/// based on the value `s`. |
| 711 |
/// |
| 712 |
/// When `s` is `0.0`, the result will be equal to `self`. When `s` |
| 713 |
/// is `1.0`, the result will be equal to `end`. |
| 714 |
/// |
| 715 |
/// # Panics |
| 716 |
/// |
| 717 |
/// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled. |
| 718 |
#[inline] |
| 719 |
#[must_use] |
| 720 |
pub fn slerp(self, mut end: Self, s: f32) -> Self { |
| 721 |
// http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/ |
| 722 |
glam_assert!(self.is_normalized()); |
| 723 |
glam_assert!(end.is_normalized()); |
| 724 |
|
| 725 |
// Note that a rotation can be represented by two quaternions: `q` and |
| 726 |
// `-q`. The slerp path between `q` and `end` will be different from the |
| 727 |
// path between `-q` and `end`. One path will take the long way around and |
| 728 |
// one will take the short way. In order to correct for this, the `dot` |
| 729 |
// product between `self` and `end` should be positive. If the `dot` |
| 730 |
// product is negative, slerp between `self` and `-end`. |
| 731 |
let mut dot = self.dot(end); |
| 732 |
if dot < 0.0 { |
| 733 |
end = -end; |
| 734 |
dot = -dot; |
| 735 |
} |
| 736 |
|
| 737 |
const DOT_THRESHOLD: f32 = 1.0 - f32::EPSILON; |
| 738 |
if dot > DOT_THRESHOLD { |
| 739 |
// if above threshold perform linear interpolation to avoid divide by zero |
| 740 |
self.lerp_impl(end, s) |
| 741 |
} else { |
| 742 |
let theta = math::acos_approx(dot); |
| 743 |
|
| 744 |
let scale1 = math::sin(theta * (1.0 - s)); |
| 745 |
let scale2 = math::sin(theta * s); |
| 746 |
let theta_sin = math::sin(theta); |
| 747 |
((self * scale1) + (end * scale2)) * (1.0 / theta_sin) |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
/// Multiplies a quaternion and a 3D vector, returning the rotated vector. |
| 752 |
/// |
| 753 |
/// # Panics |
| 754 |
/// |
| 755 |
/// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 756 |
#[inline] |
| 757 |
#[must_use] |
| 758 |
pub fn mul_vec3(self, rhs: Vec3) -> Vec3 { |
| 759 |
glam_assert!(self.is_normalized()); |
| 760 |
|
| 761 |
self.mul_vec3a(rhs.into()).into() |
| 762 |
} |
| 763 |
|
| 764 |
/// Multiplies two quaternions. If they each represent a rotation, the result will |
| 765 |
/// represent the combined rotation. |
| 766 |
/// |
| 767 |
/// Note that due to floating point rounding the result may not be perfectly normalized. |
| 768 |
/// |
| 769 |
/// # Panics |
| 770 |
/// |
| 771 |
/// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled. |
| 772 |
#[inline] |
| 773 |
#[must_use] |
| 774 |
pub fn mul_quat(self, rhs: Self) -> Self { |
| 775 |
let lhs = self.0; |
| 776 |
let rhs = rhs.0; |
| 777 |
|
| 778 |
const CONTROL_WZYX: f32x4 = f32x4::from_array([1.0, -1.0, 1.0, -1.0]); |
| 779 |
const CONTROL_ZWXY: f32x4 = f32x4::from_array([1.0, 1.0, -1.0, -1.0]); |
| 780 |
const CONTROL_YXWZ: f32x4 = f32x4::from_array([-1.0, 1.0, 1.0, -1.0]); |
| 781 |
|
| 782 |
let r_xxxx = simd_swizzle!(lhs, [0, 0, 0, 0]); |
| 783 |
let r_yyyy = simd_swizzle!(lhs, [1, 1, 1, 1]); |
| 784 |
let r_zzzz = simd_swizzle!(lhs, [2, 2, 2, 2]); |
| 785 |
let r_wwww = simd_swizzle!(lhs, [3, 3, 3, 3]); |
| 786 |
|
| 787 |
let lxrw_lyrw_lzrw_lwrw = r_wwww * rhs; |
| 788 |
let l_wzyx = simd_swizzle!(rhs, [3, 2, 1, 0]); |
| 789 |
|
| 790 |
let lwrx_lzrx_lyrx_lxrx = r_xxxx * l_wzyx; |
| 791 |
let l_zwxy = simd_swizzle!(l_wzyx, [1, 0, 3, 2]); |
| 792 |
|
| 793 |
let lwrx_nlzrx_lyrx_nlxrx = lwrx_lzrx_lyrx_lxrx * CONTROL_WZYX; |
| 794 |
|
| 795 |
let lzry_lwry_lxry_lyry = r_yyyy * l_zwxy; |
| 796 |
let l_yxwz = simd_swizzle!(l_zwxy, [3, 2, 1, 0]); |
| 797 |
|
| 798 |
let lzry_lwry_nlxry_nlyry = lzry_lwry_lxry_lyry * CONTROL_ZWXY; |
| 799 |
|
| 800 |
let lyrz_lxrz_lwrz_lzrz = r_zzzz * l_yxwz; |
| 801 |
let result0 = lxrw_lyrw_lzrw_lwrw + lwrx_nlzrx_lyrx_nlxrx; |
| 802 |
|
| 803 |
let nlyrz_lxrz_lwrz_wlzrz = lyrz_lxrz_lwrz_lzrz * CONTROL_YXWZ; |
| 804 |
let result1 = lzry_lwry_nlxry_nlyry + nlyrz_lxrz_lwrz_wlzrz; |
| 805 |
Self(result0 + result1) |
| 806 |
} |
| 807 |
|
| 808 |
/// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform. |
| 809 |
/// |
| 810 |
/// Note if the input affine matrix contain scales, shears, or other non-rotation |
| 811 |
/// transformations then the resulting quaternion will be ill-defined. |
| 812 |
/// |
| 813 |
/// # Panics |
| 814 |
/// |
| 815 |
/// Will panic if any input affine matrix column is not normalized when `glam_assert` is |
| 816 |
/// enabled. |
| 817 |
#[inline] |
| 818 |
#[must_use] |
| 819 |
pub fn from_affine3(a: &crate::Affine3A) -> Self { |
| 820 |
#[allow(clippy::useless_conversion)] |
| 821 |
Self::from_rotation_axes( |
| 822 |
a.matrix3.x_axis.into(), |
| 823 |
a.matrix3.y_axis.into(), |
| 824 |
a.matrix3.z_axis.into(), |
| 825 |
) |
| 826 |
} |
| 827 |
|
| 828 |
/// Multiplies a quaternion and a 3D vector, returning the rotated vector. |
| 829 |
#[inline] |
| 830 |
#[must_use] |
| 831 |
pub fn mul_vec3a(self, rhs: Vec3A) -> Vec3A { |
| 832 |
const TWO: f32x4 = f32x4::from_array([2.0; 4]); |
| 833 |
let w = simd_swizzle!(self.0, [3, 3, 3, 3]); |
| 834 |
let b = self.0; |
| 835 |
let b2 = dot3_into_f32x4(b, b); |
| 836 |
Vec3A( |
| 837 |
rhs.0 |
| 838 |
.mul(w.mul(w).sub(b2)) |
| 839 |
.add(b.mul(dot3_into_f32x4(rhs.0, b).mul(TWO))) |
| 840 |
.add(Vec3A(b).cross(rhs).0.mul(w.mul(TWO))), |
| 841 |
) |
| 842 |
} |
| 843 |
|
| 844 |
#[inline] |
| 845 |
#[must_use] |
| 846 |
pub fn as_dquat(self) -> DQuat { |
| 847 |
DQuat::from_xyzw(self.x as f64, self.y as f64, self.z as f64, self.w as f64) |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
impl fmt::Debug for Quat { |
| 852 |
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 853 |
fmt.debug_tuple(stringify!(Quat)) |
| 854 |
.field(&self.x) |
| 855 |
.field(&self.y) |
| 856 |
.field(&self.z) |
| 857 |
.field(&self.w) |
| 858 |
.finish() |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
impl fmt::Display for Quat { |
| 863 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 864 |
if let Some(p) = f.precision() { |
| 865 |
write!( |
| 866 |
f, |
| 867 |
"[{:.*}, {:.*}, {:.*}, {:.*}]", |
| 868 |
p, self.x, p, self.y, p, self.z, p, self.w |
| 869 |
) |
| 870 |
} else { |
| 871 |
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w) |
| 872 |
} |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
impl Add for Quat { |
| 877 |
type Output = Self; |
| 878 |
/// Adds two quaternions. |
| 879 |
/// |
| 880 |
/// The sum is not guaranteed to be normalized. |
| 881 |
/// |
| 882 |
/// Note that addition is not the same as combining the rotations represented by the |
| 883 |
/// two quaternions! That corresponds to multiplication. |
| 884 |
#[inline] |
| 885 |
fn add(self, rhs: Self) -> Self { |
| 886 |
Self::from_vec4(Vec4::from(self) + Vec4::from(rhs)) |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
impl Add<&Self> for Quat { |
| 891 |
type Output = Self; |
| 892 |
#[inline] |
| 893 |
fn add(self, rhs: &Self) -> Self { |
| 894 |
self.add(*rhs) |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
impl Add<&Quat> for &Quat { |
| 899 |
type Output = Quat; |
| 900 |
#[inline] |
| 901 |
fn add(self, rhs: &Quat) -> Quat { |
| 902 |
(*self).add(*rhs) |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
impl Add<Quat> for &Quat { |
| 907 |
type Output = Quat; |
| 908 |
#[inline] |
| 909 |
fn add(self, rhs: Quat) -> Quat { |
| 910 |
(*self).add(rhs) |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
impl AddAssign for Quat { |
| 915 |
#[inline] |
| 916 |
fn add_assign(&mut self, rhs: Self) { |
| 917 |
*self = self.add(rhs); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
impl AddAssign<&Self> for Quat { |
| 922 |
#[inline] |
| 923 |
fn add_assign(&mut self, rhs: &Self) { |
| 924 |
self.add_assign(*rhs); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
impl Sub for Quat { |
| 929 |
type Output = Self; |
| 930 |
/// Subtracts the `rhs` quaternion from `self`. |
| 931 |
/// |
| 932 |
/// The difference is not guaranteed to be normalized. |
| 933 |
#[inline] |
| 934 |
fn sub(self, rhs: Self) -> Self { |
| 935 |
Self::from_vec4(Vec4::from(self) - Vec4::from(rhs)) |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
impl Sub<&Self> for Quat { |
| 940 |
type Output = Self; |
| 941 |
#[inline] |
| 942 |
fn sub(self, rhs: &Self) -> Self { |
| 943 |
self.sub(*rhs) |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
impl Sub<&Quat> for &Quat { |
| 948 |
type Output = Quat; |
| 949 |
#[inline] |
| 950 |
fn sub(self, rhs: &Quat) -> Quat { |
| 951 |
(*self).sub(*rhs) |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
impl Sub<Quat> for &Quat { |
| 956 |
type Output = Quat; |
| 957 |
#[inline] |
| 958 |
fn sub(self, rhs: Quat) -> Quat { |
| 959 |
(*self).sub(rhs) |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
impl SubAssign for Quat { |
| 964 |
#[inline] |
| 965 |
fn sub_assign(&mut self, rhs: Self) { |
| 966 |
*self = self.sub(rhs); |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
impl SubAssign<&Self> for Quat { |
| 971 |
#[inline] |
| 972 |
fn sub_assign(&mut self, rhs: &Self) { |
| 973 |
self.sub_assign(*rhs); |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
impl Mul<f32> for Quat { |
| 978 |
type Output = Self; |
| 979 |
/// Multiplies a quaternion by a scalar value. |
| 980 |
/// |
| 981 |
/// The product is not guaranteed to be normalized. |
| 982 |
#[inline] |
| 983 |
fn mul(self, rhs: f32) -> Self { |
| 984 |
Self::from_vec4(Vec4::from(self) * rhs) |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
impl Mul<&f32> for Quat { |
| 989 |
type Output = Self; |
| 990 |
#[inline] |
| 991 |
fn mul(self, rhs: &f32) -> Self { |
| 992 |
self.mul(*rhs) |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
impl Mul<&f32> for &Quat { |
| 997 |
type Output = Quat; |
| 998 |
#[inline] |
| 999 |
fn mul(self, rhs: &f32) -> Quat { |
| 1000 |
(*self).mul(*rhs) |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
impl Mul<f32> for &Quat { |
| 1005 |
type Output = Quat; |
| 1006 |
#[inline] |
| 1007 |
fn mul(self, rhs: f32) -> Quat { |
| 1008 |
(*self).mul(rhs) |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
impl MulAssign<f32> for Quat { |
| 1013 |
#[inline] |
| 1014 |
fn mul_assign(&mut self, rhs: f32) { |
| 1015 |
*self = self.mul(rhs); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
impl MulAssign<&f32> for Quat { |
| 1020 |
#[inline] |
| 1021 |
fn mul_assign(&mut self, rhs: &f32) { |
| 1022 |
self.mul_assign(*rhs); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
impl Div<f32> for Quat { |
| 1027 |
type Output = Self; |
| 1028 |
/// Divides a quaternion by a scalar value. |
| 1029 |
/// The quotient is not guaranteed to be normalized. |
| 1030 |
#[inline] |
| 1031 |
fn div(self, rhs: f32) -> Self { |
| 1032 |
Self::from_vec4(Vec4::from(self) / rhs) |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
impl Div<&f32> for Quat { |
| 1037 |
type Output = Self; |
| 1038 |
#[inline] |
| 1039 |
fn div(self, rhs: &f32) -> Self { |
| 1040 |
self.div(*rhs) |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
impl Div<&f32> for &Quat { |
| 1045 |
type Output = Quat; |
| 1046 |
#[inline] |
| 1047 |
fn div(self, rhs: &f32) -> Quat { |
| 1048 |
(*self).div(*rhs) |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
impl Div<f32> for &Quat { |
| 1053 |
type Output = Quat; |
| 1054 |
#[inline] |
| 1055 |
fn div(self, rhs: f32) -> Quat { |
| 1056 |
(*self).div(rhs) |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
impl DivAssign<f32> for Quat { |
| 1061 |
#[inline] |
| 1062 |
fn div_assign(&mut self, rhs: f32) { |
| 1063 |
*self = self.div(rhs); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
impl DivAssign<&f32> for Quat { |
| 1068 |
#[inline] |
| 1069 |
fn div_assign(&mut self, rhs: &f32) { |
| 1070 |
self.div_assign(*rhs); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
impl Mul for Quat { |
| 1075 |
type Output = Self; |
| 1076 |
/// Multiplies two quaternions. If they each represent a rotation, the result will |
| 1077 |
/// represent the combined rotation. |
| 1078 |
/// |
| 1079 |
/// Note that due to floating point rounding the result may not be perfectly |
| 1080 |
/// normalized. |
| 1081 |
/// |
| 1082 |
/// # Panics |
| 1083 |
/// |
| 1084 |
/// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled. |
| 1085 |
#[inline] |
| 1086 |
fn mul(self, rhs: Self) -> Self { |
| 1087 |
self.mul_quat(rhs) |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
impl Mul<&Self> for Quat { |
| 1092 |
type Output = Self; |
| 1093 |
#[inline] |
| 1094 |
fn mul(self, rhs: &Self) -> Self { |
| 1095 |
self.mul(*rhs) |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
impl Mul<&Quat> for &Quat { |
| 1100 |
type Output = Quat; |
| 1101 |
#[inline] |
| 1102 |
fn mul(self, rhs: &Quat) -> Quat { |
| 1103 |
(*self).mul(*rhs) |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
impl Mul<Quat> for &Quat { |
| 1108 |
type Output = Quat; |
| 1109 |
#[inline] |
| 1110 |
fn mul(self, rhs: Quat) -> Quat { |
| 1111 |
(*self).mul(rhs) |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
impl MulAssign for Quat { |
| 1116 |
#[inline] |
| 1117 |
fn mul_assign(&mut self, rhs: Self) { |
| 1118 |
*self = self.mul(rhs); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
impl MulAssign<&Self> for Quat { |
| 1123 |
#[inline] |
| 1124 |
fn mul_assign(&mut self, rhs: &Self) { |
| 1125 |
self.mul_assign(*rhs); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
impl Mul<Vec3> for Quat { |
| 1130 |
type Output = Vec3; |
| 1131 |
/// Multiplies a quaternion and a 3D vector, returning the rotated vector. |
| 1132 |
/// |
| 1133 |
/// # Panics |
| 1134 |
/// |
| 1135 |
/// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 1136 |
#[inline] |
| 1137 |
fn mul(self, rhs: Vec3) -> Self::Output { |
| 1138 |
self.mul_vec3(rhs) |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
impl Mul<&Vec3> for Quat { |
| 1143 |
type Output = Vec3; |
| 1144 |
#[inline] |
| 1145 |
fn mul(self, rhs: &Vec3) -> Vec3 { |
| 1146 |
self.mul(*rhs) |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
impl Mul<&Vec3> for &Quat { |
| 1151 |
type Output = Vec3; |
| 1152 |
#[inline] |
| 1153 |
fn mul(self, rhs: &Vec3) -> Vec3 { |
| 1154 |
(*self).mul(*rhs) |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
impl Mul<Vec3> for &Quat { |
| 1159 |
type Output = Vec3; |
| 1160 |
#[inline] |
| 1161 |
fn mul(self, rhs: Vec3) -> Vec3 { |
| 1162 |
(*self).mul(rhs) |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
impl Mul<Vec3A> for Quat { |
| 1167 |
type Output = Vec3A; |
| 1168 |
#[inline] |
| 1169 |
fn mul(self, rhs: Vec3A) -> Self::Output { |
| 1170 |
self.mul_vec3a(rhs) |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
impl Mul<&Vec3A> for Quat { |
| 1175 |
type Output = Vec3A; |
| 1176 |
#[inline] |
| 1177 |
fn mul(self, rhs: &Vec3A) -> Vec3A { |
| 1178 |
self.mul(*rhs) |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
impl Mul<&Vec3A> for &Quat { |
| 1183 |
type Output = Vec3A; |
| 1184 |
#[inline] |
| 1185 |
fn mul(self, rhs: &Vec3A) -> Vec3A { |
| 1186 |
(*self).mul(*rhs) |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
impl Mul<Vec3A> for &Quat { |
| 1191 |
type Output = Vec3A; |
| 1192 |
#[inline] |
| 1193 |
fn mul(self, rhs: Vec3A) -> Vec3A { |
| 1194 |
(*self).mul(rhs) |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
impl Neg for Quat { |
| 1199 |
type Output = Self; |
| 1200 |
#[inline] |
| 1201 |
fn neg(self) -> Self { |
| 1202 |
self * -1.0 |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
impl Neg for &Quat { |
| 1207 |
type Output = Quat; |
| 1208 |
#[inline] |
| 1209 |
fn neg(self) -> Quat { |
| 1210 |
(*self).neg() |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
impl Default for Quat { |
| 1215 |
#[inline] |
| 1216 |
fn default() -> Self { |
| 1217 |
Self::IDENTITY |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
impl PartialEq for Quat { |
| 1222 |
#[inline] |
| 1223 |
fn eq(&self, rhs: &Self) -> bool { |
| 1224 |
Vec4::from(*self).eq(&Vec4::from(*rhs)) |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
impl AsRef<[f32; 4]> for Quat { |
| 1229 |
#[inline] |
| 1230 |
fn as_ref(&self) -> &[f32; 4] { |
| 1231 |
unsafe { &*(self as *const Self as *const [f32; 4]) } |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
impl Sum<Self> for Quat { |
| 1236 |
fn sum<I>(iter: I) -> Self |
| 1237 |
where |
| 1238 |
I: Iterator<Item = Self>, |
| 1239 |
{ |
| 1240 |
iter.fold(Self::ZERO, Self::add) |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
impl<'a> Sum<&'a Self> for Quat { |
| 1245 |
fn sum<I>(iter: I) -> Self |
| 1246 |
where |
| 1247 |
I: Iterator<Item = &'a Self>, |
| 1248 |
{ |
| 1249 |
iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
impl Product for Quat { |
| 1254 |
fn product<I>(iter: I) -> Self |
| 1255 |
where |
| 1256 |
I: Iterator<Item = Self>, |
| 1257 |
{ |
| 1258 |
iter.fold(Self::IDENTITY, Self::mul) |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
impl<'a> Product<&'a Self> for Quat { |
| 1263 |
fn product<I>(iter: I) -> Self |
| 1264 |
where |
| 1265 |
I: Iterator<Item = &'a Self>, |
| 1266 |
{ |
| 1267 |
iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b)) |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
impl From<Quat> for Vec4 { |
| 1272 |
#[inline] |
| 1273 |
fn from(q: Quat) -> Self { |
| 1274 |
Self(q.0) |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
impl From<Quat> for (f32, f32, f32, f32) { |
| 1279 |
#[inline] |
| 1280 |
fn from(q: Quat) -> Self { |
| 1281 |
Vec4::from(q).into() |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
impl From<Quat> for [f32; 4] { |
| 1286 |
#[inline] |
| 1287 |
fn from(q: Quat) -> Self { |
| 1288 |
Vec4::from(q).into() |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
impl From<Quat> for f32x4 { |
| 1293 |
#[inline] |
| 1294 |
fn from(q: Quat) -> Self { |
| 1295 |
q.0 |
| 1296 |
} |
| 1297 |
} |
| 1298 |
|
| 1299 |
impl Deref for Quat { |
| 1300 |
type Target = crate::deref::Vec4<f32>; |
| 1301 |
#[inline] |
| 1302 |
fn deref(&self) -> &Self::Target { |
| 1303 |
unsafe { &*(self as *const Self).cast() } |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
impl DerefMut for Quat { |
| 1308 |
#[inline] |
| 1309 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 1310 |
unsafe { &mut *(self as *mut Self).cast() } |
| 1311 |
} |
| 1312 |
} |