简体   繁体   English

在 Python 中创建 2D 中非矩形形状的三角形网格

[英]Create triangular meshing of a non rectangular shape in 2D in Python

Suppose I have a set of points defining a perimeter of a non rectangular shape in the 2d plane.假设我有一组点定义了 2d 平面中非矩形形状的周长。

I need a function to create a triangular meshing where I can modify the number of triangle cells and return (x,y) coordinates of each cell.我需要一个函数来创建三角形网格,我可以在其中修改三角形单元格的数量并返回每个单元格的 (x,y) 坐标。

Thank you.谢谢你。

You should probably check out你应该看看

(I'm the author of dmsh and pygmsh.) (我是 dmsh 和 pygmsh 的作者。)

I poste what i did finally.我发布了我最后所做的事情。 Some problems are found when working with Python3 .使用 Python3 时发现了一些问题。 I advice to run on GoogleColab.我建议在 GoogleColab 上运行。

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

Run in google colab if you have python3 and you do not want to downgrad to python2如果您有 python3 并且不想降级到 python2,请在 google colab 中运行

!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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM