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)]
6pub struct DVec3{
8 pub x: f64,
9 pub y: f64,
10 pub z: f64
11}
12impl DVec3{
13 pub const ZERO: Self = dvec3(0.0, 0.0, 0.0);
15 pub const ONE: Self = dvec3(1.0, 1.0, 1.0);
17 pub const X: Self = dvec3(1.0, 0.0, 0.0);
19 pub const Y: Self = dvec3(0.0, 1.0, 0.0);
21 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 pub const fn splat(value: f64) -> Self { Self::new(value, value, value) }
29
30 pub fn length_squared(self) -> f64{
32 self.x*self.x + self.y*self.y + self.z*self.z
33 }
34 pub fn length(self) -> f64{
36 self.length_squared().sqrt()
37 }
38 pub fn distance_squared(self, other: DVec3) -> f64{
40 (self - other).length_squared()
41 }
42 pub fn distance(self, other: DVec3) -> f64{
44 (self - other).length()
45 }
46 pub fn dot(self, other: DVec3) -> f64{
48 self.x * other.x + self.y * other.y + self.z * other.z
49 }
50 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 pub fn scale(self, scalar: f64) -> DVec3{
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 dvec3(0.0, 0.0, 0.0); }
73 self.scale(1.0 / length)
74 }
75 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 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<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}
146pub const fn dvec3(x: f64, y: f64, z: f64) -> DVec3{
148 DVec3 { x, y, z }
149}