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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use core::iter::{once, Chain, Once};
use core::ops;
use crate::math::EPSILON;
#[derive(Clone, Copy, Debug, Default)]
pub struct Vec3 {
x: f32,
y: f32,
z: f32,
}
impl ops::Add for Vec3 {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self::new(self.x + other.x, self.y + other.y, self.z + other.z)
}
}
impl ops::AddAssign for Vec3 {
fn add_assign(&mut self, other: Self) {
*self = *self + other
}
}
impl ops::Sub for Vec3 {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self::new(self.x - other.x, self.y - other.y, self.z - other.z)
}
}
impl ops::SubAssign for Vec3 {
fn sub_assign(&mut self, other: Self) {
*self = *self - other
}
}
impl ops::Neg for Vec3 {
type Output = Self;
fn neg(self) -> Self::Output {
Self::new(-self.x, -self.y, -self.z)
}
}
impl ops::Mul<f32> for Vec3 {
type Output = Self;
fn mul(self, scale: f32) -> Self::Output {
Self::new(self.x * scale, self.y * scale, self.z * scale)
}
}
impl ops::Mul<Vec3> for f32 {
type Output = Vec3;
fn mul(self, rhs: Vec3) -> Self::Output {
rhs * self
}
}
impl ops::MulAssign<f32> for Vec3 {
fn mul_assign(&mut self, scale: f32) {
*self = *self * scale
}
}
impl ops::Mul for Vec3 {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
Self::new(self.x * other.x, self.y * other.y, self.z * other.z)
}
}
impl ops::MulAssign for Vec3 {
fn mul_assign(&mut self, other: Self) {
*self = *self * other
}
}
impl ops::Div<f32> for Vec3 {
type Output = Self;
fn div(self, scale: f32) -> Self::Output {
Self::new(self.x / scale, self.y / scale, self.z / scale)
}
}
impl ops::DivAssign<f32> for Vec3 {
fn div_assign(&mut self, scale: f32) {
*self = *self / scale
}
}
impl ops::Div for Vec3 {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
Self::new(self.x / other.x, self.y / other.y, self.z / other.z)
}
}
impl ops::DivAssign for Vec3 {
fn div_assign(&mut self, other: Self) {
*self = *self / other
}
}
impl PartialEq for Vec3 {
fn eq(&self, other: &Self) -> bool {
f32::abs(self.x - other.x) < EPSILON
&& f32::abs(self.y - other.y) < EPSILON
&& f32::abs(self.z - other.z) < EPSILON
}
}
impl Eq for Vec3 {}
impl From<(f32, f32, f32)> for Vec3 {
fn from((x, y, z): (f32, f32, f32)) -> Self {
Self::new(x, y, z)
}
}
impl From<Vec3> for (f32, f32, f32) {
fn from(vec: Vec3) -> Self {
(vec.x(), vec.y(), vec.z())
}
}
impl From<[f32; 3]> for Vec3 {
fn from([x, y, z]: [f32; 3]) -> Self {
Self::new(x, y, z)
}
}
impl From<Vec3> for [f32; 3] {
fn from(vec: Vec3) -> Self {
[vec.x(), vec.y(), vec.z()]
}
}
pub type IntoIter = Chain<Once<f32>, Chain<Once<f32>, Once<f32>>>;
impl IntoIterator for Vec3 {
type Item = f32;
type IntoIter = IntoIter;
fn into_iter(self) -> Self::IntoIter {
once(self.x).chain(once(self.y).chain(once(self.z)))
}
}
impl Vec3 {
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
pub const fn zero() -> Self {
Self::new(0.0, 0.0, 0.0)
}
pub const fn x(self) -> f32 {
self.x
}
pub const fn y(self) -> f32 {
self.y
}
pub const fn z(self) -> f32 {
self.z
}
pub fn dot(self, other: Self) -> f32 {
self.x * other.x + self.y * other.y + self.z * other.z
}
pub fn norm2(self) -> f32 {
self.dot(self)
}
pub fn norm(self) -> f32 {
f32::sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
}
pub fn normalized(self) -> Self {
self / self.norm()
}
pub fn into_vec2(self) -> super::Vec2 {
super::Vec2::new(self.x(), self.y())
}
}