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
use std::fmt;
use std::ops;
#[derive(Clone, Copy, PartialEq)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl fmt::Debug for Vec3 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({:?},{:?},{:?})", self.x, self.y, self.z)
}
}
impl ops::Add for Vec3 {
type Output = Vec3;
fn add(self, other: Self) -> Self::Output {
Vec3::new(self.x + other.x, self.y + other.y, self.z + other.z)
}
}
impl ops::Add<f32> for Vec3 {
type Output = Vec3;
fn add(self, other: f32) -> Self::Output {
Vec3::new(self.x + other, self.y + other, self.z + other)
}
}
impl ops::AddAssign for Vec3 {
fn add_assign(&mut self, other: Vec3) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
}
}
impl ops::Sub for Vec3 {
type Output = Vec3;
fn sub(self, other: Self) -> Self::Output {
Vec3::new(self.x - other.x, self.y - other.y, self.z - other.z)
}
}
impl ops::Sub<f32> for Vec3 {
type Output = Vec3;
fn sub(self, other: f32) -> Self::Output {
Vec3::new(self.x - other, self.y - other, self.z - other)
}
}
impl ops::SubAssign for Vec3 {
fn sub_assign(&mut self, other: Vec3) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
}
}
impl ops::Mul<f32> for Vec3 {
type Output = Vec3;
fn mul(self, s: f32) -> Self::Output {
Vec3::new(self.x * s, self.y * s, self.z * s)
}
}
impl ops::Div<f32> for Vec3 {
type Output = Vec3;
fn div(self, s: f32) -> Vec3 {
Vec3::new(self.x / s, self.y / s, self.z / s)
}
}
impl ops::Neg for Vec3 {
type Output = Vec3;
fn neg(self) -> Vec3 {
Vec3::new(-self.x, -self.y, -self.z)
}
}
impl Vec3 {
pub fn new(x: f32, y: f32, z: f32) -> Vec3 {
Vec3 { x, y, z }
}
pub fn mag(self) -> f32 {
(self.x).hypot(self.y).hypot(self.z)
}
pub fn transform_dir(self, rhs: ::Mat4) -> Self {
let x = rhs.0[0]*self.x + rhs.0[4]*self.y + rhs.0[8]*self.z;
let y = rhs.0[1]*self.x + rhs.0[5]*self.y + rhs.0[9]*self.z;
let z = rhs.0[2]*self.x + rhs.0[6]*self.y + rhs.0[10]*self.z;
Self::new(x, y, z)
}
pub fn zero() -> Self {
Vec3::new(0.0, 0.0, 0.0)
}
pub fn midpoint(self, other: Self) -> Self {
let x = (self.x + other.x) / 2.0;
let y = (self.y + other.y) / 2.0;
let z = (self.z + other.z) / 2.0;
Vec3::new(x, y, z)
}
pub fn dist_sq(self, other: Self) -> f32 {
let dx = other.x - self.x;
let dy = other.y - self.y;
let dz = other.z - self.z;
dx * dx + dy * dy + dz * dz
}
pub fn recip(self) -> Self {
Vec3::new(1.0 / self.x, 1.0 / self.y, 1.0 / self.z)
}
pub fn dot(&self, other: Vec3) -> f32 {
self.x * other.x + self.y * other.y + self.z * other.z
}
pub fn normalize(self) -> Self {
let m = self.mag();
if m > 0.0 {
self / m
} else {
Vec3::zero()
}
}
pub fn angle(&self, other: Vec3) -> f32 {
let mag1 = (self.x as f64)
.hypot(self.y as f64)
.hypot(self.z as f64);
let mag2 = (other.x as f64)
.hypot(other.y as f64)
.hypot(other.z as f64);
let dot = ((self.x as f64) * (other.x as f64))
+ ((self.y as f64) * (other.y as f64))
+ ((self.z as f64) * (other.z as f64));
(dot / (mag1 * mag2)).acos() as f32
}
}