<jupyter_start><jupyter_text>B-Splines curves The B-spline curve in $\mathbb{R}^d$ associated to $T=(t_i)_{0\leqslant i \leqslant n + p + 1}$ and control points $(\mathbf{P}_i)_{ 0 \leqslant i \leqslant n}$ is defined by :$$\mathcal{C}(t) = \sum_{i=0}^n N_i^p(t) \textbf{P}_i$$  Examples<jupyter_code>def example_1():    
    knots  = [0., 0., 0., 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [1., 1., 0.]
    P[:, 1] = [0., 1., 1.]    
    
    nx = 101
    xs = np.linspace(0., 1., nx)
    
    Q = np.zeros((nx, 2))
    for i,x in enumerate(xs):
        Q[i,:] = point_on_bspline_curve(knots, P, x)

    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-0.2, 1.2, -0.2, 1.2])

    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')    

example_1()
def example_2():    
    knots  = [0., 0., 0., 0., 1., 1., 1., 1.]
    degree = 3
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [1., 1., 0., -1.]
    P[:, 1] = [0., 1., 1.,  0.]  
    
    nx = 101
    xs = np.linspace(0., 1., nx)
    
    Q = np.zeros((nx, 2))
    for i,x in enumerate(xs):
        Q[i,:] = point_on_bspline_curve(knots, P, x)

    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-1.2, 1.2, -0.2, 1.2])

    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')    

example_2()
def example_3():    
    knots  = [0., 0., 0., 0.25, 0.5, 0.75, 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [0.,  1., 1., -2., -1., -2.]
    P[:, 1] = [.5, -1., 1.,  1.,  0., -1.]   
    
    nx = 101
    xs = np.linspace(0., 1., nx)
    
    Q = np.zeros((nx, 2))
    for i,x in enumerate(xs):
        Q[i,:] = point_on_bspline_curve(knots, P, x)

    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-2.2, 1.3, -1.2, 1.4])

    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')    

example_3()
def example_4():    
    knots  = [0., 0., 0., 0.25, 0.5, 0.5, 0.75, 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [0.,  1., 1., 0., -2., -1., -2.]
    P[:, 1] = [.5, -1., 1., 2.,  1.,  0., -1.]   
    
    nx = 101
    xs = np.linspace(0., 1., nx)
    
    Q = np.zeros((nx, 2))
    for i,x in enumerate(xs):
        Q[i,:] = point_on_bspline_curve(knots, P, x)

    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-2.2, 1.3, -1.2, 2.2])

    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')    

example_4()
def example_5():    
    knots  = [0., 0., 0., 0.25, 0.5, 0.5, 0.75, 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [0.,  1., 1., 0., -2., -1., -2.]
    P[:, 1] = [.5, -1., 1., 1.,  1.,  0., -1.]   
    
    nx = 101
    xs = np.linspace(0., 1., nx)
    
    Q = np.zeros((nx, 2))
    for i,x in enumerate(xs):
        Q[i,:] = point_on_bspline_curve(knots, P, x)

    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-2.2, 1.3, -1.2, 1.2])

    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')    

example_5()
def example_6a():    
    knots  = [0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1.]
    degree = 6
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [7., 6., 5.,  4., 3., 2., 1.]
    P[:, 1] = [0., 1., 1., -1., .5, -.5, 0.]   
    
    nx = 101
    xs = np.linspace(0., 1., nx)
    
    Q = np.zeros((nx, 2))
    for i,x in enumerate(xs):
        Q[i,:] = point_on_bspline_curve(knots, P, x)

    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([0.6, 7.4, -1.4, 1.4])

    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')    

