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 = createScaling3d(varargin)
29%CREATESCALING3D Create the 4x4 matrix of a 3D scaling.
30%
31%   TRANS = createScaling3d(S);
32%   returns the scaling transform corresponding to a scaling factor S in
33%   each direction. S can be a scalar, or a 1-by-3 vector containing the
34%   scaling factor in each direction.
35%
36%   TRANS = createScaling3d(SX, SY, SZ);
37%   returns the scaling transform corresponding to a different scaling
38%   factor in each direction.
39%
40%   The returned matrix has the form :
41%   [SX  0  0  0]
42%   [ 0 SY  0  0]
43%   [ 0  0 SZ  0]
44%   [ 0  0  0  0]
45%
46%   See also:
47%   transforms3d, transformPoint3d, transformVector3d, createTranslation3d,
48%   createRotationOx, createRotationOy, createRotationOz
49
50%
51%   ---------
52%   author : David Legland
53%   INRA - TPV URPOI - BIA IMASTE
54%   created the 20/04/2006.
55%
56
57%   HISTORY
58%   25/11/2008 rename from scale3d to scaling3d
59%   30/04/2009 rename to createScaling3d
60
61
62%% default arguments
63sx = 1;
64sy = 1;
65sz = 1;
66center = [0 0 0];
67
68%% process input parameters
69if nargin == 1
70    % only one argument -> scaling factor
71    [sx, sy, sz]= parseScalingFactors(varargin{1});
72
73elseif nargin == 2
74    % 2 arguments, giving center and uniform scaling
75    center = varargin{1};
76    [sx, sy, sz]= parseScalingFactors(varargin{2});
77
78elseif nargin == 3
79    % 3 arguments, giving scaling in each direction
80    sx = varargin{1};
81    sy = varargin{2};
82    sz = varargin{3};
83
84elseif nargin == 4
85    % 4 arguments, giving center and scaling in each direction
86    center = varargin{1};
87    sx = varargin{2};
88    sy = varargin{3};
89    sz = varargin{4};
90end
91
92%% create the scaling matrix
93trans = [...
94    sx 0 0 center(1)*(1-sx);...
95    0 sy 0 center(2)*(1-sy);...
96    0 0 sz center(3)*(1-sz);...
97    0 0 0 1];
98
99%% Helper function
100function [sx, sy, sz] = parseScalingFactors(var)
101
102if length(var)==1
103    % same scaling factor in each direction
104    sx = var;
105    sy = var;
106    sz = var;
107elseif length(var)==3
108    % scaling is a vector, giving different scaling in each direction
109    sx = var(1);
110    sy = var(2);
111    sz = var(3);
112else
113    error('wrong size for first parameter of "createScaling3d"');
114end
115