问题描述
我有一个定义为空间中(x,y)个点的列表的几何.我想根据这些数据创建一个三角形网格,所以我尝试了.但是,由于我的几何图形具有某些曲线,因此该算法会在我的零件的边缘之间生成不希望的三角形:
I have a geometry that is defined with a list of (x,y) points in space. I would like to create a triangular mesh out of this data, so I tried the Triangulation function in matplotlib for this purpose. However, since my geometry has some curves, the algorithm is generating undesired triangles between the edges of my part:
红色曲线是我的几何图形的边缘.
Where the red curve is the edge of my geometry.
有什么办法解决这个问题?也许三角测量功能不是我所需要的,在那种情况下,您对使用什么有任何建议吗?
Is there any way to deal this issue? Maybe the Triangulation function is not what I need, in that case, do you have any recommendation on what to use?
以下代码来自此示例.在该示例中,他们通过显式命名三个点而不是我想通过调用函数triang = tri.Triangulation(x, y)
来使用的Delaunay三角剖分来定义三角形,这将赋予我与原始图片相同的行为.
The following code comes from this example. In the example they defined the triangles by explicitly naming three points instead of the Delaunay triangulation that I want to use by calling the function: triang = tri.Triangulation(x, y)
, and which will give me the same behavior than my original picture.
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
xy = np.asarray([
[-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],
[-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],
[-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],
[-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],
[-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],
[-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],
[-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],
[-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],
[-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],
[-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],
[-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],
[-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],
[-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],
[ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],
[ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],
[ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],
[-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],
[-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],
[-0.077, 0.990], [-0.059, 0.993]])
x = np.degrees(xy[:, 0])
y = np.degrees(xy[:, 1])
triang = tri.Triangulation(x, y)
fig1, ax1 = plt.subplots()
ax1.set_aspect('equal')
ax1.triplot(triang, 'bo-', lw=1)
推荐答案
如果您具有图形的轮廓以绘制三角剖分,则可以使用@ThomasKühn的答案.
If you have the outline of the shape within to plot the triangulation you may apply the answer by @ThomasKühn.
否则,您可能在两点之间有一个最大距离,在该距离上不应考虑三角形.在这种情况下,您可以将那些三角形遮住.
Else, you may have a maximum distance between points above which triangles should not be taken into account. In that case you may mask those triangles out.
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
xy = np.asarray([
[-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],
[-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],
[-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],
[-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],
[-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],
[-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],
[-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],
[-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],
[-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],
[-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],
[-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],
[-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],
[-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],
[ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],
[ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],
[ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],
[-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],
[-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],
[-0.077, 0.990], [-0.059, 0.993]])
x = np.degrees(xy[:, 0])
y = np.degrees(xy[:, 1])
triang = tri.Triangulation(x, y)
fig1, ax1 = plt.subplots()
ax1.set_aspect('equal')
# plot all triangles
ax1.triplot(triang, 'bo-', lw=0.2)
# plot only triangles with sidelength smaller some max_radius
max_radius = 2
triangles = triang.triangles
# Mask off unwanted triangles.
xtri = x[triangles] - np.roll(x[triangles], 1, axis=1)
ytri = y[triangles] - np.roll(y[triangles], 1, axis=1)
maxi = np.max(np.sqrt(xtri**2 + ytri**2), axis=1)
triang.set_mask(maxi > max_radius)
ax1.triplot(triang, color="indigo", lw=2.6)
plt.show()
细线显示所有三角形(点的凸包),粗线仅显示边长不大于某些最大值(在这种情况下选择为2
)的那些三角形.
The narrow lines show all triangles (the convex hull of the points), the bold lines show only those triangles where no side-length is larger then some maximum values (in this case chosen to be 2
).
此线程可能同样相关: matplotlib轮廓/轮廓为* *凹**非网格数据
This thread may equally be relevant: matplotlib contour/contourf of **concave** non-gridded data
这篇关于在matplotlib中使用Triangulation时,如何处理在几何形状的边缘之间形成的(不需要的)三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!