1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3use super::{vec3::Vec3, ivec2::*, ivec4::*, bvec3::*};
4#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, CmpOps)]
5pub struct IVec3 {
7 pub x: i32,
8 pub y: i32,
9 pub z: i32
10}
11impl IVec3 {
12 pub const ZERO: Self = ivec3(0, 0, 0);
14 pub const ONE: Self = ivec3(1, 1, 1);
16 pub const X: Self = ivec3(1, 0, 0);
18 pub const Y: Self = ivec3(0, 1, 0);
20 pub const Z: Self = ivec3(0, 0, 1);
22
23 pub const fn new(x: i32, y: i32, z: i32) -> Self { Self { x, y, z } }
24 pub const fn extend(self, w: i32) -> IVec4 { ivec4(self.x, self.y, self.z, w) }
25 pub const fn truncate(self) -> IVec2 { ivec2(self.x, self.y) }
26 pub const fn splat(value: i32) -> Self { Self::new(value, value, value) }
28
29 pub fn length_squared(self) -> i32 {
31 self.x*self.x + self.y*self.y + self.z*self.z
32 }
33 pub fn distance_squared(self, other: IVec3) -> i32 {
35 (self - other).length_squared()
36 }
37 pub fn dot(self, other: IVec3) -> i32 {
39 self.x * other.x + self.y * other.y + self.z * other.z
40 }
41 pub fn cross(self, other: IVec3) -> IVec3 {
51 ivec3(
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: i32) -> IVec3 {
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<IVec3> for i32 {
73 fn mul(self, rhs: IVec3) -> Self::Output { rhs * self }
74 type Output = IVec3;
75}
76impl std::ops::Mul<i32> for IVec3 {
77 fn mul(self, rhs: i32) -> Self::Output { self.scale(rhs) }
78 type Output = Self;
79}
80impl std::ops::MulAssign<i32> for IVec3 { fn mul_assign(&mut self, rhs: i32) { *self = *self * rhs } }
81impl std::ops::Div<IVec3> for i32 {
82 fn div(self, rhs: IVec3) -> Self::Output { IVec3::splat(self) / rhs }
83 type Output = IVec3;
84}
85impl std::ops::Div<i32> for IVec3 {
86 fn div(self, rhs: i32) -> Self::Output { self / IVec3::splat(rhs) }
87 type Output = Self;
88}
89impl std::ops::DivAssign<i32> for IVec3 { fn div_assign(&mut self, rhs: i32) { *self = *self / rhs } }
90impl std::ops::Rem<IVec3> for i32 {
91 fn rem(self, rhs: IVec3) -> Self::Output { IVec3::splat(self) % rhs }
92 type Output = IVec3;
93}
94impl std::ops::Rem<i32> for IVec3 {
95 fn rem(self, rhs: i32) -> Self::Output { self % IVec3::splat(rhs) }
96 type Output = Self;
97}
98impl std::ops::RemAssign<i32> for IVec3 { fn rem_assign(&mut self, rhs: i32) { *self = *self % rhs } }
99
100impl AsUniformValue for IVec3 {
101 fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
102 glium::uniforms::UniformValue::IntVec3([self.x, self.y, self.z])
103 }
104}
105impl From<Vec3> for IVec3 {
106 fn from(value: Vec3) -> Self {
107 Self { x: value.x as i32, y: value.y as i32, z: value.z as i32 }
108 }
109}
110impl From<(i32, i32, i32)> for IVec3 {
111 fn from(value: (i32, i32, i32)) -> Self {
112 Self { x: value.0, y: value.1, z: value.2 }
113 }
114}
115impl From<[i32; 3]> for IVec3 {
116 fn from(value: [i32; 3]) -> Self {
117 Self { x: value[0], y: value[1], z: value[2] }
118 }
119}
120impl From<IVec3> for [i32; 3] {
121 fn from(value: IVec3) -> Self {
122 [value.x, value.y, value.z]
123 }
124}
125impl From<IVec3> for (i32, i32, i32) {
126 fn from(value: IVec3) -> Self {
127 (value.x, value.y, value.z)
128 }
129}
130pub const fn ivec3(x: i32, y: i32, z: i32) -> IVec3{
132 IVec3 { x, y, z }
133}