Code covered by the BSD License

# geom3d

### David Legland (view profile)

19 Jun 2009 (Updated )

Library to handle 3D geometric primitives: create, intersect, display, and make basic computations

### Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

polyhedronCentroid(vertices, faces)
```function centroid = polyhedronCentroid(vertices, faces) %#ok<INUSD>
%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
%   polyhedronCentroid
%
%   meshes3d, tetrahedronVolume
%
% ------
% Author: David Legland
% e-mail: david.legland@grignon.inra.fr
% Created: 2012-04-05,    using Matlab 7.9.0.529 (R2009b)
% Copyright 2012 INRA - Cepia Software Platform.

% compute set of elementary tetrahedra
T = delaunayn(vertices);

% number of tetrahedra
nT  = size(T, 1);

% initialize result
centroid = zeros(1, 3);

% Compute the centroid and the volume of each tetrahedron
for i = 1:nT
% coordinates of tetrahedron vertices
tetra = vertices(T(i, :), :);

% centroid is the average of vertices.
centi = mean(tetra);

% compute volume of tetrahedron
vol = det(tetra(1:3,:) - tetra([4 4 4],:)) / 6;

% add weighted centroid of current tetraedron
centroid = centroid + centi * vol;

% compute the sum of tetraedra volumes
vt = vt + vol;
end

% compute by sum of tetrahedron volumes
centroid = centroid / vt;
```