diff options
Diffstat (limited to 'src/i32/ivec2.rs')
-rw-r--r-- | src/i32/ivec2.rs | 303 |
1 files changed, 289 insertions, 14 deletions
diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index 0a78305..fdc4c81 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -1,6 +1,6 @@ // Generated from vec.rs.tera template. Edit the template, not the generated file. -use crate::{BVec2, IVec3}; +use crate::{BVec2, I16Vec2, I64Vec2, IVec3, U16Vec2, U64Vec2, UVec2}; #[cfg(not(target_arch = "spirv"))] use core::fmt; @@ -9,6 +9,7 @@ use core::{f32, ops::*}; /// Creates a 2-dimensional vector. #[inline(always)] +#[must_use] pub const fn ivec2(x: i32, y: i32) -> IVec2 { IVec2::new(x, y) } @@ -34,16 +35,22 @@ impl IVec2 { /// All negative ones. pub const NEG_ONE: Self = Self::splat(-1); - /// A unit-length vector pointing along the positive X axis. + /// All `i32::MIN`. + pub const MIN: Self = Self::splat(i32::MIN); + + /// All `i32::MAX`. + pub const MAX: Self = Self::splat(i32::MAX); + + /// A unit vector pointing along the positive X axis. pub const X: Self = Self::new(1, 0); - /// A unit-length vector pointing along the positive Y axis. + /// A unit vector pointing along the positive Y axis. pub const Y: Self = Self::new(0, 1); - /// A unit-length vector pointing along the negative X axis. + /// A unit vector pointing along the negative X axis. pub const NEG_X: Self = Self::new(-1, 0); - /// A unit-length vector pointing along the negative Y axis. + /// A unit vector pointing along the negative Y axis. pub const NEG_Y: Self = Self::new(0, -1); /// The unit axes. @@ -51,12 +58,14 @@ impl IVec2 { /// Creates a new vector. #[inline(always)] + #[must_use] pub const fn new(x: i32, y: i32) -> Self { Self { x, y } } /// Creates a vector with all elements set to `v`. #[inline] + #[must_use] pub const fn splat(v: i32) -> Self { Self { x: v, y: v } } @@ -67,21 +76,24 @@ impl IVec2 { /// A true element in the mask uses the corresponding element from `if_true`, and false /// uses the element from `if_false`. #[inline] + #[must_use] pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self { Self { - x: if mask.x { if_true.x } else { if_false.x }, - y: if mask.y { if_true.y } else { if_false.y }, + x: if mask.test(0) { if_true.x } else { if_false.x }, + y: if mask.test(1) { if_true.y } else { if_false.y }, } } /// Creates a new vector from an array. #[inline] + #[must_use] pub const fn from_array(a: [i32; 2]) -> Self { Self::new(a[0], a[1]) } /// `[x, y]` #[inline] + #[must_use] pub const fn to_array(&self) -> [i32; 2] { [self.x, self.y] } @@ -92,6 +104,7 @@ impl IVec2 { /// /// Panics if `slice` is less than 2 elements long. #[inline] + #[must_use] pub const fn from_slice(slice: &[i32]) -> Self { Self::new(slice[0], slice[1]) } @@ -109,18 +122,21 @@ impl IVec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] + #[must_use] pub const fn extend(self, z: i32) -> IVec3 { IVec3::new(self.x, self.y, z) } /// Computes the dot product of `self` and `rhs`. #[inline] + #[must_use] pub fn dot(self, rhs: Self) -> i32 { (self.x * rhs.x) + (self.y * rhs.y) } /// Returns a vector where every component is the dot product of `self` and `rhs`. #[inline] + #[must_use] pub fn dot_into_vec(self, rhs: Self) -> Self { Self::splat(self.dot(rhs)) } @@ -129,6 +145,7 @@ impl IVec2 { /// /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`. #[inline] + #[must_use] pub fn min(self, rhs: Self) -> Self { Self { x: self.x.min(rhs.x), @@ -140,6 +157,7 @@ impl IVec2 { /// /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`. #[inline] + #[must_use] pub fn max(self, rhs: Self) -> Self { Self { x: self.x.max(rhs.x), @@ -155,6 +173,7 @@ impl IVec2 { /// /// Will panic if `min` is greater than `max` when `glam_assert` is enabled. #[inline] + #[must_use] pub fn clamp(self, min: Self, max: Self) -> Self { glam_assert!(min.cmple(max).all(), "clamp: expected min <= max"); self.max(min).min(max) @@ -164,6 +183,7 @@ impl IVec2 { /// /// In other words this computes `min(x, y, ..)`. #[inline] + #[must_use] pub fn min_element(self) -> i32 { self.x.min(self.y) } @@ -172,6 +192,7 @@ impl IVec2 { /// /// In other words this computes `max(x, y, ..)`. #[inline] + #[must_use] pub fn max_element(self) -> i32 { self.x.max(self.y) } @@ -182,6 +203,7 @@ impl IVec2 { /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all /// elements. #[inline] + #[must_use] pub fn cmpeq(self, rhs: Self) -> BVec2 { BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y)) } @@ -192,6 +214,7 @@ impl IVec2 { /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all /// elements. #[inline] + #[must_use] pub fn cmpne(self, rhs: Self) -> BVec2 { BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y)) } @@ -202,6 +225,7 @@ impl IVec2 { /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all /// elements. #[inline] + #[must_use] pub fn cmpge(self, rhs: Self) -> BVec2 { BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y)) } @@ -212,6 +236,7 @@ impl IVec2 { /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all /// elements. #[inline] + #[must_use] pub fn cmpgt(self, rhs: Self) -> BVec2 { BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y)) } @@ -222,6 +247,7 @@ impl IVec2 { /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all /// elements. #[inline] + #[must_use] pub fn cmple(self, rhs: Self) -> BVec2 { BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y)) } @@ -232,12 +258,14 @@ impl IVec2 { /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all /// elements. #[inline] + #[must_use] pub fn cmplt(self, rhs: Self) -> BVec2 { BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y)) } /// Returns a vector containing the absolute value of each element of `self`. #[inline] + #[must_use] pub fn abs(self) -> Self { Self { x: self.x.abs(), @@ -251,6 +279,7 @@ impl IVec2 { /// - `1` if the number is positive /// - `-1` if the number is negative #[inline] + #[must_use] pub fn signum(self) -> Self { Self { x: self.x.signum(), @@ -258,23 +287,56 @@ impl IVec2 { } } - /// Returns a vector with signs of `rhs` and the magnitudes of `self`. - #[inline] - pub fn copysign(self, rhs: Self) -> Self { - Self::select(rhs.cmpge(Self::ZERO), self, -self) - } - /// Returns a bitmask with the lowest 2 bits set to the sign bits from the elements of `self`. /// /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes /// into the first lowest bit, element `y` into the second, etc. #[inline] + #[must_use] pub fn is_negative_bitmask(self) -> u32 { (self.x.is_negative() as u32) | (self.y.is_negative() as u32) << 1 } + /// Computes the squared length of `self`. + #[doc(alias = "magnitude2")] + #[inline] + #[must_use] + pub fn length_squared(self) -> i32 { + self.dot(self) + } + + /// Compute the squared euclidean distance between two points in space. + #[inline] + #[must_use] + pub fn distance_squared(self, rhs: Self) -> i32 { + (self - rhs).length_squared() + } + + /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`. + /// + /// # Panics + /// This function will panic if any `rhs` element is 0 or the division results in overflow. + #[inline] + #[must_use] + pub fn div_euclid(self, rhs: Self) -> Self { + Self::new(self.x.div_euclid(rhs.x), self.y.div_euclid(rhs.y)) + } + + /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`. + /// + /// # Panics + /// This function will panic if any `rhs` element is 0 or the division results in overflow. + /// + /// [Euclidean division]: i32::rem_euclid + #[inline] + #[must_use] + pub fn rem_euclid(self, rhs: Self) -> Self { + Self::new(self.x.rem_euclid(rhs.x), self.y.rem_euclid(rhs.y)) + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] + #[must_use] pub fn perp(self) -> Self { Self { x: -self.y, @@ -288,6 +350,7 @@ impl IVec2 { #[doc(alias = "cross")] #[doc(alias = "determinant")] #[inline] + #[must_use] pub fn perp_dot(self, rhs: Self) -> i32 { (self.x * rhs.y) - (self.y * rhs.x) } @@ -295,8 +358,8 @@ impl IVec2 { /// Returns `rhs` rotated by the angle of `self`. If `self` is normalized, /// then this just rotation. This is what you usually want. Otherwise, /// it will be like a rotation with a multiplication by `self`'s length. - #[must_use] #[inline] + #[must_use] pub fn rotate(self, rhs: Self) -> Self { Self { x: self.x * rhs.x - self.y * rhs.y, @@ -306,21 +369,148 @@ impl IVec2 { /// Casts all elements of `self` to `f32`. #[inline] + #[must_use] pub fn as_vec2(&self) -> crate::Vec2 { crate::Vec2::new(self.x as f32, self.y as f32) } /// Casts all elements of `self` to `f64`. #[inline] + #[must_use] pub fn as_dvec2(&self) -> crate::DVec2 { crate::DVec2::new(self.x as f64, self.y as f64) } + /// Casts all elements of `self` to `i16`. + #[inline] + #[must_use] + pub fn as_i16vec2(&self) -> crate::I16Vec2 { + crate::I16Vec2::new(self.x as i16, self.y as i16) + } + + /// Casts all elements of `self` to `u16`. + #[inline] + #[must_use] + pub fn as_u16vec2(&self) -> crate::U16Vec2 { + crate::U16Vec2::new(self.x as u16, self.y as u16) + } + /// Casts all elements of `self` to `u32`. #[inline] + #[must_use] pub fn as_uvec2(&self) -> crate::UVec2 { crate::UVec2::new(self.x as u32, self.y as u32) } + + /// Casts all elements of `self` to `i64`. + #[inline] + #[must_use] + pub fn as_i64vec2(&self) -> crate::I64Vec2 { + crate::I64Vec2::new(self.x as i64, self.y as i64) + } + + /// Casts all elements of `self` to `u64`. + #[inline] + #[must_use] + pub fn as_u64vec2(&self) -> crate::U64Vec2 { + crate::U64Vec2::new(self.x as u64, self.y as u64) + } + + /// Returns a vector containing the wrapping addition of `self` and `rhs`. + /// + /// In other words this computes `[self.x.wrapping_add(rhs.x), self.y.wrapping_add(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn wrapping_add(self, rhs: Self) -> Self { + Self { + x: self.x.wrapping_add(rhs.x), + y: self.y.wrapping_add(rhs.y), + } + } + + /// Returns a vector containing the wrapping subtraction of `self` and `rhs`. + /// + /// In other words this computes `[self.x.wrapping_sub(rhs.x), self.y.wrapping_sub(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn wrapping_sub(self, rhs: Self) -> Self { + Self { + x: self.x.wrapping_sub(rhs.x), + y: self.y.wrapping_sub(rhs.y), + } + } + + /// Returns a vector containing the wrapping multiplication of `self` and `rhs`. + /// + /// In other words this computes `[self.x.wrapping_mul(rhs.x), self.y.wrapping_mul(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn wrapping_mul(self, rhs: Self) -> Self { + Self { + x: self.x.wrapping_mul(rhs.x), + y: self.y.wrapping_mul(rhs.y), + } + } + + /// Returns a vector containing the wrapping division of `self` and `rhs`. + /// + /// In other words this computes `[self.x.wrapping_div(rhs.x), self.y.wrapping_div(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn wrapping_div(self, rhs: Self) -> Self { + Self { + x: self.x.wrapping_div(rhs.x), + y: self.y.wrapping_div(rhs.y), + } + } + + /// Returns a vector containing the saturating addition of `self` and `rhs`. + /// + /// In other words this computes `[self.x.saturating_add(rhs.x), self.y.saturating_add(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn saturating_add(self, rhs: Self) -> Self { + Self { + x: self.x.saturating_add(rhs.x), + y: self.y.saturating_add(rhs.y), + } + } + + /// Returns a vector containing the saturating subtraction of `self` and `rhs`. + /// + /// In other words this computes `[self.x.saturating_sub(rhs.x), self.y.saturating_sub(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn saturating_sub(self, rhs: Self) -> Self { + Self { + x: self.x.saturating_sub(rhs.x), + y: self.y.saturating_sub(rhs.y), + } + } + + /// Returns a vector containing the saturating multiplication of `self` and `rhs`. + /// + /// In other words this computes `[self.x.saturating_mul(rhs.x), self.y.saturating_mul(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn saturating_mul(self, rhs: Self) -> Self { + Self { + x: self.x.saturating_mul(rhs.x), + y: self.y.saturating_mul(rhs.y), + } + } + + /// Returns a vector containing the saturating division of `self` and `rhs`. + /// + /// In other words this computes `[self.x.saturating_div(rhs.x), self.y.saturating_div(rhs.y), ..]`. + #[inline] + #[must_use] + pub const fn saturating_div(self, rhs: Self) -> Self { + Self { + x: self.x.saturating_div(rhs.x), + y: self.y.saturating_div(rhs.y), + } + } } impl Default for IVec2 { @@ -785,6 +975,28 @@ impl Shr<i32> for IVec2 { } } +impl Shl<i64> for IVec2 { + type Output = Self; + #[inline] + fn shl(self, rhs: i64) -> Self::Output { + Self { + x: self.x.shl(rhs), + y: self.y.shl(rhs), + } + } +} + +impl Shr<i64> for IVec2 { + type Output = Self; + #[inline] + fn shr(self, rhs: i64) -> Self::Output { + Self { + x: self.x.shr(rhs), + y: self.y.shr(rhs), + } + } +} + impl Shl<u8> for IVec2 { type Output = Self; #[inline] @@ -851,6 +1063,28 @@ impl Shr<u32> for IVec2 { } } +impl Shl<u64> for IVec2 { + type Output = Self; + #[inline] + fn shl(self, rhs: u64) -> Self::Output { + Self { + x: self.x.shl(rhs), + y: self.y.shl(rhs), + } + } +} + +impl Shr<u64> for IVec2 { + type Output = Self; + #[inline] + fn shr(self, rhs: u64) -> Self::Output { + Self { + x: self.x.shr(rhs), + y: self.y.shr(rhs), + } + } +} + impl Shl<crate::IVec2> for IVec2 { type Output = Self; #[inline] @@ -962,3 +1196,44 @@ impl From<IVec2> for (i32, i32) { (v.x, v.y) } } + +impl From<I16Vec2> for IVec2 { + #[inline] + fn from(v: I16Vec2) -> Self { + Self::new(i32::from(v.x), i32::from(v.y)) + } +} + +impl From<U16Vec2> for IVec2 { + #[inline] + fn from(v: U16Vec2) -> Self { + Self::new(i32::from(v.x), i32::from(v.y)) + } +} + +impl TryFrom<UVec2> for IVec2 { + type Error = core::num::TryFromIntError; + + #[inline] + fn try_from(v: UVec2) -> Result<Self, Self::Error> { + Ok(Self::new(i32::try_from(v.x)?, i32::try_from(v.y)?)) + } +} + +impl TryFrom<I64Vec2> for IVec2 { + type Error = core::num::TryFromIntError; + + #[inline] + fn try_from(v: I64Vec2) -> Result<Self, Self::Error> { + Ok(Self::new(i32::try_from(v.x)?, i32::try_from(v.y)?)) + } +} + +impl TryFrom<U64Vec2> for IVec2 { + type Error = core::num::TryFromIntError; + + #[inline] + fn try_from(v: U64Vec2) -> Result<Self, Self::Error> { + Ok(Self::new(i32::try_from(v.x)?, i32::try_from(v.y)?)) + } +} |