POLYHEDRONCENTROID Compute the centroid of a 3D convex polyhedron. CENTRO = polyhedronCentroid(V, F) Computes the centroid (center of mass) of the polyhedron defined by vertices V and faces F. The polyhedron is assumed to be convex. Example % Creates a polyhedron centered on origin, and add an arbitrary % translation [v, f] = createDodecahedron; v2 = bsxfun(@plus, v, [3 4 5]); % computes the centroid, that should equal the translation vector centroid = polyhedronCentroid(v2, f) centroid = 3.0000 4.0000 5.0000 See also meshes3d, meshVolume, meshSurfaceArea, polyhedronMeanBreadth
0001 function centroid = polyhedronCentroid(vertices, faces) %#ok<INUSD> 0002 %POLYHEDRONCENTROID Compute the centroid of a 3D convex polyhedron. 0003 % 0004 % CENTRO = polyhedronCentroid(V, F) 0005 % Computes the centroid (center of mass) of the polyhedron defined by 0006 % vertices V and faces F. 0007 % The polyhedron is assumed to be convex. 0008 % 0009 % Example 0010 % % Creates a polyhedron centered on origin, and add an arbitrary 0011 % % translation 0012 % [v, f] = createDodecahedron; 0013 % v2 = bsxfun(@plus, v, [3 4 5]); 0014 % % computes the centroid, that should equal the translation vector 0015 % centroid = polyhedronCentroid(v2, f) 0016 % centroid = 0017 % 3.0000 4.0000 5.0000 0018 % 0019 % 0020 % See also 0021 % meshes3d, meshVolume, meshSurfaceArea, polyhedronMeanBreadth 0022 % 0023 0024 % ------ 0025 % Author: David Legland 0026 % e-mail: david.legland@nantes.inra.fr 0027 % Created: 2012-04-05, using Matlab 7.9.0.529 (R2009b) 0028 % Copyright 2012 INRA - Cepia Software Platform. 0029 0030 % 2015.11.13 use delaunayTriangulation instead of delaunayn (strange bug 0031 % with icosahedron...) 0032 0033 % compute set of elementary tetrahedra 0034 DT = delaunayTriangulation(vertices); 0035 T = DT.ConnectivityList; 0036 0037 % number of tetrahedra 0038 nT = size(T, 1); 0039 0040 % initialize result 0041 centroid = zeros(1, 3); 0042 vt = 0; 0043 0044 % Compute the centroid and the volume of each tetrahedron 0045 for i = 1:nT 0046 % coordinates of tetrahedron vertices 0047 tetra = vertices(T(i, :), :); 0048 0049 % centroid is the average of vertices. 0050 centi = mean(tetra); 0051 0052 % compute volume of tetrahedron 0053 vol = det(tetra(1:3,:) - tetra([4 4 4],:)) / 6; 0054 0055 % add weighted centroid of current tetraedron 0056 centroid = centroid + centi * vol; 0057 0058 % compute the sum of tetraedra volumes 0059 vt = vt + vol; 0060 end 0061 0062 % compute by sum of tetrahedron volumes 0063 centroid = centroid / vt;