source: anuga_core/source/anuga/fit_interpolate/test_search_functions.py @ 7718

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

Tag for new quadtree complete - beginning optimisations.

File size: 6.7 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        triangles = root._trilist_from_data(root.search(x))
155   
156        found, sigma0, sigma1, sigma2, k = \
157               root._search_triangles_of_vertices(triangles, x)
158
159        if k >= 0:
160            V = mesh.get_vertex_coordinates(k) # nodes for triangle k
161            assert is_inside_polygon(x, V)
162            assert found is True
163        else:
164            assert found is False               
165
166       
167
168        # More points   
169        for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7],
170                  [0.1,0.9], [0.4,0.6], [0.9,0.1],
171                  [10, 3]]:
172               
173            triangles = root._trilist_from_data(root.search(x))
174
175            #print x, candidate_vertices
176            found, sigma0, sigma1, sigma2, k = \
177                   root._search_triangles_of_vertices(triangles,
178                                                 ensure_numeric(x))
179            if k >= 0:
180                V = mesh.get_vertex_coordinates(k) # nodes for triangle k
181                assert is_inside_polygon(x, V)
182                assert found is True
183            else:
184                assert found is False
185
186               
187
188    def expanding_search(self):
189        """test_larger mesh and different quad trees
190        """
191       
192        p0 = [2,1]
193        p1 = [4,1]
194        p2 = [4.,4]
195        p3 = [2,4]
196        p4 = [5,4]
197
198        p5 = [-1,-1]
199        p6 = [1,-1]
200        p7 = [1,1]
201        p8 = [-1,1]
202
203        points = [p0,p1,p2, p3,p4,p5,p6,p7,p8]
204        #
205        vertices = [[0,1,2],[0,2,3],[1,4,2],[5,6,7], [5,7,8]]
206        mesh = Mesh(points, vertices)
207
208        # Don't do this, want to control the max and mins
209        #root = build_quadtree(mesh, max_points_per_cell=4)
210   
211
212        root = Cell(-3, 9, -3, 9,
213                    max_points_per_cell = 4)
214        #Insert indices of all vertices
215        root.insert( range(mesh.number_of_nodes) )
216
217        #Build quad tree and return
218        root.split()
219       
220        # One point
221        #x = [3.5, 1.5]
222        x = [2.5, 1.5]
223        element_found, sigma0, sigma1, sigma2, k = root.search_fast(x)
224        # One point
225        x = [3.00005, 2.999994]
226        element_found, sigma0, sigma1, sigma2, k = root.search_fast(x)
227        assert element_found is True
228        assert k == 1
229       
230
231#-------------------------------------------------------------
232if __name__ == "__main__":
233    suite = unittest.makeSuite(Test_search_functions, 'test')
234    runner = unittest.TextTestRunner(verbosity=1)
235    runner.run(suite)
236   
Note: See TracBrowser for help on using the repository browser.