POLYGONCENTROID3D Centroid (or center of mass) of a polygon. PTC = polygonCentroid3d(POLY) Computes center of mass of a polygon defined by POLY. POLY is a N-by-3 array of double containing coordinates of polygon vertices. The result PTC is given as a 1-by-3 numeric array. The algorithm assumes (1) that the vertices of the polygon are within the same plane and (2) that the planar projection of the polygon (on the embedding plane) do not self-intersect. PTC = polygonCentroid3d(VX, VY, VZ) Specifies vertex coordinates as three separate arrays. Example % compute centroid of a basic polygon poly = [0 0 0; 10 0 10;10 10 20;0 10 10]; centro = polygonCentroid3d(poly) centro = 5.0000 5.0000 10.0000 See also polygons3d, polygonArea3d, polygonCentroid, planePosition, planePoint
0001 function [centroid, area] = polygonCentroid3d(varargin) 0002 %POLYGONCENTROID3D Centroid (or center of mass) of a polygon. 0003 % 0004 % PTC = polygonCentroid3d(POLY) 0005 % Computes center of mass of a polygon defined by POLY. POLY is a N-by-3 0006 % array of double containing coordinates of polygon vertices. The result 0007 % PTC is given as a 1-by-3 numeric array. 0008 % The algorithm assumes (1) that the vertices of the polygon are within 0009 % the same plane and (2) that the planar projection of the polygon (on 0010 % the embedding plane) do not self-intersect. 0011 % 0012 % PTC = polygonCentroid3d(VX, VY, VZ) 0013 % Specifies vertex coordinates as three separate arrays. 0014 % 0015 % Example 0016 % % compute centroid of a basic polygon 0017 % poly = [0 0 0; 10 0 10;10 10 20;0 10 10]; 0018 % centro = polygonCentroid3d(poly) 0019 % centro = 0020 % 5.0000 5.0000 10.0000 0021 % 0022 % See also 0023 % polygons3d, polygonArea3d, polygonCentroid, planePosition, planePoint 0024 % 0025 0026 % ------ 0027 % Author: David Legland 0028 % E-mail: david.legland@inrae.fr 0029 % Created: 2007-09-18 0030 % Copyright 2007-2024 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas) 0031 0032 if nargin == 1 0033 % polygon is given as a single argument 0034 pts = varargin{1}; 0035 elseif nargin == 3 0036 % polygon is given as 3 coordinate arrays 0037 px = varargin{1}; 0038 py = varargin{2}; 0039 pz = varargin{3}; 0040 pts = [px py pz]; 0041 end 0042 0043 pts = parsePolygon(pts, 'repetition'); 0044 0045 % create supporting plane 0046 plane = fitPlane(pts); 0047 0048 % project points onto the plane 0049 pts = planePosition(pts, plane); 0050 0051 % compute centroid in 2D 0052 [centro2d, area] = polygonCentroid(pts); 0053 0054 % project back in 3D 0055 centroid = planePoint(plane, centro2d);