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@inrae.fr
0031 % Created: 2006-07-05
0032 % Copyright 2006-2024 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas)
0033 
0034 % parse input data
0035 [vertices, faces] = parseMeshData(varargin{:});
0036 
0037 if isnumeric(faces)
0038     % compute vector of first edges
0039     v1 = vertices(faces(:,2),1:3) - vertices(faces(:,1),1:3);
0040     v2 = vertices(faces(:,3),1:3) - vertices(faces(:,1),1:3);
0041     
0042     % compute normals using cross product (nodes have same size)
0043     normals = cross(v1, v2, 2);
0044 
0045 else
0046     % initialize empty array
0047     normals = zeros(length(faces), 3);
0048     
0049     for i = 1:length(faces)
0050         face = faces{i};
0051         % compute vector of first edges
0052         v1 = vertices(face(2),1:3) - vertices(face(1),1:3);
0053         v2 = vertices(face(3),1:3) - vertices(face(1),1:3);
0054 
0055         % compute normals using cross product
0056         normals(i, :) = cross(v1, v2, 2);
0057     end
0058 end
0059 
0060 normals = normalizeVector3d(normals);

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022