use crate::{BVec4, IVec2, IVec3};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::*;
use crate::nums::*;
use auto_ops_det::impl_op_ex;
use core::ops;
#[inline]
pub const fn ivec4(x: i32, y: i32, z: i32, w: i32) -> IVec4 {
IVec4::new(x, y, z, w)
}
#[cfg_attr(not(target_arch = "spirv"), derive(Hash))]
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "cuda", repr(align(16)))]
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
pub struct IVec4 {
pub x: i32,
pub y: i32,
pub z: i32,
pub w: i32,
}
impl IVec4 {
pub const ZERO: Self = Self::splat(0);
pub const ONE: Self = Self::splat(1);
pub const NEG_ONE: Self = Self::splat(-1);
pub const X: Self = Self::new(1, 0, 0, 0);
pub const Y: Self = Self::new(0, 1, 0, 0);
pub const Z: Self = Self::new(0, 0, 1, 0);
pub const W: Self = Self::new(0, 0, 0, 1);
pub const NEG_X: Self = Self::new(-1, 0, 0, 0);
pub const NEG_Y: Self = Self::new(0, -1, 0, 0);
pub const NEG_Z: Self = Self::new(0, 0, -1, 0);
pub const NEG_W: Self = Self::new(0, 0, 0, -1);
pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
#[inline]
pub const fn new(x: i32, y: i32, z: i32, w: i32) -> Self {
Self { x, y, z, w }
}
#[inline]
pub const fn splat(v: i32) -> Self {
Self {
x: v,
y: v,
z: v,
w: v,
}
}
#[inline]
pub fn select(mask: BVec4, 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 },
z: if mask.z { if_true.z } else { if_false.z },
w: if mask.w { if_true.w } else { if_false.w },
}
}
#[inline]
pub const fn from_array(a: [i32; 4]) -> Self {
Self::new(a[0], a[1], a[2], a[3])
}
#[inline]
pub const fn to_array(&self) -> [i32; 4] {
[self.x, self.y, self.z, self.w]
}
#[inline]
pub const fn from_slice(slice: &[i32]) -> Self {
Self::new(slice[0], slice[1], slice[2], slice[3])
}
#[inline]
pub fn write_to_slice(self, slice: &mut [i32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
}
#[inline]
pub fn truncate(self) -> IVec3 {
use crate::swizzles::Vec4Swizzles;
self.xyz()
}
#[inline]
pub fn dot(self, rhs: Self) -> i32 {
(self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
}
#[inline]
pub fn min(self, rhs: Self) -> Self {
Self {
x: self.x.min(rhs.x),
y: self.y.min(rhs.y),
z: self.z.min(rhs.z),
w: self.w.min(rhs.w),
}
}
#[inline]
pub fn max(self, rhs: Self) -> Self {
Self {
x: self.x.max(rhs.x),
y: self.y.max(rhs.y),
z: self.z.max(rhs.z),
w: self.w.max(rhs.w),
}
}
#[inline]
pub fn clamp(self, min: Self, max: Self) -> Self {
glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
self.max(min).min(max)
}
#[inline]
pub fn min_element(self) -> i32 {
self.x.min(self.y.min(self.z.min(self.w)))
}
#[inline]
pub fn max_element(self) -> i32 {
self.x.max(self.y.max(self.z.max(self.w)))
}
#[inline]
pub fn cmpeq(self, rhs: Self) -> BVec4 {
BVec4::new(
self.x.eq(&rhs.x),
self.y.eq(&rhs.y),
self.z.eq(&rhs.z),
self.w.eq(&rhs.w),
)
}
#[inline]
pub fn cmpne(self, rhs: Self) -> BVec4 {
BVec4::new(
self.x.ne(&rhs.x),
self.y.ne(&rhs.y),
self.z.ne(&rhs.z),
self.w.ne(&rhs.w),
)
}
#[inline]
pub fn cmpge(self, rhs: Self) -> BVec4 {
BVec4::new(
self.x.ge(&rhs.x),
self.y.ge(&rhs.y),
self.z.ge(&rhs.z),
self.w.ge(&rhs.w),
)
}
#[inline]
pub fn cmpgt(self, rhs: Self) -> BVec4 {
BVec4::new(
self.x.gt(&rhs.x),
self.y.gt(&rhs.y),
self.z.gt(&rhs.z),
self.w.gt(&rhs.w),
)
}
#[inline]
pub fn cmple(self, rhs: Self) -> BVec4 {
BVec4::new(
self.x.le(&rhs.x),
self.y.le(&rhs.y),
self.z.le(&rhs.z),
self.w.le(&rhs.w),
)
}
#[inline]
pub fn cmplt(self, rhs: Self) -> BVec4 {
BVec4::new(
self.x.lt(&rhs.x),
self.y.lt(&rhs.y),
self.z.lt(&rhs.z),
self.w.lt(&rhs.w),
)
}
#[inline]
pub fn abs(self) -> Self {
Self {
x: self.x.absf(),
y: self.y.absf(),
z: self.z.absf(),
w: self.w.absf(),
}
}
#[inline]
pub fn signum(self) -> Self {
Self {
x: self.x.signumf(),
y: self.y.signumf(),
z: self.z.signumf(),
w: self.w.signumf(),
}
}
#[inline]
pub fn as_vec4(&self) -> crate::Vec4 {
crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32)
}
#[inline]
pub fn as_dvec4(&self) -> crate::DVec4 {
crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
}
#[inline]
pub fn as_uvec4(&self) -> crate::UVec4 {
crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
}
}
impl Default for IVec4 {
#[inline]
fn default() -> Self {
Self::ZERO
}
}
impl_op_ex!(/ |a: &IVec4, b: &IVec4| -> IVec4 {
IVec4 {
x: a.x.div(b.x),
y: a.y.div(b.y),
z: a.z.div(b.z),
w: a.w.div(b.w),
}
});
impl_op_ex!(/= |a: &mut IVec4, b: &IVec4| {
a.x.div_assign(b.x);
a.y.div_assign(b.y);
a.z.div_assign(b.z);
a.w.div_assign(b.w);
});
impl_op_ex!(/ |a: &IVec4, b: &i32| -> IVec4 {
IVec4 {
x: a.x.div(b),
y: a.y.div(b),
z: a.z.div(b),
w: a.w.div(b),
}
});
impl_op_ex!(/= |a: &mut IVec4, b: &i32| {
a.x.div_assign(b);
a.y.div_assign(b);
a.z.div_assign(b);
a.w.div_assign(b);
});
impl_op_ex!(/ |a: &i32, b: &IVec4| -> IVec4 {
IVec4 {
x: a.div(b.x),
y: a.div(b.y),
z: a.div(b.z),
w: a.div(b.w),
}
});
impl_op_ex!(*|a: &IVec4, b: &IVec4| -> IVec4 {
IVec4 {
x: a.x.mul(b.x),
y: a.y.mul(b.y),
z: a.z.mul(b.z),
w: a.w.mul(b.w),
}
});
impl_op_ex!(*= |a: &mut IVec4, b: &IVec4| {
a.x.mul_assign(b.x);
a.y.mul_assign(b.y);
a.z.mul_assign(b.z);
a.w.mul_assign(b.w);
});
impl_op_ex!(*|a: &IVec4, b: &i32| -> IVec4 {
IVec4 {
x: a.x.mul(b),
y: a.y.mul(b),
z: a.z.mul(b),
w: a.w.mul(b),
}
});
impl_op_ex!(*= |a: &mut IVec4, b: &i32| {
a.x.mul_assign(b);
a.y.mul_assign(b);
a.z.mul_assign(b);
a.w.mul_assign(b);
});
impl_op_ex!(*|a: &i32, b: &IVec4| -> IVec4 {
IVec4 {
x: a.mul(b.x),
y: a.mul(b.y),
z: a.mul(b.z),
w: a.mul(b.w),
}
});
impl_op_ex!(+ |a: &IVec4, b: &IVec4| -> IVec4 {
IVec4 {
x: a.x.add(b.x),
y: a.y.add(b.y),
z: a.z.add(b.z),
w: a.w.add(b.w),
}
});
impl_op_ex!(+= |a: &mut IVec4, b: &IVec4| {
a.x.add_assign(b.x);
a.y.add_assign(b.y);
a.z.add_assign(b.z);
a.w.add_assign(b.w);
});
impl_op_ex!(+ |a: &IVec4, b: &i32| -> IVec4 {
IVec4 {
x: a.x.add(b),
y: a.y.add(b),
z: a.z.add(b),
w: a.w.add(b),
}
});
impl_op_ex!(+= |a: &mut IVec4, b: &i32| {
a.x.add_assign(b);
a.y.add_assign(b);
a.z.add_assign(b);
a.w.add_assign(b);
});
impl_op_ex!(+ |a: &i32, b: &IVec4| -> IVec4 {
IVec4 {
x: a.add(b.x),
y: a.add(b.y),
z: a.add(b.z),
w: a.add(b.w),
}
});
impl_op_ex!(-|a: &IVec4, b: &IVec4| -> IVec4 {
IVec4 {
x: a.x.sub(b.x),
y: a.y.sub(b.y),
z: a.z.sub(b.z),
w: a.w.sub(b.w),
}
});
impl_op_ex!(-= |a: &mut IVec4, b: &IVec4| {
a.x.sub_assign(b.x);
a.y.sub_assign(b.y);
a.z.sub_assign(b.z);
a.w.sub_assign(b.w);
});
impl_op_ex!(-|a: &IVec4, b: &i32| -> IVec4 {
IVec4 {
x: a.x.sub(b),
y: a.y.sub(b),
z: a.z.sub(b),
w: a.w.sub(b),
}
});
impl_op_ex!(-= |a: &mut IVec4, b: &i32| {
a.x.sub_assign(b);
a.y.sub_assign(b);
a.z.sub_assign(b);
a.w.sub_assign(b);
});
impl_op_ex!(-|a: &i32, b: &IVec4| -> IVec4 {
IVec4 {
x: a.sub(b.x),
y: a.sub(b.y),
z: a.sub(b.z),
w: a.sub(b.w),
}
});
impl_op_ex!(% |a: &IVec4, b: &IVec4| -> IVec4 {
IVec4 {
x: a.x.rem(b.x),
y: a.y.rem(b.y),
z: a.z.rem(b.z),
w: a.w.rem(b.w),
}
});
impl_op_ex!(%= |a: &mut IVec4, b: &IVec4| {
a.x.rem_assign(b.x);
a.y.rem_assign(b.y);
a.z.rem_assign(b.z);
a.w.rem_assign(b.w);
});
impl_op_ex!(% |a: &IVec4, b: &i32| -> IVec4 {
IVec4 {
x: a.x.rem(b),
y: a.y.rem(b),
z: a.z.rem(b),
w: a.w.rem(b),
}
});
impl_op_ex!(%= |a: &mut IVec4, b: &i32| {
a.x.rem_assign(b);
a.y.rem_assign(b);
a.z.rem_assign(b);
a.w.rem_assign(b);
});
impl_op_ex!(% |a: &i32, b: &IVec4| -> IVec4 {
IVec4 {
x: a.rem(b.x),
y: a.rem(b.y),
z: a.rem(b.z),
w: a.rem(b.w),
}
});
impl<'a> Sum<&'a Self> for IVec4 {
#[inline]
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
}
}
impl<'a> Product<&'a Self> for IVec4 {
#[inline]
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
}
}
impl Neg for IVec4 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self {
x: self.x.neg(),
y: self.y.neg(),
z: self.z.neg(),
w: self.w.neg(),
}
}
}
impl Not for IVec4 {
type Output = Self;
#[inline]
fn not(self) -> Self::Output {
Self {
x: self.x.not(),
y: self.y.not(),
z: self.z.not(),
w: self.w.not(),
}
}
}
impl BitAnd for IVec4 {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self::Output {
Self {
x: self.x.bitand(rhs.x),
y: self.y.bitand(rhs.y),
z: self.z.bitand(rhs.z),
w: self.w.bitand(rhs.w),
}
}
}
impl BitOr for IVec4 {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
Self {
x: self.x.bitor(rhs.x),
y: self.y.bitor(rhs.y),
z: self.z.bitor(rhs.z),
w: self.w.bitor(rhs.w),
}
}
}
impl BitXor for IVec4 {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self::Output {
Self {
x: self.x.bitxor(rhs.x),
y: self.y.bitxor(rhs.y),
z: self.z.bitxor(rhs.z),
w: self.w.bitxor(rhs.w),
}
}
}
impl BitAnd<i32> for IVec4 {
type Output = Self;
#[inline]
fn bitand(self, rhs: i32) -> Self::Output {
Self {
x: self.x.bitand(rhs),
y: self.y.bitand(rhs),
z: self.z.bitand(rhs),
w: self.w.bitand(rhs),
}
}
}
impl BitOr<i32> for IVec4 {
type Output = Self;
#[inline]
fn bitor(self, rhs: i32) -> Self::Output {
Self {
x: self.x.bitor(rhs),
y: self.y.bitor(rhs),
z: self.z.bitor(rhs),
w: self.w.bitor(rhs),
}
}
}
impl BitXor<i32> for IVec4 {
type Output = Self;
#[inline]
fn bitxor(self, rhs: i32) -> Self::Output {
Self {
x: self.x.bitxor(rhs),
y: self.y.bitxor(rhs),
z: self.z.bitxor(rhs),
w: self.w.bitxor(rhs),
}
}
}
impl Shl<i8> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: i8) -> Self::Output {
Self {
x: self.x.shl(rhs),
y: self.y.shl(rhs),
z: self.z.shl(rhs),
w: self.w.shl(rhs),
}
}
}
impl Shr<i8> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: i8) -> Self::Output {
Self {
x: self.x.shr(rhs),
y: self.y.shr(rhs),
z: self.z.shr(rhs),
w: self.w.shr(rhs),
}
}
}
impl Shl<i16> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: i16) -> Self::Output {
Self {
x: self.x.shl(rhs),
y: self.y.shl(rhs),
z: self.z.shl(rhs),
w: self.w.shl(rhs),
}
}
}
impl Shr<i16> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: i16) -> Self::Output {
Self {
x: self.x.shr(rhs),
y: self.y.shr(rhs),
z: self.z.shr(rhs),
w: self.w.shr(rhs),
}
}
}
impl Shl<i32> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: i32) -> Self::Output {
Self {
x: self.x.shl(rhs),
y: self.y.shl(rhs),
z: self.z.shl(rhs),
w: self.w.shl(rhs),
}
}
}
impl Shr<i32> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: i32) -> Self::Output {
Self {
x: self.x.shr(rhs),
y: self.y.shr(rhs),
z: self.z.shr(rhs),
w: self.w.shr(rhs),
}
}
}
impl Shl<u8> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: u8) -> Self::Output {
Self {
x: self.x.shl(rhs),
y: self.y.shl(rhs),
z: self.z.shl(rhs),
w: self.w.shl(rhs),
}
}
}
impl Shr<u8> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: u8) -> Self::Output {
Self {
x: self.x.shr(rhs),
y: self.y.shr(rhs),
z: self.z.shr(rhs),
w: self.w.shr(rhs),
}
}
}
impl Shl<u16> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: u16) -> Self::Output {
Self {
x: self.x.shl(rhs),
y: self.y.shl(rhs),
z: self.z.shl(rhs),
w: self.w.shl(rhs),
}
}
}
impl Shr<u16> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: u16) -> Self::Output {
Self {
x: self.x.shr(rhs),
y: self.y.shr(rhs),
z: self.z.shr(rhs),
w: self.w.shr(rhs),
}
}
}
impl Shl<u32> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: u32) -> Self::Output {
Self {
x: self.x.shl(rhs),
y: self.y.shl(rhs),
z: self.z.shl(rhs),
w: self.w.shl(rhs),
}
}
}
impl Shr<u32> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: u32) -> Self::Output {
Self {
x: self.x.shr(rhs),
y: self.y.shr(rhs),
z: self.z.shr(rhs),
w: self.w.shr(rhs),
}
}
}
impl Shl<crate::IVec4> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: crate::IVec4) -> Self::Output {
Self {
x: self.x.shl(rhs.x),
y: self.y.shl(rhs.y),
z: self.z.shl(rhs.z),
w: self.w.shl(rhs.w),
}
}
}
impl Shr<crate::IVec4> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: crate::IVec4) -> Self::Output {
Self {
x: self.x.shr(rhs.x),
y: self.y.shr(rhs.y),
z: self.z.shr(rhs.z),
w: self.w.shr(rhs.w),
}
}
}
impl Shl<crate::UVec4> for IVec4 {
type Output = Self;
#[inline]
fn shl(self, rhs: crate::UVec4) -> Self::Output {
Self {
x: self.x.shl(rhs.x),
y: self.y.shl(rhs.y),
z: self.z.shl(rhs.z),
w: self.w.shl(rhs.w),
}
}
}
impl Shr<crate::UVec4> for IVec4 {
type Output = Self;
#[inline]
fn shr(self, rhs: crate::UVec4) -> Self::Output {
Self {
x: self.x.shr(rhs.x),
y: self.y.shr(rhs.y),
z: self.z.shr(rhs.z),
w: self.w.shr(rhs.w),
}
}
}
impl Index<usize> for IVec4 {
type Output = i32;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
match index {
0 => &self.x,
1 => &self.y,
2 => &self.z,
3 => &self.w,
_ => panic!("index out of bounds"),
}
}
}
impl IndexMut<usize> for IVec4 {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
match index {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
3 => &mut self.w,
_ => panic!("index out of bounds"),
}
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for IVec4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for IVec4 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple(stringify!(IVec4))
.field(&self.x)
.field(&self.y)
.field(&self.z)
.field(&self.w)
.finish()
}
}
impl From<[i32; 4]> for IVec4 {
#[inline]
fn from(a: [i32; 4]) -> Self {
Self::new(a[0], a[1], a[2], a[3])
}
}
impl From<IVec4> for [i32; 4] {
#[inline]
fn from(v: IVec4) -> Self {
[v.x, v.y, v.z, v.w]
}
}
impl From<(i32, i32, i32, i32)> for IVec4 {
#[inline]
fn from(t: (i32, i32, i32, i32)) -> Self {
Self::new(t.0, t.1, t.2, t.3)
}
}
impl From<IVec4> for (i32, i32, i32, i32) {
#[inline]
fn from(v: IVec4) -> Self {
(v.x, v.y, v.z, v.w)
}
}
impl From<(IVec3, i32)> for IVec4 {
#[inline]
fn from((v, w): (IVec3, i32)) -> Self {
Self::new(v.x, v.y, v.z, w)
}
}
impl From<(i32, IVec3)> for IVec4 {
#[inline]
fn from((x, v): (i32, IVec3)) -> Self {
Self::new(x, v.x, v.y, v.z)
}
}
impl From<(IVec2, i32, i32)> for IVec4 {
#[inline]
fn from((v, z, w): (IVec2, i32, i32)) -> Self {
Self::new(v.x, v.y, z, w)
}
}
impl From<(IVec2, IVec2)> for IVec4 {
#[inline]
fn from((v, u): (IVec2, IVec2)) -> Self {
Self::new(v.x, v.y, u.x, u.y)
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsRef<[i32; 4]> for IVec4 {
#[inline]
fn as_ref(&self) -> &[i32; 4] {
unsafe { &*(self as *const IVec4 as *const [i32; 4]) }
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsMut<[i32; 4]> for IVec4 {
#[inline]
fn as_mut(&mut self) -> &mut [i32; 4] {
unsafe { &mut *(self as *mut IVec4 as *mut [i32; 4]) }
}
}