# How to use vtkImageSliceMapper-\>SetSliceNumber?

**URL:** https://discourse.vtk.org/t/how-to-use-vtkimageslicemapper-setslicenumber/6038
**Category:** Support
**Created:** [June 22, 2021, 4:07pm UTC](https://discourse.vtk.org/t/how-to-use-vtkimageslicemapper-setslicenumber/6038 "2021-06-22T16:07:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kretep](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kretep/32/3588_2.png) [@kretep](https://discourse.vtk.org/u/kretep)
#### Post date: [June 22, 2021, 4:07pm UTC](https://discourse.vtk.org/t/how-to-use-vtkimageslicemapper-setslicenumber/6038/1 "2021-06-22T16:07:26Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [June 22, 2021, 10:28pm UTC](https://discourse.vtk.org/t/how-to-use-vtkimageslicemapper-setslicenumber/6038/2 "2021-06-22T22:28:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kretep](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kretep/32/3588_2.png) [@kretep](https://discourse.vtk.org/u/kretep)
#### Post date: [June 23, 2021, 10:33am UTC](https://discourse.vtk.org/t/how-to-use-vtkimageslicemapper-setslicenumber/6038/3 "2021-06-23T10:33:44Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [June 23, 2021, 1:24pm UTC](https://discourse.vtk.org/t/how-to-use-vtkimageslicemapper-setslicenumber/6038/4 "2021-06-23T13:24:06Z")

</div>

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