example_6a()
def example_6b():    
    knots  = [0., 0., 0., .2, .4, .6, .8, 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [7., 6., 5.,  4., 3., 2., 1.]
    P[:, 1] = [0., 1., 1., -1., .5, -.5, 0.]   
    
    nx = 101
    xs = np.linspace(0., 1., nx)
    
    Q = np.zeros((nx, 2))
    for i,x in enumerate(xs):
        Q[i,:] = point_on_bspline_curve(knots, P, x)

    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([0.6, 7.4, -1.4, 1.4])

    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')    

example_6b()<jupyter_output><empty_output><jupyter_text>Rational B-Splines (NURBS) curves We recall that NURBS are defined as$$  R_i^p(t) := \frac{w_i N_i^p(t)}{\sum_{j=0}^n w_j N_j^p(t)}$$where $w_i > 0, \forall i \in \left[0, n \right]$ are non-negative real numbers, called the weights.NURBS curves are then defined as$$  \mathcal{C}(t) = \sum_{i=0}^n \mathbf{P}_i R_i^p(t) $$Notice that a NURBS curve in $\mathbb{R}^d$ can be described as a NURBS curve in $\mathbb{R}^{d+1}$ using the control points:$\textbf{P}^{\omega}_i = ( \omega_i \textbf{P}_i, \omega_i )$This remark is used for the evaluation and also to extend most of the B-Splines fundamental geometric operations to NURBS curves.**Remark**: Although there is a function **point_on_nurbs_curve** to evaluate NURBS curves, we make use of an explicit call to the spline version in this notebook. **Example 1.** (Quarter circle)<jupyter_code>def example_1():   
    
    knots  = [0., 0., 0., 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [1., 1., 0.]
    P[:, 1] = [0., 1., 1.]
    
    # weights
    s2 = 1./np.sqrt(2)
    W = np.zeros(n)
    W[:] = [1., s2, 1.]
    
    # weithed control points in 3D
    Pw = np.zeros((n,3))
    for i in range(0, n):
        Pw[i,:2] = W[i]*P[i,:]
        Pw[i,2]  = W[i]
    
    nx = 200
    xs = np.linspace(0., 1., nx)    
    
    Qw = np.zeros((nx, 3))
    for i,x in enumerate(xs):
        Qw[i,:] = point_on_bspline_curve(knots, Pw, x)
   
    Q = np.zeros((nx, 2))
    Q[:,0] = Qw[:,0]/Qw[:,2]
    Q[:,1] = Qw[:,1]/Qw[:,2]
    
    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-0.2, 1.2, -0.2, 1.2])    
    
    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')
   
example_1()<jupyter_output><empty_output><jupyter_text>**Example 2.** (Circular arc of $120°$)<jupyter_code>def example_2():   
    
    knots  = [0., 0., 0., 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    a = np.cos(np.pi/6.)
    P[:, 0] = [ a, 0., -a]
    P[:, 1] = [.5, 2., .5]
    
    # weights
    W = np.zeros(n)
    W[:] = [1., 1./2., 1.]
    
    # weithed control points in 3D
    Pw = np.zeros((n,3))
    for i in range(0, n):
        Pw[i,:2] = W[i]*P[i,:]
        Pw[i,2]  = W[i]
    
    nx = 200
    xs = np.linspace(0., 1., nx)    
    
    Qw = np.zeros((nx, 3))
    for i,x in enumerate(xs):
        Qw[i,:] = point_on_bspline_curve(knots, Pw, x)
   
    Q = np.zeros((nx, 2))
    Q[:,0] = Qw[:,0]/Qw[:,2]
    Q[:,1] = Qw[:,1]/Qw[:,2]
    
    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-1.2, 1.2, .0, 2.2])    
    
    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')
   
example_2()<jupyter_output><empty_output><jupyter_text>**Example 3.** (half circle)<jupyter_code>def example_3():   
    
    knots  = [0., 0., 0., 0., 1., 1., 1., 1.]
    degree = 3
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [1., 1., -1., -1.]
    P[:, 1] = [0., 2.,  2.,  0.]
    
    # weights
    W = np.zeros(n)
    W[:] = [1., 1./3., 1./3., 1.]
    
    # weithed control points in 3D
    Pw = np.zeros((n,3))
    for i in range(0, n):
        Pw[i,:2] = W[i]*P[i,:]
        Pw[i,2]  = W[i]
    
    nx = 200
    xs = np.linspace(0., 1., nx)    
    
    Qw = np.zeros((nx, 3))
    for i,x in enumerate(xs):
        Qw[i,:] = point_on_bspline_curve(knots, Pw, x)
   
    Q = np.zeros((nx, 2))
    Q[:,0] = Qw[:,0]/Qw[:,2]
    Q[:,1] = Qw[:,1]/Qw[:,2]
    
    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(0, n):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')

    plt.axis([-1.2, 1.2, -.2, 2.2])    
    
    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')
   
