From: <jpi...@us...> - 2012-03-22 17:33:35
|
Revision: 10004 http://octave.svn.sourceforge.net/octave/?rev=10004&view=rev Author: jpicarbajal Date: 2012-03-22 17:33:22 +0000 (Thu, 22 Mar 2012) Log Message: ----------- geometry: deprecating svg functions. Adding license to python scripts Modified Paths: -------------- trunk/octave-forge/main/geometry/INDEX trunk/octave-forge/main/geometry/NEWS trunk/octave-forge/main/geometry/inst/io/@svg/parsePath.py trunk/octave-forge/main/geometry/inst/io/@svg/parseSVGData.py Added Paths: ----------- trunk/octave-forge/main/geometry/inst/io/deprecated/ trunk/octave-forge/main/geometry/inst/io/deprecated/private/ trunk/octave-forge/main/geometry/inst/io/deprecated/private/SVGstrPath2SVGpath.m trunk/octave-forge/main/geometry/inst/io/deprecated/private/formatSVGstr.m trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGPaths_py.m trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGdata.m trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGstrPath.m trunk/octave-forge/main/geometry/inst/io/deprecated/svgload.m trunk/octave-forge/main/geometry/inst/io/deprecated/svgnormalize.m trunk/octave-forge/main/geometry/inst/io/deprecated/svgpath2polygon.m trunk/octave-forge/main/geometry/inst/io/private/_parsePath.py Removed Paths: ------------- trunk/octave-forge/main/geometry/inst/io/private/SVGstrPath2SVGpath.m trunk/octave-forge/main/geometry/inst/io/private/formatSVGstr.m trunk/octave-forge/main/geometry/inst/io/private/getSVGPaths_py.m trunk/octave-forge/main/geometry/inst/io/private/getSVGdata.m trunk/octave-forge/main/geometry/inst/io/private/getSVGstrPath.m trunk/octave-forge/main/geometry/inst/io/private/parsePath.py trunk/octave-forge/main/geometry/inst/io/svgload.m trunk/octave-forge/main/geometry/inst/io/svgnormalize.m trunk/octave-forge/main/geometry/inst/io/svgpath2polygon.m Modified: trunk/octave-forge/main/geometry/INDEX =================================================================== --- trunk/octave-forge/main/geometry/INDEX 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/INDEX 2012-03-22 17:33:22 UTC (rev 10004) @@ -144,9 +144,6 @@ drawGraph.m Geometric graphs manipulation Input - svgload - svgnormalize - svgpath2polygon @svg/svg @svg/plot @svg/getpath Modified: trunk/octave-forge/main/geometry/NEWS =================================================================== --- trunk/octave-forge/main/geometry/NEWS 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/NEWS 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,12 +1,15 @@ Summary of important user-visible changes for releases of the geometry package =============================================================================== -geometry-1.4.1 Release Date: 2012-03-18 Release Manager: Juan Pablo Carbajal +geometry-1.4.1 Release Date: 2012-03-22 Release Manager: Juan Pablo Carbajal =============================================================================== * Renamed functions - Contents.m renamed to geom2d_Contents to avoid clashes. +* Deprecated functions + - svgload, svgnormalize, svgpath2polygon: Use the methods in class svg. + * Bug fixes - @svg/path2polygon.m - Fix addpath/rmpath installation warnings Modified: trunk/octave-forge/main/geometry/inst/io/@svg/parsePath.py =================================================================== --- trunk/octave-forge/main/geometry/inst/io/@svg/parsePath.py 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/@svg/parsePath.py 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,5 +1,20 @@ #!/usr/bin/env python +## Copyright (c) 2012 Juan Pablo Carbajal <car...@if...> +## +## 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 +## 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/>. + import inkex, simplepath import sys #import getopt @@ -8,7 +23,7 @@ svg = inkex.Effect () svg.parse (filen) - + paths = svg.document.xpath ('//svg:path', namespaces=inkex.NSS) for path in paths: D = simplepath.parsePath (path.attrib['d']) @@ -17,14 +32,14 @@ for cmd,params in D: cmdlst.append(cmd) parlst.append(params) - + print 'svgpath = struct("cmd","{0}","data",{{{1}}});' \ .format(''.join(cmdlst),str(parlst).replace('[[','[').replace(']]',']')) print 'svgpathid = "{0}"; $'.format(path.attrib['id']) - - + + # ---------------------------- if __name__=="__main__": @@ -34,7 +49,7 @@ except getopt.GetoptError: usage() sys.exit(2) - + doHelp = 0 c = Context() c.doPrint = 1 Modified: trunk/octave-forge/main/geometry/inst/io/@svg/parseSVGData.py =================================================================== --- trunk/octave-forge/main/geometry/inst/io/@svg/parseSVGData.py 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/@svg/parseSVGData.py 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,5 +1,19 @@ #!/usr/bin/env python +## Copyright (c) 2012 Juan Pablo Carbajal <car...@if...> +## +## 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 +## 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/>. import inkex import sys #import getopt @@ -8,7 +22,7 @@ svg = inkex.Effect () svg.parse (filen) - + root = svg.document.xpath ('//svg:svg', namespaces=inkex.NSS) print 'data = struct("height",{0},"width",{1},"id","{2}");' \ .format(root[0].attrib['height'],root[0].attrib['width'], Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/private/SVGstrPath2SVGpath.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/private/SVGstrPath2SVGpath.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/private/SVGstrPath2SVGpath.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/private/SVGstrPath2SVGpath.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,103 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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/>. + +function SVGpath = SVGstrPath2SVGpath (SVGstrPath) + + nPaths = numel (SVGstrPath); + SVGpath = repmat (struct('coord', [], 'closed', [], 'id', []), 1, nPaths); + + for ip = 1:nPaths + path = SVGstrPath{ip}; + + % Match data + [s e te m] = regexpi (path, 'd="(?:(?!").)*'); + data=strtrim (m{1}); + % parse data + d = parsePathData (data); + SVGpath(ip).coord = d.coord; + SVGpath(ip).closed = d.closed; + + % Match id + [s e te m] = regexp (path, 'id="(?:(?!").)*'); + if ~isempty (m) + SVGpath(ip).id = strtrim (m{1}(5:end)); + end + end + +end + +function d = parsePathData (data) + + d = struct ('coord', [], 'closed', []); + + [s e te comm] = regexp (data, '[MmLlHhVvCcSsQqTtAaZz]{1}'); + % TODO + % This info could be used to preallocate d + [zcomm zpos] = ismember ({'Z','z'}, comm); + + if any (zcomm) + d.closed = true; + else + d.closed = false; + s(end+1) = length (data); + end + comm(zpos(zcomm)) = []; + ncomm = size (comm, 2); + for ic = 1:ncomm + + switch comm{ic} + case {'M','L'} + [x y] = strread (data(s(ic) + 1 : s(ic + 1) - 1), ... + '%f%f', 'delimiter', ', '); + coord = [x y]; + + case 'm' + [x y] = strread( data(s(ic) + 1 : s(ic + 1) - 1), ... + '%f%f', 'delimiter', ', '); + nc = size (x, 1); + coord = [x y]; + if ic == 1 + % relative moveto at begining of data. + % First coordinates are absolute, the rest are relative. + coord = cumsum (coord); + else + % Relative moveto. + % The coordinates are relative to the last one loaded + coord(1,:) =coord(1,:) + d.coord(end,:); + coord = cumsum(coord); + warning('svg2octWarning',['Relative moveto in path data.'... + ' May mean that the orginal path was not a simple polygon.' ... + ' If that is the case, the path will not display equally.']) + end + + case 'l' + % Relative lineto, coordinates are relative to last point loaded. + [x y] = strread( data(s(ic) + 1 : s(ic + 1) - 1), ... + '%f%f', 'delimiter', ', '); + nc = size (x, 1); + coord = [x y] + coord(1,:) =coord(1,:) + d.coord(end,:); + coord = cumsum(coord); + + otherwise + warning('svg2oct:Warning',... + 'Path data command "%s" not implemented yet.',comm{ic}); + end + + nc = size(coord,1); + d.coord(end+1:end+nc,:) = coord; + + end +end Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/private/formatSVGstr.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/private/formatSVGstr.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/private/formatSVGstr.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/private/formatSVGstr.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,24 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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/>. + +function svgF = formatSVGstr(svg) + + % Remove all newlines and tabs + svgF = strrep(svg,"\n",' '); + svgF = strrep(svgF,"\t",' '); + % Remove consecutive blanks + svgF = regexprep(svgF,' +',' '); + +end Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGPaths_py.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/private/getSVGPaths_py.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGPaths_py.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGPaths_py.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,113 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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/>. + +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 + Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGdata.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/private/getSVGdata.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGdata.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGdata.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,33 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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/>. + +function svgData = getSVGdata(svg) + + svgData = struct('height',[],'width',[]); + attr = fieldnames(svgData); + nattr = numel(attr); + + [s e te data] = regexpi(svg,'<[ ]*svg(?:(?!>).)*>'); + data=strtrim(data{1}); + + for a = 1:nattr + pattr =sprintf('%s="(?:(?!").)*',attr{a}); + [s e te m] = regexpi(data,pattr); + m=strtrim(m{1}); + [dummy value] = strread(m,'%s%f','delimiter','"'); + svgData.(attr{a}) = value; + end + +end Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGstrPath.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/private/getSVGstrPath.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGstrPath.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/private/getSVGstrPath.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,20 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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/>. + +function strPath = getSVGstrPath(svg) + + [s e te strPath] = regexpi(svg,'<[ ]*path(?:(?!/>).)*/>'); + +end Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/svgload.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/svgload.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/svgload.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/svgload.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,62 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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{SVG} = loadSVG (@var{filename}) +%% Reads the plain SVG file @var{filename} and returns an SVG structure. +%% +%% In the current version only SVG path elements are parsed and stored in the field +%% path of the @var{SVG} structure. +%% The coordinates of the path are not modified in anyway. This produces the path +%% to be ploted vertically reflected. +%% +%% @seealso{svgnormalize, svgpath2polygon} +%% @end deftypefn + + +function SVG = svgload (filename) + + SVG = struct ('height', [], 'width', [], 'path', [], 'normalized', []); + + SVG.normalized = false; + + fid = fopen (filename); + svg = char (fread (fid, "uchar")'); + fclose (fid); + svgF = formatSVGstr (svg); + + % Get SVG Data + data = getSVGdata (svgF); + SVG.height = data.height; + SVG.width = data.width; + + % Get SVG Paths + SVGstrPath = getSVGstrPath (svgF); + SVGpath = SVGstrPath2SVGpath (SVGstrPath); + SVG.path = SVGpath; + +end + +%!demo +%! file = 'tmp__.svg'; +%! fid = fopen (file,'w'); +%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /></svg></body></html>'; +%! fprintf (fid,"%s\n",svgfile); +%! fclose (fid); +%! SVG = svgload (file); +%! SVG +%! plot([SVG.path.coord(:,1); SVG.path.coord(1,1)], ... +%! [SVG.path.coord(:,2); SVG.path.coord(1,2)]); +%! delete (file); Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/svgnormalize.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/svgnormalize.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/svgnormalize.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/svgnormalize.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,68 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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{SVGn} = loadSVG (@var{SVG}) +%% Scales and reflects the @var{SVG} structure and returns a modified @var{SVGn} +%% structure. +%% +%% The height and width of the SVG are scaled such that the diagonal of the +%% bounding box has length 1. Coordinates are trasnfomed such that a plot of the +%% paths coincides with the visualization of the original SVG. +%% +%% @seealso{svgload, svgpath2polygon} +%% @end deftypefn + +function SVGn = svgnormalize (SVG) + + SVGn = SVG; + if ~SVG.normalized + % Translate + TransV = [0, SVG.height/2]; + % Scale + Dnorm = sqrt (SVG.width ^ 2 + SVG.height ^ 2); + S = (1 / Dnorm) * eye (2); + for i = 1:numel (SVG.path) + nc = size (SVG.path(i).coord, 1); + % Translate to middle + T = repmat (TransV,nc, 1); + SVGn.path(i).coord = SVG.path(i).coord - T; + % Reflect + SVGn.path(i).coord(:, 2) = -SVGn.path(i).coord(:,2); + T(:,2) = -T(:,2); + % Translate to bottom + SVGn.path(i).coord = SVGn.path(i).coord - T; + %Scale + SVGn.path(i).coord = SVGn.path(i).coord * S; + end + SVGn.height = SVG.height / Dnorm; + SVGn.width = SVG.width / Dnorm; + SVGn.normalized = true; + end + +end + +%!demo +%! file = 'tmp__.svg'; +%! fid = fopen (file,'w'); +%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /></svg></body></html>'; +%! fprintf (fid,"%s\n",svgfile); +%! fclose (fid); +%! SVG = svgload (file); +%! SVGn = svgnormalize (SVG); +%! SVGn +%! plot([SVGn.path.coord(:,1); SVGn.path.coord(1,1)], ... +%! [SVGn.path.coord(:,2); SVGn.path.coord(1,2)]); +%! delete (file); Copied: trunk/octave-forge/main/geometry/inst/io/deprecated/svgpath2polygon.m (from rev 10003, trunk/octave-forge/main/geometry/inst/io/svgpath2polygon.m) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/deprecated/svgpath2polygon.m (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/deprecated/svgpath2polygon.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,60 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> +%% +%% 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 +%% 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{P} = svgpath2polygon (@var{SVGpath}) +%% Converts the SVG path structure @var{SVGpath} to an array of polygons +%% compatible with the geometry package and matGeom (@url{http://matgeom.sf.net}). +%% +%% @var{SVGpath} is a substructure of the SVG structure output by loadSVG. This +%% function extracts the field named "coord" if there is only one path. If there +%% are more than oe path it puts the "coord" field of each path in the same +%% array, separated by nans. +%% +%% @seealso{svgnormalize, svgload} +%% @end deftypefn + +function P = svgpath2polygon (SVGpath) + + P = SVGpath(1).coord; + for ip = 2:numel (SVGpath) + P = [P; nan(1,2); SVGpath(ip).coord]; + end + +end + +%!demo +%! file = 'tmp__.svg'; +%! fid = fopen (file,'w'); +%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /></svg></body></html>'; +%! fprintf (fid,"%s\n",svgfile); +%! fclose (fid); +%! SVG = svgload (file); +%! SVG = svgnormalize (SVG); +%! P = svgpath2polygon (SVG.path); +%! plot (P(:,1),P(:,2)); +%! delete (file); + +%!demo +%! file = 'tmp__.svg'; +%! fid = fopen (file,'w'); +%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /><path d="M0,0 100,0 100,100 0,100 Z" /></svg></body></html>'; +%! fprintf (fid,"%s\n",svgfile); +%! fclose (fid); +%! SVG = svgload (file); +%! SVG = svgnormalize (SVG); +%! P = svgpath2polygon (SVG.path); +%! plot (P(:,1),P(:,2)); +%! delete (file); Deleted: trunk/octave-forge/main/geometry/inst/io/private/SVGstrPath2SVGpath.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/private/SVGstrPath2SVGpath.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/private/SVGstrPath2SVGpath.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,103 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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/>. - -function SVGpath = SVGstrPath2SVGpath (SVGstrPath) - - nPaths = numel (SVGstrPath); - SVGpath = repmat (struct('coord', [], 'closed', [], 'id', []), 1, nPaths); - - for ip = 1:nPaths - path = SVGstrPath{ip}; - - % Match data - [s e te m] = regexpi (path, 'd="(?:(?!").)*'); - data=strtrim (m{1}); - % parse data - d = parsePathData (data); - SVGpath(ip).coord = d.coord; - SVGpath(ip).closed = d.closed; - - % Match id - [s e te m] = regexp (path, 'id="(?:(?!").)*'); - if ~isempty (m) - SVGpath(ip).id = strtrim (m{1}(5:end)); - end - end - -end - -function d = parsePathData (data) - - d = struct ('coord', [], 'closed', []); - - [s e te comm] = regexp (data, '[MmLlHhVvCcSsQqTtAaZz]{1}'); - % TODO - % This info could be used to preallocate d - [zcomm zpos] = ismember ({'Z','z'}, comm); - - if any (zcomm) - d.closed = true; - else - d.closed = false; - s(end+1) = length (data); - end - comm(zpos(zcomm)) = []; - ncomm = size (comm, 2); - for ic = 1:ncomm - - switch comm{ic} - case {'M','L'} - [x y] = strread (data(s(ic) + 1 : s(ic + 1) - 1), ... - '%f%f', 'delimiter', ', '); - coord = [x y]; - - case 'm' - [x y] = strread( data(s(ic) + 1 : s(ic + 1) - 1), ... - '%f%f', 'delimiter', ', '); - nc = size (x, 1); - coord = [x y]; - if ic == 1 - % relative moveto at begining of data. - % First coordinates are absolute, the rest are relative. - coord = cumsum (coord); - else - % Relative moveto. - % The coordinates are relative to the last one loaded - coord(1,:) =coord(1,:) + d.coord(end,:); - coord = cumsum(coord); - warning('svg2octWarning',['Relative moveto in path data.'... - ' May mean that the orginal path was not a simple polygon.' ... - ' If that is the case, the path will not display equally.']) - end - - case 'l' - % Relative lineto, coordinates are relative to last point loaded. - [x y] = strread( data(s(ic) + 1 : s(ic + 1) - 1), ... - '%f%f', 'delimiter', ', '); - nc = size (x, 1); - coord = [x y] - coord(1,:) =coord(1,:) + d.coord(end,:); - coord = cumsum(coord); - - otherwise - warning('svg2oct:Warning',... - 'Path data command "%s" not implemented yet.',comm{ic}); - end - - nc = size(coord,1); - d.coord(end+1:end+nc,:) = coord; - - end -end Copied: trunk/octave-forge/main/geometry/inst/io/private/_parsePath.py (from rev 10003, trunk/octave-forge/main/geometry/inst/io/private/parsePath.py) =================================================================== --- trunk/octave-forge/main/geometry/inst/io/private/_parsePath.py (rev 0) +++ trunk/octave-forge/main/geometry/inst/io/private/_parsePath.py 2012-03-22 17:33:22 UTC (rev 10004) @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +import inkex, simplepath +import sys +#import getopt + +def parsePaths (filen=None): + + svg = inkex.Effect () + svg.parse (filen) + + paths = svg.document.xpath ('//svg:path', namespaces=inkex.NSS) + for path in paths: + D = simplepath.parsePath (path.attrib['d']) + cmdlst = []; + parlst = []; + for cmd,params in D: + cmdlst.append(cmd) + parlst.append(params) + + print 'svgpath = struct("cmd","{0}","data",{{{1}}});' \ + .format(''.join(cmdlst),str(parlst).replace('[[','[').replace(']]',']')) + + print 'svgpathid = "{0}"; $'.format(path.attrib['id']) + + + +# ---------------------------- + +if __name__=="__main__": + ''' + try: + optlist,args = getopt.getopt(sys.argv[1:],"thdp") + except getopt.GetoptError: + usage() + sys.exit(2) + + doHelp = 0 + c = Context() + c.doPrint = 1 + for opt in optlist: + if opt[0] == "-d": c.debug = 1 + if opt[0] == "-p": c.plot = 1 + if opt[0] == "-t": c.triangulate = 1 + if opt[0] == "-h": doHelp = 1 + + if not doHelp: + pts = [] + fp = sys.stdin + if len(args) > 0: + fp = open(args[0],'r') + for line in fp: + fld = line.split() + x = float(fld[0]) + y = float(fld[1]) + pts.append(Site(x,y)) + if len(args) > 0: fp.close() + + if doHelp or len(pts) == 0: + usage() + sys.exit(2) + ''' + svg = sys.argv[1] + parsePaths(svg) Deleted: trunk/octave-forge/main/geometry/inst/io/private/formatSVGstr.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/private/formatSVGstr.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/private/formatSVGstr.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,24 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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/>. - -function svgF = formatSVGstr(svg) - - % Remove all newlines and tabs - svgF = strrep(svg,"\n",' '); - svgF = strrep(svgF,"\t",' '); - % Remove consecutive blanks - svgF = regexprep(svgF,' +',' '); - -end Deleted: trunk/octave-forge/main/geometry/inst/io/private/getSVGPaths_py.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/private/getSVGPaths_py.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/private/getSVGPaths_py.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,113 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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/>. - -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 - Deleted: trunk/octave-forge/main/geometry/inst/io/private/getSVGdata.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/private/getSVGdata.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/private/getSVGdata.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,33 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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/>. - -function svgData = getSVGdata(svg) - - svgData = struct('height',[],'width',[]); - attr = fieldnames(svgData); - nattr = numel(attr); - - [s e te data] = regexpi(svg,'<[ ]*svg(?:(?!>).)*>'); - data=strtrim(data{1}); - - for a = 1:nattr - pattr =sprintf('%s="(?:(?!").)*',attr{a}); - [s e te m] = regexpi(data,pattr); - m=strtrim(m{1}); - [dummy value] = strread(m,'%s%f','delimiter','"'); - svgData.(attr{a}) = value; - end - -end Deleted: trunk/octave-forge/main/geometry/inst/io/private/getSVGstrPath.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/private/getSVGstrPath.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/private/getSVGstrPath.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,20 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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/>. - -function strPath = getSVGstrPath(svg) - - [s e te strPath] = regexpi(svg,'<[ ]*path(?:(?!/>).)*/>'); - -end Deleted: trunk/octave-forge/main/geometry/inst/io/private/parsePath.py =================================================================== --- trunk/octave-forge/main/geometry/inst/io/private/parsePath.py 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/private/parsePath.py 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,64 +0,0 @@ -#!/usr/bin/env python - -import inkex, simplepath -import sys -#import getopt - -def parsePaths (filen=None): - - svg = inkex.Effect () - svg.parse (filen) - - paths = svg.document.xpath ('//svg:path', namespaces=inkex.NSS) - for path in paths: - D = simplepath.parsePath (path.attrib['d']) - cmdlst = []; - parlst = []; - for cmd,params in D: - cmdlst.append(cmd) - parlst.append(params) - - print 'svgpath = struct("cmd","{0}","data",{{{1}}});' \ - .format(''.join(cmdlst),str(parlst).replace('[[','[').replace(']]',']')) - - print 'svgpathid = "{0}"; $'.format(path.attrib['id']) - - - -# ---------------------------- - -if __name__=="__main__": - ''' - try: - optlist,args = getopt.getopt(sys.argv[1:],"thdp") - except getopt.GetoptError: - usage() - sys.exit(2) - - doHelp = 0 - c = Context() - c.doPrint = 1 - for opt in optlist: - if opt[0] == "-d": c.debug = 1 - if opt[0] == "-p": c.plot = 1 - if opt[0] == "-t": c.triangulate = 1 - if opt[0] == "-h": doHelp = 1 - - if not doHelp: - pts = [] - fp = sys.stdin - if len(args) > 0: - fp = open(args[0],'r') - for line in fp: - fld = line.split() - x = float(fld[0]) - y = float(fld[1]) - pts.append(Site(x,y)) - if len(args) > 0: fp.close() - - if doHelp or len(pts) == 0: - usage() - sys.exit(2) - ''' - svg = sys.argv[1] - parsePaths(svg) Deleted: trunk/octave-forge/main/geometry/inst/io/svgload.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/svgload.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/svgload.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,62 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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{SVG} = loadSVG (@var{filename}) -%% Reads the plain SVG file @var{filename} and returns an SVG structure. -%% -%% In the current version only SVG path elements are parsed and stored in the field -%% path of the @var{SVG} structure. -%% The coordinates of the path are not modified in anyway. This produces the path -%% to be ploted vertically reflected. -%% -%% @seealso{svgnormalize, svgpath2polygon} -%% @end deftypefn - - -function SVG = svgload (filename) - - SVG = struct ('height', [], 'width', [], 'path', [], 'normalized', []); - - SVG.normalized = false; - - fid = fopen (filename); - svg = char (fread (fid, "uchar")'); - fclose (fid); - svgF = formatSVGstr (svg); - - % Get SVG Data - data = getSVGdata (svgF); - SVG.height = data.height; - SVG.width = data.width; - - % Get SVG Paths - SVGstrPath = getSVGstrPath (svgF); - SVGpath = SVGstrPath2SVGpath (SVGstrPath); - SVG.path = SVGpath; - -end - -%!demo -%! file = 'tmp__.svg'; -%! fid = fopen (file,'w'); -%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /></svg></body></html>'; -%! fprintf (fid,"%s\n",svgfile); -%! fclose (fid); -%! SVG = svgload (file); -%! SVG -%! plot([SVG.path.coord(:,1); SVG.path.coord(1,1)], ... -%! [SVG.path.coord(:,2); SVG.path.coord(1,2)]); -%! delete (file); Deleted: trunk/octave-forge/main/geometry/inst/io/svgnormalize.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/svgnormalize.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/svgnormalize.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,68 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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{SVGn} = loadSVG (@var{SVG}) -%% Scales and reflects the @var{SVG} structure and returns a modified @var{SVGn} -%% structure. -%% -%% The height and width of the SVG are scaled such that the diagonal of the -%% bounding box has length 1. Coordinates are trasnfomed such that a plot of the -%% paths coincides with the visualization of the original SVG. -%% -%% @seealso{svgload, svgpath2polygon} -%% @end deftypefn - -function SVGn = svgnormalize (SVG) - - SVGn = SVG; - if ~SVG.normalized - % Translate - TransV = [0, SVG.height/2]; - % Scale - Dnorm = sqrt (SVG.width ^ 2 + SVG.height ^ 2); - S = (1 / Dnorm) * eye (2); - for i = 1:numel (SVG.path) - nc = size (SVG.path(i).coord, 1); - % Translate to middle - T = repmat (TransV,nc, 1); - SVGn.path(i).coord = SVG.path(i).coord - T; - % Reflect - SVGn.path(i).coord(:, 2) = -SVGn.path(i).coord(:,2); - T(:,2) = -T(:,2); - % Translate to bottom - SVGn.path(i).coord = SVGn.path(i).coord - T; - %Scale - SVGn.path(i).coord = SVGn.path(i).coord * S; - end - SVGn.height = SVG.height / Dnorm; - SVGn.width = SVG.width / Dnorm; - SVGn.normalized = true; - end - -end - -%!demo -%! file = 'tmp__.svg'; -%! fid = fopen (file,'w'); -%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /></svg></body></html>'; -%! fprintf (fid,"%s\n",svgfile); -%! fclose (fid); -%! SVG = svgload (file); -%! SVGn = svgnormalize (SVG); -%! SVGn -%! plot([SVGn.path.coord(:,1); SVGn.path.coord(1,1)], ... -%! [SVGn.path.coord(:,2); SVGn.path.coord(1,2)]); -%! delete (file); Deleted: trunk/octave-forge/main/geometry/inst/io/svgpath2polygon.m =================================================================== --- trunk/octave-forge/main/geometry/inst/io/svgpath2polygon.m 2012-03-22 15:08:33 UTC (rev 10003) +++ trunk/octave-forge/main/geometry/inst/io/svgpath2polygon.m 2012-03-22 17:33:22 UTC (rev 10004) @@ -1,60 +0,0 @@ -%% Copyright (c) 2011 Juan Pablo Carbajal <car...@if...> -%% -%% 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 -%% 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{P} = svgpath2polygon (@var{SVGpath}) -%% Converts the SVG path structure @var{SVGpath} to an array of polygons -%% compatible with the geometry package and matGeom (@url{http://matgeom.sf.net}). -%% -%% @var{SVGpath} is a substructure of the SVG structure output by loadSVG. This -%% function extracts the field named "coord" if there is only one path. If there -%% are more than oe path it puts the "coord" field of each path in the same -%% array, separated by nans. -%% -%% @seealso{svgnormalize, svgload} -%% @end deftypefn - -function P = svgpath2polygon (SVGpath) - - P = SVGpath(1).coord; - for ip = 2:numel (SVGpath) - P = [P; nan(1,2); SVGpath(ip).coord]; - end - -end - -%!demo -%! file = 'tmp__.svg'; -%! fid = fopen (file,'w'); -%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /></svg></body></html>'; -%! fprintf (fid,"%s\n",svgfile); -%! fclose (fid); -%! SVG = svgload (file); -%! SVG = svgnormalize (SVG); -%! P = svgpath2polygon (SVG.path); -%! plot (P(:,1),P(:,2)); -%! delete (file); - -%!demo -%! file = 'tmp__.svg'; -%! fid = fopen (file,'w'); -%! svgfile = '<html><body><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="250" width="250"><path d="M150,0 75,200 225,200 Z" /><path d="M0,0 100,0 100,100 0,100 Z" /></svg></body></html>'; -%! fprintf (fid,"%s\n",svgfile); -%! fclose (fid); -%! SVG = svgload (file); -%! SVG = svgnormalize (SVG); -%! P = svgpath2polygon (SVG.path); -%! plot (P(:,1),P(:,2)); -%! delete (file); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |