在尝试编写代码以选择Mayavi中由三角形网格(连接它们的点和三角形)定义的表面时,我遇到了麻烦。
我已经尝试了以下方法:
from mayavi import mlab
from numpy import array
################################################################################
# define data
D = 150
B = 100
H = 100
L = 50
# defining coordinates of points
x = array([0, 0, D, D])
y = array([B, B/2, B/2, B])
z = array([0, 0, 0, 0])
# defining the 2 triangles to construct surface
triangles = array([(0, 1, 2),
(0, 2, 3)])
################################################################################
# Plot the data
fig = mlab.figure(1)
mlab.clf()
# Plot a plane with a triangular mesh constructed with above-defined data
plane = mlab.triangular_mesh(x, y, z, triangles,
transparent=False,
resolution=10,
color=(0.5,0.3,0.5))
# Plot the cursor which will be updated at each click on the plane
cursor3d = mlab.points3d(0., 0., 0.,
mode='axes',
color=(0, 0, 0),
scale_factor=1)
mlab.title('Click on the plane')
mlab.view(180, 0)
################################################################################
# Some logic to select 'mesh' and the data index when picking.
def picker_callback(picker_obj):
# retrieve VTK actors of picked objects
picked = picker_obj.actors
if plane.actor.actor._vtk_obj in [o._vtk_obj for o in picked]:
# Get mouse position on-click
position = picker_obj.pick_position
# Move the cursor in scene according to new clicked coordinates
cursor3d.mlab_source.reset(x=position[0],
y=position[1],
z=position[2])
# Connecting picker_callback to the figure
fig.on_mouse_pick(picker_callback)
# show the fig
mlab.show()
但是很显然,单击表面时,选择器只能识别由(x,y,z)定义的表面顶点。我想单击的是我可以在表面上能够找到的点坐标。
我已经尝试调整选择器的容忍度,但是没有结果。
我也试图借助100 * 100数组中的outline3d函数来实现。自从选择器感知到轮廓3d生成的字形以来,它就起作用了,但是数据太多了,什么也没做,并且与triangle_mesh函数相比,绘制时间太长了。
有什么想法吗?