コード例 #1
0
 def initialize(self):
     self.layoutManager=slicer.app.layoutManager()
     self.threeDWidget=self.layoutManager.threeDWidget(0)
     self.threeDView=self.threeDWidget.threeDView()
     self.renderWindow=self.threeDView.renderWindow()
     self.renderers=self.renderWindow.GetRenderers()
     self.renderer=self.renderers.GetFirstRenderer()
     self.actor = vtk.vtkActor()
コード例 #2
0
 def initialize(self):
     self.layoutManager=slicer.app.layoutManager()
     self.threeDWidget=self.layoutManager.threeDWidget(0)
     self.threeDView=self.threeDWidget.threeDView()
     self.renderWindow=self.threeDView.renderWindow()
     self.renderers=self.renderWindow.GetRenderers()
     self.renderer=self.renderers.GetFirstRenderer()
     
     self.polydata = vtk.vtkPolyData()
     self.points = vtk.vtkPoints()
     self.planeSource = vtk.vtkPlaneSource()
     self.mapper = vtk.vtkPolyDataMapper()
     self.actor = vtk.vtkActor()
     self.renderer.AddViewProp(self.actor)
     self.renderWindow.AddRenderer(self.renderer)
コード例 #3
0
    def initialize(self):
        self.layoutManager = slicer.app.layoutManager()
        self.threeDWidget = self.layoutManager.threeDWidget(0)
        self.threeDView = self.threeDWidget.threeDView()
        self.renderWindow = self.threeDView.renderWindow()
        self.renderers = self.renderWindow.GetRenderers()
        self.renderer = self.renderers.GetFirstRenderer()

        self.polydata = vtk.vtkPolyData()
        self.points = vtk.vtkPoints()
        self.planeSource = vtk.vtkPlaneSource()
        self.mapper = vtk.vtkPolyDataMapper()
        self.actor = vtk.vtkActor()
        self.renderer.AddViewProp(self.actor)
        self.renderWindow.AddRenderer(self.renderer)
