Example #1
0
def clipping_plane(v, interactor, event):
    if interactor.GetKeyCode() == "c":
        if hasattr(v, 'planew'):
            is_enabled = v.planew.GetEnabled()
            v.planew.SetEnabled(not is_enabled)
            print ("should set to not", is_enabled)
            v.getRenderer().Render()
        else:
            print ("handling c")
            planew = vtk.vtkImplicitPlaneWidget2()
            
            rep = vtk.vtkImplicitPlaneRepresentation()
            planew.SetInteractor(v.getInteractor())
            planew.SetRepresentation(rep)

            plane = vtk.vtkPlane()
            plane.SetOrigin(0,125,0)
            plane.SetNormal(0.,1.,0.)
            rep.GetPlane(plane)
            rep.UpdatePlacement()
            rep.PlaceWidget(v.volume.GetBounds())
            v.volume.GetMapper().AddClippingPlane(plane)
            v.volume.Modified()
            planew.On()
            v.plane = plane
            v.planew = planew
            v.style.AddObserver('LeftButtonReleaseEvent', functools.partial(update_clipping_plane, v, planew), 0.5)
Example #2
0
    def __init__(self, dataShape, interactor):
        self.dataShape = dataShape
        self.planes = []
        self.coordinate = [0,0,0]
        self.lastChangedAxis = -1
        for i in range(3):
            p = vtkImplicitPlaneRepresentation()
            p.SetPlaceFactor(1.0)
            p.OutsideBoundsOn()
            p.ScaleEnabledOff()
            p.SetOrigin(0.25,0.25,0.25)
            p.PlaceWidget([0.1,dataShape[0],0.1,dataShape[1],0.1,dataShape[2]])

            args = [0, 0, 0]
            args[i] = 1
            p.SetNormal(*args)
            p.GetSelectedPlaneProperty().SetColor(*args)
            p.GetEdgesProperty().SetColor(*args) #bug in VTK

            p.GetPlaneProperty().SetOpacity(0.001)
            #do not draw outline
            p.GetOutlineProperty().SetColor(0,0,0)
            p.GetOutlineProperty().SetOpacity(0.0)
            #do not draw normal
            p.GetSelectedNormalProperty().SetOpacity(0.0)
            p.GetNormalProperty().SetOpacity(0.0)
            p.OutlineTranslationOff()
            p.TubingOff()
            
            self.cross = vtkPolyData()
            points = vtkPoints()
            polys = vtkCellArray()
            points.SetNumberOfPoints(6)
            for i in range(3):
                polys.InsertNextCell(2)
                polys.InsertCellPoint(2*i); polys.InsertCellPoint(2*i+1)
            self.cross.SetPoints(points)
            self.cross.SetLines(polys)
            
            pw = vtkImplicitPlaneWidget2()
            pw.SetRepresentation(p)
            pw.SetInteractor(interactor)
            pw.AddObserver("InteractionEvent", self.__PlanePositionCallback)
            
            self.planes.append(pw)
            
        tubes = vtkTubeFilter()
        tubes.SetNumberOfSides(16)
        tubes.SetInput(self.cross)
        tubes.SetRadius(1.0)
        
        crossMapper = vtkPolyDataMapper()
        crossMapper.SetInput(self.cross)
        crossActor = vtkActor()
        crossActor.SetMapper(crossMapper)
        crossActor.GetProperty().SetColor(0,0,0)
        self.AddPart(crossActor)
        
        #initially invoke the event!
        self.InvokeEvent("CoordinatesEvent")
