mesh_smooth - smooth vertices of a mesh FV = mesh_smooth(FV,origin,attract) FV is a struct with fields: FV.vertices - Nx3 matrix of Cartesian vertex coordindates (X,Y,Z) FV.faces - Mx3 matrix of triangulation of FV.vertices origin - 1x3 row vector, usually (0,0,0) attract - how much to align the mesh vertices, percent 0:1; currently not implemented, effectively 1
0001 function FV = mesh_smooth(FV,origin,attract), 0002 0003 % mesh_smooth - smooth vertices of a mesh 0004 % 0005 % FV = mesh_smooth(FV,origin,attract) 0006 % 0007 % FV is a struct with fields: 0008 % 0009 % FV.vertices - Nx3 matrix of Cartesian vertex coordindates (X,Y,Z) 0010 % FV.faces - Mx3 matrix of triangulation of FV.vertices 0011 % 0012 % origin - 1x3 row vector, usually (0,0,0) 0013 % 0014 % attract - how much to align the mesh vertices, percent 0:1; 0015 % currently not implemented, effectively 1 0016 % 0017 0018 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $ 0019 0020 % Licence: GNU GPL, no implied or express warranties 0021 % History: 10/2002, Darren.Weber_at_radiology.ucsf.edu 0022 % 0023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0024 0025 0026 if ~isfield(FV,'edge'), 0027 FV.edge = mesh_edges(FV); 0028 end 0029 0030 Nvert = size(FV.vertices,1); 0031 0032 xo = origin(1); yo = origin(2); zo = origin(3); 0033 0034 fprintf('...mesh smoothing...'); tic; 0035 0036 % Check every vertex 0037 for v = 1:Nvert, 0038 0039 x = FV.vertices(v,1); 0040 y = FV.vertices(v,2); 0041 z = FV.vertices(v,3); 0042 0043 % Find direction cosines for line from centre to vertex 0044 d = sqrt( (x-xo)^2 + (y-yo)^2 + (z-zo)^2 ); 0045 l = (x-xo)/d; % cos alpha 0046 m = (y-yo)/d; % cos beta 0047 n = (z-zo)/d; % cos gamma 0048 0049 % Calc distance of neighbour vertices 0050 vi = find(FV.edge(v,:)); % the indices of the neighbours 0051 % remove duplicate vertices 0052 %[vert, i, j] = unique(FV.vertices(vi,:),'rows'); 0053 X = FV.vertices(vi,1); 0054 Y = FV.vertices(vi,2); 0055 Z = FV.vertices(vi,3); 0056 D = sqrt( (X-xo).^2 + (Y-yo).^2 + (Z-zo).^2 ); 0057 0058 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0059 % Maybe check for outliers in D at this point 0060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0061 0062 Dmean = mean(D,1); 0063 0064 % now modify d above using Dmean of neighbour vertices 0065 if d > Dmean, d = d - (d - Dmean); 0066 elseif d < Dmean, d = d + (Dmean - d); 0067 end 0068 0069 % locate vertex at the new distance 0070 x = (l * d) + xo; 0071 y = (m * d) + yo; 0072 z = (n * d) + zo; 0073 FV.vertices(v,:) = [ x y z ]; 0074 0075 end 0076 0077 t = toc; fprintf('done (%5.2f sec).\n',t); 0078 0079 return