コード例 #4
0
ファイル: ShadedModels.py プロジェクト: afruehstueck/CommonGL
  def test_ShadedModels1(self):
    """ Ideally you should have several levels of tests.  At the lowest level
    tests should exercise the functionality of the logic with different inputs
    (both valid and invalid).  At higher levels your tests should emulate the
    way the user would interact with your code and confirm that it still works
    the way you intended.
    One of the most important features of the tests is that it should alert other
    developers when their changes will have an impact on the behavior of your
    module.  For example, if a developer removes a feature that you depend on,
    your test should break so they know that the feature is needed.
    """

    self.delayDisplay("Starting the test")

    import SampleData
    sampleDataLogic = SampleData.SampleDataLogic()
    print("Getting MR Head Volume")
    mrHeadVolume = sampleDataLogic.downloadMRHead()

    resize = vtk.vtkImageResize()
    resize.SetInputDataObject(mrHeadVolume.GetImageData())
    resize.SetOutputDimensions(256,256,128)
    resize.Update()


    tdw = slicer.qMRMLThreeDWidget()
    tdw.setMRMLScene(slicer.mrmlScene)
    tdw.setMRMLViewNode(slicer.util.getNode("*ViewNode*"))
    tdw.show()

    from vtkSlicerShadedActorModuleLogicPython import *

    shadedActor=vtkOpenGLShadedActor()
    tdv = tdw.threeDView()
    rw = tdv.renderWindow()
    rens = rw.GetRenderers()
    ren = rens.GetItemAsObject(0)
    ren.AddActor(shadedActor)
    mapper = vtk.vtkPolyDataMapper()
    shadedActor.SetMapper(mapper)
    shadedActor.SetVertexShaderSource("""
      #version 120
      attribute vec3 vertexAttribute;
      attribute vec2 textureCoordinateAttribute;
      varying vec4 interpolatedColor;
      varying vec3 interpolatedTextureCoordinate;
      void main()
      {
        interpolatedColor = vec4(0.5) + vec4(vertexAttribute, 1.);
        interpolatedTextureCoordinate = vec3(textureCoordinateAttribute, .5);
        gl_Position = vec4(vertexAttribute, 1.);
      }
    """)

    shadedActor.SetFragmentShaderSource("""
      #version 120
      varying vec4 interpolatedColor;
      varying vec3 interpolatedTextureCoordinate;
      uniform sampler3D volumeSampler;
      void main()
      {
        gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );
        gl_FragColor = interpolatedColor;
        vec4 volumeSample = texture3D(volumeSampler, interpolatedTextureCoordinate);
        volumeSample *= 100;
        gl_FragColor = mix( volumeSample, interpolatedColor, 0.5);
        
        vec4 integratedRay = vec4(0.);
        for (int i = 0; i < 256; i++) {
          vec3 samplePoint = vec3(interpolatedTextureCoordinate.st, i/256.);
          vec4 volumeSample = texture3D(volumeSampler, samplePoint);
          integratedRay += volumeSample;
        }
        gl_FragColor = integratedRay;
      }
    """)
    shadedActor.SetTextureImageData(resize.GetOutputDataObject(0))
    #shadedActor.SetTextureImageData(mrHeadVolume.GetImageData())
    sphereSource = vtk.vtkSphereSource()
    mapper.SetInputConnection(sphereSource.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetScale(20,20,20)
    actor.SetMapper(mapper)
    ren.AddActor(actor)
    rw.Render()

    wti = vtk.vtkWindowToImageFilter()
    wti.SetInput(rw)
    iv = vtk.vtkImageViewer()
    iv.SetColorLevel(128)
    iv.SetColorWindow(256)
    iv.SetInputConnection(wti.GetOutputPort())
    iv.Render()

    extensionManager = vtk.vtkOpenGLExtensionManager()
    extensionManager.SetRenderWindow(rw)
    extensionManager.Update()
    print(extensionManager) 
    print(extensionManager.GetDriverGLVendor()) 
    print(extensionManager.GetDriverGLVersion()) 
    print(extensionManager.GetDriverGLRenderer()) 

    slicer.modules.ShadedModelsWidget.tdw = tdw
    slicer.modules.ShadedModelsWidget.iv = iv
コード例 #5
0
  def makeScene(self, sliceLogic):
    sliceNode = sliceLogic.GetBackgroundLayer().GetSliceNode()
    sliceViewName = sliceNode.GetLayoutName()

    if self.sliceViews[sliceViewName]:
      ren = self.renderers[sliceViewName]
      rw = self.sliceViews[sliceViewName].renderWindow()
      ren.SetViewport(self.viewPortStartWidth,0,1,self.viewPortFinishHeight)
      ren.SetLayer(1)

      if self.showHumanModelCheckBox.checked:
        if self.humanActor == None:
          #
          # Making vtk mappers and actors
          #
          # Mappers
          humanMapper = vtk.vtkPolyDataMapper()
          if vtk.VTK_MAJOR_VERSION <= 5:
            humanMapper.SetInput(self.humanNode.GetPolyData())
          else:
            humanMapper.SetInputData(self.humanNode.GetPolyData())

          shortsMapper = vtk.vtkPolyDataMapper()
          if vtk.VTK_MAJOR_VERSION <= 5:
            shortsMapper.SetInput(self.shortsNode.GetPolyData())
          else:
            shortsMapper.SetInputData(self.shortsNode.GetPolyData())

          leftShoeMapper = vtk.vtkPolyDataMapper()
          if vtk.VTK_MAJOR_VERSION <= 5:
            leftShoeMapper.SetInput(self.leftShoeNode.GetPolyData())
          else:
            leftShoeMapper.SetInputData(self.leftShoeNode.GetPolyData())

          rightShoeMapper = vtk.vtkPolyDataMapper()
          if vtk.VTK_MAJOR_VERSION <= 5:
            rightShoeMapper.SetInput(self.rightShoeNode.GetPolyData())
          else:
            rightShoeMapper.SetInputData(self.rightShoeNode.GetPolyData())

          # Actors
          humanModelScale = 0.01
          self.humanActor = vtk.vtkActor()
          self.humanActor.SetMapper(humanMapper)
          #self.humanActor.GetProperty().SetColor(0.93,0.81,0.80)
          self.humanActor.GetProperty().SetColor(255/256,204/256,34/256)
          self.humanActor.SetScale(humanModelScale,humanModelScale,humanModelScale)

          self.shortsActor = vtk.vtkActor()
          self.shortsActor.SetMapper(shortsMapper)
          #self.shortsActor.GetProperty().SetColor(0,0,1)
          self.shortsActor.GetProperty().SetColor(0,0,1)
          self.shortsActor.SetScale(humanModelScale,humanModelScale,humanModelScale)

          self.leftShoeActor = vtk.vtkActor()
          self.leftShoeActor.SetMapper(leftShoeMapper)
          self.leftShoeActor.GetProperty().SetColor(1,0,0)
          self.leftShoeActor.SetScale(humanModelScale,humanModelScale,humanModelScale)

          self.rightShoeActor = vtk.vtkActor()
          self.rightShoeActor.SetMapper(rightShoeMapper)
          self.rightShoeActor.GetProperty().SetColor(0,1,0)
          self.rightShoeActor.SetScale(humanModelScale,humanModelScale,humanModelScale)

        if self.cube == None:
          self.cube = vtk.vtkAnnotatedCubeActor()
          self.cube.SetXPlusFaceText('R')
          self.cube.SetXMinusFaceText('L')
          self.cube.SetYMinusFaceText('A')
          self.cube.SetYPlusFaceText('P')
          self.cube.SetZMinusFaceText('I')
          self.cube.SetZPlusFaceText('S')
          self.cube.SetZFaceTextRotation(90)
          self.cube.GetTextEdgesProperty().SetColor(0.95,0.95,0.95)
          self.cube.GetTextEdgesProperty().SetLineWidth(1)
          self.cube.GetCubeProperty().SetColor(0.15,0.15,0.15)

        if self.axes == None:
          self.axes = vtk.vtkAxesActor()
          self.axes.SetXAxisLabelText('R')
          self.axes.SetYAxisLabelText('A')
          self.axes.SetZAxisLabelText('S')
          transform = vtk.vtkTransform()
          transform.Translate(0,0,0)
          self.axes.SetUserTransform(transform)

        # Add actors to renderer
        if self.cubeRadioButton.checked:
          ren.AddActor(self.cube)
          ren.RemoveActor(self.humanActor)
          ren.RemoveActor(self.shortsActor)
          ren.RemoveActor(self.leftShoeActor)
          ren.RemoveActor(self.rightShoeActor)
          ren.RemoveActor(self.axes)

        if self.axesRadioButton.checked:
          ren.AddActor(self.axes)
          ren.RemoveActor(self.humanActor)
          ren.RemoveActor(self.shortsActor)
          ren.RemoveActor(self.leftShoeActor)
          ren.RemoveActor(self.rightShoeActor)
          ren.RemoveActor(self.cube)

        if self.humanRadioButton.checked:
          ren.AddActor(self.humanActor)
          ren.AddActor(self.shortsActor)
          ren.AddActor(self.leftShoeActor)
          ren.AddActor(self.rightShoeActor)
          ren.RemoveActor(self.cube)
          ren.RemoveActor(self.axes)

        # Calculate the camera position and viewup based on XYToRAS matrix
        camera = vtk.vtkCamera()

        m = sliceNode.GetSliceToRAS()
 
        v = np.array([[m.GetElement(0,0),m.GetElement(0,1),m.GetElement(0,2)],
            [m.GetElement(1,0),m.GetElement(1,1),m.GetElement(1,2)],
            [m.GetElement(2,0),m.GetElement(2,1),m.GetElement(2,2)]])
        det = np.linalg.det(v)
        if det > 0: # right hand
          y = np.array([0,0,-self.cameraPositionMultiplier])
        elif det < 0: # left hand
          y = np.array([0,0,self.cameraPositionMultiplier])
 
        x = np.matrix([[m.GetElement(0,0),m.GetElement(0,1),m.GetElement(0,2)],
            [m.GetElement(1,0),m.GetElement(1,1),m.GetElement(1,2)],
            [m.GetElement(2,0),m.GetElement(2,1),m.GetElement(2,2)]])
 
        # Calculating position
        position = np.inner(x,y)
        camera.SetPosition(-position[0,0],-position[0,1],-position[0,2])

        '''
        m = sliceNode.GetSliceToRAS()
        x = np.matrix([[m.GetElement(0,0),m.GetElement(0,1),m.GetElement(0,2)],
            [m.GetElement(1,0),m.GetElement(1,1),m.GetElement(1,2)],
            [m.GetElement(2,0),m.GetElement(2,1),m.GetElement(2,2)]])

        # Calculating position
        y = np.array([0,0,self.cameraPositionMultiplier])
        position = np.inner(x,y)
        camera.SetPosition(-position[0,0],-position[0,1],-position[0,2])

        '''
        # Calculating viewUp
        n = np.array([0,1,0])
        viewUp = np.inner(x,n)
        camera.SetViewUp(viewUp[0,0],viewUp[0,1],viewUp[0,2])

        #ren.PreserveDepthBufferOff()
        ren.SetActiveCamera(camera)
        rw.AddRenderer(ren)

      else:
        ren.RemoveActor(self.humanActor)
        ren.RemoveActor(self.shortsActor)
        ren.RemoveActor(self.leftShoeActor)
        ren.RemoveActor(self.rightShoeActor)
        ren.RemoveActor(self.axes)
        ren.RemoveActor(self.cube)
        rw.RemoveRenderer(ren)

      # Refresh view
      self.sliceViews[sliceViewName].scheduleRender()
コード例 #6
0
ファイル: Constructor.py プロジェクト: willyci/SlicerFab
  def iceCream(self):

    # based on iceCream.py from VTK/Examples/Modelling

    # This example demonstrates how to use boolean combinations of implicit
    # functions to create a model of an ice cream cone.

    import vtk
    from vtk.util.colors import chocolate, mint

    # Create implicit function primitives. These have been carefully
    # placed to give the effect that we want. We are going to use various
    # combinations of these functions to create the shape we want; for
    # example, we use planes intersected with a cone (which is infinite in
    # extent) to get a finite cone.
    cone = vtk.vtkCone()
    cone.SetAngle(20)
    vertPlane = vtk.vtkPlane()
    vertPlane.SetOrigin(.1, 0, 0)
    vertPlane.SetNormal(-1, 0, 0)
    basePlane = vtk.vtkPlane()
    basePlane.SetOrigin(1.2, 0, 0)
    basePlane.SetNormal(1, 0, 0)
    iceCream = vtk.vtkSphere()
    iceCream.SetCenter(1.333, 0, 0)
    iceCream.SetRadius(0.5)
    bite = vtk.vtkSphere()
    bite.SetCenter(1.5, 0, 0.5)
    bite.SetRadius(0.25)

    # Combine primitives to build ice-cream cone. Clip the cone with planes.
    theCone = vtk.vtkImplicitBoolean()
    theCone.SetOperationTypeToIntersection()
    theCone.AddFunction(cone)
    theCone.AddFunction(vertPlane)
    theCone.AddFunction(basePlane)

    # Take a bite out of the ice cream.
    theCream = vtk.vtkImplicitBoolean()
    theCream.SetOperationTypeToDifference()
    theCream.AddFunction(iceCream)
    theCream.AddFunction(bite)

    # The sample function generates a distance function from the implicit
    # function (which in this case is the cone). This is then contoured to
    # get a polygonal surface.
    theConeSample = vtk.vtkSampleFunction()
    theConeSample.SetImplicitFunction(theCone)
    theConeSample.SetModelBounds(-1, 1.5, -1.25, 1.25, -1.25, 1.25)
    theConeSample.SetSampleDimensions(60, 60, 60)
    theConeSample.ComputeNormalsOff()
    theConeSurface = vtk.vtkContourFilter()
    theConeSurface.SetInputConnection(theConeSample.GetOutputPort())
    theConeSurface.SetValue(0, 0.0)
    coneMapper = vtk.vtkPolyDataMapper()
    coneMapper.SetInputConnection(theConeSurface.GetOutputPort())
    coneMapper.ScalarVisibilityOff()
    coneActor = vtk.vtkActor()
    coneActor.SetMapper(coneMapper)
    coneActor.GetProperty().SetColor(chocolate)

    # The same here for the ice cream.
    theCreamSample = vtk.vtkSampleFunction()
    theCreamSample.SetImplicitFunction(theCream)
    theCreamSample.SetModelBounds(0, 2.5, -1.25, 1.25, -1.25, 1.25)
    theCreamSample.SetSampleDimensions(60, 60, 60)
    theCreamSample.ComputeNormalsOff()
    theCreamSurface = vtk.vtkContourFilter()
    theCreamSurface.SetInputConnection(theCreamSample.GetOutputPort())
    theCreamSurface.SetValue(0, 0.0)
    creamMapper = vtk.vtkPolyDataMapper()
    creamMapper.SetInputConnection(theCreamSurface.GetOutputPort())
    creamMapper.ScalarVisibilityOff()
    creamActor = vtk.vtkActor()
    creamActor.SetMapper(creamMapper)
    creamActor.GetProperty().SetColor(mint)

    # Create the usual rendering stuff
    ren = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    # Add the actors to the renderer, set the background and size
    ren.AddActor(coneActor)
    ren.AddActor(creamActor)
    ren.SetBackground(1, 1, 1)
    renWin.SetSize(500, 500)
    ren.ResetCamera()
    ren.GetActiveCamera().Roll(90)
    ren.GetActiveCamera().Dolly(1.5)
    ren.ResetCameraClippingRange()

    iren.Initialize()
    renWin.Render()
    iren.Start()
    return iren
コード例 #7
0
ファイル: ShadedModels.py プロジェクト: pieper/CommonGL
    def test_ShadedModels1(self):
        """ Ideally you should have several levels of tests.  At the lowest level
    tests should exercise the functionality of the logic with different inputs
    (both valid and invalid).  At higher levels your tests should emulate the
    way the user would interact with your code and confirm that it still works
    the way you intended.
    One of the most important features of the tests is that it should alert other
    developers when their changes will have an impact on the behavior of your
    module.  For example, if a developer removes a feature that you depend on,
    your test should break so they know that the feature is needed.
    """

        self.delayDisplay("Starting the test")

        import SampleData
        sampleDataLogic = SampleData.SampleDataLogic()
        print("Getting MR Head Volume")
        mrHeadVolume = sampleDataLogic.downloadMRHead()

        resize = vtk.vtkImageResize()
        resize.SetInputDataObject(mrHeadVolume.GetImageData())
        resize.SetOutputDimensions(256, 256, 128)
        resize.Update()

        tdw = slicer.qMRMLThreeDWidget()
        tdw.setMRMLScene(slicer.mrmlScene)
        tdw.setMRMLViewNode(slicer.util.getNode("*ViewNode*"))
        tdw.show()

        from vtkSlicerShadedActorModuleLogicPython import *

        shadedActor = vtkOpenGLShadedActor()
        tdv = tdw.threeDView()
        rw = tdv.renderWindow()
        rens = rw.GetRenderers()
        ren = rens.GetItemAsObject(0)
        ren.AddActor(shadedActor)
        mapper = vtk.vtkPolyDataMapper()
        shadedActor.SetMapper(mapper)
        shadedActor.SetVertexShaderSource("""
      #version 120
      attribute vec3 vertexAttribute;
      attribute vec2 textureCoordinateAttribute;
      varying vec4 interpolatedColor;
      varying vec3 interpolatedTextureCoordinate;
      void main()
      {
        interpolatedColor = vec4(0.5) + vec4(vertexAttribute, 1.);
        interpolatedTextureCoordinate = vec3(textureCoordinateAttribute, .5);
        gl_Position = vec4(vertexAttribute, 1.);
      }
    """)

        shadedActor.SetFragmentShaderSource("""
      #version 120
      varying vec4 interpolatedColor;
      varying vec3 interpolatedTextureCoordinate;
      uniform sampler3D volumeSampler;
      void main()
      {
        gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );
        gl_FragColor = interpolatedColor;
        vec4 volumeSample = texture3D(volumeSampler, interpolatedTextureCoordinate);
        volumeSample *= 100;
        gl_FragColor = mix( volumeSample, interpolatedColor, 0.5);
        
        vec4 integratedRay = vec4(0.);
        for (int i = 0; i < 256; i++) {
          vec3 samplePoint = vec3(interpolatedTextureCoordinate.st, i/256.);
          vec4 volumeSample = texture3D(volumeSampler, samplePoint);
          integratedRay += volumeSample;
        }
        gl_FragColor = integratedRay;
      }
    """)
        shadedActor.SetTextureImageData(resize.GetOutputDataObject(0))
        #shadedActor.SetTextureImageData(mrHeadVolume.GetImageData())
        sphereSource = vtk.vtkSphereSource()
        mapper.SetInputConnection(sphereSource.GetOutputPort())
        actor = vtk.vtkActor()
        actor.SetScale(20, 20, 20)
        actor.SetMapper(mapper)
        ren.AddActor(actor)
        rw.Render()

        wti = vtk.vtkWindowToImageFilter()
        wti.SetInput(rw)
        iv = vtk.vtkImageViewer()
        iv.SetColorLevel(128)
        iv.SetColorWindow(256)
        iv.SetInputConnection(wti.GetOutputPort())
        iv.Render()

        extensionManager = vtk.vtkOpenGLExtensionManager()
        extensionManager.SetRenderWindow(rw)
        extensionManager.Update()
        print(extensionManager)
        print(extensionManager.GetDriverGLVendor())
        print(extensionManager.GetDriverGLVersion())
        print(extensionManager.GetDriverGLRenderer())

        slicer.modules.ShadedModelsWidget.tdw = tdw
        slicer.modules.ShadedModelsWidget.iv = iv
コード例 #8
0
    def vtk_to_obj_converter(self, node, radius=0.1, number_of_sides=3):

        qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)

        polydata = node.GetPolyData()
        tuber = vtk.vtkTubeFilter()
        tuber.SetNumberOfSides(int(number_of_sides))
        tuber.SetRadius(radius)
        tuber.SetInputData(polydata)
        tuber.Update()

        tubes = tuber.GetOutputDataObject(0)
        # scalars = tubes.GetPointData().GetArray(0)
        # scalars.SetName("scalars")

        triangles = vtk.vtkTriangleFilter()
        triangles.SetInputData(tubes)
        triangles.Update()

        tripolydata = vtk.vtkPolyData()
        tripolydata.ShallowCopy(triangles.GetOutput())

        # Decrease the number of triangle of 30% to reduce Blender loading costs
        decimate = vtk.vtkDecimatePro()
        decimate.SetInputData(tripolydata)
        decimate.SetTargetReduction(.30)
        decimate.Update()

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(decimate.GetOutputPort())

        r = random.randrange(0, 256, 1)
        g = random.randrange(0, 256, 1)
        b = random.randrange(0, 256, 1)

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        # actor.GetProperty().SetColor(0, 0, 0)  # Ka: ambient color of the material (r, g, b)
        actor.GetProperty().SetDiffuseColor(
            r, g, b)  # Kd: diffuse color of the material (r, g, b)
        # actor.GetProperty().SetSpecularColor(0, 0, 0)  # Ks: specular color of the material (r, g, b)

        renderer = vtk.vtkRenderer()
        renderer.AddActor(actor)

        window = vtk.vtkRenderWindow()
        window.AddRenderer(renderer)

        # Get output path
        storageNode = node.GetStorageNode()
        filepath = storageNode.GetFullNameFromFileName()
        filename = filepath.rsplit('.', 1)

        writer = vtk.vtkOBJExporter()
        writer.SetFilePrefix(filename[0])
        writer.SetInput(window)
        writer.Write()

        qt.QApplication.restoreOverrideCursor()

        if writer.Write() == 0:
            qt.QMessageBox.critical(None, "Conversion", "Conversion failed")
        else:
            qt.QMessageBox.information(None, "Conversion", "Conversion done")