1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3use crate::prelude::Mat3;
4use super::{vec2::{vec2, Vec2}, vec4::{vec4, Vec4}, bvec3::*};
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, CmpOps)]
6pub struct Vec3{
8 pub x: f32,
9 pub y: f32,
10 pub z: f32
11}
12impl Vec3{
13 pub const ZERO: Self = vec3(0.0, 0.0, 0.0);
15 pub const ONE: Self = vec3(1.0, 1.0, 1.0);
17 pub const X: Self = vec3(1.0, 0.0, 0.0);
19 pub const Y: Self = vec3(0.0, 1.0, 0.0);
21 pub const Z: Self = vec3(0.0, 0.0, 1.0);
23
24 pub const fn new(x: f32, y: f32, z: f32) -> Self { Self { x, y, z } }
25 pub const fn extend(self, w: f32) -> Vec4 { vec4(self.x, self.y, self.z, w) }
26 pub const fn truncate(self) -> Vec2 { vec2(self.x, self.y) }
27 pub const fn splat(value: f32) -> Self { Self::new(value, value, value) }
29
30 pub fn length_squared(self) -> f32{
32 self.x*self.x + self.y*self.y + self.z*self.z
33 }
34 pub fn length(self) -> f32{
36 self.length_squared().sqrt()
37 }
38 pub fn distance_squared(self, other: Vec3) -> f32{
40 (self - other).length_squared()
41 }
42 pub fn distance(self, other: Vec3) -> f32{
44 (self - other).length()
45 }
46 pub fn dot(self, other: Vec3) -> f32{
48 self.x * other.x + self.y * other.y + self.z * other.z
49 }
50 pub fn cross(self, other: Vec3) -> Vec3{
59 vec3(
60 self.y*other.z - self.z*other.y,
61 self.z*other.x - self.x*other.z,
62 self.x*other.y - self.y*other.x
63 )
64 }
65 pub fn scale(self, scalar: f32) -> Vec3{
67 Self::new(self.x * scalar, self.y * scalar, self.z * scalar)
68 }
69 pub fn normalise(self) -> Self{
71 let length = self.length();
72 if length == 0.0 { return vec3(0.0, 0.0, 0.0); }
73 self.scale(1.0 / length)
74 }
75 pub fn transform(self, matrix: Mat3) -> Self{
77 let a: Vec3 = matrix.row(0).into();
78 let b: Vec3 = matrix.row(1).into();
79 let c: Vec3 = matrix.row(2).into();
80 vec3(a.dot(self), b.dot(self), c.dot(self))
81 }
82 pub fn eq(self, rhs: Self) -> BVec3 { bvec3(self.x == rhs.x, self.y == rhs.y, self.z == rhs.z) }
84 pub fn less(self, rhs: Self) -> BVec3 { bvec3(self.x < rhs.x, self.y < rhs.y, self.z < rhs.z) }
86 pub fn more(self, rhs: Self) -> BVec3 { bvec3(self.x > rhs.x, self.y > rhs.y, self.z > rhs.z) }
88 pub fn less_or_eq(self, rhs: Self) -> BVec3 { bvec3(self.x <= rhs.x, self.y <= rhs.y, self.z <= rhs.z) }
90 pub fn more_or_eq(self, rhs: Self) -> BVec3 { bvec3(self.x >= rhs.x, self.y >= rhs.y, self.z >= rhs.z) }
92}
93impl std::ops::Mul<Vec3> for f32 {
94 fn mul(self, rhs: Vec3) -> Self::Output { rhs * self }
95 type Output = Vec3;
96}
97impl std::ops::Mul<f32> for Vec3 {
98 fn mul(self, rhs: f32) -> Self::Output { self.scale(rhs) }
99 type Output = Self;
100}
101impl std::ops::MulAssign<f32> for Vec3 { fn mul_assign(&mut self, rhs: f32) { *self = *self * rhs } }
102impl std::ops::Div<Vec3> for f32 {
103 fn div(self, rhs: Vec3) -> Self::Output { Vec3::splat(self) / rhs }
104 type Output = Vec3;
105}
106impl std::ops::Div<f32> for Vec3 {
107 fn div(self, rhs: f32) -> Self::Output { self.scale(1.0/rhs) }
108 type Output = Self;
109}
110impl std::ops::DivAssign<f32> for Vec3 { fn div_assign(&mut self, rhs: f32) { *self = *self / rhs } }
111impl std::ops::Rem<Vec3> for f32 {
112 fn rem(self, rhs: Vec3) -> Self::Output { Vec3::splat(self) % rhs }
113 type Output = Vec3;
114}
115impl std::ops::Rem<f32> for Vec3 {
116 fn rem(self, rhs: f32) -> Self::Output { self % Vec3::splat(rhs) }
117 type Output = Self;
118}
119impl std::ops::RemAssign<f32> for Vec3 { fn rem_assign(&mut self, rhs: f32) { *self = *self % rhs } }
120
121impl AsUniformValue for Vec3{
122 fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
123 glium::uniforms::UniformValue::Vec3([self.x, self.y, self.z])
124 }
125}
126impl From<[f32; 3]> for Vec3 {
127 fn from(value: [f32; 3]) -> Self {
128 Self { x: value[0], y: value[1], z: value[2] }
129 }
130}
131impl From<(f32, f32, f32)> for Vec3 {
132 fn from(value: (f32, f32, f32)) -> Self {
133 Self { x: value.0, y: value.1, z: value.2 }
134 }
135}
136impl From<Vec3> for [f32; 3] {
137 fn from(value: Vec3) -> Self {
138 [value.x, value.y, value.z]
139 }
140}
141impl From<Vec3> for (f32, f32, f32) {
142 fn from(value: Vec3) -> Self {
143 (value.x, value.y, value.z)
144 }
145}
146pub const fn vec3(x: f32, y: f32, z: f32) -> Vec3{
148 Vec3 { x, y, z }
149}