MESHVERTEXNORMALS Compute normals to a mesh vertices. N = meshVertexNormals(V, F) Computes vertex normals of the mesh given by vertices V and F. V is a vertex array with 3 columns, F is either a NF-by-3 or NF-by-4 index array, or a cell array with NF elements. Example % Draw the vertex normals of a sphere s = [10 20 30 20]; [v f] = sphereMesh(s); figure; drawMesh(v, f); view(3);axis equal; light; lighting gouraud; normals = meshVertexNormals(v, f); drawVector3d(v, normals*2); See also meshes3d, meshFaceNormals, triangulateFaces
0001 function [normals, faceNormals] = meshVertexNormals(varargin) 0002 %MESHVERTEXNORMALS Compute normals to a mesh vertices. 0003 % 0004 % N = meshVertexNormals(V, F) 0005 % Computes vertex normals of the mesh given by vertices V and F. 0006 % V is a vertex array with 3 columns, F is either a NF-by-3 or NF-by-4 0007 % index array, or a cell array with NF elements. 0008 % 0009 % Example 0010 % % Draw the vertex normals of a sphere 0011 % s = [10 20 30 20]; 0012 % [v f] = sphereMesh(s); 0013 % figure; drawMesh(v, f); 0014 % view(3);axis equal; light; lighting gouraud; 0015 % normals = meshVertexNormals(v, f); 0016 % drawVector3d(v, normals*2); 0017 % 0018 % See also 0019 % meshes3d, meshFaceNormals, triangulateFaces 0020 % 0021 0022 % ------ 0023 % Author: David Legland 0024 % e-mail: david.legland@inra.fr 0025 % Created: 2011-12-19, using Matlab 7.9.0.529 (R2009b) 0026 % Copyright 2011 INRA - Cepia Software Platform. 0027 0028 [vertices, faces] = parseMeshData(varargin{:}); 0029 0030 nv = size(vertices, 1); 0031 nf = size(faces, 1); 0032 0033 % unit normals to the faces 0034 faceNormals = normalizeVector3d(meshFaceNormals(vertices, faces)); 0035 0036 % compute normal of each vertex: sum of normals to each face 0037 normals = zeros(nv, 3); 0038 if isnumeric(faces) 0039 for i = 1:nf 0040 face = faces(i, :); 0041 for j = 1:length(face) 0042 v = face(j); 0043 normals(v, :) = normals(v,:) + faceNormals(i,:); 0044 end 0045 end 0046 else 0047 for i = 1:nf 0048 face = faces{i}; 0049 for j = 1:length(face) 0050 v = face(j); 0051 normals(v, :) = normals(v,:) + faceNormals(i,:); 0052 end 0053 end 0054 end 0055 0056 % normalize vertex normals to unit vectors 0057 normals = normalizeVector3d(normals);