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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use alga::general::SupersetOf;
use nalgebra::{Point3, Scalar, Unit};
use num::{Bounded, One, Zero};
use std::ops::Range;
use clamp::{Clamped, ClampedRange};
use math::{self, FRay3, FPoint3, FromSpace, FScalar, FVector3, LowerBound, Mask, UPoint3,
UpperBound, UScalar, UVector3};
#[derive(Clone, Copy)]
pub struct LogWidthRange;
impl ClampedRange<u8> for LogWidthRange {
fn max_value() -> u8 {
31
}
fn min_value() -> u8 {
4
}
}
pub type LogWidth = Clamped<u8, LogWidthRange>;
impl LogWidth {
pub fn unit() -> Self {
LogWidth::new(8)
}
pub fn exp(&self) -> UScalar {
UScalar::one() << self.to_inner()
}
}
#[derive(Clone, Copy)]
pub enum Axis {
X = 0,
Y = 1,
Z = 2,
}
impl Axis {
pub fn range() -> Range<usize> {
(Axis::X as usize)..(Axis::Z as usize + 1)
}
pub fn to_vector(&self) -> FVector3 {
match *self {
Axis::X => FVector3::x(),
Axis::Y => FVector3::y(),
Axis::Z => FVector3::z(),
}
}
}
impl From<usize> for Axis {
fn from(index: usize) -> Self {
match index {
0 => Axis::X,
1 => Axis::Y,
2 => Axis::Z,
_ => panic!(),
}
}
}
pub enum Direction {
Positive,
Negative,
}
pub enum Orientation {
Left,
Right,
Top,
Bottom,
Front,
Back,
}
impl Orientation {
pub fn axis(&self) -> Axis {
match *self {
Orientation::Left | Orientation::Right => Axis::X,
Orientation::Top | Orientation::Bottom => Axis::Y,
Orientation::Front | Orientation::Back => Axis::Z,
}
}
pub fn direction(&self) -> Direction {
match *self {
Orientation::Left | Orientation::Bottom | Orientation::Back => Direction::Positive,
Orientation::Right | Orientation::Top | Orientation::Front => Direction::Negative,
}
}
}
pub trait PointNormal {
fn normal(&self, point: &FPoint3) -> FVector3;
}
pub trait Intersects<T> {
fn intersects(&self, other: &T) -> bool;
}
pub struct RayIntersection {
pub distance: FScalar,
pub point: FPoint3,
pub normal: Unit<FVector3>,
}
impl RayIntersection {
fn new(distance: FScalar, point: FPoint3, normal: FVector3) -> Self {
RayIntersection {
distance: distance,
point: point,
normal: Unit::new_normalize(normal),
}
}
}
pub trait PartialRayCast {
fn partial_ray_intersection(&self, ray: &FRay3) -> Option<(FScalar, FScalar)>;
}
pub trait RayCast: PartialRayCast {
fn ray_intersection(&self, ray: &FRay3) -> Option<RayIntersection>;
}
impl<T> Intersects<FRay3> for T
where
T: PartialRayCast,
{
fn intersects(&self, ray: &FRay3) -> bool {
self.partial_ray_intersection(ray).is_some()
}
}
impl<T> RayCast for T
where
T: PartialRayCast + PointNormal,
{
fn ray_intersection(&self, ray: &FRay3) -> Option<RayIntersection> {
self.partial_ray_intersection(ray).map(|(distance, _)| {
let point = ray.origin + (*ray.direction * distance);
RayIntersection::new(distance, point, self.normal(&point))
})
}
}
pub struct AABB {
pub origin: UPoint3,
pub extent: UVector3,
}
impl AABB {
pub fn new(origin: UPoint3, extent: UVector3) -> Self {
AABB {
origin: origin,
extent: extent,
}
}
pub fn union(&self, other: &Self) -> Self {
let start = self.origin.lower_bound(&other.origin);
let end = self.endpoint().upper_bound(&other.endpoint());
AABB::new(start, end - start)
}
pub fn midpoint(&self) -> UPoint3 {
self.origin + (self.extent / 2)
}
pub fn endpoint(&self) -> UPoint3 {
self.origin + self.extent
}
}
impl Intersects<AABB> for AABB {
fn intersects(&self, aabb: &AABB) -> bool {
for axis in Axis::range() {
if (self.origin[axis] + self.extent[axis]) < aabb.origin[axis] {
return false;
}
if self.origin[axis] > (aabb.origin[axis] + aabb.extent[axis]) {
return false;
}
}
true
}
}
impl<T> Intersects<Point3<T>> for AABB
where
T: PartialOrd + Scalar + SupersetOf<UScalar>,
{
fn intersects(&self, point: &Point3<T>) -> bool {
use nalgebra::convert;
for axis in Axis::range() {
if convert::<UScalar, T>(self.origin[axis] + self.extent[axis]) < point[axis] {
return false;
}
if convert::<UScalar, T>(self.origin[axis]) > point[axis] {
return false;
}
}
true
}
}
impl PartialRayCast for AABB {
fn partial_ray_intersection(&self, ray: &FRay3) -> Option<(FScalar, FScalar)> {
let mut min = FVector3::zero();
let mut max = FVector3::zero();
for axis in Axis::range() {
let lower = self.origin[axis] as FScalar;
let upper = lower + self.extent[axis] as FScalar;
let origin = ray.origin[axis];
let direction = ray.direction[axis];
let (lower, upper) =
math::ordered_pair((lower - origin) / direction, (upper - origin) / direction);
min[axis] = lower;
max[axis] = upper;
}
let min = math::partial_max(math::partial_max(min.x, min.y), min.z);
let max = math::partial_min(math::partial_min(max.x, max.y), max.z);
if max < 0.0 || min > max {
None
}
else {
Some((min, max))
}
}
}
impl PointNormal for AABB {
fn normal(&self, point: &FPoint3) -> FVector3 {
let point = point - FPoint3::from_space(self.midpoint());
let mut min_distance = FScalar::max_value();
let mut normal = FVector3::zero();
for axis in Axis::range() {
let distance = (self.extent[axis] as FScalar - point[axis].abs()).abs();
if distance < min_distance {
min_distance = distance;
normal = Axis::from(axis).to_vector() * point[axis].signum();
}
}
normal
}
}
#[derive(Clone, Copy)]
pub struct Partition {
origin: UPoint3,
width: LogWidth,
}
impl Partition {
pub fn at_point(point: &UPoint3, width: LogWidth) -> Self {
Partition {
origin: point.mask(!UScalar::zero() << width.to_inner()),
width: width,
}
}
pub fn at_index(&self, index: usize) -> Option<Self> {
if self.is_min_width() {
None
}
else {
let width = self.width - 1;
Some(Partition {
origin: self.origin + vector_at_index(index, width),
width: width,
})
}
}
pub fn origin(&self) -> &UPoint3 {
&self.origin
}
pub fn width(&self) -> LogWidth {
self.width
}
pub fn midpoint(&self) -> UPoint3 {
let m = (self.width - 1).exp();
self.origin + UVector3::new(m, m, m)
}
pub fn extent(&self) -> UVector3 {
(UVector3::new(1, 1, 1) * self.width.exp()) - UVector3::new(1, 1, 1)
}
pub fn aabb(&self) -> AABB {
AABB::new(self.origin, self.extent())
}
pub fn is_min_width(&self) -> bool {
self.width == LogWidth::min_value()
}
}
pub trait Spatial {
fn partition(&self) -> &Partition;
fn depth(&self) -> u8;
fn aabb(&self) -> AABB {
self.partition().aabb()
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn index_at_point(point: &UPoint3, width: LogWidth) -> usize {
let width = width.to_inner();
(( (point.x >> width) & UScalar::one() ) |
(((point.y >> width) & UScalar::one()) << 1) |
(((point.z >> width) & UScalar::one()) << 2)) as usize
}
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn vector_at_index(index: usize, width: LogWidth) -> UVector3 {
assert!(index < 8);
let index = index as UScalar;
let width = width.exp();
UVector3::new(
( index & UScalar::one()) * width,
((index >> 1) & UScalar::one()) * width,
((index >> 2) & UScalar::one()) * width,
)
}