]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/io/deprecated/svgnormalize.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / io / deprecated / svgnormalize.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{SVGn} = loadSVG (@var{SVG})
18 %% Scales and reflects the @var{SVG} structure and returns a modified @var{SVGn}
19 %% structure.
20 %% 
21 %% The height and width of the SVG are scaled such that the diagonal of the 
22 %% bounding box has length 1. Coordinates are trasnfomed such that a plot of the
23 %% paths coincides with the visualization of the original SVG.
24 %%
25 %% @seealso{svgload, svgpath2polygon}
26 %% @end deftypefn
27
28 function SVGn = svgnormalize (SVG)
29     
30   SVGn = SVG;
31   if ~SVG.normalized
32     % Translate
33     TransV = [0, SVG.height/2];
34     % Scale
35     Dnorm = sqrt (SVG.width ^ 2 + SVG.height ^ 2);
36     S = (1 / Dnorm) * eye (2);
37     for i = 1:numel (SVG.path)
38         nc = size (SVG.path(i).coord, 1);
39         % Translate to middle
40         T = repmat (TransV,nc, 1);
41         SVGn.path(i).coord = SVG.path(i).coord - T;
42         % Reflect 
43         SVGn.path(i).coord(:, 2) = -SVGn.path(i).coord(:,2);
44         T(:,2) = -T(:,2);
45         % Translate to bottom
46         SVGn.path(i).coord = SVGn.path(i).coord - T;
47         %Scale
48         SVGn.path(i).coord = SVGn.path(i).coord * S;
49     end
50     SVGn.height = SVG.height / Dnorm;
51     SVGn.width = SVG.width / Dnorm;
52     SVGn.normalized = true;
53   end
54
55 end
56
57 %!demo
58 %! file    = 'tmp__.svg';
59 %! fid     = fopen (file,'w');
60 %! 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>';
61 %! fprintf (fid,"%s\n",svgfile);
62 %! fclose (fid);
63 %! SVG     = svgload (file);
64 %! SVGn    = svgnormalize (SVG);
65 %! SVGn
66 %! plot([SVGn.path.coord(:,1); SVGn.path.coord(1,1)], ...
67 %!      [SVGn.path.coord(:,2); SVGn.path.coord(1,2)]);
68 %! delete (file);