spm_mesh_inflate.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function M = spm_mesh_inflate(M,T,S)
  2. % Surface mesh inflation
  3. % FORMAT M = spm_mesh_inflate(M,T,S)
  4. %
  5. % M - surface mesh structure (see patch) or GIfTI object
  6. % or handle to a patch in a figure
  7. % T - number of time steps [default: Inf (auto)]
  8. % S - update display every S time steps [default: 0 (never)]
  9. %__________________________________________________________________________
  10. % Copyright (C) 2009-2011 Wellcome Trust Centre for Neuroimaging
  11. % Guillaume Flandin & Jean Daunizeau
  12. % $Id: spm_mesh_inflate.m 6535 2015-08-25 11:45:26Z vladimir $
  13. if nargin < 3, S = 0; end
  14. if ishandle(M)
  15. v = double(get(M,'Vertices'));
  16. f = get(M,'Faces');
  17. p = M;
  18. else
  19. v = double(M.vertices);
  20. f = double(M.faces);
  21. h = figure('visible','off');
  22. a = axes('parent',h);
  23. if isa(M,'gifti')
  24. p = patch(export(M,'patch'),'parent',a,'visible','off');
  25. else
  26. p = patch(M,'parent',a,'visible','off');
  27. end
  28. S = 0;
  29. end
  30. % Parameters
  31. %--------------------------------------------------------------------------
  32. b = 0.5;
  33. w = 0.05;
  34. if nargin < 2 || isinf(T)
  35. T = floor(size(v,1) * 0.003 - 2);
  36. T = max(T,1);
  37. end
  38. % Compute (normalised) adjacency matrix
  39. %--------------------------------------------------------------------------
  40. A = spm_mesh_adjacency(f);
  41. A = sparse(1:size(v,1),1:size(v,1),1./sum(A,2)) * A;
  42. % Compute bounding box
  43. %--------------------------------------------------------------------------
  44. minxyz = min(v);
  45. maxxyz = max(v);
  46. % Iteratively apply forces to vertices
  47. %--------------------------------------------------------------------------
  48. for i=1:T
  49. % Compute unit normals
  50. %----------------------------------------------------------------------
  51. N = spm_mesh_normals(p,1);
  52. % Compute smoothing force
  53. %----------------------------------------------------------------------
  54. mv = A*v - v;
  55. % Update vertices position
  56. %----------------------------------------------------------------------
  57. v = v + b * (w*repmat(sum(mv.*N,2),1,3).*N + (1-w)*mv);
  58. v = mean((maxxyz - minxyz)./(max(v) - min(v))) * v;
  59. set(p,'Vertices',v);
  60. % Update display
  61. %----------------------------------------------------------------------
  62. if ~mod(i,S)
  63. axis(get(p,'parent'),'image');
  64. drawnow
  65. end
  66. end
  67. % Cleanup
  68. %--------------------------------------------------------------------------
  69. if ~ishandle(M)
  70. M.vertices = v;
  71. close(h);
  72. end