Hi there!
I am working on a custom ImageInteractorStyle derived from vtkInteractorStyleImage to move an image (or camera in this case) with the left mouse button pressed. I also noticed that the default implementation doesn’t seem to support screen scaling other than 1.0, since the image doesn’t move exactly with the mouse cursor. My override implementation of OnMouseMove looks like this:
void ImageInteractorStyle::OnMouseMove()
{
if (m_panning && this->GetCurrentRenderer()) {
// Get the current mouse position
int currPos[2];
this->Interactor->GetEventPosition(currPos);
// Convert the initial and current mouse positions to world coordinates
vtkSmartPointer<vtkCoordinate> coordinate1 = vtkSmartPointer<vtkCoordinate>::New();
vtkSmartPointer<vtkCoordinate> coordinate2 = vtkSmartPointer<vtkCoordinate>::New();
coordinate1->SetCoordinateSystemToViewport();
coordinate2->SetCoordinateSystemToViewport();
coordinate1->SetViewport(this->GetCurrentRenderer());
coordinate2->SetViewport(this->GetCurrentRenderer());
coordinate1->SetValue(this->m_initialMousePos[0], this->m_initialMousePos[1], 0);
double* initialWorldPos = coordinate1->GetComputedWorldValue(this->GetCurrentRenderer());
coordinate2->SetValue(currPos[0], currPos[1], 0);
double* currentWorldPos = coordinate2->GetComputedWorldValue(this->GetCurrentRenderer());
// Calculate the offset in world coordinates
double deltaX = currentWorldPos[0] - initialWorldPos[0];
double deltaY = currentWorldPos[1] - initialWorldPos[1];
// Move the camera to follow the panning
vtkCamera* camera = this->GetCurrentRenderer()->GetActiveCamera();
// Update the camera position and focal point
double newCameraPosition[3] = { m_initialCameraPos[0] - deltaX,
m_initialCameraPos[1] - deltaY, m_initialCameraPos[2] };
double newCameraFocalPoint[3] = { m_initialFocalPos[0] - deltaX,
m_initialFocalPos[1] - deltaY, m_initialFocalPos[2] };
camera->SetPosition(newCameraPosition);
camera->SetFocalPoint(newCameraFocalPoint);
// Forward events
vtkInteractorStyleImage::OnMouseMove();
}
This behaves like the standard image interactor style, except that it now moves with the left mouse button pressed.
To make the movement of the image or camera exactly match the movement of the mouse, I have to multiply the delta by the current screen scale.
deltaX *= 1.25;
deltaY *= 1.25;
I had a similar problem when working on a custom CubeInteractorStyle. There I got the scaling from a QQuickItem which I use to render the vtk content.
Is there any other way to account for different screen scaling within the vtk coordinate transformation system??
I have also tried using vtkRenderWindow::GetDeviceToWorldMatrixForDevice but it only gives me the identity matrix.