X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=octave_packages%2Fgeometry-1.5.0%2Fpolygons2d%2Fpolygon2shape.m;fp=octave_packages%2Fgeometry-1.5.0%2Fpolygons2d%2Fpolygon2shape.m;h=c9338b3628df375f6bd3b81c44dede23172eb9e2;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hp=0000000000000000000000000000000000000000;hpb=1705066eceaaea976f010f669ce8e972f3734b05;p=CreaPhase.git diff --git a/octave_packages/geometry-1.5.0/polygons2d/polygon2shape.m b/octave_packages/geometry-1.5.0/polygons2d/polygon2shape.m new file mode 100644 index 0000000..c9338b3 --- /dev/null +++ b/octave_packages/geometry-1.5.0/polygons2d/polygon2shape.m @@ -0,0 +1,54 @@ +## 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{shape} = } polygon2shape (@var{polygon}) +%% Converts a polygon to a shape with edges defined by smooth polynomials. +%% +%% @var{polygon} is a N-by-2 matrix, each row representing a vertex. +%% @var{shape} is a N-by-1 cell, where each element is a pair of polynomials +%% compatible with polyval. +%% +%% In its current state, the shape is formed by polynomials of degree 1. Therefore +%% the shape representation costs more memory except for colinear points in the +%% polygon. +%% +%% @seealso{shape2polygon, simplifypolygon, polyval} +%% @end deftypefn + +function shape = polygon2shape (polygon) + + # Filter colinear points + polygon = simplifypolygon (polygon); + + np = size(polygon,1); + # polygonal shapes are memory inefficient!! + # TODO filter the regions where edge angles are canging slowly and fit + # polynomial of degree 3; + pp = nan (2*np,2); + + # Transform edges into polynomials of degree 1; + # pp = [(p1-p0) p0]; + pp(:,1) = diff(polygon([1:end 1],:)).'(:); + pp(:,2) = polygon.'(:); + + shape = mat2cell(pp, 2*ones (1,np), 2); + +endfunction + +%!test +%! pp = [0 0; 1 0; 1 1; 0 1]; +%! s = polygon2shape (pp); +