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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
use std::ops::{Neg, Sub};
#[derive(Debug)]
pub struct Vec2<Value>
{
pub x: Value,
pub y: Value,
}
impl Vec2<u32>
{
pub const ZERO: Self = Self { x: 0, y: 0 };
}
#[derive(Debug, Default, Clone)]
#[repr(C)]
pub struct Vec3<Value>
{
pub x: Value,
pub y: Value,
pub z: Value,
}
impl Vec3<f32>
{
pub const UP: Self = Self {
x: 0.0,
y: 1.0,
z: 0.0,
};
/// Returns the length of the vector.
#[must_use]
pub fn length(&self) -> f32
{
(self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
}
/// Normalizes the vector, returning a unit vector.
#[must_use]
pub fn normalize(&self) -> Self
{
let length = self.length();
Self {
x: self.x / length,
y: self.y / length,
z: self.z / length,
}
}
/// Returns the cross product of this and another vector.
#[must_use]
pub fn cross(&self, rhs: &Self) -> Self
{
Self {
x: (self.y * rhs.z) - (self.z * rhs.y),
y: (self.z * rhs.x) - (self.x * rhs.z),
z: (self.x * rhs.y) - (self.y * rhs.x),
}
}
/// Returns the dot product of this and another vector.
#[must_use]
pub fn dot(&self, rhs: &Self) -> f32
{
(self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
}
}
impl<Value> Sub for Vec3<Value>
where
Value: Sub<Value, Output = Value>,
{
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output
{
Self::Output {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
impl<Value> Sub for &Vec3<Value>
where
for<'a, 'b> &'a Value: Sub<&'b Value, Output = Value>,
{
type Output = Vec3<Value>;
fn sub(self, rhs: Self) -> Self::Output
{
Self::Output {
x: &self.x - &rhs.x,
y: &self.y - &rhs.y,
z: &self.z - &rhs.z,
}
}
}
impl<Value> Neg for Vec3<Value>
where
Value: Neg<Output = Value>,
{
type Output = Self;
fn neg(mut self) -> Self::Output
{
self.x = -self.x;
self.y = -self.y;
self.z = -self.z;
self
}
}
|