简体   繁体   English

在 PyVista 中将绘图仪坐标转换为世界坐标

[英]Convert from plotter coordinates to world coordinates in PyVista

I am new to PyVista and vtk.我是 PyVista 和 vtk 的新手。 I am implementing a mesh editing tool (Python=3.10, pyvista=0.37,vtk=9.1 ) When a user clicks all points within a given radius of the mouse cursor's world coordinates (eg projected point on the surface) should be selected.我正在实现一个网格编辑工具 (Python=3.10, pyvista=0.37,vtk=9.1 ) 当用户单击鼠标光标的世界坐标(例如表面上的投影点)的给定半径内的所有点时,应该被选中。 I have implemented this much through callbacks to mouse clicks using the pyvista plotters track_click_position method.我已经通过使用 pyvista 绘图仪 track_click_position 方法对鼠标点击的回调实现了这么多。

My problem is that I also want for a user to be able to preview the selection (highlight the vertices that will be selected) before they click.我的问题是我还希望用户能够在单击之前预览选择(突出显示将被选中的顶点)。 For this it is necessary to track the mouse location in world coordinates and to attach a callback function to the movement of the mouse that will highlight the relevant nodes.为此,有必要跟踪鼠标在世界坐标中的位置,并将回调 function 附加到将突出显示相关节点的鼠标移动。

The pyvista plotter's 'track_mouse_position' method doesn't support attaching callbacks but I figured out a work around for that. pyvista 绘图仪的“track_mouse_position”方法不支持附加回调,但我想出了一个解决方法。 In the minimal example below I have managed to track changes to the mouse cursor location in pixels in the plotter's coordinate system.在下面的最小示例中,我设法跟踪了绘图仪坐标系中鼠标 cursor 位置(以像素为单位)的变化。 I am stuck now as to how to convert these into world coordinates.我现在对如何将这些转换成世界坐标感到困惑。 When the mouse hovers over the sphere these 'world coordinates' this should be the projected location on the sphere.当鼠标悬停在球体上时,这些“世界坐标”应该是球体上的投影位置。 When the mouse hovers off the sphere then it should return nothing or inf or some other useless value.当鼠标悬停在球体上时,它应该不返回任何内容或 inf 或其他一些无用的值。

import pyvista as pv
def myCallback(src,evt):
    C = src.GetEventPosition() # appears to be in pixels of the viewer
    print(C)
    # how to convert C into world coordinates on the sphere

sp = pv.Sphere()
p = pv.Plotter()
p.add_mesh(sp)
p.iren.add_observer("MouseMoveEvent",myCallback)
p.show()

Thank you very much for your help.非常感谢您的帮助。 Harry哈利

I figured this one out.我想通了。 They key was to use 'pick_mouse_position' after calling 'track_mouse_position'.他们的关键是在调用“track_mouse_position”之后使用“pick_mouse_position”。

 import pyvista as pv def myCallback(src,evt): out = p.pick_mouse_position() print(out) sp = pv.Sphere() p = pv.Plotter() p.add_mesh(sp) p.track_mouse_position() p.iren.add_observer("MouseMoveEvent",myCallback) p.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM