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