1 function inurbs = nrbkntins(nurbs,iknots)
3 % NRBKNTINS: Insert a single or multiple knots into a NURBS curve,
8 % icrv = nrbkntins(crv,iuknots);
9 % isrf = nrbkntins(srf,{iuknots ivknots});
10 % ivol = nrbkntins(vol,{iuknots ivknots iwknots});
14 % crv : NURBS curve, see nrbmak.
16 % srf : NURBS surface, see nrbmak.
18 % srf : NURBS volume, see nrbmak.
20 % iuknots : Knots to be inserted along U direction.
22 % ivknots : Knots to be inserted along V direction.
24 % iwknots : Knots to be inserted along W direction.
28 % icrv : new NURBS structure for a curve with knots inserted.
30 % isrf : new NURBS structure for a surface with knots inserted.
32 % ivol : new NURBS structure for a volume with knots inserted.
36 % Inserts knots into the NURBS data structure, these can be knots at
37 % new positions or at the location of existing knots to increase the
38 % multiplicity. Note that the knot multiplicity cannot be increased
39 % beyond the order of the spline. Knots along the V direction can only
40 % inserted into NURBS surfaces, not curve that are always defined along
41 % the U direction. This function use the B-Spline function bspkntins,
42 % which interfaces to an internal 'C' routine.
46 % Insert two knots into a curve, one at 0.3 and another
49 % icrv = nrbkntins(crv, [0.3 0.4 0.4])
51 % Insert into a surface two knots as (1) into the U knot
52 % sequence and one knot into the V knot sequence at 0.5.
54 % isrf = nrbkntins(srf, {[0.3 0.4 0.4] [0.5]})
62 % No knot multiplicity will be increased beyond the order of the spline.
64 % Copyright (C) 2000 Mark Spink, 2010 Rafael Vazquez
66 % This program is free software: you can redistribute it and/or modify
67 % it under the terms of the GNU General Public License as published by
68 % the Free Software Foundation, either version 2 of the License, or
69 % (at your option) any later version.
71 % This program is distributed in the hope that it will be useful,
72 % but WITHOUT ANY WARRANTY; without even the implied warranty of
73 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
74 % GNU General Public License for more details.
76 % You should have received a copy of the GNU General Public License
77 % along with this program. If not, see <http://www.gnu.org/licenses/>.
80 error('Input argument must include the NURBS and knots to be inserted');
84 error('NURBS representation is not structure!');
87 if ~strcmp(nurbs.form,'B-NURBS')
88 error('Not a recognised NURBS representation');
91 degree = nurbs.order-1;
93 if iscell(nurbs.knots)
94 if size(nurbs.knots,2)==3
95 % NURBS represents a volume
96 num1 = nurbs.number(1);
97 num2 = nurbs.number(2);
98 num3 = nurbs.number(3);
100 % Insert knots along the w direction
101 if isempty(iknots{3})
103 knots{3} = nurbs.knots{3};
105 coefs = reshape(nurbs.coefs,4*num1*num2,num3);
106 [coefs,knots{3}] = bspkntins(degree(3),coefs,nurbs.knots{3},iknots{3});
107 num3 = size(coefs,2);
108 coefs = reshape(coefs,[4 num1 num2 num3]);
111 % Insert knots along the v direction
112 if isempty(iknots{2})
113 knots{2} = nurbs.knots{2};
115 coefs = permute(coefs,[1 2 4 3]);
116 coefs = reshape(coefs,4*num1*num3,num2);
117 [coefs,knots{2}] = bspkntins(degree(2),coefs,nurbs.knots{2},iknots{2});
118 num2 = size(coefs,2);
119 coefs = reshape(coefs,[4 num1 num3 num2]);
120 coefs = permute(coefs,[1 2 4 3]);
123 % Insert knots along the u direction
124 if isempty(iknots{1})
125 knots{1} = nurbs.knots{1};
127 coefs = permute(coefs,[1 3 4 2]);
128 coefs = reshape(coefs,4*num2*num3,num1);
129 [coefs,knots{1}] = bspkntins(degree(1),coefs,nurbs.knots{1},iknots{1});
130 coefs = reshape(coefs,[4 num2 num3 size(coefs,2)]);
131 coefs = permute(coefs,[1 4 2 3]);
134 elseif size(nurbs.knots,2)==2
135 % NURBS represents a surface
136 num1 = nurbs.number(1);
137 num2 = nurbs.number(2);
139 % Insert knots along the v direction
140 if isempty(iknots{2})
142 knots{2} = nurbs.knots{2};
144 coefs = reshape(nurbs.coefs,4*num1,num2);
145 [coefs,knots{2}] = bspkntins(degree(2),coefs,nurbs.knots{2},iknots{2});
146 num2 = size(coefs,2);
147 coefs = reshape(coefs,[4 num1 num2]);
150 % Insert knots along the u direction
151 if isempty(iknots{1})
152 knots{1} = nurbs.knots{1};
154 coefs = permute(coefs,[1 3 2]);
155 coefs = reshape(coefs,4*num2,num1);
156 [coefs,knots{1}] = bspkntins(degree(1),coefs,nurbs.knots{1},iknots{1});
157 coefs = reshape(coefs,[4 num2 size(coefs,2)]);
158 coefs = permute(coefs,[1 3 2]);
163 % NURBS represents a curve
168 [coefs,knots] = bspkntins(degree,nurbs.coefs,nurbs.knots,iknots);
173 % construct new NURBS
174 inurbs = nrbmak(coefs,knots);
180 %! plot(crv.coefs(1,:),crv.coefs(2,:),'bo')
181 %! title('Knot insertion along test curve: curve and control polygons.');
183 %! plot(crv.coefs(1,:),crv.coefs(2,:),'b--');
187 %! icrv = nrbkntins(crv,[0.125 0.375 0.625 0.875] );
188 %! plot(icrv.coefs(1,:),icrv.coefs(2,:),'ro')
189 %! plot(icrv.coefs(1,:),icrv.coefs(2,:),'r--');