X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fgeometry-1.5.0%2Fshape2d%2Fshapearea.m;fp=octave_packages%2Fgeometry-1.5.0%2Fshape2d%2Fshapearea.m;h=e6b745db07f0ba0949389ca66f37f4c565ded51b;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/geometry-1.5.0/shape2d/shapearea.m b/octave_packages/geometry-1.5.0/shape2d/shapearea.m new file mode 100644 index 0000000..e6b745d --- /dev/null +++ b/octave_packages/geometry-1.5.0/shape2d/shapearea.m @@ -0,0 +1,77 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal +%% +%% This program is free software; you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published by +%% the Free Software Foundation; either version 3 of the License, or +%% (at your option) any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program; if not, see . + +%% -*- texinfo -*- +%% @deftypefn {Function File} { @var{a} =} shapearea (@var{pp}) +%% Calculate the area of a 2D shape defined with piecewise smooth polynomials. +%% +%% Shape is defined with piecewise smooth polynomials. @var{pp} is a +%% cell where each elements is a 2-by-(poly_degree+1) array containing a pair of +%% polynomials. +%% +%% @code{px(i,:) = pp@{i@}(1,:)} and @code{py(i,:) = pp@{i@}(2,:)}. +%% +%% @seealso{shapecentroid, shape2polygon, shapeplot} +%% @end deftypefn + +function [A ccw] = shapearea (shape) + + A = sum(cellfun (@Aint, shape)); + if A < 0 + warning ('geom2d:shapearea:InvalidResult', ... + 'Shape has negative area. Assuming this is due to a clockwise parametrization of the boundary'); + A = -A; + end + +endfunction + +function dA = Aint (x) + + px = x(1,:); + py = x(2,:); + + P = polyint (conv (px, polyder(py))); + + dA = diff(polyval(P,[0 1])); + +end + +%!demo % non-convex piece-wise polynomial shape +%! boomerang = {[ 0 -2 1; ... +%! -4 4 0]; ... +%! [0.25 -1; ... +%! 0 0]; ... +%! [ 0 1.5 -0.75; ... +%! -3 3 0]; +%! [0.25 0.75; ... +%! 0 0]}; +%! A = shapearea (boomerang) + +%!test +%! triangle = {[1 0; 0 0]; [-0.5 1; 1 0]; [-0.5 0.5; -1 1]}; +%! A = shapearea (triangle); +%! assert (0.5, A); + +%!test +%! circle = {[1.715729 -6.715729 0 5; ... +%! -1.715729 -1.568542 8.284271 0]; ... +%! [1.715729 1.568542 -8.284271 0; ... +%! 1.715729 -6.715729 0 5]; ... +%! [-1.715729 6.715729 0 -5; ... +%! 1.715729 1.568542 -8.284271 0]; ... +%! [-1.715729 -1.568542 8.284271 0; ... +%! -1.715729 6.715729 0 -5]}; +%! A = shapearea (circle); +%! assert (pi*5^2, A, 5e-2);