/
bbox.go
284 lines (256 loc) · 8.09 KB
/
bbox.go
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
package cube
import (
"github.com/go-gl/mathgl/mgl64"
)
// BBox represents an Axis Aligned Bounding Box in a 3D space. It is defined as two Vec3s, of which one is the
// minimum and one is the maximum.
type BBox struct {
min, max mgl64.Vec3
}
// Box creates a new axis aligned bounding box with the minimum and maximum coordinates provided. The returned
// box has minimum and maximum coordinates swapped if necessary so that it is well-formed.
func Box(x0, y0, z0, x1, y1, z1 float64) BBox {
if x0 > x1 {
x0, x1 = x1, x0
}
if y0 > y1 {
y0, y1 = y1, y0
}
if z0 > z1 {
z0, z1 = z1, z0
}
return BBox{min: mgl64.Vec3{x0, y0, z0}, max: mgl64.Vec3{x1, y1, z1}}
}
// Grow grows the bounding box in all directions by x and returns the new bounding box.
func (box BBox) Grow(x float64) BBox {
add := mgl64.Vec3{x, x, x}
return BBox{min: box.min.Sub(add), max: box.max.Add(add)}
}
// GrowVec3 grows the BBox on all axes as represented by the Vec3 passed. The vec values are subtracted from
// the minimum values of the BBox and added to the maximum values of the BBox.
func (box BBox) GrowVec3(vec mgl64.Vec3) BBox {
return BBox{min: box.min.Sub(vec), max: box.max.Add(vec)}
}
// Min returns the minimum coordinate of the bounding box.
func (box BBox) Min() mgl64.Vec3 {
return box.min
}
// Max returns the maximum coordinate of the bounding box.
func (box BBox) Max() mgl64.Vec3 {
return box.max
}
// Width returns the width of the BBox.
func (box BBox) Width() float64 {
return box.max[0] - box.min[0]
}
// Length returns the length of the BBox.
func (box BBox) Length() float64 {
return box.max[2] - box.min[2]
}
// Height returns the height of the BBox.
func (box BBox) Height() float64 {
return box.max[1] - box.min[1]
}
// Extend expands the BBox on all axes as represented by the Vec3 passed. Negative coordinates result in an
// expansion towards the negative axis, and vice versa for positive coordinates.
func (box BBox) Extend(vec mgl64.Vec3) BBox {
if vec[0] < 0 {
box.min[0] += vec[0]
} else if vec[0] > 0 {
box.max[0] += vec[0]
}
if vec[1] < 0 {
box.min[1] += vec[1]
} else if vec[1] > 0 {
box.max[1] += vec[1]
}
if vec[2] < 0 {
box.min[2] += vec[2]
} else if vec[2] > 0 {
box.max[2] += vec[2]
}
return box
}
// ExtendTowards extends the bounding box by x in a given direction.
func (box BBox) ExtendTowards(f Face, x float64) BBox {
switch f {
case FaceDown:
box.max[1] -= x
case FaceUp:
box.min[1] += x
case FaceNorth:
box.min[2] -= x
case FaceSouth:
box.max[2] += x
case FaceWest:
box.min[0] -= x
case FaceEast:
box.max[0] += x
}
return box
}
// Stretch stretches the bounding box by x in a given axis.
func (box BBox) Stretch(a Axis, x float64) BBox {
switch a {
case Y:
box.min[1] -= x
box.max[1] += x
case Z:
box.min[2] -= x
box.max[2] += x
case X:
box.min[0] -= x
box.max[0] += x
}
return box
}
// Translate moves the entire BBox with the Vec3 given. The (minimum and maximum) x, y and z coordinates are
// moved by those in the Vec3 passed.
func (box BBox) Translate(vec mgl64.Vec3) BBox {
return BBox{min: box.min.Add(vec), max: box.max.Add(vec)}
}
// TranslateTowards moves the entire AABB by x in the direction of a Face passed.
func (box BBox) TranslateTowards(f Face, x float64) BBox {
switch f {
case FaceDown:
return box.Translate(mgl64.Vec3{0, -x, 0})
case FaceUp:
return box.Translate(mgl64.Vec3{0, x, 0})
case FaceNorth:
return box.Translate(mgl64.Vec3{0, 0, -x})
case FaceSouth:
return box.Translate(mgl64.Vec3{0, 0, x})
case FaceWest:
return box.Translate(mgl64.Vec3{-x, 0, 0})
case FaceEast:
return box.Translate(mgl64.Vec3{x, 0, 0})
}
return box
}
// IntersectsWith checks if the BBox intersects with another BBox, returning true if this is the case.
func (box BBox) IntersectsWith(other BBox) bool {
return box.intersectsWith(other, 1e-5)
}
// intersectsWith checks if the BBox intersects with another BBox, using a specific epsilon. It returns true if this is
// the case.
func (box BBox) intersectsWith(other BBox, epsilon float64) bool {
if other.max[0]-box.min[0] > epsilon && box.max[0]-other.min[0] > epsilon {
if other.max[1]-box.min[1] > epsilon && box.max[1]-other.min[1] > epsilon {
return other.max[2]-box.min[2] > epsilon && box.max[2]-other.min[2] > epsilon
}
}
return false
}
// AnyIntersections checks if any of boxes1 have intersections with any of boxes2 and returns true if this
// happens to be the case.
func AnyIntersections(boxes []BBox, search BBox) bool {
for _, box := range boxes {
if box.intersectsWith(search, 0) {
return true
}
}
return false
}
// Vec3Within checks if the BBox has a Vec3 within it, returning true if it does.
func (box BBox) Vec3Within(vec mgl64.Vec3) bool {
if vec[0] <= box.min[0] || vec[0] >= box.max[0] {
return false
}
if vec[2] <= box.min[2] || vec[2] >= box.max[2] {
return false
}
return vec[1] > box.min[1] && vec[1] < box.max[1]
}
// Vec3WithinYZ checks if the BBox has a Vec3 within its Y and Z bounds, returning true if it does.
func (box BBox) Vec3WithinYZ(vec mgl64.Vec3) bool {
if vec[2] < box.min[2] || vec[2] > box.max[2] {
return false
}
return vec[1] >= box.min[1] && vec[1] <= box.max[1]
}
// Vec3WithinXZ checks if the BBox has a Vec3 within its X and Z bounds, returning true if it does.
func (box BBox) Vec3WithinXZ(vec mgl64.Vec3) bool {
if vec[0] < box.min[0] || vec[0] > box.max[0] {
return false
}
return vec[2] >= box.min[2] && vec[2] <= box.max[2]
}
// Vec3WithinXY checks if the BBox has a Vec3 within its X and Y bounds, returning true if it does.
func (box BBox) Vec3WithinXY(vec mgl64.Vec3) bool {
if vec[0] < box.min[0] || vec[0] > box.max[0] {
return false
}
return vec[1] >= box.min[1] && vec[1] <= box.max[1]
}
// XOffset calculates the offset on the X axis between two bounding boxes, returning a delta always
// smaller than or equal to deltaX if deltaX is bigger than 0, or always bigger than or equal to deltaX if it
// is smaller than 0.
func (box BBox) XOffset(nearby BBox, deltaX float64) float64 {
// Bail out if not within the same Y/Z plane.
if box.max[1] <= nearby.min[1] || box.min[1] >= nearby.max[1] {
return deltaX
} else if box.max[2] <= nearby.min[2] || box.min[2] >= nearby.max[2] {
return deltaX
}
if deltaX > 0 && box.max[0] <= nearby.min[0] {
difference := nearby.min[0] - box.max[0]
if difference < deltaX {
deltaX = difference
}
}
if deltaX < 0 && box.min[0] >= nearby.max[0] {
difference := nearby.max[0] - box.min[0]
if difference > deltaX {
deltaX = difference
}
}
return deltaX
}
// YOffset calculates the offset on the Y axis between two bounding boxes, returning a delta always
// smaller than or equal to deltaY if deltaY is bigger than 0, or always bigger than or equal to deltaY if it
// is smaller than 0.
func (box BBox) YOffset(nearby BBox, deltaY float64) float64 {
// Bail out if not within the same X/Z plane.
if box.max[0] <= nearby.min[0] || box.min[0] >= nearby.max[0] {
return deltaY
} else if box.max[2] <= nearby.min[2] || box.min[2] >= nearby.max[2] {
return deltaY
}
if deltaY > 0 && box.max[1] <= nearby.min[1] {
difference := nearby.min[1] - box.max[1]
if difference < deltaY {
deltaY = difference
}
}
if deltaY < 0 && box.min[1] >= nearby.max[1] {
difference := nearby.max[1] - box.min[1]
if difference > deltaY {
deltaY = difference
}
}
return deltaY
}
// ZOffset calculates the offset on the Z axis between two bounding boxes, returning a delta always
// smaller than or equal to deltaZ if deltaZ is bigger than 0, or always bigger than or equal to deltaZ if it
// is smaller than 0.
func (box BBox) ZOffset(nearby BBox, deltaZ float64) float64 {
// Bail out if not within the same X/Y plane.
if box.max[0] <= nearby.min[0] || box.min[0] >= nearby.max[0] {
return deltaZ
} else if box.max[1] <= nearby.min[1] || box.min[1] >= nearby.max[1] {
return deltaZ
}
if deltaZ > 0 && box.max[2] <= nearby.min[2] {
difference := nearby.min[2] - box.max[2]
if difference < deltaZ {
deltaZ = difference
}
}
if deltaZ < 0 && box.min[2] >= nearby.max[2] {
difference := nearby.max[2] - box.min[2]
if difference > deltaZ {
deltaZ = difference
}
}
return deltaZ
}