]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/io/@svg/path2polygon.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / io / @svg / path2polygon.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 %%    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{P} = path2polygon (@var{id})
18 %% Converts the SVG path to an array of polygons.
19 %%
20 %% @end deftypefn
21
22 function P = path2polygon (obj,varargin)
23
24     narg = numel(varargin);
25
26     if narg == 1
27
28      id = varargin{1};
29      n = 32;
30
31     elseif narg == 2
32
33      id = varargin{1};
34      n = varargin{2};
35
36     else
37
38       error("svg:path2polygon:InvalidArgument", "Wrong number of arguments.");
39
40     end
41
42     P = shape2polygon(getpath(obj, id));
43
44 endfunction
45
46 %{
47     pd = obj.Path.(id).data;
48     P = cellfun(@(x)convertpath(x,n),pd,'UniformOutput',false);
49     P = cell2mat(P);
50
51 end
52
53 function p = convertpath(x,np)
54   n = size(x,2);
55
56   switch n
57     case 2
58       p = zeros(2,2);
59     % Straight segment
60       p(:,1) = polyval (x(1,:), [0; 1]);
61       p(:,2) = polyval (x(2,:), [0; 1]);
62     case 4
63       p = zeros(np,2);
64     % Cubic bezier
65       t = linspace (0, 1, np).';
66       p(:,1) = polyval (x(1,:),t);
67       p(:,2) = polyval (x(2,:),t);
68   end
69
70 end
71 %}