VERTEXNORMAL Compute normals to a mesh vertices. N = vertexNormal(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 40]; [v f] = sphereMesh(s); drawMesh(v, f); view(3);axis equal; light; lighting gouraud; normals = vertexNormal(v, f); drawVector3d(v, normals); See also meshes3d, meshFaceNormals, triangulateFaces ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2011-12-19, using Matlab 7.9.0.529 (R2009b) Copyright 2011 INRA - Cepia Software Platform.
0001 function normals = vertexNormal(vertices, faces) 0002 %VERTEXNORMAL Compute normals to a mesh vertices. 0003 % 0004 % N = vertexNormal(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 40]; 0012 % [v f] = sphereMesh(s); 0013 % drawMesh(v, f); 0014 % view(3);axis equal; light; lighting gouraud; 0015 % normals = vertexNormal(v, f); 0016 % drawVector3d(v, normals); 0017 % 0018 % See also 0019 % meshes3d, meshFaceNormals, triangulateFaces 0020 % 0021 % ------ 0022 % Author: David Legland 0023 % e-mail: david.legland@grignon.inra.fr 0024 % Created: 2011-12-19, using Matlab 7.9.0.529 (R2009b) 0025 % Copyright 2011 INRA - Cepia Software Platform. 0026 0027 0028 nv = size(vertices, 1); 0029 nf = size(faces, 1); 0030 0031 % unit normals to the faces 0032 faceNormals = normalizeVector3d(meshFaceNormals(vertices, faces)); 0033 0034 % compute normal of each vertex: sum of normals to each face 0035 normals = zeros(nv, 3); 0036 if isnumeric(faces) 0037 for i = 1:nf 0038 face = faces(i, :); 0039 for j = 1:length(face) 0040 v = face(j); 0041 normals(v, :) = normals(v,:) + faceNormals(i,:); 0042 end 0043 end 0044 else 0045 for i = 1:nf 0046 face = faces{i}; 0047 for j = 1:length(face) 0048 v = face(j); 0049 normals(v, :) = normals(v,:) + faceNormals(i,:); 0050 end 0051 end 0052 end 0053 0054 % normalize vertex normals to unit vectors 0055 normals = normalizeVector3d(normals); 0056 0057