MESHEDGEFACES Compute index of faces adjacent to each edge of a mesh. EF = meshEdgeFaces(V, E, F) Compute index array of faces adjacent to each edge of a mesh. V, E and F define the mesh: V is vertex array, E contains vertex indices of edge extremities, and F contains vertex indices of each face, either as a numerical array or as a cell array. The result EF has as many rows as the number of edges, and two column. The first column contains index of faces located on the left of the corresponding edge, whereas the second column contains index of the face located on the right. Some indices may be 0 if the mesh is not 'closed'. Note: a faster version is available for triangular meshes. Example meshEdgeFaces See also meshes3d, trimeshEdgeFaces, meshDihedralAngles, polyhedronMeanBreadth
0001 function edgeFaces = meshEdgeFaces(vertices, edges, faces) %#ok<INUSL> 0002 %MESHEDGEFACES Compute index of faces adjacent to each edge of a mesh. 0003 % 0004 % EF = meshEdgeFaces(V, E, F) 0005 % Compute index array of faces adjacent to each edge of a mesh. 0006 % V, E and F define the mesh: V is vertex array, E contains vertex 0007 % indices of edge extremities, and F contains vertex indices of each 0008 % face, either as a numerical array or as a cell array. 0009 % The result EF has as many rows as the number of edges, and two column. 0010 % The first column contains index of faces located on the left of the 0011 % corresponding edge, whereas the second column contains index of the 0012 % face located on the right. Some indices may be 0 if the mesh is not 0013 % 'closed'. 0014 % 0015 % Note: a faster version is available for triangular meshes. 0016 % 0017 % Example 0018 % meshEdgeFaces 0019 % 0020 % See also 0021 % meshes3d, trimeshEdgeFaces, meshDihedralAngles, polyhedronMeanBreadth 0022 0023 % ------ 0024 % Author: David Legland 0025 % e-mail: david.legland@grignon.inra.fr 0026 % Created: 2010-10-04, using Matlab 7.9.0.529 (R2009b) 0027 % Copyright 2010 INRA - Cepia Software Platform. 0028 0029 Ne = size(edges, 1); 0030 0031 % indices of faces adjacent to each edge 0032 edgeFaces = zeros(Ne, 2); 0033 0034 % different method for extracting current face depending if faces are 0035 % stored as index 2D array or as cell array of 1D arrays. 0036 if isnumeric(faces) 0037 Nf = size(faces, 1); 0038 for i = 1:Nf 0039 face = faces(i, :); 0040 processFace(face, i) 0041 end 0042 elseif iscell(faces) 0043 Nf = length(faces); 0044 for i = 1:Nf 0045 face = faces{i}; 0046 processFace(face, i) 0047 end 0048 end 0049 0050 function processFace(face, indFace) 0051 % iterate on face edges 0052 for j = 1:length(face) 0053 % build edge: array of vertices 0054 j2 = mod(j, length(face)) + 1; 0055 0056 % do not process edges with same vertices 0057 if face(j) == face(j2) 0058 continue; 0059 end 0060 0061 % vertex indices of current edge 0062 currentEdge = [face(j) face(j2)]; 0063 0064 % find index of current edge, assuming face is left-located 0065 b1 = ismember(edges, currentEdge, 'rows'); 0066 indEdge = find(b1); 0067 if ~isempty(indEdge) 0068 if edgeFaces(indEdge, 1) ~= 0 0069 error('meshes3d:IllegalTopology', ... 0070 'Two faces were found on left side of edge %d ', indEdge); 0071 end 0072 0073 edgeFaces(indEdge, 1) = indFace; 0074 continue; 0075 end 0076 0077 % otherwise, assume the face is right-located 0078 b2 = ismember(edges, currentEdge([2 1]), 'rows'); 0079 indEdge = find(b2); 0080 if ~isempty(indEdge) 0081 if edgeFaces(indEdge, 2) ~= 0 0082 error('meshes3d:IllegalTopology', ... 0083 'Two faces were found on left side of edge %d ', indEdge); 0084 end 0085 0086 edgeFaces(indEdge, 2) = indFace; 0087 continue; 0088 end 0089 0090 % If face was neither left nor right, error 0091 warning('meshes3d:IllegalTopology', ... 0092 'Edge %d of face %d was not found in edge array', ... 0093 j, indFace); 0094 continue; 0095 end 0096 end 0097 0098 end