]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/io/@svg/loadpaths.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / io / @svg / loadpaths.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 function Paths = loadpaths (obj, svg, varargin)
17
18   here   = which ("@svg/loadpaths");
19   here   = fileparts (here);
20   script = fullfile (here, 'parsePath.py');
21
22   %% Call python script
23   if exist (svg,'file')
24   % read from file  
25     [st str]=system (sprintf ('python %s %s', script, svg));
26     
27   else
28   % inline SVG  
29     [st str]=system (sprintf ('python %s < %s', script, svg));
30   end
31   
32   %% Parse ouput
33   strpath = strsplit (str(1:end-1), '$', true);
34
35   npaths = numel (strpath);
36
37   %% Convert path data to polynoms
38   for ip = 1:npaths
39
40     eval (strpath{ip});
41     %% FIXME: intialize struct with cell field
42     svgpath2.cmd = svgpath(1).cmd;
43     svgpath2.data = {svgpath.data};
44     
45     nD = length(svgpath2.cmd);
46     pathdata = cell (nD-1,1);
47     
48     point_end=[];
49     %% If the path is closed, last command is Z and we set initial point == final
50     if svgpath2.cmd(end) == 'Z'
51       nD -= 1;
52       point_end = svgpath2.data{1};
53       svgpath2.data(end) = [];
54     end
55     
56     %% Initial point
57     points(1,:) = svgpath2.data{1};
58     
59     for jp = 2:nD
60       switch svgpath2.cmd(jp)
61         case 'L'
62           %% Straigth segment to polygon
63           points(2,:) = svgpath2.data{jp};
64           pp = [(points(2,:)-points(1,:))' points(1,:)'];
65           clear points
66           points(1,:) = [polyval(pp(1,:),1) polyval(pp(2,:),1)];
67           
68         case 'C'
69           %% Cubic bezier to polygon
70           points(2:4,:) = reshape (svgpath2.data{jp}, 2, 3).';
71           pp = cbezier2poly (points);
72           clear points
73           points(1,:) = [polyval(pp(1,:),1) polyval(pp(2,:),1)];
74       end
75       
76       pathdata{jp-1} = pp;
77     end
78     
79     if ~isempty(point_end)
80       %% Straight segment to close the path
81       points(2,:) = point_end;
82       pp = [(points(2,:)-points(1,:))' points(1,:)'];
83       
84       if all ( abs(pp(:,1)) < sqrt(eps) )
85       % Final point of last segment is already initial point
86         pathdata(end) = [];
87       else
88         pathdata{end} = pp;
89       end
90       
91     end
92     %% TODO
93     % pathdata = shapetransform(pathdata);
94     
95     Paths.(svgpathid).data = pathdata;
96   end
97 endfunction