edgenb=edgeneighbors(t,opt) to find neighboring triangular elements in a triangule surface author: Qianqian Fang, <q.fang at neu.edu> input: t: a triangular surface element list, 3 columns of integers opt: if opt='general', return the edge neighbors for a general triangular surface: each edge can be shared by more than 2 triangles; if ignored, we assume all triangles are shared by no more than 2 triangles. output: edgenb: if opt is not supplied, edgenb is a size(t,1) by 3 array with each element being the triangle ID of the edge neighbor of that triangle. For each row, the order of the neighbors is listed as those sharing edges [1 2], [2 3] and [3 1] between the triangle nodes. when opt='general', edgenb is a cell array with a length of size(t). each member of the cell array is a list of edge neighbors (the order is not defined). -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0001 function edgenb=edgeneighbors(t,opt) 0002 % 0003 % edgenb=edgeneighbors(t,opt) 0004 % 0005 % to find neighboring triangular elements in a triangule surface 0006 % 0007 % author: Qianqian Fang, <q.fang at neu.edu> 0008 % 0009 % input: 0010 % t: a triangular surface element list, 3 columns of integers 0011 % opt: if opt='general', return the edge neighbors for a general 0012 % triangular surface: each edge can be shared by more than 2 0013 % triangles; if ignored, we assume all triangles are shared by no 0014 % more than 2 triangles. 0015 % 0016 % output: 0017 % edgenb: if opt is not supplied, edgenb is a size(t,1) by 3 array with 0018 % each element being the triangle ID of the edge neighbor of that 0019 % triangle. For each row, the order of the neighbors is listed as those 0020 % sharing edges [1 2], [2 3] and [3 1] between the triangle nodes. 0021 % 0022 % when opt='general', edgenb is a cell array with a length of size(t). 0023 % each member of the cell array is a list of edge neighbors (the order 0024 % is not defined). 0025 % 0026 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net) 0027 % 0028 0029 edges=[t(:,[1,2]); 0030 t(:,[2,3]); 0031 t(:,[3 1])]; 0032 edges=sort(edges,2); 0033 [foo,ix,jx]=unique(edges,'rows'); 0034 0035 if(nargin==2) 0036 if(strcmp(opt,'general')) 0037 ne=size(t,1); 0038 edgenb=cell(ne,1); 0039 for i=1:ne 0040 % this is very slow, need to be optimized 0041 nb=unique(mod([find(jx==jx(i) | jx==jx(i+ne) | jx==jx(i+2*ne))]',ne),'first'); 0042 nb(nb==0)=ne; 0043 edgenb{i}=nb(nb~=i); 0044 end 0045 return; 0046 else 0047 error(['supplied option "' opt '" is not supported.']); 0048 end 0049 end 0050 0051 if(isoctavemesh) 0052 u=unique(jx); 0053 qx=u(hist(jx,u)==2); 0054 else 0055 vec=histc(jx,1:max(jx)); 0056 qx=find(vec==2); 0057 end 0058 0059 nn=max(t(:)); 0060 ne=size(t,1); 0061 edgenb=zeros(size(t)); 0062 0063 % now I need to find all repeatitive elements 0064 % that share a face, to do this, unique('first') 0065 % will give me the 1st element, and 'last' will 0066 % give me the second. There will be no more than 2 0067 0068 % doing this is 60 times faster than doing find(jx==qx(i)) 0069 % inside a loop 0070 0071 [ujx,ii]=unique(jx,'first'); 0072 [ujx,ii2]=unique(jx,'last'); 0073 0074 % iddup is the list of all pairs that share a common face 0075 0076 iddup=[ii(qx) ii2(qx)]; 0077 faceid=ceil(iddup/ne); 0078 eid=mod(iddup,ne); 0079 eid(eid==0)=ne; 0080 0081 % now rearrange this list into an element format 0082 0083 for i=1:length(qx) 0084 edgenb(eid(i,1),faceid(i,1))=eid(i,2); 0085 edgenb(eid(i,2),faceid(i,2))=eid(i,1); 0086 end 0087 0088 % edgenb may contain 0s, that just means the corresponding 0089 % face is a boundary face and has no neighbor. 0090 0091 % if the second option is 'surface', I am going to find 0092 % and return surface patches only 0093 0094