From 9064663600c1bc7260041008cf6189e5098ab05a Mon Sep 17 00:00:00 2001 From: Stephen Mardson McQuay Date: Sun, 7 Feb 2010 20:23:19 -0700 Subject: [PATCH] added face information to the grid. i need to parse and add point->face information ... somehow ... --- bin/grid_driver.py | 16 +++++++ lib/grid.py | 114 +++++++++++++++++++++++++++++++++++++------- lib/smcqdelaunay.py | 18 ++++--- 3 files changed, 125 insertions(+), 23 deletions(-) create mode 100755 bin/grid_driver.py diff --git a/bin/grid_driver.py b/bin/grid_driver.py new file mode 100755 index 0000000..97c5d6a --- /dev/null +++ b/bin/grid_driver.py @@ -0,0 +1,16 @@ +#!/usr/bin/python + +import sys + +from grid import simple_rect_grid +from smcqdelaunay import get_qdelaunay_dump_str + +if __name__ == '__main__': + try: + resolution = int(sys.argv[1]) + except: + resolution = 3 + g = simple_rect_grid(resolution, resolution) + s = get_qdelaunay_dump_str(g) + g.construct_connectivity(s) + print g diff --git a/lib/grid.py b/lib/grid.py index 2dd1454..9c5c52a 100755 --- a/lib/grid.py +++ b/lib/grid.py @@ -1,22 +1,109 @@ #!/usr/bin/python import sys +import re + import numpy as np import scipy.spatial from tools import exact_func +from smcqdelaunay import * +class face(object): + def __init__(self, name): + self.name = name + self.verts = [] + self.neighbors = [] + + def add_vert(self, v): + self.verts.append(v) + + def add_neighbor(self, n): + self.neighbors.append(n) + + def __str__(self): + neighbors = [i.name for i in self.neighbors] + return '%s: points: %s neighbors: [%s]' %\ + ( + self.name, + self.verts, + ", ".join(neighbors) + ) class grid(object): def __init__(self, points, q): + """ + this thing eats two pre-constructed arrays of stuff: + points = array of arrays (2 for 2D, 3 for 3D) + q = array (1D) of important values + """ + self.points = np.array(points) self.q = np.array(q) + self.faces = {} + + def construct_connectivity(self, s): + facet_re = re.compile(r''' + -\s+(?Pf\d+).*? + vertices:\s(?P.*?)\n.*? + neighboring\s facets:\s+(?P[\sf\d]*) + ''', re.S|re.X) + + vert_re = re.compile(r''' + (p\d+) + ''', re.S|re.X) + + + rajoutter = [] + + for matcher in facet_re.finditer(s): + d = matcher.groupdict() + facet_name = d['facet'] + verticies = d['verts'] + neighbors = d['neigh'] + + cur_face = face(facet_name) + self.faces[facet_name] = cur_face + + for v in vert_re.findall(verticies): + cur_face.add_vert(int(v[1:])) + + nghbrs = [(facet_name, i) for i in neighbors.split()] + rajoutter.extend(nghbrs) + + for rel in rajoutter: + if rel[1] in self.faces: + self.faces[rel[0]].add_neighbor(self.faces[rel[1]]) + + def for_qhull_generator(self): + """ + this returns a generator that should be fed into qdelaunay + """ + + yield '2'; + yield '%d' % len(self.points) + + for p in self.points: + yield "%f %f" % (p[0], p[1]) + + def for_qhull(self): + """ + this returns a single string that should be fed into qdelaunay + """ + r = '2\n' + r += '%d\n' % len(self.points) + for p in self.points: + r += "%f %f\n" % (p[0], p[1]) + return r def __str__(self): r = '' assert( len(self.points) == len(self.q) ) for c, i in enumerate(zip(self.points, self.q)): r += "%d %r: %0.4f\n" % (c,i[0], i[1]) + if self.faces: + for v in self.faces.itervalues(): + r += "%s\n" % v return r class simple_rect_grid(grid): @@ -32,30 +119,22 @@ class simple_rect_grid(grid): ydel = yspan / float(yres - 1) - self.points = [] - self.q = [] + points = [] + q = [] for x in xrange(xres): cur_x = xmin + (x * xdel) for y in xrange(yres): cur_y = ymin + (y * ydel) - self.points.append([cur_x, cur_y]) - self.q.append(exact_func(cur_x, cur_y)) - self.points = np.array(self.points) - self.q = np.array(self.q) + points.append([cur_x, cur_y]) + q.append(exact_func(cur_x, cur_y)) + grid.__init__(self, points, q) - def for_qhull(self): - r = '2\n' - r += '%d\n' % len(self.points) - for p in self.points: - r += "%f %f\n" % (p[0], p[1]) - return r - class simple_random_grid(simple_rect_grid): def __init__(self, num_points = 10): - self.points = [] - self.q = [] + points = [] + q = [] r = np.random @@ -63,8 +142,9 @@ class simple_random_grid(simple_rect_grid): cur_x = r.rand() cur_y = r.rand() - self.points.append([cur_x, cur_y]) - self.q.append(exact_func(cur_x, cur_y)) + points.append([cur_x, cur_y]) + q.append(exact_func(cur_x, cur_y)) + grid.__init__(self, points, q) self.points = np.array(self.points) self.q = np.array(self.q) diff --git a/lib/smcqdelaunay.py b/lib/smcqdelaunay.py index d17ec8d..9be1949 100644 --- a/lib/smcqdelaunay.py +++ b/lib/smcqdelaunay.py @@ -1,7 +1,6 @@ #!/usr/bin/python from subprocess import Popen, PIPE -from grid import simple_rect_grid def get_qdelaunay_dump(g): cmd = 'qdelaunay Qt f' @@ -10,8 +9,15 @@ def get_qdelaunay_dump(g): for i in so.splitlines(): yield i -if __name__ == '__main__': - g = simple_rect_grid(3,3) - print g - for i in get_qdelaunay_dump(g): - print i +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))