% v = triangulate_pareBySideLength(XYZ, v, maxSideLength)
%
% pare results of triangulate by length of sides of
% triangles generated. this is useful because if the
% original vertices were on a regular grid, erroneous
% triangles completing the convex shape enclosing all the
% vertices will have at least one long side and can be pared
% easily.
function v = triangulate_pareBySideLength(XYZ, v, maxSideLength)
N = size(v,2);
tris = cat(3, XYZ(:,v(1,:)), XYZ(:,v(2,:)), XYZ(:,v(3,:)));
tris = permute(tris, [1 3 2]);
tris = tris(1:2,:,:); % ignore Z
accepted = zeros(N,1);
maxSideLengthSq = maxSideLength^2;
for n=1:N
tri = tris(:,:,n);
A = tri(:,1) - tri(:,2);
B = tri(:,2) - tri(:,3);
C = tri(:,3) - tri(:,1);
a = sum(A.^2);
b = sum(B.^2);
c = sum(C.^2);
maxSideSq = max([a b c]);
if maxSideSq <= maxSideLengthSq accepted(n)=1; end
end
v = v(:,find(accepted));