Exemplo n.º 1
0
def test_refinement():
    points_ori = np.array([
        [0, 0, 0],
        [1, 1, 0],
        [2, 0, 0],
        # [1,0,0]
    ])
    knot = np.array([0, 0, 0, 1, 1, 1])  # D1
    resolution = 500
    reso = np.linspace(0, 1, resolution)

    nr = gn.NURB(points_ori, [knot])
    points = nr.cpoints
    B = nr.B
    weight = nr.weight

    x, y, z = gn.engine.NURB_construction([knot], points, resolution, weight)
    basis_fun, derivatives = gn.NURB_engine.basis_function_array_nurbs(
        knot, 2, resolution, weight)
    fig = make_plot(B, basis_fun, reso, np.asarray([x, y]).T)

    knot_ins1 = np.asarray([0.5])
    points1, knot1 = gn.nurb.knot_insertion(B, [2], [knot], knot_ins1)

    basis_fun1, derivatives1 = gn.NURB_engine.basis_function_array_nurbs(
        knot1[0], 2, resolution, weight)
    x1, y1, z1 = gn.engine.NURB_construction(knot1, points1, resolution,
                                             weight)
    fig1 = make_plot(points1, basis_fun1, reso, np.asarray([x1, y1]).T)

    nr2 = gn.NURB(points1, knot1)
    points2 = nr2.cpoints
    B2 = nr2.B
    weight = nr.weight

    knot_ins2 = np.asarray([0.3])
    points2, knot2 = gn.nurb.knot_insertion(B2, [2], knot1, knot_ins2)

    basis_fun2, derivatives2 = gn.NURB_engine.basis_function_array_nurbs(
        knot2[0], 2, resolution, weight)
    x2, y2, z2 = gn.engine.NURB_construction(knot2, points2, resolution,
                                             weight)
    fig2 = make_plot(points2, basis_fun2, reso, np.asarray([x2, y2]).T)

    knot_ins3 = np.asarray([0.3, 0.5, 0.7])
    points3, knot3 = gn.nurb.knot_insertion(B, [2], [knot], knot_ins3)

    basis_fun3, derivatives3 = gn.NURB_engine.basis_function_array_nurbs(
        knot3[0], 2, resolution, weight)
    x3, y3, z3 = gn.engine.NURB_construction(knot3, points3, resolution,
                                             weight)
    fig3 = make_plot(points3, basis_fun3, reso, np.asarray([x3, y3]).T)

    save = True
    if save or save_all:
        fig.savefig(fig_folder + "h_refine_ori.pdf", **kwargs_savefig)
        fig1.savefig(fig_folder + "h_refine_1.pdf", **kwargs_savefig)
        fig2.savefig(fig_folder + "h_refine_2.pdf", **kwargs_savefig)
        fig3.savefig(fig_folder + "h_refine_3.pdf", **kwargs_savefig)
