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 trans = createScaling(varargin)
29%CREATESCALING Create the 3*3 matrix of a scaling in 2 dimensions.
30%
31%   TRANS = createScaling(SX, SY);
32%   return the matrix corresponding to scaling by SX and SY in the 2
33%   main directions.
34%   The returned matrix has the form:
35%   [SX  0  0]
36%   [0  SY  0]
37%   [0   0  1]
38%
39%   TRANS = createScaling(SX);
40%   Assume SX and SY are equals.
41%
42%   TRANS = createScaling(CENTER, ...);
43%   Specifies the center of the scaling transform. The argument CENTER
44%   should be a 1-by-2 array representing coordinates of center.
45%
46%   See also:
47%   transforms2d, transformPoint, createTranslation, createRotation
48
49%
50%   ---------
51%   author : David Legland
52%   INRA - TPV URPOI - BIA IMASTE
53%   created the 07/04/2004.
54
55
56%   HISTORY
57%   04/01/2007: rename as scaling
58%   22/04/2009: rename as createScaling
59
60% defined default arguments
61sx = 1;
62sy = 1;
63cx = 0;
64cy = 0;
65
66% process input arguments
67if nargin == 1
68    % the argument is either the scaling factor in both direction,
69    % or a 1x2 array containing scaling factor in each direction
70    var = varargin{1};
71    sx = var(1);
72    sy = var(1);
73    if length(var)>1
74        sy = var(2);
75    end
76elseif nargin == 2
77    % the 2 arguments are the scaling factors in each dimension
78    sx = varargin{1};
79    sy = varargin{2};
80elseif nargin == 3
81    % first argument is center, 2nd and 3d are scaling factors
82    center = varargin{1};
83    cx = center(1);
84    cy = center(2);
85    sx = varargin{2};
86    sy = varargin{3};
87end
88
89% concatenate results in a 3-by-3 matrix
90trans = [sx 0 cx*(1-sx); 0 sy cy*(1-sy); 0 0 1];
91
92