Why My nurbs Function code not work?

14 views (last 30 days)
Graziano Fuccio
Graziano Fuccio on 2 Aug 2016
Commented: Farid Freud on 31 Aug 2020
Hi guys! Im studying the nurbs function and as first thing i looked for matlab code around of internet. I found some code but its not for me, too complicated and bugged! So i decided to write a matlab code for nurbs. Someone can help me?? This is my code:
function [C, N, U] = nurbs_function(n, t, w, P, U)
% n = curve degree
% t = knot vector
% w = weight vector
% P = control points
% U = evaluation poins [Optionals]
if nargin < 5
U = linspace(t(n), t(end-n), 10*size(P,2));% allocate points uniformly,
elseif isscalar(U) && U > 1
validateattributes(U, {'numeric'}, {'positive','integer','scalar'});
U = linspace(t(d+1), t(end-d), U); % allocate points uniformly
end
m = numel(U);
N = zeros(m, size(P, 2));
for j = 1 : m
u = U(j);
for i = 1 : numel(t) - n -1
N(j, i) = bspline_basis(i, n, t, u); %here i calculate the basis function N in u
end
end
C = zeros(2, m);
for j = 1 : m
saved = [0;0];
for i = 1 : size(N, 2)
if N(j, i) ~= 0
temp = (N(j, i) * w(i) * P(:, i))/(N(j, i) * w(i)); %this is formula of nurbs, but doesn't work well
saved = saved + temp;
end
end
C(:, j) = saved;
end
end
And this is the bspline_basis function written by Levente Hunyadi:
function [y,x] = bspline_basis(j,n,t,x)
% B-spline basis function value B(j,n) at x.
%
% Input arguments:
% j:
% interval index, 0 =< j < numel(t)-n
% n:
% B-spline order (2 for linear, 3 for quadratic, etc.)
% t:
% knot vector
% x (optional):
% value where the basis function is to be evaluated
%
% Output arguments:
% y:
% B-spline basis function value, nonzero for a knot span of n
% Copyright 2010 Levente Hunyadi
validateattributes(j, {'numeric'}, {'nonnegative','integer','scalar'});
validateattributes(n, {'numeric'}, {'positive','integer','scalar'});
validateattributes(t, {'numeric'}, {'real','vector'});
assert(all( t(2:end)-t(1:end-1) >= 0 ), ...
'Knot vector values should be nondecreasing.');
if nargin < 4
x = linspace(t(n), t(end-n+1), 100); % allocate points uniformly
else
validateattributes(x, {'numeric'}, {'real','vector'});
end
assert(0 <= j && j < numel(t)-n, ...
'Invalid interval index j = %d, expected 0 =< j < %d (0 =< j < numel(t)-n).', j, numel(t)-n);
y = bspline_basis_recurrence(j,n,t,x);
function y = bspline_basis_recurrence(j,n,t,x)
y = zeros(size(x));
if n > 1
b = bspline_basis(j,n-1,t,x);
dn = x - t(j+1);
dd = t(j+n) - t(j+1);
if dd ~= 0 % indeterminate forms 0/0 are deemed to be zero
y = y + b.*(dn./dd);
end
b = bspline_basis(j+1,n-1,t,x);
dn = t(j+n+1) - x;
dd = t(j+n+1) - t(j+1+1);
if dd ~= 0
y = y + b.*(dn./dd);
end
elseif t(j+2) < t(end) % treat last element of knot vector as a special case
y(t(j+1) <= x & x < t(j+2)) = 1;
else
y(t(j+1) <= x) = 1;
end
I suppose that this code is right. I tryed my code with this params:
t = [0 0 0 1 2 5 5.5 6 9 11 13 13 13];
w = [1.2 3 5 1 0 0.5 2 3 5];
P = [2 5 3 3 6 10 10 7 9; 0 1 4 8 10 8 4 1 0];
n = 3;
The result is this: a matrix 2 x 90 with this value:
C(:, 1: 10):
0 7 7 7 7 7 7 7 7 10
0 1 1 1 1 1 1 1 1 5
C(:, 41 : 51):
11 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
13 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
C(:, 81 : 90):
27 27 27 27 27 27 27 27 27 17
13 13 13 13 13 13 13 13 13 5
This are only some portion of matrix... but as you can see the matrix is abnormal! Can someone help me? Where i wrong? Thanks
  1 Comment
Farid Freud
Farid Freud on 31 Aug 2020
j: % interval index, 0 =< j < numel(t)-n
how i can input j in matlab ?
as vector or point , in brackets or parentheses ?

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!