有没有可能在x-z平面(y=0)上通过鼠标在渲染窗口中的位置获取世界坐标?
最佳答案
这应该是可能的,但也许需要一些计算。
我将从这个 this example 开始.抓取鼠标移动或鼠标单击等鼠标事件,并使用 vtkCoordinate
将其转换为世界坐标。
来自示例(不是 python,但应该足以理解这个想法):
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();
}
现在,当您在单击时获得世界坐标时,您可能需要计算直线 cameraposition 到 mouseposition(在世界中)的交点以及它与 x-z 平面的交点。
关于python - Vtk - 鼠标位置的世界坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59724217/