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
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@inrae.fr 0026 % Created: 2006-07-05 0027 % Copyright 2006-2024 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas) 0028 0029 % parse input data 0030 [vertices, faces] = parseMeshData(varargin{:}); 0031 0032 if isnumeric(faces) 0033 % trimesh or quadmesh 0034 nf = size(faces, 1); 0035 centroids = zeros(nf, size(vertices, 2)); 0036 if size(vertices, 2) == 2 0037 % planar case 0038 for f = 1:nf 0039 centroids(f,:) = polygonCentroid(vertices(faces(f,:), :)); 0040 end 0041 else 0042 % 3D case 0043 if size(faces, 2) == 3 0044 % For triangular meshes, uses accelerated method 0045 % (taken from https://github.com/alecjacobson/gptoolbox) 0046 for ff = 1:3 0047 centroids = centroids + 1/3 * vertices(faces(:,ff),:); 0048 end 0049 else 0050 % for quad (or larger) meshes, use slower but more precise method 0051 for f = 1:nf 0052 centroids(f,:) = polygonCentroid3d(vertices(faces(f,:), :)); 0053 end 0054 end 0055 end 0056 else 0057 % mesh with faces stored as cell array 0058 nf = length(faces); 0059 centroids = zeros(nf, size(vertices, 2)); 0060 if size(vertices, 2) == 2 0061 % planar case 0062 for f = 1:nf 0063 centroids(f,:) = polygonCentroid(vertices(faces{f}, :)); 0064 end 0065 else 0066 % 3D case 0067 for f = 1:nf 0068 centroids(f,:) = polygonCentroid3d(vertices(faces{f}, :)); 0069 end 0070 end 0071 end 0072