-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathkdtree.go
140 lines (119 loc) · 2.72 KB
/
kdtree.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
package trees
import (
"math"
"github.com/EliCDavis/polyform/math/geometry"
)
type axis int
const (
xAxis axis = iota
yAxis
zAxis
none
)
func nextkdAxis(current axis) axis {
switch current {
case xAxis:
return yAxis
case yAxis:
return zAxis
case zAxis:
return xAxis
}
panic("unimplemented axis")
}
type KDTree struct {
left *KDTree
right *KDTree
axis axis
splitValue float64
bounds geometry.AABB
elements []elementReference
}
// func (kdt KDTree) ElementsContainingPoint(v vector3.Float64) []int {
// }
// func (kdt KDTree) ClosestPoint(v vector3.Float64) (int, vector3.Float64) {
// }
// func (kdt KDTree) ElementsIntersectingRay(ray geometry.Ray, min, max float64) []int {
// if !kdt.bounds.IntersectsRayInRange(ray, min, max) {
// return nil
// }
// }
func (kdt KDTree) BoundingBox() geometry.AABB {
return kdt.bounds
}
func newKDTreeWithDepth(elements []elementReference, maxDepth int, axis axis) *KDTree {
if len(elements) == 0 {
return nil
}
if len(elements) == 1 {
return &KDTree{
bounds: elements[0].primitive.BoundingBox(),
axis: none,
elements: elements,
}
}
bounds := elements[0].primitive.BoundingBox()
min, max := 0., 0.
for _, item := range elements {
box := item.primitive.BoundingBox()
bounds.EncapsulateBounds(box)
switch axis {
case xAxis:
min = math.Min(min, box.Min().X())
max = math.Max(max, box.Max().X())
case yAxis:
min = math.Min(min, box.Min().Y())
max = math.Max(max, box.Max().Y())
case zAxis:
min = math.Min(min, box.Min().Z())
max = math.Max(max, box.Max().Z())
}
}
if maxDepth == 0 {
return &KDTree{
bounds: bounds,
elements: elements,
left: nil,
right: nil,
axis: none,
}
}
split := min + ((max - min) / 2.)
left := make([]elementReference, 0)
right := make([]elementReference, 0)
for _, item := range elements {
center := item.primitive.BoundingBox().Center()
leftSide := true
switch axis {
case xAxis:
leftSide = center.X() < split
case yAxis:
leftSide = center.Y() < split
case zAxis:
leftSide = center.Z() < split
}
if leftSide {
left = append(left, item)
} else {
right = append(right, item)
}
}
return &KDTree{
left: newKDTreeWithDepth(left, maxDepth-1, nextkdAxis(axis)),
right: newKDTreeWithDepth(right, maxDepth-1, nextkdAxis(axis)),
axis: axis,
splitValue: split,
bounds: bounds,
elements: nil,
}
}
func NewKDTreeWithDepth(elements []Element, maxDepth int) *KDTree {
primitives := make([]elementReference, len(elements))
for i, ele := range elements {
primitives[i] = elementReference{
primitive: ele,
originalIndex: i,
}
}
return newKDTreeWithDepth(primitives, maxDepth, xAxis)
}