1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::*;
#[repr(C)]
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Trans)]
pub struct Vec4<T> {
pub x: T,
pub y: T,
pub z: T,
pub w: T,
}
impl<T: Display> Display for Vec4<T> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> fmt::Result {
write!(fmt, "({}, {}, {}, {})", self.x, self.y, self.z, self.w)
}
}
pub const fn vec4<T>(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x, y, z, w }
}
impl<T> From<[T; 4]> for Vec4<T> {
fn from(arr: [T; 4]) -> Vec4<T> {
let [x, y, z, w] = arr;
vec4(x, y, z, w)
}
}
impl<T> Deref for Vec4<T> {
type Target = [T; 4];
fn deref(&self) -> &[T; 4] {
unsafe { mem::transmute(self) }
}
}
impl<T> DerefMut for Vec4<T> {
fn deref_mut(&mut self) -> &mut [T; 4] {
unsafe { mem::transmute(self) }
}
}
impl<T> Vec4<T> {
pub fn xy(self) -> Vec2<T> {
vec2(self.x, self.y)
}
pub fn xyz(self) -> Vec3<T> {
vec3(self.x, self.y, self.z)
}
pub fn map<U, F: Fn(T) -> U>(self, f: F) -> Vec4<U> {
vec4(f(self.x), f(self.y), f(self.z), f(self.w))
}
}
impl<T: Copy + Num> Vec4<T> {
pub fn dot(a: Self, b: Self) -> T {
a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
}
}