glium_types/vectors/
vec3.rs

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)]
6///a vector made from a x, y and z coordinate.
7pub struct Vec3{
8    pub x: f32,
9    pub y: f32,
10    pub z: f32
11}
12impl Vec3{
13    ///a zero vector
14    pub const ZERO: Self = vec3(0.0, 0.0, 0.0);
15    ///a vector full of ones
16    pub const ONE: Self = vec3(1.0, 1.0, 1.0);
17    ///the x axis
18    pub const X: Self = vec3(1.0, 0.0, 0.0);
19    ///the y axis
20    pub const Y: Self = vec3(0.0, 1.0, 0.0);
21    ///the z axis
22    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    ///create a vector where x, y and z equals `value`.
28    pub const fn splat(value: f32) -> Self { Self::new(value, value, value) }
29
30    ///the length of the vector before being square rooted.
31    pub fn length_squared(self) -> f32{
32        self.x*self.x + self.y*self.y + self.z*self.z
33    }
34    ///length of the vector.
35    pub fn length(self) -> f32{
36        self.length_squared().sqrt()
37    }
38    ///distance between two vectors before being square rooted.
39    pub fn distance_squared(self, other: Vec3) -> f32{
40        (self - other).length_squared()
41    }
42    ///distance between two vectors.
43    pub fn distance(self, other: Vec3) -> f32{
44        (self - other).length()
45    }
46    ///get the dot product of 2 vectors. equal to the cosign of the angle between vectors.
47    pub fn dot(self, other: Vec3) -> f32{
48        self.x * other.x + self.y * other.y + self.z * other.z
49    }
50    ///get the cross product of 2 vectors. equal to the vector that is perpendicular to both input vectors. the output vector is not normalised.
51    /// ```
52    /// use glium_types::vectors::{Vec3, vec3};
53    /// let x = vec3(1.0, 0.0, 0.0);
54    /// let y = vec3(0.0, 1.0, 0.0);
55    /// let z = vec3(0.0, 0.0, 1.0);
56    /// assert!(x.cross(y) == z);
57    /// ```
58    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    ///multiplies each value by the scalar.
66    pub fn scale(self, scalar: f32) -> Vec3{
67        Self::new(self.x * scalar, self.y * scalar, self.z * scalar)
68    }
69    ///makes the length of the vector equal to 1. on fail returns vec3 of zeros
70    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    ///transforms vector by the matrix
76    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    /// returns whether the 2 components are equal
83    pub fn eq(self, rhs: Self) -> BVec3 { bvec3(self.x == rhs.x, self.y == rhs.y, self.z == rhs.z) }
84    /// returns whether the 1st components are less than the 2nd
85    pub fn less(self, rhs: Self) -> BVec3 { bvec3(self.x < rhs.x, self.y < rhs.y, self.z < rhs.z) }
86    /// returns whether the 1st components are more than the 2nd
87    pub fn more(self, rhs: Self) -> BVec3 { bvec3(self.x > rhs.x, self.y > rhs.y, self.z > rhs.z) }
88    /// returns whether the 1st components are less than or equal to the 2nd
89    pub fn less_or_eq(self, rhs: Self) -> BVec3 { bvec3(self.x <= rhs.x, self.y <= rhs.y, self.z <= rhs.z) }
90    /// returns whether the 1st components are more than or equal to the 2nd
91    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}
146///create a vector with an x, y and z coordinate.
147pub const fn vec3(x: f32, y: f32, z: f32) -> Vec3{
148    Vec3 { x, y, z }
149}