1## Copyright (C) 2021 David Legland
2## All rights reserved.
3##
4## Redistribution and use in source and binary forms, with or without
5## modification, are permitted provided that the following conditions are met:
6##
7##     1 Redistributions of source code must retain the above copyright notice,
8##       this list of conditions and the following disclaimer.
9##     2 Redistributions in binary form must reproduce the above copyright
10##       notice, this list of conditions and the following disclaimer in the
11##       documentation and/or other materials provided with the distribution.
12##
13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23##
24## The views and conclusions contained in the software and documentation are
25## those of the authors and should not be interpreted as representing official
26## policies, either expressed or implied, of the copyright holders.
27
28function transfo = createBasisTransform(source, target)
29%CREATEBASISTRANSFORM Compute matrix for transforming a basis into another basis.
30%
31%   TRANSFO = createBasisTransform(SOURCE, TARGET)
32%   Both SOURCE and TARGET represent basis, in the following form:
33%   [x0 y0  ex1 ey1  ex2 ey2]
34%   [y0 y0] is the origin of the basis, [ex1 ey1] is the first direction
35%   vector, and [ex2 ey2] is the second direction vector.
36%
37%   The result TRANSFO is a 3-by-3 matrix such that a point expressed with
38%   coordinates of the first basis will be represented by new coordinates
39%   P2 = transformPoint(P1, TRANSFO) in the target basis.
40%
41%   TRANSFO = createBasisTransform(TARGET)
42%   Assumes the source is the standard (Oij) basis, with origin at (0,0),
43%   first direction vector equal to (1,0) and second direction  vector
44%   equal to (0,1).
45%
46%
47%   Example
48%     % define source and target bases
49%     src = [ 0 0   1  0    0  1];
50%     tgt = [20 0  .5 .5  -.5 .5];
51%     trans = createBasisTransform(src, tgt);
52%     % create a polygon in source basis
53%     poly = [10 10;30 10; 30 20; 20 20;20 40; 10 40];
54%     figure;
55%     subplot(121); drawPolygon(poly, 'b'); axis equal; axis([-10 50 -10 50]);
56%     hold on; drawLine([0 0 1 0], 'k'); drawLine([0 0 0 1], 'k');
57%     drawLine([20 0 1 1], 'r'); drawLine([20 0 -1 1], 'r');
58%     t = -1:5; plot(t*5+20, t*5, 'r.'); plot(-t*5+20, t*5, 'r.');
59%     % transform the polygon in target basis
60%     poly2 = transformPoint(poly, trans);
61%     subplot(122); drawPolygon(poly2, 'b'); axis equal; axis([-10 50 -10 50]);
62%     hold on; drawLine([0 0 1 0], 'r'); drawLine([0 0 0 1], 'r');
63%     t = -1:5; plot(t*10, zeros(size(t)), 'r.'); plot(zeros(size(t)), t*10, 'r.');
64%
65%   See also
66%   transforms2d
67%
68
69% ------
70% Author: David Legland
71% e-mail: david.legland@inra.fr
72% Created: 2010-12-03,    using Matlab 7.9.0.529 (R2009b)
73% Copyright 2010 INRA - Cepia Software Platform.
74
75% init basis transform to identity
76t1 = eye(3);
77t2 = eye(3);
78
79if nargin == 2
80    % from source to reference basis
81    t1(1:2, 1) = source(3:4);
82    t1(1:2, 2) = source(5:6);
83    t1(1:2, 3) = source(1:2);
84else
85    % if only one input, use first input as target basis, and leave the
86    % first matrix to identity
87    target = source;
88end
89
90% from reference to target basis
91t2(1:2, 1) = target(3:4);
92t2(1:2, 2) = target(5:6);
93t2(1:2, 3) = target(1:2);
94
95% compute transform matrix
96transfo = zeros(3, 3);
97maxSz = 1;
98for i = 1:maxSz
99    % coordinate of three reference points in source basis
100    po = t1(1:2, 3, i)';
101    px = po + t1(1:2, 1, i)';
102    py = po + t1(1:2, 2, i)';
103
104    % express coordinates of reference points in the new basis
105    t2i = inv(t2(:,:,i));
106    pot = transformPoint(po, t2i);
107    pxt = transformPoint(px, t2i);
108    pyt = transformPoint(py, t2i);
109
110    % compute direction vectors in new basis
111    vx = pxt - pot;
112    vy = pyt - pot;
113
114    % concatenate result in a 3-by-3 affine transform matrix
115    transfo(:,:,i) = [vx' vy' pot' ; 0 0 1];
116end
117
118