Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[79637f]: deprecated / getSVGPaths_py.m Maximize Restore History

Download this file

getSVGPaths_py.m    117 lines (99 with data), 3.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
## Copyright (C) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
##
## 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 <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{} =} getSVGPaths_py (@var{}, @var{})
##
## @end deftypefn
function Paths = getSVGPaths_py (svg, varargin)
## Call python script
if exist (svg,'file')
# read from file
[st str]=system (sprintf ('python parsePath.py %s', svg));
else
# inline SVG
[st str]=system (sprintf ('python parsePath.py < %s', svg));
end
## Parse ouput
strpath = strsplit (str(1:end-1), '$', true);
npaths = numel (strpath);
## Convert path data to polygons
for ip = 1:npaths
eval (strpath{ip});
## FIXME: intialize struct with cell field
svgpath2.cmd = svgpath(1).cmd;
svgpath2.data = {svgpath.data};
nD = length(svgpath2.cmd);
pathdata = cell (nD-1,1);
point_end=[];
## If the path is closed, last command is Z and we set initial point == final
if svgpath2.cmd(end) == 'Z'
nD -= 1;
point_end = svgpath2.data{1};
end
## Initial point
points(1,:) = svgpath2.data{1};
for jp = 2:nD
switch svgpath2.cmd(jp)
case 'L'
## Straigth segment to polygon
points(2,:) = svgpath2.data{jp};
pp = [(points(2,:)-points(1,:))' points(1,:)'];
clear points
points(1,:) = [polyval(pp(1,:),1) polyval(pp(2,:),1)];
case 'C'
## Cubic bezier to polygon
points(2:4,:) = reshape (svgpath2.data{jp}, 2, 3).';
pp = cbezier2poly (points);
clear points
points(1,:) = [polyval(pp(1,:),1) polyval(pp(2,:),1)];
end
pathdata{jp-1} = pp;
end
if ~isempty(point_end)
## Straight segmet to close the path
points(2,:) = point_end;
pp = [(points(2,:)-points(1,:))' points(1,:)'];
pathdata{end} = pp;
end
Paths.(svgpathid).data = pathdata;
end
endfunction
%!test
%! figure(1)
%! hold on
%! paths = getSVGPaths_py ('../drawing.svg');
%!
%! # Get path ids
%! ids = fieldnames(paths);
%! npath = numel(ids);
%!
%! t = linspace (0, 1, 64);
%!
%! for i = 1:npath
%! x = []; y = [];
%! data = paths.(ids(i)).data;
%!
%! for j = 1:numel(data)
%! x = cat (2, x, polyval (data{j}(1,:),t));
%! y = cat (2, y, polyval (data{j}(2,:),t));
%! end
%!
%! plot(x,y,'-');
%! end
%! axis ij
%! if strcmpi(input('You should see drawing.svg [y/n] ','s'),'n')
%! error ("didn't get what was expected.");
%! end
%! close