| 1 | use super::scalar::{FloatEx, SignedEx}; |
| 2 | use crate::core::storage::{XY, XYZ, XYZW}; |
| 3 | use core::ops::{Add, Mul, Sub}; |
| 4 | |
| 5 | /// Mask vector constants that are independent of vector length |
| 6 | pub trait MaskVectorConst: Sized { |
| 7 | const FALSE: Self; |
| 8 | } |
| 9 | |
| 10 | /// Mask vector methods that are independent of vector dimension. |
| 11 | pub trait MaskVector: MaskVectorConst { |
| 12 | fn bitand(self, other: Self) -> Self; |
| 13 | fn bitor(self, other: Self) -> Self; |
| 14 | fn not(self) -> Self; |
| 15 | } |
| 16 | |
| 17 | /// Mask vector methods specific to 2D vectors. |
| 18 | pub trait MaskVector2: MaskVector { |
| 19 | fn new(x: bool, y: bool) -> Self; |
| 20 | fn bitmask(self) -> u32; |
| 21 | fn any(self) -> bool; |
| 22 | fn all(self) -> bool; |
| 23 | fn into_bool_array(self) -> [bool; 2]; |
| 24 | fn into_u32_array(self) -> [u32; 2]; |
| 25 | } |
| 26 | |
| 27 | /// Mask vector methods specific to 3D vectors. |
| 28 | pub trait MaskVector3: MaskVector { |
| 29 | fn new(x: bool, y: bool, z: bool) -> Self; |
| 30 | fn bitmask(self) -> u32; |
| 31 | fn any(self) -> bool; |
| 32 | fn all(self) -> bool; |
| 33 | fn into_bool_array(self) -> [bool; 3]; |
| 34 | fn into_u32_array(self) -> [u32; 3]; |
| 35 | } |
| 36 | |
| 37 | /// Mask vector methods specific to 4D vectors. |
| 38 | pub trait MaskVector4: MaskVector { |
| 39 | fn new(x: bool, y: bool, z: bool, w: bool) -> Self; |
| 40 | fn bitmask(self) -> u32; |
| 41 | fn any(self) -> bool; |
| 42 | fn all(self) -> bool; |
| 43 | fn into_bool_array(self) -> [bool; 4]; |
| 44 | fn into_u32_array(self) -> [u32; 4]; |
| 45 | } |
| 46 | |
| 47 | /// Vector constants that are independent of vector dimension. |
| 48 | pub trait VectorConst { |
| 49 | const ZERO: Self; |
| 50 | const ONE: Self; |
| 51 | } |
| 52 | |
| 53 | /// Vector constants specific to 2D vectors. |
| 54 | pub trait Vector2Const: VectorConst { |
| 55 | const X: Self; |
| 56 | const Y: Self; |
| 57 | } |
| 58 | |
| 59 | /// Vector constants specific to 3D vectors. |
| 60 | pub trait Vector3Const: VectorConst { |
| 61 | const X: Self; |
| 62 | const Y: Self; |
| 63 | const Z: Self; |
| 64 | } |
| 65 | |
| 66 | /// Vector constants specific to 4D vectors. |
| 67 | pub trait Vector4Const: VectorConst { |
| 68 | const X: Self; |
| 69 | const Y: Self; |
| 70 | const Z: Self; |
| 71 | const W: Self; |
| 72 | } |
| 73 | |
| 74 | /// Vector methods that are independent of vector dimension. |
| 75 | /// |
| 76 | /// These methods typically need to be implemented for each type as while the method signature does |
| 77 | /// not imply any dimensionality, the implemtation does. |
| 78 | pub trait Vector<T>: Sized + Copy + Clone { |
| 79 | type Mask; |
| 80 | |
| 81 | fn splat(s: T) -> Self; |
| 82 | |
| 83 | fn select(mask: Self::Mask, a: Self, b: Self) -> Self; |
| 84 | |
| 85 | fn cmpeq(self, other: Self) -> Self::Mask; |
| 86 | fn cmpne(self, other: Self) -> Self::Mask; |
| 87 | fn cmpge(self, other: Self) -> Self::Mask; |
| 88 | fn cmpgt(self, other: Self) -> Self::Mask; |
| 89 | fn cmple(self, other: Self) -> Self::Mask; |
| 90 | fn cmplt(self, other: Self) -> Self::Mask; |
| 91 | |
| 92 | fn add(self, other: Self) -> Self; |
| 93 | fn div(self, other: Self) -> Self; |
| 94 | fn mul(self, other: Self) -> Self; |
| 95 | fn mul_add(self, a: Self, b: Self) -> Self; |
| 96 | fn sub(self, other: Self) -> Self; |
| 97 | |
| 98 | fn scale(self, other: T) -> Self { |
| 99 | self.mul_scalar(other) |
| 100 | } |
| 101 | |
| 102 | fn mul_scalar(self, other: T) -> Self; |
| 103 | fn div_scalar(self, other: T) -> Self; |
| 104 | |
| 105 | fn min(self, other: Self) -> Self; |
| 106 | fn max(self, other: Self) -> Self; |
| 107 | } |
| 108 | |
| 109 | /// Vector methods specific to 2D vectors. |
| 110 | pub trait Vector2<T>: Vector<T> + Vector2Const |
| 111 | where |
| 112 | T: Copy + Mul<Output = T> + Sub<Output = T> + Add<Output = T>, |
| 113 | { |
| 114 | fn new(x: T, y: T) -> Self; |
| 115 | fn x(self) -> T; |
| 116 | fn y(self) -> T; |
| 117 | |
| 118 | fn as_ref_xy(&self) -> &XY<T>; |
| 119 | fn as_mut_xy(&mut self) -> &mut XY<T>; |
| 120 | |
| 121 | // min and max behave differently for float and integer types in Rust, so we can't have a |
| 122 | // default implementation here. |
| 123 | fn min_element(self) -> T; |
| 124 | fn max_element(self) -> T; |
| 125 | fn clamp(self, min: Self, max: Self) -> Self; |
| 126 | |
| 127 | #[inline(always)] |
| 128 | fn splat_x(self) -> Self { |
| 129 | Self::splat(self.x()) |
| 130 | } |
| 131 | |
| 132 | #[inline(always)] |
| 133 | fn splat_y(self) -> Self { |
| 134 | Self::splat(self.y()) |
| 135 | } |
| 136 | |
| 137 | #[inline(always)] |
| 138 | fn from_slice_unaligned(slice: &[T]) -> Self { |
| 139 | Self::new(slice[0], slice[1]) |
| 140 | } |
| 141 | |
| 142 | #[inline(always)] |
| 143 | fn write_to_slice_unaligned(self, slice: &mut [T]) { |
| 144 | slice[0] = self.x(); |
| 145 | slice[1] = self.y(); |
| 146 | } |
| 147 | |
| 148 | #[inline(always)] |
| 149 | fn into_xyz(self, z: T) -> XYZ<T> { |
| 150 | XYZ { |
| 151 | x: self.x(), |
| 152 | y: self.y(), |
| 153 | z, |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #[inline(always)] |
| 158 | fn into_xyzw(self, z: T, w: T) -> XYZW<T> { |
| 159 | XYZW { |
| 160 | x: self.x(), |
| 161 | y: self.y(), |
| 162 | z, |
| 163 | w, |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | #[inline(always)] |
| 168 | fn from_array(a: [T; 2]) -> Self { |
| 169 | Self::new(a[0], a[1]) |
| 170 | } |
| 171 | |
| 172 | #[inline(always)] |
| 173 | fn into_array(self) -> [T; 2] { |
| 174 | [self.x(), self.y()] |
| 175 | } |
| 176 | |
| 177 | #[inline(always)] |
| 178 | fn from_tuple(t: (T, T)) -> Self { |
| 179 | Self::new(t.0, t.1) |
| 180 | } |
| 181 | |
| 182 | #[inline(always)] |
| 183 | fn into_tuple(self) -> (T, T) { |
| 184 | (self.x(), self.y()) |
| 185 | } |
| 186 | |
| 187 | #[inline] |
| 188 | fn dot(self, other: Self) -> T { |
| 189 | (self.x() * other.x()) + (self.y() * other.y()) |
| 190 | } |
| 191 | |
| 192 | #[inline(always)] |
| 193 | fn dot_into_vec(self, other: Self) -> Self { |
| 194 | Self::splat(self.dot(other)) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// Vector methods specific to 3D vectors. |
| 199 | pub trait Vector3<T>: Vector<T> + Vector3Const |
| 200 | where |
| 201 | T: Copy + Mul<Output = T> + Sub<Output = T> + Add<Output = T>, |
| 202 | { |
| 203 | fn new(x: T, y: T, z: T) -> Self; |
| 204 | fn x(self) -> T; |
| 205 | fn y(self) -> T; |
| 206 | fn z(self) -> T; |
| 207 | |
| 208 | fn as_ref_xyz(&self) -> &XYZ<T>; |
| 209 | fn as_mut_xyz(&mut self) -> &mut XYZ<T>; |
| 210 | |
| 211 | fn min_element(self) -> T; |
| 212 | fn max_element(self) -> T; |
| 213 | fn clamp(self, min: Self, max: Self) -> Self; |
| 214 | |
| 215 | #[inline(always)] |
| 216 | fn splat_x(self) -> Self { |
| 217 | Self::splat(self.x()) |
| 218 | } |
| 219 | |
| 220 | #[inline(always)] |
| 221 | fn splat_y(self) -> Self { |
| 222 | Self::splat(self.y()) |
| 223 | } |
| 224 | |
| 225 | #[inline(always)] |
| 226 | fn splat_z(self) -> Self { |
| 227 | Self::splat(self.z()) |
| 228 | } |
| 229 | |
| 230 | #[inline(always)] |
| 231 | fn from_slice_unaligned(slice: &[T]) -> Self { |
| 232 | Self::new(slice[0], slice[1], slice[2]) |
| 233 | } |
| 234 | |
| 235 | #[inline(always)] |
| 236 | fn write_to_slice_unaligned(self, slice: &mut [T]) { |
| 237 | slice[0] = self.x(); |
| 238 | slice[1] = self.y(); |
| 239 | slice[2] = self.z(); |
| 240 | } |
| 241 | |
| 242 | #[inline(always)] |
| 243 | fn from_xy(v2: XY<T>, z: T) -> Self { |
| 244 | Self::new(v2.x, v2.y, z) |
| 245 | } |
| 246 | |
| 247 | #[inline(always)] |
| 248 | fn from_xyzw(v4: XYZW<T>) -> Self { |
| 249 | Self::new(v4.x, v4.y, v4.z) |
| 250 | } |
| 251 | |
| 252 | #[inline(always)] |
| 253 | fn into_xy(self) -> XY<T> { |
| 254 | XY { |
| 255 | x: self.x(), |
| 256 | y: self.y(), |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | #[inline(always)] |
| 261 | fn into_xyzw(self, w: T) -> XYZW<T> { |
| 262 | XYZW { |
| 263 | x: self.x(), |
| 264 | y: self.y(), |
| 265 | z: self.z(), |
| 266 | w, |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | #[inline(always)] |
| 271 | fn from_array(a: [T; 3]) -> Self { |
| 272 | Self::new(a[0], a[1], a[2]) |
| 273 | } |
| 274 | |
| 275 | #[inline(always)] |
| 276 | fn into_array(self) -> [T; 3] { |
| 277 | [self.x(), self.y(), self.z()] |
| 278 | } |
| 279 | |
| 280 | #[inline(always)] |
| 281 | fn from_tuple(t: (T, T, T)) -> Self { |
| 282 | Self::new(t.0, t.1, t.2) |
| 283 | } |
| 284 | |
| 285 | #[inline(always)] |
| 286 | fn into_tuple(self) -> (T, T, T) { |
| 287 | (self.x(), self.y(), self.z()) |
| 288 | } |
| 289 | |
| 290 | #[inline] |
| 291 | fn dot(self, other: Self) -> T { |
| 292 | (self.x() * other.x()) + (self.y() * other.y()) + (self.z() * other.z()) |
| 293 | } |
| 294 | |
| 295 | #[inline(always)] |
| 296 | fn dot_into_vec(self, other: Self) -> Self { |
| 297 | Self::splat(self.dot(other)) |
| 298 | } |
| 299 | |
| 300 | #[inline] |
| 301 | fn cross(self, other: Self) -> Self { |
| 302 | Self::new( |
| 303 | self.y() * other.z() - other.y() * self.z(), |
| 304 | self.z() * other.x() - other.z() * self.x(), |
| 305 | self.x() * other.y() - other.x() * self.y(), |
| 306 | ) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /// Vector methods specific to 3D vectors. |
| 311 | pub trait Vector4<T>: Vector<T> + Vector4Const |
| 312 | where |
| 313 | T: Copy + Mul<Output = T> + Sub<Output = T> + Add<Output = T>, |
| 314 | { |
| 315 | fn new(x: T, y: T, z: T, w: T) -> Self; |
| 316 | |
| 317 | fn x(self) -> T; |
| 318 | fn y(self) -> T; |
| 319 | fn z(self) -> T; |
| 320 | fn w(self) -> T; |
| 321 | |
| 322 | fn as_ref_xyzw(&self) -> &XYZW<T>; |
| 323 | fn as_mut_xyzw(&mut self) -> &mut XYZW<T>; |
| 324 | |
| 325 | fn min_element(self) -> T; |
| 326 | fn max_element(self) -> T; |
| 327 | fn clamp(self, min: Self, max: Self) -> Self; |
| 328 | |
| 329 | #[inline(always)] |
| 330 | fn splat_x(self) -> Self { |
| 331 | Self::splat(self.x()) |
| 332 | } |
| 333 | |
| 334 | #[inline(always)] |
| 335 | fn splat_y(self) -> Self { |
| 336 | Self::splat(self.y()) |
| 337 | } |
| 338 | |
| 339 | #[inline(always)] |
| 340 | fn splat_z(self) -> Self { |
| 341 | Self::splat(self.z()) |
| 342 | } |
| 343 | |
| 344 | #[inline(always)] |
| 345 | fn splat_w(self) -> Self { |
| 346 | Self::splat(self.w()) |
| 347 | } |
| 348 | |
| 349 | #[inline(always)] |
| 350 | fn from_slice_unaligned(slice: &[T]) -> Self { |
| 351 | Self::new(slice[0], slice[1], slice[2], slice[3]) |
| 352 | } |
| 353 | |
| 354 | #[inline(always)] |
| 355 | fn write_to_slice_unaligned(self, slice: &mut [T]) { |
| 356 | slice[0] = self.x(); |
| 357 | slice[1] = self.y(); |
| 358 | slice[2] = self.z(); |
| 359 | slice[3] = self.w(); |
| 360 | } |
| 361 | |
| 362 | #[inline(always)] |
| 363 | fn from_xy(v2: XY<T>, z: T, w: T) -> Self { |
| 364 | Self::new(v2.x, v2.y, z, w) |
| 365 | } |
| 366 | |
| 367 | #[inline(always)] |
| 368 | fn from_xyz(v3: XYZ<T>, w: T) -> Self { |
| 369 | Self::new(v3.x, v3.y, v3.z, w) |
| 370 | } |
| 371 | |
| 372 | #[inline(always)] |
| 373 | fn into_xy(self) -> XY<T> { |
| 374 | XY { |
| 375 | x: self.x(), |
| 376 | y: self.y(), |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | #[inline(always)] |
| 381 | fn into_xyz(self) -> XYZ<T> { |
| 382 | XYZ { |
| 383 | x: self.x(), |
| 384 | y: self.y(), |
| 385 | z: self.z(), |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | #[inline(always)] |
| 390 | fn from_array(a: [T; 4]) -> Self { |
| 391 | Self::new(a[0], a[1], a[2], a[3]) |
| 392 | } |
| 393 | |
| 394 | #[inline(always)] |
| 395 | fn into_array(self) -> [T; 4] { |
| 396 | [self.x(), self.y(), self.z(), self.w()] |
| 397 | } |
| 398 | |
| 399 | #[inline(always)] |
| 400 | fn from_tuple(t: (T, T, T, T)) -> Self { |
| 401 | Self::new(t.0, t.1, t.2, t.3) |
| 402 | } |
| 403 | |
| 404 | #[inline(always)] |
| 405 | fn into_tuple(self) -> (T, T, T, T) { |
| 406 | (self.x(), self.y(), self.z(), self.w()) |
| 407 | } |
| 408 | |
| 409 | #[inline] |
| 410 | fn dot(self, other: Self) -> T { |
| 411 | (self.x() * other.x()) |
| 412 | + (self.y() * other.y()) |
| 413 | + (self.z() * other.z()) |
| 414 | + (self.w() * other.w()) |
| 415 | } |
| 416 | |
| 417 | #[inline(always)] |
| 418 | fn dot_into_vec(self, other: Self) -> Self { |
| 419 | Self::splat(self.dot(other)) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /// Vector methods for vectors of signed types that are independent of vector dimension. |
| 424 | /// |
| 425 | /// These methods typically need to be implemented for each type as while the method signature does |
| 426 | /// not imply any dimensionality, the implemtation does. |
| 427 | pub trait SignedVector<T: SignedEx>: Vector<T> { |
| 428 | fn neg(self) -> Self; |
| 429 | } |
| 430 | |
| 431 | /// Vector methods specific to 2D vectors of signed types. |
| 432 | pub trait SignedVector2<T: SignedEx>: SignedVector<T> + Vector2<T> { |
| 433 | #[inline] |
| 434 | fn abs(self) -> Self { |
| 435 | Self::new(self.x().abs(), self.y().abs()) |
| 436 | } |
| 437 | |
| 438 | #[inline] |
| 439 | fn signum(self) -> Self { |
| 440 | Self::new(self.x().signum(), self.y().signum()) |
| 441 | } |
| 442 | |
| 443 | #[inline] |
| 444 | fn perp(self) -> Self { |
| 445 | Self::new(-self.y(), self.x()) |
| 446 | } |
| 447 | |
| 448 | #[inline] |
| 449 | fn perp_dot(self, other: Self) -> T { |
| 450 | (self.x() * other.y()) - (self.y() * other.x()) |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /// Vector methods specific to 3D vectors of signed types. |
| 455 | pub trait SignedVector3<T: SignedEx>: SignedVector<T> + Vector3<T> { |
| 456 | #[inline] |
| 457 | fn abs(self) -> Self { |
| 458 | Self::new(self.x().abs(), self.y().abs(), self.z().abs()) |
| 459 | } |
| 460 | |
| 461 | #[inline] |
| 462 | fn signum(self) -> Self { |
| 463 | Self::new(self.x().signum(), self.y().signum(), self.z().signum()) |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | pub trait SignedVector4<T: SignedEx>: SignedVector<T> + Vector4<T> { |
| 468 | #[inline] |
| 469 | fn abs(self) -> Self { |
| 470 | Self::new( |
| 471 | self.x().abs(), |
| 472 | self.y().abs(), |
| 473 | self.z().abs(), |
| 474 | self.w().abs(), |
| 475 | ) |
| 476 | } |
| 477 | |
| 478 | #[inline] |
| 479 | fn signum(self) -> Self { |
| 480 | Self::new( |
| 481 | self.x().signum(), |
| 482 | self.y().signum(), |
| 483 | self.z().signum(), |
| 484 | self.w().signum(), |
| 485 | ) |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | pub trait FloatVector2<T: FloatEx>: SignedVector2<T> { |
| 490 | #[inline] |
| 491 | fn floor(self) -> Self { |
| 492 | Self::new(self.x().floor(), self.y().floor()) |
| 493 | } |
| 494 | |
| 495 | #[inline] |
| 496 | fn ceil(self) -> Self { |
| 497 | Self::new(self.x().ceil(), self.y().ceil()) |
| 498 | } |
| 499 | |
| 500 | #[inline] |
| 501 | fn round(self) -> Self { |
| 502 | Self::new(self.x().round(), self.y().round()) |
| 503 | } |
| 504 | |
| 505 | #[inline] |
| 506 | fn recip(self) -> Self { |
| 507 | Self::new(self.x().recip(), self.y().recip()) |
| 508 | } |
| 509 | |
| 510 | #[inline] |
| 511 | fn exp(self) -> Self { |
| 512 | Self::new(self.x().exp(), self.y().exp()) |
| 513 | } |
| 514 | |
| 515 | #[inline] |
| 516 | fn powf(self, n: T) -> Self { |
| 517 | Self::new(self.x().powf(n), self.y().powf(n)) |
| 518 | } |
| 519 | |
| 520 | #[inline] |
| 521 | fn is_finite(self) -> bool { |
| 522 | self.x().is_finite() && self.y().is_finite() |
| 523 | } |
| 524 | |
| 525 | #[inline] |
| 526 | fn is_nan(self) -> bool { |
| 527 | self.x().is_nan() || self.y().is_nan() |
| 528 | } |
| 529 | |
| 530 | #[inline] |
| 531 | fn is_nan_mask(self) -> Self::Mask |
| 532 | where |
| 533 | <Self as Vector<T>>::Mask: MaskVector2, |
| 534 | { |
| 535 | Self::Mask::new(self.x().is_nan(), self.y().is_nan()) |
| 536 | } |
| 537 | |
| 538 | #[inline] |
| 539 | fn length(self) -> T { |
| 540 | self.dot(self).sqrt() |
| 541 | } |
| 542 | |
| 543 | #[inline] |
| 544 | fn length_recip(self) -> T { |
| 545 | self.length().recip() |
| 546 | } |
| 547 | |
| 548 | #[inline] |
| 549 | fn normalize(self) -> Self { |
| 550 | self.mul_scalar(self.length_recip()) |
| 551 | } |
| 552 | |
| 553 | #[inline(always)] |
| 554 | fn length_squared(self) -> T { |
| 555 | self.dot(self) |
| 556 | } |
| 557 | |
| 558 | #[inline] |
| 559 | fn is_normalized(self) -> bool { |
| 560 | // TODO: do something with epsilon |
| 561 | (self.length_squared() - T::ONE).abs() <= T::from_f64(1e-6) |
| 562 | } |
| 563 | |
| 564 | #[inline] |
| 565 | fn abs_diff_eq(self, other: Self, max_abs_diff: T) -> bool |
| 566 | where |
| 567 | <Self as Vector<T>>::Mask: MaskVector2, |
| 568 | { |
| 569 | self.sub(other).abs().cmple(Self::splat(max_abs_diff)).all() |
| 570 | } |
| 571 | |
| 572 | #[inline] |
| 573 | fn angle_between(self, other: Self) -> T { |
| 574 | let angle = (self.dot(other) / (self.length_squared() * other.length_squared()).sqrt()) |
| 575 | .acos_approx(); |
| 576 | |
| 577 | if self.perp_dot(other) < T::ZERO { |
| 578 | -angle |
| 579 | } else { |
| 580 | angle |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | pub trait FloatVector3<T: FloatEx>: SignedVector3<T> { |
| 586 | #[inline] |
| 587 | fn floor(self) -> Self { |
| 588 | Self::new(self.x().floor(), self.y().floor(), self.z().floor()) |
| 589 | } |
| 590 | |
| 591 | #[inline] |
| 592 | fn ceil(self) -> Self { |
| 593 | Self::new(self.x().ceil(), self.y().ceil(), self.z().ceil()) |
| 594 | } |
| 595 | |
| 596 | #[inline] |
| 597 | fn round(self) -> Self { |
| 598 | Self::new(self.x().round(), self.y().round(), self.z().round()) |
| 599 | } |
| 600 | |
| 601 | #[inline] |
| 602 | fn recip(self) -> Self { |
| 603 | Self::new(self.x().recip(), self.y().recip(), self.z().recip()) |
| 604 | } |
| 605 | |
| 606 | #[inline] |
| 607 | fn exp(self) -> Self { |
| 608 | Self::new(self.x().exp(), self.y().exp(), self.z().exp()) |
| 609 | } |
| 610 | |
| 611 | #[inline] |
| 612 | fn powf(self, n: T) -> Self { |
| 613 | Self::new(self.x().powf(n), self.y().powf(n), self.z().powf(n)) |
| 614 | } |
| 615 | |
| 616 | #[inline] |
| 617 | fn is_finite(self) -> bool { |
| 618 | self.x().is_finite() && self.y().is_finite() && self.z().is_finite() |
| 619 | } |
| 620 | |
| 621 | #[inline] |
| 622 | fn is_nan(self) -> bool { |
| 623 | self.x().is_nan() || self.y().is_nan() || self.z().is_nan() |
| 624 | } |
| 625 | |
| 626 | #[inline] |
| 627 | fn is_nan_mask(self) -> Self::Mask |
| 628 | where |
| 629 | <Self as Vector<T>>::Mask: MaskVector3, |
| 630 | { |
| 631 | Self::Mask::new(self.x().is_nan(), self.y().is_nan(), self.z().is_nan()) |
| 632 | } |
| 633 | |
| 634 | #[inline] |
| 635 | fn length(self) -> T { |
| 636 | self.dot(self).sqrt() |
| 637 | } |
| 638 | |
| 639 | #[inline] |
| 640 | fn length_recip(self) -> T { |
| 641 | self.length().recip() |
| 642 | } |
| 643 | |
| 644 | #[inline] |
| 645 | fn normalize(self) -> Self { |
| 646 | self.mul_scalar(self.length_recip()) |
| 647 | } |
| 648 | |
| 649 | #[inline(always)] |
| 650 | fn length_squared(self) -> T { |
| 651 | self.dot(self) |
| 652 | } |
| 653 | |
| 654 | #[inline] |
| 655 | fn is_normalized(self) -> bool { |
| 656 | // TODO: do something with epsilon |
| 657 | (self.length_squared() - T::ONE).abs() <= T::from_f64(1e-6) |
| 658 | } |
| 659 | |
| 660 | #[inline] |
| 661 | fn abs_diff_eq(self, other: Self, max_abs_diff: T) -> bool |
| 662 | where |
| 663 | <Self as Vector<T>>::Mask: MaskVector3, |
| 664 | { |
| 665 | self.sub(other).abs().cmple(Self::splat(max_abs_diff)).all() |
| 666 | } |
| 667 | |
| 668 | fn angle_between(self, other: Self) -> T { |
| 669 | self.dot(other) |
| 670 | .div(self.length_squared().mul(other.length_squared()).sqrt()) |
| 671 | .acos_approx() |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | pub trait FloatVector4<T: FloatEx>: SignedVector4<T> { |
| 676 | #[inline] |
| 677 | fn floor(self) -> Self { |
| 678 | Self::new( |
| 679 | self.x().floor(), |
| 680 | self.y().floor(), |
| 681 | self.z().floor(), |
| 682 | self.w().floor(), |
| 683 | ) |
| 684 | } |
| 685 | |
| 686 | #[inline] |
| 687 | fn ceil(self) -> Self { |
| 688 | Self::new( |
| 689 | self.x().ceil(), |
| 690 | self.y().ceil(), |
| 691 | self.z().ceil(), |
| 692 | self.w().ceil(), |
| 693 | ) |
| 694 | } |
| 695 | |
| 696 | #[inline] |
| 697 | fn round(self) -> Self { |
| 698 | Self::new( |
| 699 | self.x().round(), |
| 700 | self.y().round(), |
| 701 | self.z().round(), |
| 702 | self.w().round(), |
| 703 | ) |
| 704 | } |
| 705 | |
| 706 | #[inline] |
| 707 | fn recip(self) -> Self { |
| 708 | Self::new( |
| 709 | self.x().recip(), |
| 710 | self.y().recip(), |
| 711 | self.z().recip(), |
| 712 | self.w().recip(), |
| 713 | ) |
| 714 | } |
| 715 | |
| 716 | #[inline] |
| 717 | fn exp(self) -> Self { |
| 718 | Self::new( |
| 719 | self.x().exp(), |
| 720 | self.y().exp(), |
| 721 | self.z().exp(), |
| 722 | self.w().exp(), |
| 723 | ) |
| 724 | } |
| 725 | |
| 726 | #[inline] |
| 727 | fn powf(self, n: T) -> Self { |
| 728 | Self::new( |
| 729 | self.x().powf(n), |
| 730 | self.y().powf(n), |
| 731 | self.z().powf(n), |
| 732 | self.w().powf(n), |
| 733 | ) |
| 734 | } |
| 735 | |
| 736 | #[inline] |
| 737 | fn is_finite(self) -> bool { |
| 738 | self.x().is_finite() && self.y().is_finite() && self.z().is_finite() && self.w().is_finite() |
| 739 | } |
| 740 | |
| 741 | #[inline] |
| 742 | fn is_nan(self) -> bool { |
| 743 | self.x().is_nan() || self.y().is_nan() || self.z().is_nan() || self.w().is_nan() |
| 744 | } |
| 745 | |
| 746 | #[inline] |
| 747 | fn is_nan_mask(self) -> Self::Mask |
| 748 | where |
| 749 | <Self as Vector<T>>::Mask: MaskVector4, |
| 750 | { |
| 751 | Self::Mask::new( |
| 752 | self.x().is_nan(), |
| 753 | self.y().is_nan(), |
| 754 | self.z().is_nan(), |
| 755 | self.w().is_nan(), |
| 756 | ) |
| 757 | } |
| 758 | |
| 759 | #[inline] |
| 760 | fn length(self) -> T { |
| 761 | self.dot(self).sqrt() |
| 762 | } |
| 763 | |
| 764 | #[inline] |
| 765 | fn length_recip(self) -> T { |
| 766 | self.length().recip() |
| 767 | } |
| 768 | |
| 769 | #[inline] |
| 770 | fn normalize(self) -> Self { |
| 771 | self.mul_scalar(self.length_recip()) |
| 772 | } |
| 773 | |
| 774 | #[inline(always)] |
| 775 | fn length_squared(self) -> T { |
| 776 | self.dot(self) |
| 777 | } |
| 778 | |
| 779 | #[inline] |
| 780 | fn is_normalized(self) -> bool { |
| 781 | // TODO: do something with epsilon |
| 782 | (self.length_squared() - T::ONE).abs() <= T::from_f64(1e-6) |
| 783 | } |
| 784 | |
| 785 | #[inline] |
| 786 | fn abs_diff_eq(self, other: Self, max_abs_diff: T) -> bool |
| 787 | where |
| 788 | <Self as Vector<T>>::Mask: MaskVector4, |
| 789 | { |
| 790 | self.sub(other).abs().cmple(Self::splat(max_abs_diff)).all() |
| 791 | } |
| 792 | } |