2010-03-20 16:10:02 -07:00
|
|
|
import sys
|
|
|
|
from collections import defaultdict
|
2011-03-28 14:36:50 -07:00
|
|
|
import pickle
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2011-01-26 22:01:18 -08:00
|
|
|
from xml.dom.minidom import Document
|
|
|
|
|
2010-03-20 16:10:02 -07:00
|
|
|
import numpy as np
|
2010-11-08 15:25:59 -08:00
|
|
|
from scipy.spatial import KDTree
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2010-10-23 12:49:15 -07:00
|
|
|
from interp.baker import run_baker
|
2011-03-21 15:55:09 -07:00
|
|
|
import interp.grid.simplex
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2011-03-22 10:10:19 -07:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger("interp")
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2011-03-30 15:11:04 -07:00
|
|
|
MAX_SEARCH_COUNT = 256
|
2011-03-23 16:34:42 -07:00
|
|
|
|
2010-03-20 16:10:02 -07:00
|
|
|
class grid(object):
|
2010-10-23 16:01:10 -07:00
|
|
|
def __init__(self, verts = None, q = None):
|
2010-10-22 15:10:58 -07:00
|
|
|
"""
|
2010-10-23 16:01:10 -07:00
|
|
|
verts = array of arrays (if passed in, will convert to numpy.array)
|
|
|
|
[
|
|
|
|
[x0,y0],
|
|
|
|
[x1,y1], ...
|
|
|
|
]
|
|
|
|
|
|
|
|
q = array (1D) of physical values
|
2010-10-22 15:10:58 -07:00
|
|
|
"""
|
|
|
|
|
2011-01-26 22:01:18 -08:00
|
|
|
if verts != None:
|
2010-10-23 16:01:10 -07:00
|
|
|
self.verts = np.array(verts)
|
2011-02-15 14:22:28 -08:00
|
|
|
self.tree = KDTree(self.verts)
|
|
|
|
|
|
|
|
if q != None:
|
2010-10-23 16:01:10 -07:00
|
|
|
self.q = np.array(q)
|
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
self.cells = {}
|
|
|
|
self.cells_for_vert = defaultdict(list)
|
2010-10-22 15:10:58 -07:00
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
def get_containing_simplex(self, X):
|
2010-11-08 14:07:48 -08:00
|
|
|
if not self.cells:
|
|
|
|
raise Exception("cell connectivity is not set up")
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
# 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
|
2010-11-08 14:07:48 -08:00
|
|
|
checked_cells = []
|
|
|
|
cells_to_check = list(self.cells_for_vert[closest_point])
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
attempts = 0
|
2010-11-08 14:07:48 -08:00
|
|
|
while not simplex and cells_to_check:
|
2010-10-23 16:01:10 -07:00
|
|
|
attempts += 1
|
|
|
|
|
2011-03-23 16:34:42 -07:00
|
|
|
if attempts > MAX_SEARCH_COUNT:
|
2011-03-23 23:27:06 -07:00
|
|
|
raise Exception("Is the search becoming exhaustive? (%d attempts)" % attempts)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
cur_cell = cells_to_check.pop(0)
|
|
|
|
checked_cells.append(cur_cell)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
if cur_cell.contains(X, self):
|
|
|
|
simplex = cur_cell
|
2010-10-23 16:01:10 -07:00
|
|
|
continue
|
|
|
|
|
2010-11-08 14:07:48 -08:00
|
|
|
for neighbor in cur_cell.neighbors:
|
|
|
|
if (neighbor not in checked_cells) and (neighbor not in cells_to_check):
|
|
|
|
cells_to_check.append(neighbor)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
if not simplex:
|
2010-11-01 19:24:12 -07:00
|
|
|
raise Exception('no containing simplex found')
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2010-11-01 19:24:12 -07:00
|
|
|
log.debug("simplex vert indicies: %s" % simplex.verts)
|
2010-10-23 16:01:10 -07:00
|
|
|
R = self.create_mesh(simplex.verts)
|
2011-03-28 18:07:19 -07:00
|
|
|
log.debug("R:\n%s", R)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
log.debug('total attempts before finding simplex: %d' % attempts)
|
|
|
|
return R
|
|
|
|
|
|
|
|
def create_mesh(self, indicies):
|
|
|
|
"""
|
2011-03-23 10:23:32 -07:00
|
|
|
this function takes a list of indicies, and then creates and returns a
|
|
|
|
grid object (collection of verts and q).
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
note: the input is indicies, the grid contains verts
|
|
|
|
"""
|
2010-10-29 11:43:18 -07:00
|
|
|
p = [self.verts[i] for i in indicies]
|
2010-10-23 16:01:10 -07:00
|
|
|
q = [self.q[i] for i in indicies]
|
|
|
|
return grid(p, q)
|
|
|
|
|
2011-03-28 18:07:19 -07:00
|
|
|
def get_simplex_and_nearest_points(self, X, extra_points = 3):
|
2010-10-23 16:01:10 -07:00
|
|
|
"""
|
|
|
|
this returns two grid objects: R and S.
|
|
|
|
|
2011-03-23 10:23:32 -07:00
|
|
|
R is a grid object that is supposedly a containing simplex around point X
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2011-03-23 10:23:32 -07:00
|
|
|
S is S_j from baker's paper : some verts from all point that are not the
|
|
|
|
simplex
|
2010-10-23 16:01:10 -07:00
|
|
|
"""
|
2011-03-28 18:07:19 -07:00
|
|
|
simplex_size = self.dim + 1
|
2010-10-29 11:43:18 -07:00
|
|
|
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)
|
2010-11-01 19:24:12 -07:00
|
|
|
log.debug("extra indicies: %s" % indicies)
|
2010-10-29 11:43:18 -07:00
|
|
|
|
|
|
|
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)
|
2010-10-23 16:01:10 -07:00
|
|
|
|
|
|
|
def run_baker(self, X, extra_points = 3, order = 2):
|
2010-10-29 11:43:18 -07:00
|
|
|
(R, S) = self.get_simplex_and_nearest_points(X, extra_points)
|
|
|
|
answer = run_baker(X, R, S, order)
|
|
|
|
return answer
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2011-03-21 14:24:25 -07:00
|
|
|
def for_qhull_generator(self):
|
|
|
|
"""
|
|
|
|
this returns a generator that should be fed into qdelaunay
|
|
|
|
"""
|
|
|
|
|
2011-03-21 15:55:09 -07:00
|
|
|
yield str(len(self.verts[0]));
|
2011-03-21 14:24:25 -07:00
|
|
|
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
|
|
|
|
"""
|
2011-03-21 15:55:09 -07:00
|
|
|
r = '%d\n' % len(self.verts[0])
|
2011-03-21 14:24:25 -07:00
|
|
|
r += '%d\n' % len(self.verts)
|
|
|
|
for p in self.verts:
|
2011-03-21 16:27:12 -07:00
|
|
|
# r += "%f %f %f\n" % tuple(p)
|
|
|
|
r += "%s\n" % " ".join("%f" % i for i in p)
|
2011-03-21 14:24:25 -07:00
|
|
|
return r
|
|
|
|
|
2010-10-23 16:01:10 -07:00
|
|
|
def __str__(self):
|
|
|
|
r = ''
|
|
|
|
assert( len(self.verts) == len(self.q) )
|
|
|
|
for c, i in enumerate(zip(self.verts, self.q)):
|
2010-10-29 11:43:18 -07:00
|
|
|
r += "%d vert(%s): q(%0.4f)" % (c,i[0], i[1])
|
2010-11-08 14:07:48 -08:00
|
|
|
cell_str = ", ".join([str(f.name) for f in self.cells_for_vert[c]])
|
|
|
|
r += " cells: [%s]" % cell_str
|
2010-10-23 16:01:10 -07:00
|
|
|
r += "\n"
|
2010-11-08 14:07:48 -08:00
|
|
|
if self.cells:
|
|
|
|
for v in self.cells.itervalues():
|
2010-10-23 16:01:10 -07:00
|
|
|
r += "%s\n" % v
|
|
|
|
return r
|
|
|
|
|
2011-01-26 22:01:18 -08:00
|
|
|
def normalize_q(self, new_max = 0.1):
|
|
|
|
largest_number = np.max(np.abs(self.q))
|
|
|
|
self.q *= new_max/largest_number
|
|
|
|
|
|
|
|
|
2011-03-28 14:36:50 -07:00
|
|
|
def dump_to_blender_files(self, pfile = '/tmp/points.p', cfile = '/tmp/cells.p'):
|
|
|
|
if len(self.verts[0]) == 2:
|
|
|
|
pickle.dump([(p[0], p[1], 0.0) for p in self.verts], open(pfile, 'w'))
|
|
|
|
else:
|
|
|
|
pickle.dump([(p[0], p[1], p[2]) for p in self.verts], open(pfile, 'w'))
|
|
|
|
|
|
|
|
pickle.dump([f.verts for f in self.cells.itervalues()], open(cfile, 'w'))
|
2011-01-26 22:01:18 -08:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2011-03-21 15:55:09 -07:00
|
|
|
class cell(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.verts = []
|
|
|
|
self.neighbors = []
|
2010-10-23 16:01:10 -07:00
|
|
|
|
2011-03-21 15:55:09 -07:00
|
|
|
def add_vert(self, v):
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2011-03-21 15:55:09 -07:00
|
|
|
v should be an index into grid.verts
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2011-03-21 15:55:09 -07:00
|
|
|
self.verts.append(v)
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2011-03-21 15:55:09 -07:00
|
|
|
def add_neighbor(self, n):
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2011-03-21 15:55:09 -07:00
|
|
|
reference to another cell object
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2011-03-21 15:55:09 -07:00
|
|
|
self.neighbors.append(n)
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2011-03-21 15:55:09 -07:00
|
|
|
def contains(self, X, G):
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2011-03-21 15:55:09 -07:00
|
|
|
X = point of interest
|
|
|
|
G = corrensponding grid object (G.verts)
|
2011-03-23 10:23:32 -07:00
|
|
|
|
|
|
|
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.
|
2010-03-20 16:10:02 -07:00
|
|
|
|
2011-03-21 15:55:09 -07:00
|
|
|
this simply calls grid.simplex.contains
|
2010-03-20 16:10:02 -07:00
|
|
|
"""
|
2011-03-21 15:55:09 -07:00
|
|
|
return interp.grid.simplex.contains(X, [G.verts[i] for i in self.verts])
|
|
|
|
|
|
|
|
def __str__(self):
|
2011-03-28 18:07:19 -07:00
|
|
|
# neighbors = [str(i.name) for i in self.neighbors]
|
|
|
|
return '<cell %s: verts: %s neighbor count: %s>' %\
|
2011-03-21 15:55:09 -07:00
|
|
|
(
|
|
|
|
self.name,
|
|
|
|
self.verts,
|
2011-03-28 18:07:19 -07:00
|
|
|
len(self.neighbors),
|
|
|
|
# ", ".join(neighbors)
|
2011-03-21 15:55:09 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
__repr__ = __str__
|