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