Exemplo n.º 2
0
def test_knot_refinement():
    points_ori = np.array([[0, 0, 0 ],  # , 0],
                               [2, 0.2, 0],  # 0],
                               [0, 1, 0],  # , 0],
                               [2, 1, 0],
                               [4, 2, 0],
                               [5, 1, 0],
                               [3, 0, 0]])  # , 0]])
    knot = np.array([0, 0, 0, 0.4, 0.6, 0.8, 0.8, 1, 1, 1])
    resolution = 500
    reso = np.linspace(0, 1, resolution)

    nr = gn.NURB(points_ori, [knot])
    points = nr.cpoints
    B=nr.B
    weight=nr.weight

    x, y, z = gn.engine.NURB_construction([knot],points, resolution, weight)
    basis_fun, derivatives = gn.NURB_engine.basis_function_array_nurbs(knot, 2, resolution, weight)
    fig = make_plot(B, [knot], basis_fun, reso, np.asarray([x, y]).T)

    knot_ins1 = np.asarray([0.5])
    points1, knot1 = gn.nurb.knot_insertion(B, [2], [knot], knot_ins1)

    basis_fun1, derivatives1 = gn.NURB_engine.basis_function_array_nurbs(knot1[0], 2, resolution, weight)
    x1, y1, z = gn.engine.NURB_construction(knot1, points1, resolution, weight)
    fig1 = make_plot(points1, knot1, basis_fun1, reso, np.asarray([x1, y1]).T)

    nr2 = gn.NURB(points1, knot1)
    points2 = nr2.cpoints
    B2 = nr2.B
    weight = nr2.weight

    knot_ins2 = np.asarray([0.3])
    points2, knot2 = gn.nurb.knot_insertion(B2, [2], knot1, knot_ins2)

    basis_fun2, derivatives2 = gn.NURB_engine.basis_function_array_nurbs(knot2[0], 2, resolution, weight)
    x2, y2, z = gn.engine.NURB_construction(knot2, points2, resolution, weight)
    fig2 = make_plot(points2,knot2, basis_fun2, reso, np.asarray([x2, y2]).T)

    nr3 = gn.NURB(points2, knot2)
    points3 = nr3.cpoints
    B3 = nr3.B
    weight = nr3.weight

    knot_ins3 = np.asarray([0.9])
    points3, knot3 = gn.nurb.knot_insertion(B3, [2], knot2, knot_ins3)

    basis_fun3, derivatives3 = gn.NURB_engine.basis_function_array_nurbs(knot3[0], 2, resolution, weight)
    x3, y3, z = gn.engine.NURB_construction(knot3, points3, resolution, weight)
    fig3 = make_plot(points3, knot3, basis_fun3, reso, np.asarray([x3, y3]).T)

    knot_ins4 = np.arange(0.05,1,0.1)
    points4, knot4 = gn.nurb.knot_insertion(B, [2], [knot], knot_ins4)

    basis_fun4, derivatives4 = gn.NURB_engine.basis_function_array_nurbs(knot4[0], 2, resolution, weight=None)
    x4, y4, z = gn.engine.NURB_construction(knot4, points4, resolution, weight)
    fig4 = make_plot(points4, knot4, basis_fun4, reso, np.asarray([x4, y4]).T)
Exemplo n.º 3
0
def test_degree_refinement():
    points_ori = np.array([[0, 0, 0],  # , 0],
                           [2, 0.2, 0],  # 0],
                           [0, 1, 0],  # , 0],
                           [2, 1, 0],
                           [4, 2, 0],
                           [5, 1, 0],
                           [3, 0, 0]])  # , 0]])
    knot = np.array([0, 0, 0, 0.4, 0.6, 0.8, 0.8, 1, 1, 1])
    resolution=500
    reso = np.linspace(0, 1, resolution)
    from pygeoiga.nurb.refinement import degree_elevation
    nr = gn.NURB(points_ori, [knot])
    B, knots = degree_elevation(nr.B, nr.knots, times=1)
    basis_fun, derivatives = gn.NURB_engine.basis_function_array_nurbs(knots[0], 3, resolution, weight=None)
    x, y, z = gn.engine.NURB_construction(knots, B, resolution, weight=None)
    fig3 = make_plot(B[...,:-1], knots, basis_fun, reso, np.asarray([x, y]).T)

    B, knots = degree_elevation(B, knots, times=1)
    basis_fun, derivatives = gn.NURB_engine.basis_function_array_nurbs(knots[0], 4, resolution, weight=None)
    x, y, z = gn.engine.NURB_construction(knots, B, resolution, weight=None)
    fig4 = make_plot(B[..., :-1], knots, basis_fun, reso, np.asarray([x, y]).T)

    B, knots = degree_elevation(B, knots, times=1)
    basis_fun, derivatives = gn.NURB_engine.basis_function_array_nurbs(knots[0], 5, resolution, weight=None)
    x, y, z = gn.engine.NURB_construction(knots, B, resolution, weight=None)
    fig5 = make_plot(B[..., :-1], knots, basis_fun, reso, np.asarray([x, y]).T)
    print(knots)
    #array([0. , 0. , 0. , 0. , 0. , 0. , 0.4, 0.4, 0.4, 0.4, 0.6, 0.6, 0.6,
    # 0.6, 0.8, 0.8, 0.8, 0.8, 0.8, 1. , 1. , 1. , 1. , 1. , 1. ]),
    save = True
    if save:
        fig3.savefig("3_degree.png", transparent=True)
        fig4.savefig("4_degree.png", transparent=True)
        fig5.savefig( "5_degree.png", transparent=True)
