]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/kntbrkdegreg.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / kntbrkdegreg.m
1 % KNTBRKDEGREG: Construct an open knot vector by giving the sequence of
2 %                knots, the degree and the regularity.
3 %
4 %   knots = kntbrkdegreg (breaks, degree)
5 %   knots = kntbrkdegreg (breaks, degree, regularity)
6 %
7 % INPUT:
8 %
9 %     breaks:     sequence of knots.
10 %     degree:     polynomial degree of the splines associated to the knot vector.
11 %     regularity: splines regularity.
12 %
13 % OUTPUT:
14 %
15 %     knots:  knot vector.
16 %
17 % If REGULARITY has as many entries as BREAKS, or as the number of interior
18 %   knots, a different regularity will be assigned to each knot. If
19 %   REGULARITY is not present, it will be taken equal to DEGREE-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 = kntbrkdegreg (breaks, degree, reg)
37
38 if (iscell (breaks))
39   if (nargin == 2)
40     reg = degree - 1;
41   end
42   
43   if (numel(breaks)~=numel(degree) || numel(breaks)~=numel(reg))
44     error('kntbrkdegreg: degree and regularity must have the same length as the number of knot vectors')
45   end
46
47   degree = num2cell (degree);
48
49   if (~iscell (reg))
50     reg = num2cell (reg);
51   end
52
53   knots = cellfun (@do_kntbrkdegreg, breaks, degree, reg, 'uniformoutput', false);
54 else
55
56   if (nargin == 2)
57     reg = degree - 1;
58   end
59
60   knots = do_kntbrkdegreg (breaks, degree, reg);
61 end
62
63 end
64
65 function knots = do_kntbrkdegreg (breaks, degree, reg)
66
67 if (numel (breaks) < 2)
68   error ('kntbrkdegreg: the knots sequence should contain at least two points') 
69 end
70
71 if (numel (reg) == 1)
72   mults = [-1, (degree (ones (1, numel (breaks) - 2)) - reg), -1];
73 elseif (numel (reg) == numel (breaks))
74   mults = degree - reg;
75 elseif (numel (reg) == numel (breaks) - 2)
76   mults = [-1 degree-reg -1];
77 else
78   error('kntbrkdegreg: the length of mult should be equal to one or the number of knots')
79 end
80
81 if (any (reg < -1))
82   warning ('kntbrkdegreg: for some knots the regularity is lower than -1')
83 elseif (any (reg > degree-1))
84   error('kntbrkdegreg: the regularity should be lower than the degree')
85 end
86
87 knots = kntbrkdegmult (breaks, degree, mults);
88
89 end
90
91 %!test
92 %! breaks = [0 1 2 3 4];
93 %! degree = 3;
94 %! knots = kntbrkdegreg (breaks, degree);
95 %! assert (knots, [0 0 0 0 1 2 3 4 4 4 4])
96
97 %!test
98 %! breaks = [0 1 2 3 4];
99 %! degree = 3;
100 %! reg    = 1;
101 %! knots = kntbrkdegreg (breaks, degree, reg);
102 %! assert (knots, [0 0 0 0 1 1 2 2 3 3 4 4 4 4])
103
104 %!test
105 %! breaks = [0 1 2 3 4];
106 %! degree = 3;
107 %! reg    = [0 1 2];
108 %! knots = kntbrkdegreg (breaks, degree, reg);
109 %! assert (knots, [0 0 0 0 1 1 1 2 2 3 4 4 4 4])
110
111 %!test
112 %! breaks = {[0 1 2 3 4] [0 1 2 3]};
113 %! degree = [3 2];
114 %! reg    = {[0 1 2] 0};
115 %! knots = kntbrkdegreg (breaks, degree, reg);
116 %! assert (knots, {[0 0 0 0 1 1 1 2 2 3 4 4 4 4] [0 0 0 1 1 2 2 3 3 3]})
117