Home > matGeom > meshes3d > meshFaceCentroids.m

meshFaceCentroids

PURPOSE ^

MESHFACECENTROIDS Compute centroids of faces in a mesh.

SYNOPSIS ^

function centroids = meshFaceCentroids(varargin)

DESCRIPTION ^

MESHFACECENTROIDS Compute centroids of faces in a mesh.

   CENTROIDS = meshFaceCentroids(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 centroid of each face, and returns a Nf-by-3 array
   containing their coordinates.

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


   See also:
     meshes3d, drawMesh, meshFaceNormals, meshFaceAreas, convhull

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function centroids = meshFaceCentroids(varargin)
0002 %MESHFACECENTROIDS Compute centroids of faces in a mesh.
0003 %
0004 %   CENTROIDS = meshFaceCentroids(VERTICES, FACES)
0005 %   VERTICES is a set of 3D points  (as a N-by-3 array), and FACES is
0006 %   either a N-by-3 index array or a cell array of indices. The function
0007 %   computes the centroid of each face, and returns a Nf-by-3 array
0008 %   containing their coordinates.
0009 %
0010 %   Example
0011 %     [v, e, f] = createIcosahedron;
0012 %     normals1 = meshFaceNormals(v, f);
0013 %     centros1 = meshFaceCentroids(v, f);
0014 %     figure; hold on; axis equal; view(3);
0015 %     drawMesh(v, f);
0016 %     drawVector3d(centros1, normals1);
0017 %
0018 %
0019 %   See also:
0020 %     meshes3d, drawMesh, meshFaceNormals, meshFaceAreas, convhull
0021 %
0022 
0023 % ------
0024 % Author: David Legland
0025 % e-mail: david.legland@inra.fr
0026 % Created: 2006-07-05
0027 % Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).
0028 
0029 % HISTORY
0030 % 2007-09-18 fix: worked only for 2D case, now works also for 3D
0031 % 2017-11-24 rename from faceCentroids to meshFaceCentroids
0032 
0033 % parse input data
0034 [vertices, faces] = parseMeshData(varargin{:});
0035 
0036 if isnumeric(faces)
0037     % trimesh or quadmesh
0038     nf = size(faces, 1);
0039     centroids = zeros(nf, size(vertices, 2));
0040     if size(vertices, 2) == 2
0041         % planar case
0042         for f = 1:nf
0043             centroids(f,:) = polygonCentroid(vertices(faces(f,:), :));
0044         end
0045     else
0046         % 3D case
0047         if size(faces, 2) == 3
0048             % For triangular meshes, uses accelerated method
0049             % (taken from https://github.com/alecjacobson/gptoolbox)
0050             for ff = 1:3
0051                 centroids = centroids + 1/3 * vertices(faces(:,ff),:);
0052             end
0053         else
0054             % for quad (or larger) meshes, use slower but more precise method
0055             for f = 1:nf
0056                 centroids(f,:) = polygonCentroid3d(vertices(faces(f,:), :));
0057             end
0058         end
0059     end        
0060 else
0061     % mesh with faces stored as cell array
0062     nf = length(faces);
0063     centroids = zeros(nf, size(vertices, 2));
0064     if size(vertices, 2) == 2
0065         % planar case
0066         for f = 1:nf
0067             centroids(f,:) = polygonCentroid(vertices(faces{f}, :));
0068         end
0069     else
0070         % 3D case
0071         for f = 1:nf
0072             centroids(f,:) = polygonCentroid3d(vertices(faces{f}, :));
0073         end
0074     end
0075 end
0076

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