Exemplo n.º 4
0
def test_create2d():
    points2D = np.array([[[150, 0, 50], [150, 25, 50], [150, 50, 50],
                          [140, 50, 50], [140, 25, 50], [140, 0, 50]],
                         [[150, 0, 30], [150, 25, 30], [150, 50, 30],
                          [140, 50, 30], [140, 25, 30], [140, 0, 30]],
                         [[150, 0, 15], [150, 25, 15], [150, 50, 15],
                          [140, 50, 15], [140, 25, 15], [140, 0, 15]],
                         [[150, 0, 0], [150, 25, 0], [150, 50, 0],
                          [140, 50, 0], [140, 25, 0], [140, 0, 0]]])

    knot1 = np.array([0, 0, 0, 0, 1, 1, 1, 1])  # D1
    knot2 = np.array([0, 0, 0, 0.3, 0.5, 0.7, 1, 1, 1])  #D2
    #knot = np.asarray((knot1, knot2))
    weight2D = np.array([[[1], [1], [1], [1], [1], [1]],
                         [[1], [1], [1], [1], [1], [1]],
                         [[1], [1], [1], [1], [1], [1]],
                         [[1], [1], [1], [1], [1], [1]]])
    resolution = 30
    #nurb2 = gn.NURB(points2D,[knot1, knot2],weight2D, resolution=resolution, engine = "c++")
    #print(nurb2.model)
    #print(nurb2.degree, nurb2.dim)

    nurb3 = gn.NURB(points2D, [knot1, knot2],
                    weight2D,
                    resolution=resolution,
                    engine="python")
    print(nurb3.model)
    print(nurb3.degree, nurb3.dim)
Exemplo n.º 5
0
def test_create1d():
    points1D = np.array([[30, 0, 15], [30, 5, 15], [30, 10, 15], [25, 10, 15],
                         [25, 5, 15], [25, 0, 15]])
    knot10 = np.array([0, 0, 0, 0.3, 0.5, 0.7, 1, 1, 1])  #D1
    weight1D = np.array([[[1], [1], [1], [1], [1], [1]]])
    resolution = 30

    #nurb1 = gn.NURB(points1D, [knot10], weight1D, resolution=resolution, engine="c++")
    #print(nurb1.degree, nurb1.dim)

    nurb2 = gn.NURB(points1D, [knot10],
                    weight1D,
                    resolution=resolution,
                    engine="python")
    print(nurb2.degree, nurb2.dim)
Exemplo n.º 6
0
def test_create3d():
    points3D = np.array([
        [[[100, 0, 50], [100, 50, 50], [50, 50, 50], [45, 20, 50],
          [15, 20, 50], [15, 50, 50]],
         [[100, 0, 30], [100, 50, 30], [50, 50, 30], [45, 20, 30],
          [15, 20, 30], [15, 50, 30]],
         [[100, 0, 15], [100, 50, 15], [50, 50, 15], [45, 20, 15],
          [15, 20, 15], [15, 50, 15]],
         [[100, 0, 0], [100, 50, 0], [50, 50, 0], [45, 20, 0], [15, 20, 0],
          [15, 50, 0]]],
        [[[95, 0, 50], [95, 45, 50], [55, 45, 50], [50, 15, 50], [10, 15, 50],
          [10, 50, 50]],
         [[95, 0, 30], [95, 45, 30], [55, 45, 30], [50, 15, 30], [10, 15, 30],
          [10, 50, 30]],
         [[95, 0, 15], [95, 45, 15], [55, 45, 15], [50, 15, 15], [10, 15, 15],
          [10, 50, 15]],
         [[95, 0, 0], [95, 45, 0], [55, 45, 0], [50, 15, 0], [10, 15, 0],
          [10, 50, 0]]],
        [[[90, 0, 50], [90, 40, 50], [60, 40, 50], [55, 10, 50], [5, 10, 50],
          [5, 50, 50]],
         [[90, 0, 30], [90, 40, 30], [60, 40, 30], [55, 10, 30], [5, 10, 30],
          [5, 50, 30]],
         [[90, 0, 15], [90, 40, 15], [60, 40, 15], [55, 10, 15], [5, 10, 15],
          [5, 50, 15]],
         [[90, 0, 0], [90, 40, 0], [60, 40, 0], [55, 10, 0], [5, 10, 0],
          [5, 50, 0]]],
        [[[85, 0, 50], [85, 35, 50], [65, 35, 50], [60, 5, 50], [0, 5, 50],
          [0, 50, 50]],
         [[85, 0, 30], [85, 35, 30], [65, 35, 30], [60, 5, 30], [0, 5, 30],
          [0, 50, 30]],
         [[85, 0, 15], [85, 35, 15], [65, 35, 15], [60, 5, 15], [0, 5, 15],
          [0, 50, 15]],
         [[85, 0, 0], [85, 35, 0], [65, 35, 0], [60, 5, 0], [0, 5, 0],
          [0, 50, 0]]],
    ])
    knot11 = np.array([0, 0, 0, 0, 1, 1, 1, 1])  # D1
    knot22 = np.array([0, 0, 0, 0, 1, 1, 1, 1])  #D2
    knot33 = np.array([0, 0, 0, 0.3, 0.5, 0.7, 1, 1, 1])  # D3

    weight3D = np.ones(
        (points3D.shape[0], points3D.shape[1], points3D.shape[2], 1))

    knot = np.asarray((knot11, knot22, knot33))
    nurb3 = gn.NURB(points3D, knot, weight3D, engine="c++")
    print(nurb3.model)
    print(nurb3.degree, nurb3.dim)