Example #3
0
    def __init__(self, dataShape, interactor):
        self.dataShape = dataShape
        self.planes = []
        self.coordinate = [0,0,0]
        self.lastChangedAxis = -1
        for i in range(3):
            p = vtkImplicitPlaneRepresentation()
            p.SetPlaceFactor(1.0)
            p.OutsideBoundsOn()
            p.ScaleEnabledOff()
            p.SetOrigin(0.25,0.25,0.25)
            p.PlaceWidget([0.1,dataShape[0],0.1,dataShape[1],0.1,dataShape[2]])

            args = [0, 0, 0]
            args[i] = 1
            p.SetNormal(*args)
            p.GetSelectedPlaneProperty().SetColor(*args)
            p.GetEdgesProperty().SetColor(*args) #bug in VTK

            p.GetPlaneProperty().SetOpacity(0.001)
            #do not draw outline
            p.GetOutlineProperty().SetColor(0,0,0)
            p.GetOutlineProperty().SetOpacity(0.0)
            #do not draw normal
            p.GetSelectedNormalProperty().SetOpacity(0.0)
            p.GetNormalProperty().SetOpacity(0.0)
            p.OutlineTranslationOff()
            p.TubingOff()
            
            self.cross = vtkPolyData()
            points = vtkPoints()
            polys = vtkCellArray()
            points.SetNumberOfPoints(6)
            for i in range(3):
                polys.InsertNextCell(2)
                polys.InsertCellPoint(2*i); polys.InsertCellPoint(2*i+1)
            self.cross.SetPoints(points)
            self.cross.SetLines(polys)
            
            pw = vtkImplicitPlaneWidget2()
            pw.SetRepresentation(p)
            pw.SetInteractor(interactor)
            pw.AddObserver("InteractionEvent", self.__PlanePositionCallback)
            
            self.planes.append(pw)
            
        tubes = vtkTubeFilter()
        tubes.SetNumberOfSides(16)
        tubes.SetInput(self.cross)
        tubes.SetRadius(1.0)
        
        crossMapper = vtkPolyDataMapper()
        crossMapper.SetInput(self.cross)
        crossActor = vtkActor()
        crossActor.SetMapper(crossMapper)
        crossActor.GetProperty().SetColor(0,0,0)
        self.AddPart(crossActor)
        
        #initially invoke the event!
        self.InvokeEvent("CoordinatesEvent")
Example #4
0
    def __init__(self, item_actor, interactor):
        super(PlaneWidget, self).__init__()
        self.interactor = interactor
        self.plane = vtkPlane()
        self.plane.SetOrigin(0, 0, 0)
        self.plane.SetNormal(0, 0, 1)

        def callback(caller, name):
            rep = caller.GetRepresentation()
            rep.GetPlane(self.plane)
            self.interactor.plane_changed.emit(self.plane.GetOrigin(),
                                               self.plane.GetNormal())

        self.rep = vtkImplicitPlaneRepresentation()
        self.rep.SetPlaceFactor(1.5)
        self.rep.PlaceWidget(item_actor.GetBounds())
        self.rep.SetNormal(self.plane.GetNormal())
        self.rep.OutlineTranslationOff()
        self.rep.ScaleEnabledOff()
        plane_prop = self.rep.GetPlaneProperty()
        plane_prop.SetOpacity(0.1)
        #self.rep.SetDrawPlane(False)

        self.SetInteractor(interactor)
        self.SetRepresentation(self.rep)
        self.AddObserver(vtkCommand.InteractionEvent, callback)
        self.On()
