source: trunk/anuga_core/source/anuga/fit_interpolate/test_search_functions.py @ 7876

Last change on this file since 7876 was 7724, checked in by hudson, 14 years ago

Fixed broken unit test that was using nonexistent function.

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