-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbox3.go
More file actions
264 lines (229 loc) · 7.87 KB
/
Copy pathbox3.go
File metadata and controls
264 lines (229 loc) · 7.87 KB
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
// Copyright 2019 The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Initially copied from G3N: github.com/g3n/engine/math32
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// with modifications needed to suit GoGi functionality.
package mat32
// Box3 represents a 3D bounding box defined by two points:
// the point with minimum coordinates and the point with maximum coordinates.
type Box3 struct {
Min Vec3
Max Vec3
}
// B3 returns a new [Box3] from the given minimum and maximum x, y, and z coordinates.
func B3(x0, y0, z0, x1, y1, z1 float32) Box3 {
return Box3{V3(x0, y0, z0), V3(x1, y1, z1)}
}
// B3Empty returns a new [Box3] with empty minimum and maximum values.
func B3Empty() Box3 {
bx := Box3{}
bx.SetEmpty()
return bx
}
// SetEmpty set this bounding box to empty (min / max +/- Infinity)
func (b *Box3) SetEmpty() {
b.Min.SetScalar(Infinity)
b.Max.SetScalar(-Infinity)
}
// IsEmpty returns true if this bounding box is empty (max < min on any coord).
func (b Box3) IsEmpty() bool {
return (b.Max.X < b.Min.X) || (b.Max.Y < b.Min.Y) || (b.Max.Z < b.Min.Z)
}
// Set sets this bounding box minimum and maximum coordinates.
// If either min or max are nil, then corresponding values are set to +/- Infinity.
func (b *Box3) Set(min, max *Vec3) {
if min != nil {
b.Min = *min
} else {
b.Min.SetScalar(Infinity)
}
if max != nil {
b.Max = *max
} else {
b.Max.SetScalar(-Infinity)
}
}
// SetFromPoints sets this bounding box from the specified array of points.
func (b *Box3) SetFromPoints(points []Vec3) {
b.SetEmpty()
b.ExpandByPoints(points)
}
// ExpandByPoints may expand this bounding box from the specified array of points.
func (b *Box3) ExpandByPoints(points []Vec3) {
for i := 0; i < len(points); i++ {
b.ExpandByPoint(points[i])
}
}
// ExpandByPoint may expand this bounding box to include the specified point.
func (b *Box3) ExpandByPoint(point Vec3) {
b.Min.SetMin(point)
b.Max.SetMax(point)
}
// ExpandByBox may expand this bounding box to include the specified box
func (b *Box3) ExpandByBox(box Box3) {
b.ExpandByPoint(box.Min)
b.ExpandByPoint(box.Max)
}
// ExpandByVector expands this bounding box by the specified vector
// subtracting from min and adding to max.
func (b *Box3) ExpandByVector(vector Vec3) {
b.Min.SetSub(vector)
b.Max.SetAdd(vector)
}
// ExpandByScalar expands this bounding box by the specified scalar
// subtracting from min and adding to max.
func (b *Box3) ExpandByScalar(scalar float32) {
b.Min.SetSubScalar(scalar)
b.Max.SetAddScalar(scalar)
}
// SetFromCenterAndSize sets this bounding box from a center point and size.
// Size is a vector from the minimum point to the maximum point.
func (b *Box3) SetFromCenterAndSize(center, size Vec3) {
halfSize := size.MulScalar(0.5)
b.Min = center.Sub(halfSize)
b.Max = center.Add(halfSize)
}
// Center returns the center of the bounding box.
func (b Box3) Center() Vec3 {
return b.Min.Add(b.Max).MulScalar(0.5)
}
// Size calculates the size of this bounding box: the vector from
// its minimum point to its maximum point.
func (b Box3) Size() Vec3 {
return b.Max.Sub(b.Min)
}
// ContainsPoint returns if this bounding box contains the specified point.
func (b Box3) ContainsPoint(point Vec3) bool {
if point.X < b.Min.X || point.X > b.Max.X ||
point.Y < b.Min.Y || point.Y > b.Max.Y ||
point.Z < b.Min.Z || point.Z > b.Max.Z {
return false
}
return true
}
// ContainsBox returns if this bounding box contains other box.
func (b Box3) ContainsBox(box Box3) bool {
if (b.Min.X <= box.Max.X) && (box.Max.X <= b.Max.X) &&
(b.Min.Y <= box.Min.Y) && (box.Max.Y <= b.Max.Y) &&
(b.Min.Z <= box.Min.Z) && (box.Max.Z <= b.Max.Z) {
return true
}
return false
}
// IntersectsBox returns if other box intersects this one.
func (b Box3) IntersectsBox(other Box3) bool {
// using 6 splitting planes to rule out intersections.
if other.Max.X < b.Min.X || other.Min.X > b.Max.X ||
other.Max.Y < b.Min.Y || other.Min.Y > b.Max.Y ||
other.Max.Z < b.Min.Z || other.Min.Z > b.Max.Z {
return false
}
return true
}
// ClampPoint returns a new point which is the specified point clamped inside this box.
func (b Box3) ClampPoint(point Vec3) Vec3 {
point.Clamp(b.Min, b.Max)
return point
}
// DistToPoint returns the distance from this box to the specified point.
func (b Box3) DistToPoint(point Vec3) float32 {
clamp := b.ClampPoint(point)
return clamp.Sub(point).Length()
}
// GetBoundingSphere returns a bounding sphere to this bounding box.
func (b Box3) GetBoundingSphere() Sphere {
return Sphere{b.Center(), b.Size().Length() * 0.5}
}
// Intersect returns the intersection with other box.
func (b Box3) Intersect(other Box3) Box3 {
other.Min.SetMax(b.Min)
other.Max.SetMin(b.Max)
return other
}
// Union returns the union with other box.
func (b Box3) Union(other Box3) Box3 {
other.Min.SetMin(b.Min)
other.Max.SetMax(b.Max)
return other
}
// MulMat4 multiplies the specified matrix to the vertices of this bounding box
// and computes the resulting spanning Box3 of the transformed points
func (b Box3) MulMat4(m *Mat4) Box3 {
xax := m[0] * b.Min.X
xay := m[1] * b.Min.X
xaz := m[2] * b.Min.X
xbx := m[0] * b.Max.X
xby := m[1] * b.Max.X
xbz := m[2] * b.Max.X
yax := m[4] * b.Min.Y
yay := m[5] * b.Min.Y
yaz := m[6] * b.Min.Y
ybx := m[4] * b.Max.Y
yby := m[5] * b.Max.Y
ybz := m[6] * b.Max.Y
zax := m[8] * b.Min.Z
zay := m[9] * b.Min.Z
zaz := m[10] * b.Min.Z
zbx := m[8] * b.Max.Z
zby := m[9] * b.Max.Z
zbz := m[10] * b.Max.Z
nb := Box3{}
nb.Min.X = Min(xax, xbx) + Min(yax, ybx) + Min(zax, zbx) + m[12]
nb.Min.Y = Min(xay, xby) + Min(yay, yby) + Min(zay, zby) + m[13]
nb.Min.Z = Min(xaz, xbz) + Min(yaz, ybz) + Min(zaz, zbz) + m[14]
nb.Max.X = Max(xax, xbx) + Max(yax, ybx) + Max(zax, zbx) + m[12]
nb.Max.Y = Max(xay, xby) + Max(yay, yby) + Max(zay, zby) + m[13]
nb.Max.Z = Max(xaz, xbz) + Max(yaz, ybz) + Max(zaz, zbz) + m[14]
return nb
}
// MulQuat multiplies the specified quaternion to the vertices of this bounding box
// and computes the resulting spanning Box3 of the transformed points
func (b Box3) MulQuat(q Quat) Box3 {
var cs [8]Vec3
cs[0] = V3(b.Min.X, b.Min.Y, b.Min.Z).MulQuat(q)
cs[1] = V3(b.Min.X, b.Min.Y, b.Max.Z).MulQuat(q)
cs[2] = V3(b.Min.X, b.Max.Y, b.Min.Z).MulQuat(q)
cs[3] = V3(b.Max.X, b.Min.Y, b.Min.Z).MulQuat(q)
cs[4] = V3(b.Max.X, b.Max.Y, b.Max.Z).MulQuat(q)
cs[5] = V3(b.Max.X, b.Max.Y, b.Min.Z).MulQuat(q)
cs[6] = V3(b.Max.X, b.Min.Y, b.Max.Z).MulQuat(q)
cs[7] = V3(b.Min.X, b.Max.Y, b.Max.Z).MulQuat(q)
nb := B3Empty()
for i := 0; i < 8; i++ {
nb.ExpandByPoint(cs[i])
}
return nb
}
// Translate returns translated position of this box by offset.
func (b Box3) Translate(offset Vec3) Box3 {
nb := Box3{}
nb.Min = b.Min.Add(offset)
nb.Max = b.Max.Add(offset)
return nb
}
// IsEqual returns if this box is equal to other
func (b Box3) IsEqual(other Box3) bool {
return other.Min.IsEqual(b.Min) && other.Max.IsEqual(b.Max)
}
// MVProjToNDC projects bounding box through given MVP model-view-projection Mat4
// with perspective divide to return normalized display coordinates (NDC).
func (b Box3) MVProjToNDC(m *Mat4) Box3 {
// all corners: i = min, a = max
var cs [8]Vec3
cs[0] = Vec4{b.Min.X, b.Min.Y, b.Min.Z, 1}.MulMat4(m).PerspDiv()
cs[1] = Vec4{b.Min.X, b.Min.Y, b.Max.Z, 1}.MulMat4(m).PerspDiv()
cs[2] = Vec4{b.Min.X, b.Max.Y, b.Min.Z, 1}.MulMat4(m).PerspDiv()
cs[3] = Vec4{b.Max.X, b.Min.Y, b.Min.Z, 1}.MulMat4(m).PerspDiv()
cs[4] = Vec4{b.Max.X, b.Max.Y, b.Max.Z, 1}.MulMat4(m).PerspDiv()
cs[5] = Vec4{b.Max.X, b.Max.Y, b.Min.Z, 1}.MulMat4(m).PerspDiv()
cs[6] = Vec4{b.Max.X, b.Min.Y, b.Max.Z, 1}.MulMat4(m).PerspDiv()
cs[7] = Vec4{b.Min.X, b.Max.Y, b.Max.Z, 1}.MulMat4(m).PerspDiv()
nb := B3Empty()
for i := 0; i < 8; i++ {
nb.ExpandByPoint(cs[i])
}
return nb
}