-
Notifications
You must be signed in to change notification settings - Fork 3
/
octree.lua
246 lines (185 loc) · 4.32 KB
/
octree.lua
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
local TREE = {}
TREE.__index = TREE
function new()
local tree = {
root = {
size = 1,
x = 0,
y = 0,
z = 0
}
}
setmetatable( tree, TREE )
return tree
end
-- Tree metatable
function TREE:expandIfNeeded( x, y, z )
local root = self.root
local size = root.size
-- Relative coordinates
local rX = x - root.x
local rY = y - root.y
local rZ = z - root.z
-- Out of bounds marks
local xPos = rX >= size
local xNeg = rX < -size
local yPos = rY >= size
local yNeg = rY < -size
local zPos = rZ >= size
local zNeg = rZ < -size
-- Check if the point is in the bounds
if ( xPos or xNeg or
yPos or yNeg or
zPos or zNeg ) then
-- Change the root node to fit
local node = {
size = size *2,
x = 0,
y = 0,
z = 0
}
local index = 0
-- Offset the new root, and place the old into it
if ( rX >= 0 ) then node.x = root.x + size end
if ( rY >= 0 ) then node.y = root.y + size end
if ( rZ >= 0 ) then node.z = root.z + size end
if ( rX < 0 ) then node.x = root.x - size index = index +1 end
if ( rY < 0 ) then node.y = root.y - size index = index +2 end
if ( rZ < 0 ) then node.z = root.z - size index = index +4 end
node[index] = root
-- Earse previous root information
root.size = nil
root.x = nil
root.y = nil
root.z = nil
-- Set the new root
self.root = node
-- Repeat until the size is sufficient
self:expandIfNeeded( x, y, z )
end
end
function TREE:get( x, y, z )
self:expandIfNeeded( x, y, z )
-- Convert the coordinates relative to the root
local node = self.root
local size = node.size
local rX = x - node.x
local rY = y - node.y
local rZ = z - node.z
while ( true ) do
size = size /2
local index = 0
-- Seek, and offset the point for the next node
if ( rX >= 0 ) then
index = index +1
rX = rX - size
else
rX = rX + size
end
if ( rY >= 0 ) then
index = index +2
rY = rY - size
else
rY = rY + size
end
if ( rZ >= 0 ) then
index = index +4
rZ = rZ - size
else
rZ = rZ + size
end
-- Get the node/value at the calculated index
local child = node[index]
if ( type( child ) ~= "table" ) then
return child
end
-- We must go deeper!
node = child
end
end
local function mergeIfCan( stack, path, ref )
for i = #stack, 1, -1 do
local node = stack[i]
-- Check if every value is the same in the node
for x = 0, 7, 1 do
if ( ref ~= node[x] ) then
-- Break if any is not
return
end
end
-- Successful merge
stack[ i -1 ][ path[i] ] = ref
end
end
function TREE:set( x, y, z, value )
self:expandIfNeeded( x, y, z )
-- Convert the coordinates relative to the root
local node = self.root
local size = node.size
local rX = x - node.x
local rY = y - node.y
local rZ = z - node.z
local stack = {}
local path = {}
while ( true ) do
size = size /2
local index = 0
if ( rX >= 0 ) then
index = index +1
rX = rX - size
else
rX = rX + size
end
if ( rY >= 0 ) then
index = index +2
rY = rY - size
else
rY = rY + size
end
if ( rZ >= 0 ) then
index = index +4
rZ = rZ - size
else
rZ = rZ + size
end
table.insert( stack, node )
table.insert( path, index )
-- Get the node/value at the calculated index
local child = node[index]
if ( type( child ) ~= "table" ) then
if ( child ~= value ) then
-- No node/value present
if ( child == nil ) then
-- If the size is not 1, it needs further populating,
-- Otherwise, it just needs a value
if ( size ~= 0.5 ) then
child = {}
node[index] = child
else
node[index] = value
mergeIfCan( stack, path, value )
return
end
else
-- There is a node, but its value does not match, divide it
-- If the size is over 1, otherwise, just set the value
if ( size ~= 0.5 ) then
local split = { child, child, child, child,
child, child, child, child }
child = split
node[index] = split
else
-- Hit a real leaf, set the value and walk away
node[index] = value
mergeIfCan( stack, path, value )
return
end
end
else
-- This treenode already has the same value, nothing to do
return
end
end
node = child
end
end