source: branches/inundation-numpy-branch/fit_interpolate/search_functions.py @ 7256

Last change on this file since 7256 was 2201, checked in by duncan, 19 years ago

comments

File size: 3.6 KB
Line 
1"""
2General functions used in fit and interpolate.
3
4   Ole Nielsen, Stephen Roberts, Duncan Gray
5   Geoscience Australia, 2006.
6
7"""
8from Numeric import dot
9
10
11def search_tree_of_vertices(root, mesh, x):
12    """
13    Find the triangle (element) that the point x is in.
14   
15    root: A quad tree of the vertices
16    Return the associated sigma and k values
17    (and if the element was found) .
18    """
19    #Find triangle containing x:
20    element_found = False
21
22    # This will be returned if element_found = False
23    sigma2 = -10.0
24    sigma0 = -10.0
25    sigma1 = -10.0
26    k = -10.0
27           
28    #Find vertices near x
29    candidate_vertices = root.search(x[0], x[1])
30    is_more_elements = True
31
32    element_found, sigma0, sigma1, sigma2, k = \
33                   _search_triangles_of_vertices(mesh,
34                                                 candidate_vertices, x)
35    while not element_found and is_more_elements:
36        candidate_vertices, branch = root.expand_search()
37        if branch == []:
38            # Searching all the verts from the root cell that haven't
39            # been searched.  This is the last try
40            element_found, sigma0, sigma1, sigma2, k = \
41                           _search_triangles_of_vertices(mesh,
42                                                         candidate_vertices, x)
43            is_more_elements = False
44        else:
45            element_found, sigma0, sigma1, sigma2, k = \
46                       _search_triangles_of_vertices(mesh,
47                                                     candidate_vertices, x)
48
49    return element_found, sigma0, sigma1, sigma2, k
50
51def _search_triangles_of_vertices(mesh, candidate_vertices, x):
52    #Find triangle containing x:
53    element_found = False
54
55    # This will be returned if element_found = False
56    sigma2 = -10.0
57    sigma0 = -10.0
58    sigma1 = -10.0
59    k = -10.0
60    #print "*$* candidate_vertices", candidate_vertices
61    #For all vertices in same cell as point x
62    for v in candidate_vertices:
63        #FIXME (DSG-DSG): this catches verts with no triangle.
64        #Currently pmesh is producing these.
65        #this should be stopped,
66        if mesh.vertexlist[v] is None:
67            continue
68        #for each triangle id (k) which has v as a vertex
69        for k, _ in mesh.vertexlist[v]:
70            #Get the three vertex_points of candidate triangle
71            xi0 = mesh.get_vertex_coordinate(k, 0)
72            xi1 = mesh.get_vertex_coordinate(k, 1)
73            xi2 = mesh.get_vertex_coordinate(k, 2)
74
75            #Get the three normals
76            n0 = mesh.get_normal(k, 0)
77            n1 = mesh.get_normal(k, 1)
78            n2 = mesh.get_normal(k, 2)
79           
80            #Compute interpolation
81            sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2)
82            sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0)
83            sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)
84
85            #FIXME: Maybe move out to test or something
86            epsilon = 1.0e-6
87            assert abs(sigma0 + sigma1 + sigma2 - 1.0) < epsilon
88
89            #Check that this triangle contains the data point
90           
91            #Sigmas can get negative within
92            #machine precision on some machines (e.g nautilus)
93            #Hence the small eps
94            eps = 1.0e-15
95            if sigma0 >= -eps and sigma1 >= -eps and sigma2 >= -eps:
96                element_found = True
97                break
98
99        if element_found is True:
100            #Don't look for any other triangle
101            break
102    return element_found, sigma0, sigma1, sigma2, k
103
104
Note: See TracBrowser for help on using the repository browser.