Home > matGeom > geom3d > polygonArea3d.m

polygonArea3d

PURPOSE ^

POLYGONAREA3D Area of a 3D polygon.

SYNOPSIS ^

function area = polygonArea3d(poly, varargin)

DESCRIPTION ^

POLYGONAREA3D Area of a 3D polygon.

   AREA = polygonArea3d(POLY)
   POLY is given as a N-by-3 array of vertex coordinates. The resulting
   area is positive.
   Works also for polygons given as a cell array of polygons.

   Example
     % area of a simple 3D square 
     poly = [10 30 20;20 30 20;20 40 20;10 40 20];
     polygonArea3d(poly)
     ans =
        100

     % Area of a 3D mesh
     [v f] = createCubeOctahedron;
     polygons = meshFacePolygons(v, f);
     areas = polygonArea3d(polygons);
     sum(areas)
     ans =
         18.9282

   See also
     polygons3d, triangleArea3d, polygonArea, polygonCentroid3d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function area = polygonArea3d(poly, varargin)
0002 %POLYGONAREA3D Area of a 3D polygon.
0003 %
0004 %   AREA = polygonArea3d(POLY)
0005 %   POLY is given as a N-by-3 array of vertex coordinates. The resulting
0006 %   area is positive.
0007 %   Works also for polygons given as a cell array of polygons.
0008 %
0009 %   Example
0010 %     % area of a simple 3D square
0011 %     poly = [10 30 20;20 30 20;20 40 20;10 40 20];
0012 %     polygonArea3d(poly)
0013 %     ans =
0014 %        100
0015 %
0016 %     % Area of a 3D mesh
0017 %     [v f] = createCubeOctahedron;
0018 %     polygons = meshFacePolygons(v, f);
0019 %     areas = polygonArea3d(polygons);
0020 %     sum(areas)
0021 %     ans =
0022 %         18.9282
0023 %
0024 %   See also
0025 %     polygons3d, triangleArea3d, polygonArea, polygonCentroid3d
0026 
0027 % ------
0028 % Author: David Legland
0029 % e-mail: david.legland@inra.fr
0030 % Created: 2012-02-24,    using Matlab 7.9.0.529 (R2009b)
0031 % Copyright 2012 INRA - Cepia Software Platform.
0032 
0033 %   HISTORY
0034 %   2013-08-20 add support for multiple polygons
0035 
0036 % Check multiple polygons
0037 if iscell(poly) || sum(sum(isnan(poly))) > 0
0038     % split the polygons into a cell array
0039     polygons = splitPolygons3d(poly);
0040     nPolys = length(polygons);
0041     
0042     % compute area of each polygon
0043     area = zeros(nPolys, 1);
0044     for  i = 1:nPolys
0045         area(i) = polygonArea3d(polygons{i});
0046     end
0047     
0048     return;
0049 end
0050 
0051 % put the first vertex at origin (reducing computation errors for polygons
0052 % far from origin)
0053 v0 = poly(1, :);
0054 poly = bsxfun(@minus, poly, v0);
0055 
0056 % indices of next vertices
0057 N = size(poly, 1);
0058 iNext = [2:N 1];
0059 
0060 % compute cross-product of each elementary triangle formed by origin and
0061 % two consecutive vertices
0062 cp = cross(poly, poly(iNext,:), 2);
0063 
0064 % choose one of the triangles as reference for the normal direction
0065 vn = vectorNorm3d(cp);
0066 [tmp, ind] = max(vn); %#ok<ASGLU>
0067 cpRef = cp(ind,:);
0068 
0069 % compute the sign of the area of each triangle
0070 % (need to compute the sign explicitely, as the norm of the cross product
0071 % does not keep orientation within supporting plane)
0072 sign_i = sign(dot(cp, repmat(cpRef, N, 1), 2));
0073 
0074 % compute area of each triangle, using sign correction
0075 area_i = vectorNorm3d(cp) .* sign_i;
0076 
0077 % sum up individual triangles area
0078 area = sum(area_i) / 2;

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