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
pub use self::aabb2::Aabb2;
pub use self::aabb3::Aabb3;
use std::cmp::{Ordering, PartialOrd};
use cgmath::{BaseNum, Point2, Point3};
use cgmath::prelude::*;
use traits::Bound;
mod aabb2;
mod aabb3;
pub(crate) fn min<S: PartialOrd + Copy>(lhs: S, rhs: S) -> S {
match lhs.partial_cmp(&rhs) {
Some(Ordering::Less) | Some(Ordering::Equal) | None => lhs,
_ => rhs,
}
}
pub(crate) fn max<S: PartialOrd + Copy>(lhs: S, rhs: S) -> S {
match lhs.partial_cmp(&rhs) {
Some(Ordering::Greater) | Some(Ordering::Equal) | None => lhs,
_ => rhs,
}
}
pub trait MinMax {
fn min(a: Self, b: Self) -> Self;
fn max(a: Self, b: Self) -> Self;
}
impl<S: PartialOrd> MinMax for Point2<S>
where
S: BaseNum,
{
fn min(a: Point2<S>, b: Point2<S>) -> Point2<S> {
Point2::new(min(a.x, b.x), min(a.y, b.y))
}
fn max(a: Point2<S>, b: Point2<S>) -> Point2<S> {
Point2::new(max(a.x, b.x), max(a.y, b.y))
}
}
impl<S: PartialOrd> MinMax for Point3<S>
where
S: BaseNum,
{
fn min(a: Point3<S>, b: Point3<S>) -> Point3<S> {
Point3::new(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z))
}
fn max(a: Point3<S>, b: Point3<S>) -> Point3<S> {
Point3::new(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z))
}
}
pub trait Aabb: Sized {
type Scalar: BaseNum;
type Diff: VectorSpace<Scalar = Self::Scalar> + ElementWise + Array<Element = Self::Scalar>;
type Point: EuclideanSpace<Scalar = Self::Scalar, Diff = Self::Diff> + MinMax;
fn new(p1: Self::Point, p2: Self::Point) -> Self;
fn zero() -> Self {
let p = Self::Point::origin();
Self::new(p, p)
}
fn min(&self) -> Self::Point;
fn max(&self) -> Self::Point;
#[inline]
fn dim(&self) -> Self::Diff {
self.max() - self.min()
}
#[inline]
fn volume(&self) -> Self::Scalar {
self.dim().product()
}
#[inline]
fn center(&self) -> Self::Point {
let two = Self::Scalar::one() + Self::Scalar::one();
self.min() + self.dim() / two
}
fn grow(&self, p: Self::Point) -> Self {
Aabb::new(MinMax::min(self.min(), p), MinMax::max(self.max(), p))
}
#[inline]
fn add_v(&self, v: Self::Diff) -> Self {
Aabb::new(self.min() + v, self.max() + v)
}
fn add_margin(&self, margin: Self::Diff) -> Self;
#[inline]
fn mul_s(&self, s: Self::Scalar) -> Self {
Aabb::new(self.min() * s, self.max() * s)
}
fn mul_v(&self, v: Self::Diff) -> Self {
let min = Self::Point::from_vec(self.min().to_vec().mul_element_wise(v));
let max = Self::Point::from_vec(self.max().to_vec().mul_element_wise(v));
Aabb::new(min, max)
}
fn transform<T>(&self, transform: &T) -> Self
where
T: Transform<Self::Point>;
}
impl<A> Bound for A
where
A: Aabb,
A::Point: EuclideanSpace,
{
type Point = A::Point;
fn min_extent(&self) -> A::Point {
self.min()
}
fn max_extent(&self) -> A::Point {
self.max()
}
fn with_margin(&self, add: <A::Point as EuclideanSpace>::Diff) -> Self {
self.add_margin(add)
}
fn transform_volume<T>(&self, transform: &T) -> Self
where
T: Transform<Self::Point>,
{
self.transform(transform)
}
fn empty() -> Self {
A::zero()
}
}