-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.py
366 lines (327 loc) · 12.4 KB
/
Tests.py
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
'''
Created on 25 Oct 2016
'''
import unittest
from random import random, randint
from QuadTree import quadTree2D
class TestQuadTree2D(unittest.TestCase):
def setUp(self):
self.xDim = 10.0
self.yDim = 10.0
self.numPts = 1000
self.maxPtsPerQuad = 10
self.centrePt = [float(self.xDim/2.0), float(self.yDim/2.0)]
#Init the blank tree
self.quadT = quadTree2D(self.centrePt, float(self.xDim)/2.0, float(self.yDim)/2.0, self.maxPtsPerQuad, 0)
def tearDown(self):
self.quadT.dumpAllPoints()
self.assertEqual(self.quadT.getTotalPts(), 0)
self.assertEqual(self.quadT.children, [])
def genTestData(self, xDim, yDim, numPts):
'''
Generate some test data to insert to the tree
'''
points = []
for i in range(0, numPts):
points.append([random()+randint(0,xDim), random()+randint(0,yDim)])
return points
def test_initTree(self):
'''
Basic Init Tests
'''
self.assertEqual(self.quadT.maxPts, self.maxPtsPerQuad)
self.assertEqual(self.quadT.children, [])
self.assertEqual(self.quadT.centrePt, [float(self.xDim/2.0), float(self.yDim/2.0)])
self.assertEqual(self.quadT.halfXDim, float(self.xDim/2.0))
self.assertEqual(self.quadT.halfYDim, float(self.yDim/2.0))
self.assertEqual(self.quadT.points, [])
self.assertEqual(self.quadT.subDivided, False)
self.assertEqual(self.quadT.levelID, 0)
def test_loadData(self):
'''
Load Data
'''
tstData = self.genTestData(self.xDim, self.yDim, self.numPts)
cnt = 0
badPts = 0
for pt in tstData:
#Check random gen points and bounds independently
if pt[0]>10.0 or pt[1]>10.0:
badPts+=1
chk = self.quadT.insertPt(pt)
if chk == True:
cnt+=1
self.assertEqual(cnt, self.numPts-badPts)
self.assertEqual(self.quadT.getTotalPts(), cnt)
def test_dumpAllPoints(self):
'''
Flush the tree
'''
self.quadT.dumpAllPoints()
self.assertEqual(self.quadT.getTotalPts(), 0)
self.assertEqual(self.quadT.getDepth(0), 0)
def test_getDepth(self):
'''
Check Quadtree depth - init with non random data
'''
cnt = 0
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 10)
self.assertEqual(self.quadT.getTotalPts(), 10)
self.assertEqual(self.quadT.getDepth(0), 0)
#Tip it over to next layer
chk = self.quadT.insertPt([5.51, 5.51])
if chk == True:
cnt+=1
self.assertEqual(cnt, 11)
self.assertEqual(self.quadT.getTotalPts(), 11)
self.assertEqual(self.quadT.getDepth(0), 1)
#Once more
for i in range(0,9):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([5.51, 5.51])
if chk == True:
cnt+=1
self.assertEqual(cnt, 20)
self.assertEqual(self.quadT.getTotalPts(), 20)
self.assertEqual(self.quadT.getDepth(0), 1)
#Tip it over to next layer
chk = self.quadT.insertPt([7.51, 7.51])
if chk == True:
cnt+=1
self.assertEqual(cnt, 21)
self.assertEqual(self.quadT.getTotalPts(), 21)
self.assertEqual(self.quadT.getDepth(0), 2)
def test_getQueryQuad(self):
'''
Query the tree at a given position
'''
self.quadT.dumpAllPoints()
tstPt = []
chkPts1 = []
cnt = 0
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chkPts1.append([1.1, 1.1])
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 10)
self.assertEqual(self.quadT.getTotalPts(), 10)
self.assertEqual(self.quadT.getDepth(0), 0)
#Tip it over to next layer
chk = self.quadT.insertPt([5.51, 5.51])
if chk == True:
cnt+=1
#Grab points
chkPts2 = self.quadT.getPtsByPt([7.5, 7.5])
self.assertEqual(chkPts2, [[5.51, 5.51]])
chkPts2 = self.quadT.getPtsByPt([2.456, 4.334])
self.assertEqual(chkPts2, chkPts1)
self.quadT.dumpAllPoints()
def test_quadids(self):
'''
Check we are mapping quadIDs properly
'''
tstPt = []
chkPts1 = []
cnt = 0
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chkPts1.append([1.1, 1.1])
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 10)
self.assertEqual(self.quadT.getTotalPts(), 10)
self.assertEqual(self.quadT.getDepth(0), 0)
#Tip it over to next layer
chk = self.quadT.insertPt([5.51, 5.51])
self.assertEqual(self.quadT.getDepth(0), 1)
self.assertEqual(self.quadT.quadID, [0])
for idx, child in enumerate(self.quadT.children):
self.assertEqual(child.quadID, [0, idx])
#Once more
for i in range(0,9):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([5.51, 5.51])
if chk == True:
cnt+=1
for idx1, child in enumerate(self.quadT.children):
for idx2, child2 in enumerate(child.children):
self.assertEqual(child2.quadID, [0, idx1, idx2])
def test_maxDepth(self):
'''
Test if the tree bottoms out at max depth without dying
'''
cnt = 0
for i in range(0,11):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 11)
self.assertEqual(self.quadT.getTotalPts(), 11)
self.assertEqual(self.quadT.getDepth(0), self.quadT.maxDepth)
def test_getPtsbyQuadID(self):
'''
Retrieve all points under a given quadID
'''
tstPt = []
chkPts1 = []
cnt = 0
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 10)
self.assertEqual(self.quadT.getTotalPts(), 10)
self.assertEqual(self.quadT.getDepth(0), 0)
#Tip it over to next layer
chk = self.quadT.insertPt([5.51, 5.51])
self.assertEqual(self.quadT.getTotalPts(), 11)
#Now get the points at quadID
outPoints = []
self.quadT.getPtsByQID([0,1], outPoints)
self.assertEqual(outPoints, [[5.51, 5.51]])
self.assertEqual(self.quadT.getDepth(0), 1)
#Check we dont get points above a small requested QID
outPoints = []
self.quadT.getPtsByQID([0,1,1,1], outPoints)
self.assertEqual(outPoints, [])
#Try deeper
chkPts1 = []
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([9.1, 9.12])
chkPts1.append([9.1, 9.12])
if chk == True:
cnt+=1
self.assertEqual(self.quadT.getTotalPts(), 21)
self.assertEqual(self.quadT.getDepth(0), 2)
#Check we can get all the points this way
outPoints = []
self.quadT.getPtsByQID([0], outPoints)
self.assertEqual(len(outPoints), self.quadT.getTotalPts())
#Check hi-res
outPoints = []
self.quadT.getPtsByQID([0,1,1], outPoints)
self.assertEqual(outPoints, chkPts1)
#Check hi-res
outPoints = []
self.quadT.getPtsByQID([0,1,2], outPoints)
self.assertEqual(outPoints, [[5.51, 5.51]])
def test_quadIDsByVertexDist(self):
'''
Get quads by distance of their vertices from given point
'''
tstPt = []
chkPts1 = []
cnt = 0
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 10)
self.assertEqual(self.quadT.getTotalPts(), 10)
self.assertEqual(self.quadT.getDepth(0), 0)
#Tip it over to next layer
chk = self.quadT.insertPt([5.51, 5.51])
self.assertEqual(self.quadT.getTotalPts(), 11)
self.assertEqual(self.quadT.getDepth(0), 1)
chkPt = [5.1, 9.9]
chkDist = 2.1
quadIDs = []
self.quadT.vertexDist(chkPt, chkDist, quadIDs)
self.assertEqual(quadIDs, [[0,1]])
def test_getBounds(self):
inShapeVerts = [[1.0, 10.0], [10.0, 12.0], [10.0, 1.5], [1.0, 0.5]]
bbox = self.quadT.getBounds(inShapeVerts)
self.assertEqual(bbox, [[1.0, 0.5], [10.0, 12.0]])
def test_minBoundingQuad(self):
'''
Get smallest quad that fiully bounds the given shape
'''
tstPt = []
chkPts1 = []
cnt = 0
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 10)
self.assertEqual(self.quadT.getTotalPts(), 10)
self.assertEqual(self.quadT.getDepth(0), 0)
#Tip it over to next layer
chk = self.quadT.insertPt([5.51, 5.51])
self.assertEqual(self.quadT.getTotalPts(), 11)
#Try deeper
chkPts1 = []
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([9.1, 9.12])
chkPts1.append([9.1, 9.12])
if chk == True:
cnt+=1
self.assertEqual(self.quadT.getTotalPts(), 21)
self.assertEqual(self.quadT.getDepth(0), 2)
#Now check the bounding quad
inShapeVerts = [[7.6, 7.6], [7.6, 9.2], [9.2, 9.2], [9.2, 7.6]]
quadID = self.quadT.minBoundingQuad(inShapeVerts)
self.assertEqual(quadID, [0,1,1])
#Try deeper
chkPts1 = []
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([9.1, 9.12])
chkPts1.append([9.1, 9.12])
if chk == True:
cnt+=1
self.assertEqual(self.quadT.getTotalPts(), 31)
self.assertEqual(self.quadT.getDepth(0), 10)
#Now check the bounding quad
inShapeVerts = [[7.6, 7.6], [7.6, 9.2], [9.2, 9.2], [9.2, 7.6]]
quadID = self.quadT.minBoundingQuad(inShapeVerts)
self.assertEqual(quadID, [0,1,1])
#Check a very small one
inShapeVerts = [[7.6123123, 7.66123123], [7.6123123, 7.67123123], [7.6223123, 7.67123123], [7.6223123, 7.66123123]]
quadID = self.quadT.minBoundingQuad(inShapeVerts)
self.assertEqual(quadID, [0, 1, 1, 2])
def test_getPtsByDistance(self):
'''
Query tree to get points by distance from given point
'''
tstPt = []
chkPts1 = []
cnt = 0
for i in range(0,10):
#Need to add a bit of noise so the tree bottoms out
chk = self.quadT.insertPt([1.1, 1.1])
if chk == True:
cnt+=1
self.assertEqual(cnt, 10)
self.assertEqual(self.quadT.getTotalPts(), 10)
self.assertEqual(self.quadT.getDepth(0), 0)
#Tip it over to next layer
chk = self.quadT.insertPt([5.51, 5.51])
self.assertEqual(self.quadT.getTotalPts(), 11)
#Try getting all top level points
tstPt = [6.5, 7.2]
tstDist = 2.1
closePts = self.quadT.getPtsByDistance(tstPt, tstDist)
self.assertEqual(len(closePts), self.quadT.getTotalPts())
#Try getting all top level points
tstPt = [5.52, 5.52]
tstDist = 0.02
closePts = self.quadT.getPtsByDistance(tstPt, tstDist)
self.assertEqual(closePts, [[5.51, 5.51]])
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()