Пример #1
0
def figure_10c(**kwargs):
    fig = utils.get_figure(.425, aspect_ratio=1 / utils.golden_ratio)

    # ax_upper = fig.add_subplot(211)
    # ax_lower = fig.add_subplot(212)

    ax_upper = fig.add_axes([.2, .6, .75, .25])
    ax_lower = fig.add_axes([.2, .1, .75, .25])

    axes = (ax_upper, ax_lower)

    basis_upper = BSplineBasis([0, 0, 0, 1, 1, 1], polynomial_order=2)
    basis_lower = BSplineBasis([0, 0, 0, 1 / 3, 2 / 3, 1, 1, 1],
                               polynomial_order=2)
    bases = (basis_upper, basis_lower)

    titles = (
        r"$\Xi' = \left\lbrace 0, 0, 0, 1, 1, 1 \right\rbrace, \, p = 2$",
        r"$\Xi'' = \left\lbrace 0, 0, 0, \frac{1}{3}, \frac{2}{3}, 1, 1, 1 \right\rbrace, \, p = 2$"
    )

    method = (r'$p$-refinement \\ (order elevation)',
              r'$h$-refinement \\ (knot insertion)')

    for ax, basis, title, method in zip(axes, bases, titles, method):
        basis.attach_basis_functions(ax)

        title_size = 10
        ax.set_ylabel(r'$N_{i, \,p}\left(\xi\right)$', fontsize=title_size)
        ax.set_xlabel(r'$\xi$', fontsize=title_size)
        ax.set_title(title, fontsize=title_size)
        ax.set_xlim(0, 1.0)
        ax.set_ylim(0, 1.0)
        ax.grid(True, **GRID_KWARGS)

        ax.annotate(
            s=method,
            xy=(0.5, 1.275),
            xycoords='axes fraction',
            xytext=(10, 8),
            textcoords='offset points',
            fontsize=title_size,
        )

        ax.annotate(s='',
                    xy=(0.5, 1.275),
                    xycoords='axes fraction',
                    xytext=(0, 25),
                    textcoords='offset points',
                    arrowprops=dict(
                        width=1,
                        facecolor='black',
                        headlength=10,
                    ))

    # plt.tight_layout()

    utils.save_current_figure('fig_10c', tight=False, **kwargs)

    plt.close()
Пример #2
0
    def plot_curve_2D(self, fig_scale='full', **kwargs):
        """Only works in 2D..."""
        fig = utils.get_figure(fig_scale)
        ax = fig.add_subplot(111)

        self.attach_curve_2D(ax)

        utils.save_current_figure(**kwargs)

        plt.close()
Пример #3
0
def figure_10a(**kwargs):
    fig = utils.get_figure('half')
    ax = fig.add_subplot(111)

    basis = BSplineBasis([0, 0, 1, 1], polynomial_order=1)

    basis.attach_basis_functions(ax)

    title_size = 10
    ax.set_xlabel(r'$\xi$', fontsize=title_size)
    ax.set_ylabel(r'$N_{i, \,p}\left(\xi\right)$', fontsize=title_size)
    ax.set_title(r'$\Xi = \left\lbrace 0, 0, 1, 1 \right\rbrace, \, p = 1$',
                 fontsize=title_size)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.grid(True, **GRID_KWARGS)

    utils.save_current_figure('fig_10a', **kwargs)

    plt.close()
