glium_types/vectors/
dvec3.rs

1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3use crate::matrices::DMat3;
4use super::{vec3::Vec3, dvec2::{dvec2, DVec2}, dvec4::{dvec4, DVec4}, bvec3::*};
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, CmpOps)]
6///a double vector made from a x, y and z coordinate.
7pub struct DVec3{
8    pub x: f64,
9    pub y: f64,
10    pub z: f64
11}
12impl DVec3{
13    ///a zero vector
14    pub const ZERO: Self = dvec3(0.0, 0.0, 0.0);
15    ///a vector full of ones
16    pub const ONE: Self = dvec3(1.0, 1.0, 1.0);
17    ///the x axis
18    pub const X: Self = dvec3(1.0, 0.0, 0.0);
19    ///the y axis
20    pub const Y: Self = dvec3(0.0, 1.0, 0.0);
21    ///the z axis
22    pub const Z: Self = dvec3(0.0, 0.0, 1.0);
23
24    pub const fn new(x: f64, y: f64, z: f64) -> Self { Self { x, y, z } }
25    pub const fn extend(self, w: f64) -> DVec4 { dvec4(self.x, self.y, self.z, w) }
26    pub const fn truncate(self) -> DVec2 { dvec2(self.x, self.y) }
27    ///create a vector where x, y and z equals `value`.
28    pub const fn splat(value: f64) -> Self { Self::new(value, value, value) }
29
30    ///the length of the vector before being square rooted.
31    pub fn length_squared(self) -> f64{
32        self.x*self.x + self.y*self.y + self.z*self.z
33    }
34    ///length of the vector.
35    pub fn length(self) -> f64{
36        self.length_squared().sqrt()
37    }
38    ///distance between two vectors before being square rooted.
39    pub fn distance_squared(self, other: DVec3) -> f64{
40        (self - other).length_squared()
41    }
42    ///distance between two vectors.
43    pub fn distance(self, other: DVec3) -> f64{
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: DVec3) -> f64{
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::{DVec3, dvec3};
53    /// let x = dvec3(1.0, 0.0, 0.0);
54    /// let y = dvec3(0.0, 1.0, 0.0);
55    /// let z = dvec3(0.0, 0.0, 1.0);
56    /// assert!(x.cross(y) == z);
57    /// ```
58    pub fn cross(self, other: DVec3) -> DVec3{
59        dvec3(
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: f64) -> DVec3{
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 dvec3 of zeros
70    pub fn normalise(self) -> Self{
71        let length = self.length();
72        if length == 0.0 { return dvec3(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: DMat3) -> Self{
77        let a: DVec3 = matrix.row(0).into();
78        let b: DVec3 = matrix.row(1).into();
79        let c: DVec3 = matrix.row(2).into();
80        dvec3(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<DVec3> for f64 {
94    fn mul(self, rhs: DVec3) -> Self::Output { rhs * self }
95    type Output = DVec3;
96}
97impl std::ops::Mul<f64> for DVec3 {
98    fn mul(self, rhs: f64) -> Self::Output { self.scale(rhs) }
99    type Output = Self;
100}
101impl std::ops::MulAssign<f64> for DVec3 { fn mul_assign(&mut self, rhs: f64) { *self = *self * rhs } }
102impl std::ops::Div<DVec3> for f64 {
103    fn div(self, rhs: DVec3) -> Self::Output { DVec3::splat(self) / rhs }
104    type Output = DVec3;
105}
106impl std::ops::Div<f64> for DVec3 {
107    fn div(self, rhs: f64) -> Self::Output { self.scale(1.0/rhs) }
108    type Output = Self;
109}
110impl std::ops::DivAssign<f64> for DVec3 { fn div_assign(&mut self, rhs: f64) { *self = *self / rhs } }
111impl std::ops::Rem<DVec3> for f64 {
112    fn rem(self, rhs: DVec3) -> Self::Output { DVec3::splat(self) % rhs }
113    type Output = DVec3;
114}
115impl std::ops::Rem<f64> for DVec3 {
116    fn rem(self, rhs: f64) -> Self::Output { self % DVec3::splat(rhs) }
117    type Output = Self;
118}
119impl std::ops::RemAssign<f64> for DVec3 { fn rem_assign(&mut self, rhs: f64) { *self = *self % rhs } }
120
121impl AsUniformValue for DVec3{
122    fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
123        glium::uniforms::UniformValue::DoubleVec3([self.x, self.y, self.z])
124    }
125}
126impl From<Vec3> for DVec3 {
127    fn from(value: Vec3) -> Self {
128        Self { x: value.x as f64, y: value.y as f64, z: value.z as f64 }
129    }
130}
131impl From<(f64, f64, f64)> for DVec3 {
132    fn from(value: (f64, f64, f64)) -> Self {
133        Self { x: value.0, y: value.1, z: value.2 }
134    }
135}
136impl From<[f64; 3]> for DVec3 {
137    fn from(value: [f64; 3]) -> Self {
138        Self { x: value[0], y: value[1], z: value[2] }
139    }
140}
141impl From<[f32; 3]> for DVec3 {
142    fn from(value: [f32; 3]) -> Self {
143        Self { x: value[0] as f64, y: value[1] as f64, z: value[2] as f64 }
144    }
145}
146///create a double vector with an x, y and z coordinate.
147pub const fn dvec3(x: f64, y: f64, z: f64) -> DVec3{
148    DVec3 { x, y, z }
149}