[FV] = mesh_vertex_spacing(FV) FV.vertices - Nx3 FV.faces - Nx3 FV.edges - NxN, see mesh_edges This function shifts vertex location toward the center of its neighbouring vertices. It is useful to maintain vertex spacing. This code is developed on the basis of Smith (2002, fig 4). Smith, S. (2002). Fast robust automated brain extraction. Human Brain Mapping, 17(3): 143-155.
0001 function [FV] = mesh_vertex_spacing(FV), 0002 0003 % [FV] = mesh_vertex_spacing(FV) 0004 % 0005 % FV.vertices - Nx3 0006 % FV.faces - Nx3 0007 % FV.edges - NxN, see mesh_edges 0008 % 0009 % This function shifts vertex location toward the center of its 0010 % neighbouring vertices. It is useful to maintain vertex spacing. 0011 % 0012 % This code is developed on the basis of Smith (2002, fig 4). 0013 % 0014 % Smith, S. (2002). Fast robust automated brain extraction. 0015 % Human Brain Mapping, 17(3): 143-155. 0016 0017 0018 if isfield(FV,'edges'), 0019 if isempty(FV.edges), 0020 FV.edges = mesh_edges(FV); 0021 end 0022 else 0023 FV.edges = mesh_edges(FV); 0024 end 0025 0026 %[normals,unit_normals] = mesh_vertex_normals(FV); 0027 0028 % get surface normals 0029 hf = figure('Visible','off'); 0030 hp = patch('faces',FV.faces,'vertices',FV.vertices); 0031 normals = get(hp,'VertexNormals'); 0032 close(hf); clear hf hp 0033 %Convert to unit normals 0034 [normals,unit_normals] = colnorm(normals'); 0035 unit_normals = unit_normals'; 0036 clear normals 0037 0038 0039 Nvertices = size(FV.vertices,1); 0040 0041 for index = 1:Nvertices, 0042 0043 v = FV.vertices(index,:); 0044 x = FV.vertices(index,1); 0045 y = FV.vertices(index,2); 0046 z = FV.vertices(index,3); 0047 0048 unit_normal = unit_normals(index,:); 0049 0050 % Find neighbouring vertex coordinates 0051 vi = find(FV.edges(index,:)); % the indices 0052 neighbour_vertices = FV.vertices(vi,:); 0053 X = neighbour_vertices(:,1); 0054 Y = neighbour_vertices(:,2); 0055 Z = neighbour_vertices(:,3); 0056 0057 % Find neighbour mean location; this is 'mean position of A and B' in 0058 % figure 4 of Smith (2002) 0059 Xmean = mean(X); 0060 Ymean = mean(Y); 0061 Zmean = mean(Z); 0062 0063 % Find difference in distance between the vertex of interest and its 0064 % neighbours; this value is 's' and 'sn' in figure 4 of 0065 % Smith (2002, eq. 1 to 4) 0066 s = [ Xmean - x, Ymean - y, Zmean - z]; % inward toward mean 0067 0068 % Find the vector sn 0069 % the projection of s in the direction of the surface normal 0070 sn = dot( s, unit_normal ) * unit_normal; 0071 0072 % Find the vector st, the component of s orthogonal to the 0073 % surface normal vector. 0074 st = s - sn; % absolute value 0075 0076 0077 S(index,:) = s; 0078 Sn(index,:) = sn; 0079 St(index,:) = st; 0080 0081 end 0082 0083 % We can now use St to move the vertex toward the mean location of the 0084 % neighbour vertices 0085 0086 FV.vertices = FV.vertices + St; 0087 0088 return