-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbox.ts
110 lines (93 loc) · 3.02 KB
/
box.ts
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
import { Coords3D } from './type';
class BoundingBox {
public xMin: number;
public xMax: number;
public yMin: number;
public yMax: number;
public zMin: number;
public zMax: number;
constructor(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) {
this.xMin = xMin;
this.xMax = xMax;
this.yMin = yMin;
this.yMax = yMax;
this.zMin = zMin;
this.zMax = zMax;
}
public get xCenter() {
return this.xMax - Math.abs(this.xMax - this.xMin);
}
public get yCenter() {
return this.yMax - Math.abs(this.yMax - this.yMin);
}
public get zCenter() {
return this.zMax - Math.abs(this.zMax - this.zMin);
}
public static createFromPoints(objectPoints: Coords3D, xOffset = 0) {
let box = undefined;
for (let i = 0; i < objectPoints.length; i++) {
const p = objectPoints[i];
const x = p[0];
const y = p[1];
const z = p[2];
if (box === undefined) {
box = {
xMin: x,
xMax: x,
yMin: y,
yMax: y,
zMin: z,
zMax: z,
};
}
if (x < box.xMin) box.xMin = x;
if (x > box.xMax) box.xMax = x;
if (y < box.yMin) box.yMin = y;
if (y > box.yMax) box.yMax = y;
if (z < box.zMin) box.zMin = z;
if (z > box.zMax) box.zMax = z;
}
if (box) {
box.xMax = box.xMax + xOffset;
box.xMin = box.xMin - xOffset;
}
return box
? new BoundingBox(box.xMin, box.xMax, box.yMin, box.yMax, box.zMin, box.zMax)
: undefined;
}
public getIntersectionVolume(box: BoundingBox) {
if (!box) return 0.0;
// determine the coordinates of the intersection rectangle
const xMin = Math.max(this.xMin, box.xMin);
const yMin = Math.max(this.yMin, box.yMin);
const zMin = Math.max(this.zMin, box.zMin);
const xMax = Math.min(this.xMax, box.xMax);
const yMax = Math.min(this.yMax, box.yMax);
const zMax = Math.min(this.zMax, box.zMax);
if (xMax < xMin || yMax < yMin || zMax < zMin) {
return 0.0;
}
return (xMax - xMin) * (yMax - yMin) * (zMax - zMin);
}
public toPoints() {
return [
[this.xMin, this.yMin, this.zMin],
[this.xMin, this.yMin, this.zMax],
[this.xMin, this.yMax, this.zMin],
[this.xMin, this.yMax, this.zMax],
[this.xMax, this.yMin, this.zMin],
[this.xMax, this.yMin, this.zMax],
[this.xMax, this.yMax, this.zMin],
[this.xMax, this.yMax, this.zMax],
];
}
}
const boxLookup = {
top: [0, 2, 6, 4, 0],
bottom: [1, 3, 7, 5, 1],
column1: [0, 1],
column2: [2, 3],
column3: [4, 5],
column4: [6, 7],
};
export { BoundingBox, boxLookup }