-
Notifications
You must be signed in to change notification settings - Fork 4
/
aabb.js
197 lines (180 loc) · 4.83 KB
/
aabb.js
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
/** @module aabb */
import { avec3, vec3 } from "pex-math";
/**
* Creates a new bounding box.
* @returns {import("./types.js").aabb}
*/
export function create() {
// [min, max]
return [
[Infinity, Infinity, Infinity],
[-Infinity, -Infinity, -Infinity],
];
}
/**
* Reset a bounding box.
* @param {import("./types.js").aabb} a
* @returns {import("./types.js").rect}
*/
export function empty(a) {
a[0][0] = Infinity;
a[0][1] = Infinity;
a[0][2] = Infinity;
a[1][0] = -Infinity;
a[1][1] = -Infinity;
a[1][2] = -Infinity;
return a;
}
/**
* Copies a bounding box.
* @param {import("./types.js").aabb} a
* @returns {import("./types.js").aabb}
*/
export function copy(a) {
return [a[0].slice(), a[1].slice()];
}
/**
* Sets a bounding box to another.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").aabb} b
* @returns {import("./types.js").aabb}
*/
export function set(a, b) {
a[0][0] = b[0][0];
a[0][1] = b[0][1];
a[0][2] = b[0][2];
a[1][0] = b[1][0];
a[1][1] = b[1][1];
a[1][2] = b[1][2];
return a;
}
/**
* Checks if a bounding box is empty.
* @param {import("./types.js").aabb} a
* @returns {boolean}
*/
export function isEmpty(a) {
return a[0][0] > a[1][0] || a[0][1] > a[1][1] || a[0][2] > a[1][2];
}
/**
* Updates a bounding box from a list of points.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").vec3[] | import("./types.js").TypedArray} points
* @returns {import("./types.js").aabb}
*/
export function fromPoints(a, points) {
const isFlatArray = !points[0]?.length;
const l = points.length / (isFlatArray ? 3 : 1);
for (let i = 0; i < l; i++) {
if (isFlatArray) {
includePoint(a, points, i * 3);
} else {
includePoint(a, points[i]);
}
}
return a;
}
/**
* Returns a list of 8 points from a bounding box.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").vec3[]} [points]
* @returns {import("./types.js").vec3[]}
*/
export function getCorners(a, points = Array.from({ length: 8 }, () => [])) {
avec3.set3(points[0], 0, a[0][0], a[0][1], a[0][2]);
avec3.set3(points[1], 0, a[1][0], a[0][1], a[0][2]);
avec3.set3(points[2], 0, a[1][0], a[0][1], a[1][2]);
avec3.set3(points[3], 0, a[0][0], a[0][1], a[1][2]);
avec3.set3(points[4], 0, a[0][0], a[1][1], a[0][2]);
avec3.set3(points[5], 0, a[1][0], a[1][1], a[0][2]);
avec3.set3(points[6], 0, a[1][0], a[1][1], a[1][2]);
avec3.set3(points[7], 0, a[0][0], a[1][1], a[1][2]);
return points;
}
/**
* Returns the center of a bounding box.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").vec3} out
* @returns {import("./types.js").vec3}
*/
export function center(a, out = [0, 0, 0]) {
out[0] = (a[0][0] + a[1][0]) / 2;
out[1] = (a[0][1] + a[1][1]) / 2;
out[2] = (a[0][2] + a[1][2]) / 2;
return out;
}
/**
* Returns the size of a bounding box.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").vec3} out
* @returns {import("./types.js").vec3}
*/
export function size(a, out = [0, 0, 0]) {
out[0] = Math.abs(a[1][0] - a[0][0]);
out[1] = Math.abs(a[1][1] - a[0][1]);
out[2] = Math.abs(a[1][2] - a[0][2]);
return out;
}
/**
* Checks if a point is inside a bounding box.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").vec3} p
* @returns {boolean}
*/
export function containsPoint(a, [x, y, z]) {
return (
x >= a[0][0] &&
x <= a[1][0] &&
y >= a[0][1] &&
y <= a[1][1] &&
z >= a[0][2] &&
z <= a[1][2]
);
}
/**
* Includes a bounding box in another.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").aabb} b
* @returns {import("./types.js").aabb}
*/
export function includeAABB(a, b) {
if (isEmpty(a)) {
set(a, b);
} else if (isEmpty(b)) {
// do nothing
} else {
a[0][0] = Math.min(a[0][0], b[0][0]);
a[0][1] = Math.min(a[0][1], b[0][1]);
a[0][2] = Math.min(a[0][2], b[0][2]);
a[1][0] = Math.max(a[1][0], b[1][0]);
a[1][1] = Math.max(a[1][1], b[1][1]);
a[1][2] = Math.max(a[1][2], b[1][2]);
}
return a;
}
/**
* Includes a point in a bounding box.
* @param {import("./types.js").aabb} a
* @param {import("./types.js").vec3} p
* @param {number} [i=0] offset in the point array
* @returns {import("./types.js").vec3}
*/
export function includePoint(a, p, i = 0) {
a[0][0] = Math.min(a[0][0], p[i + 0]);
a[0][1] = Math.min(a[0][1], p[i + 1]);
a[0][2] = Math.min(a[0][2], p[i + 2]);
a[1][0] = Math.max(a[1][0], p[i + 0]);
a[1][1] = Math.max(a[1][1], p[i + 1]);
a[1][2] = Math.max(a[1][2], p[i + 2]);
return a;
}
/**
* Prints a bounding box to a string.
* @param {import("./types.js").aabb} a
* @param {number} [precision=4]
* @returns {string}
*/
export function toString(a, precision = 4) {
// prettier-ignore
return `[${vec3.toString(a[0], precision)}, ${vec3.toString(a[1], precision)}]`;
}