我正在尝试从http://docs.enthought.com/mayavi/mayavi/auto/example_pick_on_surface.html上的Mayavi官方示例改编简单的代码行。
我想在imshow()图上使用picker命令,而不是在示例中使用网格图;特别是,我只希望接受图中的那些点(不在外面)。
但是,当我在数据单元格的边界之外单击时,将返回值(0,0,0)
...如何区分在边界之外单击的用户与在中心{ {1}}?
(0,0,0)
更新:
import os
import numpy as np
from mayavi import mlab
data = np.random.uniform(0, 2, (450, 450))
fig = mlab.figure(1)
mlab.clf()
landscape = mlab.imshow(data)
cursor3d = mlab.points3d(0., 0., 0., mode='axes',
color=(0, 0, 0),
scale_factor=0.5)
mlab.title('click me')
mlab.view(90, 0)
def picker_callback2(picker_obj):
point2d = picker_obj.mapper_position
x_, y_, z_ = point2d
print(x_,y_,z_) #clicks outside data cells return (0,0,0)
fig.on_mouse_pick(picker_callback2)
mlab.show()
答案 0 :(得分:0)
您缺少检查对象的选项,我们试图将拾取的对象与我们(用户)要渲染的对象进行匹配:
# …
picked = picker_obj.actors
if mesh.actor.actor._vtk_obj in [o._vtk_obj for o in picked]:
# …
这是第二个最佳答案:使用标量值高的网格来模拟imshow
:
def test_mesh():
x, y = numpy.mgrid[0:100,0:100]
z = numpy.zeros_like(x)
s = numpy.random.rand(*z.shape)
return mlab.mesh(x, y, z, scalars=s)
_fig = mlab.gcf()
_m = test_mesh()
mlab.view()
def picker_callback(picker):
picked = picker.actors
picked_actors=[o for o in picked]
if _m.actor.actor in picked_actors:
point2d = picker.mapper_position
x_, y_, z_ = point2d
print(x_,y_,z_)
_fig.on_mouse_pick(picker_callback)
mlab.show()