centroid=meshcentroid(v,f) compute the centroids of a mesh defined by nodes and elements (surface or tetrahedra) in R^n space author: Qianqian Fang, <q.fang at neu.edu> input: v: surface node list, dimension (nn,3) f: surface face element list, dimension (be,3) output: centroid: centroid positions, one row for each element -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0001 function centroid=meshcentroid(v,f) 0002 % 0003 % centroid=meshcentroid(v,f) 0004 % 0005 % compute the centroids of a mesh defined by nodes and elements 0006 % (surface or tetrahedra) in R^n space 0007 % 0008 % author: Qianqian Fang, <q.fang at neu.edu> 0009 % 0010 % input: 0011 % v: surface node list, dimension (nn,3) 0012 % f: surface face element list, dimension (be,3) 0013 % 0014 % output: 0015 % centroid: centroid positions, one row for each element 0016 % 0017 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net) 0018 % 0019 0020 if(~iscell(f)) 0021 ec=reshape(v(f(:,1:size(f,2))',:)', [size(v,2) size(f,2) size(f,1)]); 0022 centroid=squeeze(mean(ec,2))'; 0023 else 0024 len=length(f); 0025 centroid=zeros(len,size(v,2)); 0026 try 0027 for i=1:len 0028 fc=f{i}; 0029 if(~isempty(fc)) % need to set centroid to NaN if fc is empty? 0030 vlist=fc{1}; 0031 centroid(i,:)=mean(v(vlist(find(~isnan(vlist))),:)); 0032 end 0033 end 0034 catch 0035 error('malformed face cell array'); 0036 end 0037 end 0038