Пример #1
0
ctfun.AddRGBPoint(1.0, 1, 1, 1)
ctfun.AddRGBPoint(2.0, 0.5, 0.0, 0.0)
ctfun.AddRGBPoint(5.5, 1.0, 0.5, 0.5)
ctfun.AddRGBPoint(7.0, 0.9, 0.2, 0.3)
ctfun.AddRGBPoint(9.0, 0.81, 0.27, 0.1)
ctfun.AddRGBPoint(11.0, 0.5, 0.5, 0.5)

delny = vtk.vtkDelaunay3D()
delny.SetInput(diskGlyph.GetOutput())
delny.SetTolerance(1.0)
delny.SetAlpha(2.0)
delny.BoundingTriangulationOff()
delnyMapper=vtk.vtkDataSetMapper()
delnyMapper.SetInput(delny.GetOutput())

volumeMapper=vtk.vtkUnstructuredGridVolumeRayCastMapper()
volumeMapper.SetInput(delny.GetOutput())
volprop=vtk.vtkVolumeProperty()
volprop.SetColor(ctfun)
volprop.SetInterpolationTypeToLinear()


# Create a renderer and add the actors to it
#NTM: I also create the actors in here.
renderer = vtk.vtkRenderer()
renderer.SetBackground(0, 0, 0)
renderer.AddActor(ballActor)
renderer.AddActor(tubeActor)

volume=vtk.vtkVolume()
volume.SetMapper(volumeMapper)
Пример #2
0
    def __init__(
        self,
        inputobj=None,
        c=('r', 'y', 'lg', 'lb', 'b'),  #('b','lb','lg','y','r')
        alpha=(0.5, 1),
        alphaUnit=1,
        mapper='tetra',
    ):

        BaseGrid.__init__(self)

        self.useArray = 0

        inputtype = str(type(inputobj))
        #printc('TetMesh inputtype', inputtype)

        ###################
        if inputobj is None:
            self._data = vtk.vtkUnstructuredGrid()

        elif isinstance(inputobj, vtk.vtkUnstructuredGrid):
            self._data = inputobj

        elif isinstance(inputobj, vtk.vtkRectilinearGrid):
            r2t = vtk.vtkRectilinearGridToTetrahedra()
            r2t.SetInputData(inputobj)
            r2t.RememberVoxelIdOn()
            r2t.SetTetraPerCellTo6()
            r2t.Update()
            self._data = r2t.GetOutput()

        elif isinstance(inputobj, vtk.vtkDataSet):
            r2t = vtk.vtkDataSetTriangleFilter()
            r2t.SetInputData(inputobj)
            #r2t.TetrahedraOnlyOn()
            r2t.Update()
            self._data = r2t.GetOutput()

        elif isinstance(inputobj, str):
            from vedo.io import download, loadUnStructuredGrid
            if "https://" in inputobj:
                inputobj = download(inputobj, verbose=False)
            ug = loadUnStructuredGrid(inputobj)
            tt = vtk.vtkDataSetTriangleFilter()
            tt.SetInputData(ug)
            tt.SetTetrahedraOnly(True)
            tt.Update()
            self._data = tt.GetOutput()

        elif utils.isSequence(inputobj):
            # if "ndarray" not in inputtype:
            #     inputobj = np.array(inputobj)
            self._data = self._buildtetugrid(inputobj[0], inputobj[1])

        ###################
        if 'tetra' in mapper:
            self._mapper = vtk.vtkProjectedTetrahedraMapper()
        elif 'ray' in mapper:
            self._mapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
        elif 'zs' in mapper:
            self._mapper = vtk.vtkUnstructuredGridVolumeZSweepMapper()
        elif isinstance(mapper, vtk.vtkMapper):
            self._mapper = mapper
        else:
            printc('Unknown mapper type', [mapper], c='r')
            raise RuntimeError()

        self._mapper.SetInputData(self._data)
        self.SetMapper(self._mapper)
        self.color(c).alpha(alpha)
        if alphaUnit:
            self.GetProperty().SetScalarOpacityUnitDistance(alphaUnit)

        # remember stuff:
        self._color = c
        self._alpha = alpha
        self._alphaUnit = alphaUnit
