| 1 | // Generated from vec.rs.tera template. Edit the template, not the generated file. |
| 2 | |
| 3 | #[cfg(not(feature = "scalar-math"))] |
| 4 | use crate::BVec4A; |
| 5 | use crate::{f64::math, BVec4, DVec2, DVec3, IVec4, UVec4, Vec4}; |
| 6 | |
| 7 | #[cfg(not(target_arch = "spirv"))] |
| 8 | use core::fmt; |
| 9 | use core::iter::{Product, Sum}; |
| 10 | use core::{f32, ops::*}; |
| 11 | |
| 12 | /// Creates a 4-dimensional vector. |
| 13 | #[inline(always)] |
| 14 | #[must_use] |
| 15 | pub const fn dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec4 { |
| 16 | DVec4::new(x, y, z, w) |
| 17 | } |
| 18 | |
| 19 | /// A 4-dimensional vector. |
| 20 | #[derive(Clone, Copy, PartialEq)] |
| 21 | #[cfg_attr(feature = "cuda", repr(align(16)))] |
| 22 | #[cfg_attr(not(target_arch = "spirv"), repr(C))] |
| 23 | #[cfg_attr(target_arch = "spirv", repr(simd))] |
| 24 | pub struct DVec4 { |
| 25 | pub x: f64, |
| 26 | pub y: f64, |
| 27 | pub z: f64, |
| 28 | pub w: f64, |
| 29 | } |
| 30 | |
| 31 | impl DVec4 { |
| 32 | /// All zeroes. |
| 33 | pub const ZERO: Self = Self::splat(0.0); |
| 34 | |
| 35 | /// All ones. |
| 36 | pub const ONE: Self = Self::splat(1.0); |
| 37 | |
| 38 | /// All negative ones. |
| 39 | pub const NEG_ONE: Self = Self::splat(-1.0); |
| 40 | |
| 41 | /// All `f64::MIN`. |
| 42 | pub const MIN: Self = Self::splat(f64::MIN); |
| 43 | |
| 44 | /// All `f64::MAX`. |
| 45 | pub const MAX: Self = Self::splat(f64::MAX); |
| 46 | |
| 47 | /// All `f64::NAN`. |
| 48 | pub const NAN: Self = Self::splat(f64::NAN); |
| 49 | |
| 50 | /// All `f64::INFINITY`. |
| 51 | pub const INFINITY: Self = Self::splat(f64::INFINITY); |
| 52 | |
| 53 | /// All `f64::NEG_INFINITY`. |
| 54 | pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY); |
| 55 | |
| 56 | /// A unit vector pointing along the positive X axis. |
| 57 | pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0); |
| 58 | |
| 59 | /// A unit vector pointing along the positive Y axis. |
| 60 | pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0); |
| 61 | |
| 62 | /// A unit vector pointing along the positive Z axis. |
| 63 | pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0); |
| 64 | |
| 65 | /// A unit vector pointing along the positive W axis. |
| 66 | pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0); |
| 67 | |
| 68 | /// A unit vector pointing along the negative X axis. |
| 69 | pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0); |
| 70 | |
| 71 | /// A unit vector pointing along the negative Y axis. |
| 72 | pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0); |
| 73 | |
| 74 | /// A unit vector pointing along the negative Z axis. |
| 75 | pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0); |
| 76 | |
| 77 | /// A unit vector pointing along the negative W axis. |
| 78 | pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0); |
| 79 | |
| 80 | /// The unit axes. |
| 81 | pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W]; |
| 82 | |
| 83 | /// Creates a new vector. |
| 84 | #[inline(always)] |
| 85 | #[must_use] |
| 86 | pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Self { |
| 87 | Self { x, y, z, w } |
| 88 | } |
| 89 | |
| 90 | /// Creates a vector with all elements set to `v`. |
| 91 | #[inline] |
| 92 | #[must_use] |
| 93 | pub const fn splat(v: f64) -> Self { |
| 94 | Self { |
| 95 | x: v, |
| 96 | |
| 97 | y: v, |
| 98 | |
| 99 | z: v, |
| 100 | |
| 101 | w: v, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use |
| 106 | /// for each element of `self`. |
| 107 | /// |
| 108 | /// A true element in the mask uses the corresponding element from `if_true`, and false |
| 109 | /// uses the element from `if_false`. |
| 110 | #[inline] |
| 111 | #[must_use] |
| 112 | pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self { |
| 113 | Self { |
| 114 | x: if mask.test(0) { if_true.x } else { if_false.x }, |
| 115 | y: if mask.test(1) { if_true.y } else { if_false.y }, |
| 116 | z: if mask.test(2) { if_true.z } else { if_false.z }, |
| 117 | w: if mask.test(3) { if_true.w } else { if_false.w }, |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// Creates a new vector from an array. |
| 122 | #[inline] |
| 123 | #[must_use] |
| 124 | pub const fn from_array(a: [f64; 4]) -> Self { |
| 125 | Self::new(a[0], a[1], a[2], a[3]) |
| 126 | } |
| 127 | |
| 128 | /// `[x, y, z, w]` |
| 129 | #[inline] |
| 130 | #[must_use] |
| 131 | pub const fn to_array(&self) -> [f64; 4] { |
| 132 | [self.x, self.y, self.z, self.w] |
| 133 | } |
| 134 | |
| 135 | /// Creates a vector from the first 4 values in `slice`. |
| 136 | /// |
| 137 | /// # Panics |
| 138 | /// |
| 139 | /// Panics if `slice` is less than 4 elements long. |
| 140 | #[inline] |
| 141 | #[must_use] |
| 142 | pub const fn from_slice(slice: &[f64]) -> Self { |
| 143 | Self::new(slice[0], slice[1], slice[2], slice[3]) |
| 144 | } |
| 145 | |
| 146 | /// Writes the elements of `self` to the first 4 elements in `slice`. |
| 147 | /// |
| 148 | /// # Panics |
| 149 | /// |
| 150 | /// Panics if `slice` is less than 4 elements long. |
| 151 | #[inline] |
| 152 | pub fn write_to_slice(self, slice: &mut [f64]) { |
| 153 | slice[0] = self.x; |
| 154 | slice[1] = self.y; |
| 155 | slice[2] = self.z; |
| 156 | slice[3] = self.w; |
| 157 | } |
| 158 | |
| 159 | /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. |
| 160 | /// |
| 161 | /// Truncation to [`DVec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()]. |
| 162 | #[inline] |
| 163 | #[must_use] |
| 164 | pub fn truncate(self) -> DVec3 { |
| 165 | use crate::swizzles::Vec4Swizzles; |
| 166 | self.xyz() |
| 167 | } |
| 168 | |
| 169 | /// Creates a 4D vector from `self` with the given value of `x`. |
| 170 | #[inline] |
| 171 | #[must_use] |
| 172 | pub fn with_x(mut self, x: f64) -> Self { |
| 173 | self.x = x; |
| 174 | self |
| 175 | } |
| 176 | |
| 177 | /// Creates a 4D vector from `self` with the given value of `y`. |
| 178 | #[inline] |
| 179 | #[must_use] |
| 180 | pub fn with_y(mut self, y: f64) -> Self { |
| 181 | self.y = y; |
| 182 | self |
| 183 | } |
| 184 | |
| 185 | /// Creates a 4D vector from `self` with the given value of `z`. |
| 186 | #[inline] |
| 187 | #[must_use] |
| 188 | pub fn with_z(mut self, z: f64) -> Self { |
| 189 | self.z = z; |
| 190 | self |
| 191 | } |
| 192 | |
| 193 | /// Creates a 4D vector from `self` with the given value of `w`. |
| 194 | #[inline] |
| 195 | #[must_use] |
| 196 | pub fn with_w(mut self, w: f64) -> Self { |
| 197 | self.w = w; |
| 198 | self |
| 199 | } |
| 200 | |
| 201 | /// Computes the dot product of `self` and `rhs`. |
| 202 | #[inline] |
| 203 | #[must_use] |
| 204 | pub fn dot(self, rhs: Self) -> f64 { |
| 205 | (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w) |
| 206 | } |
| 207 | |
| 208 | /// Returns a vector where every component is the dot product of `self` and `rhs`. |
| 209 | #[inline] |
| 210 | #[must_use] |
| 211 | pub fn dot_into_vec(self, rhs: Self) -> Self { |
| 212 | Self::splat(self.dot(rhs)) |
| 213 | } |
| 214 | |
| 215 | /// Returns a vector containing the minimum values for each element of `self` and `rhs`. |
| 216 | /// |
| 217 | /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`. |
| 218 | #[inline] |
| 219 | #[must_use] |
| 220 | pub fn min(self, rhs: Self) -> Self { |
| 221 | Self { |
| 222 | x: self.x.min(rhs.x), |
| 223 | y: self.y.min(rhs.y), |
| 224 | z: self.z.min(rhs.z), |
| 225 | w: self.w.min(rhs.w), |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /// Returns a vector containing the maximum values for each element of `self` and `rhs`. |
| 230 | /// |
| 231 | /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`. |
| 232 | #[inline] |
| 233 | #[must_use] |
| 234 | pub fn max(self, rhs: Self) -> Self { |
| 235 | Self { |
| 236 | x: self.x.max(rhs.x), |
| 237 | y: self.y.max(rhs.y), |
| 238 | z: self.z.max(rhs.z), |
| 239 | w: self.w.max(rhs.w), |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /// Component-wise clamping of values, similar to [`f64::clamp`]. |
| 244 | /// |
| 245 | /// Each element in `min` must be less-or-equal to the corresponding element in `max`. |
| 246 | /// |
| 247 | /// # Panics |
| 248 | /// |
| 249 | /// Will panic if `min` is greater than `max` when `glam_assert` is enabled. |
| 250 | #[inline] |
| 251 | #[must_use] |
| 252 | pub fn clamp(self, min: Self, max: Self) -> Self { |
| 253 | glam_assert!(min.cmple(max).all(), "clamp: expected min <= max"); |
| 254 | self.max(min).min(max) |
| 255 | } |
| 256 | |
| 257 | /// Returns the horizontal minimum of `self`. |
| 258 | /// |
| 259 | /// In other words this computes `min(x, y, ..)`. |
| 260 | #[inline] |
| 261 | #[must_use] |
| 262 | pub fn min_element(self) -> f64 { |
| 263 | self.x.min(self.y.min(self.z.min(self.w))) |
| 264 | } |
| 265 | |
| 266 | /// Returns the horizontal maximum of `self`. |
| 267 | /// |
| 268 | /// In other words this computes `max(x, y, ..)`. |
| 269 | #[inline] |
| 270 | #[must_use] |
| 271 | pub fn max_element(self) -> f64 { |
| 272 | self.x.max(self.y.max(self.z.max(self.w))) |
| 273 | } |
| 274 | |
| 275 | /// Returns the sum of all elements of `self`. |
| 276 | /// |
| 277 | /// In other words, this computes `self.x + self.y + ..`. |
| 278 | #[inline] |
| 279 | #[must_use] |
| 280 | pub fn element_sum(self) -> f64 { |
| 281 | self.x + self.y + self.z + self.w |
| 282 | } |
| 283 | |
| 284 | /// Returns the product of all elements of `self`. |
| 285 | /// |
| 286 | /// In other words, this computes `self.x * self.y * ..`. |
| 287 | #[inline] |
| 288 | #[must_use] |
| 289 | pub fn element_product(self) -> f64 { |
| 290 | self.x * self.y * self.z * self.w |
| 291 | } |
| 292 | |
| 293 | /// Returns a vector mask containing the result of a `==` comparison for each element of |
| 294 | /// `self` and `rhs`. |
| 295 | /// |
| 296 | /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all |
| 297 | /// elements. |
| 298 | #[inline] |
| 299 | #[must_use] |
| 300 | pub fn cmpeq(self, rhs: Self) -> BVec4 { |
| 301 | BVec4::new( |
| 302 | self.x.eq(&rhs.x), |
| 303 | self.y.eq(&rhs.y), |
| 304 | self.z.eq(&rhs.z), |
| 305 | self.w.eq(&rhs.w), |
| 306 | ) |
| 307 | } |
| 308 | |
| 309 | /// Returns a vector mask containing the result of a `!=` comparison for each element of |
| 310 | /// `self` and `rhs`. |
| 311 | /// |
| 312 | /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all |
| 313 | /// elements. |
| 314 | #[inline] |
| 315 | #[must_use] |
| 316 | pub fn cmpne(self, rhs: Self) -> BVec4 { |
| 317 | BVec4::new( |
| 318 | self.x.ne(&rhs.x), |
| 319 | self.y.ne(&rhs.y), |
| 320 | self.z.ne(&rhs.z), |
| 321 | self.w.ne(&rhs.w), |
| 322 | ) |
| 323 | } |
| 324 | |
| 325 | /// Returns a vector mask containing the result of a `>=` comparison for each element of |
| 326 | /// `self` and `rhs`. |
| 327 | /// |
| 328 | /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all |
| 329 | /// elements. |
| 330 | #[inline] |
| 331 | #[must_use] |
| 332 | pub fn cmpge(self, rhs: Self) -> BVec4 { |
| 333 | BVec4::new( |
| 334 | self.x.ge(&rhs.x), |
| 335 | self.y.ge(&rhs.y), |
| 336 | self.z.ge(&rhs.z), |
| 337 | self.w.ge(&rhs.w), |
| 338 | ) |
| 339 | } |
| 340 | |
| 341 | /// Returns a vector mask containing the result of a `>` comparison for each element of |
| 342 | /// `self` and `rhs`. |
| 343 | /// |
| 344 | /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all |
| 345 | /// elements. |
| 346 | #[inline] |
| 347 | #[must_use] |
| 348 | pub fn cmpgt(self, rhs: Self) -> BVec4 { |
| 349 | BVec4::new( |
| 350 | self.x.gt(&rhs.x), |
| 351 | self.y.gt(&rhs.y), |
| 352 | self.z.gt(&rhs.z), |
| 353 | self.w.gt(&rhs.w), |
| 354 | ) |
| 355 | } |
| 356 | |
| 357 | /// Returns a vector mask containing the result of a `<=` comparison for each element of |
| 358 | /// `self` and `rhs`. |
| 359 | /// |
| 360 | /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all |
| 361 | /// elements. |
| 362 | #[inline] |
| 363 | #[must_use] |
| 364 | pub fn cmple(self, rhs: Self) -> BVec4 { |
| 365 | BVec4::new( |
| 366 | self.x.le(&rhs.x), |
| 367 | self.y.le(&rhs.y), |
| 368 | self.z.le(&rhs.z), |
| 369 | self.w.le(&rhs.w), |
| 370 | ) |
| 371 | } |
| 372 | |
| 373 | /// Returns a vector mask containing the result of a `<` comparison for each element of |
| 374 | /// `self` and `rhs`. |
| 375 | /// |
| 376 | /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all |
| 377 | /// elements. |
| 378 | #[inline] |
| 379 | #[must_use] |
| 380 | pub fn cmplt(self, rhs: Self) -> BVec4 { |
| 381 | BVec4::new( |
| 382 | self.x.lt(&rhs.x), |
| 383 | self.y.lt(&rhs.y), |
| 384 | self.z.lt(&rhs.z), |
| 385 | self.w.lt(&rhs.w), |
| 386 | ) |
| 387 | } |
| 388 | |
| 389 | /// Returns a vector containing the absolute value of each element of `self`. |
| 390 | #[inline] |
| 391 | #[must_use] |
| 392 | pub fn abs(self) -> Self { |
| 393 | Self { |
| 394 | x: math::abs(self.x), |
| 395 | y: math::abs(self.y), |
| 396 | z: math::abs(self.z), |
| 397 | w: math::abs(self.w), |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /// Returns a vector with elements representing the sign of `self`. |
| 402 | /// |
| 403 | /// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 404 | /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 405 | /// - `NAN` if the number is `NAN` |
| 406 | #[inline] |
| 407 | #[must_use] |
| 408 | pub fn signum(self) -> Self { |
| 409 | Self { |
| 410 | x: math::signum(self.x), |
| 411 | y: math::signum(self.y), |
| 412 | z: math::signum(self.z), |
| 413 | w: math::signum(self.w), |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /// Returns a vector with signs of `rhs` and the magnitudes of `self`. |
| 418 | #[inline] |
| 419 | #[must_use] |
| 420 | pub fn copysign(self, rhs: Self) -> Self { |
| 421 | Self { |
| 422 | x: math::copysign(self.x, rhs.x), |
| 423 | y: math::copysign(self.y, rhs.y), |
| 424 | z: math::copysign(self.z, rhs.z), |
| 425 | w: math::copysign(self.w, rhs.w), |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`. |
| 430 | /// |
| 431 | /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes |
| 432 | /// into the first lowest bit, element `y` into the second, etc. |
| 433 | #[inline] |
| 434 | #[must_use] |
| 435 | pub fn is_negative_bitmask(self) -> u32 { |
| 436 | (self.x.is_sign_negative() as u32) |
| 437 | | (self.y.is_sign_negative() as u32) << 1 |
| 438 | | (self.z.is_sign_negative() as u32) << 2 |
| 439 | | (self.w.is_sign_negative() as u32) << 3 |
| 440 | } |
| 441 | |
| 442 | /// Returns `true` if, and only if, all elements are finite. If any element is either |
| 443 | /// `NaN`, positive or negative infinity, this will return `false`. |
| 444 | #[inline] |
| 445 | #[must_use] |
| 446 | pub fn is_finite(self) -> bool { |
| 447 | self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite() |
| 448 | } |
| 449 | |
| 450 | /// Returns `true` if any elements are `NaN`. |
| 451 | #[inline] |
| 452 | #[must_use] |
| 453 | pub fn is_nan(self) -> bool { |
| 454 | self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan() |
| 455 | } |
| 456 | |
| 457 | /// Performs `is_nan` on each element of self, returning a vector mask of the results. |
| 458 | /// |
| 459 | /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`. |
| 460 | #[inline] |
| 461 | #[must_use] |
| 462 | pub fn is_nan_mask(self) -> BVec4 { |
| 463 | BVec4::new( |
| 464 | self.x.is_nan(), |
| 465 | self.y.is_nan(), |
| 466 | self.z.is_nan(), |
| 467 | self.w.is_nan(), |
| 468 | ) |
| 469 | } |
| 470 | |
| 471 | /// Computes the length of `self`. |
| 472 | #[doc(alias = "magnitude")] |
| 473 | #[inline] |
| 474 | #[must_use] |
| 475 | pub fn length(self) -> f64 { |
| 476 | math::sqrt(self.dot(self)) |
| 477 | } |
| 478 | |
| 479 | /// Computes the squared length of `self`. |
| 480 | /// |
| 481 | /// This is faster than `length()` as it avoids a square root operation. |
| 482 | #[doc(alias = "magnitude2")] |
| 483 | #[inline] |
| 484 | #[must_use] |
| 485 | pub fn length_squared(self) -> f64 { |
| 486 | self.dot(self) |
| 487 | } |
| 488 | |
| 489 | /// Computes `1.0 / length()`. |
| 490 | /// |
| 491 | /// For valid results, `self` must _not_ be of length zero. |
| 492 | #[inline] |
| 493 | #[must_use] |
| 494 | pub fn length_recip(self) -> f64 { |
| 495 | self.length().recip() |
| 496 | } |
| 497 | |
| 498 | /// Computes the Euclidean distance between two points in space. |
| 499 | #[inline] |
| 500 | #[must_use] |
| 501 | pub fn distance(self, rhs: Self) -> f64 { |
| 502 | (self - rhs).length() |
| 503 | } |
| 504 | |
| 505 | /// Compute the squared euclidean distance between two points in space. |
| 506 | #[inline] |
| 507 | #[must_use] |
| 508 | pub fn distance_squared(self, rhs: Self) -> f64 { |
| 509 | (self - rhs).length_squared() |
| 510 | } |
| 511 | |
| 512 | /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`. |
| 513 | #[inline] |
| 514 | #[must_use] |
| 515 | pub fn div_euclid(self, rhs: Self) -> Self { |
| 516 | Self::new( |
| 517 | math::div_euclid(self.x, rhs.x), |
| 518 | math::div_euclid(self.y, rhs.y), |
| 519 | math::div_euclid(self.z, rhs.z), |
| 520 | math::div_euclid(self.w, rhs.w), |
| 521 | ) |
| 522 | } |
| 523 | |
| 524 | /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`. |
| 525 | /// |
| 526 | /// [Euclidean division]: f64::rem_euclid |
| 527 | #[inline] |
| 528 | #[must_use] |
| 529 | pub fn rem_euclid(self, rhs: Self) -> Self { |
| 530 | Self::new( |
| 531 | math::rem_euclid(self.x, rhs.x), |
| 532 | math::rem_euclid(self.y, rhs.y), |
| 533 | math::rem_euclid(self.z, rhs.z), |
| 534 | math::rem_euclid(self.w, rhs.w), |
| 535 | ) |
| 536 | } |
| 537 | |
| 538 | /// Returns `self` normalized to length 1.0. |
| 539 | /// |
| 540 | /// For valid results, `self` must _not_ be of length zero, nor very close to zero. |
| 541 | /// |
| 542 | /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`]. |
| 543 | /// |
| 544 | /// Panics |
| 545 | /// |
| 546 | /// Will panic if `self` is zero length when `glam_assert` is enabled. |
| 547 | #[inline] |
| 548 | #[must_use] |
| 549 | pub fn normalize(self) -> Self { |
| 550 | #[allow(clippy::let_and_return)] |
| 551 | let normalized = self.mul(self.length_recip()); |
| 552 | glam_assert!(normalized.is_finite()); |
| 553 | normalized |
| 554 | } |
| 555 | |
| 556 | /// Returns `self` normalized to length 1.0 if possible, else returns `None`. |
| 557 | /// |
| 558 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 559 | /// the result of this operation will be `None`. |
| 560 | /// |
| 561 | /// See also [`Self::normalize_or_zero()`]. |
| 562 | #[inline] |
| 563 | #[must_use] |
| 564 | pub fn try_normalize(self) -> Option<Self> { |
| 565 | let rcp = self.length_recip(); |
| 566 | if rcp.is_finite() && rcp > 0.0 { |
| 567 | Some(self * rcp) |
| 568 | } else { |
| 569 | None |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /// Returns `self` normalized to length 1.0 if possible, else returns a |
| 574 | /// fallback value. |
| 575 | /// |
| 576 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 577 | /// the result of this operation will be the fallback value. |
| 578 | /// |
| 579 | /// See also [`Self::try_normalize()`]. |
| 580 | #[inline] |
| 581 | #[must_use] |
| 582 | pub fn normalize_or(self, fallback: Self) -> Self { |
| 583 | let rcp = self.length_recip(); |
| 584 | if rcp.is_finite() && rcp > 0.0 { |
| 585 | self * rcp |
| 586 | } else { |
| 587 | fallback |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /// Returns `self` normalized to length 1.0 if possible, else returns zero. |
| 592 | /// |
| 593 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 594 | /// the result of this operation will be zero. |
| 595 | /// |
| 596 | /// See also [`Self::try_normalize()`]. |
| 597 | #[inline] |
| 598 | #[must_use] |
| 599 | pub fn normalize_or_zero(self) -> Self { |
| 600 | self.normalize_or(Self::ZERO) |
| 601 | } |
| 602 | |
| 603 | /// Returns whether `self` is length `1.0` or not. |
| 604 | /// |
| 605 | /// Uses a precision threshold of approximately `1e-4`. |
| 606 | #[inline] |
| 607 | #[must_use] |
| 608 | pub fn is_normalized(self) -> bool { |
| 609 | math::abs(self.length_squared() - 1.0) <= 2e-4 |
| 610 | } |
| 611 | |
| 612 | /// Returns the vector projection of `self` onto `rhs`. |
| 613 | /// |
| 614 | /// `rhs` must be of non-zero length. |
| 615 | /// |
| 616 | /// # Panics |
| 617 | /// |
| 618 | /// Will panic if `rhs` is zero length when `glam_assert` is enabled. |
| 619 | #[inline] |
| 620 | #[must_use] |
| 621 | pub fn project_onto(self, rhs: Self) -> Self { |
| 622 | let other_len_sq_rcp = rhs.dot(rhs).recip(); |
| 623 | glam_assert!(other_len_sq_rcp.is_finite()); |
| 624 | rhs * self.dot(rhs) * other_len_sq_rcp |
| 625 | } |
| 626 | |
| 627 | /// Returns the vector rejection of `self` from `rhs`. |
| 628 | /// |
| 629 | /// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 630 | /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 631 | /// |
| 632 | /// `rhs` must be of non-zero length. |
| 633 | /// |
| 634 | /// # Panics |
| 635 | /// |
| 636 | /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled. |
| 637 | #[inline] |
| 638 | #[must_use] |
| 639 | pub fn reject_from(self, rhs: Self) -> Self { |
| 640 | self - self.project_onto(rhs) |
| 641 | } |
| 642 | |
| 643 | /// Returns the vector projection of `self` onto `rhs`. |
| 644 | /// |
| 645 | /// `rhs` must be normalized. |
| 646 | /// |
| 647 | /// # Panics |
| 648 | /// |
| 649 | /// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 650 | #[inline] |
| 651 | #[must_use] |
| 652 | pub fn project_onto_normalized(self, rhs: Self) -> Self { |
| 653 | glam_assert!(rhs.is_normalized()); |
| 654 | rhs * self.dot(rhs) |
| 655 | } |
| 656 | |
| 657 | /// Returns the vector rejection of `self` from `rhs`. |
| 658 | /// |
| 659 | /// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 660 | /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 661 | /// |
| 662 | /// `rhs` must be normalized. |
| 663 | /// |
| 664 | /// # Panics |
| 665 | /// |
| 666 | /// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 667 | #[inline] |
| 668 | #[must_use] |
| 669 | pub fn reject_from_normalized(self, rhs: Self) -> Self { |
| 670 | self - self.project_onto_normalized(rhs) |
| 671 | } |
| 672 | |
| 673 | /// Returns a vector containing the nearest integer to a number for each element of `self`. |
| 674 | /// Round half-way cases away from 0.0. |
| 675 | #[inline] |
| 676 | #[must_use] |
| 677 | pub fn round(self) -> Self { |
| 678 | Self { |
| 679 | x: math::round(self.x), |
| 680 | y: math::round(self.y), |
| 681 | z: math::round(self.z), |
| 682 | w: math::round(self.w), |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /// Returns a vector containing the largest integer less than or equal to a number for each |
| 687 | /// element of `self`. |
| 688 | #[inline] |
| 689 | #[must_use] |
| 690 | pub fn floor(self) -> Self { |
| 691 | Self { |
| 692 | x: math::floor(self.x), |
| 693 | y: math::floor(self.y), |
| 694 | z: math::floor(self.z), |
| 695 | w: math::floor(self.w), |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /// Returns a vector containing the smallest integer greater than or equal to a number for |
| 700 | /// each element of `self`. |
| 701 | #[inline] |
| 702 | #[must_use] |
| 703 | pub fn ceil(self) -> Self { |
| 704 | Self { |
| 705 | x: math::ceil(self.x), |
| 706 | y: math::ceil(self.y), |
| 707 | z: math::ceil(self.z), |
| 708 | w: math::ceil(self.w), |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /// Returns a vector containing the integer part each element of `self`. This means numbers are |
| 713 | /// always truncated towards zero. |
| 714 | #[inline] |
| 715 | #[must_use] |
| 716 | pub fn trunc(self) -> Self { |
| 717 | Self { |
| 718 | x: math::trunc(self.x), |
| 719 | y: math::trunc(self.y), |
| 720 | z: math::trunc(self.z), |
| 721 | w: math::trunc(self.w), |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`. |
| 726 | /// |
| 727 | /// Note that this differs from the GLSL implementation of `fract` which returns |
| 728 | /// `self - self.floor()`. |
| 729 | /// |
| 730 | /// Note that this is fast but not precise for large numbers. |
| 731 | #[inline] |
| 732 | #[must_use] |
| 733 | pub fn fract(self) -> Self { |
| 734 | self - self.trunc() |
| 735 | } |
| 736 | |
| 737 | /// Returns a vector containing the fractional part of the vector as `self - self.floor()`. |
| 738 | /// |
| 739 | /// Note that this differs from the Rust implementation of `fract` which returns |
| 740 | /// `self - self.trunc()`. |
| 741 | /// |
| 742 | /// Note that this is fast but not precise for large numbers. |
| 743 | #[inline] |
| 744 | #[must_use] |
| 745 | pub fn fract_gl(self) -> Self { |
| 746 | self - self.floor() |
| 747 | } |
| 748 | |
| 749 | /// Returns a vector containing `e^self` (the exponential function) for each element of |
| 750 | /// `self`. |
| 751 | #[inline] |
| 752 | #[must_use] |
| 753 | pub fn exp(self) -> Self { |
| 754 | Self::new( |
| 755 | math::exp(self.x), |
| 756 | math::exp(self.y), |
| 757 | math::exp(self.z), |
| 758 | math::exp(self.w), |
| 759 | ) |
| 760 | } |
| 761 | |
| 762 | /// Returns a vector containing each element of `self` raised to the power of `n`. |
| 763 | #[inline] |
| 764 | #[must_use] |
| 765 | pub fn powf(self, n: f64) -> Self { |
| 766 | Self::new( |
| 767 | math::powf(self.x, n), |
| 768 | math::powf(self.y, n), |
| 769 | math::powf(self.z, n), |
| 770 | math::powf(self.w, n), |
| 771 | ) |
| 772 | } |
| 773 | |
| 774 | /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`. |
| 775 | #[inline] |
| 776 | #[must_use] |
| 777 | pub fn recip(self) -> Self { |
| 778 | Self { |
| 779 | x: 1.0 / self.x, |
| 780 | y: 1.0 / self.y, |
| 781 | z: 1.0 / self.z, |
| 782 | w: 1.0 / self.w, |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | /// Performs a linear interpolation between `self` and `rhs` based on the value `s`. |
| 787 | /// |
| 788 | /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result |
| 789 | /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly |
| 790 | /// extrapolated. |
| 791 | #[doc(alias = "mix")] |
| 792 | #[inline] |
| 793 | #[must_use] |
| 794 | pub fn lerp(self, rhs: Self, s: f64) -> Self { |
| 795 | self + ((rhs - self) * s) |
| 796 | } |
| 797 | |
| 798 | /// Moves towards `rhs` based on the value `d`. |
| 799 | /// |
| 800 | /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to |
| 801 | /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`. |
| 802 | #[inline] |
| 803 | #[must_use] |
| 804 | pub fn move_towards(&self, rhs: Self, d: f64) -> Self { |
| 805 | let a = rhs - *self; |
| 806 | let len = a.length(); |
| 807 | if len <= d || len <= 1e-4 { |
| 808 | return rhs; |
| 809 | } |
| 810 | *self + a / len * d |
| 811 | } |
| 812 | |
| 813 | /// Calculates the midpoint between `self` and `rhs`. |
| 814 | /// |
| 815 | /// The midpoint is the average of, or halfway point between, two vectors. |
| 816 | /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)` |
| 817 | /// while being slightly cheaper to compute. |
| 818 | #[inline] |
| 819 | pub fn midpoint(self, rhs: Self) -> Self { |
| 820 | (self + rhs) * 0.5 |
| 821 | } |
| 822 | |
| 823 | /// Returns true if the absolute difference of all elements between `self` and `rhs` is |
| 824 | /// less than or equal to `max_abs_diff`. |
| 825 | /// |
| 826 | /// This can be used to compare if two vectors contain similar elements. It works best when |
| 827 | /// comparing with a known value. The `max_abs_diff` that should be used used depends on |
| 828 | /// the values being compared against. |
| 829 | /// |
| 830 | /// For more see |
| 831 | /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 832 | #[inline] |
| 833 | #[must_use] |
| 834 | pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool { |
| 835 | self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all() |
| 836 | } |
| 837 | |
| 838 | /// Returns a vector with a length no less than `min` and no more than `max`. |
| 839 | /// |
| 840 | /// # Panics |
| 841 | /// |
| 842 | /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled. |
| 843 | #[inline] |
| 844 | #[must_use] |
| 845 | pub fn clamp_length(self, min: f64, max: f64) -> Self { |
| 846 | glam_assert!(0.0 <= min); |
| 847 | glam_assert!(min <= max); |
| 848 | let length_sq = self.length_squared(); |
| 849 | if length_sq < min * min { |
| 850 | min * (self / math::sqrt(length_sq)) |
| 851 | } else if length_sq > max * max { |
| 852 | max * (self / math::sqrt(length_sq)) |
| 853 | } else { |
| 854 | self |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | /// Returns a vector with a length no more than `max`. |
| 859 | /// |
| 860 | /// # Panics |
| 861 | /// |
| 862 | /// Will panic if `max` is negative when `glam_assert` is enabled. |
| 863 | #[inline] |
| 864 | #[must_use] |
| 865 | pub fn clamp_length_max(self, max: f64) -> Self { |
| 866 | glam_assert!(0.0 <= max); |
| 867 | let length_sq = self.length_squared(); |
| 868 | if length_sq > max * max { |
| 869 | max * (self / math::sqrt(length_sq)) |
| 870 | } else { |
| 871 | self |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /// Returns a vector with a length no less than `min`. |
| 876 | /// |
| 877 | /// # Panics |
| 878 | /// |
| 879 | /// Will panic if `min` is negative when `glam_assert` is enabled. |
| 880 | #[inline] |
| 881 | #[must_use] |
| 882 | pub fn clamp_length_min(self, min: f64) -> Self { |
| 883 | glam_assert!(0.0 <= min); |
| 884 | let length_sq = self.length_squared(); |
| 885 | if length_sq < min * min { |
| 886 | min * (self / math::sqrt(length_sq)) |
| 887 | } else { |
| 888 | self |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding |
| 893 | /// error, yielding a more accurate result than an unfused multiply-add. |
| 894 | /// |
| 895 | /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target |
| 896 | /// architecture has a dedicated fma CPU instruction. However, this is not always true, |
| 897 | /// and will be heavily dependant on designing algorithms with specific target hardware in |
| 898 | /// mind. |
| 899 | #[inline] |
| 900 | #[must_use] |
| 901 | pub fn mul_add(self, a: Self, b: Self) -> Self { |
| 902 | Self::new( |
| 903 | math::mul_add(self.x, a.x, b.x), |
| 904 | math::mul_add(self.y, a.y, b.y), |
| 905 | math::mul_add(self.z, a.z, b.z), |
| 906 | math::mul_add(self.w, a.w, b.w), |
| 907 | ) |
| 908 | } |
| 909 | |
| 910 | /// Casts all elements of `self` to `f32`. |
| 911 | #[inline] |
| 912 | #[must_use] |
| 913 | pub fn as_vec4(&self) -> crate::Vec4 { |
| 914 | crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32) |
| 915 | } |
| 916 | |
| 917 | /// Casts all elements of `self` to `i16`. |
| 918 | #[inline] |
| 919 | #[must_use] |
| 920 | pub fn as_i16vec4(&self) -> crate::I16Vec4 { |
| 921 | crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16) |
| 922 | } |
| 923 | |
| 924 | /// Casts all elements of `self` to `u16`. |
| 925 | #[inline] |
| 926 | #[must_use] |
| 927 | pub fn as_u16vec4(&self) -> crate::U16Vec4 { |
| 928 | crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16) |
| 929 | } |
| 930 | |
| 931 | /// Casts all elements of `self` to `i32`. |
| 932 | #[inline] |
| 933 | #[must_use] |
| 934 | pub fn as_ivec4(&self) -> crate::IVec4 { |
| 935 | crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32) |
| 936 | } |
| 937 | |
| 938 | /// Casts all elements of `self` to `u32`. |
| 939 | #[inline] |
| 940 | #[must_use] |
| 941 | pub fn as_uvec4(&self) -> crate::UVec4 { |
| 942 | crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32) |
| 943 | } |
| 944 | |
| 945 | /// Casts all elements of `self` to `i64`. |
| 946 | #[inline] |
| 947 | #[must_use] |
| 948 | pub fn as_i64vec4(&self) -> crate::I64Vec4 { |
| 949 | crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64) |
| 950 | } |
| 951 | |
| 952 | /// Casts all elements of `self` to `u64`. |
| 953 | #[inline] |
| 954 | #[must_use] |
| 955 | pub fn as_u64vec4(&self) -> crate::U64Vec4 { |
| 956 | crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64) |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | impl Default for DVec4 { |
| 961 | #[inline(always)] |
| 962 | fn default() -> Self { |
| 963 | Self::ZERO |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | impl Div<DVec4> for DVec4 { |
| 968 | type Output = Self; |
| 969 | #[inline] |
| 970 | fn div(self, rhs: Self) -> Self { |
| 971 | Self { |
| 972 | x: self.x.div(rhs.x), |
| 973 | y: self.y.div(rhs.y), |
| 974 | z: self.z.div(rhs.z), |
| 975 | w: self.w.div(rhs.w), |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | impl DivAssign<DVec4> for DVec4 { |
| 981 | #[inline] |
| 982 | fn div_assign(&mut self, rhs: Self) { |
| 983 | self.x.div_assign(rhs.x); |
| 984 | self.y.div_assign(rhs.y); |
| 985 | self.z.div_assign(rhs.z); |
| 986 | self.w.div_assign(rhs.w); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | impl Div<f64> for DVec4 { |
| 991 | type Output = Self; |
| 992 | #[inline] |
| 993 | fn div(self, rhs: f64) -> Self { |
| 994 | Self { |
| 995 | x: self.x.div(rhs), |
| 996 | y: self.y.div(rhs), |
| 997 | z: self.z.div(rhs), |
| 998 | w: self.w.div(rhs), |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | impl DivAssign<f64> for DVec4 { |
| 1004 | #[inline] |
| 1005 | fn div_assign(&mut self, rhs: f64) { |
| 1006 | self.x.div_assign(rhs); |
| 1007 | self.y.div_assign(rhs); |
| 1008 | self.z.div_assign(rhs); |
| 1009 | self.w.div_assign(rhs); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | impl Div<DVec4> for f64 { |
| 1014 | type Output = DVec4; |
| 1015 | #[inline] |
| 1016 | fn div(self, rhs: DVec4) -> DVec4 { |
| 1017 | DVec4 { |
| 1018 | x: self.div(rhs.x), |
| 1019 | y: self.div(rhs.y), |
| 1020 | z: self.div(rhs.z), |
| 1021 | w: self.div(rhs.w), |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | impl Mul<DVec4> for DVec4 { |
| 1027 | type Output = Self; |
| 1028 | #[inline] |
| 1029 | fn mul(self, rhs: Self) -> Self { |
| 1030 | Self { |
| 1031 | x: self.x.mul(rhs.x), |
| 1032 | y: self.y.mul(rhs.y), |
| 1033 | z: self.z.mul(rhs.z), |
| 1034 | w: self.w.mul(rhs.w), |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | impl MulAssign<DVec4> for DVec4 { |
| 1040 | #[inline] |
| 1041 | fn mul_assign(&mut self, rhs: Self) { |
| 1042 | self.x.mul_assign(rhs.x); |
| 1043 | self.y.mul_assign(rhs.y); |
| 1044 | self.z.mul_assign(rhs.z); |
| 1045 | self.w.mul_assign(rhs.w); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | impl Mul<f64> for DVec4 { |
| 1050 | type Output = Self; |
| 1051 | #[inline] |
| 1052 | fn mul(self, rhs: f64) -> Self { |
| 1053 | Self { |
| 1054 | x: self.x.mul(rhs), |
| 1055 | y: self.y.mul(rhs), |
| 1056 | z: self.z.mul(rhs), |
| 1057 | w: self.w.mul(rhs), |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | impl MulAssign<f64> for DVec4 { |
| 1063 | #[inline] |
| 1064 | fn mul_assign(&mut self, rhs: f64) { |
| 1065 | self.x.mul_assign(rhs); |
| 1066 | self.y.mul_assign(rhs); |
| 1067 | self.z.mul_assign(rhs); |
| 1068 | self.w.mul_assign(rhs); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | impl Mul<DVec4> for f64 { |
| 1073 | type Output = DVec4; |
| 1074 | #[inline] |
| 1075 | fn mul(self, rhs: DVec4) -> DVec4 { |
| 1076 | DVec4 { |
| 1077 | x: self.mul(rhs.x), |
| 1078 | y: self.mul(rhs.y), |
| 1079 | z: self.mul(rhs.z), |
| 1080 | w: self.mul(rhs.w), |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | impl Add<DVec4> for DVec4 { |
| 1086 | type Output = Self; |
| 1087 | #[inline] |
| 1088 | fn add(self, rhs: Self) -> Self { |
| 1089 | Self { |
| 1090 | x: self.x.add(rhs.x), |
| 1091 | y: self.y.add(rhs.y), |
| 1092 | z: self.z.add(rhs.z), |
| 1093 | w: self.w.add(rhs.w), |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | impl AddAssign<DVec4> for DVec4 { |
| 1099 | #[inline] |
| 1100 | fn add_assign(&mut self, rhs: Self) { |
| 1101 | self.x.add_assign(rhs.x); |
| 1102 | self.y.add_assign(rhs.y); |
| 1103 | self.z.add_assign(rhs.z); |
| 1104 | self.w.add_assign(rhs.w); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | impl Add<f64> for DVec4 { |
| 1109 | type Output = Self; |
| 1110 | #[inline] |
| 1111 | fn add(self, rhs: f64) -> Self { |
| 1112 | Self { |
| 1113 | x: self.x.add(rhs), |
| 1114 | y: self.y.add(rhs), |
| 1115 | z: self.z.add(rhs), |
| 1116 | w: self.w.add(rhs), |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | impl AddAssign<f64> for DVec4 { |
| 1122 | #[inline] |
| 1123 | fn add_assign(&mut self, rhs: f64) { |
| 1124 | self.x.add_assign(rhs); |
| 1125 | self.y.add_assign(rhs); |
| 1126 | self.z.add_assign(rhs); |
| 1127 | self.w.add_assign(rhs); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | impl Add<DVec4> for f64 { |
| 1132 | type Output = DVec4; |
| 1133 | #[inline] |
| 1134 | fn add(self, rhs: DVec4) -> DVec4 { |
| 1135 | DVec4 { |
| 1136 | x: self.add(rhs.x), |
| 1137 | y: self.add(rhs.y), |
| 1138 | z: self.add(rhs.z), |
| 1139 | w: self.add(rhs.w), |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | impl Sub<DVec4> for DVec4 { |
| 1145 | type Output = Self; |
| 1146 | #[inline] |
| 1147 | fn sub(self, rhs: Self) -> Self { |
| 1148 | Self { |
| 1149 | x: self.x.sub(rhs.x), |
| 1150 | y: self.y.sub(rhs.y), |
| 1151 | z: self.z.sub(rhs.z), |
| 1152 | w: self.w.sub(rhs.w), |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | impl SubAssign<DVec4> for DVec4 { |
| 1158 | #[inline] |
| 1159 | fn sub_assign(&mut self, rhs: DVec4) { |
| 1160 | self.x.sub_assign(rhs.x); |
| 1161 | self.y.sub_assign(rhs.y); |
| 1162 | self.z.sub_assign(rhs.z); |
| 1163 | self.w.sub_assign(rhs.w); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | impl Sub<f64> for DVec4 { |
| 1168 | type Output = Self; |
| 1169 | #[inline] |
| 1170 | fn sub(self, rhs: f64) -> Self { |
| 1171 | Self { |
| 1172 | x: self.x.sub(rhs), |
| 1173 | y: self.y.sub(rhs), |
| 1174 | z: self.z.sub(rhs), |
| 1175 | w: self.w.sub(rhs), |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | impl SubAssign<f64> for DVec4 { |
| 1181 | #[inline] |
| 1182 | fn sub_assign(&mut self, rhs: f64) { |
| 1183 | self.x.sub_assign(rhs); |
| 1184 | self.y.sub_assign(rhs); |
| 1185 | self.z.sub_assign(rhs); |
| 1186 | self.w.sub_assign(rhs); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | impl Sub<DVec4> for f64 { |
| 1191 | type Output = DVec4; |
| 1192 | #[inline] |
| 1193 | fn sub(self, rhs: DVec4) -> DVec4 { |
| 1194 | DVec4 { |
| 1195 | x: self.sub(rhs.x), |
| 1196 | y: self.sub(rhs.y), |
| 1197 | z: self.sub(rhs.z), |
| 1198 | w: self.sub(rhs.w), |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | impl Rem<DVec4> for DVec4 { |
| 1204 | type Output = Self; |
| 1205 | #[inline] |
| 1206 | fn rem(self, rhs: Self) -> Self { |
| 1207 | Self { |
| 1208 | x: self.x.rem(rhs.x), |
| 1209 | y: self.y.rem(rhs.y), |
| 1210 | z: self.z.rem(rhs.z), |
| 1211 | w: self.w.rem(rhs.w), |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | impl RemAssign<DVec4> for DVec4 { |
| 1217 | #[inline] |
| 1218 | fn rem_assign(&mut self, rhs: Self) { |
| 1219 | self.x.rem_assign(rhs.x); |
| 1220 | self.y.rem_assign(rhs.y); |
| 1221 | self.z.rem_assign(rhs.z); |
| 1222 | self.w.rem_assign(rhs.w); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | impl Rem<f64> for DVec4 { |
| 1227 | type Output = Self; |
| 1228 | #[inline] |
| 1229 | fn rem(self, rhs: f64) -> Self { |
| 1230 | Self { |
| 1231 | x: self.x.rem(rhs), |
| 1232 | y: self.y.rem(rhs), |
| 1233 | z: self.z.rem(rhs), |
| 1234 | w: self.w.rem(rhs), |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | impl RemAssign<f64> for DVec4 { |
| 1240 | #[inline] |
| 1241 | fn rem_assign(&mut self, rhs: f64) { |
| 1242 | self.x.rem_assign(rhs); |
| 1243 | self.y.rem_assign(rhs); |
| 1244 | self.z.rem_assign(rhs); |
| 1245 | self.w.rem_assign(rhs); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | impl Rem<DVec4> for f64 { |
| 1250 | type Output = DVec4; |
| 1251 | #[inline] |
| 1252 | fn rem(self, rhs: DVec4) -> DVec4 { |
| 1253 | DVec4 { |
| 1254 | x: self.rem(rhs.x), |
| 1255 | y: self.rem(rhs.y), |
| 1256 | z: self.rem(rhs.z), |
| 1257 | w: self.rem(rhs.w), |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | #[cfg(not(target_arch = "spirv"))] |
| 1263 | impl AsRef<[f64; 4]> for DVec4 { |
| 1264 | #[inline] |
| 1265 | fn as_ref(&self) -> &[f64; 4] { |
| 1266 | unsafe { &*(self as *const DVec4 as *const [f64; 4]) } |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | #[cfg(not(target_arch = "spirv"))] |
| 1271 | impl AsMut<[f64; 4]> for DVec4 { |
| 1272 | #[inline] |
| 1273 | fn as_mut(&mut self) -> &mut [f64; 4] { |
| 1274 | unsafe { &mut *(self as *mut DVec4 as *mut [f64; 4]) } |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | impl Sum for DVec4 { |
| 1279 | #[inline] |
| 1280 | fn sum<I>(iter: I) -> Self |
| 1281 | where |
| 1282 | I: Iterator<Item = Self>, |
| 1283 | { |
| 1284 | iter.fold(Self::ZERO, Self::add) |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | impl<'a> Sum<&'a Self> for DVec4 { |
| 1289 | #[inline] |
| 1290 | fn sum<I>(iter: I) -> Self |
| 1291 | where |
| 1292 | I: Iterator<Item = &'a Self>, |
| 1293 | { |
| 1294 | iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | impl Product for DVec4 { |
| 1299 | #[inline] |
| 1300 | fn product<I>(iter: I) -> Self |
| 1301 | where |
| 1302 | I: Iterator<Item = Self>, |
| 1303 | { |
| 1304 | iter.fold(Self::ONE, Self::mul) |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | impl<'a> Product<&'a Self> for DVec4 { |
| 1309 | #[inline] |
| 1310 | fn product<I>(iter: I) -> Self |
| 1311 | where |
| 1312 | I: Iterator<Item = &'a Self>, |
| 1313 | { |
| 1314 | iter.fold(Self::ONE, |a, &b| Self::mul(a, b)) |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | impl Neg for DVec4 { |
| 1319 | type Output = Self; |
| 1320 | #[inline] |
| 1321 | fn neg(self) -> Self { |
| 1322 | Self { |
| 1323 | x: self.x.neg(), |
| 1324 | y: self.y.neg(), |
| 1325 | z: self.z.neg(), |
| 1326 | w: self.w.neg(), |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | impl Index<usize> for DVec4 { |
| 1332 | type Output = f64; |
| 1333 | #[inline] |
| 1334 | fn index(&self, index: usize) -> &Self::Output { |
| 1335 | match index { |
| 1336 | 0 => &self.x, |
| 1337 | 1 => &self.y, |
| 1338 | 2 => &self.z, |
| 1339 | 3 => &self.w, |
| 1340 | _ => panic!("index out of bounds"), |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | impl IndexMut<usize> for DVec4 { |
| 1346 | #[inline] |
| 1347 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 1348 | match index { |
| 1349 | 0 => &mut self.x, |
| 1350 | 1 => &mut self.y, |
| 1351 | 2 => &mut self.z, |
| 1352 | 3 => &mut self.w, |
| 1353 | _ => panic!("index out of bounds"), |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | #[cfg(not(target_arch = "spirv"))] |
| 1359 | impl fmt::Display for DVec4 { |
| 1360 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1361 | if let Some(p) = f.precision() { |
| 1362 | write!( |
| 1363 | f, |
| 1364 | "[{:.*}, {:.*}, {:.*}, {:.*}]", |
| 1365 | p, self.x, p, self.y, p, self.z, p, self.w |
| 1366 | ) |
| 1367 | } else { |
| 1368 | write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w) |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | #[cfg(not(target_arch = "spirv"))] |
| 1374 | impl fmt::Debug for DVec4 { |
| 1375 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1376 | fmt.debug_tuple(stringify!(DVec4)) |
| 1377 | .field(&self.x) |
| 1378 | .field(&self.y) |
| 1379 | .field(&self.z) |
| 1380 | .field(&self.w) |
| 1381 | .finish() |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | impl From<[f64; 4]> for DVec4 { |
| 1386 | #[inline] |
| 1387 | fn from(a: [f64; 4]) -> Self { |
| 1388 | Self::new(a[0], a[1], a[2], a[3]) |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | impl From<DVec4> for [f64; 4] { |
| 1393 | #[inline] |
| 1394 | fn from(v: DVec4) -> Self { |
| 1395 | [v.x, v.y, v.z, v.w] |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | impl From<(f64, f64, f64, f64)> for DVec4 { |
| 1400 | #[inline] |
| 1401 | fn from(t: (f64, f64, f64, f64)) -> Self { |
| 1402 | Self::new(t.0, t.1, t.2, t.3) |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | impl From<DVec4> for (f64, f64, f64, f64) { |
| 1407 | #[inline] |
| 1408 | fn from(v: DVec4) -> Self { |
| 1409 | (v.x, v.y, v.z, v.w) |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | impl From<(DVec3, f64)> for DVec4 { |
| 1414 | #[inline] |
| 1415 | fn from((v, w): (DVec3, f64)) -> Self { |
| 1416 | Self::new(v.x, v.y, v.z, w) |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | impl From<(f64, DVec3)> for DVec4 { |
| 1421 | #[inline] |
| 1422 | fn from((x, v): (f64, DVec3)) -> Self { |
| 1423 | Self::new(x, v.x, v.y, v.z) |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | impl From<(DVec2, f64, f64)> for DVec4 { |
| 1428 | #[inline] |
| 1429 | fn from((v, z, w): (DVec2, f64, f64)) -> Self { |
| 1430 | Self::new(v.x, v.y, z, w) |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | impl From<(DVec2, DVec2)> for DVec4 { |
| 1435 | #[inline] |
| 1436 | fn from((v, u): (DVec2, DVec2)) -> Self { |
| 1437 | Self::new(v.x, v.y, u.x, u.y) |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | impl From<Vec4> for DVec4 { |
| 1442 | #[inline] |
| 1443 | fn from(v: Vec4) -> Self { |
| 1444 | Self::new( |
| 1445 | f64::from(v.x), |
| 1446 | f64::from(v.y), |
| 1447 | f64::from(v.z), |
| 1448 | f64::from(v.w), |
| 1449 | ) |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | impl From<IVec4> for DVec4 { |
| 1454 | #[inline] |
| 1455 | fn from(v: IVec4) -> Self { |
| 1456 | Self::new( |
| 1457 | f64::from(v.x), |
| 1458 | f64::from(v.y), |
| 1459 | f64::from(v.z), |
| 1460 | f64::from(v.w), |
| 1461 | ) |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | impl From<UVec4> for DVec4 { |
| 1466 | #[inline] |
| 1467 | fn from(v: UVec4) -> Self { |
| 1468 | Self::new( |
| 1469 | f64::from(v.x), |
| 1470 | f64::from(v.y), |
| 1471 | f64::from(v.z), |
| 1472 | f64::from(v.w), |
| 1473 | ) |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | impl From<BVec4> for DVec4 { |
| 1478 | #[inline] |
| 1479 | fn from(v: BVec4) -> Self { |
| 1480 | Self::new( |
| 1481 | f64::from(v.x), |
| 1482 | f64::from(v.y), |
| 1483 | f64::from(v.z), |
| 1484 | f64::from(v.w), |
| 1485 | ) |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | #[cfg(not(feature = "scalar-math"))] |
| 1490 | |
| 1491 | impl From<BVec4A> for DVec4 { |
| 1492 | #[inline] |
| 1493 | fn from(v: BVec4A) -> Self { |
| 1494 | let bool_array: [bool; 4] = v.into(); |
| 1495 | Self::new( |
| 1496 | f64::from(bool_array[0]), |
| 1497 | f64::from(bool_array[1]), |
| 1498 | f64::from(bool_array[2]), |
| 1499 | f64::from(bool_array[3]), |
| 1500 | ) |
| 1501 | } |
| 1502 | } |