我想从3D文件创建2D横截面,而又不会丢失什么是物质和什么是空气的信息。
最后,我想获得一个字典,其中包含构成材料和空气夹杂物(可以是多个)的最外面的点,即
"material" : [[x1,y1],[x2,y2]...]
"air_inclusions": [[[x11,y11],[x12,y12],...],[[x21,y21],[x22,y22],...],[[x31,y31],[x32,y32],...]]
这是我尝试执行此操作的示例:
我有以下.stl文件,您可以在此处下载https://filebin.net/c9o0zy4bnv8dvuew
使用惊人的python包trimesh,我可以导入.stl文件
import trimesh
import numpy as np
mesh = trimesh.load_mesh(r"PATH_TO_FILE")
# give it a color
mesh.visual.face_colors = [100, 100, 100, 255]
# and show it
mesh.show(viewer='gl')
创建2D幻灯片
# I can create a 2D slice of the geometry at origin [0,0,5] and slice-plane with normal direction [0,0,1]
slice = mesh.section(plane_origin=[0,0,5],
plane_normal=[0,0,1])
slice.show(viewer='gl')
提取顶点
# take 2D slice (before was still 3D)
slice_2D, to_3D = slice.to_planar()
# get vertices
vertices = np.asanyarray(slice_2D.vertices)
# plot
import matplotlib.pyplot as plt
x,y = vertices.T
plt.scatter(x,y,s=0.4)
plt.show()
我检索有关什么是物质和什么是空气的信息的方法
我的假设
最外面的点定义材料的边界。所有积分
定义空气夹杂物的边界。
我得到最重要的一点-> convex hull
from scipy.spatial import ConvexHull
# compute the hull
hull = ConvexHull(vertices)
# plot
plt.plot(vertices[:,0], vertices[:,1], 'o')
for simplex in hull.simplices:
plt.plot(vertices[simplex, 0], vertices[simplex, 1], 'k-')
要知道船体内部的所有点,我使用此答案What's an efficient way to find if a point lies in the convex hull of a point cloud?
# Source: https://stackoverflow.com/questions/16750618/whats-an-efficient-way-to-find-if-a-point-lies-in-the-convex-hull-of-a-point-cl
def in_hull(p, hull):
"""
Test if points in `p` are in `hull`
`p` should be a `NxK` coordinates of `N` points in `K` dimensions
`hull` is either a scipy.spatial.Delaunay object or the `MxK` array of the
coordinates of `M` points in `K`dimensions for which Delaunay triangulation
will be computed
"""
from scipy.spatial import Delaunay
if not isinstance(hull,Delaunay):
hull = Delaunay(hull)
return hull.find_simplex(p)>=0
我收集剩余的积分
# Remaining points
remaining = []
for i,in_hull in enumerate(in_hull(vertices,hull.simplices)):
if in_hull:
remaining.append(vertices[i])
问题
其余点仅是两点,但应该更多,如上图所示。为什么会这样,我该如何解决?
[TrackedArray([21.60581633,8.99397324]),
TrackedArray([12.95590211,23.97608075])
如果您有一个以上的空气夹杂物,您是否有任何想法?
即
您可以在这里找到文件:https://filebin.net/6blzvrrwhanv0jib
最佳答案
得益于匀称的多边形,在其上构建了三边形,您无需遍历顶点。您可以使用一些内置函数。创建2D路径后,您几乎可以看到了。该路径具有两个属性:polygons_full
和polygons_closed
。第一个是没有内部多边形的最外面的多边形,第二个是路径的所有多边形。
您可以简单地执行以下操作:
slice_2D, to_3D = slice.to_planar()
# create a new figure to which you can attach patches
fig = plt.figure(1)
ax = fig.add_subplot(111)
# Here you get your outmost polygons and add them as patches to your plot
for p in slice_2D.polygons_full:
ax.add_patch(PolygonPatch(p))
# this is needed due to the differences of polygons_full and polygons_closed to check, if the polygon is one of the outer polygons
outer_polys = [x.exterior for x in slice_2D.polygons_full]
# iterate over all polygons and check, whether they are one of the outmost polygons. If not plot it (the outmost ones have already been added as patches).
for p in (slice_2D.polygons_closed):
if p.exterior not in outer_polys:
plt.plot(*(p.exterior.xy), 'r')
# show the plot
plt.show()
情节:
或者,您可以使用多边形的
interior
属性将其缩短:slice_2D, to_3D = slice.to_planar()
for p in slice_2D.polygons_full:
plt.plot(*(p.exterior.xy),'k')
for r in p.interiors:
plt.plot(*zip(*r.coords), 'b')
内部是已填充多边形的“孔”,因此这应该正是您想要的。它们是
LinearRing
,因此您不能直接使用Polygon属性。