Пример #3
0
    def __init__(self, parent=None):

        super(VTK_Widget1, self).__init__(parent)
        self.source_is_connected = False
        
        # vtk to point data
        self.c2p = vtk.vtkCellDataToPointData()
        self.opacityTransferFunction = vtk.vtkPiecewiseFunction()
        self.colorTransferFunction = vtk.vtkColorTransferFunction()

        # create a volume property for describing how the data will look
        self.volumeProperty = vtk.vtkVolumeProperty()
        self.volumeProperty.SetColor(self.colorTransferFunction)
        self.volumeProperty.SetScalarOpacity(self.opacityTransferFunction)
        self.volumeProperty.ShadeOn()
        self.volumeProperty.SetInterpolationTypeToLinear()
        
        # convert to unstructured grid volume
        self.triangleFilter = vtk.vtkDataSetTriangleFilter()
        self.triangleFilter.TetrahedraOnlyOn()
        self.triangleFilter.SetInputConnection(self.c2p.GetOutputPort())

        # create a ray cast mapper
        self.compositeFunction = vtk.vtkUnstructuredGridBunykRayCastFunction()
        self.volumeMapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
        self.volumeMapper.SetRayCastFunction(self.compositeFunction)
        self.volumeMapper.SetInputConnection(self.triangleFilter.GetOutputPort())
        
        # create a volume
        self.volume = vtk.vtkVolume()
        self.volume.SetMapper(self.volumeMapper)
        self.volume.SetProperty(self.volumeProperty)
        self.volume.VisibilityOff()
        
        # create the VTK widget for rendering
        self.vtkw=QVTKRenderWindowInteractor(self)
        self.ren = vtk.vtkRenderer()
        self.vtkw.GetRenderWindow().AddRenderer(self.ren)
        self.ren.AddVolume(self.volume)
        
        self.alphaSlider = QSlider(Qt.Horizontal)
        self.alphaSlider.setValue(50)
        self.alphaSlider.setRange(0,100)
        self.alphaSlider.setTickPosition(QSlider.NoTicks) 
        self.connect(self.alphaSlider,SIGNAL("valueChanged(int)"),self.AdjustAlpha)
        
        self.alphaLabel = QLabel("alpha: ")
        
        # layout manager
        self.layout = QVBoxLayout()
        self.layout2 = QHBoxLayout()
        self.layout2.addWidget(self.alphaLabel)
        self.layout2.addWidget(self.alphaSlider)
        self.layout.addWidget(self.vtkw)
        self.layout.addSpacing(34)
        self.layout.addLayout(self.layout2)
        
        self.setLayout(self.layout)
        
        # initialize the interactor
        self.vtkw.Initialize()
        self.vtkw.Start()
