1 | """ Test for the geometry classes. |
---|
2 | |
---|
3 | Pylint quality rating as of June 2010: 8.51/10. |
---|
4 | """ |
---|
5 | |
---|
6 | import unittest |
---|
7 | |
---|
8 | from aabb import AABB |
---|
9 | from quad import Cell |
---|
10 | |
---|
11 | #------------------------------------------------------------- |
---|
12 | |
---|
13 | class Test_Geometry(unittest.TestCase): |
---|
14 | """ Test geometry classes. """ |
---|
15 | def setUp(self): |
---|
16 | """ Generic set up for geometry tests. """ |
---|
17 | pass |
---|
18 | |
---|
19 | def tearDown(self): |
---|
20 | """ Generic shut down for geometry tests. """ |
---|
21 | pass |
---|
22 | |
---|
23 | def test_aabb_contains(self): |
---|
24 | """ Test if point is correctly classified as falling inside or |
---|
25 | outside of bounding box. """ |
---|
26 | box = AABB(1, 21, 1, 11) |
---|
27 | assert box.contains([10, 5]) |
---|
28 | assert box.contains([1, 1]) |
---|
29 | assert box.contains([20, 6]) |
---|
30 | assert not box.contains([-1, -1]) |
---|
31 | assert not box.contains([5, 70]) |
---|
32 | assert not box.contains([6, -70]) |
---|
33 | assert not box.contains([-1, 6]) |
---|
34 | assert not box.contains([50, 6]) |
---|
35 | |
---|
36 | def test_aabb_split_vert(self): |
---|
37 | """ Test that a bounding box can be split correctly along an axis. |
---|
38 | """ |
---|
39 | parent = AABB(1, 21, 1, 11) |
---|
40 | |
---|
41 | child1, child2 = parent.split(0.6) |
---|
42 | |
---|
43 | self.assertEqual(child1.xmin, 1) |
---|
44 | self.assertEqual(child1.xmax, 13) |
---|
45 | self.assertEqual(child1.ymin, 1) |
---|
46 | self.assertEqual(child1.ymax, 11) |
---|
47 | |
---|
48 | self.assertEqual(child2.xmin, 9) |
---|
49 | self.assertEqual(child2.xmax, 21) |
---|
50 | self.assertEqual(child2.ymin, 1) |
---|
51 | self.assertEqual(child2.ymax, 11) |
---|
52 | |
---|
53 | def test_aabb_split_horiz(self): |
---|
54 | """ Test that a bounding box will be split along the horizontal axis |
---|
55 | correctly. """ |
---|
56 | parent = AABB(1, 11, 1, 41) |
---|
57 | |
---|
58 | child1, child2 = parent.split(0.6) |
---|
59 | |
---|
60 | self.assertEqual(child1.xmin, 1) |
---|
61 | self.assertEqual(child1.xmax, 11) |
---|
62 | self.assertEqual(child1.ymin, 1) |
---|
63 | self.assertEqual(child1.ymax, 25) |
---|
64 | |
---|
65 | self.assertEqual(child2.xmin, 1) |
---|
66 | self.assertEqual(child2.xmax, 11) |
---|
67 | self.assertEqual(child2.ymin, 17) |
---|
68 | self.assertEqual(child2.ymax, 41) |
---|
69 | |
---|
70 | def test_add_data(self): |
---|
71 | """ Test add and retrieve arbitrary data from tree structure. """ |
---|
72 | cell = Cell(AABB(0, 10, 0, 5), None) |
---|
73 | cell.insert([(AABB(1, 3, 1, 3), 111), (AABB(8, 9, 1, 2), 222), \ |
---|
74 | (AABB(7, 8, 3, 4), 333), (AABB(1, 10, 0, 1), 444)]) |
---|
75 | |
---|
76 | result = cell.retrieve() |
---|
77 | assert isinstance(result, (list, tuple)), 'should be a list' |
---|
78 | |
---|
79 | self.assertEqual(len(result), 4) |
---|
80 | |
---|
81 | def test_search(self): |
---|
82 | """ Test search tree for an intersection. """ |
---|
83 | test_tag = 222 |
---|
84 | cell = Cell(AABB(0, 10, 0,5), None) |
---|
85 | cell.insert([(AABB(1, 3, 1, 3), 111), (AABB(8, 9, 1, 2), test_tag), \ |
---|
86 | (AABB(7, 8, 3, 4), 333), (AABB(1, 10, 0, 1), 444)]) |
---|
87 | |
---|
88 | result = cell.search([8.5, 1.5]) |
---|
89 | assert isinstance(result, (list, tuple)), 'should be a list' |
---|
90 | assert(len(result) == 1) |
---|
91 | data, _ = result[0] |
---|
92 | self.assertEqual(data, test_tag, 'only 1 point should intersect') |
---|
93 | |
---|
94 | def test_get_siblings(self): |
---|
95 | """ Make sure children know their parent. """ |
---|
96 | cell = Cell(AABB(0, 10, 0, 5), None) |
---|
97 | cell.insert([(AABB(1, 3, 1, 3), 111), (AABB(8, 9, 1, 2), 222)]) |
---|
98 | |
---|
99 | assert len(cell.children) == 2 |
---|
100 | assert cell.parent == None |
---|
101 | assert cell.children[0].parent == cell |
---|
102 | assert cell.children[1].parent == cell |
---|
103 | |
---|
104 | |
---|
105 | ################################################################################ |
---|
106 | |
---|
107 | if __name__ == "__main__": |
---|
108 | mysuite = unittest.makeSuite(Test_Geometry, 'test') |
---|
109 | runner = unittest.TextTestRunner() |
---|
110 | runner.run(mysuite) |
---|