1use derive_cmp_ops::CmpOps;
2use glium::uniforms::AsUniformValue;
3use crate::matrices::DMat4;
4use super::{vec4::Vec4, dvec3::{dvec3, DVec3}, bvec4::*};
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, CmpOps)]
6pub struct DVec4 {
8 pub x: f64,
9 pub y: f64,
10 pub z: f64,
11 pub w: f64
12}
13impl DVec4 {
14 pub const ZERO: Self = dvec4(0.0, 0.0, 0.0, 0.0);
16 pub const ONE: Self = dvec4(1.0, 1.0, 1.0, 1.0);
18 pub const X: Self = dvec4(1.0, 0.0, 0.0, 0.0);
20 pub const Y: Self = dvec4(0.0, 1.0, 0.0, 0.0);
22 pub const Z: Self = dvec4(0.0, 0.0, 1.0, 0.0);
24 pub const W: Self = dvec4(0.0, 0.0, 0.0, 1.0);
26
27 pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Self { Self { x, y, z, w } }
28 pub const fn truncate(self) -> DVec3 { dvec3(self.x, self.y, self.z) }
29 pub const fn splat(value: f64) -> Self { Self::new(value, value, value, value) }
31
32 pub fn length_squared(self) -> f64 {
34 self.x*self.x + self.y*self.y + self.z*self.z + self.w*self.w
35 }
36 pub fn length(self) -> f64 {
38 self.length_squared().sqrt()
39 }
40 pub fn distance_squared(self, other: DVec4) -> f64 {
42 (self - other).length_squared()
43 }
44 pub fn distance(self, other: DVec4) -> f64 {
46 (self - other).length()
47 }
48 pub fn dot(self, other: DVec4) -> f64 {
51 self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
52 }
53 pub fn scale(self, scalar: f64) -> DVec4 {
55 Self::new(self.x * scalar, self.y * scalar, self.z * scalar, self.w * scalar)
56 }
57 pub fn normalise(self) -> Self {
59 let length = self.length();
60 if length == 0.0 { return DVec4::ZERO; }
61 self.scale(1.0 / length)
62 }
63 pub fn transform(self, matrix: DMat4) -> DVec4 {
65 let a: DVec4 = matrix.row(0).into();
66 let b: DVec4 = matrix.row(1).into();
67 let c: DVec4 = matrix.row(2).into();
68 let d: DVec4 = matrix.row(3).into();
69 dvec4(a.dot(self), b.dot(self), c.dot(self), d.dot(self))
70 }
71 pub fn eq(self, rhs: Self) -> BVec4 { bvec4(self.x == rhs.x, self.y == rhs.y, self.z == rhs.z, self.w == rhs.w) }
73 pub fn less(self, rhs: Self) -> BVec4 { bvec4(self.x < rhs.x, self.y < rhs.y, self.z < rhs.z, self.w < rhs.w) }
75 pub fn more(self, rhs: Self) -> BVec4 { bvec4(self.x > rhs.x, self.y > rhs.y, self.z > rhs.z, self.w > rhs.w) }
77 pub fn less_or_eq(self, rhs: Self) -> BVec4 {
79 bvec4(self.x <= rhs.x, self.y <= rhs.y, self.z <= rhs.z, self.w <= rhs.w)
80 }
81 pub fn more_or_eq(self, rhs: Self) -> BVec4 {
83 bvec4(self.x >= rhs.x, self.y >= rhs.y, self.z >= rhs.z, self.w >= rhs.w)
84 }
85}
86impl std::ops::Mul<DVec4> for f64 {
88 fn mul(self, rhs: DVec4) -> Self::Output { rhs * self }
89 type Output = DVec4;
90}
91impl std::ops::Mul<f64> for DVec4 {
92 fn mul(self, rhs: f64) -> Self::Output { self.scale(rhs) }
93 type Output = Self;
94}
95impl std::ops::MulAssign<f64> for DVec4 { fn mul_assign(&mut self, rhs: f64) { *self = *self * rhs } }
96impl std::ops::Div<DVec4> for f64 {
97 fn div(self, rhs: DVec4) -> Self::Output { DVec4::splat(self) / rhs }
98 type Output = DVec4;
99}
100impl std::ops::Div<f64> for DVec4 {
101 fn div(self, rhs: f64) -> Self::Output { self.scale(1.0/rhs) }
102 type Output = Self;
103}
104impl std::ops::DivAssign<f64> for DVec4 { fn div_assign(&mut self, rhs: f64) { *self = *self / rhs } }
105impl std::ops::Rem<DVec4> for f64 {
106 fn rem(self, rhs: DVec4) -> Self::Output { DVec4::splat(self) % rhs }
107 type Output = DVec4;
108}
109impl std::ops::Rem<f64> for DVec4 {
110 fn rem(self, rhs: f64) -> Self::Output { self % DVec4::splat(rhs) }
111 type Output = Self;
112}
113impl std::ops::RemAssign<f64> for DVec4 { fn rem_assign(&mut self, rhs: f64) { *self = *self % rhs } }
114
115impl AsUniformValue for DVec4 {
116 fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
117 glium::uniforms::UniformValue::DoubleVec4([self.x, self.y, self.z, self.w])
118 }
119}
120impl From<Vec4> for DVec4 {
121 fn from(value: Vec4) -> Self {
122 Self { x: value.x as f64, y: value.y as f64, z: value.z as f64, w: value.w as f64 }
123 }
124}
125impl From<(f64, f64, f64, f64)> for DVec4 {
126 fn from(value: (f64, f64, f64, f64)) -> Self {
127 Self { x: value.0, y: value.1, z: value.2, w: value.3 }
128 }
129}
130impl From<[f64; 4]> for DVec4 {
131 fn from(value: [f64; 4]) -> Self {
132 Self { x: value[0], y: value[1], z: value[2], w: value[3] }
133 }
134}
135impl From<[f32; 4]> for DVec4 {
136 fn from(value: [f32; 4]) -> Self {
137 Self { x: value[0] as f64, y: value[1] as f64, z: value[2] as f64, w: value[3] as f64 }
138 }
139}
140impl From<DVec4> for [f64; 4] {
141 fn from(value: DVec4) -> Self {
142 [value.x, value.y, value.z, value.w]
143 }
144}
145impl From<DVec4> for (f64, f64, f64, f64) {
146 fn from(value: DVec4) -> Self {
147 (value.x, value.y, value.z, value.w)
148 }
149}
150pub const fn dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec4 { DVec4 { x, y, z, w } }