-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_mesh_clusters.m
44 lines (38 loc) · 1.44 KB
/
spm_mesh_clusters.m
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
function [C, N] = spm_mesh_clusters(M,T)
% Label connected components of surface mesh data
% FORMAT [C, N] = spm_mesh_clusters(M,T)
% M - a [mx3] faces array or a patch structure
% T - a [nx1] data vector (using NaNs or logicals), n = #vertices
%
% C - a [nx1] vector of cluster indices
% N - a [px1] size of connected components {in vertices}
%__________________________________________________________________________
% Copyright (C) 2010-2012 Wellcome Trust Centre for Neuroimaging
% Guillaume Flandin
% $Id: spm_mesh_clusters.m 5065 2012-11-16 20:00:21Z guillaume $
%-Input parameters
%--------------------------------------------------------------------------
if ~islogical(T)
T = ~isnan(T);
end
%-Compute the (reduced) adjacency matrix
%--------------------------------------------------------------------------
A = spm_mesh_adjacency(M);
A = A + speye(size(A));
A(~T,:) = [];
A(:,~T) = [];
%-And perform Dulmage-Mendelsohn decomposition to find connected components
%--------------------------------------------------------------------------
[p,q,r] = dmperm(A);
N = diff(r);
CC = zeros(size(A,1),1);
for i=1:length(r)-1
CC(p(r(i):r(i+1)-1)) = i;
end
C = NaN(numel(T),1);
C(T) = CC;
%-Sort connected component labels according to their size
%--------------------------------------------------------------------------
[N,ni] = sort(N(:), 1, 'descend');
[ni,ni] = sort(ni);
C(T) = ni(C(T));