]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/polygons2d/polygon2shape.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / polygons2d / polygon2shape.m
1 ## Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
15
16 %% -*- texinfo -*-
17 %% @deftypefn {Function File} {@var{shape} = } polygon2shape (@var{polygon})
18 %% Converts a polygon to a shape with edges defined by smooth polynomials.
19 %%
20 %% @var{polygon} is a N-by-2 matrix, each row representing a vertex.
21 %% @var{shape} is a N-by-1 cell, where each element is a pair of polynomials
22 %% compatible with polyval.
23 %%
24 %% In its current state, the shape is formed by polynomials of degree 1. Therefore
25 %% the shape representation costs more memory except for colinear points in the
26 %% polygon.
27 %%
28 %% @seealso{shape2polygon, simplifypolygon, polyval}
29 %% @end deftypefn
30
31 function shape = polygon2shape (polygon)
32
33   # Filter colinear points
34   polygon = simplifypolygon (polygon);
35
36   np = size(polygon,1);
37   # polygonal shapes are memory inefficient!!
38   # TODO filter the regions where edge angles are canging slowly and fit
39   # polynomial of degree 3;
40   pp = nan (2*np,2);
41
42   # Transform edges into polynomials of degree 1;
43   # pp = [(p1-p0) p0];
44   pp(:,1) = diff(polygon([1:end 1],:)).'(:);
45   pp(:,2) = polygon.'(:);
46
47   shape = mat2cell(pp, 2*ones (1,np), 2);
48
49 endfunction
50
51 %!test
52 %! pp = [0 0; 1 0; 1 1; 0 1];
53 %! s = polygon2shape (pp);
54