From fcb2bf2931e0b30052c846377c7f643417ab71e7 Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Sat, 17 Sep 2011 19:23:21 -0600 Subject: [PATCH] Added a delaunay triangulating grid that uses scipy.spatial faculties --- interp/grid/__init__.py | 249 ++-------------------------------------- interp/grid/delaunay.py | 95 +++------------ 2 files changed, 30 insertions(+), 314 deletions(-) diff --git a/interp/grid/__init__.py b/interp/grid/__init__.py index 7e3cc4f..0f3abe4 100644 --- a/interp/grid/__init__.py +++ b/interp/grid/__init__.py @@ -1,184 +1,27 @@ -from collections import defaultdict import pickle -from xml.dom.minidom import Document - -import numpy as np -from scipy.spatial import KDTree - from interp.baker import interpolate from interp.baker import get_phis import interp -import logging -log = logging.getLogger("interp") - -MAX_SEARCH_COUNT = 256 -TOL = 1e-8 - __version__ = interp.__version__ class grid(object): - def __init__(self, verts=None, q=None): + def __init__(self): """ - verts = array of arrays (if passed in, will convert to numpy.array) - [ - [x0,y0 <, z0>], - [x1,y1 <, z1>], - ... - ] - - q = array (1D) of physical values + Child classes should populate at a minimum the points and values + arrays, and a method for getting a simplex and extra points. """ + pass - if verts != None: - self.verts = np.array(verts) - self.tree = KDTree(self.verts) - - if q != None: - self.q = np.array(q) - - self.cells = {} - self.cells_for_vert = defaultdict(list) - - def get_containing_simplex(self, X): - if not self.cells: - raise Exception("cell connectivity is not set up") - - # get closest point - (dist, indicies) = self.tree.query(X, 2) - closest_point = indicies[0] - - log.debug('X: %s' % X) - log.debug('point index: %d' % closest_point) - log.debug('actual point %s' % self.verts[closest_point]) - log.debug('distance = %0.4f' % dist[0]) - - simplex = None - checked_cells = [] - cells_to_check = list(self.cells_for_vert[closest_point]) - - attempts = 0 - while not simplex and cells_to_check: - attempts += 1 - - if attempts > MAX_SEARCH_COUNT: - raise Exception("Is the search becoming exhaustive?'\ - '(%d attempts)" % attempts) - - cur_cell = cells_to_check.pop(0) - checked_cells.append(cur_cell) - - if cur_cell.contains(X, self): - simplex = cur_cell - continue - - for neighbor in cur_cell.neighbors: - if (neighbor not in checked_cells) \ - and (neighbor not in cells_to_check): - cells_to_check.append(neighbor) - - if not simplex: - raise Exception('no containing simplex found') - - log.debug("simplex vert indicies: %s" % simplex.verts) - R = self.create_mesh(simplex.verts) - log.debug("R:\n%s", R) - - log.debug('total attempts before finding simplex: %d' % attempts) - return R - - def create_mesh(self, indicies): - """ - this function takes a list of indicies, and then creates and - returns a grid object (collection of verts and q). - - note: the input is indicies, the grid contains verts - """ - - return grid(self.verts[indicies], self.q[indicies]) - - def get_simplex_and_nearest_points(self, X, extra_points=3): - """ - this returns two grid objects: R and S. - - R is a grid object that is a containing simplex around point X - - S : some verts from all points that are not the simplex - """ - simplex_size = self.dim + 1 - log.debug("extra verts: %d" % extra_points) - log.debug("simplex size: %d" % simplex_size) - - r_mesh = self.get_containing_simplex(X) - - # and some UNIQUE extra verts - (dist, indicies) = self.tree.query(X, simplex_size + extra_points) - log.debug("extra indicies: %s" % indicies) - - unique_indicies = [] - for index in indicies: - close_point_in_R = False - for rvert in r_mesh.verts: - if all(rvert == self.verts[index]): - close_point_in_R = True - break - - if not close_point_in_R: - unique_indicies.append(index) - else: - log.debug('throwing out %s: %s' % (index, self.verts[index])) - - log.debug("indicies: %s" % indicies) - log.debug("unique indicies: %s" % unique_indicies) - s_mesh = self.create_mesh(unique_indicies) - - return (r_mesh, s_mesh) + def get_simplex_extra_points(self, X, extra_points=8): + pass def interpolate(self, X, order=2, extra_points=3): - (R, S) = self.get_simplex_and_nearest_points(X, extra_points) - answer = interpolate(X, R, S, order) - return answer - - def for_qhull_generator(self): - """ - this returns a generator that should be fed into qdelaunay - """ - - yield str(len(self.verts[0])) - yield '%d' % len(self.verts) - - for p in self.verts: - yield "%f %f %f" % tuple(p) - - def for_qhull(self): - """ - this returns a single string that should be fed into qdelaunay - """ - r = '%d\n' % len(self.verts[0]) - r += '%d\n' % len(self.verts) - for p in self.verts: - # r += "%f %f %f\n" % tuple(p) - r += "%s\n" % " ".join("%f" % i for i in p) - return r - - def __str__(self): - r = '' - assert(len(self.verts) == len(self.q)) - for c, i in enumerate(zip(self.verts, self.q)): - r += "%d vert(%s): q(%0.4f)" % (c, i[0], i[1]) - cell_str = ", ".join([str(f.name) for f in self.cells_for_vert[c]]) - r += " cells: [%s]" % cell_str - r += "\n" - if self.cells: - for v in self.cells.itervalues(): - r += "%s\n" % v - return r - - def normalize_q(self, new_max=0.1): - largest_number = np.max(np.abs(self.q)) - self.q *= new_max / largest_number + r, s = self.get_simplex_extra_points(X, extra_points=extra_points) + return interpolate(X, self.points[r], self.values[r], + self.points[s], self.values[s], order=order) def dump_to_blender_files(self, pfile='/tmp/points.p', cfile='/tmp/cells.p'): @@ -192,81 +35,13 @@ class grid(object): pickle.dump([f.verts for f in self.cells.itervalues()], open(cfile, 'w')) - def get_xml(self): - doc = Document() - ps = doc.createElement("points") - doc.appendChild(ps) - for i in zip(self.verts, self.q): - p = doc.createElement("point") - - p.setAttribute("x", str(i[0][0])) - p.setAttribute('y', str(i[0][1])) - p.setAttribute('z', str(i[0][2])) - p.setAttribute('q', str(i[1])) - ps.appendChild(p) - - return doc - - def toxml(self): - return self.get_xml().toxml() - - def toprettyxml(self): - return self.get_xml().toprettyxml() - - -class cell(object): - def __init__(self, name): - self.name = name - self.verts = [] - self.neighbors = [] - - def add_vert(self, v): - """ - v should be an index into grid.verts - """ - self.verts.append(v) - - def add_neighbor(self, n): - """ - reference to another cell object - """ - self.neighbors.append(n) - - def contains(self, X, G): - """ - X = point of interest - G = corrensponding grid object (G.verts) - - because of the way i'm storing things, a cell simply stores - indicies, and so one must pass in a reference to the grid object - containing real verts. - - this simply calls grid.simplex.contains - """ - return contains(X, [G.verts[i] for i in self.verts]) - - def __str__(self): - # neighbors = [str(i.name) for i in self.neighbors] - return '' %\ - ( - self.name, - self.verts, - len(self.neighbors), - # ", ".join(neighbors) - ) - - __repr__ = __str__ - def contains(X, R): """ tests if X (point) is in R - R is a simplex, represented by a list of n-degree coordinates + R is a simplex, represented by a list of N-degree coordinates """ phis = get_phis(X, R) - - r = True - if [i for i in phis if i < 0.0 - TOL]: - r = False - return r + any_negatives = any(map(lambda x: x < 0, phis)) + return not any_negatives diff --git a/interp/grid/delaunay.py b/interp/grid/delaunay.py index 71fe17f..826f64e 100644 --- a/interp/grid/delaunay.py +++ b/interp/grid/delaunay.py @@ -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+(?Pf\d+).*? - vertices:\s(?P.*?)\n.*? - neighboring\s facets:\s+(?P[\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)