【问题标题】:How to extrude an irregular polygon?如何挤出不规则的多边形?
【发布时间】:2021-06-26 00:02:24
【问题描述】:

我在平面中有一组 2D 顶点坐标(假设 xy 平面),我想在 z 方向上拉伸它以形成一个可以变换和渲染的 PolyData 对象。

理想的函数将采用 nx2 ndarray 顶点和高度并返回 PolyData

后备解决方案是在 VTK 中执行此操作并将结果包装为 PyVista 对象。

【问题讨论】:

    标签: python 3d polygon vtk pyvista


    【解决方案1】:

    在 3d 中嵌入 2d 顶点以创建实际多边形并进行拉伸的直接解决方案:

    import numpy as np
    import pyvista as pv
    
    rng = np.random.default_rng()
    
    # create dummy data
    N = 10
    angles = np.linspace(0, 2*np.pi, N, endpoint=False)
    radii = rng.uniform(0.5, 1.5, N)
    coords = np.array([np.cos(angles), np.sin(angles)]) * radii
    points_2d = coords.T  # shape (N, 2)
    
    # embed in 3d, create polygon
    points_3d = np.pad(points_2d, [(0, 0), (0, 1)])  # shape (N, 3)
    polygon = pv.lines_from_points(points_3d, close=True)
    
    # extrude along z and plot
    body = polygon.extrude((0, 0, 0.5))
    body.plot(color='white', specular=1, screenshot='extruded.png')
    

    如果您在挤压后需要一个封闭的表面,您必须从一个实心多边形开始(即一个面而不是一条线):

    # embed in 3d, create filled polygon
    points_3d = np.pad(points_2d, [(0, 0), (0, 1)])  # shape (N, 3)
    face = [N + 1] + list(range(N)) + [0]  # cell connectivity for a single cell
    polygon = pv.PolyData(points_3d, faces=face)
    
    # extrude along z and plot
    body = polygon.extrude((0, 0, 0.5))
    body.plot(color='white', specular=1, screenshot='extruded.png')
    

    【讨论】:

    • 谢谢@Andras Deak。这看起来像一个千篇一律,如何添加一个顶部和一个底部来做一个固体?
    猜你喜欢
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多