POLYHEDRONNORMALANGLE Compute normal angle at a vertex of a 3D polyhedron. THETA = polyhedraNormalAngle(NODES, EDGES, FACES, IND); THETA = polyhedraNormalAngle(NODES, FACES, IND); where NODES is a set of 3D points, and FACES a set of faces, whose elements are indices to NODES array, compute the normal angle at the vertex whose index is given by IND. THETA = polyhedraNormalAngle(GRAPH, IND); Uses a graph structure. GRAPH should contain at least fields : 'nodes' and 'faces'. Example : % create a simple (irregular) tetrahedra nodes = [0 0 0;1 0 0;0 1 0;0 0 1]; faces = [1 2 3;1 2 4;1 3 4;2 3 4]; % compute normal angle at each vertex theta = polyhedronNormalAngle(nodes, faces, 1:size(nodes, 1)); % sum of normal angles should be equal to 4*pi : sum(theta) TODO works only for polyhedra with convex faces ! ! ! See also polyhedra, polygon3dNormalAngle ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2005-11-30 Copyright 2005 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).
0001 function theta = polyhedronNormalAngle(varargin) 0002 %POLYHEDRONNORMALANGLE Compute normal angle at a vertex of a 3D polyhedron. 0003 % 0004 % THETA = polyhedraNormalAngle(NODES, EDGES, FACES, IND); 0005 % THETA = polyhedraNormalAngle(NODES, FACES, IND); 0006 % where NODES is a set of 3D points, and FACES a set of faces, whose 0007 % elements are indices to NODES array, compute the normal angle at the 0008 % vertex whose index is given by IND. 0009 % 0010 % THETA = polyhedraNormalAngle(GRAPH, IND); 0011 % Uses a graph structure. GRAPH should contain at least fields : 'nodes' 0012 % and 'faces'. 0013 % 0014 % Example : 0015 % % create a simple (irregular) tetrahedra 0016 % nodes = [0 0 0;1 0 0;0 1 0;0 0 1]; 0017 % faces = [1 2 3;1 2 4;1 3 4;2 3 4]; 0018 % % compute normal angle at each vertex 0019 % theta = polyhedronNormalAngle(nodes, faces, 1:size(nodes, 1)); 0020 % % sum of normal angles should be equal to 4*pi : 0021 % sum(theta) 0022 % 0023 % 0024 % TODO works only for polyhedra with convex faces ! ! ! 0025 % 0026 % See also 0027 % polyhedra, polygon3dNormalAngle 0028 % 0029 % ------ 0030 % Author: David Legland 0031 % e-mail: david.legland@grignon.inra.fr 0032 % Created: 2005-11-30 0033 % Copyright 2005 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0034 0035 0036 if length(varargin)==4 0037 nodes = varargin{1}; 0038 faces = varargin{3}; 0039 ind = varargin{4}; 0040 0041 elseif length(varargin)==3 0042 nodes = varargin{1}; 0043 faces = varargin{2}; 0044 ind = varargin{3}; 0045 0046 elseif length(varargin)==2 0047 graph = varargin{1}; 0048 nodes = graph.nodes; 0049 faces = graph.faces; 0050 ind = varargin{2}; 0051 else 0052 error('wrong number of arguments'); 0053 end 0054 0055 0056 % number of angles to compute 0057 na = length(ind); 0058 0059 theta = zeros(na, 1); 0060 for i=1:na 0061 0062 thetaf = []; 0063 0064 % find faces containing given vertex, 0065 % and compute normal angle at each face containing vertex 0066 if iscell(faces) 0067 for j=1:length(faces) 0068 if ismember(ind(i), faces{j}) 0069 % create 3D polygon 0070 face = nodes(faces{j}, :); 0071 0072 % index of point in polygon 0073 indp = find(faces{j}==i); 0074 0075 % compute normal angle of vertex 0076 thetaf = [thetaf polygon3dNormalAngle(face, indp)]; %#ok<AGROW> 0077 end 0078 end 0079 else 0080 indf = find(sum(ismember(faces, ind(i)), 2)); 0081 0082 thetaf = zeros(length(indf), 1); 0083 for j=1:length(indf) 0084 ind2 = faces(indf(j), :); 0085 face = nodes(ind2, :); 0086 indp = find(ind2==ind(i)); 0087 thetaf(j) = pi - polygon3dNormalAngle(face, indp); 0088 end 0089 end 0090 0091 0092 % compute normal angle of polyhedron, by use of angle defect formula 0093 theta(i) = 2*pi - sum(thetaf); 0094 0095 end