在Python中创建二维非矩形形状的三角形网格

2024-06-16 09:01:22 发布

您现在位置:Python中文网/ 问答频道 /正文

假设有一组点定义二维平面中非矩形形状的周长

我需要一个函数来创建三角形网格,在这里我可以修改三角形单元的数量并返回每个单元的(x,y)坐标

多谢各位


Tags: 函数网格数量定义平面单元形状矩形
2条回答

我把我最后做的事发出去了。 使用Python3时会发现一些问题。我建议在谷歌上运行

资料来源:https://github.com/inducer/meshpy/blob/master/examples/test_triangle.py

如果您有python3,并且不想降级到python2,请在googlecolab中运行

!pip install Meshpy
!pip install pybind11
!pip install pyvtk

from __future__ import division
from __future__ import absolute_import

import meshpy.triangle as triangle
import numpy as np
import numpy.linalg as la
from six.moves import range
import matplotlib.pyplot as pt

def round_trip_connect(start, end):
    return [(i, i+1) for i in range(start, end)] + [(end, start)]



points = [(100,400),
          (400,400),
          (400,280),
          (320,270), 
          (320,160), 
          (120,160), 
          (120,250), 
          (100,250)]

facets = round_trip_connect(0, len(points)-1)

circ_start = len(points)
points.extend((3 * np.cos(angle), 3 * np.sin(angle)) for angle in np.linspace(0, 2*np.pi, 30, endpoint=False))

facets.extend(round_trip_connect(circ_start, len(points)-1))


def needs_refinement(vertices, area):
    bary = np.sum(np.array(vertices), axis=0)/3
    max_area = 5 + (la.norm(bary, np.inf)-1)*5
    return bool(area > max_area)

info = triangle.MeshInfo()
info.set_points(points)
info.set_holes([(0, 0)])
info.set_facets(facets)

mesh = triangle.build(info, refinement_func=needs_refinement)

mesh_points = np.array(mesh.points)
mesh_tris = np.array(mesh.elements)


pt.triplot(mesh_points[:, 0], mesh_points[:, 1], mesh_tris)
pt.show()

print (mesh_points)

你也许应该退房

(我是dmsh和pygmsh的作者。)

相关问题 更多 >