Skip to content

Commit 0826b88

Browse files
committed
feat: update code, docs and tests
- feat: use default argument values - feat: ray intersection enum - feat: hitTestTriangle only has 4 return values (TRIANGLE_DEGENERATE, SAME_PLANE, NO_INTERSECT, INTERSECT) - feat: add aabb.setPoints - feat: make aabb.set return a - feat: use aabb.includePoint in aabb.setPoints - fix: make rect.setPosition use param width before it is changed - fix: make rect.getCenter based on rect min (handle negative rect) - feat: remove rect.zero - feat: rename getWidth/getHeight/getSize/getAspectRatio/getCenter (remove get) - feat: add rect.setPoints and rename createFromPoints to fromPoints - feat: add rect.getPoints - feat: add aabb.empty - feat: rename rect.setEmpty to rect.empty - feat: use slice in aabb.copy - test: add mocha and write tests - upgrade to pex-math 3+ BREAKING CHANGE: API rename and removals
1 parent 8917754 commit 0826b88

12 files changed

+2576
-1286
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
.DS_Store
33
types
44
lib
5+
coverage

aabb.js

+127-40
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,151 @@
1+
/**
2+
* @typedef {number[][]} aabb An axis-aligned bounding box defined by two min and max 3D points.
3+
*/
4+
5+
/**
6+
* Creates a new bounding box.
7+
* @returns {aabb}
8+
*/
19
export function create() {
2-
const min = [Infinity, Infinity, Infinity];
3-
const max = [-Infinity, -Infinity, -Infinity];
4-
return [min, max];
10+
// [min, max]
11+
return [
12+
[Infinity, Infinity, Infinity],
13+
[-Infinity, -Infinity, -Infinity],
14+
];
15+
}
16+
17+
/**
18+
* Reset a bounding box.
19+
* @param {aabb} a
20+
* @returns {rect}
21+
*/
22+
export function empty(a) {
23+
a[0][0] = Infinity;
24+
a[0][1] = Infinity;
25+
a[0][2] = Infinity;
26+
a[1][0] = -Infinity;
27+
a[1][1] = -Infinity;
28+
a[1][2] = -Infinity;
29+
return a;
530
}
631

32+
/**
33+
* Copies a bounding box.
34+
* @param {aabb} a
35+
* @returns {aabb}
36+
*/
37+
export function copy(a) {
38+
return [a[0].slice(), a[1].slice()];
39+
}
40+
41+
/**
42+
* Sets a bounding box to another.
43+
* @param {aabb} a
44+
* @param {aabb} b
45+
* @returns {aabb}
46+
*/
747
export function set(a, b) {
848
a[0][0] = b[0][0];
949
a[0][1] = b[0][1];
1050
a[0][2] = b[0][2];
1151
a[1][0] = b[1][0];
1252
a[1][1] = b[1][1];
1353
a[1][2] = b[1][2];
54+
return a;
1455
}
1556

