glium_types/vectors/
ivec4.rs

1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3use super::{vec4::Vec4, ivec3::*, bvec4::*};
4#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, CmpOps)]
5/// an interger vector made from a x, y, z and w coordinate.
6pub struct IVec4 {
7    pub x: i32,
8    pub y: i32,
9    pub z: i32,
10    pub w: i32
11}
12impl IVec4 {
13    ///a zero vector
14    pub const ZERO: Self = ivec4(0, 0, 0, 0);
15    ///a vector full of ones
16    pub const ONE: Self = ivec4(1, 1, 1, 1);
17    ///the x axis
18    pub const X: Self = ivec4(1, 0, 0, 0);
19    ///the y axis
20    pub const Y: Self = ivec4(0, 1, 0, 0);
21    ///the z axis
22    pub const Z: Self = ivec4(0, 0, 1, 0);
23    ///the w axis
24    pub const W: Self = ivec4(0, 0, 0, 1);
25
26    pub const fn new(x: i32, y: i32, z: i32, w: i32) -> Self { Self { x, y, z, w } }
27    pub const fn truncate(self) -> IVec3 { ivec3(self.x, self.y, self.z) }
28    /// create a vector where x, y and z equals `value`.
29    pub const fn splat(value: i32) -> Self { Self::new(value, value, value, value) }
30
31    /// the length of the vector without being square rooted.
32    pub fn length_squared(self) -> i32 {
33        self.x*self.x + self.y*self.y + self.z*self.z + self.w*self.w
34    }
35    /// distance between two vectors before being square rooted.
36    pub fn distance_squared(self, other: IVec4) -> i32{
37        (self - other).length_squared()
38    }
39    /// get the dot product of 2 vectors. equal to the cosign of the angle between vectors.
40    pub fn dot(self, other: IVec4) -> i32{
41        self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
42    }
43    /// multiplies each value by the scalar.
44    pub fn scale(self, scalar: i32) -> IVec4{
45        Self::new(self.x * scalar, self.y * scalar, self.z * scalar, self.w * scalar)
46    }
47    /// returns whether the 2 components are equal
48    pub fn eq(self, rhs: Self) -> BVec4 { bvec4(self.x == rhs.x, self.y == rhs.y, self.z == rhs.z, self.w == rhs.w) }
49    /// returns whether the 1st components are less than the 2nd
50    pub fn less(self, rhs: Self) -> BVec4 { bvec4(self.x < rhs.x, self.y < rhs.y, self.z < rhs.z, self.w < rhs.w) }
51    /// returns whether the 1st components are more than the 2nd
52    pub fn more(self, rhs: Self) -> BVec4 { bvec4(self.x > rhs.x, self.y > rhs.y, self.z > rhs.z, self.w > rhs.w) }
53    /// returns whether the 1st components are less than or equal to the 2nd
54    pub fn less_or_eq(self, rhs: Self) -> BVec4 {
55        bvec4(self.x <= rhs.x, self.y <= rhs.y, self.z <= rhs.z, self.w <= rhs.w)
56    }
57    /// returns whether the 1st components are more than or equal to the 2nd
58    pub fn more_or_eq(self, rhs: Self) -> BVec4 {
59        bvec4(self.x >= rhs.x, self.y >= rhs.y, self.z >= rhs.z, self.w >= rhs.w)
60    } 
61}
62impl std::ops::Mul<IVec4> for i32 {
63    fn mul(self, rhs: IVec4) -> Self::Output { rhs * self }
64    type Output = IVec4;
65}
66impl std::ops::Mul<i32> for IVec4 {
67    fn mul(self, rhs: i32) -> Self::Output { self.scale(rhs) }
68    type Output = Self;
69}
70impl std::ops::MulAssign<i32> for IVec4 { fn mul_assign(&mut self, rhs: i32) { *self = *self * rhs } }
71impl std::ops::Div<IVec4> for i32 {
72    fn div(self, rhs: IVec4) -> Self::Output { IVec4::splat(self) / rhs }
73    type Output = IVec4;
74}
75impl std::ops::Div<i32> for IVec4 {
76    fn div(self, rhs: i32) -> Self::Output { self / IVec4::splat(rhs) }
77    type Output = Self;
78}
79impl std::ops::DivAssign<i32> for IVec4 { fn div_assign(&mut self, rhs: i32) { *self = *self / rhs } }
80impl std::ops::Rem<IVec4> for i32 {
81    fn rem(self, rhs: IVec4) -> Self::Output { IVec4::splat(self) % rhs }
82    type Output = IVec4;
83}
84impl std::ops::Rem<i32> for IVec4 {
85    fn rem(self, rhs: i32) -> Self::Output { self % IVec4::splat(rhs) }
86    type Output = Self;
87}
88impl std::ops::RemAssign<i32> for IVec4 { fn rem_assign(&mut self, rhs: i32) { *self = *self % rhs } }
89
90impl AsUniformValue for IVec4{
91    fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
92        glium::uniforms::UniformValue::IntVec4([self.x, self.y, self.z, self.w])
93    }
94}
95impl From<Vec4> for IVec4 {
96    fn from(value: Vec4) -> Self {
97        Self { x: value.x as i32, y: value.y as i32, z: value.z as i32, w: value.w as i32 }
98    }
99}
100impl From<(i32, i32, i32, i32)> for IVec4 {
101    fn from(value: (i32, i32, i32, i32)) -> Self {
102        Self { x: value.0, y: value.1, z: value.2, w: value.3 }
103    }
104}
105impl From<[i32; 4]> for IVec4 {
106    fn from(value: [i32; 4]) -> Self {
107        Self { x: value[0], y: value[1], z: value[2], w: value[3] }
108    }
109}
110impl From<IVec4> for [i32; 4] {
111    fn from(value: IVec4) -> Self {
112        [value.x, value.y, value.z, value.w]
113    }
114}
115impl From<IVec4> for (i32, i32, i32, i32) {
116    fn from(value: IVec4) -> Self {
117        (value.x, value.y, value.z, value.w)
118    }
119}
120///create an interger vector with an x, y, z and w coordinate.
121pub const fn ivec4(x: i32, y: i32, z: i32, w: i32) -> IVec4{
122    IVec4 { x, y, z, w }
123}