Exemplo n.º 7
0
def test_NURBS_vs_Bspline():

    #control = np.array([[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1], [1, 0]])

    #weight = np.array(
    #   [[1], [1 / np.sqrt(2)], [1], [1 / np.sqrt(2)], [1], [1 / np.sqrt(2)], [1], [1 / np.sqrt(2)], [1]])

    #knot = np.array([0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4])  # D1
    control = np.array([[0, 0], [1, 0], [1, 1], [2, 1], [2, 0]])
    weight = np.array([[1], [0.1], [0.1], [0.1], [1]])

    knot = np.array([0, 0, 0, 0.5, 0.5, 1, 1, 1])  # D1
    resolution = 500
    resol = np.linspace(0, 1, resolution)

    spline_curve = gn.NURB(control, [knot], resolution=resolution)
    NURB_curve = gn.NURB(control, [knot], weight, resolution)

    dpi = 200
    figsize = (5, 3)
    fig, ax = plt.subplots(dpi=dpi, figsize=figsize)
    positions_Spline = np.asarray(spline_curve.model).T
    positions_NURBS = np.asarray(NURB_curve.model).T

    ax.plot(positions_NURBS[..., 0],
            positions_NURBS[..., 1],
            'b',
            label="NURBS curve")
    ax.plot(positions_Spline[..., 0],
            positions_Spline[..., 1],
            'g',
            label="B-spline curve")

    ax.plot(control[..., 0],
            control[..., 1],
            color='red',
            marker='s',
            linestyle='--')
    # ax.set_ylim(top=1)
    #ax.axis('off')
    #ax.set_aspect(0.5)
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    for i in range(len(control)):
        #if i==0: xytext=(-5, 0)
        #else:
        xytext = (5, 0)
        ax.annotate("$P_{%i}$" % i,
                    xy=(control[i, 0], control[i, 1]),
                    xytext=xytext,
                    textcoords="offset points",
                    fontsize=15)
    #fig.subplots_adjust(wspace=0, hspace=0)
    #fig.tight_layout()
    #ax.grid(linestyle="--")
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.legend()
    fig.show()

    #Basis-functions
    basis_fun_spline, derivatives_spline = gn.NURB_engine.basis_function_array_nurbs(
        knot, 2, resolution)
    basis_fun_NURBS, derivatives_NURBS = gn.NURB_engine.basis_function_array_nurbs(
        knot, 2, resolution, weight)

    fig2, (bas, bas2) = plt.subplots(1, 2, dpi=dpi, figsize=(15, 5))
    bas.plot(resol, basis_fun_spline)
    bas.spines["top"].set_visible(False)
    bas.spines["right"].set_visible(False)
    bas.set_xlim(0, 1)
    bas.set_ylim(0, 1)
    bas.set_xlabel("B-spline basis functions", fontsize=15)

    bas2.plot(resol, basis_fun_NURBS)
    bas2.spines["top"].set_visible(False)
    bas2.spines["right"].set_visible(False)
    #bas2.set_xticks([])
    bas2.set_xlim(0, 1)
    bas2.set_ylim(0, 1)
    bas2.set_xlabel(
        "NURBS basis functions, $w_{0} = w_{4} = 1$ and $w_{1} = w_{2} = w_{3}= 0.1$",
        fontsize=15)

    fig2.show()

    save = False
    if save or save_all:
        fig.savefig(fig_folder + "NURBS_vs_BSpline.pdf", **kwargs_savefig)
        fig2.savefig(fig_folder + "NURBS_vs_BSpline_basis_functions.pdf",
                     **kwargs_savefig)