def volumeRender(img, tf=[], spacing=[1.0, 1.0, 1.0]): importer = numpy2VTK(img, spacing) # Transfer Functions opacity_tf = vtk.vtkPiecewiseFunction() color_tf = vtk.vtkColorTransferFunction() if len(tf) == 0: tf.append([img.min(), 0, 0, 0, 0]) tf.append([img.max(), 1, 1, 1, 1]) for p in tf: color_tf.AddRGBPoint(p[0], p[1], p[2], p[3]) opacity_tf.AddPoint(p[0], p[4]) # working on the GPU volMapper = vtk.vtkGPUVolumeRayCastMapper() volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() # working on the CPU #volMapper = vtk.vtkVolumeRayCastMapper() #compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() #compositeFunction.SetCompositeMethodToInterpolateFirst() #volMapper.SetVolumeRayCastFunction(compositeFunction) #volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() # Do the lines below speed things up? # pix_diag = 5.0 # volMapper.SetSampleDistance(pix_diag / 5.0) # volProperty.SetScalarOpacityUnitDistance(pix_diag) vol = vtk.vtkVolume() vol.SetMapper(volMapper) vol.SetProperty(volProperty) return [vol]
def MakeVTKColorScheme(ZeroPoint, MaxScaledVal, poscolor=(0.95, 0.5, 0.14), negcolor=(0, 0.23, 0.48)): #Opacity transfer function opacitytf = vtk.vtkPiecewiseFunction() opacitytf.AddPoint(0, 1.0) opacitytf.AddPoint(ZeroPoint - 1, 0.0) opacitytf.AddPoint(ZeroPoint + 1, 0.0) opacitytf.AddPoint(MaxScaledVal, 1.0) #Color transfer function colortf = vtk.vtkColorTransferFunction() colortf.AddRGBPoint(0, *negcolor) colortf.AddRGBPoint(ZeroPoint - 1, *negcolor) colortf.AddRGBPoint(ZeroPoint + 1, *poscolor) colortf.AddRGBPoint(MaxScaledVal, *poscolor) volprp = vtk.vtkVolumeProperty() volprp.SetColor(colortf) volprp.SetScalarOpacity(opacitytf) volprp.ShadeOn() volprp.SetInterpolationTypeToLinear() return volprp
def extractCellsByID(self, idlist, usePointIDs=False): """Return a new TetMesh composed of the specified subset of indices.""" selectionNode = vtk.vtkSelectionNode() if usePointIDs: selectionNode.SetFieldType(vtk.vtkSelectionNode.POINT) contcells = vtk.vtkSelectionNode.CONTAINING_CELLS() selectionNode.GetProperties().Set(contcells, 1) else: selectionNode.SetFieldType(vtk.vtkSelectionNode.CELL) selectionNode.SetContentType(vtk.vtkSelectionNode.INDICES) vidlist = numpy_to_vtkIdTypeArray(np.array(idlist).astype(np.int64)) selectionNode.SetSelectionList(vidlist) selection = vtk.vtkSelection() selection.AddNode(selectionNode) es = vtk.vtkExtractSelection() es.SetInputData(0, self._ugrid) es.SetInputData(1, selection) es.Update() tm_sel = TetMesh(es.GetOutput()) pr = vtk.vtkVolumeProperty() pr.DeepCopy(self.GetProperty()) tm_sel.SetProperty(pr) #assign the same transformation to the copy tm_sel.SetOrigin(self.GetOrigin()) tm_sel.SetScale(self.GetScale()) tm_sel.SetOrientation(self.GetOrientation()) tm_sel.SetPosition(self.GetPosition()) tm_sel._mapper.SetLookupTable(utils.ctf2lut(self)) return tm_sel
def read_images(self, filename, r, g, b, t1=0): # 2 set up the source self.reader_src = vtk.vtkNIFTIImageReader() self.reader_src.SetFileName(filename) # 3 set up the volume mapper self.vol_map = vtk.vtkGPUVolumeRayCastMapper() self.vol_map.SetInputConnection(self.reader_src.GetOutputPort()) # 4 transfer functions for color and opacity self.funColor = vtk.vtkColorTransferFunction() self.funColor.AddRGBPoint(0, 0., 0., 0.) self.funColor.AddRGBPoint(1, r, g, b) # 5 assign also an alpha (opacity) gradient to the values if t1 == 1: self.funAlpha = vtk.vtkPiecewiseFunction() self.funAlpha.AddPoint(0, 0.) self.funAlpha.AddPoint(256, 0.01) else: self.funAlpha = vtk.vtkPiecewiseFunction() self.funAlpha.AddPoint(0, 0.) self.funAlpha.AddPoint(0.01, 0.01) self.volProp = vtk.vtkVolumeProperty() self.volProp.SetColor(0, self.funColor) self.volProp.SetScalarOpacity(0, self.funAlpha) self.volProp.SetInterpolationTypeToLinear() self.volActor = vtk.vtkVolume() self.volActor.SetMapper(self.vol_map) self.volActor.SetProperty(self.volProp) return self.volActor, self.funAlpha
def volumeRender(self, img, tf=[], spacing=[1.0, 1.0, 1.0]): importer = dicomShow.numpy2VTK(img, spacing) # Transfer Functions opacity_tf = vtk.vtkPiecewiseFunction() color_tf = vtk.vtkColorTransferFunction() if len(tf) == 0: tf.append([img.min(), 0, 0, 0, 0]) tf.append([img.max(), 1, 1, 1, 1]) for p in tf: color_tf.AddRGBPoint(p[0], p[1], p[2], p[3]) opacity_tf.AddPoint(p[0], p[4]) # working on the GPU volMapper = vtk.vtkGPUVolumeRayCastMapper() volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() vol = vtk.vtkVolume() vol.SetMapper(volMapper) vol.SetProperty(volProperty) return [vol]
def setup_volume_actor(image, valueRange=None, cmap='jet'): volumeMapper = vtk.vtkSmartVolumeMapper() if vtk.VTK_MAJOR_VERSION > 5: volumeMapper.SetInputData(image) else: volumeMapper.SetInput(image) if not valueRange: valueRange = image.GetPointData().GetScalars().GetRange() valueSize = valueRange[1] - valueRange[0] valueRange = [ valueRange[0] + valueSize * 0.1, valueRange[0] + valueSize * 0.9 ] volumeProperties = vtk.vtkVolumeProperty() volumeProperties.ShadeOff() colormap = build_ctf(cmap, valueRange) volumeProperties.SetColor(colormap) volumeProperties.SetScalarOpacity(build_opacity_function(valueRange)) scalarOpacityUnitDistance = min(image.GetSpacing()) * 2.0 volumeProperties.SetScalarOpacityUnitDistance(scalarOpacityUnitDistance) volumeProperties.SetGradientOpacity(default_gradient_opacity()) volumeProperties.SetInterpolationTypeToLinear() volumeActor = vtk.vtkVolume() volumeActor.SetMapper(volumeMapper) volumeActor.SetProperty(volumeProperties) return volumeMapper, volumeActor
def viz_VisualizeDICOM(self, path): volume = vtk.vtkVolume() dc_img_reader = vtk.vtkDICOMImageReader() dc_img_reader.SetDirectoryName(path) dc_img_reader.Update() dc_color_func = vtk.vtkColorTransferFunction() dc_color_func.AddRGBPoint(-3024, 0.0, 0.0, 0.0) dc_color_func.AddRGBPoint(-77, 0.55, 0.25, 0.15) dc_color_func.AddRGBPoint(94, 0.88, 0.60, 0.29) dc_color_func.AddRGBPoint(179, 1.0, 0.94, 0.95) dc_color_func.AddRGBPoint(260, 0.62, 0.0, 0.0) dc_color_func.AddRGBPoint(3071, 0.82, 0.66, 1.0) dc_alpha_func = vtk.vtkPiecewiseFunction() dc_alpha_func.AddPoint(-3024, 0.0) dc_alpha_func.AddPoint(-77, 0.0) dc_alpha_func.AddPoint(94, 0.29) dc_alpha_func.AddPoint(179, 0.55) dc_alpha_func.AddPoint(260, 0.84) dc_alpha_func.AddPoint(3071, 0.875) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(dc_color_func) volumeProperty.SetScalarOpacity(dc_alpha_func) volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() volumeMapper = vtk.vtkGPUVolumeRayCastMapper() volumeMapper.SetInputConnection(dc_img_reader.GetOutputPort()) volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) self.__ren.AddVolume(volume)
def __init__(self, data_reader): #if vtk_structured_points_data!=None: # ukosniki=vtk_structured_points_data.count("/") # etykieta=vtk_structured_points_data.split("/",ukosniki) # self.WhoAmI=self.WhoAmI+" "+ etykieta[ukosniki] #def make_name(self,name): # if name!=None: # ukosniki=name.count("/") # etykieta=name.split("/",ukosniki) # self.WhoAmI=self.WhoAmI+" "+ etykieta[ukosniki] self.make_color_function(data_reader.get_data_set().GetScalarRange()) # The property describes how the data will look self.volumeProperty = vtk.vtkVolumeProperty() self.volumeProperty.SetColor(self.colorTransferFunction) self.volumeProperty.SetScalarOpacity(self.opacityTransferFunction) self.volumeProperty.SetGradientOpacity(self.gradientTransferFunction) # The mapper / ray cast function know how to render the data self.compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() self.cast = vtk.vtkImageCast() self.cast.SetInput(data_reader.get_data_set()) self.cast.SetOutputScalarTypeToUnsignedShort() self.cast.ClampOverflowOff() self.mapper = vtk.vtkVolumeRayCastMapper() self.mapper.SetVolumeRayCastFunction(self.compositeFunction) self.mapper.SetInputConnection(self.cast.GetOutputPort()) self.actor = vtk.vtkVolume() self.actor.SetMapper(self.mapper) self.actor.SetProperty(self.volumeProperty)
def create_volume_rendering(self): opacityfunction=vtk.vtkPiecewiseFunction() opacityfunction.AddPoint(0,0.0) opacityfunction.AddPoint(0.1,0.01) opacityfunction.AddPoint(1,0.02) opacityfunction.AddPoint(1.5,0.03) volproperty=vtk.vtkVolumeProperty() volproperty.SetColor(self.arrowColor) volproperty.SetScalarOpacity(opacityfunction) volproperty.ShadeOn() volproperty.SetInterpolationTypeToLinear() volumeMapper = vtk.vtkGPUVolumeRayCastMapper() volumeMapper.SetInputConnection(self.vol_reader.GetOutputPort()) volumeMapper.SetSampleDistance(0.01) volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volproperty) return volume
def get_volume(self, color_file=None, volume_opacity=0.25): """ Default Volume Rendering Return vtkActor. For volume rendering it is vtkVolume """ if not self.flag_read: sys.stderr.write('No Image Loaded.') return # TEST print self.is_blank_image() transfer_func = self.get_transfer_functioin(color_file, volume_opacity) prop_volume = vtk.vtkVolumeProperty() prop_volume.ShadeOff() prop_volume.SetColor(transfer_func[0]) prop_volume.SetScalarOpacity(transfer_func[1]) prop_volume.SetGradientOpacity(transfer_func[2]) prop_volume.SetInterpolationTypeToLinear() prop_volume.SetAmbient(0.4) prop_volume.SetDiffuse(0.6) prop_volume.SetSpecular(0.2) mapper = vtk.vtkSmartVolumeMapper() mapper.SetRequestedRenderMode(0) # mapper = vtk.vtkGPUVolumeRayCastMapper() mapper.SetInputData(self.reader) actor = vtk.vtkVolume() actor.SetMapper(mapper) actor.SetProperty(prop_volume) return actor
def __init__(self, source): # 3 set up the volume mapper volume_mapper = vtk.vtkGPUVolumeRayCastMapper() volume_mapper.SetInputConnection(source.GetOutputPort()) # 4 transfer functions for color and opacity self.color_transfer = vtk.vtkColorTransferFunction() self.alpha_transfer = vtk.vtkPiecewiseFunction() self.c_map_name = 'copper' self.c_map = cm.get_cmap(self.c_map_name) self.init_opacity_points() self.set_color_transfer() # Fill color transfer function # 6 set up the volume properties self.volume_properties = vtk.vtkVolumeProperty() self.volume_properties.SetColor(0, self.color_transfer) self.volume_properties.SetScalarOpacity(0, self.alpha_transfer) self.volume_properties.SetInterpolationTypeToLinear() self.volume_properties.ShadeOn() self.volume_properties.SetAmbient(1.0) self.volume_properties.SetDiffuse(0.7) self.volume_properties.SetSpecular(0.5) # 7 set up the actor self.actor = vtk.vtkVolume() self.actor.SetMapper(volume_mapper) self.actor.SetProperty(self.volume_properties)
def test(): reader = vtk.vtkXMLImageDataReader() reader.SetFileName("C:/Users/Asus/Dropbox/2017-18/ScientificCompAndVisualization/Semana2/SS_2017-master/SS_2017-master/data/wind_image.vti") volume = vtk.vtkVolume() #mapper = vtk.vtkFixedPointVolumeRayCastMapper() mapper = vtk.vtkGPUVolumeRayCastMapper() mapper.SetBlendModeToMinimumIntensity(); mapper.SetSampleDistance(0.1) mapper.SetAutoAdjustSampleDistances(0) ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() iRen = vtk.vtkRenderWindowInteractor() # Set connections mapper.SetInputConnection(reader.GetOutputPort()); volume.SetMapper(mapper) ren.AddViewProp(volume) renWin.AddRenderer(ren) iRen.SetRenderWindow(renWin) # Define opacity transfer function and color functions opacityTransferFunction = vtk.vtkPiecewiseFunction() opacityTransferFunction.AddPoint(0.1, 1.0) opacityTransferFunction.AddPoint(14, 0.5) # Create transfer mapping scalar value to color colorTransferFunction = vtk.vtkColorTransferFunction() colorTransferFunction.AddRGBPoint(1.5, 1.0, 1.0, 0.0) colorTransferFunction.AddRGBPoint(0.5, 1.0, 0.0, 0.0) colorTransferFunction.AddRGBPoint(3.0, 0.0, 1.0, 0.0) colorTransferFunction.AddRGBPoint(6.0, 0.0, 1.0, 1.0) colorTransferFunction.AddRGBPoint(14.0, 0.0, 0.0, 1.0) # Now set the opacity and the color volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorTransferFunction) volumeProperty.SetScalarOpacity(opacityTransferFunction) volumeProperty.ShadeOff() volumeProperty.SetInterpolationTypeToLinear() volume.SetProperty(volumeProperty) renderer = vtk.vtkRenderer() renderer.SetBackground(0.5, 0.5, 0.5) renderer.AddVolume(volume) renderer.ResetCamera() renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindow.SetSize(500, 500) renderWindow.Render() iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renderWindow) iren.Start()
def _initialize (self): debug ("In Volume::_initialize ()") self.data_range = dr = self.mod_m.get_scalar_data_range () ctf = default_color_transfer_function(dr[0], dr[1]) otf = default_opacity_transfer_function(dr[0], dr[1]) self.rc_func = 0 # 0 - composite, 1 - MIP, 2 - Isosurface self.ray_cast_func = self.get_ray_cast_function() self.vp_func = 0 # 0 - composite, 1 - MAXIP, 2 - MINIP # 0 - Ray cast, 1 - TextureMapper, 2 -- VolumeProMapper self.map_type = -1 # Uninitialized map_type = 0 # default to ray cast. self.set_map_type(map_type) self.map.SetInput (self.mod_m.GetOutput ()) # The property describes how the data will look volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(ctf) volumeProperty.SetScalarOpacity(otf) volumeProperty.SetInterpolationTypeToLinear() volumeProperty.ShadeOn() self.act.SetMapper (self.map) self.act.SetProperty (volumeProperty) self.renwin.add_actors (self.act) # used for the pipeline browser self.pipe_objs = self.act
def __init__(self): super(VolumeVisualizationSimple, self).__init__() self.visualizationType = VisualizationTypeSimple # Create the volume property self.volProp = vtkVolumeProperty() self.volProp.SetIndependentComponents(True) self.volProp.SetInterpolationTypeToLinear() self.volProp.ShadeOn() self.volProp.SetAmbient(0.3) self.volProp.SetDiffuse(0.8) self.volProp.SetSpecular(0.2) self.volProp.SetSpecularPower(10.0) self.volProp.SetScalarOpacityUnitDistance(0.8919) self.minimum = 0 self.maximum = 1 self.lowerBound = 0 self.upperBound = 1 colors = [[255, 139, 0], [0, 147, 255], [0, 255, 147], [213, 100, 255], [255, 75, 75]] self.colors = map(lambda x: [x[0] / 255.0, x[1] / 255.0, x[2] / 255.0], colors) self.color = self.colors[0] self.opacity = 1.0 self.colorFunction = None self.opacityFunction = None
def __init__(self, name, image_data): if not isinstance(image_data, vtk.vtkImageData): raise TypeError("input has to be vtkImageData") self.name = name # Create transfer mapping scalar value to opacity. opacity_function = vtk.vtkPiecewiseFunction() opacity_function.AddPoint(0, 0.0) opacity_function.AddPoint(127, 0.0) opacity_function.AddPoint(128, 0.2) opacity_function.AddPoint(255, 0.2) # Create transfer mapping scalar value to color. color_function = vtk.vtkColorTransferFunction() color_function.SetColorSpaceToHSV() color_function.AddHSVPoint(0, 0.0, 0.0, 0.0) color_function.AddHSVPoint(127, 0.0, 0.0, 0.0) color_function.AddHSVPoint(128, 0.0, 0.0, 1.0) color_function.AddHSVPoint(255, 0.0, 0.0, 1.0) volume_property = vtk.vtkVolumeProperty() volume_property.SetColor(color_function) volume_property.SetScalarOpacity(opacity_function) volume_property.ShadeOn() volume_property.SetInterpolationTypeToLinear() volume_mapper = vtk.vtkSmartVolumeMapper() volume_mapper.SetInputData(image_data) self.volume = vtk.vtkVolume() self.volume.SetMapper(volume_mapper) self.volume.SetProperty(volume_property)
def __init__(self, input=None, MinOpacity=0.0, MaxOpacity=0.1): import qt import vtk from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor self.__MinOpacity__ = MinOpacity self.__MaxOpacity__ = MaxOpacity # every QT app needs an app self.__app__ = qt.QApplication(['itkviewer']) # create the widget self.__widget__ = QVTKRenderWindowInteractor() self.__ren__ = vtk.vtkRenderer() self.__widget__.GetRenderWindow().AddRenderer(self.__ren__) self.__itkvtkConverter__ = None self.__volumeMapper__ = vtk.vtkVolumeTextureMapper2D() self.__volume__ = vtk.vtkVolume() self.__volumeProperty__ = vtk.vtkVolumeProperty() self.__volume__.SetMapper(self.__volumeMapper__) self.__volume__.SetProperty(self.__volumeProperty__) self.__ren__.AddVolume(self.__volume__) self.__outline__ = None self.__outlineMapper__ = None self.__outlineActor__ = None self.AdaptColorAndOpacity(0, 255) if input: self.SetInput(input) self.AdaptColorAndOpacity()
def _createPipeline(self): # setup our pipeline self._splatMapper = vtkdevide.vtkOpenGLVolumeShellSplatMapper() self._splatMapper.SetOmegaL(0.9) self._splatMapper.SetOmegaH(0.9) # high-quality rendermode self._splatMapper.SetRenderMode(0) self._otf = vtk.vtkPiecewiseFunction() self._otf.AddPoint(0.0, 0.0) self._otf.AddPoint(0.9, 0.0) self._otf.AddPoint(1.0, 1.0) self._ctf = vtk.vtkColorTransferFunction() self._ctf.AddRGBPoint(0.0, 0.0, 0.0, 0.0) self._ctf.AddRGBPoint(0.9, 0.0, 0.0, 0.0) self._ctf.AddRGBPoint(1.0, 1.0, 0.937, 0.859) self._volumeProperty = vtk.vtkVolumeProperty() self._volumeProperty.SetScalarOpacity(self._otf) self._volumeProperty.SetColor(self._ctf) self._volumeProperty.ShadeOn() self._volumeProperty.SetAmbient(0.1) self._volumeProperty.SetDiffuse(0.7) self._volumeProperty.SetSpecular(0.2) self._volumeProperty.SetSpecularPower(10) self._volume = vtk.vtkVolume() self._volume.SetProperty(self._volumeProperty) self._volume.SetMapper(self._splatMapper)
def CreateVolumeProperty(self): volumeColor = vtk.vtkColorTransferFunction() volumeColor.AddRGBPoint(0, 0.0, 0.0, 0.0) volumeColor.AddRGBPoint(71.78, 0.25, 0.25, 0.25) volumeColor.AddRGBPoint(143.56, 0.5, 0.5, 0.5) volumeColor.AddRGBPoint(215.34, 0.75, 0.75, 0.75) volumeColor.AddRGBPoint(286.00, 1.0, 1.0, 1.0) volumeColor.AddRGBPoint(512, 1.0, 1.0, 1.0) volumeScalarOpacity = vtk.vtkPiecewiseFunction() volumeScalarOpacity.AddPoint(0, 0.0) volumeScalarOpacity.AddPoint(512, 1.0) volumeGradientOpacity = vtk.vtkPiecewiseFunction() volumeGradientOpacity.AddPoint(0, 1.0) volumeGradientOpacity.AddPoint(512, 1.0) volumeProp = vtk.vtkVolumeProperty() volumeProp.SetColor(volumeColor) volumeProp.SetScalarOpacity(volumeScalarOpacity) volumeProp.SetGradientOpacity(volumeGradientOpacity) volumeProp.SetInterpolationTypeToLinear() volumeProp.ShadeOn() volumeProp.SetAmbient(0.30) volumeProp.SetDiffuse(0.60) volumeProp.SetSpecular(0.20) return volumeProp
def __init__(self, input=None, MinOpacity=0.0, MaxOpacity=0.1) : import qt import vtk from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor self.__MinOpacity__ = MinOpacity self.__MaxOpacity__ = MaxOpacity # every QT app needs an app self.__app__ = qt.QApplication(['itkviewer']) # create the widget self.__widget__ = QVTKRenderWindowInteractor() self.__ren__ = vtk.vtkRenderer() self.__widget__.GetRenderWindow().AddRenderer(self.__ren__) self.__itkvtkConverter__ = None self.__volumeMapper__ = vtk.vtkVolumeTextureMapper2D() self.__volume__ = vtk.vtkVolume() self.__volumeProperty__ = vtk.vtkVolumeProperty() self.__volume__.SetMapper(self.__volumeMapper__) self.__volume__.SetProperty(self.__volumeProperty__) self.__ren__.AddVolume(self.__volume__) self.__outline__ = None self.__outlineMapper__ = None self.__outlineActor__ = None self.AdaptColorAndOpacity(0, 255) if input : self.SetInput(input) self.AdaptColorAndOpacity()
def reader(self,filename,r,g,b): self.reader_src = vtk.vtkNIFTIImageReader() self.reader_src.SetFileName(filename) self.vol_map = vtk.vtkGPUVolumeRayCastMapper() self.vol_map.SetInputConnection(self.reader_src.GetOutputPort()) # transfer functions for color and opacity self.funColor = vtk.vtkColorTransferFunction() self.funColor.AddRGBPoint(0, 0., 0., 0.) self.funColor.AddRGBPoint(1, r, g, b) # for the T1 if filename == self.filename1: self.funAlpha = vtk.vtkPiecewiseFunction() self.funAlpha.AddPoint(0, 0.) self.funAlpha.AddPoint(256, .003) # for the masks else: self.funAlpha = vtk.vtkPiecewiseFunction() self.funAlpha.AddPoint(0, 0.) self.funAlpha.AddPoint(1, 0.00003) self.volProp = vtk.vtkVolumeProperty() self.volProp.SetColor(0, self.funColor) self.volProp.SetScalarOpacity(0, self.funAlpha) self.volProp.SetInterpolationTypeToLinear() self.actor = vtk.vtkVolume() self.actor.SetMapper(self.vol_map) self.actor.SetProperty(self.volProp) return self.actor
def axonComparison(axons, N): axonsToRender = [] for i in range(N): axon = axons.pop(random.randrange(len(axons))) axonsToRender.append(axon) bins = main.BINS data_matrix = numpy.zeros([500, 500, 500], dtype=numpy.uint16) dataImporter = vtk.vtkImageImport() data_string = data_matrix.tostring() dataImporter.CopyImportVoidPointer(data_string, len(data_string)) dataImporter.SetDataScalarTypeToUnsignedChar() dataImporter.SetNumberOfScalarComponents(1) dataImporter.SetDataExtent(0, 500, 0, 500, 0, 500) dataImporter.SetWholeExtent(0, 500, 0, 500, 0, 500) volumeProperty = vtk.vtkVolumeProperty() compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() volumeMapper = vtk.vtkVolumeRayCastMapper() volumeMapper.SetVolumeRayCastFunction(compositeFunction) volumeMapper.SetInputConnection(dataImporter.GetOutputPort()) volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) renderer = vtk.vtkRenderer() renderWin = vtk.vtkRenderWindow() renderWin.AddRenderer(renderer) renderInteractor = vtk.vtkRenderWindowInteractor() renderInteractor.SetRenderWindow(renderWin) renderer.SetBackground(1, 1, 1) renderWin.SetSize(400, 400) for axon in axonsToRender: renderer = Utils.renderSingleAxon(axon, renderer, [random.random(), random.random(), random.random()]) renderWin.AddObserver("AbortCheckEvent", exitCheck) renderInteractor.Initialize() renderWin.Render() renderInteractor.Start()
def __init__(self, img, color): self.volume = vtk.vtkVolume() self.__color = color dataImporter = vtk.vtkImageImport() simg = np.ascontiguousarray(img, np.uint8) dataImporter.CopyImportVoidPointer(simg.data, len(simg.data)) dataImporter.SetDataScalarTypeToUnsignedChar() dataImporter.SetNumberOfScalarComponents(1) dataImporter.SetDataExtent(0, simg.shape[2]-1, 0, simg.shape[1]-1, 0, simg.shape[0]-1) dataImporter.SetWholeExtent(0, simg.shape[2]-1, 0, simg.shape[1]-1, 0, simg.shape[0]-1) self.__smoother = vtk.vtkImageGaussianSmooth() self.__smoother.SetStandardDeviation(1, 1, 1) self.__smoother.SetInputConnection(dataImporter.GetOutputPort()) volumeMapper = vtk.vtkSmartVolumeMapper() volumeMapper.SetInputConnection(self.__smoother.GetOutputPort()) self.__volumeProperty = vtk.vtkVolumeProperty() self.__colorFunc = vtk.vtkColorTransferFunction() self.__alpha = vtk.vtkPiecewiseFunction() for i in range(256): self.__colorFunc.AddRGBPoint(i, i * color[0], i * color[1], i * color[2]) self.__alpha.AddPoint(5, .01) self.__alpha.AddPoint(10, .03) self.__alpha.AddPoint(50, .1) self.__alpha.AddPoint(150, .2) self.__volumeProperty.SetColor(self.__colorFunc) self.__volumeProperty.SetScalarOpacity(self.__alpha) self.volume.SetMapper(volumeMapper) self.volume.SetProperty(self.__volumeProperty)
def __init__(self): super(VolumeVisualizationCT, self).__init__() self.visualizationType = VisualizationTypeCT # Cloth, Skin, Muscle, Vascular stuff, Cartilage, Bones self.sections = [-3000.0, -964.384, -656.56, 20.144, 137.168, 233.84, 394.112, 6000.0] self.sectionsOpacity = [0.0, 0.0, 0.0, 0.0, 0.07, 0.1, 0.2] self.sectionNames = ["Air:", "Cloth:", "Skin:", "Muscle:", "Blood vessels:", "Cartilage:", "Bones:"] # sectionColors should be specified for each boundary # Just like opacity should be tweaked. A section can have any slope / configuration self.sectionColors = [(1.0, 1.0, 1.0), (0.0, 1.0, 0.0), (1.0, 0.9, 0.8), (1.0, 0.7, 0.6), (1.0, 0.2, 0.2), (1.0, 0.9, 0.7), (0.9, 1.0, 0.9)] # Create property and attach the transfer function self.volProp = vtkVolumeProperty() self.volProp.SetIndependentComponents(True) self.volProp.SetInterpolationTypeToLinear() self.volProp.ShadeOn() self.volProp.SetAmbient(0.1) self.volProp.SetDiffuse(0.9) self.volProp.SetSpecular(0.2) self.volProp.SetSpecularPower(10.0) self.volProp.SetScalarOpacityUnitDistance(0.8919)
def init_all_VolumeRendering_component(self, flagMesh): self.flagMesh = flagMesh self.ren= vtk.vtkRenderer() self.renWin.AddRenderer(self.ren) self.iren = vtk.vtkXRenderWindowInteractor() self.iren.SetRenderWindow(self.renWin) self.ren.GetVolumes() self.alpha_channel_function = vtk.vtkPiecewiseFunction() self.alpha_channel_function.AddPoint(0, 0.0, 0.5, 0.0) self.alpha_channel_function.AddPoint(255, 1, 0.5, 0.0) self.color_function = vtk.vtkColorTransferFunction() self.color_function.AddRGBPoint(0, 0, 0.0, 0.0, 0.5, 0.0) self.color_function.AddRGBPoint(255, 1, 1, 1, 0.5, 0.0) self.volume_property = vtk.vtkVolumeProperty() self.volume_property.SetColor(self.color_function) self.volume_property.SetScalarOpacity(self.alpha_channel_function) self.data_importer = vtk.vtkImageImport() if self.flagMesh : self.volume_mapper = vtk.vtkPolyDataMapper() else: self.volume_mapper = vtk.vtkFixedPointVolumeRayCastMapper() self.volume = vtk.vtkVolume()
def initRenderWindow(self): pix_diag = 5.0 self.nVolumes = 0 self.vtkWidget = QVTKRenderWindowInteractor(self.frame) self.ren = vtk.vtkRenderer() self.ren.SetBackground(1.0, 1.0, 1.0) self.vtkWidget.GetRenderWindow().AddRenderer(self.ren) self.window = self.vtkWidget.GetRenderWindow() self.iren = self.vtkWidget.GetRenderWindow().GetInteractor() drange = [0,400] self.vtkWidget.opacity_tf = vtk.vtkPiecewiseFunction() self.vtkWidget.opacity_tf.AddPoint(drange[0],0.0) self.vtkWidget.opacity_tf.AddPoint(drange[1],0.0) self.vtkWidget.color_tf = vtk.vtkColorTransferFunction() self.vtkWidget.color_tf.AddRGBPoint(drange[0], 0.0, 0.0, 0.0) self.vtkWidget.color_tf.AddRGBPoint(drange[1], 1.0, 1.0, 1.0) self.vtkWidget.volProperty = vtk.vtkVolumeProperty() self.vtkWidget.volProperty.SetColor(self.vtkWidget.color_tf); self.vtkWidget.volProperty.SetScalarOpacity(self.vtkWidget.opacity_tf) self.vtkWidget.volProperty.ShadeOn() self.vtkWidget.volProperty.SetInterpolationTypeToLinear() self.vtkWidget.volProperty.SetScalarOpacityUnitDistance(pix_diag)
def __init__(self): super(VolumeVisualizationCT, self).__init__() self.visualizationType = VisualizationTypeCT # Cloth, Skin, Muscle, Vascular stuff, Cartilage, Bones self.sections = [ -3000.0, -964.384, -656.56, 20.144, 137.168, 233.84, 394.112, 6000.0 ] self.sectionsOpacity = [0.0, 0.0, 0.0, 0.0, 0.07, 0.1, 0.2] self.sectionNames = [ "Air:", "Cloth:", "Skin:", "Muscle:", "Blood vessels:", "Cartilage:", "Bones:" ] # sectionColors should be specified for each boundary # Just like opacity should be tweaked. A section can have any slope / configuration self.sectionColors = [ (1.0, 1.0, 1.0), (0.0, 1.0, 0.0), (1.0, 0.9, 0.8), (1.0, 0.7, 0.6), (1.0, 0.2, 0.2), (1.0, 0.9, 0.7), (0.9, 1.0, 0.9) ] # Create property and attach the transfer function self.volProp = vtkVolumeProperty() self.volProp.SetIndependentComponents(True) self.volProp.SetInterpolationTypeToLinear() self.volProp.ShadeOn() self.volProp.SetAmbient(0.1) self.volProp.SetDiffuse(0.9) self.volProp.SetSpecular(0.2) self.volProp.SetSpecularPower(10.0) self.volProp.SetScalarOpacityUnitDistance(0.8919)
def _create_pipeline(self): # setup our pipeline self._otf = vtk.vtkPiecewiseFunction() self._ctf = vtk.vtkColorTransferFunction() self._volume_property = vtk.vtkVolumeProperty() self._volume_property.SetScalarOpacity(self._otf) self._volume_property.SetColor(self._ctf) self._volume_property.ShadeOn() self._volume_property.SetAmbient(0.1) self._volume_property.SetDiffuse(0.7) self._volume_property.SetSpecular(0.2) self._volume_property.SetSpecularPower(10) self._volume_raycast_function = vtk.vtkVolumeRayCastMIPFunction() self._volume_mapper = vtk.vtkVolumeRayCastMapper() # can also used FixedPoint, but then we have to use: # SetBlendModeToMaximumIntensity() and not SetVolumeRayCastFunction #self._volume_mapper = vtk.vtkFixedPointVolumeRayCastMapper() self._volume_mapper.SetVolumeRayCastFunction( self._volume_raycast_function) module_utils.setup_vtk_object_progress(self, self._volume_mapper, 'Preparing render.') self._volume = vtk.vtkVolume() self._volume.SetProperty(self._volume_property) self._volume.SetMapper(self._volume_mapper)
def volumeRender(reader,ren,renWin): #Create transfer mapping scalar value to opacity opacityTransferFunction = vtk.vtkPiecewiseFunction() opacityTransferFunction.AddPoint(1, 0.0) opacityTransferFunction.AddPoint(100, 0.1) opacityTransferFunction.AddPoint(255,1.0) colorTransferFunction = vtk.vtkColorTransferFunction() colorTransferFunction.AddRGBPoint(0.0,0.0,0.0,0.0) colorTransferFunction.AddRGBPoint(64.0,1.0,0.0,0.0) colorTransferFunction.AddRGBPoint(128.0,0.0,0.0,1.0) colorTransferFunction.AddRGBPoint(192.0,0.0,1.0,0.0) colorTransferFunction.AddRGBPoint(255.0,0.0,0.2,0.0) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorTransferFunction) volumeProperty.SetScalarOpacity(opacityTransferFunction) volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInputConnection(reader.GetOutputPort()) volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) ren.RemoveAllViewProps() ren.AddVolume(volume) ren.SetBackground(1,1,1) renWin.Render()
def __init__(self): # Objects for volume rendering self.volume = vtk.vtkVolume() self.volumeMapper = vtk.vtkSmartVolumeMapper() self.volumeProperty = vtk.vtkVolumeProperty() # Objects for slice rendering self.sliceActor = vtk.vtkImageActor() self.sliceMapper = vtk.vtkImageResliceMapper() self.sliceProperty = vtk.vtkImageProperty() # Objects for importing and reslicing the data self.importer = [vtk.vtkImageImport()] self.slice = [vtk.vtkImageResliceToColors()] # Objects that point to the output algorithms # These must be specified at run time by the implementing class self.output = None self.sliceOutput = None # Some properties of the data self.numberOfDataSets = 0 self.numberOfTimeFrames = 0 self.currentTimeFrame = 0 self.dimensions = [0, 0, 0]
def volumeRender(img, tf=[],spacing=[1.0,1.0,1.0]): importer = numpy2VTK(img,spacing) # Transfer Functions opacity_tf = vtk.vtkPiecewiseFunction() color_tf = vtk.vtkColorTransferFunction() if len(tf) == 0: tf.append([img.min(),0,0,0,0]) tf.append([img.max(),1,1,1,1]) for p in tf: color_tf.AddRGBPoint(p[0], p[1], p[2], p[3]) opacity_tf.AddPoint(p[0], p[4]) volMapper = vtk.vtkGPUVolumeRayCastMapper() volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() vol = vtk.vtkVolume() vol.SetMapper(volMapper) vol.SetProperty(volProperty) return [vol]
def __init__(self, Mgr): self.Mgr = Mgr self.m_volumeInfo = None self.m_selectedIdx = None self.m_selectedImage = None # self.m_reverseSagittal = False # self.m_reverseAxial = False self.m_shoulderSide = 'L' self.m_orientation = None ##AXL:1, COR:0, SAG:2 #Selected Volume CFGS self.m_colorFunction = vtk.vtkColorTransferFunction() self.m_opacityFunction = vtk.vtkPiecewiseFunction() self.m_scalarRange = [0.0, 1.0] self.m_volumeProperty = vtk.vtkVolumeProperty() self.m_imageProperty = vtk.vtkImageProperty() self.m_imageProperty.SetInterpolationTypeToLinear() #Volume self.volume_data = vtk.vtkImageData() self.volume_cropper = vtk.vtkImageData() self.m_volumeMapper = vtk.vtkSmartVolumeMapper() self.m_volume = vtk.vtkVolume() self.m_resliceMapper = [0, 0, 0] self.m_resliceActor = [0, 0, 0] #Color MAp Volume self.m_bShowCAM = False self.m_bShowVolume = False self.m_colorMapMapper = vtk.vtkSmartVolumeMapper() self.m_colorMapVolume = vtk.vtkVolume() self.m_colorMapResliceMapper = [None, None, None] self.m_colorMapResliceActor = [None, None, None] self.resolution = 64 #Crop View self.croppingMapper = vtk.vtkImageSliceMapper() self.croppingActor = vtk.vtkImageSlice() for i in range(3): self.m_resliceMapper[i] = vtk.vtkImageSliceMapper() self.m_resliceActor[i] = vtk.vtkImageSlice() self.m_colorMapResliceMapper[i] = vtk.vtkImageSliceMapper() self.m_colorMapResliceActor[i] = vtk.vtkImageSlice() self.m_resliceActor[0].RotateY(90) self.m_resliceActor[1].RotateX(-90) self.m_colorMapResliceActor[0].RotateY(90) self.m_colorMapResliceActor[1].RotateX(-90) #Initialize self.SetPresetFunctions( self.Mgr.mainFrm.volumeWidget.GetCurrentColorIndex()) self.InitializeVolumeFunctions() self.InitializeClassActivationMap()
def vtkKWVolumeMaterialPropertyWidgetEntryPoint(parent, win): app = parent.GetApplication() # ----------------------------------------------------------------------- # Create the volume property that will be modified by the widget volprop1 = vtkVolumeProperty() # ----------------------------------------------------------------------- # Create the material widget # Assign our volume property to the editor volprop1_widget = vtkKWVolumeMaterialPropertyWidget() volprop1_widget.SetParent(parent) volprop1_widget.Create() volprop1_widget.SetBalloonHelpString( "A volume material property widget.") volprop1_widget.SetVolumeProperty(volprop1) app.Script( "pack %s -side top -anchor nw -expand n -padx 2 -pady 2", volprop1_widget.GetWidgetName()) # ----------------------------------------------------------------------- # Create another material widget, in popup mode # Assign our volume property to the editor volprop2_widget = vtkKWVolumeMaterialPropertyWidget() volprop2_widget.SetParent(parent) volprop2_widget.PopupModeOn() volprop2_widget.Create() volprop2_widget.SetMaterialColor(0.3, 0.4, 1.0) volprop2_widget.SetBalloonHelpString( "A volume material property widget, created in popup mode. Note that " "it edits the same volume property object as the first widget.") volprop2_widget.SetVolumeProperty(volprop1) app.Script( "pack %s -side top -anchor nw -expand n -padx 2 -pady 15", volprop2_widget.GetWidgetName()) # Both editor are linked to the same vol prop, so they should notify # each other of any changes to refresh the preview nicely volprop2_widget.SetPropertyChangingCommand(volprop1_widget, "Update") volprop2_widget.SetPropertyChangedCommand(volprop1_widget, "Update") volprop1_widget.SetPropertyChangingCommand(volprop2_widget, "Update") volprop1_widget.SetPropertyChangedCommand(volprop2_widget, "Update") return "TypeVTK"
def __init__(self, default = '', color = [1.0, 0.766, 0.336], singleview = True): # Create the importer and load a volume (gets things warmed up) self.dataImporter = vtk.vtkImageImport() if len(default) == 0: # a dataset's config.yaml will specify the name of the default volume and it'll be included default = '/Users/cam/data/dSpaceX/latest/nanoparticles_volume/unprocessed_data/shape_representations/Images.0002.nrrd' self.loadNewVolume(default) # colors colors = vtk.vtkNamedColors() # volume properties volumeProperty = vtk.vtkVolumeProperty() alphaChannelFunc = vtk.vtkPiecewiseFunction() alphaChannelFunc.AddPoint(0, 0.0) alphaChannelFunc.AddPoint(228, 0.0) alphaChannelFunc.AddPoint(255, 0.25) colorFunc = vtk.vtkColorTransferFunction() colorFunc.AddRGBPoint(0, color[0]/2.0, color[1]/2.0, color[2]/2.0) colorFunc.AddRGBPoint(255, color[0], color[1], color[2]) volumeProperty.SetColor(colorFunc) volumeProperty.SetScalarOpacity(alphaChannelFunc) volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() volumeProperty.SetDiffuse(2.0) volumeProperty.SetSpecular(1.5) volumeProperty.SetSpecularPower(15) # volume volumeMapper = vtk.vtkGPUVolumeRayCastMapper() volumeMapper.SetInputConnection(self.dataImporter.GetOutputPort()) self.volume = vtk.vtkVolume() self.volume.SetMapper(volumeMapper) self.volume.SetProperty(volumeProperty) # renderer self.ren = vtk.vtkRenderer() self.renWin = vtk.vtkRenderWindow() self.renWin.AddRenderer(self.ren) self.renWin.OffScreenRenderingOn() # ***** don't open a window ***** # add volume, set background and size self.ren.AddVolume(self.volume) self.ren.SetBackground(colors.GetColor3d("Wheat")) # camera self.resetCamera() self.renWin.SetWindowName('TwistyTurnyNanoparticle') # screenshot filter self.w2if = vtk.vtkWindowToImageFilter() self.w2if.SetInput(self.renWin) self.w2if.SetInputBufferTypeToRGB() self.w2if.ReadFrontBufferOff()
def __init__(self, volume_data, genren): self.volume_data = volume_data self.genren = genren self.vtk_renderer = vtk.vtkRenderer() self.vtk_renderer.SetBackground(0.310999694819562063,0.3400015259021897,0.4299992370489052) self.renWin = vtk.vtkRenderWindow() self.renWin.SetSize(256,256) self.renWin.AddRenderer(self.vtk_renderer) self.renWin.SetWindowName('VTK Renderer') self.iren = vtk.vtkRenderWindowInteractor() self.iren.SetRenderWindow(self.renWin) # compute volume center volume_spacing = np.array(self.volume_data.GetSpacing()) volume_origin = np.array(self.volume_data.GetOrigin()) volume_dimensions = np.array(self.volume_data.GetDimensions()) volume_max = volume_origin+volume_spacing*volume_dimensions volume_center = 0.5*(volume_origin+volume_max) volume_diag = volume_max-volume_origin #offset_scale = 2.25 offset_scale = 2.0 # camera self.camera = self.vtk_renderer.MakeCamera() self.camera.SetClippingRange(0.01,offset_scale*2.0*np.linalg.norm(volume_diag)) self.camera.SetPosition(volume_center[0],volume_center[1],volume_center[2]+offset_scale*np.linalg.norm(volume_diag)) self.camera.SetFocalPoint(volume_center[0],volume_center[1],volume_center[2]) self.camera.Elevation(-85) self.vtk_renderer.SetActiveCamera(self.camera) # color map, opacity map self.vtk_color_map = vtk.vtkColorTransferFunction() for color_val in self.genren.color_tf: self.vtk_color_map.AddRGBPoint(color_val[0],color_val[1],color_val[2],color_val[3]) vtk_opacity_map = vtk.vtkPiecewiseFunction() for op_val in self.genren.opacity_tf: vtk_opacity_map.AddPoint(op_val[0],op_val[1]) # volume properties self.prop_volume = vtk.vtkVolumeProperty() self.prop_volume.ShadeOff() self.prop_volume.SetColor(self.vtk_color_map) self.prop_volume.SetScalarOpacity(vtk_opacity_map) self.prop_volume.SetInterpolationTypeToLinear() # vtk volume render self.mapperVolume = vtk.vtkSmartVolumeMapper() self.mapperVolume = vtk.vtkGPUVolumeRayCastMapper() self.mapperVolume.SetBlendModeToComposite() self.mapperVolume.SetInputData(self.volume_data) self.actorVolume = vtk.vtkVolume() self.actorVolume.SetMapper(self.mapperVolume) self.actorVolume.SetProperty(self.prop_volume) self.vtk_renderer.AddActor(self.actorVolume) self.iren.Initialize()
def volumeRender(): # Create the standard renderer, render window and interactor. ren1 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Create the reader for the data. reader = vtk.vtkStructuredPointsReader() reader.SetFileName('./image/ironProt.vtk') # Create transfer mapping scalar value to opacity. volumeOpacity = vtk.vtkPiecewiseFunction() volumeOpacity.AddPoint(50, 0) volumeOpacity.AddPoint(150, 0.4) volumeOpacity.AddPoint(200, 0.7) volumeOpacity.AddPoint(255, 1) # Create transfer mapping scalar value to color. volumeColor = vtk.vtkColorTransferFunction() volumeColor.AddRGBPoint(0.0, 25.0, 25.0, 25.0) volumeColor.AddRGBPoint(64.0, 100.0, 100.0, 100.0) volumeColor.AddRGBPoint(128.0, 150.0, 150.0, 150.0) volumeColor.AddRGBPoint(192.0, 200.0, 200.0, 200.0) # The property describes how the data will look. volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(volumeColor) volumeProperty.SetScalarOpacity(volumeOpacity) volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() # The mapper / ray cast function know how to render the data. volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInputConnection(reader.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) colors = vtk.vtkNamedColors() ren1.AddVolume(volume) ren1.SetBackground(colors.GetColor3d("White")) ren1.GetActiveCamera().Azimuth(0) ren1.GetActiveCamera().Elevation(15) ren1.ResetCameraClippingRange() ren1.ResetCamera() renWin.SetSize(600, 600) renWin.Render() iren.Start()
def create_volume(self, time_step): # Get data npdatauchar = list() vtk_volume_prop = vtk.vtkVolumeProperty() vtk_volume_prop.IndependentComponentsOn() vtk_volume_prop.SetInterpolationTypeToLinear() for i, volume in enumerate(self.volume_list): npdatauchar.append(volume.get_data(time_step, 200, 300, 0.5)) vtk_volume_prop.SetColor(i, volume.vtk_color) vtk_volume_prop.SetScalarOpacity(i, volume.vtk_opacity) vtk_volume_prop.ShadeOff(i) npdatamulti = np.concatenate( [aux[..., np.newaxis] for aux in npdatauchar], axis=3) axes = self.volume_list[0].get_axes(time_step) axes_spacing = self.volume_list[0].get_axes_spacing( time_step, 200, 300, 0.5) # Normalize spacing. Too small values lead to ghost volumes. max_sp = max(axes_spacing["x"], axes_spacing["y"], axes_spacing["z"]) # Too big cells turn opaque, too small become transparent. # max_cell_size normalizes the cell size max_cell_size = 0.1 norm_factor = max_cell_size / max_sp axes_spacing["x"] = axes_spacing["x"] * norm_factor axes_spacing["y"] = axes_spacing["y"] * norm_factor axes_spacing["z"] = axes_spacing["z"] * norm_factor # Put data in VTK format vtk_data_import = vtk.vtkImageImport() vtk_data_import.SetImportVoidPointer(npdatamulti) vtk_data_import.SetDataScalarTypeToUnsignedChar() vtk_data_import.SetNumberOfScalarComponents(len(self.volume_list)) vtk_data_import.SetDataExtent(0, npdatamulti.shape[2] - 1, 0, npdatamulti.shape[1] - 1, 0, npdatamulti.shape[0] - 1) vtk_data_import.SetWholeExtent(0, npdatamulti.shape[2] - 1, 0, npdatamulti.shape[1] - 1, 0, npdatamulti.shape[0] - 1) vtk_data_import.SetDataSpacing(axes_spacing["x"], axes_spacing["y"], axes_spacing["z"]) # data origin is also changed by the normalization vtk_data_import.SetDataOrigin(axes["x"][0] * norm_factor, axes["y"][0] * norm_factor, axes["z"][0] * norm_factor) vtk_data_import.Update() # Create the mapper vtk_volume_mapper = vtk.vtkGPUVolumeRayCastMapper() vtk_volume_mapper.SetInputConnection(vtk_data_import.GetOutputPort()) vtk_volume_mapper.Update() # Add to volume self.vtk_volume.SetMapper(vtk_volume_mapper) self.vtk_volume.SetProperty(vtk_volume_prop) # Set visualization parameters self.set_render_quality(self.render_quality) self.set_color_level(self.volume_color_level) self.set_color_window(self.volume_color_window) # Add to render self.renderer.AddVolume(self.vtk_volume) self.renderer.ResetCamera() self.interactor.Render()
def InitializeClassActivationMap(self): self.cam_data = vtk.vtkImageData() self.cam_data.SetOrigin([0, 0, 0]) self.cam_data.SetDimensions( 64, 64, 64, ) self.cam_data.AllocateScalars(vtk.VTK_UNSIGNED_INT, 1) self.cam_data.SetSpacing([1.0, 1.0, 1.0]) #set Class Activation Map cam_color_function = vtk.vtkColorTransferFunction() cam_opacity_function = vtk.vtkPiecewiseFunction() scalarRange = [0.0, 255.0] cam_volume_property = vtk.vtkVolumeProperty() cam_color_function.AddRGBPoint((scalarRange[0] + scalarRange[1]) * 0.4, 0.0, 0.0, 1.0) cam_color_function.AddRGBPoint((scalarRange[0] + scalarRange[1]) * 0.7, 0.0, 1.0, 0.0) cam_color_function.AddRGBPoint(scalarRange[1], 1.0, 0.0, 0.0) cam_opacity_function.AddPoint((scalarRange[0] + scalarRange[1]) * 0.0, 0.3) cam_opacity_function.AddPoint(scalarRange[1], 0.3) cam_volume_property.SetColor(cam_color_function) cam_volume_property.SetScalarOpacity(cam_opacity_function) cam_volume_property.ShadeOff() cam_volume_property.SetInterpolationTypeToLinear() self.m_colorMapMapper.SetInputData(self.cam_data) self.m_colorMapMapper.SetBlendModeToMaximumIntensity() #Actor self.m_colorMapVolume.SetMapper(self.m_colorMapMapper) self.m_colorMapVolume.SetProperty(cam_volume_property) self.m_colorMapVolume.SetPosition([0, 0, 0]) lookupTable = vtk.vtkLookupTable() lookupTable.SetTableRange(0.0, 255.0) lookupTable.SetHueRange(0.7, 0.0) lookupTable.Build() imageProperty = vtk.vtkImageProperty() imageProperty.SetInterpolationTypeToLinear() imageProperty.SetLookupTable(lookupTable) imageProperty.SetOpacity(0.3) #Slice for i in range(3): self.m_colorMapResliceMapper[i].SetInputData(self.cam_data) self.m_colorMapResliceMapper[i].SetOrientation(i) self.m_colorMapResliceActor[i].SetMapper( self.m_colorMapResliceMapper[i]) self.m_colorMapResliceActor[i].SetProperty(imageProperty)
def defaultEnvironment(environment, attribute): """ Default 3d environment renderer. White is the highest value in the frame and black is the lowest value. """ xbounds = environment.mesh.boundsX ybounds = environment.mesh.boundsY zbounds = environment.mesh.boundsZ attributeIndex = environment.current.variables[attribute] positions = environment.mesh.voxels data = environment.current.data[attributeIndex] xCount = np.unique(environment.mesh.voxels[environment.mesh.variables['x']]).shape[0] yCount = np.unique(environment.mesh.voxels[environment.mesh.variables['y']]).shape[0] zCount = np.unique(environment.mesh.voxels[environment.mesh.variables['z']]).shape[0] minimum = np.min(data) maximum = np.max(data) if not maximum == 0: data = data / maximum data = np.reshape(data, (xCount, yCount, zCount)) imdata = vtk.vtkImageData() depthArray = numpy_support.numpy_to_vtk(data.ravel(), deep=True, array_type=vtk.VTK_DOUBLE) imdata.SetDimensions(data.shape) imdata.SetSpacing([(xbounds[1] - xbounds[0]) / xCount, (ybounds[1] - ybounds[0]) / yCount, (zbounds[1] - zbounds[0]) / zCount]) imdata.SetOrigin([xbounds[0], ybounds[0], zbounds[0]]) imdata.GetPointData().SetScalars(depthArray) colorFunc = vtk.vtkColorTransferFunction() colorFunc.AddRGBPoint(0.0, 0.0, 0.0, 0.0) colorFunc.AddRGBPoint(1.0, 1.0, 1.0, 1.0) opacity = vtk.vtkPiecewiseFunction() opacity.AddPoint(0.0, 0.0) opacity.AddPoint(1, 0.8) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorFunc) volumeProperty.SetScalarOpacity(opacity) volumeProperty.SetInterpolationTypeToLinear() volumeProperty.SetIndependentComponents(2) volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInputData(imdata) volumeMapper.SetBlendModeToMaximumIntensity() volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) return volume
def _createVolPropFromImageData(self, imageData, color): volProp = vtkVolumeProperty() if imageData is not None: color, opacityFunction = CreateRangeFunctions(imageData, color) else: color, opacityFunction = CreateEmptyFunctions() volProp.SetColor(color) volProp.SetScalarOpacity(opacityFunction) return volProp
def _createVolPropFromImageData(self, imageData): volProp = vtkVolumeProperty() if imageData is not None: color, opacityFunction = CreateRangeFunctions(imageData) else: color, opacityFunction = CreateEmptyFunctions() volProp.SetColor(color) volProp.SetScalarOpacity(opacityFunction) return volProp
def SetPresetVolumeProperty(self): '''Create the transfer functions and set volume properties in a vtkVolumeProperty object''' # parse the xml file which is loaded in the same path as the vmtkimagevolumeviewer.py file at runtime presetElementTree = ET.parse(os.path.join(os.path.dirname(__file__), 'share', 'vmtkimagevolumeviewerpresets.xml')) presetRoot = presetElementTree.getroot() volProperties = presetRoot.findall('VolumeProperty[@name="' + self.Preset + '"]') # should return a list with only one element volPropertiesDict = volProperties[0].attrib def _chunks(l, n): """Yield successive n-sized chunks from l.""" PY3 = sys.version_info[0] == 3 if PY3: range_func = range else: range_func = xrange for i in range_func(0, len(l), n): yield l[i:i + n] # need to convert the space seperated string displaying values into a list of floats colorList = [float(i) for i in volPropertiesDict['colorTransfer'].split()] gradientOpacityList = [float(i) for i in volPropertiesDict['gradientOpacity'].split()] opacityList = [float(i) for i in volPropertiesDict['scalarOpacity'].split()] # create an array of arrays with each list split into subarrays of desired size colorMapList = _chunks(colorList, 4) gradientOpacityMapList = _chunks(gradientOpacityList, 2) opacityMapList = _chunks(opacityList, 2) # create vtk objects from the mapped lists (now arrays of arrays) self.ColorTransferFunction = self.BuildVTKColorTransferFunction(colorMapList) self.GradientOpacityTransferFunction = self.BuildVTKPiecewiseFunction(gradientOpacityMapList) self.OpacityTransferFunction = self.BuildVTKPiecewiseFunction(opacityMapList) # assign other properties from the element tree to variables with the appropriate type self.InterpolationType = int(volPropertiesDict['interpolation']) self.Shade = int(volPropertiesDict['shade']) self.SpecularPower = float(volPropertiesDict['specularPower']) self.Specular = float(volPropertiesDict['specular']) self.Diffuse = float(volPropertiesDict['diffuse']) self.Ambient = float(volPropertiesDict['ambient']) # set transfer function and lighting properties of the vtk volume object volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetScalarOpacity(self.OpacityTransferFunction) volumeProperty.SetGradientOpacity(self.GradientOpacityTransferFunction) volumeProperty.SetColor(self.ColorTransferFunction) volumeProperty.SetInterpolationType(self.InterpolationType) volumeProperty.SetShade(self.Shade) volumeProperty.SetSpecularPower(self.SpecularPower) volumeProperty.SetSpecular(self.Specular) volumeProperty.SetDiffuse(self.Diffuse) volumeProperty.SetAmbient(self.Ambient) self.VolumeProperty = volumeProperty
def setVolumeVisualization(self, visualization): self.visualization = visualization if self.visualization is None: color, opacityFunction = CreateEmptyFunctions() self.fixedVolumeProperty = vtkVolumeProperty() self.fixedVolumeProperty.SetColor(color) self.fixedVolumeProperty.SetScalarOpacity(opacityFunction) self.movingVolumeProperty = vtkVolumeProperty() self.movingVolumeProperty.SetColor(color) self.movingVolumeProperty.SetScalarOpacity(opacityFunction) else: self.fixedVolumeProperty = self.visualization.fixedVolProp self.movingVolumeProperty = self.visualization.movingVolProp self.visualization.setMapper(self.mapper) if self.visualization.fixedVisualization: self._updateMapper(self.visualization.fixedVisualization, 1) if self.visualization.movingVisualization: self._updateMapper(self.visualization.movingVisualization, 2) self._updateVolumeProperties()
def _create_pipeline(self): # setup our pipeline self._volume_property = vtk.vtkVolumeProperty() self._volume_property.ShadeOn() self._volume_mapper = None self._volume = vtk.vtkVolume() self._volume.SetProperty(self._volume_property) self._volume.SetMapper(self._volume_mapper)
def originalObject(self): volumeProperty = vtkVolumeProperty() volumeProperty.SetIndependentComponents(self.independentComponents) volumeProperty.SetInterpolationType(self.interpolationType) volumeProperty.SetShade(self.shade) volumeProperty.SetAmbient(self.ambient) volumeProperty.SetDiffuse(self.diffuse) volumeProperty.SetSpecular(self.specular) volumeProperty.SetSpecularPower(self.specularPower) volumeProperty.SetScalarOpacityUnitDistance(self.scalarOpacityUnitDistance) return volumeProperty
def __init__(self): super(VolumeVisualizationMIDA, self).__init__() self.visualizationType = VisualizationTypeMIDA self.mapper = None # TODO: add two sliders with which to threshold the data # Create property and attach the transfer function self.volProp = vtkVolumeProperty() self.volProp.SetIndependentComponents(True) self.volProp.SetInterpolationTypeToLinear()
def vtkKWVolumePropertyWidgetEntryPoint(parent, win): app = parent.GetApplication() # ----------------------------------------------------------------------- # This is a faily big widget, so create a scrolled frame vpw_frame = vtkKWFrameWithScrollbar() vpw_frame.SetParent(parent) vpw_frame.Create() app.Script("pack %s -side top -fill both -expand y", vpw_frame.GetWidgetName()) # ----------------------------------------------------------------------- # Create a volume property widget vpw = vtkKWVolumePropertyWidget() vpw.SetParent(vpw_frame.GetFrame()) vpw.Create() app.Script("pack %s -side top -anchor nw -expand y -padx 2 -pady 2", vpw.GetWidgetName()) # Create a volume property and assign it # We need color tfuncs, opacity, and gradient vpw_vp = vtkVolumeProperty() vpw_vp.SetIndependentComponents(1) vpw_cfun = vtkColorTransferFunction() vpw_cfun.SetColorSpaceToHSV() vpw_cfun.AddHSVSegment(0.0, 0.2, 1.0, 1.0, 255.0, 0.8, 1.0, 1.0) vpw_cfun.AddHSVSegment(80, 0.8, 1.0, 1.0, 130.0, 0.1, 1.0, 1.0) vpw_ofun = vtkPiecewiseFunction() vpw_ofun.AddSegment(0.0, 0.2, 255.0, 0.8) vpw_ofun.AddSegment(40, 0.9, 120.0, 0.1) vpw_gfun = vtkPiecewiseFunction() vpw_gfun.AddSegment(0.0, 0.2, 60.0, 0.4) vpw_vp.SetColor(0, vpw_cfun) vpw_vp.SetScalarOpacity(0, vpw_ofun) vpw_vp.SetGradientOpacity(0, vpw_gfun) vpw.SetVolumeProperty(vpw_vp) vpw.SetWindowLevel(128, 128) # vpw.MergeScalarOpacityAndColorEditors() return "TypeVTK"
def volumeRender(img, tf=[],spacing=[1.0,1.0,1.0]): importer = numpy2VTK(img,spacing) # Transfer Functions opacity_tf = vtk.vtkPiecewiseFunction() color_tf = vtk.vtkColorTransferFunction() if len(tf) == 0: tf.append([img.min(),0,0,0,0]) tf.append([img.max(),1,1,1,1]) for p in tf: color_tf.AddRGBPoint(p[0], p[1], p[2], p[3]) opacity_tf.AddPoint(p[0], p[4]) # working on the GPU # volMapper = vtk.vtkGPUVolumeRayCastMapper() # volMapper.SetInputConnection(importer.GetOutputPort()) # # The property describes how the data will look # volProperty = vtk.vtkVolumeProperty() # volProperty.SetColor(color_tf) # volProperty.SetScalarOpacity(opacity_tf) # volProperty.ShadeOn() # volProperty.SetInterpolationTypeToLinear() # working on the CPU volMapper = vtk.vtkVolumeRayCastMapper() compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() compositeFunction.SetCompositeMethodToInterpolateFirst() volMapper.SetVolumeRayCastFunction(compositeFunction) volMapper.SetInputConnection(importer.GetOutputPort()) # The property describes how the data will look volProperty = vtk.vtkVolumeProperty() volProperty.SetColor(color_tf) volProperty.SetScalarOpacity(opacity_tf) volProperty.ShadeOn() volProperty.SetInterpolationTypeToLinear() # Do the lines below speed things up? # pix_diag = 5.0 # volMapper.SetSampleDistance(pix_diag / 5.0) # volProperty.SetScalarOpacityUnitDistance(pix_diag) vol = vtk.vtkVolume() vol.SetMapper(volMapper) vol.SetProperty(volProperty) return [vol]
def BuildView(self): if not self.vmtkRenderer: self.vmtkRenderer = vmtkrenderer.vmtkRenderer() self.vmtkRenderer.Initialize() self.OwnRenderer = 1 self.vmtkRenderer.RegisterScript(self) if self.Volume: self.vmtkRenderer.Renderer.RemoveVolume(self.Volume) if (self.ArrayName != ''): self.Image.GetPointData().SetActiveScalars(self.ArrayName) scalarRange = [0.0, 0.0] if self.WindowLevel[0] > 0.0: scalarRange = [self.WindowLevel[1] - self.WindowLevel[0]/2.0, self.WindowLevel[1] + self.WindowLevel[0]/2.0] else: scalarRange = self.Image.GetScalarRange() colorTransferFunction = vtk.vtkColorTransferFunction() colorTransferFunction.AddRGBPoint(scalarRange[0], 0.0, 0.0, 0.0) colorTransferFunction.AddRGBPoint(scalarRange[1], 1.0, 1.0, 1.0) volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper() volumeMapper.SetInput(self.Image) volumeMapper.SetBlendModeToMaximumIntensity() if self.AutoSampleDistance: volumeMapper.AutoAdjustSampleDistancesOn() else: volumeMapper.SetSampleDistance(self.SampleDistance) volumeProperty = vtk.vtkVolumeProperty() volumeProperty.ShadeOn() volumeProperty.SetInterpolationTypeToLinear() volumeProperty.SetColor(colorTransferFunction) self.Volume = vtk.vtkVolume() self.Volume.SetMapper(volumeMapper) self.Volume.SetProperty(volumeProperty) self.vmtkRenderer.Renderer.AddVolume(self.Volume) if (self.Display == 1): self.vmtkRenderer.Render() if self.OwnRenderer: self.vmtkRenderer.Deallocate()
def __init__(self): super(VolumeVisualizationRamp, self).__init__() self.visualizationType = VisualizationTypeRamp self.volProp = vtkVolumeProperty() self.volProp.SetIndependentComponents(True) self.volProp.SetInterpolationTypeToLinear() self.volProp.ShadeOn() self.volProp.SetAmbient(0.1) self.volProp.SetDiffuse(0.9) self.volProp.SetSpecular(0.2) self.volProp.SetSpecularPower(10.0) self.volProp.SetScalarOpacityUnitDistance(0.8919)
def save_vtk_image(images, dst, i): image_import = vtk.vtkImageImport() image_import.CopyImportVoidPointer(images.tostring(), len(images.tostring())) image_import.SetDataScalarTypeToUnsignedChar() image_import.SetNumberOfScalarComponents(1) image_import.SetDataExtent(0, images.shape[2] - 1, 0, images.shape[1] - 1, 0, images.shape[0] - 1) image_import.SetWholeExtent(0, images.shape[2] - 1, 0, images.shape[1] - 1, 0, images.shape[0] - 1) volume = vtk.vtkVolume() volume_mapper = vtk.vtkVolumeRayCastMapper() alpha_channel_func = vtk.vtkPiecewiseFunction() alpha_channel_func.AddPoint(0, 0.0) # alpha_channel_func.AddPoint(64, 0.3) # alpha_channel_func.AddPoint(128, 0.5) alpha_channel_func.AddPoint(100, 1.0) alpha_channel_func.ClampingOn() color_func = vtk.vtkPiecewiseFunction() color_func.AddPoint(5, 0.3) color_func.AddPoint(25, 0.5) color_func.AddPoint(125, 0.7) color_func.AddPoint(255, 1.0) volume_property = vtk.vtkVolumeProperty() volume_property.SetColor(color_func) volume_property.SetInterpolationTypeToLinear() volume_property.SetScalarOpacity(alpha_channel_func) volume.SetProperty(volume_property) volume_ray_cast_func = vtk.vtkVolumeRayCastMIPFunction() volume_mapper.SetInputConnection(image_import.GetOutputPort()) volume_mapper.SetVolumeRayCastFunction(volume_ray_cast_func) # volume_mapper.SetSampleDistance(1) # volume_mapper.SetAutoAdjustSampleDistances(0) # volume_mapper.SetImageSampleDistance(1) volume.SetMapper(volume_mapper) ren = vtk.vtkRenderer() ren.AddVolume(volume) ren.SetBackground(0, 0, 0) renWin = vtk.vtkRenderWindow() renWin.SetSize(1024, 1024) renWin.AddRenderer(ren) renWin.Render() window_2_image = vtk.vtkWindowToImageFilter() window_2_image.SetInput(renWin) window_2_image.Update() png_writer = vtk.vtkPNGWriter() png_writer.SetFileName(dst + '%05d'%(i) + '.png') png_writer.SetInput(window_2_image.GetOutput()) png_writer.Write()
def load_atlas(path, intensities, signs): ''' path: path to atlas file opacity: opacity of overlayed atlas brain ''' nifti_reader = vtk.vtkNIFTIImageReader() nifti_reader.SetFileName(path) nifti_reader.Update() # intensities = intensities/math.sqrt(np.mean(intensities**2)) # The following class is used to store transparencyv-values for later # retrival. In our case, we want the value 0 to be completly opaque alphaChannelFunc = vtk.vtkPiecewiseFunction() alphaChannelFunc.AddPoint(0, 0.0) for i in range(len(intensities)): alphaChannelFunc.AddPoint(i+1, intensities[i]) # This class stores color data and can create color tables from a few color # points. For this demo, we want the three cubes to be of the colors red # green and blue. colorFunc = vtk.vtkColorTransferFunction() colorFunc.AddRGBPoint(0, 0.0, 0.0, 0.0) for i in range(len(signs)): if signs[i] < 0: colorFunc.AddRGBPoint(i+1, 0.0, 0.0, intensities[i]) elif signs[i] > 0: colorFunc.AddRGBPoint(i+1, intensities[i], 0.0, 0.0) else: colorFunc.AddRGBPoint(i+1, 1.0, 1.0, 1.0) # The previous two classes stored properties. Because we want to apply # these properties to the volume we want to render, we have to store them # in a class that stores volume prpoperties. volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorFunc) volumeProperty.SetScalarOpacity(alphaChannelFunc) volumeProperty.ShadeOn() # We can finally create our volume. We also have to specify the data for # it, as well as how the data will be rendered. volumeMapper = vtk.vtkSmartVolumeMapper() volumeMapper.SetInputDataObject(nifti_reader.GetOutput()) # The class vtkVolume is used to pair the preaviusly declared volume as # well as the properties to be used when rendering that volume. volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) return volume
def main(argv): if len(argv) < 2: print "usage:",argv[0]," data.nrrd data.cmap" exit(1) data_fn = argv[1] cmap_fn = argv[2] reader = vtk.vtkPNrrdReader() reader.SetFileName(data_fn) reader.Update() data = reader.GetOutput() # opacity function opacityFunction = vtk.vtkPiecewiseFunction() # color function colorFunction = vtk.vtkColorTransferFunction() cmap = open(cmap_fn, 'r') for line in cmap.readlines(): parts = line.split() value = float(parts[0]) r = float(parts[1]) g = float(parts[2]) b = float(parts[3]) a = float(parts[4]) opacityFunction.AddPoint(value, a) colorFunction.AddRGBPoint(value, r, g, b) # volume setup: volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetColor(colorFunction) volumeProperty.SetScalarOpacity(opacityFunction) # composite function (using ray tracing) compositeFunction = vtk.vtkVolumeRayCastMIPFunction() volumeMapper = vtk.vtkVolumeRayCastMapper() volumeMapper.SetVolumeRayCastFunction(compositeFunction) volumeMapper.SetInput(data) # make the volume volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProperty) # renderer renderer = vtk.vtkRenderer() renderWin = vtk.vtkRenderWindow() renderWin.AddRenderer(renderer) renderInteractor = vtk.vtkRenderWindowInteractor() renderInteractor.SetRenderWindow(renderWin) renderer.AddVolume(volume) renderer.SetBackground(0,0,0) renderWin.SetSize(400, 400) renderInteractor.Initialize() renderWin.Render() renderInteractor.Start()
def __init__(self): ''' Constructor ''' vtkPythonViewImage.__init__(self) # texture mapper in 3D: vtkVolumeMapper self.__VolumeMapper = None # texture mapper in 3D: vtkVolumeTextureMapper3D self.__VolumeTextureMapper = vtk.vtkVolumeTextureMapper3D() # volume ray cast mapper vtkFixedPointVolumeRayCastMapper self.__VolumeRayCastMapper = vtk.vtkFixedPointVolumeRayCastMapper() # volume property: vtkVolumeProperty self.__VolumeProperty = vtk.vtkVolumeProperty() # volume actor: vtkVolume self.__VolumeActor = vtk.vtkVolume() # opacity transfer function: vtkPiecewiseFunction self.__OpacityFunction = vtk.vtkPiecewiseFunction() # color transfer function: vtkColorTransferFunction self.__ColorFunction = vtk.vtkColorTransferFunction() # vtkProp3DCollection self.__PhantomCollection = vtk.vtkProp3DCollection() # blender: vtkImageBlend self.__Blender = None # image 3D cropping box callback: vtkImage3DCroppingBoxCallback self.__Callback = vtkPythonImage3DCroppingBoxCallback() # box widget: vtkOrientedBoxWidget # self.__BoxWidget = vtkOrientedBoxWidget() # Now I could not wrap vtkOrientedBoxWidget self.__BoxWidget = vtk.vtkBoxWidget() # vtkPlane widget: vtkPlaneWidget self.__PlaneWidget = vtk.vtkPlaneWidget() # annotated cube actor: vtkAnnotatedCubeActor, vtkOrientationMarkerWidget self.__Cube = vtk.vtkAnnotatedCubeActor() self.__Marker = vtk.vtkOrientationMarkerWidget() self.SetupVolumeRendering() self.SetupWidgets() self.ShowAnnotationsOn() self.getTextProperty().SetColor(0, 0, 0) self.SetBackground(0.9, 0.9, 0.9) # white self.__FirstRender = 1 self.__RenderingMode = self.PLANAR_RENDERING self.__VRQuality = 1 self.__InteractorStyleSwitcher = None
def __init__(self, parent): wx.SplitterWindow.__init__(self, parent) # # setup the control panel # self.controlPanel = wx.lib.scrolledpanel.ScrolledPanel( self ) vBox = wx.BoxSizer( wx.VERTICAL ) self.mode = wx.RadioBox( self.controlPanel, label="Mode", choices=["2D", "3D"] ) vBox.Add( self.mode, 0, wx.EXPAND ) self.colorAndOpacityEditor = WrapITKColorAndOpacityEditor( self.controlPanel ) vBox.Add( self.colorAndOpacityEditor, 0, wx.EXPAND ) self.controlPanel.SetSizer( vBox ) self.controlPanel.SetupScrolling() # # setup the render window # from vtk.wx.wxVTKRenderWindowInteractor import wxVTKRenderWindowInteractor from vtk import vtkRenderer, vtkVolumeTextureMapper2D, vtkVolumeProperty, vtkVolume self.rendererWindow = wxVTKRenderWindowInteractor(self, -1) self.renderer = vtkRenderer() self.rendererWindow.GetRenderWindow().AddRenderer(self.renderer) self.volumeMapper = vtkVolumeTextureMapper2D() self.volume = vtkVolume() self.volumeProperty = vtkVolumeProperty() self.volumeProperty.SetScalarOpacity( self.colorAndOpacityEditor.opacityTransferFunction ) self.volumeProperty.SetColor( self.colorAndOpacityEditor.colorTransferFunction ) self.volume.SetMapper( self.volumeMapper ) self.volume.SetProperty( self.volumeProperty ) self.renderer.AddVolume( self.volume ) self.outline = None self.outlineMapper = None self.outlineActor = None # fill the split pane self.SplitVertically( self.controlPanel, self.rendererWindow ) # avoid loosing on panel or the other self.SetMinimumPaneSize( 1 ) # to manage update event correctly self.updateInProgress = False
def opImplement(self): opacity = vtk.vtkPiecewiseFunction(); #x is scaler value , y is opacity value opacity.AddPoint(IMAGEOUT,0.0); opacity.AddPoint(IMAGEIN ,1.0); #x is scaler value , other is rgb value color = vtk.vtkColorTransferFunction(); color.AddRGBPoint(IMAGEIN ,255.0/255.0,255.0/255.0,255.0/255.0); volumeProperty = vtk.vtkVolumeProperty(); volumeProperty.ShadeOn(); volumeProperty.SetScalarOpacity(opacity) volumeProperty.SetColor(color); mapper = vtk.vtkSmartVolumeMapper(); mapper.SetInputData(self.data); volume = vtk.vtkVolume(); volume.SetMapper(mapper); volume.SetProperty(volumeProperty); return volume;