16-
export function copy(b) {
17-
const a = create();
18-
set(a, b);
19-
return a;
57+
/**
58+
* Checks if a bounding box is empty.
59+
* @param {aabb} aabb
60+
* @returns {boolean}
61+
*/
62+
export function isEmpty(a) {
63+
return a[0][0] > a[1][0] || a[0][1] > a[1][1] || a[0][2] > a[1][2];
2064
}
2165

66+
/**
67+
* Creates a bounding box from a list of points.
68+
* @param {import("pex-math").vec3[]} points
69+
* @returns {aabb}
70+
*/
2271
export function fromPoints(points) {
23-
const aabb = create();
24-
const min = aabb[0];
25-
const max = aabb[1];
72+
return setPoints(create(), points);
73+
}
2674

27-
for (let i = 0, len = points.length; i < len; i++) {
28-
const p = points[i];
29-
min[0] = Math.min(min[0], p[0]);
30-
min[1] = Math.min(min[1], p[1]);
31-
min[2] = Math.min(min[2], p[2]);
32-
max[0] = Math.max(max[0], p[0]);
33-
max[1] = Math.max(max[1], p[1]);
34-
max[2] = Math.max(max[2], p[2]);
75+
/**
76+
* Updates a bounding box from a list of points.
77+
* @param {aabb} a
78+
* @param {import("pex-math").vec3[]} points
79+
* @returns {aabb}
80+
*/
81+
export function setPoints(a, points) {
82+
for (let i = 0; i < points.length; i++) {
83+
includePoint(a, points[i]);
3584
}
3685

37-
return aabb;
86+
return a;
3887
}
3988

40-
export function center(aabb, out) {
41-
if (out === undefined) {
42-
out = [0, 0, 0];
43-
}
44-
out[0] = (aabb[0][0] + aabb[1][0]) / 2;
45-
out[1] = (aabb[0][1] + aabb[1][1]) / 2;
46-
out[2] = (aabb[0][2] + aabb[1][2]) / 2;
47-
return out;
89+
/**
90+
* @private
91+
*/
92+
function setVec3(v = [], x, y, z) {
93+
v[0] = x;
94+
v[1] = y;
95+
v[2] = z;
96+
return v;
4897
}
4998

50-
export function size(aabb, out) {
51-
if (out === undefined) {
52-
out = [0, 0, 0];
53-
}
54-
out[0] = Math.abs(aabb[1][0] - aabb[0][0]);
55-
out[1] = Math.abs(aabb[1][1] - aabb[0][1]);
56-
out[2] = Math.abs(aabb[1][2] - aabb[0][2]);
99+
/**
100+
* Returns a list of 8 points from a bounding box.
101+
* @param {aabb} aabb
102+
* @param {import("pex-math").vec3[]} points
103+
* @returns
104+
*/
105+
export function getPoints(a, points = []) {
106+
points[0] = setVec3(points[0], a[0][0], a[0][1], a[0][2]);
107+
points[1] = setVec3(points[1], a[1][0], a[0][1], a[0][2]);
108+
points[2] = setVec3(points[2], a[1][0], a[0][1], a[1][2]);
109+
points[3] = setVec3(points[3], a[0][0], a[0][1], a[1][2]);
110+
points[4] = setVec3(points[4], a[0][0], a[1][1], a[0][2]);
111+
points[5] = setVec3(points[5], a[1][0], a[1][1], a[0][2]);
112+
points[6] = setVec3(points[6], a[1][0], a[1][1], a[1][2]);
113+
points[7] = setVec3(points[7], a[0][0], a[1][1], a[1][2]);
114+
return points;
115+
}
116+
117+
/**
118+
* Returns the center of a bounding box.
119+
* @param {aabb} a
120+
* @param {import("pex-math").vec3} out
121+
* @returns {import("pex-math").vec3}
122+
*/
123+
export function center(a, out = [0, 0, 0]) {
124+
out[0] = (a[0][0] + a[1][0]) / 2;
125+
out[1] = (a[0][1] + a[1][1]) / 2;
126+
out[2] = (a[0][2] + a[1][2]) / 2;
57127
return out;
58128
}
59129

60-
export function isEmpty(aabb) {
61-
return (
62-
aabb[0][0] > aabb[1][0] ||
63-
aabb[0][1] > aabb[1][1] ||
64-
aabb[0][2] > aabb[1][2]
65-
);
130+
/**
131+
* Returns the size of a bounding box.
132+
* @param {aabb} a
133+
* @param {import("pex-math").vec3} out
134+
* @returns {import("pex-math").vec3}
135+
*/
136+
export function size(a, out = [0, 0, 0]) {
137+
out[0] = Math.abs(a[1][0] - a[0][0]);
138+
out[1] = Math.abs(a[1][1] - a[0][1]);
139+
out[2] = Math.abs(a[1][2] - a[0][2]);
140+
return out;
66141
}
67142

143+
/**
144+
* Includes a bounding box in another.
145+
* @param {aabb} a
146+
* @param {aabb} b
147+
* @returns {aabb}
148+
*/
68149
export function includeAABB(a, b) {
69150
if (isEmpty(a)) {
70151
set(a, b);
@@ -82,6 +163,12 @@ export function includeAABB(a, b) {
82163
return a;
83164
}
84165

166+
/**
167+
* Includes a point in a bounding box.
168+
* @param {aabb} a
169+
* @param {import("pex-math").vec3} p
170+
* @returns {import("pex-math").vec3}
171+
*/
85172
export function includePoint(a, p) {
86173
a[0][0] = Math.min(a[0][0], p[0]);
87174
a[0][1] = Math.min(a[0][1], p[1]);

0 commit comments

Comments
 (0)