example_3()<jupyter_output><empty_output><jupyter_text>**Example 4.** (Circle as 4 arcs)We already saw how to construct a circular arc (with angle = $\frac{\pi}{2}$, using Bézier curves.Constructing a circular arc can be done in different ways. In this example, we consider a B-Spline curve as a composite of 4 Bézier curves.<jupyter_code>def example_4():   
    
    knots  = [0., 0., 0., .25, .25, .5, .5, .75, .75, 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    P[:, 0] = [1., 1., 0., -1., -1., -1.,  0.,  1., 1.]
    P[:, 1] = [0., 1., 1.,  1.,  0., -1., -1., -1., 0.]
    
    # weights
    s2 = 1./np.sqrt(2)
    W = np.zeros(n)
    W[:] = [1., s2, 1., s2, 1., s2, 1., s2, 1.]
    
    # weithed control points in 3D
    Pw = np.zeros((n,3))
    for i in range(0, n):
        Pw[i,:2] = W[i]*P[i,:]
        Pw[i,2]  = W[i]
    
    nx = 200
    xs = np.linspace(0., 1., nx)    
    
    Qw = np.zeros((nx, 3))
    for i,x in enumerate(xs):
        Qw[i,:] = point_on_bspline_curve(knots, Pw, x)
   
    Q = np.zeros((nx, 2))
    Q[:,0] = Qw[:,0]/Qw[:,2]
    Q[:,1] = Qw[:,1]/Qw[:,2]
    
    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(1, n-1):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')
        
    # plot P_0 = P_8
    x,y = P[0,:]
    plt.text(x+0.05,y+0.05,'$\mathbf{P}_0 = \mathbf{P}_8$')

    plt.axis([-1.2, 1.2, -1.2, 1.2])    
    
    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')
   
example_4()<jupyter_output><empty_output><jupyter_text>**Example 5.** (Circle as 3 arcs)We already saw how to construct a circular arc (with angle = $120°$, using Bézier curves.In this example, we consider a B-Spline curve as a composite of 3 Bézier curves.<jupyter_code>def example_5():   
    
    knots  = [0., 0., 0., 1./3., 1./3., 2./3., 2./3., 1., 1., 1.]
    degree = 2
    n      = len(knots) - degree - 1
    
    P = np.zeros((n, 2))
    a = np.cos(np.pi/6.)
    P[:, 0] = [ a, 0., -a, -2*a,  0., 2*a,  a]
    P[:, 1] = [.5, 2., .5,  -1., -1., -1., .5]
    
    # weights
    W = np.zeros(n)
    W[:] = [1., 1./2., 1., 1./2., 1., 1./2., 1.]
    
    # weithed control points in 3D
    Pw = np.zeros((n,3))
    for i in range(0, n):
        Pw[i,:2] = W[i]*P[i,:]
        Pw[i,2]  = W[i]
    
    nx = 200
    xs = np.linspace(0., 1., nx)    
    
    Qw = np.zeros((nx, 3))
    for i,x in enumerate(xs):
        Qw[i,:] = point_on_bspline_curve(knots, Pw, x)
   
    Q = np.zeros((nx, 2))
    Q[:,0] = Qw[:,0]/Qw[:,2]
    Q[:,1] = Qw[:,1]/Qw[:,2]
    
    plt.plot(Q[:,0], Q[:,1], '-b')
    plt.plot(P[:,0], P[:,1], '--ok', linewidth=0.7)
    
    for i in range(1, n-1):
        x,y = P[i,:]
        plt.text(x+0.05,y+0.05,'$\mathbf{P}_{' + str(i) + '}$')
        
    # plot P_0 = P_6
    x,y = P[0,:]
    plt.text(x+0.05,y+0.05,'$\mathbf{P}_0 = \mathbf{P}_8$')

    plt.axis([-2.2, 2.2, -1.2, 2.2])    
    
    #ax = plt.axes()
    #ax.set_aspect('equal', 'box')
   
example_5()
# css style
from IPython.core.display import HTML
def css_styling():
    styles = open("../styles/custom.css", "r").read()
    return HTML(styles)
css_styling()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.25506517983351656, 'fraction_numerical': 0.06423747447777603, 'mean_word_length': 1.8838315217391304, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '240189', 'license': 'MIT', 'n_tokens_mistral': 6397, 'n_tokens_neox': 6030, 'n_words': 1357, 'path': 'cad/B-Splines_curves.ipynb', 'repo_name': 'ratnania/MA5938', 'size': '12734'}