glium_types/vectors/
duvec4.rs

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