Пример #4
0
    def plot_basis_functions(self,
                             fig_scale='full',
                             title=True,
                             legend_on_right=True,
                             **kwargs):
        fig = utils.get_figure(fig_scale)
        ax = fig.add_subplot(111)

        self.attach_basis_functions(ax)

        ax.set_xlim(self.xi_min, self.xi_max)
        ax.set_ylim(0, 1.01)

        ax.set_xlabel(r'$\xi$', fontsize=12)
        ax.set_ylabel(r'$N_{i,p}(\xi)$', fontsize=12)

        if title:
            ax.set_title(
                fr'Basis Functions for $\Xi = \left\lbrace {",".join(str(s) for s in self.knot_vector)} \right\rbrace$, $p = {self.polynomial_order}$'
            )

        if legend_on_right:
            ax.legend(bbox_to_anchor=(1.02, 1),
                      loc='upper left',
                      borderaxespad=0.,
                      fontsize=12,
                      handlelength=1,
                      ncol=1 + (len(self.basis_function_indices) // 15))
        else:
            ax.legend(loc='upper right', handlelength=1)

        ax.grid(True, **GRID_KWARGS)

        utils.save_current_figure(name=self.name + '__basis', **kwargs)

        plt.close()
Пример #5
0
def figure_9(**kwargs):
    fig = utils.get_figure('full', aspect_ratio=.8)

    ax_original_curve = fig.add_subplot(221)
    ax_original_basis = fig.add_subplot(223)
    ax_new_curve = fig.add_subplot(222)
    ax_new_basis = fig.add_subplot(224)

    original_basis = BSplineBasis([0, 0, 0, 1, 1, 1], polynomial_order=2)
    original_curve = BSplineCurve(original_basis, [(0, 0), (.5, 1), (1, 0)])

    title_size = 10
    ax_original_curve.set_title(
        r'Original Curve: $\Xi = \left\lbrace0, 0, 0, 1, 1, 1\right\rbrace, \; p = 2$',
        fontsize=title_size)
    ax_original_basis.set_title(r'Original Basis Functions',
                                fontsize=title_size)
    ax_new_curve.set_title(
        r"$p$-Refined Curve: $\Xi' = \left\lbrace0, 0, 0, 0, 1, 1, 1, 1\right\rbrace, \; p = 3$",
        fontsize=title_size)
    ax_new_basis.set_title(r'$p$-Refined Basis Functions', fontsize=title_size)

    new_basis = BSplineBasis([0, 0, 0, 0, 1, 1, 1, 1], polynomial_order=3)
    new_curve = BSplineCurve(new_basis, [(0, 0), (1 / 3, 2 / 3),
                                         (2 / 3, 2 / 3), (1, 0)])

    for ax, basis in ((ax_original_curve, original_curve), (ax_new_curve,
                                                            new_curve)):
        basis.attach_curve_2D(ax)
        ax.set_xlim(-.05, 1.05)
        ax.set_ylim(-.05, 1.05)
        ax.grid(True, linewidth=.5)
        ax.axis('on')

        ax.set_xticks((0, 1 / 3, 2 / 3, 1))
        ax.set_xticklabels((
            r'$0$',
            r'$\frac{1}{3}$',
            r'$\frac{2}{3}$',
            r'$1$',
        ))
        ax.set_yticks((0, 2 / 3, 1))
        ax.set_yticklabels((
            r'$0$',
            r'$\frac{2}{3}$',
            r'$1$',
        ))

    for ax, basis in ((ax_original_basis, original_basis), (ax_new_basis,
                                                            new_basis)):
        basis.attach_basis_functions(ax)
        ax.set_xlim(0, 1.0)
        ax.set_ylim(0, 1.0)
        ax.set_xlabel(r'$\xi$')
        ax.grid(True, **GRID_KWARGS)

    ax_original_basis.set_ylabel(r'$N_{i, \,p}\left(\xi\right)$')

    plt.tight_layout()

    utils.save_current_figure('fig_9', **kwargs)

    plt.close()
Пример #6
0
    def plot_surface_3D(self, length=30, fps=30, **kwargs):
        """Only works in 3D..."""
        fig = utils.get_figure(scale=3)
        ax = fig.add_subplot(111, projection='3d')

        # surface_x = self.xi_1_mesh
        # surface_y = self.xi_2_mesh
        # surface_x, surface_y, surface_z = self.surface()
        xyz = self.surface()

        # surface_x, surface_y = np.meshgrid(surface_x, surface_y)

        # print(np.shape(surface_x))
        # print(np.shape(surface_y))
        # print(np.shape(surface_z))

        control_points_x = np.array(
            [control_point[0] for control_point in self.control_net.values()])
        control_points_y = np.array(
            [control_point[1] for control_point in self.control_net.values()])
        control_points_z = np.array(
            [control_point[2] for control_point in self.control_net.values()])

        # x_min = min(np.min(surface_x), np.min(control_points_x))
        # x_max = max(np.max(surface_x), np.max(control_points_x))
        # x_range = np.abs(x_max - x_min)
        #
        # y_min = min(np.min(surface_y), np.min(control_points_y))
        # y_max = max(np.max(surface_y), np.max(control_points_y))
        # y_range = np.abs(y_max - y_min)
        #
        # z_min = min(np.min(surface_z), np.min(control_points_z))
        # z_max = max(np.max(surface_z), np.max(control_points_z))
        # z_range = np.abs(z_max - z_min)
        #
        # ax.set_xlim(x_min - 0.05 * x_range, x_max + 0.05 * x_range)
        # ax.set_ylim(y_min - 0.05 * y_range, y_max + 0.05 * y_range)
        # ax.set_zlim(z_min - 0.05 * z_range, z_max + 0.05 * z_range)

        ax.scatter(control_points_x,
                   control_points_y,
                   control_points_z,
                   depthshade=False,
                   **CONTROL_POLYGON_KWARGS)

        # print(np.max(surface_x), np.max(surface_y), np.max(surface_z))
        # print(np.min(surface_x), np.min(surface_y), np.min(surface_z))
        # print(surface_x)
        # print(surface_y)
        # print(surface_z)
        xyz = np.reshape(xyz, (-1, 3))
        print(xyz.shape)
        x, y, z = xyz[:, 0], xyz[:, 1], xyz[:, 2]
        ax.scatter(x, y, z)
        # ax.plot_trisurf(
        #     x, y, z,
        #     cmap = plt.get_cmap('viridis'),
        #     linewidth = 0,
        #     antialiased = True,
        # )
        # ax.plot_surface(surface_x, surface_y, surface_z, rstride = 1, cstride = 1)
        # ax.plot_trisurf(surface_x, surface_y, surface_z)
        # ax.plot_trisurf(surface_x, surface_y, surface_z, **CURVE_KWARGS)

        ax.axis('off')

        ax.view_init(
            elev=45, azim=0
        )  # note that this resets ax.dist to 10, so we can't use it below
        ax.dist = 7.5  # default is 10, so zoom in a little because there's no axis to take up the rest of the space

        plt.show()
        utils.save_current_figure(**kwargs)

        ### ANIMATION ###

        frames = length * fps

        writer = anim.writers['ffmpeg'](
            fps=fps, bitrate=2000)  # don't need a very high bitrate

        def animate(frame):
            print(frame, frames, frame / frames)
            ax.azim = 360 * frame / frames  # one full rotation
            return [
            ]  # must return the list of artists we modified (i.e., nothing, since all we did is rotate the view)

        ani = anim.FuncAnimation(fig, animate, frames=frames, blit=True)
        ani.save(f"{os.path.join(kwargs['target_dir'], kwargs['name'])}.mp4",
                 writer=writer)

        plt.close()