Home > matGeom > meshes3d > meshFaceNormals.m

meshFaceNormals

PURPOSE ^

MESHFACENORMALS Compute normal vector of faces in a 3D mesh.

SYNOPSIS ^

function normals = meshFaceNormals(varargin)

DESCRIPTION ^

MESHFACENORMALS Compute normal vector of faces in a 3D mesh.

   NORMALS = meshFaceNormals(VERTICES, FACES)
   VERTICES is a set of 3D points (as a N-by-3 array), and FACES is either
   a N-by-3 index array or a cell array of indices. The function computes
   the normal vector of each face.
   The orientation of the normal is defined by the sign of cross product
   between vectors joining vertices 1 to 2 and 1 to 3.


   Example
     [v e f] = createIcosahedron;
     normals1 = meshFaceNormals(v, f);
     centros1 = meshFaceCentroids(v, f);
     figure; drawMesh(v, f); 
     hold on; axis equal; view(3);
     drawVector3d(centros1, normals1);

     pts = rand(50, 3);
     hull = minConvexHull(pts);
     normals2 = meshFaceNormals(pts, hull);

   See also
   meshes3d, meshFaceCentroids, meshVertexNormals, drawFaceNormals
   drawMesh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function normals = meshFaceNormals(varargin)
0002 %MESHFACENORMALS Compute normal vector of faces in a 3D mesh.
0003 %
0004 %   NORMALS = meshFaceNormals(VERTICES, FACES)
0005 %   VERTICES is a set of 3D points (as a N-by-3 array), and FACES is either
0006 %   a N-by-3 index array or a cell array of indices. The function computes
0007 %   the normal vector of each face.
0008 %   The orientation of the normal is defined by the sign of cross product
0009 %   between vectors joining vertices 1 to 2 and 1 to 3.
0010 %
0011 %
0012 %   Example
0013 %     [v e f] = createIcosahedron;
0014 %     normals1 = meshFaceNormals(v, f);
0015 %     centros1 = meshFaceCentroids(v, f);
0016 %     figure; drawMesh(v, f);
0017 %     hold on; axis equal; view(3);
0018 %     drawVector3d(centros1, normals1);
0019 %
0020 %     pts = rand(50, 3);
0021 %     hull = minConvexHull(pts);
0022 %     normals2 = meshFaceNormals(pts, hull);
0023 %
0024 %   See also
0025 %   meshes3d, meshFaceCentroids, meshVertexNormals, drawFaceNormals
0026 %   drawMesh
0027 
0028 % ------
0029 % Author: David Legland
0030 % e-mail: david.legland@inra.fr
0031 % Created: 2006-07-05
0032 % Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).
0033 
0034 % HISTORY
0035 % 2011-11-24 rename from faceNormal to meshFaceNormals
0036 
0037 % parse input data
0038 [vertices, faces] = parseMeshData(varargin{:});
0039 
0040 if isnumeric(faces)
0041     % compute vector of first edges
0042     v1 = vertices(faces(:,2),1:3) - vertices(faces(:,1),1:3);
0043     v2 = vertices(faces(:,3),1:3) - vertices(faces(:,1),1:3);
0044     
0045     % compute normals using cross product (nodes have same size)
0046     normals = cross(v1, v2, 2);
0047 
0048 else
0049     % initialize empty array
0050     normals = zeros(length(faces), 3);
0051     
0052     for i = 1:length(faces)
0053         face = faces{i};
0054         % compute vector of first edges
0055         v1 = vertices(face(2),1:3) - vertices(face(1),1:3);
0056         v2 = vertices(face(3),1:3) - vertices(face(1),1:3);
0057 
0058         % compute normals using cross product
0059         normals(i, :) = cross(v1, v2, 2);
0060     end
0061 end
0062 
0063 normals = normalizeVector3d(normals);

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019