spm_mesh_isosurface.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. function M = spm_mesh_isosurface(V, t, s)
  2. % Compute isosurface geometry from volume data
  3. % FORMAT M = spm_mesh_isosurface(V, t, s)
  4. % V - volume data
  5. % spm_vol struct, nifti object or 3D array
  6. % t - isosurface value
  7. % s - Gaussian filter width (FWHM) in {edges} [Default: 0]
  8. %
  9. % M - patch structure
  10. %
  11. % This is merely a wrapper around isosurface.
  12. %__________________________________________________________________________
  13. % Copyright (C) 2019 Wellcome Trust Centre for Neuroimaging
  14. % Guillaume Flandin
  15. % $Id: spm_mesh_isosurface.m 7677 2019-10-24 09:55:24Z guillaume $
  16. if ischar(V) || isstruct(V)
  17. V = spm_vol(V);
  18. V = struct('dat',spm_read_vols(V),'mat',V.mat);
  19. elseif isa(V,'nifti')
  20. V = struct('dat',full(V.dat),'mat',V.mat);
  21. elseif isnumeric(V) || islogical(V)
  22. V = struct('dat',V,'mat',eye(4));
  23. else
  24. error('Invalid volume data type.');
  25. end
  26. if nargin < 3, s = 0; end
  27. if any(s)
  28. spm_smooth(V.dat, V.dat, s);
  29. end
  30. for i=1:numel(t)
  31. [faces,vertices] = isosurface(V.dat, t(i));
  32. if isempty(vertices)
  33. faces = zeros(0,3);
  34. vertices = zeros(0,3);
  35. end
  36. % Swap around x and y because isosurface uses meshgrid and not ndgrid
  37. mat = V(1).mat(1:3,:) * [0 1 0 0;1 0 0 0;0 0 1 0; 0 0 0 1];
  38. vertices = (mat * [vertices'; ones(1,size(vertices,1))])';
  39. M(i) = struct('faces',faces, 'vertices',vertices);
  40. end