]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/nrbkntins.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / nrbkntins.m
1 function inurbs = nrbkntins(nurbs,iknots)
2
3 % NRBKNTINS: Insert a single or multiple knots into a NURBS curve,
4 %            surface or volume.
5
6 % Calling Sequence:
7
8 %   icrv = nrbkntins(crv,iuknots);
9 %   isrf = nrbkntins(srf,{iuknots ivknots});
10 %   ivol = nrbkntins(vol,{iuknots ivknots iwknots});
11
12 % INPUT:
13
14 %   crv         : NURBS curve, see nrbmak.
15
16 %   srf         : NURBS surface, see nrbmak.
17
18 %   srf         : NURBS volume, see nrbmak.
19
20 %   iuknots     : Knots to be inserted along U direction.
21
22 %   ivknots     : Knots to be inserted along V direction.
23
24 %   iwknots     : Knots to be inserted along W direction.
25
26 % OUTPUT:
27
28 %   icrv        : new NURBS structure for a curve with knots inserted.
29
30 %   isrf        : new NURBS structure for a surface with knots inserted.
31
32 %   ivol        : new NURBS structure for a volume with knots inserted.
33
34 % Description:
35
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.
43
44 % Examples:
45
46 %   Insert two knots into a curve, one at 0.3 and another
47 %   twice at 0.4
48 %
49 %   icrv = nrbkntins(crv, [0.3 0.4 0.4])
50
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.
53 %
54 %   isrf = nrbkntins(srf, {[0.3 0.4 0.4] [0.5]})
55
56 % See also:
57
58 %   bspkntins
59 %
60 % Note:
61 %
62 %   No knot multiplicity will be increased beyond the order of the spline.
63 %
64 %    Copyright (C) 2000 Mark Spink, 2010 Rafael Vazquez
65 %
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.
70
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.
75 %
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/>.
78
79 if nargin < 2
80   error('Input argument must include the NURBS and knots to be inserted');
81 end
82
83 if ~isstruct(nurbs)
84   error('NURBS representation is not structure!');
85 end
86
87 if ~strcmp(nurbs.form,'B-NURBS')
88   error('Not a recognised NURBS representation');
89 end
90
91 degree = nurbs.order-1;
92
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);
99
100   % Insert knots along the w direction
101   if isempty(iknots{3})
102     coefs = nurbs.coefs;
103     knots{3} = nurbs.knots{3};
104   else
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]);
109   end
110
111   % Insert knots along the v direction
112   if isempty(iknots{2})
113     knots{2} = nurbs.knots{2};
114   else
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]);
121   end
122
123   % Insert knots along the u direction
124   if isempty(iknots{1})
125     knots{1} = nurbs.knots{1};
126   else   
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]);
132   end
133      
134  elseif size(nurbs.knots,2)==2
135   % NURBS represents a surface
136   num1 = nurbs.number(1);
137   num2 = nurbs.number(2);
138
139   % Insert knots along the v direction
140   if isempty(iknots{2})
141     coefs = nurbs.coefs;
142     knots{2} = nurbs.knots{2};
143   else
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]);
148   end
149
150   % Insert knots along the u direction
151   if isempty(iknots{1})
152     knots{1} = nurbs.knots{1};
153   else   
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]);
159   end
160  end
161 else
162
163   % NURBS represents a curve
164   if isempty(iknots)
165     coefs = nurbs.coefs;
166     knots = nurbs.knots;
167   else
168     [coefs,knots] = bspkntins(degree,nurbs.coefs,nurbs.knots,iknots);  
169   end
170
171 end
172
173 % construct new NURBS
174 inurbs = nrbmak(coefs,knots); 
175
176 end
177
178 %!demo
179 %! crv = nrbtestcrv;
180 %! plot(crv.coefs(1,:),crv.coefs(2,:),'bo')
181 %! title('Knot insertion along test curve: curve and control polygons.');
182 %! hold on;
183 %! plot(crv.coefs(1,:),crv.coefs(2,:),'b--');
184 %!
185 %! nrbplot(crv,48);
186 %!
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--');
190 %! hold off