]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/findspan.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / findspan.m
1 function s = findspan(n,p,u,U)                
2 % FINDSPAN  Find the span of a B-Spline knot vector at a parametric point
3 %
4 % Calling Sequence:
5
6 %   s = findspan(n,p,u,U)
7
8 %  INPUT:
9
10 %    n - number of control points - 1
11 %    p - spline degree
12 %    u - parametric point
13 %    U - knot sequence
14
15 %  OUTPUT:
16
17 %    s - knot span index
18 %
19 %  Modification of Algorithm A2.1 from 'The NURBS BOOK' pg68
20 %
21 %    Copyright (C) 2010 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 if (max(u(:))>U(end) || min(u(:))<U(1))
37   error('Some value is outside the knot span')
38 end
39
40 s = zeros(size(u));
41 for j = 1:numel(u)
42   if (u(j)==U(n+2)), s(j)=n; continue, end
43   s(j) = find(u(j) >= U,1,'last')-1;
44 end
45
46 end
47
48 %!test
49 %!  n = 3; 
50 %!  U = [0 0 0 1/2 1 1 1]; 
51 %!  p = 2; 
52 %!  u = linspace(0, 1, 10);  
53 %!  s = findspan (n, p, u, U);
54 %!  assert (s, [2*ones(1, 5) 3*ones(1, 5)]);
55
56 %!test
57 %! p = 2; m = 7; n = m - p - 1;
58 %! U = [zeros(1,p)  linspace(0,1,m+1-2*p) ones(1,p)];
59 %! u = [ 0   0.11880   0.55118   0.93141   0.40068   0.35492 0.44392   0.88360   0.35414   0.92186   0.83085   1];
60 %! s = [2   2   3   4   3   3   3   4   3   4   4   4];
61 %! assert (findspan (n, p, u, U), s, 1e-10);