forked from negbook/quadtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadtree.lua
305 lines (273 loc) · 10.9 KB
/
quadtree.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
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
local setmetatable = setmetatable
local DrawLine = DrawLine
local GetEntityCoords = GetEntityCoords
local Wait = Wait
local CreateThread = CreateThread
local table = table
local ipairs = ipairs
local math = math
local GetPlayerPed = GetPlayerPed
local GetEntityCoords = GetEntityCoords
local vector3 = vector3
local vector2 = vector2
QuadTree = {}
local Contains = {
pointtopoint = function(pointA,pointB,radius)
local radius = radius or 0
return #(vector2(pointA.x,pointA.y) - vector2(pointB.x,pointB.y)) <= radius
end,
pointtorectangle = function(pointA,rectangle,radius)
local radius = radius or 0
local rectanglecenter
local rectanglecenterx
local rectanglecentery
if rectangle.getcenter then
rectanglecenter = rectangle.getcenter()
rectanglecenterx , rectanglecentery = rectanglecenter.x, rectanglecenter.y
else
rectanglecenter = rectangle.center
rectanglecenterx , rectanglecentery = rectanglecenter.x, rectanglecenter.y
end
if rectangle.getminmax then
local rectanglemin,rectanglemax = rectangle.getminmax()
local pointAx = pointA.x
local pointAy = pointA.y
local rectangleminx = rectanglemin.x
local rectangleminy = rectanglemin.y
local rectanglemaxx = rectanglemax.x
local rectanglemaxy = rectanglemax.y
return pointAx >= rectangleminx - radius and pointAx <= rectanglemaxx + radius and pointAy >= rectangleminy - radius and pointAy <= rectanglemaxy + radius
end
if rectangle.min == nil then
local rectanglesize = rectangle.size
local rectanglehalfwidth = rectanglesize.x/2
local rectanglehalfheight = rectanglesize.y/2
local pointAx = pointA.x
local pointAy = pointA.y
--return pointA.x >= rectangle.center.x - rectangle.size.x/2 - radius and pointA.x <= rectangle.center.x + rectangle.size.x/2 + radius and pointA.y >= rectangle.center.y - rectangle.size.y/2 - radius and pointA.y <= rectangle.center.y + rectangle.size.y/2 + radius
return pointAx >= rectanglecenterx - rectanglehalfwidth - radius and pointAx <= rectanglecenterx + rectanglehalfwidth + radius and pointAy >= rectanglecentery - rectanglehalfheight - radius and pointAy <= rectanglecentery + rectanglehalfheight + radius
elseif rectangle.max then
local pointAx = pointA.x
local pointAy = pointA.y
local rectangleminx = rectangle.min.x
local rectangleminy = rectangle.min.y
local rectanglemaxx = rectangle.max.x
local rectanglemaxy = rectangle.max.y
return pointAx >= rectangleminx - radius and pointAx <= rectanglemaxx + radius and pointAy >= rectangleminy - radius and pointAy <= rectanglemaxy + radius
end
end,
}
function QuadTree.new(boundary, capacity)
local o = {
center = boundary.center,
size = boundary.size,
capacity = capacity or 4,
points = {},
isdivided = false,
isinquery = false,
}
return setmetatable(
o, {
__index = QuadTree,
__tostring = function(self)
return "QuadTree: center: "..self.center.x.." "..self.center.y..
" width: "..self.size.x.." height: "..self.size.y..
" capacity: "..self.capacity.." points: "..#self.points..
" isdivided: "..tostring(self.isdivided)
end
})
end
function QuadTree:inner_subdivide()
local parentcenter = self.center
local parentsize = self.size
local parentwidth = parentsize.x
local parentheight = parentsize.y
local parentcenterx = parentcenter.x
local parentcentery = parentcenter.y
local childwidth = parentwidth/2
local childheight = parentheight/2
local childhalfwidth = childwidth/2
local childhalfheight = childheight/2
local toleftX = parentcenterx - childhalfwidth
local torightX = parentcenterx + childhalfwidth
local toupY = parentcentery - childhalfheight
local todownY = parentcentery + childhalfheight
local childsize = vector2(childwidth, childheight)
local parentcapacity = self.capacity
--create new quadtrees in 4 sub-regions
self.topright = QuadTree.new({
center = vector2(torightX, toupY),
size = childsize
}, parentcapacity)
self.bottomright = QuadTree.new({
center = vector2(torightX, todownY),
size = childsize
}, parentcapacity)
self.bottomleft = QuadTree.new({
center = vector2(toleftX, todownY),
size = childsize
}, parentcapacity)
self.topleft = QuadTree.new({
center = vector2(toleftX, toupY),
size = childsize
}, parentcapacity)
self.isdivided = true
end
function QuadTree:inner_intersects(rectangle)
local rectcenter = rectangle.center
local rectcenterx = rectcenter.x
local rectcentery = rectcenter.y
local selfcenter = self.center
local selfcenterx = selfcenter.x
local selfcentery = selfcenter.y
local recthalfsize = rectangle.size/2
local selfhalfsize = self.size/2
local recthalfsizex = recthalfsize.x
local recthalfsizey = recthalfsize.y
local selfhalfsizex = selfhalfsize.x
local selfhalfsizey = selfhalfsize.y
local isrectanglein =
rectcenterx - recthalfsizex <= selfcenterx + selfhalfsizex and
rectcenterx + recthalfsizex >= selfcenterx - selfhalfsizex and
rectcentery - recthalfsizey <= selfcentery + selfhalfsizey and
rectcentery + recthalfsizey >= selfcentery - selfhalfsizey
return isrectanglein
end
function QuadTree:inner_point_contains (point, radius)
local radius = radius or 0.0
local selfcenter = self.center
local selfcenterx = selfcenter.x
local selfcentery = selfcenter.y
local selfhalfsize = self.size/2
local pointx = point.x
local pointy = point.y
--return point.x + radius >= self.center.x - self.size.x/2 and point.x - radius <= self.center.x + self.size.x/2 and point.y + radius >= self.center.y - self.size.y/2 and point.y - radius <= self.center.y + self.size.y/2
return pointx + radius >= selfcenterx - selfhalfsize.x and pointx - radius <= selfcenterx + selfhalfsize.x and pointy + radius >= selfcentery - selfhalfsize.y and pointy - radius <= selfcentery + selfhalfsize.y
end
function QuadTree:insert_point(point)
if not self:inner_point_contains(point) then
return false
end
if #self.points < self.capacity then
table.insert(self.points, point)
return true
else
if not self.isdivided then
self:inner_subdivide()
end
if self.topright:insert_point(point) then
return true
elseif self.bottomright:insert_point(point) then
return true
elseif self.bottomleft:insert_point(point) then
return true
elseif self.topleft:insert_point(point) then
return true
end
end
end
function QuadTree:remove_point (point)
if not self:inner_point_contains(point) then
return false
end
if #self.points > 0 then
for i, v in ipairs(self.points) do
if v == point then
table.remove(self.points, i)
return true
end
end
end
if self.isdivided then
if self.topright:remove_point(point) then
return true
elseif self.bottomright:remove_point(point) then
return true
elseif self.bottomleft:remove_point(point) then
return true
elseif self.topleft:remove_point(point) then
return true
end
end
end
function QuadTree:update_point(point)
self:remove_point(point)
self:insert_point(point)
end
function QuadTree:query_points_by_rectangle(rectrange, found)
found = found or {}
if not self:inner_intersects(rectrange) then
return found
end
for i, point in ipairs(self.points) do
if Contains.pointtorectangle(point, rectrange) then
table.insert(found, point)
end
end
if self.isdivided then
self.topright:query_points_by_rectangle(rectrange, found)
self.bottomright:query_points_by_rectangle(rectrange, found)
self.bottomleft:query_points_by_rectangle(rectrange, found)
self.topleft:query_points_by_rectangle(rectrange, found)
end
return found
end
function QuadTree:query_points_by_point(point, radius, found)
found = found or {}
if not self:inner_point_contains(point, radius) then
return found
end
for i, point_ in ipairs(self.points) do
if Contains.pointtopoint(point_, point, radius) then
table.insert(found, point_)
end
end
if self.isdivided then
self.topright:query_points_by_point(point, radius, found)
self.bottomright:query_points_by_point(point, radius, found)
self.bottomleft:query_points_by_point(point, radius, found)
self.topleft:query_points_by_point(point, radius, found)
end
return found
end
function QuadTree:clear_points()
self.points = {}
if self.isdivided then
self.topright:clear_points()
self.bottomright:clear_points()
self.bottomleft:clear_points()
self.topleft:clear_points()
end
end
function QuadTree:DrawGrids(freezeZ)
local r,g,b,a = 255,255,255,255
if self.isinquery then
r,g,b,a = 0,255,0,255
end
local drawz = freezeZ or (GetEntityCoords(GetPlayerPed(-1)).z + 1.0)
DrawLine(self.center.x-self.size.x/2,self.center.y-self.size.y/2,drawz,self.center.x+self.size.x/2,self.center.y-self.size.y/2,drawz,r,g,b,a)
DrawLine(self.center.x+self.size.x/2,self.center.y-self.size.y/2,drawz,self.center.x+self.size.x/2,self.center.y+self.size.y/2,drawz,r,g,b,a)
DrawLine(self.center.x+self.size.x/2,self.center.y+self.size.y/2,drawz,self.center.x-self.size.x/2,self.center.y+self.size.y/2,drawz,r,g,b,a)
DrawLine(self.center.x-self.size.x/2,self.center.y+self.size.y/2,drawz,self.center.x-self.size.x/2,self.center.y-self.size.y/2,drawz,r,g,b,a)
DrawLine(self.center.x-self.size.x/2,self.center.y,drawz,self.center.x+self.size.x/2,self.center.y,drawz,r,g,b,a)
DrawLine(self.center.x,self.center.y-self.size.y/2,drawz,self.center.x,self.center.y+self.size.y/2,drawz,r,g,b,a)
if self.points[1] then
for i, point in ipairs(self.points) do
DrawLine(point.x,point.y,drawz,point.x,point.y,drawz+2^10,r,g,255,a)
end
end
if self.isdivided then
self.topright:DrawGrids(freezeZ)
self.bottomright:DrawGrids(freezeZ)
self.bottomleft:DrawGrids(freezeZ)
self.topleft:DrawGrids(freezeZ)
end
end
function QuadTree:Debug(freezeZ)
CreateThread(function()
while true do
self:DrawGrids(freezeZ)
Wait(0)
end
end)
end