]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/basisfun.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / basisfun.m
1 function B = basisfun (iv, uv, p, U)
2
3 % BASISFUN:  Basis function for B-Spline
4 %
5 % Calling Sequence:
6
7 %   N = basisfun(iv,uv,p,U)
8 %   
9 %    INPUT:
10 %   
11 %      iv - knot span  ( from FindSpan() )
12 %      uv - parametric points
13 %      p  - spline degree
14 %      U  - knot sequence
15 %   
16 %    OUTPUT:
17 %   
18 %      N - Basis functions vector(numel(uv)*(p+1))
19 %   
20 %    Adapted from Algorithm A2.2 from 'The NURBS BOOK' pg70.
21 %
22 % Copyright (C) 2000 Mark Spink
23 % Copyright (C) 2007 Daniel Claxton
24 % Copyright (C) 2009 Carlo de Falco
25 %
26 %    This program is free software: you can redistribute it and/or modify
27 %    it under the terms of the GNU General Public License as published by
28 %    the Free Software Foundation, either version 2 of the License, or
29 %    (at your option) any later version.
30
31 %    This program is distributed in the hope that it will be useful,
32 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
33 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34 %    GNU General Public License for more details.
35 %
36 %    You should have received a copy of the GNU General Public License
37 %    along with this program.  If not, see <http://www.gnu.org/licenses/>.
38
39 B = zeros(numel(uv), p+1);
40                                                
41 for jj = 1:numel(uv) 
42
43   i = iv(jj) + 1; %% findspan uses 0-based numbering
44   u = uv(jj);
45
46   left = zeros(p+1,1);
47   right = zeros(p+1,1);
48
49   N(1) = 1;
50   for j=1:p
51     left(j+1) = u - U(i+1-j);
52     right(j+1) = U(i+j) - u;
53     saved = 0;
54
55     for r=0:j-1
56       temp = N(r+1)/(right(r+2) + left(j-r+1));
57       N(r+1) = saved + right(r+2)*temp;
58       saved = left(j-r+1)*temp;
59     end
60     
61     N(j+1) = saved;
62   end
63
64   B (jj, :) = N;
65
66 end
67
68 end
69
70 %!test
71 %!  n = 3; 
72 %!  U = [0 0 0 1/2 1 1 1]; 
73 %!  p = 2; 
74 %!  u = linspace (0, 1, 10);  
75 %!  s = findspan (n, p, u, U);  
76 %!  Bref = [1.00000   0.00000   0.00000
77 %!          0.60494   0.37037   0.02469
78 %!          0.30864   0.59259   0.09877
79 %!          0.11111   0.66667   0.22222
80 %!          0.01235   0.59259   0.39506
81 %!          0.39506   0.59259   0.01235
82 %!          0.22222   0.66667   0.11111
83 %!          0.09877   0.59259   0.30864
84 %!          0.02469   0.37037   0.60494
85 %!          0.00000   0.00000   1.00000];
86 %!  B = basisfun (s, u, p, U);
87 %!  assert (B, Bref, 1e-5);