% This function finds the neighbors of a certain triangle in a triangular mesh
% utilized by Matlab's PDE toolbox ([p,e,t] mesh data). Triangles are neighbors
% when they share two common vertices.
%
% Input
% toi: index into the triangle list t of the triangle of interest (toi)
% whose neighbors are to be found
% t: triangle structure
%
% Output
% nbhd: indices of the neighbors of t
function nbhd = fixme_neighbors(toi,t)
% Get the number of triangles
nTriangles = size(t,2);
% Loop over all triangles and try to find the first vertex of toi in them
ix1 = [];
for iterT = 1:nTriangles
if (any(t(1:3,iterT) == t(1,toi)))
ix1 = [ix1; iterT];
end
end
% Loop over all triangles and try to find the second vertex of toi in them
ix2 = [];
for iterT = 1:nTriangles
if (any(t(1:3,iterT) == t(2,toi)))
ix2 = [ix2; iterT];
end
end
% Loop over all triangles and try to find the third vertex of toi in them
ix3 = [];
for iterT = 1:nTriangles
if (any(t(1:3,iterT) == t(3,toi)))
ix3 = [ix3; iterT];
end
end
% Now find all triangle indices sharing precisely two vertices with toi
ix12 = intersect(ix1,ix2);
ix23 = intersect(ix2,ix3);
ix31 = intersect(ix3,ix1);
ix123 = intersect(ix12,ix3);
nbhd = setdiff(union(union(ix12,ix23),ix31),ix123)';