1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| function zi=interptri(tri,x,y,z,xi,yi)
if nargin~=6
error('Six Input Arguments Required.')
end
x=x( : ); % make input data into vectors
y=y( : );
z=z( : ).';
xlen=length(x);
if ~isequal(xlen,length(y),length(z))
error('X, Y, and Z Must Have the Same Number of Elements.')
end
if size(tri,2)~=3 || any(tri( : )<0) || any(tri( : )>xlen)
error('TRI Must Be a Valid Triangulation of the Data in X, Y, Z.')
end
zisiz=size(xi);
xi=xi( : );
yi=yi( : );
if length(xi)~=length(yi)
error('Xi and Yi Must Have the Same Number of Elements.')
end
%ti=tsearch(x,y,tri,xi,yi); % find triangle associated with each data point.
% use tsearchn because tsearch is now gone
ti=tsearchn([x y],tri,[xi yi]);
tinan=isnan(ti); % True for xi, yi outside the convex hull
ti(tinan)=1; % point nan points to triangle one for now
tri=tri(ti,: ); % keep only those triangles where xi and yi exist
x1=x(tri( :,1)); % x data at vertices
x2=x(tri( :,2));
x3=x(tri( :,3));
y1=y(tri( :,1)); % y data at vertices
y2=y(tri( :,2));
y3=y(tri( :,3));
A2=(x2-x1).*(y3-y1) - (x3-x1).*(y2-y1); % shape functions
N( :,3)=((x1-xi).*(y2-yi) - (x2-xi).*(y1-yi))./A2;
N( :,2)=((x3-xi).*(y1-yi) - (x1-xi).*(y3-yi))./A2;
N( :,1)=((x2-xi).*(y3-yi) - (x3-xi).*(y2-yi))./A2;
N(tinan,: )=0; % give zero weight to nan data
zi = sum(z(tri).*N,2); % interpolate
zi(tinan)=nan; % poke in nans where needed
zi=reshape(zi,zisiz); % reshape output to match xi and yi input |
Partager