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
use cgmath;
use mint;
use std::{mem, ops};
use approx::ApproxEq;
use cgmath::InnerSpace;
use {Vec2, Vec4};
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Vec3 {
pub fn dot(self, other: Vec3) -> f32 {
let left = cgmath::Vector3::new(self.x, self.y, self.z);
let right = cgmath::Vector3::new(other.x, other.y, other.z);
left.dot(right)
}
pub fn cross(self, other: Vec3) -> Vec3 {
let left = cgmath::Vector3::new(self.x, self.y, self.z);
let right = cgmath::Vector3::new(other.x, other.y, other.z);
let result = left.cross(right);
vec3!(result.x, result.y, result.z)
}
pub fn normalize(self) -> Vec3 {
let n = cgmath::Vector3::new(self.x, self.y, self.z).normalize();
vec3!(n.x, n.y, n.z)
}
pub fn xy(self) -> Vec2 {
vec2!(self.x, self.y)
}
}
impl AsRef<[f32; 3]> for Vec3 {
fn as_ref(&self) -> &[f32; 3] {
unsafe {
mem::transmute(self)
}
}
}
impl From<[f32; 3]> for Vec3 {
fn from(v: [f32; 3]) -> Vec3 {
vec3!(v[0], v[1], v[2])
}
}
impl Into<[f32; 3]> for Vec3 {
fn into(self) -> [f32; 3] {
[self.x, self.y, self.z]
}
}
impl From<Vec4> for Vec3 {
fn from(vec4: Vec4) -> Vec3 {
Vec3 { x: vec4.x, y: vec4.y, z: vec4.z }
}
}
impl ops::Mul<f32> for Vec3 {
type Output = Vec3;
fn mul(self, rhs: f32) -> Self::Output {
Vec3 {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs,
}
}
}
impl ops::Mul<Vec3> for f32 {
type Output = Vec3;
fn mul(self, rhs: Vec3) -> Self::Output {
rhs.mul(self)
}
}
impl ops::Div<f32> for Vec3 {
type Output = Vec3;
fn div(self, rhs: f32) -> Self::Output {
Vec3 {
x: self.x / rhs,
y: self.y / rhs,
z: self.z / rhs,
}
}
}
impl ops::Div<Vec3> for f32 {
type Output = Vec3;
fn div(self, rhs: Vec3) -> Self::Output {
rhs.div(self)
}
}
impl ops::Add<Vec3> for Vec3 {
type Output = Vec3;
fn add(self, rhs: Vec3) -> Self::Output {
Vec3 {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl ops::Sub<Vec3> for Vec3 {
type Output = Vec3;
fn sub(self, rhs: Vec3) -> Self::Output {
Vec3 {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
impl ApproxEq for Vec3 {
type Epsilon = <f32 as ApproxEq>::Epsilon;
fn default_epsilon() -> Self::Epsilon {
<f32 as ApproxEq>::default_epsilon()
}
fn default_max_relative() -> Self::Epsilon {
<f32 as ApproxEq>::default_max_relative()
}
fn default_max_ulps() -> u32 {
<f32 as ApproxEq>::default_max_ulps()
}
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
<f32 as ApproxEq>::relative_eq(&self.x, &other.x, epsilon, max_relative)
&&
<f32 as ApproxEq>::relative_eq(&self.y, &other.y, epsilon, max_relative)
&&
<f32 as ApproxEq>::relative_eq(&self.z, &other.z, epsilon, max_relative)
}
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
<f32 as ApproxEq>::ulps_eq(&self.x, &other.x, epsilon, max_ulps)
&&
<f32 as ApproxEq>::ulps_eq(&self.y, &other.y, epsilon, max_ulps)
&&
<f32 as ApproxEq>::ulps_eq(&self.z, &other.z, epsilon, max_ulps)
}
}
impl From<mint::Point3<f32>> for Vec3 {
fn from(m: mint::Point3<f32>) -> Self {
let m: [f32; 3] = m.into();
Vec3::from(m)
}
}
impl Into<mint::Point3<f32>> for Vec3 {
fn into(self) -> mint::Point3<f32> {
let m: [f32; 3] = self.into();
mint::Point3::from(m)
}
}
impl From<mint::Vector3<f32>> for Vec3 {
fn from(m: mint::Vector3<f32>) -> Self {
let m: [f32; 3] = m.into();
Vec3::from(m)
}
}
impl Into<mint::Vector3<f32>> for Vec3 {
fn into(self) -> mint::Vector3<f32> {
let m: [f32; 3] = self.into();
mint::Vector3::from(m)
}
}