5
votes

Is there any decent Pythonic way to interpolate in a triangular mesh, or would I need to implement that myself? That is to say, given a (X,Y) point we'll call P, and a mesh (vertices at (X,Y) with value Z, forming triangular facets), estimate the value at P. So that means first find the facet that contains the point, then interpolate - ideally a higher order interpolation than just "linearly between the facet's vertices" (i.e., taking into account the neighboring facets)?

I could implement it myself, but if there's already something available in Python....

(I checked scipy.interpolate, but its "meshes" seem to just be regular point grids. This isn't a grid, it's a true 2D mesh; the vertices can be located anywhere.)

1
Based on my work last night: I found nothing preexisting that looks to work well. I'm using scipy.spatial.KDTree to find all nearby facets, which are then to be filtered down by bounding box, and lastly by a containment check. I've precalculated for all of my facets their neighbors and a quadratic formula for the height relative to distance from the shared edge (2 attached facets = 4 unique points; with two defining the edge then that's three distance points to solve for three variables). Plan is to estimate height from each edge and weigh them by relative edge distances. Should be doable.KarenRei

1 Answers

5
votes

I often use matplotlib.tri for this purpose. Here Xv,Yv are the vertices (or nodes) of the triangles, and Zv the values at those nodes:

from matplotlib.tri import Triangulation, LinearTriInterpolator, CubicTriInterpolator

#you can add keyword triangles here if you have the triangle array, size [Ntri,3]
triObj = Triangulation(Xv,Yv) 

#linear interpolation
fz = LinearTriInterpolator(triObj,Zv)
Z = fz(X,Y)
#cubic interpolation
fzc = CubicTriInterpolator(triObj,Zv)
Zc = fz(X,Y)