newval=meshinterp(fromval,elemid,elembary,fromelem) Interpolate nodal values from the source mesh to the target mesh based on a linear interpolation author: Qianqian Fang (q.fang at neu.edu) input: fromval: values defined at the source mesh nodes, the row or column number must be the same as the source mesh node number, which is the same as the elemid length elemid: the IDs of the source mesh element that encloses the nodes of the target mesh nodes; a vector of length of target mesh node count; elemid and elembary can be generated by calling [elemid,elembary]=tsearchn(node_src, elem_src, node_target); note that the mapping here is inverse to that in meshremap() elembary: the bary-centric coordinates of each target mesh nodes within the source mesh elements, sum of each row is 1, expect 3 or 4 columns (or can be N-D) fromelem: the element list of the source mesh output: newval: a 2D array with rows equal to the target mesh nodes (nodeto), and columns equals to the value numbers defined at each source mesh node example: [n1,f1,e1]=meshabox([0 0 0],[10 20 5],1); % target mesh [n2,f2,e2]=meshabox([0 0 0],[10 20 5],2); % src mesh [id, ww]=tsearchn(n2,e2,n1); % project target to src mesh value_src=n2(:,[2 1 3]); % create dummy values at src mesh newval=meshinterp(value_src,id, ww, e2); -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0001 function newval=meshinterp(fromval,elemid,elembary,fromelem) 0002 % 0003 % newval=meshinterp(fromval,elemid,elembary,fromelem) 0004 % 0005 % Interpolate nodal values from the source mesh to the target mesh based on 0006 % a linear interpolation 0007 % 0008 % author: Qianqian Fang (q.fang at neu.edu) 0009 % 0010 % input: 0011 % fromval: values defined at the source mesh nodes, the row or column 0012 % number must be the same as the source mesh node number, which 0013 % is the same as the elemid length 0014 % elemid: the IDs of the source mesh element that encloses the nodes of 0015 % the target mesh nodes; a vector of length of target mesh node 0016 % count; elemid and elembary can be generated by calling 0017 % 0018 % [elemid,elembary]=tsearchn(node_src, elem_src, node_target); 0019 % 0020 % note that the mapping here is inverse to that in meshremap() 0021 % 0022 % elembary: the bary-centric coordinates of each target mesh nodes 0023 % within the source mesh elements, sum of each row is 1, expect 0024 % 3 or 4 columns (or can be N-D) 0025 % fromelem: the element list of the source mesh 0026 % 0027 % 0028 % output: 0029 % newval: a 2D array with rows equal to the target mesh nodes (nodeto), 0030 % and columns equals to the value numbers defined at each source 0031 % mesh node 0032 % example: 0033 % 0034 % [n1,f1,e1]=meshabox([0 0 0],[10 20 5],1); % target mesh 0035 % [n2,f2,e2]=meshabox([0 0 0],[10 20 5],2); % src mesh 0036 % [id, ww]=tsearchn(n2,e2,n1); % project target to src mesh 0037 % value_src=n2(:,[2 1 3]); % create dummy values at src mesh 0038 % newval=meshinterp(value_src,id, ww, e2); 0039 % 0040 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net) 0041 % 0042 0043 if(size(fromval,1)==1) 0044 fromval=fromval(:); 0045 end 0046 0047 idx=find(~isnan(elemid)); 0048 0049 allval=reshape(fromval(fromelem(elemid(idx),:),:),length(idx),size(elembary,2),size(fromval,2)); 0050 tmp=cellfun(@(x) sum(elembary(idx,:).*x,2), num2cell(allval,[1 2]),'UniformOutput',false); 0051 newval=size(length(elemid),size(fromval,2)); 0052 newval(idx,:)=squeeze(cat(3,tmp{:})); 0053