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