利用Python读取三角形网格数据并图形化显示

前言

利用有限元方法计算一些数值问题,经常需要剖网格,得到的网格拓扑信息,如果能图形化显示对我们Debug程序也方便,下面利用Python语言读取文本中的网格拓扑信息,然后图形化显示,且可以选择是否显示节点或者单元编号信息。具体代码和数据可以去我的 Github下载。

数据格式

1. 节点数据格式

id_1 x1 y1
id_2 x2 y2

For Example (Node.txt)
1 0 0
2 5 0
3 5 4
4 0 4
5 0 2
6 1 1
7 2 1
8 2 2
9 1 2
10 1 1.5

2. 单元编号数据格式

id_1 tri_id1 tri_id2 tri_id3 tag
id_2 tri_id1 tri_id2 tri_id3 tag

注:tag 有时可以表示这个三角形单元的一些特殊信息,画图时亦可以用其显示三角形颜色。

For Example (Triangles.txt)
1 41 42 46 3.7
2 29 7 43 3.7
3 34 14 38 3.7
4 42 35 46 3.7
5 11 44 59 3.7
6 35 42 49 3.7
7 34 38 43 3.7
8 38 29 43 3.7
9 41 24 42 3.7
10 41 46 61 3.7

程序

1 读取节点信息

def read_Nodes(node_file):
    Nodes = []
    f = open(node_file)
    lines = f.readlines()
    for line in lines:
        data = re.findall(r'\d+\.?\d*', line)
        #line = line.strip('\n')
      	#line = line.split(' ')
        id = int(data[0])
        x = float(data[1])
        y = float(data[2])
        da = [x, y]
        Nodes.append(da)
    return Nodes
    

2 读取网格拓扑信息

def read_Triangles(triangle_file):
    Triangles = []
    Tags = []
    f = open(triangle_file)
    lines = f.readlines()
    for line in lines:
        data = re.findall(r'\d+\.?\d*', line)
        id = int(data[0])
        id_1 = int(data[1])
        id_2 = int(data[2])
        id_3 = int(data[3])
        tag = float(data[4])
        da = [id_1, id_2, id_3]
        Triangles.append(da)
        Tags.append(tag)
    return Triangles, Tags

3 网格图形化

def plotMesh(Nodes, Triangles, Tags, showNodeId = True, showTriangleId = True, showTriangleTag = False):
    fig, ax = plt.subplots()
    patches = []
    triangleId = 0
    colors = []
    for triangle in Triangles:
        triangleId = triangleId + 1
        polygon = []
        colors.append(Tags[triangleId-1])
        for id in triangle:
            polygon.append(Nodes[id-1])
        for i in range(len(polygon)):
            line_x = []
            line_y = []
            line_x.append(polygon[i][0])
            if i+1 == len(polygon) :
                line_x.append(polygon[0][0])
            else:
                line_x.append(polygon[i+1][0])
            line_y.append(polygon[i][1])
            if i+1 == len(polygon) :
                line_y.append(polygon[0][1])
            else:
                line_y.append(polygon[i+1][1])
            plt.plot(line_x, line_y, 'r-')
        patches.append(Polygon(polygon, True))
        center_x = sum([_[0] for _ in polygon]) / len(polygon)
        center_y = sum([_[1] for _ in polygon]) / len(polygon)
        if showTriangleId :
            ax.annotate(triangleId, (center_x, center_y), color='red', weight='bold', fontsize=7, ha='center', va='center')
    if showNodeId :
        nodeId = 0
        for node in Nodes:
            nodeId = nodeId + 1
            ax.annotate(nodeId, (node[0], node[1]), color='black', weight='bold', fontsize=9, ha='center', va='center')
    collection = PatchCollection(patches)
    if showTriangleTag :
        collection.set_array(np.array(colors))
    ax.add_collection(collection)
    fig.colorbar(collection, ax=ax)
    ax.axis('equal')
    plt.show()

4 完整代码

import re
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection


def read_Nodes(node_file):
    Nodes = []
    f = open(node_file)
    lines = f.readlines()
    for line in lines:
        data = re.findall(r'\d+\.?\d*', line)
        id = int(data[0])
        x = float(data[1])
        y = float(data[2])
        da = [x, y]
        Nodes.append(da)
    return Nodes


def read_Triangles(triangle_file):
    Triangles = []
    Tags = []
    f = open(triangle_file)
    lines = f.readlines()
    for line in lines:
        data = re.findall(r'\d+\.?\d*', line)
        id = int(data[0])
        id_1 = int(data[1])
        id_2 = int(data[2])
        id_3 = int(data[3])
        tag = float(data[4])
        da = [id_1, id_2, id_3]
        Triangles.append(da)
        Tags.append(tag)
    return Triangles, Tags


def plotMesh(Nodes, Triangles, Tags, showNodeId = True, showTriangleId = True, showTriangleTag = False):
    fig, ax = plt.subplots()
    patches = []
    triangleId = 0
    colors = []
    for triangle in Triangles:
        triangleId = triangleId + 1
        polygon = []
        colors.append(Tags[triangleId-1])
        for id in triangle:
            polygon.append(Nodes[id-1])
        for i in range(len(polygon)):
            line_x = []
            line_y = []
            line_x.append(polygon[i][0])
            if i+1 == len(polygon) :
                line_x.append(polygon[0][0])
            else:
                line_x.append(polygon[i+1][0])
            line_y.append(polygon[i][1])
            if i+1 == len(polygon) :
                line_y.append(polygon[0][1])
            else:
                line_y.append(polygon[i+1][1])
            plt.plot(line_x, line_y, 'r-')
        patches.append(Polygon(polygon, True))
        center_x = sum([_[0] for _ in polygon]) / len(polygon)
        center_y = sum([_[1] for _ in polygon]) / len(polygon)
        if showTriangleId :
            ax.annotate(triangleId, (center_x, center_y), color='red', weight='bold', fontsize=7, ha='center', va='center')
    if showNodeId :
        nodeId = 0
        for node in Nodes:
            nodeId = nodeId + 1
            ax.annotate(nodeId, (node[0], node[1]), color='black', weight='bold', fontsize=9, ha='center', va='center')
    collection = PatchCollection(patches)
    if showTriangleTag :
        collection.set_array(np.array(colors))
    ax.add_collection(collection)
    fig.colorbar(collection, ax=ax)
    ax.axis('equal')
    plt.show()


if __name__ == "__main__":
    node_file = "Nodes.txt"
    triangle_file = "Triangles.txt"
    Nodes = read_Nodes(node_file)
    Triangles, Tags = read_Triangles(triangle_file)
    showNodeId = True
    showTriangleId = True
    plotMesh(Nodes, Triangles, Tags, showNodeId, showTriangleId)

结果

1. 不显示编号信息

在这里插入图片描述

2. 显示编号信息

在这里插入图片描述

  • 5
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值