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 use core::fmt;
8 use core::iter::{Product, Sum};
9 use core::{f32, ops::*};
10
11 /// Creates a 4-dimensional vector.
12 #[inline(always)]
13 #[must_use]
dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec414 pub const fn dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec4 {
15 DVec4::new(x, y, z, w)
16 }
17
18 /// A 4-dimensional vector.
19 #[derive(Clone, Copy, PartialEq)]
20 #[cfg_attr(feature = "cuda", repr(align(16)))]
21 #[cfg_attr(not(target_arch = "spirv"), repr(C))]
22 #[cfg_attr(target_arch = "spirv", repr(simd))]
23 pub struct DVec4 {
24 pub x: f64,
25 pub y: f64,
26 pub z: f64,
27 pub w: f64,
28 }
29
30 impl DVec4 {
31 /// All zeroes.
32 pub const ZERO: Self = Self::splat(0.0);
33
34 /// All ones.
35 pub const ONE: Self = Self::splat(1.0);
36
37 /// All negative ones.
38 pub const NEG_ONE: Self = Self::splat(-1.0);
39
40 /// All `f64::MIN`.
41 pub const MIN: Self = Self::splat(f64::MIN);
42
43 /// All `f64::MAX`.
44 pub const MAX: Self = Self::splat(f64::MAX);
45
46 /// All `f64::NAN`.
47 pub const NAN: Self = Self::splat(f64::NAN);
48
49 /// All `f64::INFINITY`.
50 pub const INFINITY: Self = Self::splat(f64::INFINITY);
51
52 /// All `f64::NEG_INFINITY`.
53 pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
54
55 /// A unit vector pointing along the positive X axis.
56 pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
57
58 /// A unit vector pointing along the positive Y axis.
59 pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
60
61 /// A unit vector pointing along the positive Z axis.
62 pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
63
64 /// A unit vector pointing along the positive W axis.
65 pub const W: Self = Self::new(0.0, 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, 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, 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, 0.0);
75
76 /// A unit vector pointing along the negative W axis.
77 pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
78
79 /// The unit axes.
80 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
81
82 /// Creates a new vector.
83 #[inline(always)]
84 #[must_use]
new(x: f64, y: f64, z: f64, w: f64) -> Self85 pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Self {
86 Self { x, y, z, w }
87 }
88
89 /// Creates a vector with all elements set to `v`.
90 #[inline]
91 #[must_use]
splat(v: f64) -> Self92 pub const fn splat(v: f64) -> Self {
93 Self {
94 x: v,
95
96 y: v,
97
98 z: v,
99
100 w: v,
101 }
102 }
103
104 /// Returns a vector containing each element of `self` modified by a mapping function `f`.
105 #[inline]
106 #[must_use]
map<F>(self, f: F) -> Self where F: Fn(f64) -> f64,107 pub fn map<F>(self, f: F) -> Self
108 where
109 F: Fn(f64) -> f64,
110 {
111 Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
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]
select(mask: BVec4, if_true: Self, if_false: Self) -> Self121 pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
122 Self {
123 x: if mask.test(0) { if_true.x } else { if_false.x },
124 y: if mask.test(1) { if_true.y } else { if_false.y },
125 z: if mask.test(2) { if_true.z } else { if_false.z },
126 w: if mask.test(3) { if_true.w } else { if_false.w },
127 }
128 }
129
130 /// Creates a new vector from an array.
131 #[inline]
132 #[must_use]
from_array(a: [f64; 4]) -> Self133 pub const fn from_array(a: [f64; 4]) -> Self {
134 Self::new(a[0], a[1], a[2], a[3])
135 }
136
137 /// `[x, y, z, w]`
138 #[inline]
139 #[must_use]
to_array(&self) -> [f64; 4]140 pub const fn to_array(&self) -> [f64; 4] {
141 [self.x, self.y, self.z, self.w]
142 }
143
144 /// Creates a vector from the first 4 values in `slice`.
145 ///
146 /// # Panics
147 ///
148 /// Panics if `slice` is less than 4 elements long.
149 #[inline]
150 #[must_use]
from_slice(slice: &[f64]) -> Self151 pub const fn from_slice(slice: &[f64]) -> Self {
152 assert!(slice.len() >= 4);
153 Self::new(slice[0], slice[1], slice[2], slice[3])
154 }
155
156 /// Writes the elements of `self` to the first 4 elements in `slice`.
157 ///
158 /// # Panics
159 ///
160 /// Panics if `slice` is less than 4 elements long.
161 #[inline]
write_to_slice(self, slice: &mut [f64])162 pub fn write_to_slice(self, slice: &mut [f64]) {
163 slice[..4].copy_from_slice(&self.to_array());
164 }
165
166 /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
167 ///
168 /// Truncation to [`DVec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()].
169 #[inline]
170 #[must_use]
truncate(self) -> DVec3171 pub fn truncate(self) -> DVec3 {
172 use crate::swizzles::Vec4Swizzles;
173 self.xyz()
174 }
175
176 /// Creates a 4D vector from `self` with the given value of `x`.
177 #[inline]
178 #[must_use]
with_x(mut self, x: f64) -> Self179 pub fn with_x(mut self, x: f64) -> Self {
180 self.x = x;
181 self
182 }
183
184 /// Creates a 4D vector from `self` with the given value of `y`.
185 #[inline]
186 #[must_use]
with_y(mut self, y: f64) -> Self187 pub fn with_y(mut self, y: f64) -> Self {
188 self.y = y;
189 self
190 }
191
192 /// Creates a 4D vector from `self` with the given value of `z`.
193 #[inline]
194 #[must_use]
with_z(mut self, z: f64) -> Self195 pub fn with_z(mut self, z: f64) -> Self {
196 self.z = z;
197 self
198 }
199
200 /// Creates a 4D vector from `self` with the given value of `w`.
201 #[inline]
202 #[must_use]
with_w(mut self, w: f64) -> Self203 pub fn with_w(mut self, w: f64) -> Self {
204 self.w = w;
205 self
206 }
207
208 /// Computes the dot product of `self` and `rhs`.
209 #[inline]
210 #[must_use]
dot(self, rhs: Self) -> f64211 pub fn dot(self, rhs: Self) -> f64 {
212 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
213 }
214
215 /// Returns a vector where every component is the dot product of `self` and `rhs`.
216 #[inline]
217 #[must_use]
dot_into_vec(self, rhs: Self) -> Self218 pub fn dot_into_vec(self, rhs: Self) -> Self {
219 Self::splat(self.dot(rhs))
220 }
221
222 /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
223 ///
224 /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
225 #[inline]
226 #[must_use]
min(self, rhs: Self) -> Self227 pub fn min(self, rhs: Self) -> Self {
228 Self {
229 x: self.x.min(rhs.x),
230 y: self.y.min(rhs.y),
231 z: self.z.min(rhs.z),
232 w: self.w.min(rhs.w),
233 }
234 }
235
236 /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
237 ///
238 /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
239 #[inline]
240 #[must_use]
max(self, rhs: Self) -> Self241 pub fn max(self, rhs: Self) -> Self {
242 Self {
243 x: self.x.max(rhs.x),
244 y: self.y.max(rhs.y),
245 z: self.z.max(rhs.z),
246 w: self.w.max(rhs.w),
247 }
248 }
249
250 /// Component-wise clamping of values, similar to [`f64::clamp`].
251 ///
252 /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
253 ///
254 /// # Panics
255 ///
256 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
257 #[inline]
258 #[must_use]
clamp(self, min: Self, max: Self) -> Self259 pub fn clamp(self, min: Self, max: Self) -> Self {
260 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
261 self.max(min).min(max)
262 }
263
264 /// Returns the horizontal minimum of `self`.
265 ///
266 /// In other words this computes `min(x, y, ..)`.
267 #[inline]
268 #[must_use]
min_element(self) -> f64269 pub fn min_element(self) -> f64 {
270 self.x.min(self.y.min(self.z.min(self.w)))
271 }
272
273 /// Returns the horizontal maximum of `self`.
274 ///
275 /// In other words this computes `max(x, y, ..)`.
276 #[inline]
277 #[must_use]
max_element(self) -> f64278 pub fn max_element(self) -> f64 {
279 self.x.max(self.y.max(self.z.max(self.w)))
280 }
281
282 /// Returns the sum of all elements of `self`.
283 ///
284 /// In other words, this computes `self.x + self.y + ..`.
285 #[inline]
286 #[must_use]
element_sum(self) -> f64287 pub fn element_sum(self) -> f64 {
288 self.x + self.y + self.z + self.w
289 }
290
291 /// Returns the product of all elements of `self`.
292 ///
293 /// In other words, this computes `self.x * self.y * ..`.
294 #[inline]
295 #[must_use]
element_product(self) -> f64296 pub fn element_product(self) -> f64 {
297 self.x * self.y * self.z * self.w
298 }
299
300 /// Returns a vector mask containing the result of a `==` comparison for each element of
301 /// `self` and `rhs`.
302 ///
303 /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
304 /// elements.
305 #[inline]
306 #[must_use]
cmpeq(self, rhs: Self) -> BVec4307 pub fn cmpeq(self, rhs: Self) -> BVec4 {
308 BVec4::new(
309 self.x.eq(&rhs.x),
310 self.y.eq(&rhs.y),
311 self.z.eq(&rhs.z),
312 self.w.eq(&rhs.w),
313 )
314 }
315
316 /// Returns a vector mask containing the result of a `!=` comparison for each element of
317 /// `self` and `rhs`.
318 ///
319 /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
320 /// elements.
321 #[inline]
322 #[must_use]
cmpne(self, rhs: Self) -> BVec4323 pub fn cmpne(self, rhs: Self) -> BVec4 {
324 BVec4::new(
325 self.x.ne(&rhs.x),
326 self.y.ne(&rhs.y),
327 self.z.ne(&rhs.z),
328 self.w.ne(&rhs.w),
329 )
330 }
331
332 /// Returns a vector mask containing the result of a `>=` comparison for each element of
333 /// `self` and `rhs`.
334 ///
335 /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
336 /// elements.
337 #[inline]
338 #[must_use]
cmpge(self, rhs: Self) -> BVec4339 pub fn cmpge(self, rhs: Self) -> BVec4 {
340 BVec4::new(
341 self.x.ge(&rhs.x),
342 self.y.ge(&rhs.y),
343 self.z.ge(&rhs.z),
344 self.w.ge(&rhs.w),
345 )
346 }
347
348 /// Returns a vector mask containing the result of a `>` comparison for each element of
349 /// `self` and `rhs`.
350 ///
351 /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
352 /// elements.
353 #[inline]
354 #[must_use]
cmpgt(self, rhs: Self) -> BVec4355 pub fn cmpgt(self, rhs: Self) -> BVec4 {
356 BVec4::new(
357 self.x.gt(&rhs.x),
358 self.y.gt(&rhs.y),
359 self.z.gt(&rhs.z),
360 self.w.gt(&rhs.w),
361 )
362 }
363
364 /// Returns a vector mask containing the result of a `<=` comparison for each element of
365 /// `self` and `rhs`.
366 ///
367 /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
368 /// elements.
369 #[inline]
370 #[must_use]
cmple(self, rhs: Self) -> BVec4371 pub fn cmple(self, rhs: Self) -> BVec4 {
372 BVec4::new(
373 self.x.le(&rhs.x),
374 self.y.le(&rhs.y),
375 self.z.le(&rhs.z),
376 self.w.le(&rhs.w),
377 )
378 }
379
380 /// Returns a vector mask containing the result of a `<` comparison for each element of
381 /// `self` and `rhs`.
382 ///
383 /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
384 /// elements.
385 #[inline]
386 #[must_use]
cmplt(self, rhs: Self) -> BVec4387 pub fn cmplt(self, rhs: Self) -> BVec4 {
388 BVec4::new(
389 self.x.lt(&rhs.x),
390 self.y.lt(&rhs.y),
391 self.z.lt(&rhs.z),
392 self.w.lt(&rhs.w),
393 )
394 }
395
396 /// Returns a vector containing the absolute value of each element of `self`.
397 #[inline]
398 #[must_use]
abs(self) -> Self399 pub fn abs(self) -> Self {
400 Self {
401 x: math::abs(self.x),
402 y: math::abs(self.y),
403 z: math::abs(self.z),
404 w: math::abs(self.w),
405 }
406 }
407
408 /// Returns a vector with elements representing the sign of `self`.
409 ///
410 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
411 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
412 /// - `NAN` if the number is `NAN`
413 #[inline]
414 #[must_use]
signum(self) -> Self415 pub fn signum(self) -> Self {
416 Self {
417 x: math::signum(self.x),
418 y: math::signum(self.y),
419 z: math::signum(self.z),
420 w: math::signum(self.w),
421 }
422 }
423
424 /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
425 #[inline]
426 #[must_use]
copysign(self, rhs: Self) -> Self427 pub fn copysign(self, rhs: Self) -> Self {
428 Self {
429 x: math::copysign(self.x, rhs.x),
430 y: math::copysign(self.y, rhs.y),
431 z: math::copysign(self.z, rhs.z),
432 w: math::copysign(self.w, rhs.w),
433 }
434 }
435
436 /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
437 ///
438 /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes
439 /// into the first lowest bit, element `y` into the second, etc.
440 #[inline]
441 #[must_use]
is_negative_bitmask(self) -> u32442 pub fn is_negative_bitmask(self) -> u32 {
443 (self.x.is_sign_negative() as u32)
444 | (self.y.is_sign_negative() as u32) << 1
445 | (self.z.is_sign_negative() as u32) << 2
446 | (self.w.is_sign_negative() as u32) << 3
447 }
448
449 /// Returns `true` if, and only if, all elements are finite. If any element is either
450 /// `NaN`, positive or negative infinity, this will return `false`.
451 #[inline]
452 #[must_use]
is_finite(self) -> bool453 pub fn is_finite(self) -> bool {
454 self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
455 }
456
457 /// Performs `is_finite` on each element of self, returning a vector mask of the results.
458 ///
459 /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
is_finite_mask(self) -> BVec4460 pub fn is_finite_mask(self) -> BVec4 {
461 BVec4::new(
462 self.x.is_finite(),
463 self.y.is_finite(),
464 self.z.is_finite(),
465 self.w.is_finite(),
466 )
467 }
468
469 /// Returns `true` if any elements are `NaN`.
470 #[inline]
471 #[must_use]
is_nan(self) -> bool472 pub fn is_nan(self) -> bool {
473 self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
474 }
475
476 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
477 ///
478 /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
479 #[inline]
480 #[must_use]
is_nan_mask(self) -> BVec4481 pub fn is_nan_mask(self) -> BVec4 {
482 BVec4::new(
483 self.x.is_nan(),
484 self.y.is_nan(),
485 self.z.is_nan(),
486 self.w.is_nan(),
487 )
488 }
489
490 /// Computes the length of `self`.
491 #[doc(alias = "magnitude")]
492 #[inline]
493 #[must_use]
length(self) -> f64494 pub fn length(self) -> f64 {
495 math::sqrt(self.dot(self))
496 }
497
498 /// Computes the squared length of `self`.
499 ///
500 /// This is faster than `length()` as it avoids a square root operation.
501 #[doc(alias = "magnitude2")]
502 #[inline]
503 #[must_use]
length_squared(self) -> f64504 pub fn length_squared(self) -> f64 {
505 self.dot(self)
506 }
507
508 /// Computes `1.0 / length()`.
509 ///
510 /// For valid results, `self` must _not_ be of length zero.
511 #[inline]
512 #[must_use]
length_recip(self) -> f64513 pub fn length_recip(self) -> f64 {
514 self.length().recip()
515 }
516
517 /// Computes the Euclidean distance between two points in space.
518 #[inline]
519 #[must_use]
distance(self, rhs: Self) -> f64520 pub fn distance(self, rhs: Self) -> f64 {
521 (self - rhs).length()
522 }
523
524 /// Compute the squared euclidean distance between two points in space.
525 #[inline]
526 #[must_use]
distance_squared(self, rhs: Self) -> f64527 pub fn distance_squared(self, rhs: Self) -> f64 {
528 (self - rhs).length_squared()
529 }
530
531 /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
532 #[inline]
533 #[must_use]
div_euclid(self, rhs: Self) -> Self534 pub fn div_euclid(self, rhs: Self) -> Self {
535 Self::new(
536 math::div_euclid(self.x, rhs.x),
537 math::div_euclid(self.y, rhs.y),
538 math::div_euclid(self.z, rhs.z),
539 math::div_euclid(self.w, rhs.w),
540 )
541 }
542
543 /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
544 ///
545 /// [Euclidean division]: f64::rem_euclid
546 #[inline]
547 #[must_use]
rem_euclid(self, rhs: Self) -> Self548 pub fn rem_euclid(self, rhs: Self) -> Self {
549 Self::new(
550 math::rem_euclid(self.x, rhs.x),
551 math::rem_euclid(self.y, rhs.y),
552 math::rem_euclid(self.z, rhs.z),
553 math::rem_euclid(self.w, rhs.w),
554 )
555 }
556
557 /// Returns `self` normalized to length 1.0.
558 ///
559 /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
560 ///
561 /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
562 ///
563 /// Panics
564 ///
565 /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
566 #[inline]
567 #[must_use]
normalize(self) -> Self568 pub fn normalize(self) -> Self {
569 #[allow(clippy::let_and_return)]
570 let normalized = self.mul(self.length_recip());
571 glam_assert!(normalized.is_finite());
572 normalized
573 }
574
575 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
576 ///
577 /// In particular, if the input is zero (or very close to zero), or non-finite,
578 /// the result of this operation will be `None`.
579 ///
580 /// See also [`Self::normalize_or_zero()`].
581 #[inline]
582 #[must_use]
try_normalize(self) -> Option<Self>583 pub fn try_normalize(self) -> Option<Self> {
584 let rcp = self.length_recip();
585 if rcp.is_finite() && rcp > 0.0 {
586 Some(self * rcp)
587 } else {
588 None
589 }
590 }
591
592 /// Returns `self` normalized to length 1.0 if possible, else returns a
593 /// fallback value.
594 ///
595 /// In particular, if the input is zero (or very close to zero), or non-finite,
596 /// the result of this operation will be the fallback value.
597 ///
598 /// See also [`Self::try_normalize()`].
599 #[inline]
600 #[must_use]
normalize_or(self, fallback: Self) -> Self601 pub fn normalize_or(self, fallback: Self) -> Self {
602 let rcp = self.length_recip();
603 if rcp.is_finite() && rcp > 0.0 {
604 self * rcp
605 } else {
606 fallback
607 }
608 }
609
610 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
611 ///
612 /// In particular, if the input is zero (or very close to zero), or non-finite,
613 /// the result of this operation will be zero.
614 ///
615 /// See also [`Self::try_normalize()`].
616 #[inline]
617 #[must_use]
normalize_or_zero(self) -> Self618 pub fn normalize_or_zero(self) -> Self {
619 self.normalize_or(Self::ZERO)
620 }
621
622 /// Returns whether `self` is length `1.0` or not.
623 ///
624 /// Uses a precision threshold of approximately `1e-4`.
625 #[inline]
626 #[must_use]
is_normalized(self) -> bool627 pub fn is_normalized(self) -> bool {
628 math::abs(self.length_squared() - 1.0) <= 2e-4
629 }
630
631 /// Returns the vector projection of `self` onto `rhs`.
632 ///
633 /// `rhs` must be of non-zero length.
634 ///
635 /// # Panics
636 ///
637 /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
638 #[inline]
639 #[must_use]
project_onto(self, rhs: Self) -> Self640 pub fn project_onto(self, rhs: Self) -> Self {
641 let other_len_sq_rcp = rhs.dot(rhs).recip();
642 glam_assert!(other_len_sq_rcp.is_finite());
643 rhs * self.dot(rhs) * other_len_sq_rcp
644 }
645
646 /// Returns the vector rejection of `self` from `rhs`.
647 ///
648 /// The vector rejection is the vector perpendicular to the projection of `self` onto
649 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
650 ///
651 /// `rhs` must be of non-zero length.
652 ///
653 /// # Panics
654 ///
655 /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
656 #[doc(alias("plane"))]
657 #[inline]
658 #[must_use]
reject_from(self, rhs: Self) -> Self659 pub fn reject_from(self, rhs: Self) -> Self {
660 self - self.project_onto(rhs)
661 }
662
663 /// Returns the vector projection of `self` onto `rhs`.
664 ///
665 /// `rhs` must be normalized.
666 ///
667 /// # Panics
668 ///
669 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
670 #[inline]
671 #[must_use]
project_onto_normalized(self, rhs: Self) -> Self672 pub fn project_onto_normalized(self, rhs: Self) -> Self {
673 glam_assert!(rhs.is_normalized());
674 rhs * self.dot(rhs)
675 }
676
677 /// Returns the vector rejection of `self` from `rhs`.
678 ///
679 /// The vector rejection is the vector perpendicular to the projection of `self` onto
680 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
681 ///
682 /// `rhs` must be normalized.
683 ///
684 /// # Panics
685 ///
686 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
687 #[doc(alias("plane"))]
688 #[inline]
689 #[must_use]
reject_from_normalized(self, rhs: Self) -> Self690 pub fn reject_from_normalized(self, rhs: Self) -> Self {
691 self - self.project_onto_normalized(rhs)
692 }
693
694 /// Returns a vector containing the nearest integer to a number for each element of `self`.
695 /// Round half-way cases away from 0.0.
696 #[inline]
697 #[must_use]
round(self) -> Self698 pub fn round(self) -> Self {
699 Self {
700 x: math::round(self.x),
701 y: math::round(self.y),
702 z: math::round(self.z),
703 w: math::round(self.w),
704 }
705 }
706
707 /// Returns a vector containing the largest integer less than or equal to a number for each
708 /// element of `self`.
709 #[inline]
710 #[must_use]
floor(self) -> Self711 pub fn floor(self) -> Self {
712 Self {
713 x: math::floor(self.x),
714 y: math::floor(self.y),
715 z: math::floor(self.z),
716 w: math::floor(self.w),
717 }
718 }
719
720 /// Returns a vector containing the smallest integer greater than or equal to a number for
721 /// each element of `self`.
722 #[inline]
723 #[must_use]
ceil(self) -> Self724 pub fn ceil(self) -> Self {
725 Self {
726 x: math::ceil(self.x),
727 y: math::ceil(self.y),
728 z: math::ceil(self.z),
729 w: math::ceil(self.w),
730 }
731 }
732
733 /// Returns a vector containing the integer part each element of `self`. This means numbers are
734 /// always truncated towards zero.
735 #[inline]
736 #[must_use]
trunc(self) -> Self737 pub fn trunc(self) -> Self {
738 Self {
739 x: math::trunc(self.x),
740 y: math::trunc(self.y),
741 z: math::trunc(self.z),
742 w: math::trunc(self.w),
743 }
744 }
745
746 /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
747 ///
748 /// Note that this differs from the GLSL implementation of `fract` which returns
749 /// `self - self.floor()`.
750 ///
751 /// Note that this is fast but not precise for large numbers.
752 #[inline]
753 #[must_use]
fract(self) -> Self754 pub fn fract(self) -> Self {
755 self - self.trunc()
756 }
757
758 /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
759 ///
760 /// Note that this differs from the Rust implementation of `fract` which returns
761 /// `self - self.trunc()`.
762 ///
763 /// Note that this is fast but not precise for large numbers.
764 #[inline]
765 #[must_use]
fract_gl(self) -> Self766 pub fn fract_gl(self) -> Self {
767 self - self.floor()
768 }
769
770 /// Returns a vector containing `e^self` (the exponential function) for each element of
771 /// `self`.
772 #[inline]
773 #[must_use]
exp(self) -> Self774 pub fn exp(self) -> Self {
775 Self::new(
776 math::exp(self.x),
777 math::exp(self.y),
778 math::exp(self.z),
779 math::exp(self.w),
780 )
781 }
782
783 /// Returns a vector containing each element of `self` raised to the power of `n`.
784 #[inline]
785 #[must_use]
powf(self, n: f64) -> Self786 pub fn powf(self, n: f64) -> Self {
787 Self::new(
788 math::powf(self.x, n),
789 math::powf(self.y, n),
790 math::powf(self.z, n),
791 math::powf(self.w, n),
792 )
793 }
794
795 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
796 #[inline]
797 #[must_use]
recip(self) -> Self798 pub fn recip(self) -> Self {
799 Self {
800 x: 1.0 / self.x,
801 y: 1.0 / self.y,
802 z: 1.0 / self.z,
803 w: 1.0 / self.w,
804 }
805 }
806
807 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
808 ///
809 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
810 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
811 /// extrapolated.
812 #[doc(alias = "mix")]
813 #[inline]
814 #[must_use]
lerp(self, rhs: Self, s: f64) -> Self815 pub fn lerp(self, rhs: Self, s: f64) -> Self {
816 self * (1.0 - s) + rhs * s
817 }
818
819 /// Moves towards `rhs` based on the value `d`.
820 ///
821 /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
822 /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
823 #[inline]
824 #[must_use]
move_towards(&self, rhs: Self, d: f64) -> Self825 pub fn move_towards(&self, rhs: Self, d: f64) -> Self {
826 let a = rhs - *self;
827 let len = a.length();
828 if len <= d || len <= 1e-4 {
829 return rhs;
830 }
831 *self + a / len * d
832 }
833
834 /// Calculates the midpoint between `self` and `rhs`.
835 ///
836 /// The midpoint is the average of, or halfway point between, two vectors.
837 /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
838 /// while being slightly cheaper to compute.
839 #[inline]
midpoint(self, rhs: Self) -> Self840 pub fn midpoint(self, rhs: Self) -> Self {
841 (self + rhs) * 0.5
842 }
843
844 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
845 /// less than or equal to `max_abs_diff`.
846 ///
847 /// This can be used to compare if two vectors contain similar elements. It works best when
848 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
849 /// the values being compared against.
850 ///
851 /// For more see
852 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
853 #[inline]
854 #[must_use]
abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool855 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
856 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
857 }
858
859 /// Returns a vector with a length no less than `min` and no more than `max`.
860 ///
861 /// # Panics
862 ///
863 /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
864 #[inline]
865 #[must_use]
clamp_length(self, min: f64, max: f64) -> Self866 pub fn clamp_length(self, min: f64, max: f64) -> Self {
867 glam_assert!(0.0 <= min);
868 glam_assert!(min <= max);
869 let length_sq = self.length_squared();
870 if length_sq < min * min {
871 min * (self / math::sqrt(length_sq))
872 } else if length_sq > max * max {
873 max * (self / math::sqrt(length_sq))
874 } else {
875 self
876 }
877 }
878
879 /// Returns a vector with a length no more than `max`.
880 ///
881 /// # Panics
882 ///
883 /// Will panic if `max` is negative when `glam_assert` is enabled.
884 #[inline]
885 #[must_use]
clamp_length_max(self, max: f64) -> Self886 pub fn clamp_length_max(self, max: f64) -> Self {
887 glam_assert!(0.0 <= max);
888 let length_sq = self.length_squared();
889 if length_sq > max * max {
890 max * (self / math::sqrt(length_sq))
891 } else {
892 self
893 }
894 }
895
896 /// Returns a vector with a length no less than `min`.
897 ///
898 /// # Panics
899 ///
900 /// Will panic if `min` is negative when `glam_assert` is enabled.
901 #[inline]
902 #[must_use]
clamp_length_min(self, min: f64) -> Self903 pub fn clamp_length_min(self, min: f64) -> Self {
904 glam_assert!(0.0 <= min);
905 let length_sq = self.length_squared();
906 if length_sq < min * min {
907 min * (self / math::sqrt(length_sq))
908 } else {
909 self
910 }
911 }
912
913 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
914 /// error, yielding a more accurate result than an unfused multiply-add.
915 ///
916 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
917 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
918 /// and will be heavily dependant on designing algorithms with specific target hardware in
919 /// mind.
920 #[inline]
921 #[must_use]
mul_add(self, a: Self, b: Self) -> Self922 pub fn mul_add(self, a: Self, b: Self) -> Self {
923 Self::new(
924 math::mul_add(self.x, a.x, b.x),
925 math::mul_add(self.y, a.y, b.y),
926 math::mul_add(self.z, a.z, b.z),
927 math::mul_add(self.w, a.w, b.w),
928 )
929 }
930
931 /// Returns the reflection vector for a given incident vector `self` and surface normal
932 /// `normal`.
933 ///
934 /// `normal` must be normalized.
935 ///
936 /// # Panics
937 ///
938 /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
939 #[inline]
940 #[must_use]
reflect(self, normal: Self) -> Self941 pub fn reflect(self, normal: Self) -> Self {
942 glam_assert!(normal.is_normalized());
943 self - 2.0 * self.dot(normal) * normal
944 }
945
946 /// Returns the refraction direction for a given incident vector `self`, surface normal
947 /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
948 /// a zero vector will be returned.
949 ///
950 /// `self` and `normal` must be normalized.
951 ///
952 /// # Panics
953 ///
954 /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
955 #[inline]
956 #[must_use]
refract(self, normal: Self, eta: f64) -> Self957 pub fn refract(self, normal: Self, eta: f64) -> Self {
958 glam_assert!(self.is_normalized());
959 glam_assert!(normal.is_normalized());
960 let n_dot_i = normal.dot(self);
961 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
962 if k >= 0.0 {
963 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
964 } else {
965 Self::ZERO
966 }
967 }
968
969 /// Casts all elements of `self` to `f32`.
970 #[inline]
971 #[must_use]
as_vec4(&self) -> crate::Vec4972 pub fn as_vec4(&self) -> crate::Vec4 {
973 crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32)
974 }
975
976 /// Casts all elements of `self` to `i8`.
977 #[inline]
978 #[must_use]
as_i8vec4(&self) -> crate::I8Vec4979 pub fn as_i8vec4(&self) -> crate::I8Vec4 {
980 crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
981 }
982
983 /// Casts all elements of `self` to `u8`.
984 #[inline]
985 #[must_use]
as_u8vec4(&self) -> crate::U8Vec4986 pub fn as_u8vec4(&self) -> crate::U8Vec4 {
987 crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
988 }
989
990 /// Casts all elements of `self` to `i16`.
991 #[inline]
992 #[must_use]
as_i16vec4(&self) -> crate::I16Vec4993 pub fn as_i16vec4(&self) -> crate::I16Vec4 {
994 crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
995 }
996
997 /// Casts all elements of `self` to `u16`.
998 #[inline]
999 #[must_use]
as_u16vec4(&self) -> crate::U16Vec41000 pub fn as_u16vec4(&self) -> crate::U16Vec4 {
1001 crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
1002 }
1003
1004 /// Casts all elements of `self` to `i32`.
1005 #[inline]
1006 #[must_use]
as_ivec4(&self) -> crate::IVec41007 pub fn as_ivec4(&self) -> crate::IVec4 {
1008 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
1009 }
1010
1011 /// Casts all elements of `self` to `u32`.
1012 #[inline]
1013 #[must_use]
as_uvec4(&self) -> crate::UVec41014 pub fn as_uvec4(&self) -> crate::UVec4 {
1015 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
1016 }
1017
1018 /// Casts all elements of `self` to `i64`.
1019 #[inline]
1020 #[must_use]
as_i64vec4(&self) -> crate::I64Vec41021 pub fn as_i64vec4(&self) -> crate::I64Vec4 {
1022 crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
1023 }
1024
1025 /// Casts all elements of `self` to `u64`.
1026 #[inline]
1027 #[must_use]
as_u64vec4(&self) -> crate::U64Vec41028 pub fn as_u64vec4(&self) -> crate::U64Vec4 {
1029 crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
1030 }
1031 }
1032
1033 impl Default for DVec4 {
1034 #[inline(always)]
default() -> Self1035 fn default() -> Self {
1036 Self::ZERO
1037 }
1038 }
1039
1040 impl Div<DVec4> for DVec4 {
1041 type Output = Self;
1042 #[inline]
div(self, rhs: Self) -> Self1043 fn div(self, rhs: Self) -> Self {
1044 Self {
1045 x: self.x.div(rhs.x),
1046 y: self.y.div(rhs.y),
1047 z: self.z.div(rhs.z),
1048 w: self.w.div(rhs.w),
1049 }
1050 }
1051 }
1052
1053 impl Div<&DVec4> for DVec4 {
1054 type Output = DVec4;
1055 #[inline]
div(self, rhs: &DVec4) -> DVec41056 fn div(self, rhs: &DVec4) -> DVec4 {
1057 self.div(*rhs)
1058 }
1059 }
1060
1061 impl Div<&DVec4> for &DVec4 {
1062 type Output = DVec4;
1063 #[inline]
div(self, rhs: &DVec4) -> DVec41064 fn div(self, rhs: &DVec4) -> DVec4 {
1065 (*self).div(*rhs)
1066 }
1067 }
1068
1069 impl Div<DVec4> for &DVec4 {
1070 type Output = DVec4;
1071 #[inline]
div(self, rhs: DVec4) -> DVec41072 fn div(self, rhs: DVec4) -> DVec4 {
1073 (*self).div(rhs)
1074 }
1075 }
1076
1077 impl DivAssign<DVec4> for DVec4 {
1078 #[inline]
div_assign(&mut self, rhs: Self)1079 fn div_assign(&mut self, rhs: Self) {
1080 self.x.div_assign(rhs.x);
1081 self.y.div_assign(rhs.y);
1082 self.z.div_assign(rhs.z);
1083 self.w.div_assign(rhs.w);
1084 }
1085 }
1086
1087 impl DivAssign<&DVec4> for DVec4 {
1088 #[inline]
div_assign(&mut self, rhs: &DVec4)1089 fn div_assign(&mut self, rhs: &DVec4) {
1090 self.div_assign(*rhs)
1091 }
1092 }
1093
1094 impl Div<f64> for DVec4 {
1095 type Output = Self;
1096 #[inline]
div(self, rhs: f64) -> Self1097 fn div(self, rhs: f64) -> Self {
1098 Self {
1099 x: self.x.div(rhs),
1100 y: self.y.div(rhs),
1101 z: self.z.div(rhs),
1102 w: self.w.div(rhs),
1103 }
1104 }
1105 }
1106
1107 impl Div<&f64> for DVec4 {
1108 type Output = DVec4;
1109 #[inline]
div(self, rhs: &f64) -> DVec41110 fn div(self, rhs: &f64) -> DVec4 {
1111 self.div(*rhs)
1112 }
1113 }
1114
1115 impl Div<&f64> for &DVec4 {
1116 type Output = DVec4;
1117 #[inline]
div(self, rhs: &f64) -> DVec41118 fn div(self, rhs: &f64) -> DVec4 {
1119 (*self).div(*rhs)
1120 }
1121 }
1122
1123 impl Div<f64> for &DVec4 {
1124 type Output = DVec4;
1125 #[inline]
div(self, rhs: f64) -> DVec41126 fn div(self, rhs: f64) -> DVec4 {
1127 (*self).div(rhs)
1128 }
1129 }
1130
1131 impl DivAssign<f64> for DVec4 {
1132 #[inline]
div_assign(&mut self, rhs: f64)1133 fn div_assign(&mut self, rhs: f64) {
1134 self.x.div_assign(rhs);
1135 self.y.div_assign(rhs);
1136 self.z.div_assign(rhs);
1137 self.w.div_assign(rhs);
1138 }
1139 }
1140
1141 impl DivAssign<&f64> for DVec4 {
1142 #[inline]
div_assign(&mut self, rhs: &f64)1143 fn div_assign(&mut self, rhs: &f64) {
1144 self.div_assign(*rhs)
1145 }
1146 }
1147
1148 impl Div<DVec4> for f64 {
1149 type Output = DVec4;
1150 #[inline]
div(self, rhs: DVec4) -> DVec41151 fn div(self, rhs: DVec4) -> DVec4 {
1152 DVec4 {
1153 x: self.div(rhs.x),
1154 y: self.div(rhs.y),
1155 z: self.div(rhs.z),
1156 w: self.div(rhs.w),
1157 }
1158 }
1159 }
1160
1161 impl Div<&DVec4> for f64 {
1162 type Output = DVec4;
1163 #[inline]
div(self, rhs: &DVec4) -> DVec41164 fn div(self, rhs: &DVec4) -> DVec4 {
1165 self.div(*rhs)
1166 }
1167 }
1168
1169 impl Div<&DVec4> for &f64 {
1170 type Output = DVec4;
1171 #[inline]
div(self, rhs: &DVec4) -> DVec41172 fn div(self, rhs: &DVec4) -> DVec4 {
1173 (*self).div(*rhs)
1174 }
1175 }
1176
1177 impl Div<DVec4> for &f64 {
1178 type Output = DVec4;
1179 #[inline]
div(self, rhs: DVec4) -> DVec41180 fn div(self, rhs: DVec4) -> DVec4 {
1181 (*self).div(rhs)
1182 }
1183 }
1184
1185 impl Mul<DVec4> for DVec4 {
1186 type Output = Self;
1187 #[inline]
mul(self, rhs: Self) -> Self1188 fn mul(self, rhs: Self) -> Self {
1189 Self {
1190 x: self.x.mul(rhs.x),
1191 y: self.y.mul(rhs.y),
1192 z: self.z.mul(rhs.z),
1193 w: self.w.mul(rhs.w),
1194 }
1195 }
1196 }
1197
1198 impl Mul<&DVec4> for DVec4 {
1199 type Output = DVec4;
1200 #[inline]
mul(self, rhs: &DVec4) -> DVec41201 fn mul(self, rhs: &DVec4) -> DVec4 {
1202 self.mul(*rhs)
1203 }
1204 }
1205
1206 impl Mul<&DVec4> for &DVec4 {
1207 type Output = DVec4;
1208 #[inline]
mul(self, rhs: &DVec4) -> DVec41209 fn mul(self, rhs: &DVec4) -> DVec4 {
1210 (*self).mul(*rhs)
1211 }
1212 }
1213
1214 impl Mul<DVec4> for &DVec4 {
1215 type Output = DVec4;
1216 #[inline]
mul(self, rhs: DVec4) -> DVec41217 fn mul(self, rhs: DVec4) -> DVec4 {
1218 (*self).mul(rhs)
1219 }
1220 }
1221
1222 impl MulAssign<DVec4> for DVec4 {
1223 #[inline]
mul_assign(&mut self, rhs: Self)1224 fn mul_assign(&mut self, rhs: Self) {
1225 self.x.mul_assign(rhs.x);
1226 self.y.mul_assign(rhs.y);
1227 self.z.mul_assign(rhs.z);
1228 self.w.mul_assign(rhs.w);
1229 }
1230 }
1231
1232 impl MulAssign<&DVec4> for DVec4 {
1233 #[inline]
mul_assign(&mut self, rhs: &DVec4)1234 fn mul_assign(&mut self, rhs: &DVec4) {
1235 self.mul_assign(*rhs)
1236 }
1237 }
1238
1239 impl Mul<f64> for DVec4 {
1240 type Output = Self;
1241 #[inline]
mul(self, rhs: f64) -> Self1242 fn mul(self, rhs: f64) -> Self {
1243 Self {
1244 x: self.x.mul(rhs),
1245 y: self.y.mul(rhs),
1246 z: self.z.mul(rhs),
1247 w: self.w.mul(rhs),
1248 }
1249 }
1250 }
1251
1252 impl Mul<&f64> for DVec4 {
1253 type Output = DVec4;
1254 #[inline]
mul(self, rhs: &f64) -> DVec41255 fn mul(self, rhs: &f64) -> DVec4 {
1256 self.mul(*rhs)
1257 }
1258 }
1259
1260 impl Mul<&f64> for &DVec4 {
1261 type Output = DVec4;
1262 #[inline]
mul(self, rhs: &f64) -> DVec41263 fn mul(self, rhs: &f64) -> DVec4 {
1264 (*self).mul(*rhs)
1265 }
1266 }
1267
1268 impl Mul<f64> for &DVec4 {
1269 type Output = DVec4;
1270 #[inline]
mul(self, rhs: f64) -> DVec41271 fn mul(self, rhs: f64) -> DVec4 {
1272 (*self).mul(rhs)
1273 }
1274 }
1275
1276 impl MulAssign<f64> for DVec4 {
1277 #[inline]
mul_assign(&mut self, rhs: f64)1278 fn mul_assign(&mut self, rhs: f64) {
1279 self.x.mul_assign(rhs);
1280 self.y.mul_assign(rhs);
1281 self.z.mul_assign(rhs);
1282 self.w.mul_assign(rhs);
1283 }
1284 }
1285
1286 impl MulAssign<&f64> for DVec4 {
1287 #[inline]
mul_assign(&mut self, rhs: &f64)1288 fn mul_assign(&mut self, rhs: &f64) {
1289 self.mul_assign(*rhs)
1290 }
1291 }
1292
1293 impl Mul<DVec4> for f64 {
1294 type Output = DVec4;
1295 #[inline]
mul(self, rhs: DVec4) -> DVec41296 fn mul(self, rhs: DVec4) -> DVec4 {
1297 DVec4 {
1298 x: self.mul(rhs.x),
1299 y: self.mul(rhs.y),
1300 z: self.mul(rhs.z),
1301 w: self.mul(rhs.w),
1302 }
1303 }
1304 }
1305
1306 impl Mul<&DVec4> for f64 {
1307 type Output = DVec4;
1308 #[inline]
mul(self, rhs: &DVec4) -> DVec41309 fn mul(self, rhs: &DVec4) -> DVec4 {
1310 self.mul(*rhs)
1311 }
1312 }
1313
1314 impl Mul<&DVec4> for &f64 {
1315 type Output = DVec4;
1316 #[inline]
mul(self, rhs: &DVec4) -> DVec41317 fn mul(self, rhs: &DVec4) -> DVec4 {
1318 (*self).mul(*rhs)
1319 }
1320 }
1321
1322 impl Mul<DVec4> for &f64 {
1323 type Output = DVec4;
1324 #[inline]
mul(self, rhs: DVec4) -> DVec41325 fn mul(self, rhs: DVec4) -> DVec4 {
1326 (*self).mul(rhs)
1327 }
1328 }
1329
1330 impl Add<DVec4> for DVec4 {
1331 type Output = Self;
1332 #[inline]
add(self, rhs: Self) -> Self1333 fn add(self, rhs: Self) -> Self {
1334 Self {
1335 x: self.x.add(rhs.x),
1336 y: self.y.add(rhs.y),
1337 z: self.z.add(rhs.z),
1338 w: self.w.add(rhs.w),
1339 }
1340 }
1341 }
1342
1343 impl Add<&DVec4> for DVec4 {
1344 type Output = DVec4;
1345 #[inline]
add(self, rhs: &DVec4) -> DVec41346 fn add(self, rhs: &DVec4) -> DVec4 {
1347 self.add(*rhs)
1348 }
1349 }
1350
1351 impl Add<&DVec4> for &DVec4 {
1352 type Output = DVec4;
1353 #[inline]
add(self, rhs: &DVec4) -> DVec41354 fn add(self, rhs: &DVec4) -> DVec4 {
1355 (*self).add(*rhs)
1356 }
1357 }
1358
1359 impl Add<DVec4> for &DVec4 {
1360 type Output = DVec4;
1361 #[inline]
add(self, rhs: DVec4) -> DVec41362 fn add(self, rhs: DVec4) -> DVec4 {
1363 (*self).add(rhs)
1364 }
1365 }
1366
1367 impl AddAssign<DVec4> for DVec4 {
1368 #[inline]
add_assign(&mut self, rhs: Self)1369 fn add_assign(&mut self, rhs: Self) {
1370 self.x.add_assign(rhs.x);
1371 self.y.add_assign(rhs.y);
1372 self.z.add_assign(rhs.z);
1373 self.w.add_assign(rhs.w);
1374 }
1375 }
1376
1377 impl AddAssign<&DVec4> for DVec4 {
1378 #[inline]
add_assign(&mut self, rhs: &DVec4)1379 fn add_assign(&mut self, rhs: &DVec4) {
1380 self.add_assign(*rhs)
1381 }
1382 }
1383
1384 impl Add<f64> for DVec4 {
1385 type Output = Self;
1386 #[inline]
add(self, rhs: f64) -> Self1387 fn add(self, rhs: f64) -> Self {
1388 Self {
1389 x: self.x.add(rhs),
1390 y: self.y.add(rhs),
1391 z: self.z.add(rhs),
1392 w: self.w.add(rhs),
1393 }
1394 }
1395 }
1396
1397 impl Add<&f64> for DVec4 {
1398 type Output = DVec4;
1399 #[inline]
add(self, rhs: &f64) -> DVec41400 fn add(self, rhs: &f64) -> DVec4 {
1401 self.add(*rhs)
1402 }
1403 }
1404
1405 impl Add<&f64> for &DVec4 {
1406 type Output = DVec4;
1407 #[inline]
add(self, rhs: &f64) -> DVec41408 fn add(self, rhs: &f64) -> DVec4 {
1409 (*self).add(*rhs)
1410 }
1411 }
1412
1413 impl Add<f64> for &DVec4 {
1414 type Output = DVec4;
1415 #[inline]
add(self, rhs: f64) -> DVec41416 fn add(self, rhs: f64) -> DVec4 {
1417 (*self).add(rhs)
1418 }
1419 }
1420
1421 impl AddAssign<f64> for DVec4 {
1422 #[inline]
add_assign(&mut self, rhs: f64)1423 fn add_assign(&mut self, rhs: f64) {
1424 self.x.add_assign(rhs);
1425 self.y.add_assign(rhs);
1426 self.z.add_assign(rhs);
1427 self.w.add_assign(rhs);
1428 }
1429 }
1430
1431 impl AddAssign<&f64> for DVec4 {
1432 #[inline]
add_assign(&mut self, rhs: &f64)1433 fn add_assign(&mut self, rhs: &f64) {
1434 self.add_assign(*rhs)
1435 }
1436 }
1437
1438 impl Add<DVec4> for f64 {
1439 type Output = DVec4;
1440 #[inline]
add(self, rhs: DVec4) -> DVec41441 fn add(self, rhs: DVec4) -> DVec4 {
1442 DVec4 {
1443 x: self.add(rhs.x),
1444 y: self.add(rhs.y),
1445 z: self.add(rhs.z),
1446 w: self.add(rhs.w),
1447 }
1448 }
1449 }
1450
1451 impl Add<&DVec4> for f64 {
1452 type Output = DVec4;
1453 #[inline]
add(self, rhs: &DVec4) -> DVec41454 fn add(self, rhs: &DVec4) -> DVec4 {
1455 self.add(*rhs)
1456 }
1457 }
1458
1459 impl Add<&DVec4> for &f64 {
1460 type Output = DVec4;
1461 #[inline]
add(self, rhs: &DVec4) -> DVec41462 fn add(self, rhs: &DVec4) -> DVec4 {
1463 (*self).add(*rhs)
1464 }
1465 }
1466
1467 impl Add<DVec4> for &f64 {
1468 type Output = DVec4;
1469 #[inline]
add(self, rhs: DVec4) -> DVec41470 fn add(self, rhs: DVec4) -> DVec4 {
1471 (*self).add(rhs)
1472 }
1473 }
1474
1475 impl Sub<DVec4> for DVec4 {
1476 type Output = Self;
1477 #[inline]
sub(self, rhs: Self) -> Self1478 fn sub(self, rhs: Self) -> Self {
1479 Self {
1480 x: self.x.sub(rhs.x),
1481 y: self.y.sub(rhs.y),
1482 z: self.z.sub(rhs.z),
1483 w: self.w.sub(rhs.w),
1484 }
1485 }
1486 }
1487
1488 impl Sub<&DVec4> for DVec4 {
1489 type Output = DVec4;
1490 #[inline]
sub(self, rhs: &DVec4) -> DVec41491 fn sub(self, rhs: &DVec4) -> DVec4 {
1492 self.sub(*rhs)
1493 }
1494 }
1495
1496 impl Sub<&DVec4> for &DVec4 {
1497 type Output = DVec4;
1498 #[inline]
sub(self, rhs: &DVec4) -> DVec41499 fn sub(self, rhs: &DVec4) -> DVec4 {
1500 (*self).sub(*rhs)
1501 }
1502 }
1503
1504 impl Sub<DVec4> for &DVec4 {
1505 type Output = DVec4;
1506 #[inline]
sub(self, rhs: DVec4) -> DVec41507 fn sub(self, rhs: DVec4) -> DVec4 {
1508 (*self).sub(rhs)
1509 }
1510 }
1511
1512 impl SubAssign<DVec4> for DVec4 {
1513 #[inline]
sub_assign(&mut self, rhs: DVec4)1514 fn sub_assign(&mut self, rhs: DVec4) {
1515 self.x.sub_assign(rhs.x);
1516 self.y.sub_assign(rhs.y);
1517 self.z.sub_assign(rhs.z);
1518 self.w.sub_assign(rhs.w);
1519 }
1520 }
1521
1522 impl SubAssign<&DVec4> for DVec4 {
1523 #[inline]
sub_assign(&mut self, rhs: &DVec4)1524 fn sub_assign(&mut self, rhs: &DVec4) {
1525 self.sub_assign(*rhs)
1526 }
1527 }
1528
1529 impl Sub<f64> for DVec4 {
1530 type Output = Self;
1531 #[inline]
sub(self, rhs: f64) -> Self1532 fn sub(self, rhs: f64) -> Self {
1533 Self {
1534 x: self.x.sub(rhs),
1535 y: self.y.sub(rhs),
1536 z: self.z.sub(rhs),
1537 w: self.w.sub(rhs),
1538 }
1539 }
1540 }
1541
1542 impl Sub<&f64> for DVec4 {
1543 type Output = DVec4;
1544 #[inline]
sub(self, rhs: &f64) -> DVec41545 fn sub(self, rhs: &f64) -> DVec4 {
1546 self.sub(*rhs)
1547 }
1548 }
1549
1550 impl Sub<&f64> for &DVec4 {
1551 type Output = DVec4;
1552 #[inline]
sub(self, rhs: &f64) -> DVec41553 fn sub(self, rhs: &f64) -> DVec4 {
1554 (*self).sub(*rhs)
1555 }
1556 }
1557
1558 impl Sub<f64> for &DVec4 {
1559 type Output = DVec4;
1560 #[inline]
sub(self, rhs: f64) -> DVec41561 fn sub(self, rhs: f64) -> DVec4 {
1562 (*self).sub(rhs)
1563 }
1564 }
1565
1566 impl SubAssign<f64> for DVec4 {
1567 #[inline]
sub_assign(&mut self, rhs: f64)1568 fn sub_assign(&mut self, rhs: f64) {
1569 self.x.sub_assign(rhs);
1570 self.y.sub_assign(rhs);
1571 self.z.sub_assign(rhs);
1572 self.w.sub_assign(rhs);
1573 }
1574 }
1575
1576 impl SubAssign<&f64> for DVec4 {
1577 #[inline]
sub_assign(&mut self, rhs: &f64)1578 fn sub_assign(&mut self, rhs: &f64) {
1579 self.sub_assign(*rhs)
1580 }
1581 }
1582
1583 impl Sub<DVec4> for f64 {
1584 type Output = DVec4;
1585 #[inline]
sub(self, rhs: DVec4) -> DVec41586 fn sub(self, rhs: DVec4) -> DVec4 {
1587 DVec4 {
1588 x: self.sub(rhs.x),
1589 y: self.sub(rhs.y),
1590 z: self.sub(rhs.z),
1591 w: self.sub(rhs.w),
1592 }
1593 }
1594 }
1595
1596 impl Sub<&DVec4> for f64 {
1597 type Output = DVec4;
1598 #[inline]
sub(self, rhs: &DVec4) -> DVec41599 fn sub(self, rhs: &DVec4) -> DVec4 {
1600 self.sub(*rhs)
1601 }
1602 }
1603
1604 impl Sub<&DVec4> for &f64 {
1605 type Output = DVec4;
1606 #[inline]
sub(self, rhs: &DVec4) -> DVec41607 fn sub(self, rhs: &DVec4) -> DVec4 {
1608 (*self).sub(*rhs)
1609 }
1610 }
1611
1612 impl Sub<DVec4> for &f64 {
1613 type Output = DVec4;
1614 #[inline]
sub(self, rhs: DVec4) -> DVec41615 fn sub(self, rhs: DVec4) -> DVec4 {
1616 (*self).sub(rhs)
1617 }
1618 }
1619
1620 impl Rem<DVec4> for DVec4 {
1621 type Output = Self;
1622 #[inline]
rem(self, rhs: Self) -> Self1623 fn rem(self, rhs: Self) -> Self {
1624 Self {
1625 x: self.x.rem(rhs.x),
1626 y: self.y.rem(rhs.y),
1627 z: self.z.rem(rhs.z),
1628 w: self.w.rem(rhs.w),
1629 }
1630 }
1631 }
1632
1633 impl Rem<&DVec4> for DVec4 {
1634 type Output = DVec4;
1635 #[inline]
rem(self, rhs: &DVec4) -> DVec41636 fn rem(self, rhs: &DVec4) -> DVec4 {
1637 self.rem(*rhs)
1638 }
1639 }
1640
1641 impl Rem<&DVec4> for &DVec4 {
1642 type Output = DVec4;
1643 #[inline]
rem(self, rhs: &DVec4) -> DVec41644 fn rem(self, rhs: &DVec4) -> DVec4 {
1645 (*self).rem(*rhs)
1646 }
1647 }
1648
1649 impl Rem<DVec4> for &DVec4 {
1650 type Output = DVec4;
1651 #[inline]
rem(self, rhs: DVec4) -> DVec41652 fn rem(self, rhs: DVec4) -> DVec4 {
1653 (*self).rem(rhs)
1654 }
1655 }
1656
1657 impl RemAssign<DVec4> for DVec4 {
1658 #[inline]
rem_assign(&mut self, rhs: Self)1659 fn rem_assign(&mut self, rhs: Self) {
1660 self.x.rem_assign(rhs.x);
1661 self.y.rem_assign(rhs.y);
1662 self.z.rem_assign(rhs.z);
1663 self.w.rem_assign(rhs.w);
1664 }
1665 }
1666
1667 impl RemAssign<&DVec4> for DVec4 {
1668 #[inline]
rem_assign(&mut self, rhs: &DVec4)1669 fn rem_assign(&mut self, rhs: &DVec4) {
1670 self.rem_assign(*rhs)
1671 }
1672 }
1673
1674 impl Rem<f64> for DVec4 {
1675 type Output = Self;
1676 #[inline]
rem(self, rhs: f64) -> Self1677 fn rem(self, rhs: f64) -> Self {
1678 Self {
1679 x: self.x.rem(rhs),
1680 y: self.y.rem(rhs),
1681 z: self.z.rem(rhs),
1682 w: self.w.rem(rhs),
1683 }
1684 }
1685 }
1686
1687 impl Rem<&f64> for DVec4 {
1688 type Output = DVec4;
1689 #[inline]
rem(self, rhs: &f64) -> DVec41690 fn rem(self, rhs: &f64) -> DVec4 {
1691 self.rem(*rhs)
1692 }
1693 }
1694
1695 impl Rem<&f64> for &DVec4 {
1696 type Output = DVec4;
1697 #[inline]
rem(self, rhs: &f64) -> DVec41698 fn rem(self, rhs: &f64) -> DVec4 {
1699 (*self).rem(*rhs)
1700 }
1701 }
1702
1703 impl Rem<f64> for &DVec4 {
1704 type Output = DVec4;
1705 #[inline]
rem(self, rhs: f64) -> DVec41706 fn rem(self, rhs: f64) -> DVec4 {
1707 (*self).rem(rhs)
1708 }
1709 }
1710
1711 impl RemAssign<f64> for DVec4 {
1712 #[inline]
rem_assign(&mut self, rhs: f64)1713 fn rem_assign(&mut self, rhs: f64) {
1714 self.x.rem_assign(rhs);
1715 self.y.rem_assign(rhs);
1716 self.z.rem_assign(rhs);
1717 self.w.rem_assign(rhs);
1718 }
1719 }
1720
1721 impl RemAssign<&f64> for DVec4 {
1722 #[inline]
rem_assign(&mut self, rhs: &f64)1723 fn rem_assign(&mut self, rhs: &f64) {
1724 self.rem_assign(*rhs)
1725 }
1726 }
1727
1728 impl Rem<DVec4> for f64 {
1729 type Output = DVec4;
1730 #[inline]
rem(self, rhs: DVec4) -> DVec41731 fn rem(self, rhs: DVec4) -> DVec4 {
1732 DVec4 {
1733 x: self.rem(rhs.x),
1734 y: self.rem(rhs.y),
1735 z: self.rem(rhs.z),
1736 w: self.rem(rhs.w),
1737 }
1738 }
1739 }
1740
1741 impl Rem<&DVec4> for f64 {
1742 type Output = DVec4;
1743 #[inline]
rem(self, rhs: &DVec4) -> DVec41744 fn rem(self, rhs: &DVec4) -> DVec4 {
1745 self.rem(*rhs)
1746 }
1747 }
1748
1749 impl Rem<&DVec4> for &f64 {
1750 type Output = DVec4;
1751 #[inline]
rem(self, rhs: &DVec4) -> DVec41752 fn rem(self, rhs: &DVec4) -> DVec4 {
1753 (*self).rem(*rhs)
1754 }
1755 }
1756
1757 impl Rem<DVec4> for &f64 {
1758 type Output = DVec4;
1759 #[inline]
rem(self, rhs: DVec4) -> DVec41760 fn rem(self, rhs: DVec4) -> DVec4 {
1761 (*self).rem(rhs)
1762 }
1763 }
1764
1765 #[cfg(not(target_arch = "spirv"))]
1766 impl AsRef<[f64; 4]> for DVec4 {
1767 #[inline]
as_ref(&self) -> &[f64; 4]1768 fn as_ref(&self) -> &[f64; 4] {
1769 unsafe { &*(self as *const DVec4 as *const [f64; 4]) }
1770 }
1771 }
1772
1773 #[cfg(not(target_arch = "spirv"))]
1774 impl AsMut<[f64; 4]> for DVec4 {
1775 #[inline]
as_mut(&mut self) -> &mut [f64; 4]1776 fn as_mut(&mut self) -> &mut [f64; 4] {
1777 unsafe { &mut *(self as *mut DVec4 as *mut [f64; 4]) }
1778 }
1779 }
1780
1781 impl Sum for DVec4 {
1782 #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,1783 fn sum<I>(iter: I) -> Self
1784 where
1785 I: Iterator<Item = Self>,
1786 {
1787 iter.fold(Self::ZERO, Self::add)
1788 }
1789 }
1790
1791 impl<'a> Sum<&'a Self> for DVec4 {
1792 #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1793 fn sum<I>(iter: I) -> Self
1794 where
1795 I: Iterator<Item = &'a Self>,
1796 {
1797 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1798 }
1799 }
1800
1801 impl Product for DVec4 {
1802 #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = Self>,1803 fn product<I>(iter: I) -> Self
1804 where
1805 I: Iterator<Item = Self>,
1806 {
1807 iter.fold(Self::ONE, Self::mul)
1808 }
1809 }
1810
1811 impl<'a> Product<&'a Self> for DVec4 {
1812 #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1813 fn product<I>(iter: I) -> Self
1814 where
1815 I: Iterator<Item = &'a Self>,
1816 {
1817 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1818 }
1819 }
1820
1821 impl Neg for DVec4 {
1822 type Output = Self;
1823 #[inline]
neg(self) -> Self1824 fn neg(self) -> Self {
1825 Self {
1826 x: self.x.neg(),
1827 y: self.y.neg(),
1828 z: self.z.neg(),
1829 w: self.w.neg(),
1830 }
1831 }
1832 }
1833
1834 impl Neg for &DVec4 {
1835 type Output = DVec4;
1836 #[inline]
neg(self) -> DVec41837 fn neg(self) -> DVec4 {
1838 (*self).neg()
1839 }
1840 }
1841
1842 impl Index<usize> for DVec4 {
1843 type Output = f64;
1844 #[inline]
index(&self, index: usize) -> &Self::Output1845 fn index(&self, index: usize) -> &Self::Output {
1846 match index {
1847 0 => &self.x,
1848 1 => &self.y,
1849 2 => &self.z,
1850 3 => &self.w,
1851 _ => panic!("index out of bounds"),
1852 }
1853 }
1854 }
1855
1856 impl IndexMut<usize> for DVec4 {
1857 #[inline]
index_mut(&mut self, index: usize) -> &mut Self::Output1858 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1859 match index {
1860 0 => &mut self.x,
1861 1 => &mut self.y,
1862 2 => &mut self.z,
1863 3 => &mut self.w,
1864 _ => panic!("index out of bounds"),
1865 }
1866 }
1867 }
1868
1869 impl fmt::Display for DVec4 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1870 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1871 if let Some(p) = f.precision() {
1872 write!(
1873 f,
1874 "[{:.*}, {:.*}, {:.*}, {:.*}]",
1875 p, self.x, p, self.y, p, self.z, p, self.w
1876 )
1877 } else {
1878 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1879 }
1880 }
1881 }
1882
1883 impl fmt::Debug for DVec4 {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result1884 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1885 fmt.debug_tuple(stringify!(DVec4))
1886 .field(&self.x)
1887 .field(&self.y)
1888 .field(&self.z)
1889 .field(&self.w)
1890 .finish()
1891 }
1892 }
1893
1894 impl From<[f64; 4]> for DVec4 {
1895 #[inline]
from(a: [f64; 4]) -> Self1896 fn from(a: [f64; 4]) -> Self {
1897 Self::new(a[0], a[1], a[2], a[3])
1898 }
1899 }
1900
1901 impl From<DVec4> for [f64; 4] {
1902 #[inline]
from(v: DVec4) -> Self1903 fn from(v: DVec4) -> Self {
1904 [v.x, v.y, v.z, v.w]
1905 }
1906 }
1907
1908 impl From<(f64, f64, f64, f64)> for DVec4 {
1909 #[inline]
from(t: (f64, f64, f64, f64)) -> Self1910 fn from(t: (f64, f64, f64, f64)) -> Self {
1911 Self::new(t.0, t.1, t.2, t.3)
1912 }
1913 }
1914
1915 impl From<DVec4> for (f64, f64, f64, f64) {
1916 #[inline]
from(v: DVec4) -> Self1917 fn from(v: DVec4) -> Self {
1918 (v.x, v.y, v.z, v.w)
1919 }
1920 }
1921
1922 impl From<(DVec3, f64)> for DVec4 {
1923 #[inline]
from((v, w): (DVec3, f64)) -> Self1924 fn from((v, w): (DVec3, f64)) -> Self {
1925 Self::new(v.x, v.y, v.z, w)
1926 }
1927 }
1928
1929 impl From<(f64, DVec3)> for DVec4 {
1930 #[inline]
from((x, v): (f64, DVec3)) -> Self1931 fn from((x, v): (f64, DVec3)) -> Self {
1932 Self::new(x, v.x, v.y, v.z)
1933 }
1934 }
1935
1936 impl From<(DVec2, f64, f64)> for DVec4 {
1937 #[inline]
from((v, z, w): (DVec2, f64, f64)) -> Self1938 fn from((v, z, w): (DVec2, f64, f64)) -> Self {
1939 Self::new(v.x, v.y, z, w)
1940 }
1941 }
1942
1943 impl From<(DVec2, DVec2)> for DVec4 {
1944 #[inline]
from((v, u): (DVec2, DVec2)) -> Self1945 fn from((v, u): (DVec2, DVec2)) -> Self {
1946 Self::new(v.x, v.y, u.x, u.y)
1947 }
1948 }
1949
1950 impl From<Vec4> for DVec4 {
1951 #[inline]
from(v: Vec4) -> Self1952 fn from(v: Vec4) -> Self {
1953 Self::new(
1954 f64::from(v.x),
1955 f64::from(v.y),
1956 f64::from(v.z),
1957 f64::from(v.w),
1958 )
1959 }
1960 }
1961
1962 impl From<IVec4> for DVec4 {
1963 #[inline]
from(v: IVec4) -> Self1964 fn from(v: IVec4) -> Self {
1965 Self::new(
1966 f64::from(v.x),
1967 f64::from(v.y),
1968 f64::from(v.z),
1969 f64::from(v.w),
1970 )
1971 }
1972 }
1973
1974 impl From<UVec4> for DVec4 {
1975 #[inline]
from(v: UVec4) -> Self1976 fn from(v: UVec4) -> Self {
1977 Self::new(
1978 f64::from(v.x),
1979 f64::from(v.y),
1980 f64::from(v.z),
1981 f64::from(v.w),
1982 )
1983 }
1984 }
1985
1986 impl From<BVec4> for DVec4 {
1987 #[inline]
from(v: BVec4) -> Self1988 fn from(v: BVec4) -> Self {
1989 Self::new(
1990 f64::from(v.x),
1991 f64::from(v.y),
1992 f64::from(v.z),
1993 f64::from(v.w),
1994 )
1995 }
1996 }
1997
1998 #[cfg(not(feature = "scalar-math"))]
1999 impl From<BVec4A> for DVec4 {
2000 #[inline]
from(v: BVec4A) -> Self2001 fn from(v: BVec4A) -> Self {
2002 let bool_array: [bool; 4] = v.into();
2003 Self::new(
2004 f64::from(bool_array[0]),
2005 f64::from(bool_array[1]),
2006 f64::from(bool_array[2]),
2007 f64::from(bool_array[3]),
2008 )
2009 }
2010 }
2011