spm_mesh_neighbours.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. function [N,D] = spm_mesh_neighbours(M,order)
  2. % Return first-order neighbours of all vertices of a surface mesh
  3. % FORMAT N = spm_mesh_neighbours(M,order)
  4. % M - a patch structure or an adjacency matrix
  5. % order - ordinal or euclidean distance for 1st order neighbours {[0],1}
  6. %
  7. % N - a [nxp] neighbours array (n = #vertices, p = # max neighbours)
  8. % D - a [nxp] distance array to neighbours
  9. % N & D contain 0 when number of neighbours is smaller than p.
  10. %__________________________________________________________________________
  11. % Copyright (C) 2010 Wellcome Trust Centre for Neuroimaging
  12. % Guillaume Flandin
  13. % $Id: spm_mesh_neighbours.m 4081 2010-10-07 14:04:44Z guillaume $
  14. if nargin < 2, order = 0; end
  15. %-Compute adjacency matrix
  16. %--------------------------------------------------------------------------
  17. if issparse(M) && size(M,1) == size(M,2)
  18. A = M;
  19. else
  20. A = spm_mesh_distmtx(M,order);
  21. end
  22. %-Build neighbours array
  23. %--------------------------------------------------------------------------
  24. if nargout > 1
  25. [N, D] = spm_mesh_utils('neighbours',A);
  26. else
  27. N = spm_mesh_utils('neighbours',A);
  28. end
  29. % [i, j, s] = find(A);
  30. % p = max(sum(A~=0,2));
  31. % N = zeros(size(A,1),p);
  32. % if nargout > 1, D = zeros(size(N)); end
  33. % l = 1;
  34. % for k=1:length(j)
  35. % N(j(k),l) = i(k);
  36. % if nargout > 1, D(j(k),l) = s(k); end
  37. % try, if j(k+1) ~= j(k), l = 1; else l = l + 1; end; end
  38. % end