X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=octave_packages%2Fgeometry-1.5.0%2Fio%2F%40svg%2Fnormalize.m;fp=octave_packages%2Fgeometry-1.5.0%2Fio%2F%40svg%2Fnormalize.m;h=61de2036c1182029f4f2ed812c4a3b923ddb2984;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hp=0000000000000000000000000000000000000000;hpb=1705066eceaaea976f010f669ce8e972f3734b05;p=CreaPhase.git diff --git a/octave_packages/geometry-1.5.0/io/@svg/normalize.m b/octave_packages/geometry-1.5.0/io/@svg/normalize.m new file mode 100644 index 0000000..61de203 --- /dev/null +++ b/octave_packages/geometry-1.5.0/io/@svg/normalize.m @@ -0,0 +1,95 @@ +%% Copyright (c) 2011 Juan Pablo Carbajal +%% +%% 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 . + +%% -*- texinfo -*- +%% @deftypefn {Function File} @var{SVGn} = normalize (@var{SVG}) +%% normalizes and SVG. +%% @end deftypefn + +function [SVGn bb] = normalize (obj) + + SVGn = obj; + bb = []; + if ! obj.Data.normalized + + ids = fieldnames (obj.Path); + npath = numel(ids); + v = zeros(npath,2); + bb = zeros(1,4); + + for ip = 1:npath + v(ip,:) = shapecentroid(obj.Path.(ids{ip}).data); + p = shape2polygon(obj.Path.(ids{ip}).data); + bb = mergeBoxes(bb, [min(p) max(p)]([1 3 2 4])); + end + + if npath > 1 + v = mean(v)(:); + else + v = v.'; + end + + %% check whether document and bounding box agree. + bbHeight = bb(2)-bb(1); + bbWidth = bb(4)-bb(2); + + if obj.Data.height != bbHeight + warning("svg:normalize:Sanitycheck",... + ["Height of SVG %g and height boundingbox %g don't match.\n" ... + "Using bounding box.\n"],obj.Data.height,bbHeight) + end + + if obj.Data.width != bbWidth + warning("svg:normalize:Sanitycheck",... + ["Width of SVG %g and width boundingbox %g don't match.\n" ... + "Using bounding box.\n"],obj.Data.width,bbWidth) + end + + %% Move paths such that center of SVG is at 0,0 + %% Put coordinates in the usual frame + %% Scale such that diagonal of bounding box is 1 + Dnorm = sqrt (bbWidth ^ 2 + bbHeight ^ 2); + S = (1 / Dnorm) * eye (2); + bb = zeros(1,4); + + for ip = 1:npath + SVGn.Path.(ids{ip}).data = shapetransform(obj.Path.(ids{ip}).data,-v); + + % Put to middle + SVGn.Path.(ids{ip}).data = ... + shapetransform(SVGn.Path.(ids{ip}).data,[0; -bbHeight/2]); + % Reflect y + SVGn.Path.(ids{ip}).data = ... + shapetransform(SVGn.Path.(ids{ip}).data,[1 0;0 -1]); + % Put to bottom + SVGn.Path.(ids{ip}).data = ... + shapetransform(SVGn.Path.(ids{ip}).data,[0; bbHeight/2]); + + % Scale + SVGn.Path.(ids{ip}).data = ... + shapetransform(SVGn.Path.(ids{ip}).data,S); + + p = shape2polygon(SVGn.Path.(ids{ip}).data); + bb = mergeBoxes(bb, [min(p) max(p)]([1 3 2 4])); + end + bbHeight = bb(2)-bb(1); + bbWidth = bb(4)-bb(2); + + SVGn.Data.height = bbHeight; + SVGn.Data.width = bbWidth; + SVGn.Data.normalized = true; + end + +end