glium_types/vectors/
duvec3.rs

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