I have an irregular mesh which is described by two variables – a faces array that stores the indices of the vertices that constitute each face, and a verts array that stores the coordinates of each vertex. I also have a function that is assumed to be piecewise constant over each face, and it is stored in the form of an array of values per face.
I am looking for a way to construct a function f from this data. Something along the following lines:
faces = [[0,1,2], [1,2,3], [2,3,4] ...]
verts = [[0,0], [0,1], [1,0], [1,1],....]
vals = [0.0, 1.0, 0.5, 3.0,....]
f = interpolate(faces, verts, vals)
f(0.2, 0.2) = 0.0 # point inside face [0,1,2]
f(0.6, 0.6) = 1.0 # point inside face [1,2,3]
The manual way of evaluating f(x,y) would be to find the corresponding face that the point x,y lies in, and return the value that is stored in that face. Is there a function that already implements this in scipy (or in matlab)?
There’s no built-in function in MATLAB that will do what you want. You could likely build your own algorithm using the function INPOLYGON as suggested by Jonas, but you may be able to create a faster implementation yourself using some standard algorithms for finding whether a point is within a polygon.
A while back I wrote my own code for finding the intersection points between a line segment and a set of triangular surfaces in 3-D, and I found this softsurfer link to be the most helpful for implementing the algorithm. My case was more complicated than yours. Since you are working in 2-D, you can ignore the first section of the link about finding the point where the segment intersects the plane of the triangle.
I’ve included a simplified version of my MATLAB code below for you to use. The function
interpolatewill take yourfaces,vertices, andvaluesmatrices as inputs and return a function handlefthat can be evaluated at a given (x,y) point to get the piecewise value within the bounding triangle. Here are a few features of this code:fis contained in the nested functionevaluate_function. This function has access to the other variables ininterpolate, so a number of variables needed for the in-triangle test are precomputed so thatevaluate_functionruns as fast as possible.f.There are some things that are not included in the code, which you may want to add in depending on what you are using it for:
facesis an N-by-3 matrix,verticesis an M-by-2 matrix, andvaluesis a length N vector. You would likely want to add error checking to make sure that the inputs conform to these requirements, and throw an error indicating when one or more of them is incorrect.facesandverticesinputs may be degenerate (i.e. they may have an area of 0). This occurs when two or more of the triangles vertices are the same exact point, or when all three vertices of the triangle lie in a straight line. You would likely want to add a check that will ignore such triangles when it comes to evaluatingf.facesvariable.Finally, here’s the code: