Added a delaunay triangulating grid that uses scipy.spatial faculties
This commit is contained in:
+18
-77
@@ -1,86 +1,27 @@
|
||||
import re
|
||||
import logging
|
||||
import scipy.spatial
|
||||
|
||||
log = logging.getLogger("interp")
|
||||
from interp.grid import grid as basegrid
|
||||
|
||||
from interp.grid import grid as basegrid, cell
|
||||
|
||||
from subprocess import Popen, PIPE
|
||||
def get_simplex_extra_points(X, points, triangulation, kdtree, extra_points=8):
|
||||
simplex_id = triangulation.find_simplex(X)
|
||||
simplex_verts_ids = set(triangulation.vertices[simplex_id])
|
||||
|
||||
def get_qdelaunay_dump(g):
|
||||
"""
|
||||
pass in interp.grid g, and get back lines from a qhull triangulation:
|
||||
distances, kdt_ids = kdtree.query(X, extra_points + len(simplex_verts_ids))
|
||||
kdt_ids = set(kdt_ids)
|
||||
|
||||
qdelaunay Qt f
|
||||
"""
|
||||
cmd = 'qdelaunay Qt f'
|
||||
p = Popen(cmd.split(), bufsize=1, stdin=PIPE, stdout=PIPE)
|
||||
so, se = p.communicate(g.for_qhull())
|
||||
for i in so.splitlines():
|
||||
yield i
|
||||
simplex_ids = list(simplex_verts_ids)
|
||||
extra_points_ids = list(kdt_ids - simplex_verts_ids)
|
||||
|
||||
def get_qdelaunay_dump_str(g):
|
||||
return "\n".join(get_qdelaunay_dump(g))
|
||||
|
||||
def get_index_only(g):
|
||||
cmd = 'qdelaunay Qt i'
|
||||
p = Popen(cmd.split(), bufsize=1, stdin=PIPE, stdout=PIPE)
|
||||
so, se = p.communicate(g.for_qhull())
|
||||
for i in so.splitlines():
|
||||
yield i
|
||||
|
||||
def get_index_only_str(g):
|
||||
return "\n".join(get_index_only(g))
|
||||
return simplex_ids, extra_points_ids
|
||||
|
||||
class dgrid(basegrid):
|
||||
cell_re = re.compile(r'''
|
||||
-\s+(?P<cell>f\d+).*?
|
||||
vertices:\s(?P<verts>.*?)\n.*?
|
||||
neighboring\s facets:\s+(?P<neigh>[\sf\d]*)
|
||||
''', re.S|re.X)
|
||||
def __init__(self, points, values):
|
||||
self.points = points
|
||||
self.values = values
|
||||
self.triangulation = scipy.spatial.Delaunay(points)
|
||||
self.kdtree = scipy.spatial.KDTree(points)
|
||||
|
||||
vert_re = re.compile(r'''
|
||||
(p\d+)
|
||||
''', re.S|re.X)
|
||||
|
||||
def __init__(self, verts, q = None):
|
||||
self.dim = len(verts[0])
|
||||
basegrid.__init__(self, verts,q)
|
||||
self.construct_connectivity()
|
||||
|
||||
def construct_connectivity(self):
|
||||
"""
|
||||
a call to this method prepares the internal connectivity structure.
|
||||
"""
|
||||
log.info('start')
|
||||
qdelaunay_string = get_qdelaunay_dump_str(self)
|
||||
|
||||
with open('/tmp/qdel.out', 'w') as of:
|
||||
of.write(qdelaunay_string)
|
||||
|
||||
cell_to_cells = []
|
||||
for matcher in dgrid.cell_re.finditer(qdelaunay_string):
|
||||
d = matcher.groupdict()
|
||||
|
||||
cell_name = d['cell']
|
||||
verticies = d['verts']
|
||||
neighboring_cells = d['neigh']
|
||||
|
||||
cur_cell = cell(cell_name)
|
||||
self.cells[cell_name] = cur_cell
|
||||
|
||||
for v in dgrid.vert_re.findall(verticies):
|
||||
vertex_index = int(v[1:])
|
||||
cur_cell.add_vert(vertex_index)
|
||||
self.cells_for_vert[vertex_index].append(cur_cell)
|
||||
|
||||
nghbrs = [(cell_name, i) for i in neighboring_cells.split()]
|
||||
cell_to_cells.extend(nghbrs)
|
||||
log.debug(cell_to_cells)
|
||||
|
||||
for rel in cell_to_cells:
|
||||
if rel[1] in self.cells:
|
||||
self.cells[rel[0]].add_neighbor(self.cells[rel[1]])
|
||||
|
||||
log.debug(self.cells)
|
||||
log.info('end')
|
||||
def get_simplex_extra_points(self, X, extra_points=8):
|
||||
return get_simplex_extra_points(X, self.points, self.triangulation,
|
||||
self.kdtree, extra_points=extra_points)
|
||||
|
||||
Reference in New Issue
Block a user