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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use core::{Ignite, Scalar};
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "scalar64"))]
use std::f32::consts::PI;
#[cfg(feature = "scalar64")]
use std::f64::consts::PI;
pub use vek;
pub use vek::*;
pub type Rect = vek::Rect<Scalar, Scalar>;
pub type Vec2 = vek::Vec2<Scalar>;
pub type Vec3 = vek::Vec3<Scalar>;
pub type Vec4 = vek::Vec4<Scalar>;
pub type Rgba = vek::Rgba<Scalar>;
pub type Quat = vek::Quaternion<Scalar>;
pub type Mat2 = vek::Mat2<Scalar>;
pub type Mat3 = vek::Mat3<Scalar>;
pub type Mat4 = vek::Mat4<Scalar>;
pub type Transform = vek::Transform<Scalar, Scalar, Scalar>;
pub fn rect<T>(x: T, y: T, w: T, h: T) -> vek::Rect<T, T> {
vek::Rect::new(x, y, w, h)
}
pub fn vec2<T>(x: T, y: T) -> vek::Vec2<T> {
vek::Vec2::new(x, y)
}
pub fn vec3<T>(x: T, y: T, z: T) -> vek::Vec3<T> {
vek::Vec3::new(x, y, z)
}
pub fn vec4<T>(x: T, y: T, z: T, w: T) -> vek::Vec4<T> {
vek::Vec4::new(x, y, z, w)
}
pub fn mat2<T>(a: [[T; 2]; 2]) -> vek::Mat2<T> {
vek::Mat2::<T>::from_col_arrays(a)
}
pub fn mat3<T>(a: [[T; 3]; 3]) -> vek::Mat3<T> {
vek::Mat3::<T>::from_col_arrays(a)
}
pub fn mat4<T>(a: [[T; 4]; 4]) -> vek::Mat4<T> {
vek::Mat4::<T>::from_col_arrays(a)
}
#[derive(Ignite, Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub struct Eulers {
#[serde(default)]
pub yaw: Scalar,
#[serde(default)]
pub pitch: Scalar,
#[serde(default)]
pub roll: Scalar,
}
impl From<Vec3> for Eulers {
fn from(v: Vec3) -> Self {
Eulers {
yaw: v.z,
pitch: v.y,
roll: v.x,
}
}
}
impl From<Eulers> for Vec3 {
fn from(value: Eulers) -> Self {
Self::new(value.roll, value.pitch, value.yaw)
}
}
impl From<Quat> for Eulers {
fn from(q: Quat) -> Self {
let q = q.normalized();
let sinr_cosp = 2.0 * (q.w * q.x + q.y * q.z);
let cosr_cosp = 1.0 - 2.0 * (q.x * q.x + q.y * q.y);
let roll = sinr_cosp.atan2(cosr_cosp);
let sinp = 2.0 * (q.w * q.y - q.z * q.x);
let pitch = if sinp.abs() >= 1.0 {
PI * 0.5 * sinp.signum()
} else {
sinp.asin()
};
let siny_cosp = 2.0 * (q.w * q.z + q.x * q.y);
let cosy_cosp = 1.0 - 2.0 * (q.y * q.y + q.z * q.z);
let yaw = siny_cosp.atan2(cosy_cosp);
Eulers { yaw, pitch, roll }
}
}
impl From<Eulers> for Quat {
#[allow(clippy::many_single_char_names)]
fn from(value: Eulers) -> Self {
let v = Vec3::new(value.roll, value.pitch, value.yaw) * 0.5;
let (sy, cy) = v.z.sin_cos();
let (sp, cp) = v.y.sin_cos();
let (sr, cr) = v.x.sin_cos();
let w = cr * cp * cy + sr * sp * sy;
let x = sr * cp * cy - cr * sp * sy;
let y = cr * sp * cy + sr * cp * sy;
let z = cr * cp * sy - sr * sp * cy;
Self::from_xyzw(x, y, z, w)
}
}
#[derive(Ignite, Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub struct BoundsVolume {
pub origin: Vec3,
radius: Scalar,
half_extents: Vec3,
}
impl BoundsVolume {
pub fn from_sphere(origin: Vec3, radius: Scalar) -> Self {
let size = ((radius * radius) / 3.0).sqrt();
let half_extents = Vec3::new(size, size, size);
Self {
origin,
radius,
half_extents,
}
}
pub fn from_box(origin: Vec3, mut half_extents: Vec3) -> Self {
half_extents.x = half_extents.x.abs();
half_extents.y = half_extents.y.abs();
half_extents.z = half_extents.z.abs();
let radius = half_extents.magnitude();
Self {
origin,
radius,
half_extents,
}
}
pub fn from_points_cloud(iter: impl Iterator<Item = Vec3>) -> Option<Self> {
let mut limits = None;
for point in iter {
limits = Some(match limits {
Some((from, to)) => (Vec3::partial_min(from, point), Vec3::partial_max(to, point)),
None => (point, point),
});
}
limits.map(|(from, to)| Self::from_box((from + to) * 0.5, (to - from) * 0.5))
}
pub fn radius(&self) -> Scalar {
self.radius
}
pub fn half_extents(&self) -> Vec3 {
self.half_extents
}
pub fn overlap_point(&self, position: Vec3) -> bool {
let diff = position - self.origin;
diff.x.abs() <= self.half_extents.x
&& diff.y.abs() <= self.half_extents.y
&& diff.z.abs() <= self.half_extents.z
}
pub fn overlap_spheres(&self, other: &Self) -> bool {
let distance = (self.origin - other.origin).magnitude_squared();
let threshold = self.radius * self.radius + other.radius * other.radius;
distance <= threshold
}
pub fn overlap_boxes(&self, other: &Self) -> bool {
let from_a = self.origin - self.half_extents;
let to_a = self.origin + self.half_extents;
let from_b = other.origin - other.half_extents;
let to_b = other.origin + other.half_extents;
to_a.x > from_b.x
&& from_a.x < to_b.x
&& to_a.y > from_b.y
&& from_a.y < to_b.y
&& to_a.z > from_b.z
&& from_a.z < to_b.z
}
pub fn box_vertices(&self) -> [Vec3; 8] {
let he = self.half_extents;
[
self.origin + Vec3::new(-he.x, -he.y, -he.z),
self.origin + Vec3::new(he.x, -he.y, -he.z),
self.origin + Vec3::new(he.x, he.y, -he.z),
self.origin + Vec3::new(-he.x, he.y, -he.z),
self.origin + Vec3::new(-he.x, -he.y, he.z),
self.origin + Vec3::new(he.x, -he.y, he.z),
self.origin + Vec3::new(he.x, he.y, he.z),
self.origin + Vec3::new(-he.x, he.y, he.z),
]
}
pub fn transformed(&self, matrix: Mat4) -> Option<Self> {
Self::from_points_cloud(
self.box_vertices()
.into_iter()
.map(|p| Vec3::from(matrix * Vec4::from(p))),
)
}
pub fn distance_sphere(&self, position: Vec3) -> Scalar {
(position - self.origin).magnitude() - self.radius
}
pub fn distance_box(&self, position: Vec3) -> Vec3 {
let diff = position - self.origin;
let x = diff.x.abs() - self.half_extents.x;
let y = diff.y.abs() - self.half_extents.y;
let z = diff.z.abs() - self.half_extents.z;
Vec3::new(x, y, z)
}
pub fn distance_box_single(&self, position: Vec3) -> Scalar {
let dist = self.distance_box(position).magnitude();
if self.overlap_point(position) {
-dist
} else {
dist
}
}
}