B样条的一个问题的试探(续)

  用数学归纳法能够证明B样条插值算法的正确性,但比较繁复,限于篇幅不做讨论。 

  有关B样条插值算法正确性反而是在Matlab实现中发现的,以下列出目前插值算法实现代码:

function [ new_vertices, new_knots ] = mybspline_insert( vertices, knots, new_knot, k )
%MYBSPLINE_INSERT Summary of this function goes here
%  Detailed explanation goes here

old_vertices = [0,0,0; vertices']';
old_knots = knots;

r = 0;
i = -1;
new_knots = [];
for j = 1 : length(old_knots)
    if old_knots(j) < new_knot
        new_knots(j) = old_knots(j);
    elseif old_knots(j) == new_knot
        r = r + 1;
        new_knots(j) = new_knot;
    else
        if i < 0
            i = j - 1;
        end
        new_knots(j+1) = old_knots(j);
    end
end
new_knots(i+1) = new_knot;


j1 = i - k + 2;
j2 = i - r + 1;

if j1 > j2
    tmp = j1;
    j1 = j2;
    j2 = tmp;
elseif j1 < 1
    j1 = 1;
end

n = length(old_vertices);
new_vertices = zeros(3, n);

j = 1 : (j1 - 1);
new_vertices(:, j) = old_vertices(:, j + 1);


j = j1 : (j2 - 1);
beta = (new_knot - old_knots(j)) ./ (old_knots(j + k - 1) - old_knots(j));
for i = 1 : 3
    new_vertices(i, j) = (1 - beta) .* old_vertices(i, j) + beta .* old_vertices(i, j + 1);
end

j = j2 : n
new_vertices(:, j) = old_vertices(:, j);

你可能感兴趣的:(算法,function,matlab,insert)