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
use std::ops::Neg;
use auto_ops::*;
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature="serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Default for Vec3 {
fn default() -> Self {
Self::zero()
}
}
impl Vec3 {
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self{x, y, z}
}
pub const fn zero() -> Self {
Self{x: 0.0, y: 0.0, z: 0.0}
}
pub const fn one() -> Self {
Self{x: 1.0, y: 1.0, z: 1.0}
}
pub fn sqr_magnitude(&self) -> f32 {
self.x * self.x + self.y * self.y + self.z * self.z
}
pub fn magnitude(&self) -> f32 {
self.sqr_magnitude().sqrt()
}
pub fn normalize(&mut self) -> &mut Self {
let m = self.magnitude();
self.x /= m;
self.y /= m;
self.z /= m;
self
}
pub fn normalized(&self) -> Self {
*self.clone().normalize()
}
pub fn dot(&self, b: Vec3) -> f32 {
self.x * b.x + self.y * b.y + self.z * b.z
}
pub fn cross(&self, b: Vec3) -> Vec3 {
Vec3{
x: self.y * b.z - self.z * b.y,
y: self.z * b.x - self.x * b.z,
z: self.x * b.y - self.y * b.x,
}
}
}
impl_op_ex!(+= |a: &mut Vec3, b: &Vec3| { a.x += b.x; a.y += b.y; a.z += b.z; });
impl_op_ex!(-= |a: &mut Vec3, b: &Vec3| { a.x -= b.x; a.y -= b.y; a.z -= b.z; });
impl_op_ex!(*= |a: &mut Vec3, b: &Vec3| { a.x *= b.x; a.y *= b.y; a.z *= b.z; });
impl_op_ex!(/= |a: &mut Vec3, b: &Vec3| { a.x /= b.x; a.y /= b.y; a.z /= b.z; });
impl_op_ex!(*= |a: &mut Vec3, b: &f32| { a.x *= b; a.y *= b; a.z *= b; });
impl_op_ex!(/= |a: &mut Vec3, b: &f32| { a.x /= b; a.y /= b; a.z /= b; });
impl_op_ex!(+ |a: &Vec3, b: &Vec3| -> Vec3 { Vec3{x: a.x + b.x, y: a.y + b.y, z: a.z + b.z } });
impl_op_ex!(- |a: &Vec3, b: &Vec3| -> Vec3 { Vec3{x: a.x - b.x, y: a.y - b.y, z: a.z - b.z } });
impl_op_ex!(* |a: &Vec3, b: &Vec3| -> Vec3 { Vec3{x: a.x * b.x, y: a.y * b.y, z: a.z * b.z } });
impl_op_ex!(/ |a: &Vec3, b: &Vec3| -> Vec3 { Vec3{x: a.x / b.x, y: a.y / b.y, z: a.z / b.z } });
impl_op_ex_commutative!(* |a: &Vec3, b: &f32| -> Vec3 { Vec3{x: a.x * b, y: a.y * b, z: a.z * b } });
impl_op_ex!(/ |a: &Vec3, b: &f32| -> Vec3 { Vec3{x: a.x / b, y: a.y / b, z: a.z / b } });
impl_op_ex!(/ |a: &f32, b: &Vec3| -> Vec3 { Vec3{x: a / b.x, y: a / b.y, z: a / b.z } });
impl Neg for Vec3 {
type Output = Vec3;
fn neg(self) -> Self::Output {
Vec3{x: -self.x, y: -self.y, z: -self.z}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn operators() {
let a = Vec3::new(1.0, 2.0, 3.0);
let b = Vec3::new(3.0, 4.0, 5.0);
assert_eq!(-a, Vec3{x: -1.0, y: -2.0, z: -3.0});
assert_eq!(a.sqr_magnitude(), 14.0);
assert_eq!(a.magnitude(), 14.0f32.sqrt());
assert_eq!(a.dot(b), 26.0);
assert_eq!(a + b, Vec3{x: 4.0, y: 6.0, z: 8.0});
assert_eq!(a - b, Vec3{x: -2.0, y: -2.0, z: -2.0});
assert_eq!(a * b, Vec3{x: 3.0, y: 8.0, z: 15.0});
assert_eq!(a / b, Vec3{x: 1.0 / 3.0, y: 0.5, z: 3.0/5.0});
assert_eq!(a * 2.0, Vec3{x: 2.0, y: 4.0, z: 6.0});
assert_eq!(2.0 * a, Vec3{x: 2.0, y: 4.0, z: 6.0});
assert_eq!(a / 2.0, Vec3{x: 0.5, y: 1.0, z: 1.5});
assert_eq!(2.0 / a, Vec3{x: 2.0, y: 1.0, z: 2.0/3.0});
let mut c = a;
assert_eq!(c.normalized(), a / a.magnitude());
c.normalize();
assert_eq!(c, a / a.magnitude());
c = a;
c += b;
assert_eq!(c, a + b);
c = a;
c -= b;
assert_eq!(c, a - b);
c = a;
c *= b;
assert_eq!(c, a * b);
c = a;
c /= b;
assert_eq!(c, a / b);
c = a;
c *= 2.0;
assert_eq!(c, a * 2.0);
c = a;
c /= 2.0;
assert_eq!(c, a / 2.0);
}
}