在 PyVista 中合并挤压多边形时消失的侧壁

问题描述 投票:0回答:1

我有一系列使用 PyVista 挤压的多边形。每个挤出的多边形代表一个带有侧壁的 3D 形状。然而,当我使用 merge() 函数将这些挤出的多边形合并在一起时,侧壁似乎在最终结果中消失了。

这是代码的简化版本:

import numpy as np
import pyvista as pv

data_x = np.array([[6, 2, 3, 1],
                   [2, 3, 2],
                   [4, 5, 6, ]], dtype=object)

data_y = np.array([[1, 2, 3, 6],
                   [2, 3, 8],
                   [1, 4, 5, ]], dtype=object)

# Create an empty PolyData object
merged = pv.PolyData()

# Create the 3D point array
z = np.zeros_like(data_x[0])
points_3d = np.column_stack((data_x[0], data_y[0], z))

# Create the polygon and extrude it
face = [len(data_x[0])] + list(range(len(data_x[0])))
polygon = pv.PolyData(points_3d, faces=[face])
extrusion = polygon.extrude((0, 0, 1), capping=True)

merged = extrusion

# Loop to vary the value of x
for x in range(len(data_x)):
    # Create the 3D point array
    z = np.zeros_like(data_x[x])
    points_3d = np.column_stack((data_x[x], data_y[x], z))

    # Create the polygon and extrude it
    face = [len(data_x[x])] + list(range(len(data_x[x])))
    polygon = pv.PolyData(points_3d, faces=[face])
    extrusion = polygon.extrude((0, 0, 1), capping=True)

    # Combine the extruded polygon with the previous one
    merged += extrusion

# Save and download the STL file
merged.save('extrusion.stl')
from google.colab import files
files.download('extrusion.stl')

我想知道为什么侧壁在合并结果中消失,而当单独生成多边形时它们存在,这可以测试删除循环部分,因此只生成第一个(0)多边形。

我正在使用 Google Colab 中的代码。

任何帮助或建议将不胜感激!

python 3d polygon pyvista stl-format
1个回答
0
投票

作为旁注,您的示例有点偏离,因为您似乎创建了第一个挤压,然后在循环的第一次迭代中重新创建它。您可以从

merged = pv.PolyData()
开始并将每个挤压合并到其中。

作为另一个旁注,不要将 numpy 数组与

dtype=object
一起使用。这可能只是为了您的示例,但只需使用数组列表(或列表列表)即可。

至于你的问题,看来你遇到了一个错误。合并网格时,包含三角形条带(而不是仅面)的网格会出现问题。挤压是最常见的最终得到聚条的方法。如果您将代码更改为具有

merged += extrusion.triangulate()
(分解三角形条带以提供仅三角形的网格),则工件会消失。

这里是你的例子清理了一下:

import numpy as np
import pyvista as pv

data_x = [
    [6, 2, 3, 1],
    [2, 3, 2],
    [4, 5, 6],
]

data_y = [
    [1, 2, 3, 6],
    [2, 3, 8],
    [1, 4, 5],
]

# Create an empty PolyData object
merged = pv.PolyData()

# Loop to vary the value of x
for x, y in zip(data_x, data_y):
    # Create the 3D point array
    z = np.zeros_like(x)
    points_3d = np.column_stack((x, y, z))

    # Create the polygon and extrude it
    face = [len(x)] + list(range(len(x)))
    polygon = pv.PolyData(points_3d, faces=face)
    extrusion = polygon.extrude((0, 0, 1), capping=True)

    # Combine the extruded polygon with the previous one
    #merged += extrusion  # original, buggy
    merged += extrusion.triangulate()  # workaround

我会用 PyVista 打开一个问题并解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.