1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from search_functions import search_tree_of_vertices |
---|
6 | from search_functions import _search_triangles_of_vertices |
---|
7 | |
---|
8 | |
---|
9 | from Numeric import zeros, array, allclose |
---|
10 | |
---|
11 | from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh |
---|
12 | from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular |
---|
13 | from anuga.utilities.polygon import is_inside_polygon |
---|
14 | from anuga.utilities.quad import build_quadtree |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | class Test_search_functions(unittest.TestCase): |
---|
21 | def setUp(self): |
---|
22 | pass |
---|
23 | |
---|
24 | def tearDown(self): |
---|
25 | pass |
---|
26 | |
---|
27 | def NOtest_that_C_extension_compiles(self): |
---|
28 | FN = 'search_functions_ext.c' |
---|
29 | try: |
---|
30 | import search_functions_ext |
---|
31 | except: |
---|
32 | from compile import compile |
---|
33 | |
---|
34 | try: |
---|
35 | compile(FN) |
---|
36 | except: |
---|
37 | raise 'Could not compile %s' %FN |
---|
38 | else: |
---|
39 | import search_functions_ext |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | def test_small(self): |
---|
44 | """test_small: Two triangles |
---|
45 | """ |
---|
46 | |
---|
47 | points, vertices, boundary = rectangular(1, 1, 1, 1) |
---|
48 | mesh = Mesh(points, vertices, boundary) |
---|
49 | |
---|
50 | #Test that points are arranged in a counter clock wise order |
---|
51 | mesh.check_integrity() |
---|
52 | |
---|
53 | root = build_quadtree(mesh, max_points_per_cell = 1) |
---|
54 | |
---|
55 | x = [0.2, 0.7] |
---|
56 | found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x) |
---|
57 | assert k == 1 # Triangle one |
---|
58 | assert found is True |
---|
59 | |
---|
60 | def test_bigger(self): |
---|
61 | """test_larger mesh |
---|
62 | """ |
---|
63 | |
---|
64 | points, vertices, boundary = rectangular(4, 4, 1, 1) |
---|
65 | mesh = Mesh(points, vertices, boundary) |
---|
66 | |
---|
67 | #Test that points are arranged in a counter clock wise order |
---|
68 | mesh.check_integrity() |
---|
69 | |
---|
70 | root = build_quadtree(mesh, max_points_per_cell = 4) |
---|
71 | |
---|
72 | for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], |
---|
73 | [0.1,0.9], [0.4,0.6], [0.9,0.1], |
---|
74 | [10, 3]]: |
---|
75 | |
---|
76 | found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x) |
---|
77 | |
---|
78 | if k >= 0: |
---|
79 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
80 | assert is_inside_polygon(x, V) |
---|
81 | assert found is True |
---|
82 | #print k, x |
---|
83 | else: |
---|
84 | assert found is False |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | def test_large(self): |
---|
89 | """test_larger mesh and different quad trees |
---|
90 | """ |
---|
91 | |
---|
92 | points, vertices, boundary = rectangular(10, 12, 1, 1) |
---|
93 | mesh = Mesh(points, vertices, boundary) |
---|
94 | |
---|
95 | #Test that points are arranged in a counter clock wise order |
---|
96 | mesh.check_integrity() |
---|
97 | |
---|
98 | |
---|
99 | for m in range(8): |
---|
100 | root = build_quadtree(mesh, max_points_per_cell = m) |
---|
101 | #print m, root.show() |
---|
102 | |
---|
103 | for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], |
---|
104 | [0.1,0.9], [0.4,0.6], [0.9,0.1], |
---|
105 | [10, 3]]: |
---|
106 | |
---|
107 | found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x) |
---|
108 | |
---|
109 | if k >= 0: |
---|
110 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
111 | assert is_inside_polygon(x, V) |
---|
112 | assert found is True |
---|
113 | else: |
---|
114 | assert found is False |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | def test_underlying_function(self): |
---|
119 | """test_larger mesh and different quad trees |
---|
120 | """ |
---|
121 | |
---|
122 | points, vertices, boundary = rectangular(2, 2, 1, 1) |
---|
123 | mesh = Mesh(points, vertices, boundary) |
---|
124 | |
---|
125 | root = build_quadtree(mesh, max_points_per_cell = 4) |
---|
126 | |
---|
127 | # One point |
---|
128 | x = [0.5, 0.5] |
---|
129 | candidate_vertices = root.search(x[0], x[1]) |
---|
130 | |
---|
131 | #print x, candidate_vertices |
---|
132 | found, sigma0, sigma1, sigma2, k = \ |
---|
133 | _search_triangles_of_vertices(mesh, |
---|
134 | candidate_vertices, |
---|
135 | x) |
---|
136 | |
---|
137 | if k >= 0: |
---|
138 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
139 | assert is_inside_polygon(x, V) |
---|
140 | assert found is True |
---|
141 | else: |
---|
142 | assert found is False |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | # More points |
---|
147 | for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], |
---|
148 | [0.1,0.9], [0.4,0.6], [0.9,0.1], |
---|
149 | [10, 3]]: |
---|
150 | |
---|
151 | candidate_vertices = root.search(x[0], x[1]) |
---|
152 | |
---|
153 | #print x, candidate_vertices |
---|
154 | found, sigma0, sigma1, sigma2, k = \ |
---|
155 | _search_triangles_of_vertices(mesh, |
---|
156 | candidate_vertices, |
---|
157 | x) |
---|
158 | if k >= 0: |
---|
159 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
160 | assert is_inside_polygon(x, V) |
---|
161 | assert found is True |
---|
162 | else: |
---|
163 | assert found is False |
---|
164 | #------------------------------------------------------------- |
---|
165 | if __name__ == "__main__": |
---|
166 | suite = unittest.makeSuite(Test_search_functions,'test') |
---|
167 | runner = unittest.TextTestRunner(verbosity=1) |
---|
168 | runner.run(suite) |
---|
169 | |
---|