使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

我想从3D文件创建2D横截面,而又不会丢失什么是物质和什么是空气的信息。
使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

最后,我想获得一个字典,其中包含构成材料和空气夹杂物(可以是多个)的最外面的点,即

"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')

使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

创建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')

使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

提取顶点

# 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()

使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

我检索有关什么是物质和什么是空气的信息的方法

我的假设

最外面的点定义材料的边界。所有积分
定义空气夹杂物的边界。

我得到最重要的一点-> 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-')

使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

要知道船体内部的所有点,我使用此答案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])
如果您有一个以上的空气夹杂物,您是否有任何想法?

使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

您可以在这里找到文件:https://filebin.net/6blzvrrwhanv0jib

参考方案

得益于匀称的多边形,在其上构建了三边形,您无需遍历顶点。您可以使用一些内置函数。创建2D路径后,您几乎可以看到了。该路径具有两个属性:polygons_fullpolygons_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()

情节:
使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

或者,您可以使用多边形的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')

使用python从3d .stl文件中查找2D横截面中的材料和空气 - python

内部是已填充多边形的“孔”,因此这应该正是您想要的。它们是LinearRing,因此您不能直接使用Polygon属性。

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

python-docx应该在空单元格已满时返回空单元格 - python

我试图遍历文档中的所有表并从中提取文本。作为中间步骤,我只是尝试将文本打印到控制台。我在类似的帖子中已经看过scanny提供的其他代码,但是由于某种原因,它并没有提供我正在解析的文档的预期输出可以在https://www.ontario.ca/laws/regulation/140300中找到该文档from docx import Document from…

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…