如何使用 Matplotlib 创建由多个 3d 多边形组成的单个绘图对象

问题描述

以下代码是我目前正在做的工作的基础。它确实完成了我想要的,但生成的图旋转很慢。我的目标/假设是,我不会向轴添加 Npoly3DCollection 对象,而是以某种方式创建一个表示整个多边形集合到轴的对象。我的想法是,通过拥有一个对象而不是可能有数百个对象,可能会提高图形交互性的性能。我通常将 VTK 用于这样的事情,并且类似的概念也适用。

如果可能,我正在寻找一种方法来制作与此类似的集合example that demonstrates how to use patch collections for 2D polygons

给那些关心的人一个快速的背景:变量 obj 是一个自定义类对象,用于存储 .obj 文件中的几何数据,而 geo 是一个字典,按子对象名称组织字典中的每个子对象;字典 obj.geo 中的每个子对象都包含顶点和面 ndarrays。

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import art3d

# Initialiez a figure space
fig = plt.figure()
ax = fig.add_subplot( projection='3d' )

# For each sub-object
for sub_obj_name,sub_obj in obj.geo.items():
    
    # Create a 3D collection object to represent the parsed obj file contents
    pc = art3d.poly3DCollection( sub_obj.vertices[ sub_obj.faces ],facecolor='white',edgecolor='black',linewidths=0.05,alpha=1 )
    ax.add_collection( pc )
    
# Extract the geometric limits of the object geometry
geo_limits = obj.get_geometry_limits()
    
# Label and show the plot
ax.set_xlabel( 'X' )
ax.set_ylabel( 'Y' )
ax.set_zlabel( 'Z' )
ax.set_xlim3d( geo_limits[0] )
ax.set_ylim3d( geo_limits[1] )
ax.set_zlim3d( geo_limits[2] )
ax.set_Box_aspect( [ 1,1,1 ] )
set_axes_equal( ax )
plt.show()

更新: 我确实尝试了以下方法,渲染速度稍快,但仍然有延迟旋转:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import art3d

# Initialiez a figure space
fig = plt.figure()
ax = fig.add_subplot( projection='3d' )

# Initialize an object to concatentate all the face vertices
poly_collection = None

# For each sub-object
for sub_obj_index,( sub_obj_name,sub_obj ) in enumerate( obj.geo.items() ):
    
    # Check if concatenation should happen
    if sub_obj_index > 0:
        poly_collection = np.concatenate( ( poly_collection,sub_obj.vertices[ sub_obj.faces ] ) )
    else:
        poly_collection = sub_obj.vertices[ sub_obj.faces ]
    
# Create a 3D collection object to represent the parsed obj file contents
pc = art3d.poly3DCollection( poly_collection,alpha=1 )
ax.add_collection( pc )
    
# Extract the geometric limits of the object geometry
geo_limits = obj.get_geometry_limits()
    
# Label and show the plot
ax.set_xlabel( 'X' )
ax.set_ylabel( 'Y' )
ax.set_zlabel( 'Z' )
ax.set_xlim3d( geo_limits[0] )
ax.set_ylim3d( geo_limits[1] )
ax.set_zlim3d( geo_limits[2] )
ax.set_Box_aspect( [ 1,1 ] )
set_axes_equal( ax )
plt.show()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)