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 varargout = transformVector(varargin)
29%TRANSFORMVECTOR Transform a vector with an affine transform.
30%
31%   VECT2 = transformVector(VECT1, TRANS);
32%   where VECT1 has the form [xv yv], and TRANS is a [2*2], [2*3] or [3*3]
33%   matrix, returns the vector transformed with affine transform TRANS.
34%
35%   Format of TRANS can be one of :
36%   [a b]   ,   [a b c] , or [a b c]
37%   [d e]       [d e f]      [d e f]
38%                            [0 0 1]
39%
40%   VECT2 = transformVector(VECT1, TRANS);
41%   Also works when PTA is a [N*2] array of double. In this case, VECT2 has
42%   the same size as VECT1.
43%
44%   [vx2 vy2] = transformVector(vx1, vy1, TRANS);
45%   Also works when vx1 and vy1 are arrays the same size. The function
46%   transform each couple of (vx1, vy1), and return the result in
47%   (vx2, vy2), which is the same size as (vx1 vy1).
48%
49%
50%   See also:
51%   vectors2d, transforms2d, rotateVector, transformPoint
52%
53%   ---------
54%
55%   author : David Legland
56%   INRA - TPV URPOI - BIA IMASTE
57%   created the 12/03/2007.
58%
59
60%   HISTORY
61
62
63if length(varargin)==2
64    var = varargin{1};
65    vx = var(:,1);
66    vy = var(:,2);
67    trans = varargin{2};
68elseif length(varargin)==3
69    vx = varargin{1};
70    vy = varargin{2};
71    trans = varargin{3};
72else
73    error('wrong number of arguments in "transformVector"');
74end
75
76
77% compute new position of vector
78vx2 = vx*trans(1,1) + vy*trans(1,2);
79vy2 = vx*trans(2,1) + vy*trans(2,2);
80
81% format output
82if nargout==0 || nargout==1
83    varargout{1} = [vx2 vy2];
84elseif nargout==2
85    varargout{1} = vx2;
86    varargout{2} = vy2;
87end
88