How to implement B-Spline in matrix form?

346 views Asked by At

I am simply trying to draw a cubic B-Spline using the matrix representation given in this paper: http://vision.ucsd.edu/~kbranson/research/bsplines/bsplines.pdf

Specifically, I am trying the to exactly replicate the formula in section 3 (using placement matrix G) of the PDF. But I am not sure where I am going wrong. It keeps producing straight lines. Can anyone point me out what is wrong with the following code (it should run on any version of matlab; its quite simple) ?

% main.m
clc; clear;

n_cpts = 5;
deg = 3;

cpts = randi(30, n_cpts, 2);

n_knots = n_cpts + deg + 1;
knots = 0:(n_knots-1);

ts = knots(deg):0.05:knots(end-deg);
curve = [];

for t = ts(1:end-1)
    k = floor(t);
    T = [1, t, t^2, t^3];
    B = Bi(k);
    G = Gi(k, n_cpts);
    p = T * B * G * cpts;
    curve = [curve; p];
end

scatter(curve(:,1), curve(:,2));

The helper functions

% Bi.m
% The 'B' matrix
function [B] = Bi(i)
B = [[ -(1./6.)*i^3, (1./6.)*(3*i^3 + 3*i^2 - 3*i + 1), -(1./2.)*(i^3)-(i^2)+2./3., (1./2.)*(i+1)^3 ];
      [ +(1./2.)*i^2, -(1./2.)*(3*i-1)*(i+1), (1./2.)*(3*i^2 + 4*i), -(1./2.)*(i+1)^2 ];
      [ (1./2.)*i, (1./2.)*(3*i+1), -(1./2.)*(3*i+2), (1./2.)*(i+1) ];
      [ (1./6.), -(1./2.), (1./2.), -(1./6.) ]];
end
% Gi.m
% The 'G' matrix
function [G] = Gi(i, L)
G = zeros(4, L);
for m = 1:4
    for n = 1:L
        if n == i+m-3
            G(m,n) = 1;
        end
    end
end
end

My output looks like this: enter image description here

0

There are 0 answers