def coarsen_poly(self, plot=True):

        node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                        dtype=np.float)
        cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                        dtype=np.int)
        mesh = TriangleMesh(node, cell)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)
        mesh.init_level_info()

        isMarkedCell = np.array([0, 0, 0, 1, 0], dtype=np.bool_)
        mesh.refine_poly(isMarkedCell)

        NC = mesh.number_of_all_cells()
        isMarkedCell = np.zeros(NC, dtype=np.bool_)
        isMarkedCell[[1, 2, 3, 4, 5, 6]] = True
        mesh.coarsen_poly(isMarkedCell)

        fig = plt.figure()
        axes = fig.gca()
        mesh.add_plot(axes)
        mesh.find_node(axes, showindex=True)
        mesh.find_cell(axes, showindex=True)
        mesh.add_halfedge_plot(axes, showindex=True)
        plt.show()
Exemple #2
0
    def coarsen_tri(self, maxit=2, method='rg', plot=True, rb=True):
        cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                        dtype=np.int)
        node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                        dtype=np.float)

        if True:
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            isMarkedCell = np.array([0, 1, 0, 0, 1], dtype=np.bool_)
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            mesh.hedgecolor = color
            mesh.refine_triangle_rg(isMarkedCell)
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            if 0:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                mesh.add_halfedge_plot(axes, showindex=True)
                mesh.find_node(axes, showindex=True)
                mesh.find_cell(axes, showindex=True)
                plt.show()
            if method == 'rg':
                if 1:
                    NC = mesh.number_of_all_cells()
                    isMarkedCell = np.zeros(NC, dtype=np.bool_)
                    isMarkedCell[[1, 2, 5, 6, 7, 9]] = True
                    print('*************lll*********')
                    mesh.coarsen_triangle_rg(isMarkedCell)
                if 1:
                    NC = mesh.number_of_all_cells()
                    isMarkedCell = np.zeros(NC, dtype=np.bool_)
                    isMarkedCell[[0, 1, 2, 3, 4, 5, 6, 7]] = True
                    print('*************lll*********')
                    mesh.coarsen_triangle_rg(isMarkedCell)
                    NC = mesh.number_of_all_cells()
                    isMarkedCell = np.ones(NC, dtype=np.bool_)
                    print('*************lll*********')
                    mesh.refine_triangle_rg(isMarkedCell)

            else:
                color[[2, 3, 10, 11]] = 1
                mesh.hedgecolor = color
                isMarkedCell = np.array([0, 1, 1, 0, 0], dtype=np.bool_)
                #isMarkedCell = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ,0],
                #       dtype=np.bool_)
                mesh.refine_triangle_nvb(isMarkedCell)
                mesh.print()
            if plot:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                mesh.add_halfedge_plot(axes, showindex=True)
                mesh.find_node(axes, showindex=True)
                mesh.find_cell(axes, showindex=True)
                plt.show()
 def triMesh(self, method='rg'):
     cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                     dtype=np.int)
     node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                     dtype=np.float)
     mesh = TriangleMesh(node, cell)
     mesh = HalfEdgeMesh2d.from_mesh(mesh)
     mesh.init_level_info()
     if method == 'rg':
         halfedge = mesh.ds.halfedge
         cstart = mesh.ds.cellstart
         NE = mesh.ds.NE
         color = np.zeros(NE * 2, dtype=np.int_)
         mesh.hedgecolor = color
     elif method == 'nvb':
         halfedge = mesh.ds.halfedge
         cstart = mesh.ds.cellstart
         NE = mesh.ds.NE
         color = np.zeros(NE * 2, dtype=np.int_)
         color[[2, 3, 10, 11]] = 1
         mesh.hedgecolor = color
     return mesh
    def refine_tri(self, maxit=2, method='rg', plot=True, rb=True):
        cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                        dtype=np.int)
        node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                        dtype=np.float)

        if False:
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh.from_mesh(mesh)
            mesh.ds.cell2hedge = np.array([0, 3, 2, 11, 10])
            isMarkedCell = np.array([0, 1, 0, 0, 1], dtype=np.bool_)
            #mesh.refine_triangle_rbg(isMarkedCell)

            mesh.ds.NV = 3
            cell = mesh.ds.cell_to_node()
            node = mesh.entity('node')

            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            if False:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                mesh.add_halfedge_plot(axes, showindex=True)
                mesh.find_node(axes, showindex=True)
                mesh.find_cell(axes, showindex=True)
                plt.show()

            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            if method == 'rg':
                color[[4, 13, 17, 28]] = 1
                color[[23, 27]] = 2
                color[[22, 26]] = 3
                mesh.hedgecolor = color
                isMarkedCell = np.array(
                    [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool_)
                mesh.refine_triangle_rg(isMarkedCell)
            else:
                color[[2, 3, 10, 11]] = 1
                mesh.hedgecolor = color
                isMarkedCell = np.array([0, 1, 1, 0, 0], dtype=np.bool_)
                #isMarkedCell = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ,0],
                #       dtype=np.bool_)
                mesh.refine_triangle_nvb(isMarkedCell)
                mesh.print()
            if plot:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                mesh.add_halfedge_plot(axes, showindex=True)
                mesh.find_node(axes, showindex=True)
                mesh.find_cell(axes, showindex=True)
                plt.show()
        if True:
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            if method == 'nvb':
                color[[2, 3, 10, 11]] = 1
            mesh.hedgecolor = color
            c = np.array([0.8, 0.8])
            r = 0.9
            h = 1e-2
            k = 0
            NB = 0
            start = time.time()
            while k < maxit:
                halfedge = mesh.ds.halfedge
                halfedge1 = halfedge[:, 3]
                node = mesh.node
                flag = node - c
                flag = flag[:, 0]**2 + flag[:, 1]**2
                flag = flag <= r**2
                flag1 = flag[halfedge[:, 0]].astype(int)
                flag2 = flag[halfedge[halfedge1, 0]].astype(int)
                markedge = flag1 + flag2 == 1
                markedcell = halfedge[markedge, 1]
                markedcell = np.unique(markedcell)
                cell = np.unique(halfedge[:, 1])
                nc = cell.shape[0]
                markedcell1 = np.zeros(nc)
                markedcell1[markedcell] = 1
                if method == 'rg':
                    mesh.refine_triangle_rg(markedcell1.astype(np.bool_))
                else:
                    mesh.refine_triangle_nvb(markedcell1.astype(np.bool_))
                k += 1
                print('循环', k, '次***************************')
            end = time.time()
            print('用时', end - start)
            if plot:
                fig = plt.figure()
                axes = fig.gca()
                nindex = mesh.nodedata['level']
                mesh.add_plot(axes)
                #mesh.add_halfedge_plot(axes, showindex=True)
                #mesh.find_node(axes, showindex=True, multiindex=nindex)
                #mesh.find_cell(axes, showindex=True)
                #print(np.c_[np.arange(len(mesh.hedgecolor)), mesh.hedgecolor])
                plt.show()