<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Dear vtkusers,</p>
    <p>during my work on a small programm for the analysis of computed
      tomography data I found an unexpected behaviour of the
      vtkImagePlaneWidget. I tried to boil down the problem to a small
      python script, which uses a very small image dimension of only 3 x
      3 x 3, where the effect becomes prominent (see python program
      below).<br>
    </p>
    <p>The script makes a cut through a 3 x 3 x 3 volume using a
      vtkImagePlaneWidget. The four coloured spheres indicate the
      expected edges of the resulting image slice. The result is pretty
      much, what I expected (see attachement screenshot1.png). However,
      when I grab the vtkImagePlaneWidget using "Control + Middle Mouse
      Button" an move the plane around in the x-y plane, some unexpected
      effects occur (see screenshot2.png): Sometimes some kind of
      interpolation happens, which results in an image that looks rather
      like a 6 x 3 grid instead of 3 x 3 and also the position of the
      image relative to the four coloured spheres changes. What I
      require is a cut through the volume data that preserves the
      correct voxel boundaries. Is that possible using the
      vtkImagePlaneWidget or is there an alternative class, that serves
      this purpose?</p>
    <p>Many thanks<br>
      Oliver</p>
    <pre style="background-color:#ffffff;color:#000000;font-family:'Source Code Pro';font-size:10,5pt;"><span style="color:#0000dd;">import </span>vtk
<span style="color:#0000dd;">import </span>vtk.util.numpy_support
<span style="color:#0000dd;">import </span>numpy <span style="color:#0000dd;">as </span>np

<span style="color:#808080;"># create a renderer
</span>renderer = vtk.vtkRenderer()
renderer.SetBackground(<span style="color:#0000dd;">0.2</span>, <span style="color:#0000dd;">0.2</span>, <span style="color:#0000dd;">0.5</span>)
renderer.SetBackground2(<span style="color:#0000dd;">0.4</span>, <span style="color:#0000dd;">0.4</span>, <span style="color:#0000dd;">1.0</span>)
renderer.SetGradientBackground(<span style="color:#0000dd;">True</span>)

<span style="color:#808080;"># create a render_window
</span>render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)

<span style="color:#808080;"># create a renderwindowinteractor
</span>iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(render_window)
iren.Initialize()

<span style="color:#808080;"># Create a 3 x 3 x 3 image data with a spacing of 1.
</span>image = vtk.vtkImageData()
dat = np.linspace(<span style="color:#0000dd;">1</span>, <span style="color:#0000dd;">27</span>, <span style="color:#0000dd;">27</span>, <span style="color:#660099;">dtype</span>=np.int16) * <span style="color:#0000dd;">10
</span>dat = np.ascontiguousarray(dat)
dat = vtk.util.numpy_support.numpy_to_vtk(dat, <span style="color:#660099;">deep</span>=<span style="color:#0000dd;">1</span>)
image.SetDimensions(<span style="color:#0000dd;">3</span>, <span style="color:#0000dd;">3</span>, <span style="color:#0000dd;">3</span>)
image.SetSpacing(<span style="color:#0000dd;">1</span>, <span style="color:#0000dd;">1</span>, <span style="color:#0000dd;">1</span>)
image.GetPointData().SetScalars(dat)
image.Modified()

<span style="color:#808080;"># Create a vtkImagePlaneWidget and activate it
</span>plane_widget = vtk.vtkImagePlaneWidget()
plane_widget.TextureInterpolateOff()
plane_widget.SetInputData(image)
plane_widget.SetInteractor(render_window.GetInteractor())
plane_widget.SetOrigin(<span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>)
plane_widget.SetPoint1(<span style="color:#0000dd;">8</span>, <span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>)
plane_widget.SetPoint2(<span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">8</span>, <span style="color:#0000dd;">0</span>)
plane_widget.UpdatePlacement()
plane_widget.DisplayTextOn()
plane_widget.On()
plane_widget.SetWindowLevel(<span style="color:#0000dd;">200</span>, <span style="color:#0000dd;">50</span>)
rs = plane_widget.GetReslice()


<span style="color:#0000dd;">def </span>create_sphere(x, y, z, col):
    <span style="color:#006400;font-weight:bold;">"""Create a small sphere with color col at postion x, y, z"""
</span><span style="color:#006400;font-weight:bold;">    </span>source = vtk.vtkSphereSource()
    source.SetCenter(x, y, z)
    source.SetRadius(<span style="color:#0000dd;">0.2</span>)
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(source.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(col)
    renderer.AddActor(actor)


<span style="color:#808080;"># Position 4 small markers at the expected edges of the image slice.
</span>create_sphere(<span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>, (<span style="color:#0000dd;">1</span>, <span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>))
create_sphere(<span style="color:#0000dd;">3</span>, <span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>, (<span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">1</span>, <span style="color:#0000dd;">0</span>))
create_sphere(<span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">3</span>, <span style="color:#0000dd;">0</span>, (<span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">0</span>, <span style="color:#0000dd;">1</span>))
create_sphere(<span style="color:#0000dd;">3</span>, <span style="color:#0000dd;">3</span>, <span style="color:#0000dd;">0</span>, (<span style="color:#0000dd;">1</span>, <span style="color:#0000dd;">1</span>, <span style="color:#0000dd;">0</span>))

<span style="color:#808080;"># Reset the camera and start the RenderWindowInteractor
</span>renderer.ResetCamera()
iren.Start()
</pre>
  </body>
</html>