]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/nrbbasisfunder.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / nrbbasisfunder.m
1 function varargout = nrbbasisfunder (points, nrb)
2
3 % NRBBASISFUNDER:  NURBS basis functions derivatives
4 %
5 % Calling Sequence:
6
7 %   Bu          = nrbbasisfunder (u, crv)
8 %   [Bu, N]     = nrbbasisfunder (u, crv)
9 %   [Bu, Bv]    = nrbbasisfunder ({u, v}, srf)
10 %   [Bu, Bv, N] = nrbbasisfunder ({u, v}, srf)
11 %   [Bu, Bv, N] = nrbbasisfunder (p, srf)
12 %
13 %    INPUT:
14 %   
15 %      u or p(1,:,:)  - parametric points along u direction
16 %      v or p(2,:,:)  - parametric points along v direction
17 %      crv - NURBS curve
18 %      srf - NURBS surface
19 %   
20 %    OUTPUT:
21 %   
22 %      Bu - Basis functions derivatives WRT direction u
23 %           size(Bu)=[numel(u),(p+1)] for curves
24 %           or [numel(u)*numel(v), (p+1)*(q+1)] for surfaces
25 %
26 %      Bv - Basis functions derivatives WRT direction v
27 %           size(Bv)=[numel(v),(p+1)] for curves
28 %           or [numel(u)*numel(v), (p+1)*(q+1)] for surfaces
29 %
30 %      N - Indices of the basis functions that are nonvanishing at each
31 %          point. size(N) == size(B)
32 %   
33 %    Copyright (C) 2009 Carlo de Falco
34 %
35 %    This program is free software: you can redistribute it and/or modify
36 %    it under the terms of the GNU General Public License as published by
37 %    the Free Software Foundation, either version 2 of the License, or
38 %    (at your option) any later version.
39
40 %    This program is distributed in the hope that it will be useful,
41 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
42 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43 %    GNU General Public License for more details.
44 %
45 %    You should have received a copy of the GNU General Public License
46 %    along with this program.  If not, see <http://www.gnu.org/licenses/>.
47
48   
49   if (   (nargin<2) ...
50       || (nargout>3) ...
51       || (~isstruct(nrb)) ...
52       || (iscell(points) && ~iscell(nrb.knots)) ...
53       || (~iscell(points) && iscell(nrb.knots) && (size(points,1)~=2)) ...
54       || (~iscell(nrb.knots) && (nargout>2)) ...
55       )
56     error('Incorrect input arguments in nrbbasisfun');
57   end
58                             
59   if (~iscell(nrb.knots))         %% NURBS curve
60     
61     [varargout{1}, varargout{2}] = nrb_crv_basisfun_der__ (points, nrb);
62
63   elseif size(nrb.knots,2) == 2 %% NURBS surface
64
65     if (iscell(points))
66       [v, u] = meshgrid(points{2}, points{1});
67       p = [u(:), v(:)]';
68     else
69       p = points;
70     end
71     
72     [varargout{1}, varargout{2}, varargout{3}] = nrb_srf_basisfun_der__ (p, nrb);
73
74   else                            %% NURBS volume
75     error('The function nrbbasisfunder is not yet ready for volumes')
76   end
77 end
78   
79 %!demo
80 %! U = [0 0 0 0 1 1 1 1];
81 %! x = [0 1/3 2/3 1] ;
82 %! y = [0 0 0 0];
83 %! w = [1 1 1 1];
84 %! nrb = nrbmak ([x;y;y;w], U);
85 %! u = linspace(0, 1, 30);
86 %! [Bu, id] = nrbbasisfunder (u, nrb);
87 %! plot(u, Bu)
88 %! title('Derivatives of the cubic Bernstein polynomials')
89 %! hold off
90
91 %!test
92 %! U = [0 0 0 0 1 1 1 1];
93 %! x = [0 1/3 2/3 1] ;
94 %! y = [0 0 0 0];
95 %! w = rand(1,4);
96 %! nrb = nrbmak ([x;y;y;w], U);
97 %! u = linspace(0, 1, 30);
98 %! [Bu, id] = nrbbasisfunder (u, nrb);
99 %! #plot(u, Bu)
100 %! assert (sum(Bu, 2), zeros(numel(u), 1), 1e-10), 
101
102 %!test
103 %! U = [0 0 0 0 1/2 1 1 1 1];
104 %! x = [0 1/4 1/2 3/4 1] ;
105 %! y = [0 0 0 0 0];
106 %! w = rand(1,5);
107 %! nrb = nrbmak ([x;y;y;w], U);
108 %! u = linspace(0, 1, 300); 
109 %! [Bu, id] = nrbbasisfunder (u, nrb); 
110 %! assert (sum(Bu, 2), zeros(numel(u), 1), 1e-10)
111
112 %!test
113 %! p = 2;   q = 3;   m = 4; n = 5;
114 %! Lx  = 1; Ly  = 1; 
115 %! nrb = nrb4surf   ([0 0], [1 0], [0 1], [1 1]);
116 %! nrb = nrbdegelev (nrb, [p-1, q-1]);
117 %! aux1 = linspace(0,1,m); aux2 = linspace(0,1,n);
118 %! nrb = nrbkntins  (nrb, {aux1(2:end-1), aux2(2:end-1)});
119 %! nrb.coefs (4,:,:) = nrb.coefs(4,:,:) + rand (size (nrb.coefs (4,:,:)));
120 %! [Bu, Bv, N] = nrbbasisfunder ({rand(1, 20), rand(1, 20)}, nrb);
121 %! #plot3(squeeze(u(1,:,:)), squeeze(u(2,:,:)), reshape(Bu(:,10), 20, 20),'o')
122 %! assert (sum (Bu, 2), zeros(20^2, 1), 1e-10)
123