glium_types/vectors/
uvec3.rs

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