Example #5
0
def new_implicit_plane_widget(
        data_source) -> Tuple[vtk.vtkAbstractWidget, vtk.vtkProp]:
    # This portion of the code clips the mace with the vtkPlanes
    # implicit function. The clipped region is colored green.
    select_plane = vtk.vtkPlane()
    clipper = vtk.vtkClipDataSet()
    clipper.SetInputConnection(data_source.GetOutputPort())
    clipper.SetClipFunction(select_plane)
    clipper.InsideOutOn()

    selectMapper = vtk.vtkPolyDataMapper()
    selectMapper.SetInputConnection(clipper.GetOutputPort())

    selectActor = vtk.vtkActor()
    selectActor.SetMapper(selectMapper)
    selectActor.GetProperty().SetColor(0, 1, 1)
    selectActor.VisibilityOff()
    selectActor.SetScale(1.01, 1.01, 1.01)

    # Associate the line widget with the interactor
    planeRep = vtk.vtkImplicitPlaneRepresentation()
    planeRep.SetPlaceFactor(2.0)
    data_source.Update()
    bounds = data_source.GetOutput().GetBounds()
    origin = [
        (bounds[0] + bounds[1]) / 2.0,
        (bounds[2] + bounds[3]) / 2.0,
        (bounds[4] + bounds[5]) / 2.0,
    ]
    planeRep.PlaceWidget(bounds)
    planeRep.SetOrigin(origin)
    planeWidget = vtk.vtkImplicitPlaneWidget2()
    planeWidget.SetRepresentation(planeRep)

    # planeWidget.SetInputConnection(glyph.GetOutputPort())
    def myCallback(obj, event):
        planeRep.GetPlane(select_plane)
        print("MYCALLBACK", type(obj), type(event), event)
        selectActor.VisibilityOn()

    planeWidget.AddObserver("InteractionEvent", myCallback)
    return planeWidget, selectActor
 renderWindow =vtk.vtkRenderWindow()
 renderWindow.AddRenderer(renderer)
 renderer.AddActor(actor)
  
 #  An interactor
 renderWindowInteractor = vtk.vtkRenderWindowInteractor()
 renderWindowInteractor.SetRenderWindow(renderWindow)
  
 renderWindow.Render()
 
 #  The callback will do the work
 #vtk.vtkIPWCallback> myCallback = vtk.vtkIPWCallback()
 #myCallback.Plane = plane
 #myCallback.Actor = actor
 
 rep = vtk.vtkImplicitPlaneRepresentation()
 rep.SetPlaceFactor(1.25) #  This must be set prior to placing the widget
 rep.PlaceWidget(actor.GetBounds())
 rep.SetNormal(plane.GetNormal())
 
 planeWidget = vtk.vtkImplicitPlaneWidget2()
 planeWidget.SetInteractor(renderWindowInteractor)
 planeWidget.SetRepresentation(rep)
 myCallback = callback()
 myCallback.Plane = plane
 myCallback.Actor = actor
 planeWidget.AddObserver(vtk.vtkCommand.InteractionEvent, myCallback.onExecute)
 
 #  Render
 renderWindowInteractor.Initialize()
 renderWindow.Render()
