glium_types/vectors/
divec3.rs1use 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)]
5pub struct DIVec3 {
7 pub x: i64,
8 pub y: i64,
9 pub z: i64
10}
11impl DIVec3 {
12 pub const ZERO: Self = divec3(0, 0, 0);
14 pub const ONE: Self = divec3(1, 1, 1);
16 pub const X: Self = divec3(1, 0, 0);
18 pub const Y: Self = divec3(0, 1, 0);
20 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 pub const fn splat(value: i64) -> Self{ Self::new(value, value, value) }
28
29 pub fn length_squared(self) -> i64 {
31 self.x*self.x + self.y*self.y + self.z*self.z
32 }
33 pub fn distance_squared(self, other: DIVec3) -> i64 {
35 (self - other).length_squared()
36 }
37 pub fn dot(self, other: DIVec3) -> i64 {
39 self.x * other.x + self.y * other.y + self.z * other.z
40 }
41 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 pub fn scale(self, scalar: i64) -> DIVec3 {
59 Self::new(self.x * scalar, self.y * scalar, self.z * scalar)
60 }
61 pub fn eq(self, rhs: Self) -> BVec3 { bvec3(self.x == rhs.x, self.y == rhs.y, self.z == rhs.z) }
63 pub fn less(self, rhs: Self) -> BVec3 { bvec3(self.x < rhs.x, self.y < rhs.y, self.z < rhs.z) }
65 pub fn more(self, rhs: Self) -> BVec3 { bvec3(self.x > rhs.x, self.y > rhs.y, self.z > rhs.z) }
67 pub fn less_or_eq(self, rhs: Self) -> BVec3 { bvec3(self.x <= rhs.x, self.y <= rhs.y, self.z <= rhs.z) }
69 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}
120pub const fn divec3(x: i64, y: i64, z: i64) -> DIVec3 {
122 DIVec3 { x, y, z }
123}