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