def main(): colors = vtk.vtkNamedColors() # Create Sphere 1. sphere1 = vtk.vtkSphereSource() sphere1.SetRadius(3) sphere1.SetCenter(0, 0, 0) sphere1.Update() # Create Sphere 2. sphere2 = vtk.vtkSphereSource() sphere2.SetRadius(2) sphere2.SetCenter(2, 0, 0) sphere2.Update() mbds = vtk.vtkMultiBlockDataSet() mbds.SetNumberOfBlocks(3) mbds.SetBlock(0, sphere1.GetOutput()) # Leave block 1 NULL. NULL blocks are valid and should be handled by # algorithms that process multiblock datasets. Especially when # running in parallel where the blocks owned by other processes are # NULL in this process. mbds.SetBlock(2, sphere2.GetOutput()) mapper = vtk.vtkCompositePolyDataMapper2() mapper.SetInputDataObject(mbds) cdsa = vtk.vtkCompositeDataDisplayAttributes() mapper.SetCompositeDataDisplayAttributes(cdsa) # You can use the vtkCompositeDataDisplayAttributes to set the color, # opacity and visibiliy of individual blocks of the multiblock dataset. # Attributes are mapped by block pointers (vtkDataObject*), so these can # be queried by their flat index through a convenience function in the # attribute class (vtkCompositeDataDisplayAttributes::DataObjectFromIndex). # Alternatively, one can set attributes directly through the mapper using # flat indices. # # This sets the block at flat index 3 red # Note that the index is the flat index in the tree, so the whole multiblock # is index 0 and the blocks are flat indexes 1, 2 and 3. This affects # the block returned by mbds.GetBlock(2). mapper.SetBlockColor(3, colors.GetColor3d("Red")) actor = vtk.vtkActor() actor.SetMapper(mapper) # Create the Renderer, RenderWindow, and RenderWindowInteractor. renderer = vtk.vtkRenderer() renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindowInteractor = vtk.vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) # Enable user interface interactor. renderer.AddActor(actor) renderer.SetBackground(colors.GetColor3d("SteelBlue")) renderWindow.Render() renderWindowInteractor.Start()
def showTubeSelection(self, tubeSelection): '''Shows tube selections. Args: tubeSelection: an iterable of tube block indexes. ''' cdda = vtk.vtkCompositeDataDisplayAttributes() # default red cdda.SetBlockColor(0, (1,0,0)) for index in tubeSelection: cdda.SetBlockColor(index, (1,1,1)) self.tubeMapper.SetCompositeDataDisplayAttributes(cdda) self.volumeView.GetRenderWindow().Render()
def process_tre(in_file, out_dir, phi_vals, theta_vals, width, height): import itk, vtk from vtk.web.dataset_builder import ImageDataSetBuilder reader = itk.SpatialObjectReader[3].New() reader.SetFileName(in_file) reader.Update() blocks = vtk.vtkMultiBlockDataSet() tubeGroup = reader.GetGroup() for tube in _iter_tubes(tubeGroup): polydata = _tube_to_polydata(tube) index = blocks.GetNumberOfBlocks() blocks.SetBlock(index, polydata) prod = vtk.vtkTrivialProducer() prod.SetOutput(blocks) mapper = vtk.vtkCompositePolyDataMapper2() mapper.SetInputConnection(prod.GetOutputPort()) cdsa = vtk.vtkCompositeDataDisplayAttributes() cdsa.SetBlockColor(1, (1, 1, 0)) cdsa.SetBlockColor(2, (0, 1, 1)) mapper.SetCompositeDataDisplayAttributes(cdsa) window = vtk.vtkRenderWindow() window.SetSize(width, height) renderer = vtk.vtkRenderer() window.AddRenderer(renderer) actor = vtk.vtkActor() actor.SetMapper(mapper) renderer.AddActor(actor) renderer.ResetCamera() window.Render() idb = ImageDataSetBuilder(out_dir, 'image/jpg', { 'type': 'spherical', 'phi': phi_vals, 'theta': theta_vals }) idb.start(window, renderer) idb.writeImages() idb.stop()
def initRenderers(self): self.sliceRenderer = vtk.vtkRenderer() self.sliceView.GetRenderWindow().AddRenderer(self.sliceRenderer) self.volumeRenderer = vtk.vtkRenderer() self.volumeView.GetRenderWindow().AddRenderer(self.volumeRenderer) irenSlice = self.sliceRenderer.GetRenderWindow().GetInteractor() irenVolume = self.volumeRenderer.GetRenderWindow().GetInteractor() # set volume picker, since we need to have a pick list volumePicker = vtk.vtkCellPicker() volumePicker.SetPickFromList(True) irenVolume.SetPicker(volumePicker) istyleSlice = self.ClickInteractorStyleImage() irenSlice.SetInteractorStyle(istyleSlice) istyleVolume = self.ClickInteractorStyleTrackball() irenVolume.SetInteractorStyle(istyleVolume) irenSlice.Initialize() irenVolume.Initialize() irenSlice.Start() irenVolume.Start() istyleSlice.AddObserver('LeftButtonClickEvent', self.onSliceClicked) # window level is changed after mouse is released istyleSlice.AddObserver('LeftButtonReleaseEvent', self.onWindowLevelChange) istyleVolume.AddObserver('LeftButtonClickEvent', self.onVolumeClicked) # set up tube actor self.tubeMapper.SetInputConnection(self.tubeProducer.GetOutputPort()) cdsa = vtk.vtkCompositeDataDisplayAttributes() cdsa.SetBlockColor(0, (1,0,0)) self.tubeMapper.SetCompositeDataDisplayAttributes(cdsa) self.tubeActor.SetMapper(self.tubeMapper) picker = self.volumeRenderer.GetRenderWindow().GetInteractor() \ .GetPicker() picker.AddPickList(self.tubeActor)
# Pick in between the stacks # note: the ray passes between the data sets but the block's bounding box encompasses both data sets ([18., -1.], (True, 2), (False, -1, -1), (False, -1, -1, -1)), ([18., -3.], (False, -1), (False, -1, -1), (False, -1, -1, -1)), # Pick the second stack ([25., 0.], (True, 1), (True, 1, 5), (True, 1, 1, 5)), # note: the prop pick hits block #1 because the bounding box of block #1 is picked. The cell picker does not hit a cell of block #1 ([28., 2.], (True, 1), (True, 2, 6), (True, 2, 2, 6)) ] # Construct the data-set, and set up color mapping of the blocks mbd = vtk.vtkMultiBlockDataSet() mbd.SetNumberOfBlocks(3) cda = vtk.vtkCompositeDataDisplayAttributes() m = vtk.vtkCompositePolyDataMapper2() m.SetInputDataObject(mbd) m.SetCompositeDataDisplayAttributes(cda) for blk in range(0, 3): mbd.SetBlock(blk, vtk.vtkPolyData()) poly = mbd.GetBlock(blk) coords = xyz[blk] pts = vtk.vtkPoints() for coord in coords: pts.InsertNextPoint(coord[0], coord[1], float(blk)) polys = vtk.vtkCellArray() for cell in polyconn[blk]: polys.InsertNextCell(len(cell))
'polys':[[1,2,5,4]], # cell id 3 'color':[0,1,0] }, {'coordinates':xyz, 'verts':[[v] for v in range(0,2)], 'polys':[[3,4,7,6]], # cell id 2 'color':[0,0,1] }, {'coordinates':xyz, 'verts':[[v] for v in range(0,1)], 'polys':[[4,5,8,7]], # cell id 1 'color':[1,1,0] }, ] # Construct the data-set, and set up color mapping of the blocks mbd=vtk.vtkMultiBlockDataSet() mbd.SetNumberOfBlocks(len(parts)) cda=vtk.vtkCompositeDataDisplayAttributes() m=vtk.vtkCompositePolyDataMapper2() m.SetInputDataObject(mbd) m.SetCompositeDataDisplayAttributes(cda) for p in range(0,len(parts)): points=vtk.vtkPoints() verts=vtk.vtkCellArray() polys=vtk.vtkCellArray() poly=vtk.vtkPolyData() poly.SetPoints(points) poly.SetVerts(verts) poly.SetPolys(polys)