def test_bigger(self): """test_bigger test larger mesh """ points, vertices, boundary = rectangular(4, 4, 1, 1) mesh = Mesh(points, vertices, boundary) #Test that points are arranged in a counter clock wise order mesh.check_integrity() root = MeshQuadtree(mesh) root.set_last_triangle() for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], [0.1,0.9], [0.4,0.6], [0.9,0.1], [10, 3]]: found, s0, s1, s2, k = root.search_fast(ensure_numeric(x)) if k >= 0: V = mesh.get_vertex_coordinates(k) # nodes for triangle k assert is_inside_polygon(x, V) assert found is True #print k, x else: assert found is False
def test_large(self): """test_larger mesh and different quad trees """ points, vertices, boundary = rectangular(10, 12, 1, 1) mesh = Mesh(points, vertices, boundary) #Test that points are arranged in a counter clock wise order mesh.check_integrity() root = MeshQuadtree(mesh) root.set_last_triangle() #print m, root.show() for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], [0.1,0.9], [0.4,0.6], [0.9,0.1], [10, 3]]: found, s0, s1, s2, k = root.search_fast(x) if k >= 0: V = mesh.get_vertex_coordinates(k) # nodes for triangle k assert is_inside_triangle(x, V, closed=True) assert is_inside_polygon(x, V) assert found is True else: assert found is False if k == 0: return
def test_build_quadtree(self): a = [3, 7] b = [5, 7] c = [5, 5] d = [7, 7] e = [15, 15] f = [15, 30] g = [30, 10] h = [30, 30] points = [a, b, c, d, e, f, g, h] #bac, bce, ecf, dbe, daf, dae vertices = [[1,0,2], [1,3,4], [1,2,3], [5,4,7], [4,6,7]] mesh = Mesh(points, vertices) Q = MeshQuadtree(mesh) #Q.show() #print Q.count() self.assertEqual(Q.count(), len(vertices)) # test a point that falls within a triangle result = Q.search([10, 10]) assert isinstance(result, (list, tuple)), 'should be a list'
def test_small(self): """test_small: Two triangles """ points, vertices, boundary = rectangular(1, 1, 1, 1) mesh = Mesh(points, vertices, boundary) #Test that points are arranged in a counter clock wise order mesh.check_integrity() root = MeshQuadtree(mesh) root.set_last_triangle() x = [0.2, 0.7] found, s0, s1, s2, k = root.search_fast(x) assert k == 1 # Triangle one assert found is True
def test_retrieve_triangles(self): cell = Cell(AABB(0, 6, 0, 6), 'cell') p0 = [2,1] p1 = [4,1] p2 = [4.,4] p3 = [2,4] p4 = [5,4] points = [p0,p1,p2, p3, p4] # vertices = [[0,1,2],[0,2,3],[1,4,2]] mesh = Mesh(points, vertices) Q = MeshQuadtree(mesh) results = Q.search([4.5, 3])
def test_off_and_boundary(self): """test_off: Test a point off the mesh """ points, vertices, boundary = rectangular(1, 1, 1, 1) mesh = Mesh(points, vertices, boundary) #Test that points are arranged in a counter clock wise order mesh.check_integrity() root = MeshQuadtree(mesh) root.set_last_triangle() found, s0, s1, s2, k = root.search_fast([-0.2, 10.7]) assert found is False found, s0, s1, s2, k = root.search_fast([0, 0]) assert found is True
def NOtest_interpolate_one_point_many_triangles(self): # this test has 10 triangles that share the same vert. # If the number of points per cell in a quad tree is less # than 10 it should crash z0 = [2.0, 5.0] z1 = [2.0, 5.0] z2 = [2.0, 5.0] z3 = [2.0, 5.0] z4 = [2.0, 5.0] z5 = [2.0, 5.0] z6 = [2.0, 5.0] z7 = [2.0, 5.0] z8 = [2.0, 5.0] z9 = [2.0, 5.0] z10 = [2.0, 5.0] v0 = [0.0, 0.0] v1 = [1.0, 0.0] v2 = [2.0, 0.0] v3 = [3.0, 0.0] v4 = [4.0, 0.0] v5 = [0.0, 10.0] v6 = [1.0, 10.0] v7 = [2.0, 10.0] v8 = [3.0, 10.0] v9 = [4.0, 10.0] vertices = [z0,v0, v1, v2, v3,v4 ,v5, v6, v7, v8, v9, z1, z2, z3, z4, z5, z6, z7, z8, z9] triangles = [ [11,1,2], [12,2,3], [13,3,4], [14,4,5], [7,6,15], [8,7,16], [9,8,17], [10,9,18], [6,1,19], [5,10,0] ] mesh = Mesh(vertices, triangles) try: Q = MeshQuadtree(mesh, max_points_per_cell = 9) except RuntimeError: pass else: self.assertTrue(0 ==1, 'many verts at the same position no \ longer causes as error')
def test_build_quadtreeII(self): self.cell = Cell(AABB(100, 140, 0, 40), 'cell') p0 = [34.6292076111,-7999.92529297] p1 = [8000.0, 7999.0] p2 = [-7999.96630859, 7999.0] p3 = [34, 7999.97021484] points = [p0,p1,p2, p3] #bac, bce, ecf, dbe, daf, dae vertices = [[0,1,2],[0,3,2]] mesh = Mesh(points, vertices) #This was causing round off error Q = MeshQuadtree(mesh)
def test_underlying_function(self): """test_larger mesh and different quad trees """ return points, vertices, boundary = rectangular(2, 2, 1, 1) mesh = Mesh(points, vertices, boundary) root = MeshQuadtree(mesh) root.set_last_triangle() # One point x = ensure_numeric([0.5, 0.5]) found, sigma0, sigma1, sigma2, k = \ root._search_triangles_of_vertices(root.search(x), x) if k >= 0: V = mesh.get_vertex_coordinates(k) # nodes for triangle k assert is_inside_polygon(x, V) assert found is True else: assert found is False # More points for x in [[0.6, 0.3], [0.1, 0.2], [0.7,0.7], [0.1,0.9], [0.4,0.6], [0.9,0.1], [10, 3]]: triangles = root.search(x) #print x, candidate_vertices found, sigma0, sigma1, sigma2, k = \ root._search_triangles_of_vertices(triangles, ensure_numeric(x)) if k >= 0: V = mesh.get_vertex_coordinates(k) # nodes for triangle k assert is_inside_polygon(x, V) assert found is True else: assert found is False
def NOtest_num_visits(self): """ Test optimisation code. """ a = [3, 7] b = [5, 7] c = [5, 5] d = [7, 7] e = [15, 15] f = [15, 30] g = [30, 10] h = [30, 30] points = [a, b, c, d, e, f, g, h] #bac, bce, ecf, dbe, daf, dae vertices = [[1,0,2], [1,3,4], [1,2,3], [5,4,7], [4,6,7]] mesh = Mesh(points, vertices) Q = MeshQuadtree(mesh) results = Q.search_fast([5.5, 5.5]) print 'visits: ', Q.count_visits() Q.clear_visits() results = Q.search_fast([30, 10]) print 'visits: ', Q.count_visits() print 'second time:' Q.clear_visits() results = Q.search_fast([5.5, 5.5]) print 'visits: ', Q.count_visits()
def __init__(self, vertex_coordinates=None, triangles=None, mesh=None, mesh_origin=None, verbose=False): """ Build interpolation matrix mapping from function values at vertices to function values at data points Pass in a mesh instance or vertex_coordinates and triangles and optionally mesh_origin Inputs: vertex_coordinates: List of coordinate pairs [xi, eta] of points constituting a mesh (or an m x 2 numeric array or a geospatial object) Points may appear multiple times (e.g. if vertices have discontinuities) triangles: List of 3-tuples (or a numeric array) of integers representing indices of all vertices in the mesh. mesh: A mesh instance describing the mesh. mesh_origin: A geo_reference object or 3-tuples consisting of UTM zone, easting and northing. If specified vertex coordinates are assumed to be relative to their respective origins. Note: Don't supply a vertex coords as a geospatial object and a mesh origin, since geospatial has its own mesh origin. """ # NOTE PADARN: The Fit_Interpolate class now uses a the c based # quad tree to store triangles, rather than the python based tree. # The tree is still stored at self.root. However, the subtrees of # the new quad tree can not be directly accessed by python as # was previously possible. # Most of the previous functionality has been preserved. global build_quadtree_time if mesh is None: if vertex_coordinates is not None and triangles is not None: # Fixme (DSG) Throw errors if triangles or vertex_coordinates # are None # Convert input to numeric arrays triangles = ensure_numeric(triangles, num.int) vertex_coordinates = ensure_absolute(vertex_coordinates, geo_reference=mesh_origin) if verbose: log.critical('FitInterpolate: Building mesh') self.mesh = Mesh(vertex_coordinates, triangles) #self.mesh.check_integrity() # Time consuming else: self.mesh = None else: self.mesh = mesh if self.mesh is not None: if verbose: log.critical('FitInterpolate: Building quad tree') #This stores indices of vertices t0 = time.time() self.root = MeshQuadtree(self.mesh, verbose=verbose) build_quadtree_time = time.time() - t0
def build_quad_tree(self, verbose=False): self.root = MeshQuadtree(self.mesh, verbose=verbose)