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