I found a way to do this.
I first computed a sparse adjacency matrix (NxN) where N is the total number of triangular faces. One of the ways to get the adjacency matrix is mentioned in:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/46618
Suppose the name of the sparse Adjacency matrix is: adjSpr
Then I used Matlab's Grap Theory toolbox to compute the connected triangles:
[Sg,Cg]=graphconncomp(adjSpr);
where Sg gives the total number of groups formed by connected triangles, and Cg is a cell containing triangular faces in Sg number of groups. The method is quite efficient.
The graph can be visualized by:
bg=biograph(adjSpr);
view(bg);
A slightly in efficient way for the computation of adjacency matrix is:
numFaces=tris.size(1);
nbList=tris.neighbors;
adjMat=zeros(numFaces);
for count=1:numFaces
if ~isnan(nbList(count,1))
adjMat(count, nbList(count,1))=1;
end
if ~isnan(nbList(count,2))
adjMat(count, nbList(count,2))=1;
end
if ~isnan(nbList(count,3))
adjMat(count, nbList(count,3))=1;
end
end
adjMat=adjMat-diag(diag(adjMat)); % remove the diagonal since a face is always connected to itself
Best Regards
Wajahat