glium_types/vectors/
ivec3.rs

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