]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/kntbrkdegmult.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / kntbrkdegmult.m
1 % KNTBRKDEGMULT: Construct an open knot vector by giving the sequence of
2 %                knots, the degree and the multiplicity.
3 %
4 %   knots = kntbrkdegreg (breaks, degree)
5 %   knots = kntbrkdegreg (breaks, degree, mult)
6 %
7 % INPUT:
8 %
9 %     breaks:  sequence of knots.
10 %     degree:  polynomial degree of the splines associated to the knot vector.
11 %     mult:    multiplicity of the knots.
12 %
13 % OUTPUT:
14 %
15 %     knots:  knot vector.
16 %
17 % If MULT has as many entries as BREAKS, or as the number of interior
18 %   knots, a different multiplicity will be assigned to each knot. If
19 %   MULT is not present, it will be taken equal to 1.
20 %
21 % Copyright (C) 2010 Carlo de Falco, Rafael Vazquez
22 %
23 %    This program is free software: you can redistribute it and/or modify
24 %    it under the terms of the GNU General Public License as published by
25 %    the Free Software Foundation, either version 2 of the License, or
26 %    (at your option) any later version.
27
28 %    This program is distributed in the hope that it will be useful,
29 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
30 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 %    GNU General Public License for more details.
32 %
33 %    You should have received a copy of the GNU General Public License
34 %    along with this program.  If not, see <http://www.gnu.org/licenses/>.
35
36 function knots = kntbrkdegmult (breaks, degree, mult)
37
38   if (iscell (breaks))
39     if (nargin == 2)
40       mult = 1;
41     end
42   
43     if (numel(breaks)~=numel(degree) || numel(breaks)~=numel(mult))
44       error('kntbrkdegmult: degree and multiplicity must have the same length as the number of knot vectors')
45     end
46
47     degree = num2cell (degree);
48
49     if (~iscell (mult))
50       mult = num2cell (mult);
51     end
52
53     knots = cellfun (@do_kntbrkdegmult, breaks, degree, mult, 'uniformoutput', false);
54
55   else
56
57     if (nargin == 2)
58       mult = 1;
59     end
60
61     knots = do_kntbrkdegmult (breaks, degree, mult);
62   end
63 end
64
65   
66 function knots = do_kntbrkdegmult (breaks, degree, mult)  
67
68 if (numel (breaks) < 2)
69   error ('kntbrkdegmult: the knots sequence should contain at least two points')
70 end
71
72 if (numel (mult) == 1)
73   mults = [degree+1, mult(ones (1, numel (breaks) - 2)), degree+1];
74 elseif (numel (mult) == numel (breaks))
75   mults = [degree+1 mult(2:end-1) degree+1];
76 elseif (numel (mult) == numel (breaks) - 2)
77   mults = [degree+1 mult degree+1];
78 else
79   error('kntbrkdegmult: the length of mult should be equal to one or the number of knots')
80 end
81
82 if (any (mults > degree+1))
83   warning ('kntbrkdegmult: some knots have higher multiplicity than the degree+1')
84 end
85
86 breaks = sort (breaks);
87
88 lm = numel (mults);
89 sm = sum (mults);
90
91 mm = zeros (1,sm);
92 mm (cumsum ([1 reshape(mults (1:end-1), 1, lm-1)])) = ones (1,lm);
93 knots = breaks (cumsum (mm));
94
95 end
96
97 %!test
98 %! breaks = [0 1 2 3 4];
99 %! degree = 3;
100 %! knots = kntbrkdegmult (breaks, degree);
101 %! assert (knots, [0 0 0 0 1 2 3 4 4 4 4])
102
103 %!test
104 %! breaks = [0 1 2 3 4];
105 %! degree = 3;
106 %! mult   = 2;
107 %! knots = kntbrkdegmult (breaks, degree, mult);
108 %! assert (knots, [0 0 0 0 1 1 2 2 3 3 4 4 4 4])
109
110 %!test
111 %! breaks = [0 1 2 3 4];
112 %! degree = 3;
113 %! mult   = [1 2 3];
114 %! knots = kntbrkdegmult (breaks, degree, mult);
115 %! assert (knots, [0 0 0 0 1 2 2 3 3 3 4 4 4 4])
116
117 %!test
118 %! breaks = {[0 1 2 3 4] [0 1 2 3]};
119 %! degree = [3 2];
120 %! mult   = {[1 2 3] 2};
121 %! knots = kntbrkdegmult (breaks, degree, mult);
122 %! assert (knots, {[0 0 0 0 1 2 2 3 3 3 4 4 4 4] [0 0 0 1 1 2 2 3 3 3]})