glium_types/vectors/
divec3.rs

1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3use super::{vec3::Vec3, divec2::{divec2, DIVec2}, divec4::{divec4, DIVec4}, bvec3::*};
4#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, CmpOps)]
5/// a double interger vector made from a x, y and z coordinate.
6pub struct DIVec3 {
7    pub x: i64,
8    pub y: i64,
9    pub z: i64
10}
11impl DIVec3 {
12    /// a zero vector
13    pub const ZERO: Self = divec3(0, 0, 0);
14    /// a vector full of ones
15    pub const ONE: Self = divec3(1, 1, 1);
16    /// the x axis
17    pub const X: Self = divec3(1, 0, 0);
18    /// the y axis
19    pub const Y: Self = divec3(0, 1, 0);
20    /// the z axis
21    pub const Z: Self = divec3(0, 0, 1);
22
23    pub const fn new(x: i64, y: i64, z: i64) -> Self { Self { x, y, z } }
24    pub const fn extend(self, w: i64) -> DIVec4 { divec4(self.x, self.y, self.z, w) }
25    pub const fn truncate(self) -> DIVec2 { divec2(self.x, self.y) }
26    /// create a vector where x, y and z equals `value`.
27    pub const fn splat(value: i64) -> Self{ Self::new(value, value, value) }
28
29    /// the length of the vector before being square rooted.
30    pub fn length_squared(self) -> i64 {
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: DIVec3) -> i64 {
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: DIVec3) -> i64 {
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
42    /// input vectors. the output vector is not normalised.
43    /// ```
44    /// use glium_types::vectors::{DIVec3, divec3};
45    /// let x = divec3(1, 0, 0);
46    /// let y = divec3(0, 1, 0);
47    /// let z = divec3(0, 0, 1);
48    /// assert!(x.cross(y) == z);
49    /// ```
50    pub fn cross(self, other: DIVec3) -> DIVec3 {
51        divec3(
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: i64) -> DIVec3 {
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<DIVec3> for i64 {
73    fn mul(self, rhs: DIVec3) -> Self::Output { rhs * self }
74    type Output = DIVec3;
75}
76impl std::ops::Mul<i64> for DIVec3 {
77    fn mul(self, rhs: i64) -> Self::Output { self.scale(rhs) }
78    type Output = Self;
79}
80impl std::ops::MulAssign<i64> for DIVec3 { fn mul_assign(&mut self, rhs: i64) { *self = *self * rhs } }
81impl std::ops::Div<DIVec3> for i64 {
82    fn div(self, rhs: DIVec3) -> Self::Output { DIVec3::splat(self) / rhs }
83    type Output = DIVec3;
84}
85impl std::ops::Div<i64> for DIVec3 {
86    fn div(self, rhs: i64) -> Self::Output { self / DIVec3::splat(rhs) }
87    type Output = Self;
88}
89impl std::ops::DivAssign<i64> for DIVec3 { fn div_assign(&mut self, rhs: i64) { *self = *self / rhs } }
90impl std::ops::Rem<DIVec3> for i64 {
91    fn rem(self, rhs: DIVec3) -> Self::Output { DIVec3::splat(self) % rhs }
92    type Output = DIVec3;
93}
94impl std::ops::Rem<i64> for DIVec3 {
95    fn rem(self, rhs: i64) -> Self::Output { self % DIVec3::splat(rhs) }
96    type Output = Self;
97}
98impl std::ops::RemAssign<i64> for DIVec3 { fn rem_assign(&mut self, rhs: i64) { *self = *self % rhs } }
99
100impl AsUniformValue for DIVec3 {
101    fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
102        glium::uniforms::UniformValue::Int64Vec3([self.x, self.y, self.z])
103    }
104}
105impl From<Vec3> for DIVec3 {
106    fn from(value: Vec3) -> Self {
107        Self { x: value.x as i64, y: value.y as i64, z: value.z as i64 }
108    }
109}
110impl From<(i64, i64, i64)> for DIVec3 {
111    fn from(value: (i64, i64, i64)) -> Self {
112        Self { x: value.0, y: value.1, z: value.2 }
113    }
114}
115impl From<[i64; 3]> for DIVec3 {
116    fn from(value: [i64; 3]) -> Self {
117        Self { x: value[0], y: value[1], z: value[2] }
118    }
119}
120/// create an double interger vector with an x, y and z coordinate.
121pub const fn divec3(x: i64, y: i64, z: i64) -> DIVec3 {
122    DIVec3 { x, y, z }
123}