Converts a vertex index to x,y,z coordinates. Usage: coord = SurfStatInd2Coord( ind, surf ); coord = c x 3 matrix of coordinates for finding indices thereof. surf.coord = 3 x v matrix of coordinates. or surf.lat = 3D logical array, 1=in, 0=out. surf.vox = 1 x 3 vector of voxel sizes in mm, [1 1 1] by default. surf.origin = position in mm of the first voxel, [0 0 0] by default. ind = c x 1 vector of indices of the nearest vertex to the surface, 1-based. If surf is a volume and the point is outside, then ind=0.
0001 function ind = SurfStatCoord2Ind( coord, surf ); 0002 0003 %Converts a vertex index to x,y,z coordinates. 0004 % 0005 % Usage: coord = SurfStatInd2Coord( ind, surf ); 0006 % 0007 % coord = c x 3 matrix of coordinates for finding indices thereof. 0008 % surf.coord = 3 x v matrix of coordinates. 0009 % or 0010 % surf.lat = 3D logical array, 1=in, 0=out. 0011 % surf.vox = 1 x 3 vector of voxel sizes in mm, [1 1 1] by default. 0012 % surf.origin = position in mm of the first voxel, [0 0 0] by default. 0013 % 0014 % ind = c x 1 vector of indices of the nearest vertex to the surface, 0015 % 1-based. If surf is a volume and the point is outside, then ind=0. 0016 0017 c=size(coord,1); 0018 ind=zeros(c,1); 0019 if isfield(surf,'coord') 0020 v=size(surf.coord,2); 0021 for i=1:c 0022 dist=sum((surf.coord-repmat(coord(i,:)',1,v)).^2); 0023 ind(i)=find(dist==min(dist)); 0024 end 0025 end 0026 if isfield(surf,'lat') 0027 if ~isfield(surf,'vox') 0028 surf.vox=ones(1,3); 0029 end 0030 if ~isfield(surf,'origin'); 0031 surf.origin=zeros(1,3); 0032 end 0033 i=round((coord(:,1)-surf.origin(1))/(surf.vox(1)+(surf.vox(1)==0))+1); 0034 j=round((coord(:,2)-surf.origin(2))/(surf.vox(2)+(surf.vox(2)==0))+1); 0035 k=round((coord(:,3)-surf.origin(3))/(surf.vox(3)+(surf.vox(3)==0))+1); 0036 dim=size(surf.lat); 0037 i(i<1 | i>dim(1))=0; 0038 j(j<1 | j>dim(2))=0; 0039 k(k<1 | k>dim(3))=0; 0040 a=i&j&k; 0041 ind=zeros(c,1); 0042 vid=cumsum(surf.lat(:)).*surf.lat(:); 0043 ind(a)=vid(sub2ind(dim,i(a),j(a),k(a))); 0044 end 0045 0046 return 0047 end 0048