]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/shape2d/shape2polygon.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / shape2d / shape2polygon.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{polygon} = } shape2polygon (@var{shape})
18 %% @deftypefnx {Function File} {@var{polygon} = } shape2polygon (@dots{},@var{property},@var{value},@dots{})
19 %% Transforms a 2D shape described by piecewise smooth polynomials into a polygon.
20 %%
21 %% @var{shape} is a n-by-1 cell where each element is a pair of polynomials
22 %% compatible with polyval.
23 %% @var{polygon} is a k-by-2 matrix, where each row represents a vertex.
24 %% The property-value pairs are passed to @code{curve2polyline}.
25 %%
26 %% @seealso{polygon2shape, curve2poyline}
27 %% @end deftypefn
28
29 function polygon = shape2polygon (shape, varargin)
30
31   polygon = cell2mat ( ...
32              cellfun (@(x) curve2polyline(x,varargin{:}), shape,'UniformOutput',false) );
33   polygon = simplifypolygon(polygon);
34
35   if size (polygon, 1) == 1
36     polygon(2,1) = polyval (shape{1}(1,:), 1);
37     polygon(2,2) = polyval (shape{1}(2,:), 1);
38   end
39
40 endfunction
41
42 %!demo
43 %! shape = {[-93.172   606.368  -476.054   291.429; ...
44 %!           -431.196   637.253    11.085   163.791]; ...
45 %!          [-75.3626  -253.2337   457.1678   328.5714; ...
46 %!            438.7659  -653.6278    -7.9953   380.9336]; ...
47 %!          [-89.5841   344.9716  -275.3876   457.1429; ...
48 %!           -170.3613   237.8858     1.0469   158.0765];...
49 %!          [32.900  -298.704   145.804   437.143; ...
50 %!          -243.903   369.597   -34.265   226.648]; ...
51 %!          [-99.081   409.127  -352.903   317.143; ...
52 %!            55.289  -114.223   -26.781   318.076]; ...
53 %!          [-342.231   191.266   168.108   274.286; ...
54 %!            58.870   -38.083   -89.358   232.362]};
55 %!
56 %! % Estimate a good tolerance
57 %! n  = cell2mat(cellfun(@(x)curveval(x,rand(1,10)), shape, 'uniformoutput',false));
58 %! dr = (max(n(:,1))-min(n(:,1)))*(max(n(:,2))-min(n(:,2)))*40;
59 %! p  = shape2polygon (shape,'tol',dr);
60 %!
61 %! figure(1)
62 %! shapeplot(shape,'-b');
63 %! hold on;
64 %! drawPolygon (p,'-or');
65 %! hold off