def __init__(self, parent=None): super(VTKViewer, self).__init__(parent) self.slicePosition = 0 self.tubeBlocks = None self.volume = None self.sliceActor = None self.hbox = QHBoxLayout(self) self.sliceView = QVTKRenderWindowInteractor(self) self.hbox.addWidget(self.sliceView) self.sliceSlider = SliceSlider(self) self.hbox.addWidget(self.sliceSlider) self.volumeView = QVTKRenderWindowInteractor(self) self.hbox.addWidget(self.volumeView) self.sliceSlider.slicePosChanged.connect(self.updateSlice) # vtk-producers and filters self.producer = vtk.vtkTrivialProducer() self.reslice = vtk.vtkImageReslice() self.image2worldTransform = vtk.vtkTransform() self.tubeProducer = vtk.vtkTrivialProducer() self.tubeMapper = vtk.vtkCompositePolyDataMapper2() self.tubeActor = vtk.vtkActor()
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkTrivialProducer(), 'Processing.', (), (None,), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def extrude (pts, z, h): cell = vtk.vtkIdList() vtk_pts = vtk.vtkPoints() vtk_pts.SetDataTypeToDouble() [ (vtk_pts.InsertNextPoint(pt[0], pt[1], z), cell.InsertNextId(i)) for i, pt in enumerate(pts) ] pd = vtk.vtkPolyData() pd.Allocate(1, 1) pd.SetPoints(vtk_pts) pd.InsertNextCell(vtk.VTK_POLYGON, cell) prod = vtk.vtkTrivialProducer() prod.SetOutput(pd) extr = vtk.vtkLinearExtrusionFilter() extr.SetInputConnection(prod.GetOutputPort()) extr.SetVector(0, 0, h) pn = vtk.vtkPolyDataNormals() pn.SetInputConnection(extr.GetOutputPort()) pn.AutoOrientNormalsOn() return pn
def extrude(pts, z, h): cell = vtk.vtkIdList() vtk_pts = vtk.vtkPoints() vtk_pts.SetDataTypeToDouble() [(vtk_pts.InsertNextPoint(pt[0], pt[1], z), cell.InsertNextId(i)) for i, pt in enumerate(pts)] pd = vtk.vtkPolyData() pd.Allocate(1, 1) pd.SetPoints(vtk_pts) pd.InsertNextCell(vtk.VTK_POLYGON, cell) prod = vtk.vtkTrivialProducer() prod.SetOutput(pd) extr = vtk.vtkLinearExtrusionFilter() extr.SetInputConnection(prod.GetOutputPort()) extr.SetVector(0, 0, h) pn = vtk.vtkPolyDataNormals() pn.SetInputConnection(extr.GetOutputPort()) pn.AutoOrientNormalsOn() return pn
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__(self, module_manager, vtk.vtkTrivialProducer(), 'Processing.', (), (None, ), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def addMeshToScene(self, mesh): import smtk import vtk import smtk.io.vtk exprt = smtk.io.vtk.ExportVTKData() pd = vtk.vtkPolyData() exprt(mesh, pd) trivialProducer = vtk.vtkTrivialProducer() trivialProducer.SetOutput(pd) return self.addToScene(trivialProducer)
def create_cylinder(r=.5, h=1., res=30): pts = vtk.vtkPoints() pts.SetNumberOfPoints(10 * res) pd = vtk.vtkPolyData() pd.Allocate(3 * res, 1) ang = 2 * math.pi / res ind = 0 for sign in [-1, 1]: y = sign * h / 2 for i in range(res): c0, s0 = math.cos(i * ang), math.sin(i * ang) c1, s1 = math.cos((i + 1) * ang), math.sin((i + 1) * ang) x0, z0 = round(r * c0, 6), round(r * s0, 6) x1, z1 = round(r * c1, 6), round(r * s1, 6) pts.InsertPoint(ind, 0, y, 0) pts.InsertPoint(ind + 1, x0, y, z0) pts.InsertPoint(ind + 2, x1, y, z1) tri = vtk.vtkIdList() [tri.InsertNextId(ind + j) for j in range(3)] t = pd.InsertNextCell(vtk.VTK_TRIANGLE, tri) ind += 3 if sign == 1: pd.ReverseCell(t) pts.InsertPoint(ind, x0, -h / 2, z0) pts.InsertPoint(ind + 1, x0, y, z0) pts.InsertPoint(ind + 2, x1, y, z1) pts.InsertPoint(ind + 3, x1, -h / 2, z1) poly = vtk.vtkIdList() [poly.InsertNextId(ind + j) for j in range(4)] pd.InsertNextCell(vtk.VTK_POLYGON, poly) ind += 4 pd.SetPoints(pts) prod = vtk.vtkTrivialProducer() prod.SetOutput(pd) return prod
def create_cylinder(r=.5, h=1., res=30): pts = vtk.vtkPoints() pts.SetNumberOfPoints(10*res) pd = vtk.vtkPolyData() pd.Allocate(3*res, 1) ang = 2*math.pi/res ind = 0 for sign in [-1, 1]: y = sign*h/2 for i in range(res): c0, s0 = math.cos(i*ang), math.sin(i*ang) c1, s1 = math.cos((i+1)*ang), math.sin((i+1)*ang) x0, z0 = round(r*c0, 6), round(r*s0, 6) x1, z1 = round(r*c1, 6), round(r*s1, 6) pts.InsertPoint(ind, 0, y, 0) pts.InsertPoint(ind+1, x0, y, z0) pts.InsertPoint(ind+2, x1, y, z1) tri = vtk.vtkIdList() [ tri.InsertNextId(ind+j) for j in range(3) ] t = pd.InsertNextCell(vtk.VTK_TRIANGLE, tri) ind += 3 if sign == 1: pd.ReverseCell(t) pts.InsertPoint(ind, x0, -h/2, z0) pts.InsertPoint(ind+1, x0, y, z0) pts.InsertPoint(ind+2, x1, y, z1) pts.InsertPoint(ind+3, x1, -h/2, z1) poly = vtk.vtkIdList() [ poly.InsertNextId(ind+j) for j in range(4) ] pd.InsertNextCell(vtk.VTK_POLYGON, poly) ind += 4 pd.SetPoints(pts) prod = vtk.vtkTrivialProducer() prod.SetOutput(pd) return prod
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 SetInputData(self, data_object): if self._algorithm is None: self._algorithm = vtk.vtkTrivialProducer() self._algorithm.SetOutput(data_object) self._image_data_object = data_object if self.__histogram_stats is None: self.__histogram_stats = vtk.vtkImageHistogramStatistics() # VTK-6 if vtk.vtkVersion().GetVTKMajorVersion() > 5: self.__histogram_stats.SetInputConnection(self._algorithm) else: self.__histogram_stats.SetInput(self._image_data_object)
def split4(_=False): r = vtk.vtkSTLReader() r.SetFileName('Schuerze4.stl') r.Update() bnds = Bnds(*r.GetOutput().GetBounds()) y = bnds.y1 / 2 pts = [[bnds.x1 - 1, y, bnds.z1 - 1], [bnds.x2 + 1, y, bnds.z1 - 1], [bnds.x2 + 1, y, bnds.z2 + 1], [bnds.x1 - 1, y, bnds.z2 + 1]] if _: pts.reverse() cell = vtk.vtkIdList() vtk_pts = vtk.vtkPoints() vtk_pts.SetDataTypeToDouble() [(vtk_pts.InsertNextPoint(pt), cell.InsertNextId(i)) for i, pt in enumerate(pts)] pd = vtk.vtkPolyData() pd.Allocate(1, 1) pd.SetPoints(vtk_pts) pd.InsertNextCell(vtk.VTK_POLYGON, cell) prod = vtk.vtkTrivialProducer() prod.SetOutput(pd) bf = vtkboolPython.vtkPolyDataBooleanFilter() bf.SetInputConnection(r.GetOutputPort()) bf.SetInputConnection(1, prod.GetOutputPort()) bf.SetOperModeToDifference() clean = vtk.vtkCleanPolyData() clean.SetInputConnection(bf.GetOutputPort()) pn = vtk.vtkPolyDataNormals() pn.SetInputConnection(clean.GetOutputPort()) pn.AutoOrientNormalsOn() return pn
def showVolume(self, vtkImageData, preserveState): '''Shows volume of image.''' producer = vtk.vtkTrivialProducer() producer.SetOutput(vtkImageData) flip = vtk.vtkImageFlip() # flip over y axis flip.SetFilteredAxis(1) flip.SetInputConnection(producer.GetOutputPort()) mapper = vtk.vtkSmartVolumeMapper() mapper.SetInputConnection(flip.GetOutputPort()) scalarRange = vtkImageData.GetScalarRange() opacity = vtk.vtkPiecewiseFunction() opacity.AddPoint(scalarRange[0], 0.2) opacity.AddPoint(scalarRange[1], 0.9) color = vtk.vtkColorTransferFunction() color.AddRGBPoint(scalarRange[0], 0, 0, 0) color.AddRGBPoint(scalarRange[1], 1, 1, 1) prop = vtk.vtkVolumeProperty() prop.ShadeOff() prop.SetInterpolationType(vtk.VTK_LINEAR_INTERPOLATION) prop.SetColor(color) prop.SetScalarOpacity(opacity) # save tubes and property if preserving state if preserveState and self.volume: prop = self.volume.GetProperty() self.volumeRenderer.RemoveViewProp(self.volume) else: self.volumeRenderer.RemoveAllViewProps() self.volume = vtk.vtkVolume() self.volume.SetMapper(mapper) self.volume.SetProperty(prop) self.volumeRenderer.AddViewProp(self.volume) self.volumeRenderer.ResetCamera()
15,15,15,17.5,17.5+1e-12,20,20,20]: s.InsertNextValue(v) polys=vtk.vtkCellArray() polys.InsertNextCell(4) for pid in [0,1,3,2]: polys.InsertCellPoint(pid) for cell in [[4,5,7],[5,6,8],[7,10,9],[8,11,10],[7,5,10],[8,10,5]]: polys.InsertNextCell(3) for p in cell: polys.InsertCellPoint(p) ds=vtk.vtkPolyData() ds.SetPoints(pts) ds.GetPointData().SetScalars(s) ds.SetPolys(polys) src=vtk.vtkTrivialProducer() src.SetOutput(ds) # Generate contour bands bands = vtk.vtkBandedPolyDataContourFilter() bands.SetScalarModeToIndex() bands.GenerateContourEdgesOn() bands.ClippingOff() bands.SetInputConnection(src.GetOutputPort()) # This should be handled safely with a warning message bands.SetNumberOfContours(0) bands.Update() # Now add contour values, some of which are equal to point scalars clip_values=[ 15.0, 16.25, 17.5, 18.75, 20 ]
def TestDataType(dataType, filter): if rank == 0: print dataType s = GetSource(dataType) da = vtk.vtkIntArray() da.SetNumberOfTuples(6) if rank == 0: try: ext = s.GetExtent() except: ext = (0, -1, 0, -1, 0, -1) for i in range(6): da.SetValue(i, ext[i]) contr.Broadcast(da, 0) ext = [] for i in range(6): ext.append(da.GetValue(i)) ext = tuple(ext) tp = vtk.vtkTrivialProducer() tp.SetOutput(s) tp.SetWholeExtent(ext) ncells = vtk.vtkIntArray() ncells.SetNumberOfTuples(1) if rank == 0: ncells.SetValue(0, s.GetNumberOfCells()) contr.Broadcast(ncells, 0) result = vtk.vtkIntArray() result.SetNumberOfTuples(1) result.SetValue(0, 1) if rank > 0: if s.GetNumberOfCells() != 0: result.SetValue(0, 0) filter.SetInputConnection(tp.GetOutputPort()) filter.UpdateInformation() filter.SetUpdateExtent(rank, nranks, 0) filter.Update() if filter.GetOutput().GetNumberOfCells() != ncells.GetValue(0) / nranks: result.SetValue(0, 0) filter.UpdateInformation() filter.SetUpdateExtent(rank, nranks, 1) filter.Update() gl = filter.GetOutput().GetCellData().GetArray(vtk.vtkDataSetAttributes.GhostArrayName()) if not gl: result.SetValue(0, 0) else: rng = gl.GetRange() if rng[1] != 1: result.SetValue(0, 0) resArray = vtk.vtkIntArray() resArray.SetNumberOfTuples(1) contr.AllReduce(result, resArray, vtk.vtkCommunicator.MIN_OP) assert resArray.GetValue(0) == 1
def TestDataType(dataType, filter): if rank == 0: print dataType s = GetSource(dataType) da = vtk.vtkIntArray() da.SetNumberOfTuples(6) if rank == 0: try: ext = s.GetExtent() except: ext = (0, -1, 0, -1, 0, -1) for i in range(6): da.SetValue(i, ext[i]) contr.Broadcast(da, 0) ext = [] for i in range(6): ext.append(da.GetValue(i)) ext = tuple(ext) tp = vtk.vtkTrivialProducer() tp.SetOutput(s) tp.SetWholeExtent(ext) ncells = vtk.vtkIntArray() ncells.SetNumberOfTuples(1) if rank == 0: ncells.SetValue(0, s.GetNumberOfCells()) contr.Broadcast(ncells, 0) result = vtk.vtkIntArray() result.SetNumberOfTuples(1) result.SetValue(0, 1) if rank > 0: if s.GetNumberOfCells() != 0: result.SetValue(0, 0) filter.SetInputConnection(tp.GetOutputPort()) filter.Update(rank, nranks, 0) if filter.GetOutput().GetNumberOfCells() != ncells.GetValue(0) / nranks: result.SetValue(0, 0) filter.Update(rank, nranks, 1) gl = filter.GetOutput().GetCellData().GetArray(vtk.vtkDataSetAttributes.GhostArrayName()) if not gl: result.SetValue(0, 0) else: rng = gl.GetRange() if rng[1] != 1: result.SetValue(0, 0) resArray = vtk.vtkIntArray() resArray.SetNumberOfTuples(1) contr.AllReduce(result, resArray, vtk.vtkCommunicator.MIN_OP) assert resArray.GetValue(0) == 1
# Rotate the cube to show the faces being displaced transform = vtk.vtkTransform() transform.Identity() transform.RotateX(45) transform.RotateY(45) transformer = vtk.vtkTransformPolyDataFilter() transformer.SetInputConnection(cube.GetOutputPort()) transformer.SetTransform(transform) transformer.Update() # A trivial multi-block to be used as input for # vtkCompositePolyDataMapper and vtkCompositePolyDataMapper2 mbd = vtk.vtkMultiBlockDataSet() mbd.SetNumberOfBlocks(1) mbd.SetBlock(0, transformer.GetOutput()) source = vtk.vtkTrivialProducer() source.SetOutput(mbd) # Set up the render window and the interactor. renWin = vtk.vtkRenderWindow() renWin.SetSize(600, 400) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) mappers = list() actors = list() renderers = list() polyMapper = vtk.vtkPolyDataMapper() polyMapper.SetInputConnection(transformer.GetOutputPort()) mappers.append(polyMapper)
for v in [9,0,0,9, # y==0 3,4,5,3, # y==1 9,5,0,9, # y==2 0,9,9,0]: # y==3 s.InsertNextValue(v) polys=vtk.vtkCellArray() for cell in [[0,1,5,4],[2,3,7,6],[8,9,13,12],[10,11,15,14]]: polys.InsertNextCell(4) for p in cell: polys.InsertCellPoint(p) ds=vtk.vtkPolyData() ds.SetPoints(pts) ds.GetPointData().SetScalars(s) ds.SetPolys(polys) src=vtk.vtkTrivialProducer() src.SetOutput(ds) # Generate contour bands bands = vtk.vtkBandedPolyDataContourFilter() bands.SetScalarModeToIndex() bands.GenerateContourEdgesOn() bands.ClippingOff() bands.SetInputConnection(src.GetOutputPort()) # Now add contour values, some of which are equal to point scalars clip_values=[0,2,4,6,8,10] for v in clip_values: bands.SetValue(clip_values.index(v), v) bands.Update()
def handle_supported_types(writer, typelist, output_type=vtk.VTK_FLOAT): '''Cast to required type If the type sent to writer is not in typelist, it will be converted to output_type. Args: writer (vtk.vtkImageWriter): The file writer typelist (list): List of types supported by this writer output_type (int): Output type request as fall back Returns: Nothing ''' # Determine input scalar type input_algorithm = writer.GetInputAlgorithm() # Input set with SetInputData? if type(input_algorithm) == type(vtk.vtkTrivialProducer()): scalar_type = writer.GetInput().GetScalarType() # Input set with SetInputConnection else: input_algorithm.Update() scalar_type = input_algorithm.GetOutput().GetScalarType() # Cast if not appropriate if scalar_type not in typelist: # If we can cast to float, just cast to float. Otherwise, rescale # the datatypes. if output_type != vtk.VTK_FLOAT: # Hack to get data type range source = vtk.vtkImageEllipsoidSource() source.SetWholeExtent(0, 1, 0, 1, 0, 0) source.SetCenter(10, 10, 0) source.SetRadius(3, 4, 0) source.SetOutputScalarType(output_type) source.Update() # Compute min/max scalar_range = input_algorithm.GetOutput().GetScalarRange() dtype_range = [ source.GetOutput().GetScalarTypeMin(), source.GetOutput().GetScalarTypeMax() ] # Note the equation for shift/scale: # u = (v + ScalarShift)*ScalarScale m = float(dtype_range[1] - dtype_range[0]) / float(scalar_range[1] - scalar_range[0]) b = float(dtype_range[1] - m * scalar_range[1]) b = b / m caster = vtk.vtkImageReslice() caster.SetScalarShift(b) caster.SetScalarScale(m) else: caster = vtk.vtkImageCast() caster.SetInputConnection(input_algorithm.GetOutputPort()) caster.SetOutputScalarType(output_type) caster.Update() writer.SetInputConnection(caster.GetOutputPort())
def create_sill4(l): pr1 = [(0, 0), (1.75, 0), (1.75, 5. / 6 * 1.875), (0, 1.0833)] pr2 = [(.75, 0), (1.75, 0), (1.75, 5. / 6 * 1.875), (.75, 5. / 6 * 1.875)] extr1 = extrude(pr1, l + .75 + .54125) extr2 = extrude(pr2, .54125) bf = vtkboolPython.vtkPolyDataBooleanFilter() bf.SetInputConnection(extr1.GetOutputPort()) bf.SetInputConnection(1, extr2.GetOutputPort()) bf.Update() bnds = Bnds(*bf.GetOutput().GetBounds()) pts = [(bnds.x1 - 2, bnds.y1 - 2, 0), (bnds.x2 + 2, bnds.y1 - 2, 0), (bnds.x2 + 2, bnds.y2 + 2, 0), (bnds.x1 - 2, bnds.y2 + 2, 0)] cell = vtk.vtkIdList() _pts = vtk.vtkPoints() _pts.SetDataTypeToDouble() [(_pts.InsertNextPoint(pt), cell.InsertNextId(i)) for i, pt in enumerate(pts[::-1])] pd = vtk.vtkPolyData() pd.Allocate(1, 1) pd.SetPoints(_pts) pd.InsertNextCell(vtk.VTK_POLYGON, cell) prod = vtk.vtkTrivialProducer() prod.SetOutput(pd) tr = vtk.vtkTransform() tr.Translate(0, 0, bnds.z2) tr.RotateY(45) tp = vtk.vtkTransformPolyDataFilter() tp.SetTransform(tr) tp.SetInputConnection(prod.GetOutputPort()) bf2 = vtkboolPython.vtkPolyDataBooleanFilter() bf2.SetInputConnection(bf.GetOutputPort()) bf2.SetInputConnection(1, tp.GetOutputPort()) bf2.SetOperModeToDifference() clean = vtk.vtkCleanPolyData() clean.SetInputConnection(bf2.GetOutputPort()) pn = vtk.vtkPolyDataNormals() pn.SetInputConnection(clean.GetOutputPort()) pn.AutoOrientNormalsOn() tr2 = vtk.vtkTransform() tr2.Translate(1.75, 0, 0) tr2.RotateZ(180) tr2.RotateX(90) tp2 = vtk.vtkTransformPolyDataFilter() tp2.SetTransform(tr2) tp2.SetInputConnection(pn.GetOutputPort()) return tp2