2009-12-27 09:48:27 -08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import sys
|
2010-02-07 19:23:19 -08:00
|
|
|
import re
|
|
|
|
|
2009-12-27 09:48:27 -08:00
|
|
|
import numpy as np
|
|
|
|
import scipy.spatial
|
|
|
|
|
2010-01-30 11:15:00 -08:00
|
|
|
from tools import exact_func
|
2010-02-07 19:23:19 -08:00
|
|
|
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)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2010-02-07 19:23:19 -08:00
|
|
|
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)
|
|
|
|
)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
class grid(object):
|
|
|
|
def __init__(self, points, q):
|
2010-02-07 19:23:19 -08:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2009-12-27 09:48:27 -08:00
|
|
|
self.points = np.array(points)
|
|
|
|
self.q = np.array(q)
|
2010-02-07 19:23:19 -08:00
|
|
|
self.faces = {}
|
|
|
|
|
|
|
|
def construct_connectivity(self, s):
|
|
|
|
facet_re = re.compile(r'''
|
|
|
|
-\s+(?P<facet>f\d+).*?
|
|
|
|
vertices:\s(?P<verts>.*?)\n.*?
|
|
|
|
neighboring\s facets:\s+(?P<neigh>[\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
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
r = ''
|
|
|
|
assert( len(self.points) == len(self.q) )
|
2010-02-06 23:01:02 -08:00
|
|
|
for c, i in enumerate(zip(self.points, self.q)):
|
|
|
|
r += "%d %r: %0.4f\n" % (c,i[0], i[1])
|
2010-02-07 19:23:19 -08:00
|
|
|
if self.faces:
|
|
|
|
for v in self.faces.itervalues():
|
|
|
|
r += "%s\n" % v
|
2009-12-27 09:48:27 -08:00
|
|
|
return r
|
|
|
|
|
|
|
|
class simple_rect_grid(grid):
|
|
|
|
def __init__(self, xres = 5, yres = 5):
|
|
|
|
xmin = -1.0
|
|
|
|
xmax = 1.0
|
|
|
|
xspan = xmax - xmin
|
|
|
|
xdel = xspan / float(xres - 1)
|
|
|
|
|
|
|
|
ymin = -1.0
|
|
|
|
ymay = 1.0
|
|
|
|
yspan = ymay - ymin
|
|
|
|
ydel = yspan / float(yres - 1)
|
|
|
|
|
|
|
|
|
2010-02-07 19:23:19 -08:00
|
|
|
points = []
|
|
|
|
q = []
|
2009-12-27 09:48:27 -08:00
|
|
|
for x in xrange(xres):
|
|
|
|
cur_x = xmin + (x * xdel)
|
|
|
|
for y in xrange(yres):
|
|
|
|
cur_y = ymin + (y * ydel)
|
2010-02-07 19:23:19 -08:00
|
|
|
points.append([cur_x, cur_y])
|
|
|
|
q.append(exact_func(cur_x, cur_y))
|
|
|
|
grid.__init__(self, points, q)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class simple_random_grid(simple_rect_grid):
|
|
|
|
def __init__(self, num_points = 10):
|
2010-02-07 19:23:19 -08:00
|
|
|
points = []
|
|
|
|
q = []
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
r = np.random
|
|
|
|
|
|
|
|
for i in xrange(num_points):
|
|
|
|
cur_x = r.rand()
|
|
|
|
cur_y = r.rand()
|
|
|
|
|
2010-02-07 19:23:19 -08:00
|
|
|
points.append([cur_x, cur_y])
|
|
|
|
q.append(exact_func(cur_x, cur_y))
|
|
|
|
grid.__init__(self, points, q)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
self.points = np.array(self.points)
|
|
|
|
self.q = np.array(self.q)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-01-31 19:47:03 -08:00
|
|
|
try:
|
|
|
|
resolution = int(sys.argv[1])
|
|
|
|
except:
|
|
|
|
resolution = 10
|
|
|
|
g = simple_rect_grid(resolution, resolution)
|
2009-12-27 09:48:27 -08:00
|
|
|
print g.for_qhull()
|