Exemple #1
0
def Basis_Plot():
    #Fra oppgave: the order is p + 1, where p is the polynomial degree of the spline
    # p can maximally be number of repeated points + 1 ?
    # create a set of B-spline basis functions
    basis1 = sp.BSplineBasis(order=3, knots=[0, 0, 0, 1, 2, 3, 4, 4, 4])
    basis2 = sp.BSplineBasis(order=3, knots=[0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4])
    basis3 = sp.BSplineBasis(order=4, knots=[0, 0, 0, 0, 1, 2, 3, 4, 4, 4, 4])
    basis4 = sp.BSplineBasis(order=4, knots=[0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4])

    # 150 uniformly spaced evaluation points on the domain (0,4)
    t = np.linspace(0, 4, 150)

    # evaluate *all* basis functions on *all* points t. The returned variable B is a matrix
    B1 = basis1.evaluate(t)  # Bi.shape = (150,6), 150 visualization points, 6 basis functions
    B2 = basis2.evaluate(t)
    B3 = basis3.evaluate(t)
    B4 = basis4.evaluate(t)
    print(B1)
    # plot the basis functions

    plt.figure()

    plt.subplot(221)
    plt.title("[0, 0, 0, 1, 2, 3, 4, 4, 4]")
    plt.plot(t, B1)
    plt.axis('off')

    plt.subplot(222)
    plt.title("[0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4]")
    plt.plot(t, B2)
    plt.axis('off')

    plt.subplot(223)
    plt.title("[0, 0, 0, 0, 1, 2, 3, 4, 4, 4, 4]")
    plt.plot(t, B3)
    plt.axis('off')

    plt.subplot(224)
    plt.title("[0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4]")
    plt.plot(t, B4)
    plt.axis('off')
    """
    plt.plot(t, B1)
    plt.figure()
    plt.plot(t, B2)
    plt.figure()
    plt.plot(t, B3)
    plt.figure()
    plt.plot(t, B4)
    """
    plt.show()
def test(parm):
    bb = BsplineBasis()
    bb.knots = [0., 0., 0., 0., 1., 2., 3., 3., 3., 3.]
    bb.degree = 3
    # parm = 2.5

    span = bb.find_span(parm)
    print("Span index : %d" % span)
    f0 = bb.evaluate(parm, d=0)
    f1 = bb.evaluate(parm, d=1)
    f2 = bb.evaluate(parm, d=2)
    print("Basis functions    : %r" % f0)
    print("First derivatives  : %r" % f1)
    print("Second derivatives : %r" % f2)

    # compare to splipy results
    try:
        import splipy
    except ImportError:
        print("splipy is not installed.")
        return False

    basis1 = splipy.BSplineBasis(order=bb.degree + 1, knots=bb.knots)

    print("splipy results :")
    print(basis1.evaluate(parm, d=0).A1.tolist())
    print(basis1.evaluate(parm, d=1).A1.tolist())
    print(basis1.evaluate(parm, d=2).A1.tolist())
def Prepare_Data(T, p): # (knot vector, degree of polynomial)

    # 0) Prepare knot vector
    if (len(T) - p - 1) % 2: # n=len(T)-p-1. We add a knot if n is not even
        T = np.concatenate([T[:p+1],np.array([(T[p]+T[p+1])/2]),T[p+1:]],axis=0)
    n = int((len(T) - p - 1) / 2)

    # 1) Calculate exact integrals
    integrals_c = np.zeros(2*n)
    for i in range(2*n):
        integrals_c[i] = (T[i+p+1] - T[i])/(p+1)

    # 2) Generate initial xi and w
    xi = np.zeros(n)
    tau_abscissa = np.zeros(2*n)
    for i in range(2*n):
        tau_abscissa[i] = sum(T[i+1:i+p+1])/p
    for i in range(n):
        xi[i] = (tau_abscissa[2 * i] + tau_abscissa[2 * i + 1]) / 2

    w = np.zeros(n)
    for i in range(n):
        w[i] = integrals_c[2*i] + integrals_c[2*i+1]

    # 3) Generate basis functions, evaluate and get partial derivatives of F
    basis = spl.BSplineBasis(order=p + 1, knots=T)

    return basis, integrals_c, w, xi, n
 def get_spline_points(cont_pts, order = 4, sampling = 150):
     n_control_points = len(cont_pts)
     kn = [0] * order + list(range(1, n_control_points - order + 1)) + [n_control_points - order + 1] * order
     basis = sp.BSplineBasis(order=order, knots=kn)
     curve = sp.Curve(basis, cont_pts)
     t = np.linspace(0,n_control_points - order + 1,sampling)
     x = curve.evaluate(t)
     return x
Exemple #5
0
def Prepare_Data(T, p):  # T is knot vector, p is degree of polynomial
    # 0) Prepare knot vector
    if (
            len(T) - p - 1
    ) % 2:  # n=len(T)-p-1. We add a knot in the knot vector if n is not even
        new_T = T[:p + 1]
        new_T.append((T[p] + T[p + 1]) / 2)
        new_T += T[p + 1:]
        T = new_T
    print("T =", T)
    n = int((len(T) - p - 1) / 2)
    print("2n =", 2 * n)

    # 1) Calculate exact integrals
    integrals_c = np.zeros(2 * n)
    for i in range(2 * n):
        integrals_c[i] = (T[i + p + 1] - T[i]) / (p + 1)
    print("integrals_c", integrals_c)
    # Ser bra ut. De basisfunksjonene som er helt inneholdt i intervallet integreres til 1, de mot kantene har deler som 'ligger utenfor'

    # 2) Generate initial xi and w
    xi = np.zeros(n)
    tau_abscissa = np.zeros(2 * n)
    for i in range(2 * n):
        tau_abscissa[i] = sum(T[i + 1:i + p + 1]) / p
    for i in range(n):
        xi[i] = (tau_abscissa[2 * i] + tau_abscissa[2 * i + 1]) / 2
    print("xi", xi)

    w = np.zeros(n)
    for i in range(n):
        w[i] = integrals_c[2 * i] + integrals_c[2 * i + 1]
    print("w", w)

    # 3) Generate basis functions, evaluer og finn partiellderiverte av F
    basis = spl.BSplineBasis(order=p + 1, knots=T)
    print("basis", basis)
    return basis, integrals_c, w, xi, n