| 1 |
// Generated from vec.rs.tera template. Edit the template, not the generated file. |
| 2 |
|
| 3 |
use crate::{coresimd::*, f32::math, BVec3, BVec3A, FloatExt, Quat, Vec2, Vec3, Vec4}; |
| 4 |
|
| 5 |
use core::fmt; |
| 6 |
use core::iter::{Product, Sum}; |
| 7 |
use core::{f32, ops::*}; |
| 8 |
|
| 9 |
use core::simd::{cmp::SimdPartialEq, cmp::SimdPartialOrd, num::SimdFloat, *}; |
| 10 |
use std::simd::StdFloat; |
| 11 |
|
| 12 |
/// Creates a 3-dimensional vector. |
| 13 |
#[inline(always)] |
| 14 |
#[must_use] |
| 15 |
pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A { |
| 16 |
Vec3A::new(x, y, z) |
| 17 |
} |
| 18 |
|
| 19 |
/// A 3-dimensional vector. |
| 20 |
/// |
| 21 |
/// SIMD vector types are used for storage on supported platforms for better |
| 22 |
/// performance than the [`Vec3`] type. |
| 23 |
/// |
| 24 |
/// It is possible to convert between [`Vec3`] and [`Vec3A`] types using [`From`] |
| 25 |
/// or [`Into`] trait implementations. |
| 26 |
/// |
| 27 |
/// This type is 16 byte aligned. |
| 28 |
#[derive(Clone, Copy)] |
| 29 |
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))] |
| 30 |
#[repr(transparent)] |
| 31 |
pub struct Vec3A(pub(crate) f32x4); |
| 32 |
|
| 33 |
impl Vec3A { |
| 34 |
/// All zeroes. |
| 35 |
pub const ZERO: Self = Self::splat(0.0); |
| 36 |
|
| 37 |
/// All ones. |
| 38 |
pub const ONE: Self = Self::splat(1.0); |
| 39 |
|
| 40 |
/// All negative ones. |
| 41 |
pub const NEG_ONE: Self = Self::splat(-1.0); |
| 42 |
|
| 43 |
/// All `f32::MIN`. |
| 44 |
pub const MIN: Self = Self::splat(f32::MIN); |
| 45 |
|
| 46 |
/// All `f32::MAX`. |
| 47 |
pub const MAX: Self = Self::splat(f32::MAX); |
| 48 |
|
| 49 |
/// All `f32::NAN`. |
| 50 |
pub const NAN: Self = Self::splat(f32::NAN); |
| 51 |
|
| 52 |
/// All `f32::INFINITY`. |
| 53 |
pub const INFINITY: Self = Self::splat(f32::INFINITY); |
| 54 |
|
| 55 |
/// All `f32::NEG_INFINITY`. |
| 56 |
pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY); |
| 57 |
|
| 58 |
/// A unit vector pointing along the positive X axis. |
| 59 |
pub const X: Self = Self::new(1.0, 0.0, 0.0); |
| 60 |
|
| 61 |
/// A unit vector pointing along the positive Y axis. |
| 62 |
pub const Y: Self = Self::new(0.0, 1.0, 0.0); |
| 63 |
|
| 64 |
/// A unit vector pointing along the positive Z axis. |
| 65 |
pub const Z: Self = Self::new(0.0, 0.0, 1.0); |
| 66 |
|
| 67 |
/// A unit vector pointing along the negative X axis. |
| 68 |
pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0); |
| 69 |
|
| 70 |
/// A unit vector pointing along the negative Y axis. |
| 71 |
pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0); |
| 72 |
|
| 73 |
/// A unit vector pointing along the negative Z axis. |
| 74 |
pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0); |
| 75 |
|
| 76 |
/// The unit axes. |
| 77 |
pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z]; |
| 78 |
|
| 79 |
/// Vec3A uses Rust Portable SIMD |
| 80 |
pub const USES_CORE_SIMD: bool = true; |
| 81 |
/// Vec3A uses Arm NEON |
| 82 |
pub const USES_NEON: bool = false; |
| 83 |
/// Vec3A uses scalar math |
| 84 |
pub const USES_SCALAR_MATH: bool = false; |
| 85 |
/// Vec3A uses Intel SSE2 |
| 86 |
pub const USES_SSE2: bool = false; |
| 87 |
/// Vec3A uses WebAssembly 128-bit SIMD |
| 88 |
pub const USES_WASM32_SIMD: bool = false; |
| 89 |
|
| 90 |
/// Creates a new vector. |
| 91 |
#[inline(always)] |
| 92 |
#[must_use] |
| 93 |
pub const fn new(x: f32, y: f32, z: f32) -> Self { |
| 94 |
Self(f32x4::from_array([x, y, z, z])) |
| 95 |
} |
| 96 |
|
| 97 |
/// Creates a vector with all elements set to `v`. |
| 98 |
#[inline] |
| 99 |
#[must_use] |
| 100 |
pub const fn splat(v: f32) -> Self { |
| 101 |
Self(Simd::from_array([v; 4])) |
| 102 |
} |
| 103 |
|
| 104 |
/// Returns a vector containing each element of `self` modified by a mapping function `f`. |
| 105 |
#[inline] |
| 106 |
#[must_use] |
| 107 |
pub fn map<F>(self, f: F) -> Self |
| 108 |
where |
| 109 |
F: Fn(f32) -> f32, |
| 110 |
{ |
| 111 |
Self::new(f(self.x), f(self.y), f(self.z)) |
| 112 |
} |
| 113 |
|
| 114 |
/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use |
| 115 |
/// for each element of `self`. |
| 116 |
/// |
| 117 |
/// A true element in the mask uses the corresponding element from `if_true`, and false |
| 118 |
/// uses the element from `if_false`. |
| 119 |
#[inline] |
| 120 |
#[must_use] |
| 121 |
pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self { |
| 122 |
Self(mask.0.select(if_true.0, if_false.0)) |
| 123 |
} |
| 124 |
|
| 125 |
/// Creates a new vector from an array. |
| 126 |
#[inline] |
| 127 |
#[must_use] |
| 128 |
pub const fn from_array(a: [f32; 3]) -> Self { |
| 129 |
Self::new(a[0], a[1], a[2]) |
| 130 |
} |
| 131 |
|
| 132 |
/// Converts `self` to `[x, y, z]` |
| 133 |
#[inline] |
| 134 |
#[must_use] |
| 135 |
pub const fn to_array(&self) -> [f32; 3] { |
| 136 |
unsafe { *(self as *const Self as *const [f32; 3]) } |
| 137 |
} |
| 138 |
|
| 139 |
/// Creates a vector from the first 3 values in `slice`. |
| 140 |
/// |
| 141 |
/// # Panics |
| 142 |
/// |
| 143 |
/// Panics if `slice` is less than 3 elements long. |
| 144 |
#[inline] |
| 145 |
#[must_use] |
| 146 |
pub const fn from_slice(slice: &[f32]) -> Self { |
| 147 |
assert!(slice.len() >= 3); |
| 148 |
Self::new(slice[0], slice[1], slice[2]) |
| 149 |
} |
| 150 |
|
| 151 |
/// Writes the elements of `self` to the first 3 elements in `slice`. |
| 152 |
/// |
| 153 |
/// # Panics |
| 154 |
/// |
| 155 |
/// Panics if `slice` is less than 3 elements long. |
| 156 |
#[inline] |
| 157 |
pub fn write_to_slice(self, slice: &mut [f32]) { |
| 158 |
slice[..3].copy_from_slice(&self.to_array()); |
| 159 |
} |
| 160 |
|
| 161 |
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`. |
| 162 |
/// |
| 163 |
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop. |
| 164 |
#[inline] |
| 165 |
#[must_use] |
| 166 |
pub fn from_vec4(v: Vec4) -> Self { |
| 167 |
Self(v.0) |
| 168 |
} |
| 169 |
|
| 170 |
/// Creates a 4D vector from `self` and the given `w` value. |
| 171 |
#[inline] |
| 172 |
#[must_use] |
| 173 |
pub fn extend(self, w: f32) -> Vec4 { |
| 174 |
Vec4::new(self.x, self.y, self.z, w) |
| 175 |
} |
| 176 |
|
| 177 |
/// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. |
| 178 |
/// |
| 179 |
/// Truncation may also be performed by using [`self.xy()`][crate::swizzles::Vec3Swizzles::xy()]. |
| 180 |
#[inline] |
| 181 |
#[must_use] |
| 182 |
pub fn truncate(self) -> Vec2 { |
| 183 |
use crate::swizzles::Vec3Swizzles; |
| 184 |
self.xy() |
| 185 |
} |
| 186 |
|
| 187 |
/// Projects a homogeneous coordinate to 3D space by performing perspective divide. |
| 188 |
/// |
| 189 |
/// # Panics |
| 190 |
/// |
| 191 |
/// Will panic if `v.w` is `0` when `glam_assert` is enabled. |
| 192 |
#[inline] |
| 193 |
#[must_use] |
| 194 |
pub fn from_homogeneous(v: Vec4) -> Self { |
| 195 |
glam_assert!(v.w != 0.0); |
| 196 |
Self::from_vec4(v) / v.w |
| 197 |
} |
| 198 |
|
| 199 |
/// Creates a homogeneous coordinate from `self`, equivalent to `self.extend(1.0)`. |
| 200 |
#[inline] |
| 201 |
#[must_use] |
| 202 |
pub fn to_homogeneous(self) -> Vec4 { |
| 203 |
self.extend(1.0) |
| 204 |
} |
| 205 |
|
| 206 |
// Converts `self` to a `Vec3`. |
| 207 |
#[inline] |
| 208 |
#[must_use] |
| 209 |
pub fn to_vec3(self) -> Vec3 { |
| 210 |
Vec3::from(self) |
| 211 |
} |
| 212 |
|
| 213 |
/// Creates a 3D vector from `self` with the given value of `x`. |
| 214 |
#[inline] |
| 215 |
#[must_use] |
| 216 |
pub fn with_x(mut self, x: f32) -> Self { |
| 217 |
self.x = x; |
| 218 |
self |
| 219 |
} |
| 220 |
|
| 221 |
/// Creates a 3D vector from `self` with the given value of `y`. |
| 222 |
#[inline] |
| 223 |
#[must_use] |
| 224 |
pub fn with_y(mut self, y: f32) -> Self { |
| 225 |
self.y = y; |
| 226 |
self |
| 227 |
} |
| 228 |
|
| 229 |
/// Creates a 3D vector from `self` with the given value of `z`. |
| 230 |
#[inline] |
| 231 |
#[must_use] |
| 232 |
pub fn with_z(mut self, z: f32) -> Self { |
| 233 |
self.z = z; |
| 234 |
self |
| 235 |
} |
| 236 |
|
| 237 |
/// Computes the dot product of `self` and `rhs`. |
| 238 |
#[inline] |
| 239 |
#[must_use] |
| 240 |
pub fn dot(self, rhs: Self) -> f32 { |
| 241 |
dot3(self.0, rhs.0) |
| 242 |
} |
| 243 |
|
| 244 |
/// Returns a vector where every component is the dot product of `self` and `rhs`. |
| 245 |
#[inline] |
| 246 |
#[must_use] |
| 247 |
pub fn dot_into_vec(self, rhs: Self) -> Self { |
| 248 |
Self(dot3_into_f32x4(self.0, rhs.0)) |
| 249 |
} |
| 250 |
|
| 251 |
/// Computes the cross product of `self` and `rhs`. |
| 252 |
#[inline] |
| 253 |
#[must_use] |
| 254 |
pub fn cross(self, rhs: Self) -> Self { |
| 255 |
let lhszxy = simd_swizzle!(self.0, [2, 0, 1, 1]); |
| 256 |
let rhszxy = simd_swizzle!(rhs.0, [2, 0, 1, 1]); |
| 257 |
let lhszxy_rhs = lhszxy * rhs.0; |
| 258 |
let rhszxy_lhs = rhszxy * self.0; |
| 259 |
let sub = lhszxy_rhs - rhszxy_lhs; |
| 260 |
Self(simd_swizzle!(sub, [2, 0, 1, 1])) |
| 261 |
} |
| 262 |
|
| 263 |
/// Returns a vector containing the minimum values for each element of `self` and `rhs`. |
| 264 |
/// |
| 265 |
/// In other words this computes `[min(x, rhs.x), min(self.y, rhs.y), ..]`. |
| 266 |
/// |
| 267 |
/// NaN propogation does not follow IEEE 754-2008 semantics for minNum and may differ on |
| 268 |
/// different SIMD architectures. |
| 269 |
#[inline] |
| 270 |
#[must_use] |
| 271 |
pub fn min(self, rhs: Self) -> Self { |
| 272 |
Self(self.0.simd_lt(rhs.0).select(self.0, rhs.0)) |
| 273 |
} |
| 274 |
|
| 275 |
/// Returns a vector containing the maximum values for each element of `self` and `rhs`. |
| 276 |
/// |
| 277 |
/// In other words this computes `[max(self.x, rhs.x), max(self.y, rhs.y), ..]`. |
| 278 |
/// |
| 279 |
/// NaN propogation does not follow IEEE 754-2008 semantics for maxNum and may differ on |
| 280 |
/// different SIMD architectures. |
| 281 |
#[inline] |
| 282 |
#[must_use] |
| 283 |
pub fn max(self, rhs: Self) -> Self { |
| 284 |
Self(self.0.simd_gt(rhs.0).select(self.0, rhs.0)) |
| 285 |
} |
| 286 |
|
| 287 |
/// Component-wise clamping of values, similar to [`f32::clamp`]. |
| 288 |
/// |
| 289 |
/// Each element in `min` must be less-or-equal to the corresponding element in `max`. |
| 290 |
/// |
| 291 |
/// NaN propogation does not follow IEEE 754-2008 semantics and may differ on |
| 292 |
/// different SIMD architectures. |
| 293 |
/// |
| 294 |
/// # Panics |
| 295 |
/// |
| 296 |
/// Will panic if `min` is greater than `max` when `glam_assert` is enabled. |
| 297 |
#[inline] |
| 298 |
#[must_use] |
| 299 |
pub fn clamp(self, min: Self, max: Self) -> Self { |
| 300 |
glam_assert!(min.cmple(max).all(), "clamp: expected min <= max"); |
| 301 |
self.max(min).min(max) |
| 302 |
} |
| 303 |
|
| 304 |
/// Returns the horizontal minimum of `self`. |
| 305 |
/// |
| 306 |
/// In other words this computes `min(x, y, ..)`. |
| 307 |
/// |
| 308 |
/// NaN propogation does not follow IEEE 754-2008 semantics and may differ on |
| 309 |
/// different SIMD architectures. |
| 310 |
#[inline] |
| 311 |
#[must_use] |
| 312 |
pub fn min_element(self) -> f32 { |
| 313 |
let min = |a: f32x4, b: f32x4| a.simd_lt(b).select(a, b); |
| 314 |
let v = self.0; |
| 315 |
let v = min(v, simd_swizzle!(v, [2, 2, 1, 1])); |
| 316 |
let v = min(v, simd_swizzle!(v, [1, 0, 0, 0])); |
| 317 |
v[0] |
| 318 |
} |
| 319 |
|
| 320 |
/// Returns the horizontal maximum of `self`. |
| 321 |
/// |
| 322 |
/// In other words this computes `max(x, y, ..)`. |
| 323 |
/// |
| 324 |
/// NaN propogation does not follow IEEE 754-2008 semantics and may differ on |
| 325 |
/// different SIMD architectures. |
| 326 |
#[inline] |
| 327 |
#[must_use] |
| 328 |
pub fn max_element(self) -> f32 { |
| 329 |
let max = |a: f32x4, b: f32x4| a.simd_gt(b).select(a, b); |
| 330 |
let v = self.0; |
| 331 |
let v = max(v, simd_swizzle!(v, [2, 2, 0, 0])); |
| 332 |
let v = max(v, simd_swizzle!(v, [1, 0, 0, 0])); |
| 333 |
v[0] |
| 334 |
} |
| 335 |
|
| 336 |
/// Returns the index of the first minimum element of `self`. |
| 337 |
#[doc(alias = "argmin")] |
| 338 |
#[inline] |
| 339 |
#[must_use] |
| 340 |
pub fn min_position(self) -> usize { |
| 341 |
let mut min = self.x; |
| 342 |
let mut index = 0; |
| 343 |
if self.y < min { |
| 344 |
min = self.y; |
| 345 |
index = 1; |
| 346 |
} |
| 347 |
if self.z < min { |
| 348 |
index = 2; |
| 349 |
} |
| 350 |
index |
| 351 |
} |
| 352 |
|
| 353 |
/// Returns the index of the first maximum element of `self`. |
| 354 |
#[doc(alias = "argmax")] |
| 355 |
#[inline] |
| 356 |
#[must_use] |
| 357 |
pub fn max_position(self) -> usize { |
| 358 |
let mut max = self.x; |
| 359 |
let mut index = 0; |
| 360 |
if self.y > max { |
| 361 |
max = self.y; |
| 362 |
index = 1; |
| 363 |
} |
| 364 |
if self.z > max { |
| 365 |
index = 2; |
| 366 |
} |
| 367 |
index |
| 368 |
} |
| 369 |
|
| 370 |
/// Returns the sum of all elements of `self`. |
| 371 |
/// |
| 372 |
/// In other words, this computes `self.x + self.y + ..`. |
| 373 |
#[inline] |
| 374 |
#[must_use] |
| 375 |
pub fn element_sum(self) -> f32 { |
| 376 |
simd_swizzle!(self.0, Self::ZERO.0, [0, 1, 2, 4]).reduce_sum() |
| 377 |
} |
| 378 |
|
| 379 |
/// Returns the product of all elements of `self`. |
| 380 |
/// |
| 381 |
/// In other words, this computes `self.x * self.y * ..`. |
| 382 |
#[inline] |
| 383 |
#[must_use] |
| 384 |
pub fn element_product(self) -> f32 { |
| 385 |
simd_swizzle!(self.0, Self::ONE.0, [0, 1, 2, 4]).reduce_product() |
| 386 |
} |
| 387 |
|
| 388 |
/// Returns a vector mask containing the result of a `==` comparison for each element of |
| 389 |
/// `self` and `rhs`. |
| 390 |
/// |
| 391 |
/// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all |
| 392 |
/// elements. |
| 393 |
#[inline] |
| 394 |
#[must_use] |
| 395 |
pub fn cmpeq(self, rhs: Self) -> BVec3A { |
| 396 |
BVec3A(f32x4::simd_eq(self.0, rhs.0)) |
| 397 |
} |
| 398 |
|
| 399 |
/// Returns a vector mask containing the result of a `!=` comparison for each element of |
| 400 |
/// `self` and `rhs`. |
| 401 |
/// |
| 402 |
/// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all |
| 403 |
/// elements. |
| 404 |
#[inline] |
| 405 |
#[must_use] |
| 406 |
pub fn cmpne(self, rhs: Self) -> BVec3A { |
| 407 |
BVec3A(f32x4::simd_ne(self.0, rhs.0)) |
| 408 |
} |
| 409 |
|
| 410 |
/// Returns a vector mask containing the result of a `>=` comparison for each element of |
| 411 |
/// `self` and `rhs`. |
| 412 |
/// |
| 413 |
/// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all |
| 414 |
/// elements. |
| 415 |
#[inline] |
| 416 |
#[must_use] |
| 417 |
pub fn cmpge(self, rhs: Self) -> BVec3A { |
| 418 |
BVec3A(f32x4::simd_ge(self.0, rhs.0)) |
| 419 |
} |
| 420 |
|
| 421 |
/// Returns a vector mask containing the result of a `>` comparison for each element of |
| 422 |
/// `self` and `rhs`. |
| 423 |
/// |
| 424 |
/// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all |
| 425 |
/// elements. |
| 426 |
#[inline] |
| 427 |
#[must_use] |
| 428 |
pub fn cmpgt(self, rhs: Self) -> BVec3A { |
| 429 |
BVec3A(f32x4::simd_gt(self.0, rhs.0)) |
| 430 |
} |
| 431 |
|
| 432 |
/// Returns a vector mask containing the result of a `<=` comparison for each element of |
| 433 |
/// `self` and `rhs`. |
| 434 |
/// |
| 435 |
/// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all |
| 436 |
/// elements. |
| 437 |
#[inline] |
| 438 |
#[must_use] |
| 439 |
pub fn cmple(self, rhs: Self) -> BVec3A { |
| 440 |
BVec3A(f32x4::simd_le(self.0, rhs.0)) |
| 441 |
} |
| 442 |
|
| 443 |
/// Returns a vector mask containing the result of a `<` comparison for each element of |
| 444 |
/// `self` and `rhs`. |
| 445 |
/// |
| 446 |
/// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all |
| 447 |
/// elements. |
| 448 |
#[inline] |
| 449 |
#[must_use] |
| 450 |
pub fn cmplt(self, rhs: Self) -> BVec3A { |
| 451 |
BVec3A(f32x4::simd_lt(self.0, rhs.0)) |
| 452 |
} |
| 453 |
|
| 454 |
/// Returns a vector containing the absolute value of each element of `self`. |
| 455 |
#[inline] |
| 456 |
#[must_use] |
| 457 |
pub fn abs(self) -> Self { |
| 458 |
Self(self.0.abs()) |
| 459 |
} |
| 460 |
|
| 461 |
/// Returns a vector with elements representing the sign of `self`. |
| 462 |
/// |
| 463 |
/// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 464 |
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 465 |
/// - `NAN` if the number is `NAN` |
| 466 |
#[inline] |
| 467 |
#[must_use] |
| 468 |
pub fn signum(self) -> Self { |
| 469 |
Self(self.0.signum()) |
| 470 |
} |
| 471 |
|
| 472 |
/// Returns a vector with signs of `rhs` and the magnitudes of `self`. |
| 473 |
#[inline] |
| 474 |
#[must_use] |
| 475 |
pub fn copysign(self, rhs: Self) -> Self { |
| 476 |
Self(self.0.copysign(rhs.0)) |
| 477 |
} |
| 478 |
|
| 479 |
/// Returns a bitmask with the lowest 3 bits set to the sign bits from the elements of `self`. |
| 480 |
/// |
| 481 |
/// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes |
| 482 |
/// into the first lowest bit, element `y` into the second, etc. |
| 483 |
/// |
| 484 |
/// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign |
| 485 |
/// bit and negative infinity. |
| 486 |
#[inline] |
| 487 |
#[must_use] |
| 488 |
pub fn is_negative_bitmask(self) -> u32 { |
| 489 |
(self.0.is_sign_negative().to_bitmask() & 0x7) as u32 |
| 490 |
} |
| 491 |
|
| 492 |
/// Returns `true` if, and only if, all elements are finite. If any element is either |
| 493 |
/// `NaN`, positive or negative infinity, this will return `false`. |
| 494 |
#[inline] |
| 495 |
#[must_use] |
| 496 |
pub fn is_finite(self) -> bool { |
| 497 |
self.is_finite_mask().all() |
| 498 |
} |
| 499 |
|
| 500 |
/// Performs `is_finite` on each element of self, returning a vector mask of the results. |
| 501 |
/// |
| 502 |
/// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`. |
| 503 |
#[inline] |
| 504 |
#[must_use] |
| 505 |
pub fn is_finite_mask(self) -> BVec3A { |
| 506 |
BVec3A(f32x4::is_finite(self.0)) |
| 507 |
} |
| 508 |
|
| 509 |
/// Returns `true` if any elements are `NaN`. |
| 510 |
#[inline] |
| 511 |
#[must_use] |
| 512 |
pub fn is_nan(self) -> bool { |
| 513 |
self.is_nan_mask().any() |
| 514 |
} |
| 515 |
|
| 516 |
/// Performs `is_nan` on each element of self, returning a vector mask of the results. |
| 517 |
/// |
| 518 |
/// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`. |
| 519 |
#[inline] |
| 520 |
#[must_use] |
| 521 |
pub fn is_nan_mask(self) -> BVec3A { |
| 522 |
BVec3A(f32x4::is_nan(self.0)) |
| 523 |
} |
| 524 |
|
| 525 |
/// Computes the length of `self`. |
| 526 |
#[doc(alias = "magnitude")] |
| 527 |
#[inline] |
| 528 |
#[must_use] |
| 529 |
pub fn length(self) -> f32 { |
| 530 |
let dot = dot3_in_x(self.0, self.0); |
| 531 |
dot.sqrt()[0] |
| 532 |
} |
| 533 |
|
| 534 |
/// Computes the squared length of `self`. |
| 535 |
/// |
| 536 |
/// This is faster than `length()` as it avoids a square root operation. |
| 537 |
#[doc(alias = "magnitude2")] |
| 538 |
#[inline] |
| 539 |
#[must_use] |
| 540 |
pub fn length_squared(self) -> f32 { |
| 541 |
self.dot(self) |
| 542 |
} |
| 543 |
|
| 544 |
/// Computes `1.0 / length()`. |
| 545 |
/// |
| 546 |
/// For valid results, `self` must _not_ be of length zero. |
| 547 |
#[inline] |
| 548 |
#[must_use] |
| 549 |
pub fn length_recip(self) -> f32 { |
| 550 |
let dot = dot3_in_x(self.0, self.0); |
| 551 |
dot.sqrt().recip()[0] |
| 552 |
} |
| 553 |
|
| 554 |
/// Computes the Euclidean distance between two points in space. |
| 555 |
#[inline] |
| 556 |
#[must_use] |
| 557 |
pub fn distance(self, rhs: Self) -> f32 { |
| 558 |
(self - rhs).length() |
| 559 |
} |
| 560 |
|
| 561 |
/// Compute the squared euclidean distance between two points in space. |
| 562 |
#[inline] |
| 563 |
#[must_use] |
| 564 |
pub fn distance_squared(self, rhs: Self) -> f32 { |
| 565 |
(self - rhs).length_squared() |
| 566 |
} |
| 567 |
|
| 568 |
/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`. |
| 569 |
#[inline] |
| 570 |
#[must_use] |
| 571 |
pub fn div_euclid(self, rhs: Self) -> Self { |
| 572 |
Self::new( |
| 573 |
math::div_euclid(self.x, rhs.x), |
| 574 |
math::div_euclid(self.y, rhs.y), |
| 575 |
math::div_euclid(self.z, rhs.z), |
| 576 |
) |
| 577 |
} |
| 578 |
|
| 579 |
/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`. |
| 580 |
/// |
| 581 |
/// [Euclidean division]: f32::rem_euclid |
| 582 |
#[inline] |
| 583 |
#[must_use] |
| 584 |
pub fn rem_euclid(self, rhs: Self) -> Self { |
| 585 |
Self::new( |
| 586 |
math::rem_euclid(self.x, rhs.x), |
| 587 |
math::rem_euclid(self.y, rhs.y), |
| 588 |
math::rem_euclid(self.z, rhs.z), |
| 589 |
) |
| 590 |
} |
| 591 |
|
| 592 |
/// Returns `self` normalized to length 1.0. |
| 593 |
/// |
| 594 |
/// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero. |
| 595 |
/// |
| 596 |
/// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`]. |
| 597 |
/// |
| 598 |
/// # Panics |
| 599 |
/// |
| 600 |
/// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled. |
| 601 |
#[inline] |
| 602 |
#[must_use] |
| 603 |
pub fn normalize(self) -> Self { |
| 604 |
let length = dot3_into_f32x4(self.0, self.0).sqrt(); |
| 605 |
#[allow(clippy::let_and_return)] |
| 606 |
let normalized = Self(self.0 / length); |
| 607 |
glam_assert!(normalized.is_finite()); |
| 608 |
normalized |
| 609 |
} |
| 610 |
|
| 611 |
/// Returns `self` normalized to length 1.0 if possible, else returns `None`. |
| 612 |
/// |
| 613 |
/// In particular, if the input is zero (or very close to zero), or non-finite, |
| 614 |
/// the result of this operation will be `None`. |
| 615 |
/// |
| 616 |
/// See also [`Self::normalize_or_zero()`]. |
| 617 |
#[inline] |
| 618 |
#[must_use] |
| 619 |
pub fn try_normalize(self) -> Option<Self> { |
| 620 |
let rcp = self.length_recip(); |
| 621 |
if rcp.is_finite() && rcp > 0.0 { |
| 622 |
Some(self * rcp) |
| 623 |
} else { |
| 624 |
None |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
/// Returns `self` normalized to length 1.0 if possible, else returns a |
| 629 |
/// fallback value. |
| 630 |
/// |
| 631 |
/// In particular, if the input is zero (or very close to zero), or non-finite, |
| 632 |
/// the result of this operation will be the fallback value. |
| 633 |
/// |
| 634 |
/// See also [`Self::try_normalize()`]. |
| 635 |
#[inline] |
| 636 |
#[must_use] |
| 637 |
pub fn normalize_or(self, fallback: Self) -> Self { |
| 638 |
let rcp = self.length_recip(); |
| 639 |
if rcp.is_finite() && rcp > 0.0 { |
| 640 |
self * rcp |
| 641 |
} else { |
| 642 |
fallback |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
/// Returns `self` normalized to length 1.0 if possible, else returns zero. |
| 647 |
/// |
| 648 |
/// In particular, if the input is zero (or very close to zero), or non-finite, |
| 649 |
/// the result of this operation will be zero. |
| 650 |
/// |
| 651 |
/// See also [`Self::try_normalize()`]. |
| 652 |
#[inline] |
| 653 |
#[must_use] |
| 654 |
pub fn normalize_or_zero(self) -> Self { |
| 655 |
self.normalize_or(Self::ZERO) |
| 656 |
} |
| 657 |
|
| 658 |
/// Returns `self` normalized to length 1.0 and the length of `self`. |
| 659 |
/// |
| 660 |
/// If `self` is zero length then `(Self::X, 0.0)` is returned. |
| 661 |
#[inline] |
| 662 |
#[must_use] |
| 663 |
pub fn normalize_and_length(self) -> (Self, f32) { |
| 664 |
let length = self.length(); |
| 665 |
let rcp = 1.0 / length; |
| 666 |
if rcp.is_finite() && rcp > 0.0 { |
| 667 |
(self * rcp, length) |
| 668 |
} else { |
| 669 |
(Self::X, 0.0) |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
/// Returns whether `self` is length `1.0` or not. |
| 674 |
/// |
| 675 |
/// Uses a precision threshold of approximately `1e-4`. |
| 676 |
#[inline] |
| 677 |
#[must_use] |
| 678 |
pub fn is_normalized(self) -> bool { |
| 679 |
math::abs(self.length_squared() - 1.0) <= 2e-4 |
| 680 |
} |
| 681 |
|
| 682 |
/// Returns the vector projection of `self` onto `rhs`. |
| 683 |
/// |
| 684 |
/// `rhs` must be of non-zero length. |
| 685 |
/// |
| 686 |
/// # Panics |
| 687 |
/// |
| 688 |
/// Will panic if `rhs` is zero length when `glam_assert` is enabled. |
| 689 |
#[inline] |
| 690 |
#[must_use] |
| 691 |
pub fn project_onto(self, rhs: Self) -> Self { |
| 692 |
let other_len_sq_rcp = rhs.dot(rhs).recip(); |
| 693 |
glam_assert!(other_len_sq_rcp.is_finite()); |
| 694 |
rhs * self.dot(rhs) * other_len_sq_rcp |
| 695 |
} |
| 696 |
|
| 697 |
/// Returns the vector rejection of `self` from `rhs`. |
| 698 |
/// |
| 699 |
/// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 700 |
/// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 701 |
/// |
| 702 |
/// `rhs` must be of non-zero length. |
| 703 |
/// |
| 704 |
/// # Panics |
| 705 |
/// |
| 706 |
/// Will panic if `rhs` has a length of zero when `glam_assert` is enabled. |
| 707 |
#[doc(alias("plane"))] |
| 708 |
#[inline] |
| 709 |
#[must_use] |
| 710 |
pub fn reject_from(self, rhs: Self) -> Self { |
| 711 |
self - self.project_onto(rhs) |
| 712 |
} |
| 713 |
|
| 714 |
/// Returns the vector projection of `self` onto `rhs`. |
| 715 |
/// |
| 716 |
/// `rhs` must be normalized. |
| 717 |
/// |
| 718 |
/// # Panics |
| 719 |
/// |
| 720 |
/// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 721 |
#[inline] |
| 722 |
#[must_use] |
| 723 |
pub fn project_onto_normalized(self, rhs: Self) -> Self { |
| 724 |
glam_assert!(rhs.is_normalized()); |
| 725 |
rhs * self.dot(rhs) |
| 726 |
} |
| 727 |
|
| 728 |
/// Returns the vector rejection of `self` from `rhs`. |
| 729 |
/// |
| 730 |
/// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 731 |
/// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 732 |
/// |
| 733 |
/// `rhs` must be normalized. |
| 734 |
/// |
| 735 |
/// # Panics |
| 736 |
/// |
| 737 |
/// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 738 |
#[doc(alias("plane"))] |
| 739 |
#[inline] |
| 740 |
#[must_use] |
| 741 |
pub fn reject_from_normalized(self, rhs: Self) -> Self { |
| 742 |
self - self.project_onto_normalized(rhs) |
| 743 |
} |
| 744 |
|
| 745 |
/// Returns a vector containing the nearest integer to a number for each element of `self`. |
| 746 |
/// Round half-way cases away from 0.0. |
| 747 |
#[inline] |
| 748 |
#[must_use] |
| 749 |
pub fn round(self) -> Self { |
| 750 |
Self(self.0.round()) |
| 751 |
} |
| 752 |
|
| 753 |
/// Returns a vector containing the largest integer less than or equal to a number for each |
| 754 |
/// element of `self`. |
| 755 |
#[inline] |
| 756 |
#[must_use] |
| 757 |
pub fn floor(self) -> Self { |
| 758 |
Self(self.0.floor()) |
| 759 |
} |
| 760 |
|
| 761 |
/// Returns a vector containing the smallest integer greater than or equal to a number for |
| 762 |
/// each element of `self`. |
| 763 |
#[inline] |
| 764 |
#[must_use] |
| 765 |
pub fn ceil(self) -> Self { |
| 766 |
Self(self.0.ceil()) |
| 767 |
} |
| 768 |
|
| 769 |
/// Returns a vector containing the integer part each element of `self`. This means numbers are |
| 770 |
/// always truncated towards zero. |
| 771 |
#[inline] |
| 772 |
#[must_use] |
| 773 |
pub fn trunc(self) -> Self { |
| 774 |
Self(self.0.trunc()) |
| 775 |
} |
| 776 |
|
| 777 |
/// Returns a vector containing the fractional part of the vector as `self - self.trunc()`. |
| 778 |
/// |
| 779 |
/// Note that this differs from the GLSL implementation of `fract` which returns |
| 780 |
/// `self - self.floor()`. |
| 781 |
/// |
| 782 |
/// Note that this is fast but not precise for large numbers. |
| 783 |
#[inline] |
| 784 |
#[must_use] |
| 785 |
pub fn fract(self) -> Self { |
| 786 |
self - self.trunc() |
| 787 |
} |
| 788 |
|
| 789 |
/// Returns a vector containing the fractional part of the vector as `self - self.floor()`. |
| 790 |
/// |
| 791 |
/// Note that this differs from the Rust implementation of `fract` which returns |
| 792 |
/// `self - self.trunc()`. |
| 793 |
/// |
| 794 |
/// Note that this is fast but not precise for large numbers. |
| 795 |
#[inline] |
| 796 |
#[must_use] |
| 797 |
pub fn fract_gl(self) -> Self { |
| 798 |
self - self.floor() |
| 799 |
} |
| 800 |
|
| 801 |
/// Returns a vector containing `e^self` (the exponential function) for each element of |
| 802 |
/// `self`. |
| 803 |
#[inline] |
| 804 |
#[must_use] |
| 805 |
pub fn exp(self) -> Self { |
| 806 |
Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z)) |
| 807 |
} |
| 808 |
|
| 809 |
/// Returns a vector containing `2^self` for each element of `self`. |
| 810 |
#[inline] |
| 811 |
#[must_use] |
| 812 |
pub fn exp2(self) -> Self { |
| 813 |
Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z)) |
| 814 |
} |
| 815 |
|
| 816 |
/// Returns a vector containing the natural logarithm for each element of `self`. |
| 817 |
/// This returns NaN when the element is negative and negative infinity when the element is zero. |
| 818 |
#[inline] |
| 819 |
#[must_use] |
| 820 |
pub fn ln(self) -> Self { |
| 821 |
Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z)) |
| 822 |
} |
| 823 |
|
| 824 |
/// Returns a vector containing the base 2 logarithm for each element of `self`. |
| 825 |
/// This returns NaN when the element is negative and negative infinity when the element is zero. |
| 826 |
#[inline] |
| 827 |
#[must_use] |
| 828 |
pub fn log2(self) -> Self { |
| 829 |
Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z)) |
| 830 |
} |
| 831 |
|
| 832 |
/// Returns a vector containing each element of `self` raised to the power of `n`. |
| 833 |
#[inline] |
| 834 |
#[must_use] |
| 835 |
pub fn powf(self, n: f32) -> Self { |
| 836 |
Self::new( |
| 837 |
math::powf(self.x, n), |
| 838 |
math::powf(self.y, n), |
| 839 |
math::powf(self.z, n), |
| 840 |
) |
| 841 |
} |
| 842 |
|
| 843 |
/// Returns a vector containing the reciprocal `1.0/n` of each element of `self`. |
| 844 |
#[inline] |
| 845 |
#[must_use] |
| 846 |
pub fn recip(self) -> Self { |
| 847 |
Self(self.0.recip()) |
| 848 |
} |
| 849 |
|
| 850 |
/// Performs a linear interpolation between `self` and `rhs` based on the value `s`. |
| 851 |
/// |
| 852 |
/// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result |
| 853 |
/// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly |
| 854 |
/// extrapolated. |
| 855 |
#[doc(alias = "mix")] |
| 856 |
#[inline] |
| 857 |
#[must_use] |
| 858 |
pub fn lerp(self, rhs: Self, s: f32) -> Self { |
| 859 |
self * (1.0 - s) + rhs * s |
| 860 |
} |
| 861 |
|
| 862 |
/// Moves towards `rhs` based on the value `d`. |
| 863 |
/// |
| 864 |
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to |
| 865 |
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`. |
| 866 |
#[inline] |
| 867 |
#[must_use] |
| 868 |
pub fn move_towards(&self, rhs: Self, d: f32) -> Self { |
| 869 |
let a = rhs - *self; |
| 870 |
let len = a.length(); |
| 871 |
if len <= d || len <= 1e-4 { |
| 872 |
return rhs; |
| 873 |
} |
| 874 |
*self + a / len * d |
| 875 |
} |
| 876 |
|
| 877 |
/// Calculates the midpoint between `self` and `rhs`. |
| 878 |
/// |
| 879 |
/// The midpoint is the average of, or halfway point between, two vectors. |
| 880 |
/// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)` |
| 881 |
/// while being slightly cheaper to compute. |
| 882 |
#[inline] |
| 883 |
pub fn midpoint(self, rhs: Self) -> Self { |
| 884 |
(self + rhs) * 0.5 |
| 885 |
} |
| 886 |
|
| 887 |
/// Returns true if the absolute difference of all elements between `self` and `rhs` is |
| 888 |
/// less than or equal to `max_abs_diff`. |
| 889 |
/// |
| 890 |
/// This can be used to compare if two vectors contain similar elements. It works best when |
| 891 |
/// comparing with a known value. The `max_abs_diff` that should be used used depends on |
| 892 |
/// the values being compared against. |
| 893 |
/// |
| 894 |
/// For more see |
| 895 |
/// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 896 |
#[inline] |
| 897 |
#[must_use] |
| 898 |
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool { |
| 899 |
self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all() |
| 900 |
} |
| 901 |
|
| 902 |
/// Returns a vector with a length no less than `min` and no more than `max`. |
| 903 |
/// |
| 904 |
/// # Panics |
| 905 |
/// |
| 906 |
/// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled. |
| 907 |
#[inline] |
| 908 |
#[must_use] |
| 909 |
pub fn clamp_length(self, min: f32, max: f32) -> Self { |
| 910 |
glam_assert!(0.0 <= min); |
| 911 |
glam_assert!(min <= max); |
| 912 |
let length_sq = self.length_squared(); |
| 913 |
if length_sq < min * min { |
| 914 |
min * (self / math::sqrt(length_sq)) |
| 915 |
} else if length_sq > max * max { |
| 916 |
max * (self / math::sqrt(length_sq)) |
| 917 |
} else { |
| 918 |
self |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
/// Returns a vector with a length no more than `max`. |
| 923 |
/// |
| 924 |
/// # Panics |
| 925 |
/// |
| 926 |
/// Will panic if `max` is negative when `glam_assert` is enabled. |
| 927 |
#[inline] |
| 928 |
#[must_use] |
| 929 |
pub fn clamp_length_max(self, max: f32) -> Self { |
| 930 |
glam_assert!(0.0 <= max); |
| 931 |
let length_sq = self.length_squared(); |
| 932 |
if length_sq > max * max { |
| 933 |
max * (self / math::sqrt(length_sq)) |
| 934 |
} else { |
| 935 |
self |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
/// Returns a vector with a length no less than `min`. |
| 940 |
/// |
| 941 |
/// # Panics |
| 942 |
/// |
| 943 |
/// Will panic if `min` is negative when `glam_assert` is enabled. |
| 944 |
#[inline] |
| 945 |
#[must_use] |
| 946 |
pub fn clamp_length_min(self, min: f32) -> Self { |
| 947 |
glam_assert!(0.0 <= min); |
| 948 |
let length_sq = self.length_squared(); |
| 949 |
if length_sq < min * min { |
| 950 |
min * (self / math::sqrt(length_sq)) |
| 951 |
} else { |
| 952 |
self |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
/// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding |
| 957 |
/// error, yielding a more accurate result than an unfused multiply-add. |
| 958 |
/// |
| 959 |
/// Using `mul_add` *may* be more performant than an unfused multiply-add if the target |
| 960 |
/// architecture has a dedicated fma CPU instruction. However, this is not always true, |
| 961 |
/// and will be heavily dependant on designing algorithms with specific target hardware in |
| 962 |
/// mind. |
| 963 |
#[inline] |
| 964 |
#[must_use] |
| 965 |
pub fn mul_add(self, a: Self, b: Self) -> Self { |
| 966 |
Self(self.0.mul_add(a.0, b.0)) |
| 967 |
} |
| 968 |
|
| 969 |
/// Returns the reflection vector for a given incident vector `self` and surface normal |
| 970 |
/// `normal`. |
| 971 |
/// |
| 972 |
/// `normal` must be normalized. |
| 973 |
/// |
| 974 |
/// # Panics |
| 975 |
/// |
| 976 |
/// Will panic if `normal` is not normalized when `glam_assert` is enabled. |
| 977 |
#[inline] |
| 978 |
#[must_use] |
| 979 |
pub fn reflect(self, normal: Self) -> Self { |
| 980 |
glam_assert!(normal.is_normalized()); |
| 981 |
self - 2.0 * self.dot(normal) * normal |
| 982 |
} |
| 983 |
|
| 984 |
/// Returns the refraction direction for a given incident vector `self`, surface normal |
| 985 |
/// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs, |
| 986 |
/// a zero vector will be returned. |
| 987 |
/// |
| 988 |
/// `self` and `normal` must be normalized. |
| 989 |
/// |
| 990 |
/// # Panics |
| 991 |
/// |
| 992 |
/// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled. |
| 993 |
#[inline] |
| 994 |
#[must_use] |
| 995 |
pub fn refract(self, normal: Self, eta: f32) -> Self { |
| 996 |
glam_assert!(self.is_normalized()); |
| 997 |
glam_assert!(normal.is_normalized()); |
| 998 |
let n_dot_i = normal.dot(self); |
| 999 |
let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i); |
| 1000 |
if k >= 0.0 { |
| 1001 |
eta * self - (eta * n_dot_i + math::sqrt(k)) * normal |
| 1002 |
} else { |
| 1003 |
Self::ZERO |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
/// Returns the angle (in radians) between two vectors in the range `[0, +Ï€]`. |
| 1008 |
/// |
| 1009 |
/// The inputs do not need to be unit vectors however they must be non-zero. |
| 1010 |
#[inline] |
| 1011 |
#[must_use] |
| 1012 |
pub fn angle_between(self, rhs: Self) -> f32 { |
| 1013 |
math::acos_approx( |
| 1014 |
self.dot(rhs) |
| 1015 |
.div(math::sqrt(self.length_squared().mul(rhs.length_squared()))), |
| 1016 |
) |
| 1017 |
} |
| 1018 |
|
| 1019 |
/// Rotates around the x axis by `angle` (in radians). |
| 1020 |
#[inline] |
| 1021 |
#[must_use] |
| 1022 |
pub fn rotate_x(self, angle: f32) -> Self { |
| 1023 |
let (sina, cosa) = math::sin_cos(angle); |
| 1024 |
Self::new( |
| 1025 |
self.x, |
| 1026 |
self.y * cosa - self.z * sina, |
| 1027 |
self.y * sina + self.z * cosa, |
| 1028 |
) |
| 1029 |
} |
| 1030 |
|
| 1031 |
/// Rotates around the y axis by `angle` (in radians). |
| 1032 |
#[inline] |
| 1033 |
#[must_use] |
| 1034 |
pub fn rotate_y(self, angle: f32) -> Self { |
| 1035 |
let (sina, cosa) = math::sin_cos(angle); |
| 1036 |
Self::new( |
| 1037 |
self.x * cosa + self.z * sina, |
| 1038 |
self.y, |
| 1039 |
self.x * -sina + self.z * cosa, |
| 1040 |
) |
| 1041 |
} |
| 1042 |
|
| 1043 |
/// Rotates around the z axis by `angle` (in radians). |
| 1044 |
#[inline] |
| 1045 |
#[must_use] |
| 1046 |
pub fn rotate_z(self, angle: f32) -> Self { |
| 1047 |
let (sina, cosa) = math::sin_cos(angle); |
| 1048 |
Self::new( |
| 1049 |
self.x * cosa - self.y * sina, |
| 1050 |
self.x * sina + self.y * cosa, |
| 1051 |
self.z, |
| 1052 |
) |
| 1053 |
} |
| 1054 |
|
| 1055 |
/// Rotates around `axis` by `angle` (in radians). |
| 1056 |
/// |
| 1057 |
/// The axis must be a unit vector. |
| 1058 |
/// |
| 1059 |
/// # Panics |
| 1060 |
/// |
| 1061 |
/// Will panic if `axis` is not normalized when `glam_assert` is enabled. |
| 1062 |
#[inline] |
| 1063 |
#[must_use] |
| 1064 |
pub fn rotate_axis(self, axis: Self, angle: f32) -> Self { |
| 1065 |
Quat::from_axis_angle(axis.into(), angle) * self |
| 1066 |
} |
| 1067 |
|
| 1068 |
/// Rotates towards `rhs` up to `max_angle` (in radians). |
| 1069 |
/// |
| 1070 |
/// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to |
| 1071 |
/// `self.angle_between(rhs)`, the result will be parallel to `rhs`. If `max_angle` is negative, |
| 1072 |
/// rotates towards the exact opposite of `rhs`. Will not go past the target. |
| 1073 |
#[inline] |
| 1074 |
#[must_use] |
| 1075 |
pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self { |
| 1076 |
let angle_between = self.angle_between(rhs); |
| 1077 |
// When `max_angle < 0`, rotate no further than `PI` radians away |
| 1078 |
let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between); |
| 1079 |
let axis = self |
| 1080 |
.cross(rhs) |
| 1081 |
.try_normalize() |
| 1082 |
.unwrap_or_else(|| self.any_orthogonal_vector().normalize()); |
| 1083 |
Quat::from_axis_angle(axis.into(), angle) * self |
| 1084 |
} |
| 1085 |
|
| 1086 |
/// Returns some vector that is orthogonal to the given one. |
| 1087 |
/// |
| 1088 |
/// The input vector must be finite and non-zero. |
| 1089 |
/// |
| 1090 |
/// The output vector is not necessarily unit length. For that use |
| 1091 |
/// [`Self::any_orthonormal_vector()`] instead. |
| 1092 |
#[inline] |
| 1093 |
#[must_use] |
| 1094 |
pub fn any_orthogonal_vector(&self) -> Self { |
| 1095 |
// This can probably be optimized |
| 1096 |
if math::abs(self.x) > math::abs(self.y) { |
| 1097 |
Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y) |
| 1098 |
} else { |
| 1099 |
Self::new(0.0, self.z, -self.y) // self.cross(Self::X) |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
/// Returns any unit vector that is orthogonal to the given one. |
| 1104 |
/// |
| 1105 |
/// The input vector must be unit length. |
| 1106 |
/// |
| 1107 |
/// # Panics |
| 1108 |
/// |
| 1109 |
/// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 1110 |
#[inline] |
| 1111 |
#[must_use] |
| 1112 |
pub fn any_orthonormal_vector(&self) -> Self { |
| 1113 |
glam_assert!(self.is_normalized()); |
| 1114 |
// From https://graphics.pixar.com/library/OrthonormalB/paper.pdf |
| 1115 |
let sign = math::signum(self.z); |
| 1116 |
let a = -1.0 / (sign + self.z); |
| 1117 |
let b = self.x * self.y * a; |
| 1118 |
Self::new(b, sign + self.y * self.y * a, -self.y) |
| 1119 |
} |
| 1120 |
|
| 1121 |
/// Given a unit vector return two other vectors that together form an orthonormal |
| 1122 |
/// basis. That is, all three vectors are orthogonal to each other and are normalized. |
| 1123 |
/// |
| 1124 |
/// # Panics |
| 1125 |
/// |
| 1126 |
/// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 1127 |
#[inline] |
| 1128 |
#[must_use] |
| 1129 |
pub fn any_orthonormal_pair(&self) -> (Self, Self) { |
| 1130 |
glam_assert!(self.is_normalized()); |
| 1131 |
// From https://graphics.pixar.com/library/OrthonormalB/paper.pdf |
| 1132 |
let sign = math::signum(self.z); |
| 1133 |
let a = -1.0 / (sign + self.z); |
| 1134 |
let b = self.x * self.y * a; |
| 1135 |
( |
| 1136 |
Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x), |
| 1137 |
Self::new(b, sign + self.y * self.y * a, -self.y), |
| 1138 |
) |
| 1139 |
} |
| 1140 |
|
| 1141 |
/// Performs a spherical linear interpolation between `self` and `rhs` based on the value `s`. |
| 1142 |
/// |
| 1143 |
/// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result |
| 1144 |
/// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly |
| 1145 |
/// extrapolated. |
| 1146 |
#[inline] |
| 1147 |
#[must_use] |
| 1148 |
pub fn slerp(self, rhs: Self, s: f32) -> Self { |
| 1149 |
let self_length = self.length(); |
| 1150 |
let rhs_length = rhs.length(); |
| 1151 |
// Cosine of the angle between the vectors [-1, 1], or NaN if either vector has a zero length |
| 1152 |
let dot = self.dot(rhs) / (self_length * rhs_length); |
| 1153 |
// If dot is close to 1 or -1, or is NaN the calculations for t1 and t2 break down |
| 1154 |
if math::abs(dot) < 1.0 - 3e-7 { |
| 1155 |
// Angle between the vectors [0, +Ï€] |
| 1156 |
let theta = math::acos_approx(dot); |
| 1157 |
// Sine of the angle between vectors [0, 1] |
| 1158 |
let sin_theta = math::sin(theta); |
| 1159 |
let t1 = math::sin(theta * (1. - s)); |
| 1160 |
let t2 = math::sin(theta * s); |
| 1161 |
|
| 1162 |
// Interpolate vector lengths |
| 1163 |
let result_length = self_length.lerp(rhs_length, s); |
| 1164 |
// Scale the vectors to the target length and interpolate them |
| 1165 |
return (self * (result_length / self_length) * t1 |
| 1166 |
+ rhs * (result_length / rhs_length) * t2) |
| 1167 |
* sin_theta.recip(); |
| 1168 |
} |
| 1169 |
if dot < 0.0 { |
| 1170 |
// Vectors are almost parallel in opposing directions |
| 1171 |
|
| 1172 |
// Create a rotation from self to rhs along some axis |
| 1173 |
let axis = self.any_orthogonal_vector().normalize().into(); |
| 1174 |
let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s); |
| 1175 |
// Interpolate vector lengths |
| 1176 |
let result_length = self_length.lerp(rhs_length, s); |
| 1177 |
rotation * self * (result_length / self_length) |
| 1178 |
} else { |
| 1179 |
// Vectors are almost parallel in the same direction, or dot was NaN |
| 1180 |
self.lerp(rhs, s) |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
/// Casts all elements of `self` to `f64`. |
| 1185 |
#[inline] |
| 1186 |
#[must_use] |
| 1187 |
pub fn as_dvec3(&self) -> crate::DVec3 { |
| 1188 |
crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64) |
| 1189 |
} |
| 1190 |
|
| 1191 |
/// Casts all elements of `self` to `i8`. |
| 1192 |
#[inline] |
| 1193 |
#[must_use] |
| 1194 |
pub fn as_i8vec3(&self) -> crate::I8Vec3 { |
| 1195 |
crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8) |
| 1196 |
} |
| 1197 |
|
| 1198 |
/// Casts all elements of `self` to `u8`. |
| 1199 |
#[inline] |
| 1200 |
#[must_use] |
| 1201 |
pub fn as_u8vec3(&self) -> crate::U8Vec3 { |
| 1202 |
crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8) |
| 1203 |
} |
| 1204 |
|
| 1205 |
/// Casts all elements of `self` to `i16`. |
| 1206 |
#[inline] |
| 1207 |
#[must_use] |
| 1208 |
pub fn as_i16vec3(&self) -> crate::I16Vec3 { |
| 1209 |
crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16) |
| 1210 |
} |
| 1211 |
|
| 1212 |
/// Casts all elements of `self` to `u16`. |
| 1213 |
#[inline] |
| 1214 |
#[must_use] |
| 1215 |
pub fn as_u16vec3(&self) -> crate::U16Vec3 { |
| 1216 |
crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16) |
| 1217 |
} |
| 1218 |
|
| 1219 |
/// Casts all elements of `self` to `i32`. |
| 1220 |
#[inline] |
| 1221 |
#[must_use] |
| 1222 |
pub fn as_ivec3(&self) -> crate::IVec3 { |
| 1223 |
crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32) |
| 1224 |
} |
| 1225 |
|
| 1226 |
/// Casts all elements of `self` to `u32`. |
| 1227 |
#[inline] |
| 1228 |
#[must_use] |
| 1229 |
pub fn as_uvec3(&self) -> crate::UVec3 { |
| 1230 |
crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32) |
| 1231 |
} |
| 1232 |
|
| 1233 |
/// Casts all elements of `self` to `i64`. |
| 1234 |
#[inline] |
| 1235 |
#[must_use] |
| 1236 |
pub fn as_i64vec3(&self) -> crate::I64Vec3 { |
| 1237 |
crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64) |
| 1238 |
} |
| 1239 |
|
| 1240 |
/// Casts all elements of `self` to `u64`. |
| 1241 |
#[inline] |
| 1242 |
#[must_use] |
| 1243 |
pub fn as_u64vec3(&self) -> crate::U64Vec3 { |
| 1244 |
crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64) |
| 1245 |
} |
| 1246 |
|
| 1247 |
/// Casts all elements of `self` to `usize`. |
| 1248 |
#[inline] |
| 1249 |
#[must_use] |
| 1250 |
pub fn as_usizevec3(&self) -> crate::USizeVec3 { |
| 1251 |
crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize) |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
impl Default for Vec3A { |
| 1256 |
#[inline(always)] |
| 1257 |
fn default() -> Self { |
| 1258 |
Self::ZERO |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
impl PartialEq for Vec3A { |
| 1263 |
#[inline] |
| 1264 |
fn eq(&self, rhs: &Self) -> bool { |
| 1265 |
self.cmpeq(*rhs).all() |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
impl Div for Vec3A { |
| 1270 |
type Output = Self; |
| 1271 |
#[inline] |
| 1272 |
fn div(self, rhs: Self) -> Self { |
| 1273 |
Self(self.0 / rhs.0) |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
impl Div<&Self> for Vec3A { |
| 1278 |
type Output = Self; |
| 1279 |
#[inline] |
| 1280 |
fn div(self, rhs: &Self) -> Self { |
| 1281 |
self.div(*rhs) |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
impl Div<&Vec3A> for &Vec3A { |
| 1286 |
type Output = Vec3A; |
| 1287 |
#[inline] |
| 1288 |
fn div(self, rhs: &Vec3A) -> Vec3A { |
| 1289 |
(*self).div(*rhs) |
| 1290 |
} |
| 1291 |
} |
| 1292 |
|
| 1293 |
impl Div<Vec3A> for &Vec3A { |
| 1294 |
type Output = Vec3A; |
| 1295 |
#[inline] |
| 1296 |
fn div(self, rhs: Vec3A) -> Vec3A { |
| 1297 |
(*self).div(rhs) |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
impl DivAssign for Vec3A { |
| 1302 |
#[inline] |
| 1303 |
fn div_assign(&mut self, rhs: Self) { |
| 1304 |
self.0 /= rhs.0; |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
impl DivAssign<&Self> for Vec3A { |
| 1309 |
#[inline] |
| 1310 |
fn div_assign(&mut self, rhs: &Self) { |
| 1311 |
self.div_assign(*rhs); |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
impl Div<f32> for Vec3A { |
| 1316 |
type Output = Self; |
| 1317 |
#[inline] |
| 1318 |
fn div(self, rhs: f32) -> Self { |
| 1319 |
Self(self.0 / f32x4::splat(rhs)) |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
impl Div<&f32> for Vec3A { |
| 1324 |
type Output = Self; |
| 1325 |
#[inline] |
| 1326 |
fn div(self, rhs: &f32) -> Self { |
| 1327 |
self.div(*rhs) |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
impl Div<&f32> for &Vec3A { |
| 1332 |
type Output = Vec3A; |
| 1333 |
#[inline] |
| 1334 |
fn div(self, rhs: &f32) -> Vec3A { |
| 1335 |
(*self).div(*rhs) |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
impl Div<f32> for &Vec3A { |
| 1340 |
type Output = Vec3A; |
| 1341 |
#[inline] |
| 1342 |
fn div(self, rhs: f32) -> Vec3A { |
| 1343 |
(*self).div(rhs) |
| 1344 |
} |
| 1345 |
} |
| 1346 |
|
| 1347 |
impl DivAssign<f32> for Vec3A { |
| 1348 |
#[inline] |
| 1349 |
fn div_assign(&mut self, rhs: f32) { |
| 1350 |
self.0 /= f32x4::splat(rhs); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
impl DivAssign<&f32> for Vec3A { |
| 1355 |
#[inline] |
| 1356 |
fn div_assign(&mut self, rhs: &f32) { |
| 1357 |
self.div_assign(*rhs); |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
impl Div<Vec3A> for f32 { |
| 1362 |
type Output = Vec3A; |
| 1363 |
#[inline] |
| 1364 |
fn div(self, rhs: Vec3A) -> Vec3A { |
| 1365 |
Vec3A(f32x4::splat(self) / rhs.0) |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
impl Div<&Vec3A> for f32 { |
| 1370 |
type Output = Vec3A; |
| 1371 |
#[inline] |
| 1372 |
fn div(self, rhs: &Vec3A) -> Vec3A { |
| 1373 |
self.div(*rhs) |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
impl Div<&Vec3A> for &f32 { |
| 1378 |
type Output = Vec3A; |
| 1379 |
#[inline] |
| 1380 |
fn div(self, rhs: &Vec3A) -> Vec3A { |
| 1381 |
(*self).div(*rhs) |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
impl Div<Vec3A> for &f32 { |
| 1386 |
type Output = Vec3A; |
| 1387 |
#[inline] |
| 1388 |
fn div(self, rhs: Vec3A) -> Vec3A { |
| 1389 |
(*self).div(rhs) |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
impl Mul for Vec3A { |
| 1394 |
type Output = Self; |
| 1395 |
#[inline] |
| 1396 |
fn mul(self, rhs: Self) -> Self { |
| 1397 |
Self(self.0 * rhs.0) |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
impl Mul<&Self> for Vec3A { |
| 1402 |
type Output = Self; |
| 1403 |
#[inline] |
| 1404 |
fn mul(self, rhs: &Self) -> Self { |
| 1405 |
self.mul(*rhs) |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
impl Mul<&Vec3A> for &Vec3A { |
| 1410 |
type Output = Vec3A; |
| 1411 |
#[inline] |
| 1412 |
fn mul(self, rhs: &Vec3A) -> Vec3A { |
| 1413 |
(*self).mul(*rhs) |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
impl Mul<Vec3A> for &Vec3A { |
| 1418 |
type Output = Vec3A; |
| 1419 |
#[inline] |
| 1420 |
fn mul(self, rhs: Vec3A) -> Vec3A { |
| 1421 |
(*self).mul(rhs) |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
impl MulAssign for Vec3A { |
| 1426 |
#[inline] |
| 1427 |
fn mul_assign(&mut self, rhs: Self) { |
| 1428 |
self.0 *= rhs.0; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
impl MulAssign<&Self> for Vec3A { |
| 1433 |
#[inline] |
| 1434 |
fn mul_assign(&mut self, rhs: &Self) { |
| 1435 |
self.mul_assign(*rhs); |
| 1436 |
} |
| 1437 |
} |
| 1438 |
|
| 1439 |
impl Mul<f32> for Vec3A { |
| 1440 |
type Output = Self; |
| 1441 |
#[inline] |
| 1442 |
fn mul(self, rhs: f32) -> Self { |
| 1443 |
Self(self.0 * f32x4::splat(rhs)) |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
impl Mul<&f32> for Vec3A { |
| 1448 |
type Output = Self; |
| 1449 |
#[inline] |
| 1450 |
fn mul(self, rhs: &f32) -> Self { |
| 1451 |
self.mul(*rhs) |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
impl Mul<&f32> for &Vec3A { |
| 1456 |
type Output = Vec3A; |
| 1457 |
#[inline] |
| 1458 |
fn mul(self, rhs: &f32) -> Vec3A { |
| 1459 |
(*self).mul(*rhs) |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
impl Mul<f32> for &Vec3A { |
| 1464 |
type Output = Vec3A; |
| 1465 |
#[inline] |
| 1466 |
fn mul(self, rhs: f32) -> Vec3A { |
| 1467 |
(*self).mul(rhs) |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
impl MulAssign<f32> for Vec3A { |
| 1472 |
#[inline] |
| 1473 |
fn mul_assign(&mut self, rhs: f32) { |
| 1474 |
self.0 *= f32x4::splat(rhs); |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
impl MulAssign<&f32> for Vec3A { |
| 1479 |
#[inline] |
| 1480 |
fn mul_assign(&mut self, rhs: &f32) { |
| 1481 |
self.mul_assign(*rhs); |
| 1482 |
} |
| 1483 |
} |
| 1484 |
|
| 1485 |
impl Mul<Vec3A> for f32 { |
| 1486 |
type Output = Vec3A; |
| 1487 |
#[inline] |
| 1488 |
fn mul(self, rhs: Vec3A) -> Vec3A { |
| 1489 |
Vec3A(f32x4::splat(self) * rhs.0) |
| 1490 |
} |
| 1491 |
} |
| 1492 |
|
| 1493 |
impl Mul<&Vec3A> for f32 { |
| 1494 |
type Output = Vec3A; |
| 1495 |
#[inline] |
| 1496 |
fn mul(self, rhs: &Vec3A) -> Vec3A { |
| 1497 |
self.mul(*rhs) |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
impl Mul<&Vec3A> for &f32 { |
| 1502 |
type Output = Vec3A; |
| 1503 |
#[inline] |
| 1504 |
fn mul(self, rhs: &Vec3A) -> Vec3A { |
| 1505 |
(*self).mul(*rhs) |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
impl Mul<Vec3A> for &f32 { |
| 1510 |
type Output = Vec3A; |
| 1511 |
#[inline] |
| 1512 |
fn mul(self, rhs: Vec3A) -> Vec3A { |
| 1513 |
(*self).mul(rhs) |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
impl Add for Vec3A { |
| 1518 |
type Output = Self; |
| 1519 |
#[inline] |
| 1520 |
fn add(self, rhs: Self) -> Self { |
| 1521 |
Self(self.0 + rhs.0) |
| 1522 |
} |
| 1523 |
} |
| 1524 |
|
| 1525 |
impl Add<&Self> for Vec3A { |
| 1526 |
type Output = Self; |
| 1527 |
#[inline] |
| 1528 |
fn add(self, rhs: &Self) -> Self { |
| 1529 |
self.add(*rhs) |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
impl Add<&Vec3A> for &Vec3A { |
| 1534 |
type Output = Vec3A; |
| 1535 |
#[inline] |
| 1536 |
fn add(self, rhs: &Vec3A) -> Vec3A { |
| 1537 |
(*self).add(*rhs) |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
impl Add<Vec3A> for &Vec3A { |
| 1542 |
type Output = Vec3A; |
| 1543 |
#[inline] |
| 1544 |
fn add(self, rhs: Vec3A) -> Vec3A { |
| 1545 |
(*self).add(rhs) |
| 1546 |
} |
| 1547 |
} |
| 1548 |
|
| 1549 |
impl AddAssign for Vec3A { |
| 1550 |
#[inline] |
| 1551 |
fn add_assign(&mut self, rhs: Self) { |
| 1552 |
self.0 += rhs.0; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
|
| 1556 |
impl AddAssign<&Self> for Vec3A { |
| 1557 |
#[inline] |
| 1558 |
fn add_assign(&mut self, rhs: &Self) { |
| 1559 |
self.add_assign(*rhs); |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
impl Add<f32> for Vec3A { |
| 1564 |
type Output = Self; |
| 1565 |
#[inline] |
| 1566 |
fn add(self, rhs: f32) -> Self { |
| 1567 |
Self(self.0 + f32x4::splat(rhs)) |
| 1568 |
} |
| 1569 |
} |
| 1570 |
|
| 1571 |
impl Add<&f32> for Vec3A { |
| 1572 |
type Output = Self; |
| 1573 |
#[inline] |
| 1574 |
fn add(self, rhs: &f32) -> Self { |
| 1575 |
self.add(*rhs) |
| 1576 |
} |
| 1577 |
} |
| 1578 |
|
| 1579 |
impl Add<&f32> for &Vec3A { |
| 1580 |
type Output = Vec3A; |
| 1581 |
#[inline] |
| 1582 |
fn add(self, rhs: &f32) -> Vec3A { |
| 1583 |
(*self).add(*rhs) |
| 1584 |
} |
| 1585 |
} |
| 1586 |
|
| 1587 |
impl Add<f32> for &Vec3A { |
| 1588 |
type Output = Vec3A; |
| 1589 |
#[inline] |
| 1590 |
fn add(self, rhs: f32) -> Vec3A { |
| 1591 |
(*self).add(rhs) |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
impl AddAssign<f32> for Vec3A { |
| 1596 |
#[inline] |
| 1597 |
fn add_assign(&mut self, rhs: f32) { |
| 1598 |
self.0 += f32x4::splat(rhs); |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
impl AddAssign<&f32> for Vec3A { |
| 1603 |
#[inline] |
| 1604 |
fn add_assign(&mut self, rhs: &f32) { |
| 1605 |
self.add_assign(*rhs); |
| 1606 |
} |
| 1607 |
} |
| 1608 |
|
| 1609 |
impl Add<Vec3A> for f32 { |
| 1610 |
type Output = Vec3A; |
| 1611 |
#[inline] |
| 1612 |
fn add(self, rhs: Vec3A) -> Vec3A { |
| 1613 |
Vec3A(f32x4::splat(self) + rhs.0) |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
impl Add<&Vec3A> for f32 { |
| 1618 |
type Output = Vec3A; |
| 1619 |
#[inline] |
| 1620 |
fn add(self, rhs: &Vec3A) -> Vec3A { |
| 1621 |
self.add(*rhs) |
| 1622 |
} |
| 1623 |
} |
| 1624 |
|
| 1625 |
impl Add<&Vec3A> for &f32 { |
| 1626 |
type Output = Vec3A; |
| 1627 |
#[inline] |
| 1628 |
fn add(self, rhs: &Vec3A) -> Vec3A { |
| 1629 |
(*self).add(*rhs) |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
impl Add<Vec3A> for &f32 { |
| 1634 |
type Output = Vec3A; |
| 1635 |
#[inline] |
| 1636 |
fn add(self, rhs: Vec3A) -> Vec3A { |
| 1637 |
(*self).add(rhs) |
| 1638 |
} |
| 1639 |
} |
| 1640 |
|
| 1641 |
impl Sub for Vec3A { |
| 1642 |
type Output = Self; |
| 1643 |
#[inline] |
| 1644 |
fn sub(self, rhs: Self) -> Self { |
| 1645 |
Self(self.0 - rhs.0) |
| 1646 |
} |
| 1647 |
} |
| 1648 |
|
| 1649 |
impl Sub<&Self> for Vec3A { |
| 1650 |
type Output = Self; |
| 1651 |
#[inline] |
| 1652 |
fn sub(self, rhs: &Self) -> Self { |
| 1653 |
self.sub(*rhs) |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
impl Sub<&Vec3A> for &Vec3A { |
| 1658 |
type Output = Vec3A; |
| 1659 |
#[inline] |
| 1660 |
fn sub(self, rhs: &Vec3A) -> Vec3A { |
| 1661 |
(*self).sub(*rhs) |
| 1662 |
} |
| 1663 |
} |
| 1664 |
|
| 1665 |
impl Sub<Vec3A> for &Vec3A { |
| 1666 |
type Output = Vec3A; |
| 1667 |
#[inline] |
| 1668 |
fn sub(self, rhs: Vec3A) -> Vec3A { |
| 1669 |
(*self).sub(rhs) |
| 1670 |
} |
| 1671 |
} |
| 1672 |
|
| 1673 |
impl SubAssign for Vec3A { |
| 1674 |
#[inline] |
| 1675 |
fn sub_assign(&mut self, rhs: Self) { |
| 1676 |
self.0 -= rhs.0; |
| 1677 |
} |
| 1678 |
} |
| 1679 |
|
| 1680 |
impl SubAssign<&Self> for Vec3A { |
| 1681 |
#[inline] |
| 1682 |
fn sub_assign(&mut self, rhs: &Self) { |
| 1683 |
self.sub_assign(*rhs); |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
impl Sub<f32> for Vec3A { |
| 1688 |
type Output = Self; |
| 1689 |
#[inline] |
| 1690 |
fn sub(self, rhs: f32) -> Self { |
| 1691 |
Self(self.0 - f32x4::splat(rhs)) |
| 1692 |
} |
| 1693 |
} |
| 1694 |
|
| 1695 |
impl Sub<&f32> for Vec3A { |
| 1696 |
type Output = Self; |
| 1697 |
#[inline] |
| 1698 |
fn sub(self, rhs: &f32) -> Self { |
| 1699 |
self.sub(*rhs) |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
impl Sub<&f32> for &Vec3A { |
| 1704 |
type Output = Vec3A; |
| 1705 |
#[inline] |
| 1706 |
fn sub(self, rhs: &f32) -> Vec3A { |
| 1707 |
(*self).sub(*rhs) |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
impl Sub<f32> for &Vec3A { |
| 1712 |
type Output = Vec3A; |
| 1713 |
#[inline] |
| 1714 |
fn sub(self, rhs: f32) -> Vec3A { |
| 1715 |
(*self).sub(rhs) |
| 1716 |
} |
| 1717 |
} |
| 1718 |
|
| 1719 |
impl SubAssign<f32> for Vec3A { |
| 1720 |
#[inline] |
| 1721 |
fn sub_assign(&mut self, rhs: f32) { |
| 1722 |
self.0 -= f32x4::splat(rhs); |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
impl SubAssign<&f32> for Vec3A { |
| 1727 |
#[inline] |
| 1728 |
fn sub_assign(&mut self, rhs: &f32) { |
| 1729 |
self.sub_assign(*rhs); |
| 1730 |
} |
| 1731 |
} |
| 1732 |
|
| 1733 |
impl Sub<Vec3A> for f32 { |
| 1734 |
type Output = Vec3A; |
| 1735 |
#[inline] |
| 1736 |
fn sub(self, rhs: Vec3A) -> Vec3A { |
| 1737 |
Vec3A(f32x4::splat(self) - rhs.0) |
| 1738 |
} |
| 1739 |
} |
| 1740 |
|
| 1741 |
impl Sub<&Vec3A> for f32 { |
| 1742 |
type Output = Vec3A; |
| 1743 |
#[inline] |
| 1744 |
fn sub(self, rhs: &Vec3A) -> Vec3A { |
| 1745 |
self.sub(*rhs) |
| 1746 |
} |
| 1747 |
} |
| 1748 |
|
| 1749 |
impl Sub<&Vec3A> for &f32 { |
| 1750 |
type Output = Vec3A; |
| 1751 |
#[inline] |
| 1752 |
fn sub(self, rhs: &Vec3A) -> Vec3A { |
| 1753 |
(*self).sub(*rhs) |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
impl Sub<Vec3A> for &f32 { |
| 1758 |
type Output = Vec3A; |
| 1759 |
#[inline] |
| 1760 |
fn sub(self, rhs: Vec3A) -> Vec3A { |
| 1761 |
(*self).sub(rhs) |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
impl Rem for Vec3A { |
| 1766 |
type Output = Self; |
| 1767 |
#[inline] |
| 1768 |
fn rem(self, rhs: Self) -> Self { |
| 1769 |
Self(self.0 % rhs.0) |
| 1770 |
} |
| 1771 |
} |
| 1772 |
|
| 1773 |
impl Rem<&Self> for Vec3A { |
| 1774 |
type Output = Self; |
| 1775 |
#[inline] |
| 1776 |
fn rem(self, rhs: &Self) -> Self { |
| 1777 |
self.rem(*rhs) |
| 1778 |
} |
| 1779 |
} |
| 1780 |
|
| 1781 |
impl Rem<&Vec3A> for &Vec3A { |
| 1782 |
type Output = Vec3A; |
| 1783 |
#[inline] |
| 1784 |
fn rem(self, rhs: &Vec3A) -> Vec3A { |
| 1785 |
(*self).rem(*rhs) |
| 1786 |
} |
| 1787 |
} |
| 1788 |
|
| 1789 |
impl Rem<Vec3A> for &Vec3A { |
| 1790 |
type Output = Vec3A; |
| 1791 |
#[inline] |
| 1792 |
fn rem(self, rhs: Vec3A) -> Vec3A { |
| 1793 |
(*self).rem(rhs) |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
impl RemAssign for Vec3A { |
| 1798 |
#[inline] |
| 1799 |
fn rem_assign(&mut self, rhs: Self) { |
| 1800 |
self.0 %= rhs.0; |
| 1801 |
} |
| 1802 |
} |
| 1803 |
|
| 1804 |
impl RemAssign<&Self> for Vec3A { |
| 1805 |
#[inline] |
| 1806 |
fn rem_assign(&mut self, rhs: &Self) { |
| 1807 |
self.rem_assign(*rhs); |
| 1808 |
} |
| 1809 |
} |
| 1810 |
|
| 1811 |
impl Rem<f32> for Vec3A { |
| 1812 |
type Output = Self; |
| 1813 |
#[inline] |
| 1814 |
fn rem(self, rhs: f32) -> Self { |
| 1815 |
self.rem(Self::splat(rhs)) |
| 1816 |
} |
| 1817 |
} |
| 1818 |
|
| 1819 |
impl Rem<&f32> for Vec3A { |
| 1820 |
type Output = Self; |
| 1821 |
#[inline] |
| 1822 |
fn rem(self, rhs: &f32) -> Self { |
| 1823 |
self.rem(*rhs) |
| 1824 |
} |
| 1825 |
} |
| 1826 |
|
| 1827 |
impl Rem<&f32> for &Vec3A { |
| 1828 |
type Output = Vec3A; |
| 1829 |
#[inline] |
| 1830 |
fn rem(self, rhs: &f32) -> Vec3A { |
| 1831 |
(*self).rem(*rhs) |
| 1832 |
} |
| 1833 |
} |
| 1834 |
|
| 1835 |
impl Rem<f32> for &Vec3A { |
| 1836 |
type Output = Vec3A; |
| 1837 |
#[inline] |
| 1838 |
fn rem(self, rhs: f32) -> Vec3A { |
| 1839 |
(*self).rem(rhs) |
| 1840 |
} |
| 1841 |
} |
| 1842 |
|
| 1843 |
impl RemAssign<f32> for Vec3A { |
| 1844 |
#[inline] |
| 1845 |
fn rem_assign(&mut self, rhs: f32) { |
| 1846 |
self.0 %= f32x4::splat(rhs); |
| 1847 |
} |
| 1848 |
} |
| 1849 |
|
| 1850 |
impl RemAssign<&f32> for Vec3A { |
| 1851 |
#[inline] |
| 1852 |
fn rem_assign(&mut self, rhs: &f32) { |
| 1853 |
self.rem_assign(*rhs); |
| 1854 |
} |
| 1855 |
} |
| 1856 |
|
| 1857 |
impl Rem<Vec3A> for f32 { |
| 1858 |
type Output = Vec3A; |
| 1859 |
#[inline] |
| 1860 |
fn rem(self, rhs: Vec3A) -> Vec3A { |
| 1861 |
Vec3A::splat(self).rem(rhs) |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
impl Rem<&Vec3A> for f32 { |
| 1866 |
type Output = Vec3A; |
| 1867 |
#[inline] |
| 1868 |
fn rem(self, rhs: &Vec3A) -> Vec3A { |
| 1869 |
self.rem(*rhs) |
| 1870 |
} |
| 1871 |
} |
| 1872 |
|
| 1873 |
impl Rem<&Vec3A> for &f32 { |
| 1874 |
type Output = Vec3A; |
| 1875 |
#[inline] |
| 1876 |
fn rem(self, rhs: &Vec3A) -> Vec3A { |
| 1877 |
(*self).rem(*rhs) |
| 1878 |
} |
| 1879 |
} |
| 1880 |
|
| 1881 |
impl Rem<Vec3A> for &f32 { |
| 1882 |
type Output = Vec3A; |
| 1883 |
#[inline] |
| 1884 |
fn rem(self, rhs: Vec3A) -> Vec3A { |
| 1885 |
(*self).rem(rhs) |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
impl AsRef<[f32; 3]> for Vec3A { |
| 1890 |
#[inline] |
| 1891 |
fn as_ref(&self) -> &[f32; 3] { |
| 1892 |
unsafe { &*(self as *const Self as *const [f32; 3]) } |
| 1893 |
} |
| 1894 |
} |
| 1895 |
|
| 1896 |
impl AsMut<[f32; 3]> for Vec3A { |
| 1897 |
#[inline] |
| 1898 |
fn as_mut(&mut self) -> &mut [f32; 3] { |
| 1899 |
unsafe { &mut *(self as *mut Self as *mut [f32; 3]) } |
| 1900 |
} |
| 1901 |
} |
| 1902 |
|
| 1903 |
impl Sum for Vec3A { |
| 1904 |
#[inline] |
| 1905 |
fn sum<I>(iter: I) -> Self |
| 1906 |
where |
| 1907 |
I: Iterator<Item = Self>, |
| 1908 |
{ |
| 1909 |
iter.fold(Self::ZERO, Self::add) |
| 1910 |
} |
| 1911 |
} |
| 1912 |
|
| 1913 |
impl<'a> Sum<&'a Self> for Vec3A { |
| 1914 |
#[inline] |
| 1915 |
fn sum<I>(iter: I) -> Self |
| 1916 |
where |
| 1917 |
I: Iterator<Item = &'a Self>, |
| 1918 |
{ |
| 1919 |
iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
impl Product for Vec3A { |
| 1924 |
#[inline] |
| 1925 |
fn product<I>(iter: I) -> Self |
| 1926 |
where |
| 1927 |
I: Iterator<Item = Self>, |
| 1928 |
{ |
| 1929 |
iter.fold(Self::ONE, Self::mul) |
| 1930 |
} |
| 1931 |
} |
| 1932 |
|
| 1933 |
impl<'a> Product<&'a Self> for Vec3A { |
| 1934 |
#[inline] |
| 1935 |
fn product<I>(iter: I) -> Self |
| 1936 |
where |
| 1937 |
I: Iterator<Item = &'a Self>, |
| 1938 |
{ |
| 1939 |
iter.fold(Self::ONE, |a, &b| Self::mul(a, b)) |
| 1940 |
} |
| 1941 |
} |
| 1942 |
|
| 1943 |
impl Neg for Vec3A { |
| 1944 |
type Output = Self; |
| 1945 |
#[inline] |
| 1946 |
fn neg(self) -> Self { |
| 1947 |
Self(-self.0) |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
impl Neg for &Vec3A { |
| 1952 |
type Output = Vec3A; |
| 1953 |
#[inline] |
| 1954 |
fn neg(self) -> Vec3A { |
| 1955 |
(*self).neg() |
| 1956 |
} |
| 1957 |
} |
| 1958 |
|
| 1959 |
impl Index<usize> for Vec3A { |
| 1960 |
type Output = f32; |
| 1961 |
#[inline] |
| 1962 |
fn index(&self, index: usize) -> &Self::Output { |
| 1963 |
&self.0[index] |
| 1964 |
} |
| 1965 |
} |
| 1966 |
|
| 1967 |
impl IndexMut<usize> for Vec3A { |
| 1968 |
#[inline] |
| 1969 |
fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 1970 |
&mut self.0[index] |
| 1971 |
} |
| 1972 |
} |
| 1973 |
|
| 1974 |
impl fmt::Display for Vec3A { |
| 1975 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1976 |
if let Some(p) = f.precision() { |
| 1977 |
write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z) |
| 1978 |
} else { |
| 1979 |
write!(f, "[{}, {}, {}]", self.x, self.y, self.z) |
| 1980 |
} |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
impl fmt::Debug for Vec3A { |
| 1985 |
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1986 |
fmt.debug_tuple(stringify!(Vec3A)) |
| 1987 |
.field(&self.x) |
| 1988 |
.field(&self.y) |
| 1989 |
.field(&self.z) |
| 1990 |
.finish() |
| 1991 |
} |
| 1992 |
} |
| 1993 |
|
| 1994 |
impl From<Vec3A> for f32x4 { |
| 1995 |
#[inline(always)] |
| 1996 |
fn from(t: Vec3A) -> Self { |
| 1997 |
t.0 |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
impl From<f32x4> for Vec3A { |
| 2002 |
#[inline(always)] |
| 2003 |
fn from(t: f32x4) -> Self { |
| 2004 |
Self(t) |
| 2005 |
} |
| 2006 |
} |
| 2007 |
|
| 2008 |
impl From<[f32; 3]> for Vec3A { |
| 2009 |
#[inline] |
| 2010 |
fn from(a: [f32; 3]) -> Self { |
| 2011 |
Self::new(a[0], a[1], a[2]) |
| 2012 |
} |
| 2013 |
} |
| 2014 |
|
| 2015 |
impl From<Vec3A> for [f32; 3] { |
| 2016 |
#[inline] |
| 2017 |
fn from(v: Vec3A) -> Self { |
| 2018 |
unsafe { *(v.0.as_array().as_ptr() as *const Self) } |
| 2019 |
} |
| 2020 |
} |
| 2021 |
|
| 2022 |
impl From<(f32, f32, f32)> for Vec3A { |
| 2023 |
#[inline] |
| 2024 |
fn from(t: (f32, f32, f32)) -> Self { |
| 2025 |
Self::new(t.0, t.1, t.2) |
| 2026 |
} |
| 2027 |
} |
| 2028 |
|
| 2029 |
impl From<Vec3A> for (f32, f32, f32) { |
| 2030 |
#[inline] |
| 2031 |
fn from(v: Vec3A) -> Self { |
| 2032 |
(v.x, v.y, v.z) |
| 2033 |
} |
| 2034 |
} |
| 2035 |
|
| 2036 |
impl From<Vec3> for Vec3A { |
| 2037 |
#[inline] |
| 2038 |
fn from(v: Vec3) -> Self { |
| 2039 |
Self::new(v.x, v.y, v.z) |
| 2040 |
} |
| 2041 |
} |
| 2042 |
|
| 2043 |
impl From<Vec3A> for Vec3 { |
| 2044 |
#[inline] |
| 2045 |
fn from(v: Vec3A) -> Self { |
| 2046 |
unsafe { *(v.0.as_array().as_ptr() as *const Self) } |
| 2047 |
} |
| 2048 |
} |
| 2049 |
|
| 2050 |
impl From<(Vec2, f32)> for Vec3A { |
| 2051 |
#[inline] |
| 2052 |
fn from((v, z): (Vec2, f32)) -> Self { |
| 2053 |
Self::new(v.x, v.y, z) |
| 2054 |
} |
| 2055 |
} |
| 2056 |
|
| 2057 |
impl Deref for Vec3A { |
| 2058 |
type Target = crate::deref::Vec3<f32>; |
| 2059 |
#[inline] |
| 2060 |
fn deref(&self) -> &Self::Target { |
| 2061 |
unsafe { &*(self as *const Self).cast() } |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
impl DerefMut for Vec3A { |
| 2066 |
#[inline] |
| 2067 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 2068 |
unsafe { &mut *(self as *mut Self).cast() } |
| 2069 |
} |
| 2070 |
} |
| 2071 |
|
| 2072 |
impl From<BVec3> for Vec3A { |
| 2073 |
#[inline] |
| 2074 |
fn from(v: BVec3) -> Self { |
| 2075 |
Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z)) |
| 2076 |
} |
| 2077 |
} |
| 2078 |
|
| 2079 |
impl From<BVec3A> for Vec3A { |
| 2080 |
#[inline] |
| 2081 |
fn from(v: BVec3A) -> Self { |
| 2082 |
let bool_array: [bool; 3] = v.into(); |
| 2083 |
Self::new( |
| 2084 |
f32::from(bool_array[0]), |
| 2085 |
f32::from(bool_array[1]), |
| 2086 |
f32::from(bool_array[2]), |
| 2087 |
) |
| 2088 |
} |
| 2089 |
} |