]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/shape2d/shapetransform.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / shape2d / shapetransform.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{nshape} = } shapetransform (@var{shape}, @var{T})
18 %% Applies transformation to a shape defined by piecewise smooth polynomials.
19 %%
20 %% @var{shape} is a cell where each elements is a 2-by-(poly_degree+1) matrix
21 %% containing a pair of polynomials.
22 %%
23 %% Format of @var{T} can be one of :
24 %% @example
25 %% @group
26 %%  [c] , [a b] , [a b c] or [a b c]
27 %%  [f]   [d e]   [d e f]    [d e f]
28 %%                           [0 0 1]
29 %% @end group
30 %% @end example
31 %%
32 %% @seealso{shape2polygon, shapeplot}
33 %% @end deftypefn
34
35 function nshape = shapetransform (shape, Trans)
36
37   if size(Trans,1) < 2
38     error("geometry:shapetransform:InvalidArgument", ...
39                        "Transformation can be 2x1, 2x2, 2x3 or 3x3. See help.");
40   end
41
42   if ~iscell(shape)
43     error("geometry:shapetransform:InvalidArgument", "Shape must be a cell of 2D polynomials.");
44   end
45
46   A =[];
47   v = [];
48
49   switch size(Trans,2)
50     case 1
51     % Just translation
52       v = Trans;
53
54     case 2
55     % Just linear transformation
56       A = Trans;
57
58     case 3
59     % Affine transform
60       A = Trans(1:2,1:2);
61       v = Trans(1:2,3);
62   end
63
64   nshape = cellfun (@(x)polytransform (x,A,v), shape, 'UniformOutput',false);
65
66 endfunction
67
68 function np = polytransform(p,A,v)
69
70   np = p;
71   if ~isempty (A)
72     np = A*np;
73   end
74   if ~isempty (v)
75     np(:,end) = np(:,end) + v;
76   end
77
78 endfunction
79
80 %!demo
81 %! shape = {[-93.172   606.368  -476.054   291.429; ...
82 %!          -431.196   637.253    11.085   163.791]; ...
83 %!         [-75.3626  -253.2337   457.1678   328.5714; ...
84 %!           438.7659  -653.6278    -7.9953   380.9336]; ...
85 %!         [-89.5841   344.9716  -275.3876   457.1429; ...
86 %!          -170.3613   237.8858     1.0469   158.0765];...
87 %!         [32.900  -298.704   145.804   437.143; ...
88 %!         -243.903   369.597   -34.265   226.648]; ...
89 %!         [-99.081   409.127  -352.903   317.143; ...
90 %!           55.289  -114.223   -26.781   318.076]; ...
91 %!         [-342.231   191.266   168.108   274.286; ...
92 %!           58.870   -38.083   -89.358   232.362]};
93 %!
94 %! A = shapearea (shape);
95 %! T = eye(2)/sqrt(A);
96 %! shape = shapetransform (shape,T);
97 %! T = shapecentroid (shape)(:);
98 %! shape = shapetransform (shape,-T + [2; 0]);
99 %!
100 %! close
101 %! shapeplot (shape,'-r','linewidth',2);
102 %! hold on
103 %! for i = 1:9
104 %!   T = createRotation (i*pi/5)(1:2,1:2)/exp(0.3*i);
105 %!   shapeplot (shapetransform(shape, T), 'color',rand(1,3),'linewidth',2);
106 %! end
107 %! hold off
108 %! axis tight
109 %! axis square