-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathoctree.go
322 lines (262 loc) · 7.63 KB
/
octree.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package trees
import (
"container/heap"
"math"
"github.com/EliCDavis/polyform/math/geometry"
"github.com/EliCDavis/vector/vector3"
)
type OctTree struct {
children []*OctTree
elements []elementReference
bounds geometry.AABB
intersectionsBuffer []int
}
func (ot OctTree) BoundingBox() geometry.AABB {
return ot.bounds
}
func (ot *OctTree) ElementsIntersectingRay(ray geometry.Ray, min, max float64) []int {
if !ot.bounds.IntersectsRayInRange(ray, min, max) {
return nil
}
ot.intersectionsBuffer = ot.intersectionsBuffer[:0]
for i := 0; i < len(ot.elements); i++ {
bounds := ot.elements[i].bounds
oi := ot.elements[i].originalIndex
if bounds.IntersectsRayInRange(ray, min, max) {
ot.intersectionsBuffer = append(ot.intersectionsBuffer, oi)
}
}
for i := 0; i < len(ot.children); i++ {
ot.intersectionsBuffer = append(ot.intersectionsBuffer, ot.children[i].ElementsIntersectingRay(ray, min, max)...)
}
return ot.intersectionsBuffer
}
func (ot OctTree) TraverseIntersectingRay(ray geometry.Ray, min, max float64, iterator func(i int, min, max *float64)) {
if !ot.bounds.IntersectsRayInRange(ray, min, max) {
return
}
tMin := min
tMax := max
for i := 0; i < len(ot.elements); i++ {
bounds := ot.elements[i].bounds
oi := ot.elements[i].originalIndex
if bounds.IntersectsRayInRange(ray, tMin, tMax) {
iterator(oi, &tMin, &tMax)
}
}
for i := 0; i < len(ot.children); i++ {
ot.children[i].TraverseIntersectingRay(ray, tMin, tMax, iterator)
}
}
func (ot OctTree) ElementsContainingPoint(v vector3.Float64) []int {
intersections := make([]int, 0)
for i := 0; i < len(ot.elements); i++ {
if ot.elements[i].bounds.Contains(v) {
intersections = append(intersections, ot.elements[i].originalIndex)
}
}
for _, child := range ot.children {
if child.bounds.Contains(v) {
intersections = append(intersections, child.ElementsContainingPoint(v)...)
}
}
return intersections
}
func (ot OctTree) ElementsWithinRange(position vector3.Float64, distance float64) []int {
if ot.bounds.ClosestPoint(position).Distance(position) > distance {
return nil
}
points := make([]int, 0)
for _, ele := range ot.elements {
if ele.bounds.ClosestPoint(position).Distance(position) <= distance {
points = append(points, ele.originalIndex)
}
}
for _, child := range ot.children {
points = append(points, child.ElementsWithinRange(position, distance)...)
}
return points
}
type octDistItem struct {
dist float64
cell *OctTree
element *elementReference
point vector3.Float64
}
type octItemPriorityQueue []octDistItem
func (pq octItemPriorityQueue) Len() int { return len(pq) }
func (pq octItemPriorityQueue) Less(i, j int) bool {
return pq[i].dist < pq[j].dist
}
func (pq octItemPriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *octItemPriorityQueue) Push(x any) {
item := x.(octDistItem)
*pq = append(*pq, item)
}
func (pq *octItemPriorityQueue) Pop() any {
old := *pq
n := len(old)
item := old[n-1]
*pq = old[0 : n-1]
return item
}
func (ot OctTree) ClosestPoint(v vector3.Float64) (int, vector3.Float64) {
pq := make(octItemPriorityQueue, 1)
pq[0] = octDistItem{
dist: ot.bounds.ClosestPoint(v).DistanceSquared(v),
cell: &ot,
}
heap.Init(&pq)
for pq.Len() > 0 {
item := heap.Pop(&pq).(octDistItem)
if item.element != nil {
return item.element.originalIndex, item.point
}
if item.cell != nil {
for _, child := range item.cell.children {
if child == nil {
continue
}
heap.Push(&pq, octDistItem{
dist: child.bounds.ClosestPoint(v).DistanceSquared(v),
cell: child,
})
}
for _, element := range item.cell.elements {
point := element.primitive.ClosestPoint(v)
heap.Push(&pq, octDistItem{
dist: point.DistanceSquared(v),
element: &element,
point: point,
})
}
}
}
return -1, vector3.Zero[float64]()
}
func octreeIndex(center, item vector3.Float64) int {
left := 0
if item.X() < center.X() {
left = 1
}
bottom := 0
if item.Y() < center.Y() {
bottom = 2
}
back := 0
if item.Z() < center.Z() {
back = 4
}
return left | bottom | back
}
func newOctree(elements []elementReference, maxDepth int) *OctTree {
if len(elements) == 0 {
return nil
}
if len(elements) == 1 {
return &OctTree{
bounds: elements[0].bounds,
elements: []elementReference{elements[0]},
children: nil,
intersectionsBuffer: make([]int, 0),
}
}
bounds := elements[0].primitive.BoundingBox()
for _, item := range elements {
bounds.EncapsulateBounds(item.bounds)
}
if maxDepth == 0 {
return &OctTree{
bounds: bounds,
elements: elements,
children: nil,
intersectionsBuffer: make([]int, 0),
}
}
childrenNodes := [][]elementReference{
make([]elementReference, 0),
make([]elementReference, 0),
make([]elementReference, 0),
make([]elementReference, 0),
make([]elementReference, 0),
make([]elementReference, 0),
make([]elementReference, 0),
make([]elementReference, 0),
}
globalCenter := bounds.Center()
leftOver := make([]elementReference, 0)
for i := 0; i < len(elements); i++ {
primBounds := elements[i].bounds
distMin := globalCenter.Distance(primBounds.Min())
distMax := globalCenter.Distance(primBounds.Max())
// Prioritize what will keep us furthest from the center to prevent as
// much overlap as possible
if distMin > distMax {
minIndex := octreeIndex(globalCenter, primBounds.Min())
childrenNodes[minIndex] = append(childrenNodes[minIndex], elements[i])
} else {
maxIndex := octreeIndex(globalCenter, primBounds.Max())
childrenNodes[maxIndex] = append(childrenNodes[maxIndex], elements[i])
}
// minIndex := octreeIndex(globalCenter, primBounds.Min())
// maxIndex := octreeIndex(globalCenter, primBounds.Max())
// if minIndex == maxIndex {
// // child is contained completely within the division, pass it down.
// childrenNodes[minIndex] = append(childrenNodes[minIndex], elements[i])
// } else {
// // Doesn't fit within a single subdivision, stop recursing for this item.
// leftOver = append(leftOver, elements[i])
// }
}
potentialChildren := []*OctTree{
newOctree(childrenNodes[0], maxDepth-1),
newOctree(childrenNodes[1], maxDepth-1),
newOctree(childrenNodes[2], maxDepth-1),
newOctree(childrenNodes[3], maxDepth-1),
newOctree(childrenNodes[4], maxDepth-1),
newOctree(childrenNodes[5], maxDepth-1),
newOctree(childrenNodes[6], maxDepth-1),
newOctree(childrenNodes[7], maxDepth-1),
}
children := make([]*OctTree, 0)
for _, child := range potentialChildren {
if child == nil {
continue
}
children = append(children, child)
}
if len(leftOver) == 0 && len(children) == 1 {
// Prevents us from creating an octree node that's just a proxy to another
// node. Faster traversal!
return children[0]
}
return &OctTree{
bounds: bounds,
elements: leftOver,
children: children,
intersectionsBuffer: make([]int, 0),
}
}
func logBase8(x float64) float64 {
return math.Log(x) / math.Log(8)
}
func OctreeDepthFromCount(count int) int {
return int(math.Max(1, math.Round(logBase8(float64(count)))))
}
func NewOctree(elements []Element) *OctTree {
treeDepth := OctreeDepthFromCount(len(elements))
return NewOctreeWithDepth(elements, treeDepth)
}
func NewOctreeWithDepth(elements []Element, maxDepth int) *OctTree {
primitives := make([]elementReference, len(elements))
for i := 0; i < len(elements); i++ {
primitives[i] = elementReference{
primitive: elements[i],
originalIndex: i,
bounds: elements[i].BoundingBox(),
}
}
return newOctree(primitives, maxDepth)
}