Пример #4
0
opacityTransferFunction.AddPoint(255,0.2)
# Create transfer mapping scalar value to color
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.AddRGBPoint(80.0,0.0,0.0,0.0)
colorTransferFunction.AddRGBPoint(120.0,0.0,0.0,1.0)
colorTransferFunction.AddRGBPoint(160.0,1.0,0.0,0.0)
colorTransferFunction.AddRGBPoint(200.0,0.0,1.0,0.0)
colorTransferFunction.AddRGBPoint(255.0,0.0,1.0,1.0)
# The property describes how the data will look
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.ShadeOff()
volumeProperty.SetInterpolationTypeToLinear()
# The mapper / ray cast function know how to render the data
volumeMapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
volumeMapper.SetInputConnection(trifilter.GetOutputPort())
# The volume holds the mapper and the property and
# can be used to position/orient the volume
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
# contour the second dataset
contour = vtk.vtkContourFilter()
contour.SetValue(0,80)
contour.SetInputConnection(reader2.GetOutputPort())
# create a mapper for the polygonal data
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(contour.GetOutputPort())
mapper.ScalarVisibilityOff()
# create an actor for the polygonal data
Пример #5
0
    def __init__(
        self,
        inputobj=None,
        c=('b', 'lb', 'lg', 'y', 'r'),
        alpha=(0.0, 0.0, 0.2, 0.4, 0.8, 1),
        alphaGradient=None,
        mode=0,
        origin=None,
        spacing=None,
        shape=None,
        mapperType='smart',
    ):

        vtk.vtkVolume.__init__(self)
        ActorBase.__init__(self)

        inputtype = str(type(inputobj))
        #colors.printc('Volume inputtype', inputtype)

        if isinstance(inputobj, str):
            import glob
            inputobj = sorted(glob.glob(inputobj))

        if inputobj is None:
            img = vtk.vtkImageData()

        elif utils.isSequence(inputobj):

            if isinstance(inputobj[0], str):  # scan sequence of BMP files
                ima = vtk.vtkImageAppend()
                ima.SetAppendAxis(2)
                pb = utils.ProgressBar(0, len(inputobj))
                for i in pb.range():
                    f = inputobj[i]
                    picr = vtk.vtkBMPReader()
                    picr.SetFileName(f)
                    picr.Update()
                    mgf = vtk.vtkImageMagnitude()
                    mgf.SetInputData(picr.GetOutput())
                    mgf.Update()
                    ima.AddInputData(mgf.GetOutput())
                    pb.print('loading..')
                ima.Update()
                img = ima.GetOutput()

            else:
                if "ndarray" not in inputtype:
                    inputobj = np.array(inputobj)

                varr = numpy_to_vtk(inputobj.ravel(order='F'),
                                    deep=True,
                                    array_type=vtk.VTK_FLOAT)
                varr.SetName('input_scalars')

                img = vtk.vtkImageData()
                if shape is not None:
                    img.SetDimensions(shape)
                else:
                    img.SetDimensions(inputobj.shape)
                img.GetPointData().SetScalars(varr)

                #to convert rgb to numpy
                #        img_scalar = data.GetPointData().GetScalars()
                #        dims = data.GetDimensions()
                #        n_comp = img_scalar.GetNumberOfComponents()
                #        temp = numpy_support.vtk_to_numpy(img_scalar)
                #        numpy_data = temp.reshape(dims[1],dims[0],n_comp)
                #        numpy_data = numpy_data.transpose(0,1,2)
                #        numpy_data = np.flipud(numpy_data)

        elif "ImageData" in inputtype:
            img = inputobj
        elif "UniformGrid" in inputtype:
            img = inputobj
        elif "UnstructuredGrid" in inputtype:
            img = inputobj
            mapperType = 'tetra'
        elif hasattr(
                inputobj,
                "GetOutput"):  # passing vtk object, try extract imagdedata
            if hasattr(inputobj, "Update"):
                inputobj.Update()
            img = inputobj.GetOutput()
        else:
            colors.printc("Volume(): cannot understand input type:\n",
                          inputtype,
                          c=1)
            return

        if 'gpu' in mapperType:
            self._mapper = vtk.vtkGPUVolumeRayCastMapper()
        elif 'opengl_gpu' in mapperType:
            self._mapper = vtk.vtkOpenGLGPUVolumeRayCastMapper()
        elif 'smart' in mapperType:
            self._mapper = vtk.vtkSmartVolumeMapper()
        elif 'fixed' in mapperType:
            self._mapper = vtk.vtkFixedPointVolumeRayCastMapper()
        elif 'tetra' in mapperType:
            self._mapper = vtk.vtkProjectedTetrahedraMapper()
        elif 'unstr' in mapperType:
            self._mapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
        else:
            print("Error unknown mapperType", mapperType)
            raise RuntimeError()

        if origin is not None:
            img.SetOrigin(origin)
        if spacing is not None:
            img.SetSpacing(spacing)
        if shape is not None:
            img.SetDimensions(shape)

        self._imagedata = img
        self._mapper.SetInputData(img)
        self.SetMapper(self._mapper)
        self.mode(mode).color(c).alpha(alpha).alphaGradient(alphaGradient)
        self.GetProperty().SetInterpolationType(1)
        # remember stuff:
        self._mode = mode
        self._color = c
        self._alpha = alpha
        self._alphaGrad = alphaGradient
