过程如下:
- 将两次鼠标位置的显示坐标移动到世界坐标
- 计算x、y轴的偏移量
- 设置相机的位置和相机焦点
关键代码如下:
void OnLeftButtonDown() {
clickPos = this->GetInteractor()->GetEventPosition();
point1[0] = clickPos[0];
point1[1] = clickPos[1];
renderer->SetDisplayPoint(point1[0], point1[1], 0);
renderer->DisplayToWorld();
WorldPoint[0] = (renderer->GetWorldPoint())[0];
WorldPoint[1] = (renderer->GetWorldPoint())[1];
WorldPoint[2] = (renderer->GetWorldPoint())[2];
btPressed = true;
}
void stVtkInteractorStyleImage::OnLeftButtonUp() {
clickPos1 = this->GetInteractor()->GetEventPosition();
point2[0] = clickPos1[0];
point2[1] = clickPos1[1];
renderer->SetDisplayPoint(point2[0], point2[1], 0);
renderer->DisplayToWorld();
WorldPoint1[0] = (renderer->GetWorldPoint())[0];
WorldPoint1[1] = (renderer->GetWorldPoint())[1];
WorldPoint1[2] = (renderer->GetWorldPoint())[2];
//计算两点的偏移量
double disX = WorldPoint1[0] - WorldPoint[0];
double disY = WorldPoint1[1] - WorldPoint[1];
vtkCamera *camera = this->renderer->GetActiveCamera();
double* pos = camera->GetPosition();
double* focalPos = camera->GetFocalPoint();
camera->SetPosition(pos[0] - disX, pos[1] - disY, pos[2]);
camera->SetFocalPoint(focalPos[0] - disX, focalPos[1] - disY, focalPos[2]);
renderer->GetRenderWindow()->GetInteractor()->Render();
}