1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from search_functions import search_tree_of_vertices, set_last_triangle |
---|
6 | from search_functions import _search_triangles_of_vertices |
---|
7 | |
---|
8 | from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh |
---|
9 | from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular |
---|
10 | from anuga.utilities.polygon import is_inside_polygon |
---|
11 | from anuga.utilities.quad import build_quadtree, Cell |
---|
12 | |
---|
13 | |
---|
14 | class Test_search_functions(unittest.TestCase): |
---|
15 | def setUp(self): |
---|
16 | pass |
---|
17 | |
---|
18 | def tearDown(self): |
---|
19 | pass |
---|
20 | |
---|
21 | def NOtest_that_C_extension_compiles(self): |
---|
22 | FN = 'search_functions_ext.c' |
---|
23 | try: |
---|
24 | import search_functions_ext |
---|
25 | except: |
---|
26 | from compile import compile |
---|
27 | |
---|
28 | try: |
---|
29 | compile(FN) |
---|
30 | except: |
---|
31 | raise 'Could not compile %s' %FN |
---|
32 | else: |
---|
33 | import search_functions_ext |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | def test_small(self): |
---|
38 | """test_small: Two triangles |
---|
39 | """ |
---|
40 | |
---|
41 | points, vertices, boundary = rectangular(1, 1, 1, 1) |
---|
42 | mesh = Mesh(points, vertices, boundary) |
---|
43 | |
---|
44 | #Test that points are arranged in a counter clock wise order |
---|
45 | mesh.check_integrity() |
---|
46 | |
---|
47 | root = build_quadtree(mesh, max_points_per_cell = 1) |
---|
48 | set_last_triangle() |
---|
49 | |
---|
50 | x = [0.2, 0.7] |
---|
51 | found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x) |
---|
52 | assert k == 1 # Triangle one |
---|
53 | assert found is True |
---|
54 | |
---|
55 | def test_bigger(self): |
---|
56 | """test_larger mesh |
---|
57 | """ |
---|
58 | |
---|
59 | points, vertices, boundary = rectangular(4, 4, 1, 1) |
---|
60 | mesh = Mesh(points, vertices, boundary) |
---|
61 | |
---|
62 | #Test that points are arranged in a counter clock wise order |
---|
63 | mesh.check_integrity() |
---|
64 | |
---|
65 | root = build_quadtree(mesh, max_points_per_cell = 4) |
---|
66 | set_last_triangle() |
---|
67 | |
---|
68 | for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], |
---|
69 | [0.1,0.9], [0.4,0.6], [0.9,0.1], |
---|
70 | [10, 3]]: |
---|
71 | |
---|
72 | found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x) |
---|
73 | |
---|
74 | if k >= 0: |
---|
75 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
76 | assert is_inside_polygon(x, V) |
---|
77 | assert found is True |
---|
78 | #print k, x |
---|
79 | else: |
---|
80 | assert found is False |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | def test_large(self): |
---|
85 | """test_larger mesh and different quad trees |
---|
86 | """ |
---|
87 | |
---|
88 | points, vertices, boundary = rectangular(10, 12, 1, 1) |
---|
89 | mesh = Mesh(points, vertices, boundary) |
---|
90 | |
---|
91 | #Test that points are arranged in a counter clock wise order |
---|
92 | mesh.check_integrity() |
---|
93 | |
---|
94 | |
---|
95 | for m in range(8): |
---|
96 | root = build_quadtree(mesh, max_points_per_cell = m) |
---|
97 | set_last_triangle() |
---|
98 | #print m, root.show() |
---|
99 | |
---|
100 | for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], |
---|
101 | [0.1,0.9], [0.4,0.6], [0.9,0.1], |
---|
102 | [10, 3]]: |
---|
103 | |
---|
104 | found, s0, s1, s2, k = search_tree_of_vertices(root, mesh, x) |
---|
105 | |
---|
106 | if k >= 0: |
---|
107 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
108 | assert is_inside_polygon(x, V) |
---|
109 | assert found is True |
---|
110 | else: |
---|
111 | assert found is False |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | def test_underlying_function(self): |
---|
116 | """test_larger mesh and different quad trees |
---|
117 | """ |
---|
118 | |
---|
119 | points, vertices, boundary = rectangular(2, 2, 1, 1) |
---|
120 | mesh = Mesh(points, vertices, boundary) |
---|
121 | |
---|
122 | root = build_quadtree(mesh, max_points_per_cell = 4) |
---|
123 | set_last_triangle() |
---|
124 | |
---|
125 | # One point |
---|
126 | x = [0.5, 0.5] |
---|
127 | candidate_vertices = root.search(x[0], x[1]) |
---|
128 | |
---|
129 | #print x, candidate_vertices |
---|
130 | found, sigma0, sigma1, sigma2, k = \ |
---|
131 | _search_triangles_of_vertices(mesh, |
---|
132 | candidate_vertices, |
---|
133 | x) |
---|
134 | |
---|
135 | if k >= 0: |
---|
136 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
137 | assert is_inside_polygon(x, V) |
---|
138 | assert found is True |
---|
139 | else: |
---|
140 | assert found is False |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | # More points |
---|
145 | for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], |
---|
146 | [0.1,0.9], [0.4,0.6], [0.9,0.1], |
---|
147 | [10, 3]]: |
---|
148 | |
---|
149 | candidate_vertices = root.search(x[0], x[1]) |
---|
150 | |
---|
151 | #print x, candidate_vertices |
---|
152 | found, sigma0, sigma1, sigma2, k = \ |
---|
153 | _search_triangles_of_vertices(mesh, |
---|
154 | candidate_vertices, |
---|
155 | x) |
---|
156 | if k >= 0: |
---|
157 | V = mesh.get_vertex_coordinates(k) # nodes for triangle k |
---|
158 | assert is_inside_polygon(x, V) |
---|
159 | assert found is True |
---|
160 | else: |
---|
161 | assert found is False |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | def expanding_search(self): |
---|
166 | """test_larger mesh and different quad trees |
---|
167 | """ |
---|
168 | |
---|
169 | p0 = [2,1] |
---|
170 | p1 = [4,1] |
---|
171 | p2 = [4.,4] |
---|
172 | p3 = [2,4] |
---|
173 | p4 = [5,4] |
---|
174 | |
---|
175 | p5 = [-1,-1] |
---|
176 | p6 = [1,-1] |
---|
177 | p7 = [1,1] |
---|
178 | p8 = [-1,1] |
---|
179 | |
---|
180 | points = [p0,p1,p2, p3,p4,p5,p6,p7,p8] |
---|
181 | # |
---|
182 | vertices = [[0,1,2],[0,2,3],[1,4,2],[5,6,7], [5,7,8]] |
---|
183 | mesh = Mesh(points, vertices) |
---|
184 | |
---|
185 | # Don't do this, want to control the max and mins |
---|
186 | #root = build_quadtree(mesh, max_points_per_cell=4) |
---|
187 | |
---|
188 | |
---|
189 | root = Cell(-3, 9, -3, 9, mesh, |
---|
190 | max_points_per_cell = 4) |
---|
191 | #Insert indices of all vertices |
---|
192 | root.insert( range(mesh.number_of_nodes) ) |
---|
193 | |
---|
194 | #Build quad tree and return |
---|
195 | root.split() |
---|
196 | |
---|
197 | # One point |
---|
198 | #x = [3.5, 1.5] |
---|
199 | x = [2.5, 1.5] |
---|
200 | element_found, sigma0, sigma1, sigma2, k = \ |
---|
201 | search_tree_of_vertices(root, mesh, x) |
---|
202 | # One point |
---|
203 | x = [3.00005, 2.999994] |
---|
204 | element_found, sigma0, sigma1, sigma2, k = \ |
---|
205 | search_tree_of_vertices(root, mesh, x) |
---|
206 | assert element_found is True |
---|
207 | assert k == 1 |
---|
208 | |
---|
209 | ################################################################################ |
---|
210 | |
---|
211 | if __name__ == "__main__": |
---|
212 | suite = unittest.makeSuite(Test_search_functions,'test') |
---|
213 | runner = unittest.TextTestRunner() #verbosity=1) |
---|
214 | runner.run(suite) |
---|
215 | |
---|