Example #7
0
def implicitplane(filename):
    '''
    filename:str, 文件路径
    使用第二代ImplicitPlaneWidget2交互式定义多数据的剪切平面。 
    如果未指定任何参数,则vtkSphereSource会生成多边形数据。 
    通过指定.ply文件,该示例可以对任意多边形数据进行操作。
    '''
    sphereSource = vtk.vtkSphereSource()
    sphereSource.SetRadius(10.0)

    reader = vtk.vtkPLYReader()

    # 设立一个渲染管道
    plane = vtk.vtkPlane()
    clipper = vtk.vtkClipPolyData()
    clipper.SetClipFunction(plane)
    clipper.InsideOutOn()
    if filename == None:
        clipper.SetInputConnection(sphereSource.GetOutputPort())
    else:
        reader.SetFileName(filename)
        clipper.SetInputConnection(reader.GetOutputPort())

    # 创建一个渲染的角色
    colors = vtk.vtkNamedColors()
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(clipper.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    # actor.GetProperty().SetColor(colors.GetColor3d("black"))

    backFaces = vtk.vtkProperty()
    backFaces.SetDiffuseColor(.8, .8, .4)
    # backFaces.SetDiffuseColor(0, 0, 0)

    actor.SetBackfaceProperty(backFaces)

    # 渲染器和窗口
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderer.AddActor(actor)

    # 一个交互器
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    renderWindow.Render()

    # 回调将完成工作
    myCallback = vtkIPWCallback()
    myCallback.Plane = plane
    myCallback.Actor = actor

    rep = vtk.vtkImplicitPlaneRepresentation()
    rep.SetPlaceFactor(1.25)  # 必须在放置小部件之前进行设置
    rep.PlaceWidget(actor.GetBounds())
    rep.SetNormal(plane.GetNormal())

    planeWidget = vtk.vtkImplicitPlaneWidget2()
    planeWidget.SetInteractor(renderWindowInteractor)
    planeWidget.SetRepresentation(rep)
    planeWidget.AddObserver('InteractionEvent', myCallback)

    # 渲染
    renderWindowInteractor.Initialize()
    renderWindow.Render()
    planeWidget.On()

    # 开启鼠标和键盘的交互模式
    renderWindowInteractor.Start()
Example #8
0
def SelectCuttingPlane(input_file):
    ## Get vtk format
    file_format = input_file.split(".")[-1]
    input_vtk = input_file.replace(file_format, "vtk")
    if file_format == "nrrd":
        print("\nCreating mesh from: " + input_file)
        print("\nSaving as: " + input_vtk)
        xml_filename = input_file.rsplit(os.sep,
                                         1)[0] + "/cutting_plane_nrrd2vtk.xml"
        create_meshfromDT_xml(xml_filename, input_file, input_vtk)
        execCommand = ["MeshFromDistanceTransforms", xml_filename]
        subprocess.check_call(execCommand)
        print("Calling cmd:\n" + " ".join(execCommand))
    elif file_format == "ply":
        execCommand = ["ply2vtk", input_file, input_vtk]
        subprocess.check_call(execCommand)
        print("Calling cmd:\n" + " ".join(execCommand))
    elif file_format == "stl":
        execCommand = ["stl2vtk", input_file, input_vtk]
        subprocess.check_call(execCommand)
        print("Calling cmd:\n" + " ".join(execCommand))
    elif file_format == "vtk":
        pass
    else:
        print("Error, file format unrecognized: " + input_file)

    ## VTK interactive window
    print(
        '\n Use the interactive window to select your cutting plane. When you are content with your selection, simply close the window. \n'
    )
    # read data from file
    # read data from file
    reader = vtk.vtkPolyDataReader()
    reader.SetFileName(input_vtk)
    reader.ReadAllVectorsOn()
    reader.ReadAllScalarsOn()
    reader.Update()
    # get data
    data = reader.GetOutput()
    (xmin, xmax, ymin, ymax, zmin, zmax) = data.GetBounds()
    (xcenter, ycenter, zcenter) = data.GetCenter()
    #create mapper
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(data)
    # The actor is a grouping mechanism
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    # create camera
    camera = vtk.vtkCamera()
    camera.SetFocalPoint(xcenter, ycenter, zcenter)
    camera.SetPosition(100, -300, -50)
    camera.SetViewUp(0, 0, 1)
    # create a renderer
    renderer = vtk.vtkRenderer()
    renderer.SetActiveCamera(camera)
    renderer.SetBackground(0.2, 0.2, 0.5)
    renderer.SetBackground2(0.4, 0.4, 1.0)
    renderer.SetGradientBackground(True)
    renderer.AddActor(actor)
    # create a render_window
    render_window = vtk.vtkRenderWindow()
    render_window.AddRenderer(renderer)
    render_window.SetSize(1000, 1000)
    # create a renderwindowiren
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(render_window)
    iren.Initialize()
    rep = vtk.vtkImplicitPlaneRepresentation()
    rep.SetPlaceFactor(1.25)
    rep.PlaceWidget(actor.GetBounds())
    rep.SetNormal(0, 0, 1)
    # Create a vtkImagePlaneWidget and activate it
    plane_widget = vtk.vtkImplicitPlaneWidget2()
    plane_widget.SetInteractor(iren)
    plane_widget.SetRepresentation(rep)
    plane_widget.On()
    iren.Initialize()
    iren.Start()

    # use orgin as one point and use normla to solve for two others
    (o1, o2, o3) = rep.GetOrigin()
    (n1, n2, n3) = rep.GetNormal()
    # using x = 1 and y =-1 solve for z
    pt1_z = (-n1 + (n1 * o1) + n2 + (n2 * o2) + (n3 * o3)) / n3
    # using x = -1 and y = 1 solve for z
    pt2_z = (n1 + (n1 * o1) - n2 + (n2 * o2) + (n3 * o3)) / n3
    # fix 0 edge case
    if o1 == 0 and o2 == 0:
        o1 = -1
        o2 = -1
    return np.array([[o1, o2, o3], [1, -1, pt1_z], [-1, 1, pt2_z]])
Example #9
0
    def LerSTL(self, path):
        mesh = vtk.vtkSTLReader()
        mesh.SetFileName(path)
        mesh.Update()

        # faz a mapeação dos dados do stl

        stlMapper = vtk.vtkPolyDataMapper()
        stlMapper.SetInputConnection(mesh.GetOutputPort())
        #represenatação da visualização, neste caso sem ver.

        stlActor = vtk.vtkLODActor()
        stlActor.SetMapper(stlMapper)

        #-------------------------------------------------------------------
        # construção do plano para corte

        plane = self.plane = vtk.vtkPlane()  # chamar a função plano de corte
        PlaneCollection = vtk.vtkPlaneCollection(
        )  # cria uma colecção de planos
        PlaneCollection.AddItem(
            plane)  # estou a adicionar o plano de corte a essa colecção

        # faz o corte e fecha logo de imediato o polidata
        self.clipper = clipper = vtk.vtkClipClosedSurface()
        clipper.SetInputData(mesh.GetOutput())
        clipper.SetClippingPlanes(
            PlaneCollection)  #recebe os meus planos de corte
        clipper.SetBaseColor(0.0, 0.0, 1.0)
        clipper.SetClipColor(0.0, 0.0, 1.0)
        clipper.SetScalarModeToColors()
        clipper.GenerateFacesOn()  #gera a face que fecha o polidata

        # mapeamento da região da polidata cortada.
        clipMapper = vtk.vtkPolyDataMapper()
        clipMapper.SetInputData(clipper.GetOutput())
        clipMapper.ScalarVisibilityOn()
        #-------------------------------------------

        #--------------da a cor a parte cortada----
        backProp = vtk.vtkProperty()
        backProp.SetDiffuseColor((0.0, 1.0, 0.0))
        #---------------------------------------------------
        # faz a visualização do polidata cortada--
        clipActor = vtk.vtkActor()
        clipActor.SetMapper(clipMapper)
        clipActor.GetProperty().SetColor((0.0, 1.0, 0.0))
        #--------------------------------------------------------

        # esta é a representação do wigdet do plano de corte

        rep = vtk.vtkImplicitPlaneRepresentation()
        rep.PlaceWidget(stlActor.GetBounds()
                        )  #é a posição dos limite aonde o plano de corte fica
        rep.SetPlaceFactor(1.25)  # expandir os limite do widget para 25%
        rep.UseBoundsOn()  # mostra a fronteira
        rep.NormalToZAxisOn()  #da a posição de corte, no sentido de z
        rep.OutlineTranslationOff(
        )  # nao posso mexer o widget, so posso mexer o plano de corte
        rep.PlaceWidget(mesh.GetOutput().GetBounds()
                        )  #mostra aonde vai ser posicionado o widget

        rep.OutsideBoundsOn()
        rep.VisibilityOn()
        #............

        # chama o proprio widget , para interação para fazer o corte
        self.planeWidget = vtk.vtkImplicitPlaneWidget2()
        self.planeWidget.SetInteractor(self.Interactor)
        self.planeWidget.SetRepresentation(rep)  # adiconar a representação
        self.planeWidget.SetEnabled(0)
        self.planeWidget.AddObserver(
            "EndInteractionEvent",
            self.myCallback)  # vai tratar o evento para fazero corte

        self.planeWidget.On()

        #---------------------------------------

        # adiconar os actores ao renderer para visualização
        self.renderer.AddActor(stlActor)
        self.renderer.AddActor(clipActor)
        #-------------------------------------------
        #posicionar a camera para poder visualizar a volume todo

        self.renderer.ResetCamera()
        #----------------------------------------
        #faze a renderização total
        self.Interactor.Render()
        #-----------------------------

        # liga o plano para fazer funcionar o widget-
        self.VerPlano = True
cutActor.GetProperty().SetOpacity(1)

# Create the RenderWindow, Renderer and both Actors
#
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.SetMultiSamples(0)
renWin.AddRenderer(ren)
iRen = vtk.vtkRenderWindowInteractor()
iRen.SetRenderWindow(renWin)

# Create the widget, its representation, and callback
def MovePlane(widget, event_string):
    rep.GetPlane(plane)

rep = vtk.vtkImplicitPlaneRepresentation()
rep.SetPlaceFactor(1.0);
rep.PlaceWidget(sample.GetOutput().GetBounds())
rep.DrawPlaneOff()
rep.SetPlane(plane)

planeWidget = vtk.vtkImplicitPlaneWidget2()
planeWidget.SetInteractor(iRen)
planeWidget.SetRepresentation(rep);
planeWidget.AddObserver("InteractionEvent",MovePlane);

recorder = vtk.vtkInteractorEventRecorder()
recorder.SetInteractor(iRen)
recorder.ReadFromInputStringOn()
recorder.SetInputString(Recording)
Example #11
0
def ShowCuttingPlanesOnImage(input_file, cutting_planes):
    ## Get vtk format
    file_format = input_file.split(".")[-1]
    input_vtk = input_file.replace(file_format, "vtk")
    if file_format == "nrrd":
        image = Image(input_file)
        mesh = image.toMesh()
        mesh.write(input_vtk)
        print("\nCreating mesh from: " + input_file)
        print("\nSaving as: " + input_vtk)
    elif file_format == "ply" or file_format == "stl":
        mesh = Mesh(file_format)
        mesh.write(input_vtk)
    else:
        print("Error, file format unrecognized: " + input_file)

    ## VTK interactive window
    print('\n Use the interactive window to select your cutting plane. When you are content with your selection, simply close the window. \n')
    # read data from file
    reader = vtk.vtkPolyDataReader()
    reader.SetFileName(input_vtk)
    reader.ReadAllVectorsOn()
    reader.ReadAllScalarsOn()
    reader.Update()
    # get data
    data = reader.GetOutput()
    (xcenter, ycenter, zcenter) = data.GetCenter()
    #create mapper
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(data)
    # The actor is a grouping mechanism
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    # create camera
    camera = vtk.vtkCamera()
    camera.SetFocalPoint(xcenter, ycenter, zcenter)
    camera.SetPosition(100, -300, -50)
    camera.SetViewUp(0,0,1)
    # create a renderer
    renderer = vtk.vtkRenderer()
    renderer.SetActiveCamera(camera)
    renderer.SetBackground(0.2, 0.2, 0.5)
    renderer.SetBackground2(0.4, 0.4, 1.0)
    renderer.SetGradientBackground(True)
    renderer.AddActor(actor)
    # create a render_window
    render_window = vtk.vtkRenderWindow()
    render_window.AddRenderer(renderer)
    render_window.SetSize(1000,1000)
    # create a renderwindowiren
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(render_window)
    iren.Initialize()
    # Create a vtkImagePlaneWidget and activate it
    reps = []
    plane_widgets = []
    print(len(cutting_planes))
    for plane in cutting_planes:
        print(plane)
        A = np.array(plane[0])
        B = np.array(plane[1])
        C = np.array(plane[2])
        n = np.cross(B-A, C-A)
        n = n/np.linalg.norm(n)
        d = -np.dot(A, n)
        print(n, d)
        rep = vtk.vtkImplicitPlaneRepresentation()
        rep.SetPlaceFactor(1.5)
        rep.SetOrigin(A)
        rep.PlaceWidget(actor.GetBounds())
        rep.SetNormal(n[0],n[1],n[2])
        reps.append(rep)
        plane_widget = vtk.vtkImplicitPlaneWidget2()
        plane_widget.SetInteractor(iren)
        plane_widget.SetRepresentation(rep)
        plane_widget.On()
        plane_widgets.append(plane_widget)
    iren.Initialize()
    iren.Start()
    '''
Example #12
0
def main(argv):
  # Create some synthetic data
  #
  # Create a synthetic source: sample a sphere across a volume
  sample = None
  if len(argv) > 1:
    # TODO: FIX ME
    fileName = argv[1]
    sample = vtk.vtkMetaImageReader()
    sample.SetFileName(fileName)
    sample.Update()
  else:
    sphere = vtk.vtkSphere()
    sphere.SetCenter( 0.0,0.0,0.0)
    sphere.SetRadius(0.25)
    
    res = 100
    sample = vtk.vtkSampleFunction()
    sample.SetImplicitFunction(sphere)
    sample.SetModelBounds(-0.5,0.5, -0.5,0.5, -0.5,0.5)
    sample.SetSampleDimensions(res,res,res)
    sample.SetOutputScalarTypeToFloat()
    sample.Update()
  
  # The cut plane
  plane = vtk.vtkPlane()
  plane.SetOrigin(0,0,0)
  plane.SetNormal(1,1,1)

  # Replace with vtkImageReslice
  
  cut = vtk.vtkFlyingEdgesPlaneCutter()
  cut.SetInputConnection(sample.GetOutputPort())
  cut.SetPlane(plane)
  cut.ComputeNormalsOff()

  cutMapper = vtk.vtkPolyDataMapper()
  cutMapper.SetInputConnection(cut.GetOutputPort())
  
  cutActor = vtk.vtkActor()
  cutActor.SetMapper(cutMapper)
  cutActor.GetProperty().SetColor(1,1,1)
  cutActor.GetProperty().SetOpacity(1)
  
  # Create the RenderWindow, Renderer and both Actors
  #
  ren = vtk.vtkRenderer()
  renWin = vtk.vtkRenderWindow()
  renWin.SetMultiSamples(0)
  renWin.AddRenderer(ren)
  iRen = vtk.vtkRenderWindowInteractor()
  iRen.SetRenderWindow(renWin)
  
  # Create the widget, its representation, and callback
  def MovePlane(widget, event_string):
      rep.GetPlane(plane)
  
  rep = vtk.vtkImplicitPlaneRepresentation()
  rep.SetPlaceFactor(1.0);
  rep.PlaceWidget(sample.GetOutput().GetBounds())
  rep.DrawPlaneOff()
  rep.SetPlane(plane)
  
  planeWidget = vtk.vtkImplicitPlaneWidget2()
  planeWidget.SetInteractor(iRen)
  planeWidget.SetRepresentation(rep);
  planeWidget.AddObserver("InteractionEvent",MovePlane);
  
  recorder = vtk.vtkInteractorEventRecorder()
  recorder.SetInteractor(iRen)
  recorder.ReadFromInputStringOn()
  recorder.SetInputString(Recording)
  
  # Add the actors to the renderer, set the background and size
  #
  ren.AddActor(cutActor)
  ren.SetBackground(1, 1, 1)
  renWin.SetSize(300, 300)
  ren.SetBackground(0.1, 0.2, 0.4)
  
  iRen.Initialize()
  renWin.Render()
  planeWidget.On()
  
  # Actually cut the data
  recorder.Play()
  iRen.Start()