Пример #6
0
    def __init__(
        self,
        inputobj,
        c=('b', 'lb', 'lg', 'y', 'r'),
        alpha=(0.0, 0.0, 0.2, 0.4, 0.8, 1),
        alphaGradient=None,
        mode=0,
        origin=None,
        spacing=None,
        shape=None,
        mapperType='gpu',
    ):

        vtk.vtkVolume.__init__(self)
        ActorBase.__init__(self)

        inputtype = str(type(inputobj))
        #colors.printc('Volume inputtype', inputtype)

        if inputobj is None:
            img = vtk.vtkImageData()

        elif utils.isSequence(inputobj):
            if "ndarray" not in inputtype:
                inputobj = np.array(inputobj)

            varr = numpy_to_vtk(inputobj.ravel(order='F'),
                                deep=True,
                                array_type=vtk.VTK_FLOAT)
            varr.SetName('input_scalars')

            img = vtk.vtkImageData()
            if shape is not None:
                img.SetDimensions(shape)
            else:
                img.SetDimensions(inputobj.shape)
            img.GetPointData().SetScalars(varr)

            #to convert rgb to numpy
            #        img_scalar = data.GetPointData().GetScalars()
            #        dims = data.GetDimensions()
            #        n_comp = img_scalar.GetNumberOfComponents()
            #        temp = numpy_support.vtk_to_numpy(img_scalar)
            #        numpy_data = temp.reshape(dims[1],dims[0],n_comp)
            #        numpy_data = numpy_data.transpose(0,1,2)
            #        numpy_data = np.flipud(numpy_data)

        elif "ImageData" in inputtype:
            img = inputobj
        elif "UniformGrid" in inputtype:
            img = inputobj
        elif "UnstructuredGrid" in inputtype:
            img = inputobj
            mapperType = 'tetra'
        elif hasattr(
                inputobj,
                "GetOutput"):  # passing vtk object, try extract imagdedata
            if hasattr(inputobj, "Update"):
                inputobj.Update()
            img = inputobj.GetOutput()
        else:
            colors.printc("Volume(): cannot understand input type:\n",
                          inputtype,
                          c=1)
            return

        if 'gpu' in mapperType:
            self._mapper = vtk.vtkGPUVolumeRayCastMapper()
        elif 'opengl_gpu' in mapperType:
            self._mapper = vtk.vtkOpenGLGPUVolumeRayCastMapper()
        elif 'smart' in mapperType:
            self._mapper = vtk.vtkSmartVolumeMapper()
        elif 'fixed' in mapperType:
            self._mapper = vtk.vtkFixedPointVolumeRayCastMapper()
        elif 'tetra' in mapperType:
            self._mapper = vtk.vtkProjectedTetrahedraMapper()
        elif 'unstr' in mapperType:
            self._mapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()

        if origin is not None:
            img.SetOrigin(origin)
        if spacing is not None:
            img.SetSpacing(spacing)
        if shape is not None:
            img.SetDimensions(shape)

        self._imagedata = img
        self._mapper.SetInputData(img)
        self.SetMapper(self._mapper)
        self.mode(mode).color(c).alpha(alpha).alphaGradient(alphaGradient)
        # remember stuff:
        self._mode = mode
        self._color = c
        self._alpha = alpha
        self._alphaGrad = alphaGradient
Пример #7
0
    def __init__(
        self,
        inputobj=None,
        c=('r', 'y', 'lg', 'lb', 'b'),  #('b','lb','lg','y','r')
        alpha=(0.5, 1),
        alphaUnit=1,
        mapper='tetra',
    ):

        vtk.vtkVolume.__init__(self)
        ActorBase.__init__(self)

        self._ugrid = None

        self.useCells = True
        self.useArray = 0

        inputtype = str(type(inputobj))
        #printc('TetMesh inputtype', inputtype)

        ###################
        if inputobj is None:
            self._ugrid = vtk.vtkUnstructuredGrid()
        elif isinstance(inputobj, vtk.vtkUnstructuredGrid):
            self._ugrid = inputobj
        elif isinstance(inputobj, vtk.vtkRectilinearGrid):
            r2t = vtk.vtkRectilinearGridToTetrahedra()
            r2t.SetInputData(inputobj)
            r2t.RememberVoxelIdOn()
            r2t.SetTetraPerCellTo6()
            r2t.Update()
            self._ugrid = r2t.GetOutput()
        elif isinstance(inputobj, vtk.vtkDataSet):
            r2t = vtk.vtkDataSetTriangleFilter()
            r2t.SetInputData(inputobj)
            #r2t.TetrahedraOnlyOn()
            r2t.Update()
            self._ugrid = r2t.GetOutput()
        elif isinstance(inputobj, str):
            from vtkplotter.vtkio import loadUnStructuredGrid
            self._ugrid = loadUnStructuredGrid(inputobj)
        elif utils.isSequence(inputobj):
            if "ndarray" not in inputtype:
                inputobj = np.array(inputobj)
            self._ugrid = self._buildugrid(inputobj[0], inputobj[1])

        ###################
        if 'tetra' in mapper:
            self._mapper = vtk.vtkProjectedTetrahedraMapper()
        elif 'ray' in mapper:
            self._mapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
        elif 'zs' in mapper:
            self._mapper = vtk.vtkUnstructuredGridVolumeZSweepMapper()
        elif isinstance(mapper, vtk.vtkMapper):
            self._mapper = mapper
        else:
            printc('Unknown mapper type', [mapper], c=1)
            return

        self._mapper.SetInputData(self._ugrid)
        self.SetMapper(self._mapper)
        self.color(c).alpha(alpha)
        if alphaUnit:
            self.GetProperty().SetScalarOpacityUnitDistance(alphaUnit)

        # remember stuff:
        self._color = c
        self._alpha = alpha
        self._alphaUnit = alphaUnit