在渲染窗口上,通过鼠标位置获取世界坐标在x-z平面上的可能性(y=0)?
vtkCoordinate
将其转换为世界坐标。virtual void OnLeftButtonDown()
{
std::cout << "Pressed left mouse button." << std::endl;
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
std::cout << "(x,y) = (" << x << "," << y << ")" << std::endl;
vtkSmartPointer<vtkCoordinate> coordinate =
vtkSmartPointer<vtkCoordinate>::New();
coordinate->SetCoordinateSystemToDisplay();
coordinate->SetValue(x,y,0);
// This doesn't produce the right value if the sphere is zoomed in???
double* world = coordinate->GetComputedWorldValue(this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
std::cout << "World coordinate: " << world[0] << ", " << world[1] << ", " << world[2] << std::endl;
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
我可以使用vtkplotter来进行复现:
from vtkplotter import datadir, Plotter, Point
import vtk
def onLeftClick(mesh):
vcr = vtk.vtkCoordinate()
vcr.SetCoordinateSystemToDisplay()
pt = vcr.GetComputedWorldValue(vp.renderer)
vp.add(Point(mesh.picked3d, c='red'))
vp.add(Point(pt, c='white'))
vp.renderer.ResetCamera()
print(mesh.picked3d, pt)
vp = Plotter(verbose=0)
vp.load(datadir+"bunny.obj")
vp.mouseLeftClickFunction = onLeftClick
vp.show()