Interpolating 2d piecewise constant data on faces

I have an irregular grid that is described by two variables: an array of faces that stores the indices of the vertices that make up each face, and an array of verts that stores the coordinates of each vertex. I also have a function that is considered a piecewise constant over each face and is stored as an array of values ​​per face.

I am looking for a way to build a function ffrom this data. Something in 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]

A manual way of evaluating f(x,y)would be to find the corresponding face in which the point is located x,yand return the value that is stored on this face. Is there a function that already implements this in scipy (or in matlab)?

+3
5

MATLAB , , . , , INPOLYGON Jonas, , , .

3-D, softsurfer . , . 2-D, , .

MATLAB . interpolate faces, vertices values f, (x, y) , . :

  • , f, evaluate_function. interpolate, , , , evaluate_function .
  • , , , , . , (.. ) . , , .
  • - , NaN f.

, , , :

  • : , faces N--3, vertices M-by-2, values - N . , , , , , , .
  • : , , faces vertices, (.. 0). , . , , , f.
  • : , . , (.. , ..). , faces.

, :

function f = interpolate(faces,vertices,values)

  %# Precompute some data (helps increase speed):

  triVertex = vertices(faces(:,2),:);              %# Triangles main vertices
  triLegLeft = vertices(faces(:,1),:)-triVertex;   %# Triangles left legs
  triLegRight = vertices(faces(:,3),:)-triVertex;  %# Triangles right legs
  C1 = sum(triLegLeft.*triLegRight,2);  %# Dot product of legs
  C2 = sum(triLegLeft.^2,2);            %# Squared length of left leg
  C3 = sum(triLegRight.^2,2);           %# Squared length of right leg
  triBoundary = max(C2,C3);             %# Squared radius of triangle boundary
  scale = C1.^2-C2.*C3;
  C1 = C1./scale;
  C2 = C2./scale;
  C3 = C3./scale;

  %# Return a function handle to the nested function:

  f = @evaluate_function;

  %# The nested evaluation function:

  function val = evaluate_function(x,y)

    w = [x-triVertex(:,1) y-triVertex(:,2)];
    nearIndex = find(sum(w.^2,2) <= triBoundary);  %# Find nearby triangles
    if isempty(nearIndex)
      val = NaN;           %# Return NaN if no triangles are nearby
      return
    end
    w = w(nearIndex,:);
    wdotu = sum(w.*triLegLeft(nearIndex,:),2);
    wdotv = sum(w.*triLegRight(nearIndex,:),2);
    C = C1(nearIndex);
    s = C.*wdotv-C3(nearIndex).*wdotu;
    t = C.*wdotu-C2(nearIndex).*wdotv;
    inIndex = find((s >= 0) & (t >= 0) & (s+t <= 1),1);
    if isempty(inIndex)
      val = NaN;         %# Return NaN if point is outside all triangles
    else
      val = values(nearIndex(inIndex));
    end

  end

end
+1

, , , . . , , . , , ( + - x, , . , , ).

, -, Matlab scipy, , , .

+1

CGAL-python ; , CGAL . , , , , , . Voronoi ( Matlab ), , , .

+1

Matlab inpolygon, , . , .

​​, , , inpolygon , , . .

+1

matplotlib.delaunay.interpolate, C. ( class LinearInterpolator " ".)

+1

Source: https://habr.com/ru/post/1733613/


All Articles