diff --git a/.hgignore b/.hgignore index 493485d..205d1c5 100644 --- a/.hgignore +++ b/.hgignore @@ -1 +1,2 @@ \.pyc$ +\.blend$ diff --git a/bin/grid_driver.py b/bin/grid_driver.py deleted file mode 100755 index 97c5d6a..0000000 --- a/bin/grid_driver.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/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/bin/grid_random.py b/bin/grid_random.py new file mode 100755 index 0000000..bdafe64 --- /dev/null +++ b/bin/grid_random.py @@ -0,0 +1,28 @@ +#!/usr/bin/python + +import sys +import pickle + +from grid import simple_rect_grid, simple_random_grid + +pfile = '/tmp/grid_random.p' +qfile = '/tmp/grid_random.txt' +MAX_POINTS = 200 + +if __name__ == '__main__': + sys.setrecursionlimit(4096) + print sys.getrecursionlimit() + try: + total_points = int(sys.argv[1]) + except: + total_points = 10 + + if total_points > MAX_POINTS: + print "too many points" + sys.exit(1) + + g = simple_random_grid(total_points) + g.construct_connectivity() + print g + open(qfile, 'w').write(g.for_qhull()) + pickle.dump(g, open(pfile, 'w')) diff --git a/bin/grid_regular.py b/bin/grid_regular.py new file mode 100755 index 0000000..719afaa --- /dev/null +++ b/bin/grid_regular.py @@ -0,0 +1,22 @@ +#!/usr/bin/python + +import sys +import pickle + +from grid import simple_rect_grid, simple_random_grid + +pfile = '/tmp/grid_regular.p' +qfile = '/tmp/grid_regular.txt' + +if __name__ == '__main__': + try: + resolution = int(sys.argv[1]) + if resolution > 200: + raise Exception + except: + resolution = 3 + + g = simple_rect_grid(resolution, resolution) + g.construct_connectivity() + open(qfile, 'w').write(g.for_qhull()) + pickle.dump(g, open(pfile, 'w')) diff --git a/lib/grid.py b/lib/grid.py index 6dc9211..8450794 100755 --- a/lib/grid.py +++ b/lib/grid.py @@ -2,6 +2,7 @@ import sys import re +from collections import defaultdict import numpy as np import scipy.spatial @@ -45,19 +46,28 @@ class grid(object): vert_re = re.compile(r''' (p\d+) ''', re.S|re.X) + + def __init__(self, points, q): """ this thing eats two pre-constructed arrays of stuff: - points = array of arrays (2 for 2D, 3 for 3D) + points = array of arrays (i will convert to numpy.array) + [[x0,y0], [x1,y1], ...] q = array (1D) of important values """ self.points = np.array(points) self.q = np.array(q) self.faces = {} - self.facets_for_point = {} + self.facets_for_point = defaultdict(list) - def construct_connectivity(self, qdelaunay_string): + def construct_connectivity(self): + """ + a call to this method prepares the internal connectivity structure. + + this is part of the __init__ for a simple_rect_grid, but can be called from any grid object + """ + qdelaunay_string = get_qdelaunay_dump_str(self) facet_to_facets = [] for matcher in grid.facet_re.finditer(qdelaunay_string): d = matcher.groupdict() @@ -70,7 +80,9 @@ class grid(object): self.faces[facet_name] = cur_face for v in grid.vert_re.findall(verticies): - cur_face.add_vert(int(v[1:])) + vertex_index = int(v[1:]) + cur_face.add_vert(vertex_index) + self.facets_for_point[vertex_index].append(cur_face) nghbrs = [(facet_name, i) for i in neighboring_facets.split()] facet_to_facets.extend(nghbrs) @@ -79,13 +91,13 @@ class grid(object): if rel[1] in self.faces: self.faces[rel[0]].add_neighbor(self.faces[rel[1]]) - for matcher in grid.point_re.finditer(qdelaunay_string): - d = matcher.groupdict() + # for matcher in grid.point_re.finditer(qdelaunay_string): + # d = matcher.groupdict() - point = d['point'] - neighboring_facets = d['neigh'] + # point = d['point'] + # neighboring_facets = d['neigh'] - self.facets_for_point[int(point[1:])] = [i for i in neighboring_facets.split() if i in self.faces] + # self.facets_for_point[int(point[1:])] = [i for i in neighboring_facets.split() if i in self.faces] def for_qhull_generator(self): """ @@ -113,7 +125,9 @@ class grid(object): assert( len(self.points) == len(self.q) ) for c, i in enumerate(zip(self.points, self.q)): r += "%d %r: %0.4f" % (c,i[0], i[1]) - r += " faces: %s\n" % (self.facets_for_point[c]) + facet_str = ", ".join([f.name for f in self.facets_for_point[c]]) + r += " faces: [%s]" % facet_str + r += "\n" if self.faces: for v in self.faces.itervalues(): r += "%s\n" % v @@ -141,6 +155,7 @@ class simple_rect_grid(grid): points.append([cur_x, cur_y]) q.append(exact_func(cur_x, cur_y)) grid.__init__(self, points, q) + self.construct_connectivity() diff --git a/tools/blender/plot.py b/tools/blender/plot.py new file mode 100644 index 0000000..224e550 --- /dev/null +++ b/tools/blender/plot.py @@ -0,0 +1,11 @@ +import pickle + +from Blender import * +import bpy + +p = pickle.load(open('/tmp/grid_driver.p', 'r')) +me = bpy.data.meshes.new('points') +me.verts.extend([[v[0], v[1], 0] for v in p.points]) +me.faces.extend([i.verts for i in p.faces.itervalues()]) +scn = bpy.data.scenes.active +ob = scn.objects.new(me, 'points_obj')