How to use vtkImageSliceMapper->SetSliceNumber?

Hi, I’m visualizing a sliced image volume using a vtkImageSliceMapper and vtkImageSlice. Slicing through it with the vtkInteractorStyleImage works fine, but when I want to set the slice index through code (I want to synchronize multiple views) it doesn’t work.

I call vtkImageSliceMapper->SetSliceNumber(int), which does change the value of the slice number (I check it afterwards), but as soon as a render is performed, it reverts back to the old value. What am I missing?

Are you trying this while you have vtkInteractorStyleImage attached to the window? If so, then you won’t be able to manipulate the slice number directly by calling SetSliceNumber(), since vtkInteractorStyleImage changes the slice by adjusting the camera’s focal point (i.e. whichever slice intersects the focal point is the one that’s shown). Overall, vtkInteractorStyleImage is neither very sophisticated, nor is it very flexible.

So to adjust the shown slice when vtkInteractorStyleImage is in use, you must adjust the camera’s focal point. One way to do this is by calling camera->SetDistance(). The advantage is that the slice position is set in world coordinates, though the disadvantage is that you have to do some math to figure out the focal point that corresponds to a specific slice number.

Yes, I do have the vtkInteractorStyleImage attached to the window, so that will be the cause.

Maybe I’m better off using a vtkImageViewer2 instead, as that seems to wrap such functionality; is that a recommended approach?

The vtkImageViewer2 offers more functionality than vtkInteractorStyleImage, but less flexibility. My own inclination is towards vtkInteractorStyleImage.