def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkVectorNorm(), 'Processing.', ('vtkDataSet',), ('vtkDataSet',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def generate_arrays (self, outputGrid): if (self.vector_name.get () == "Source vector"): return outputGrid # Calculate vorticity debug ("Calculating vorticity...") self.fil.SetInput (outputGrid) self.fil.Update () outputGrid = self.fil.GetOutput () outputGrid.Update () # Convert vorticity to point data cellToPoint = vtk.vtkCellDataToPointData () cellToPoint.SetInput (outputGrid) cellToPoint.PassCellDataOff () cellToPoint.Update () outputGrid = cellToPoint.GetOutput () # Allow the user to constrain vorticity to one dimension (this # has the same effect as flattening the source array # (e.g. velocity) onto a plane). desiredDimensionIndex = self.dimension_to_allow.get () if desiredDimensionIndex != -1: vorticity = outputGrid.GetPointData ().GetArray ("Vorticity") for i in range(3): if i != desiredDimensionIndex: vorticity.FillComponent(i, 0.0) outputGrid.Update () # Extract vector norm vectorNorm = vtk.vtkVectorNorm () vectorNorm.SetAttributeModeToUsePointData () vectorNorm.SetInput (outputGrid) vectorNorm.Update () outputGrid = vectorNorm.GetOutput() return outputGrid
def execute(self, obj, event): if self.timer_count == 10: self.timer_count = 0 warpVector = vtk.vtkWarpVector() warpVector.SetInputData(pd) warpVector.SetScaleFactor(0.1 * (self.timer_count + 1)) warpVector.Update() poly = warpVector.GetPolyDataOutput() getScalars = vtk.vtkExtractVectorComponents() getScalars.SetInputData(poly) getScalars.Update() vectorNorm = vtk.vtkVectorNorm() vectorNorm.SetInputData(poly) vectorNorm.Update() scalars = [] scalars.append(getScalars.GetVzComponent()) scalars.append(vectorNorm.GetOutput()) scalars.append(getScalars.GetVxComponent()) scalars.append(getScalars.GetVyComponent()) names = ("Z", "Mag", "X", "Y") for k, a in enumerate(self.actors): calc = vtk.vtkArrayCalculator() scalars[k].GetPointData().GetScalars().SetName(names[k]) calc.SetInputData(scalars[k]) calc.AddScalarArrayName(names[k]) calc.SetResultArrayName(names[k]) calc.SetFunction("%s * 0.1 * %f" % (names[k], self.timer_count + 1)) calc.Update() mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(calc.GetOutput()) mapper.SetScalarRange(calc.GetOutput().GetScalarRange()) mapper.SetScalarModeToUsePointData() mapper.SetColorModeToMapScalars() mapper.Update() a.SetMapper(mapper) cb.scalar_bars[k].SetLookupTable(mapper.GetLookupTable()) iren = obj iren.GetRenderWindow().Render() time.sleep(0.3) if self.key == "Up": try: os.mkdir(self.directory) except: pass w2i = vtk.vtkWindowToImageFilter() w2i.SetInput(obj.GetRenderWindow()) w2i.Update() png = vtk.vtkPNGWriter() png.SetInputConnection(w2i.GetOutputPort()) png.SetFileName(self.directory + os.sep + "frame{:d}.png".format(self.timer_count)) png.Update() png.Write() self.timer_count += 1
def animate(self, pd, ind): """ Helper function called by **deformableRegistration** if **animate** is *True*. Spawns a window with an interactive 3-D rendering of the current analyzed object in its reference state. The displacements calculated from the deformable image registration can be applied to this object to animate the deformation by pressing the RIGHT-ARROW. Pressing the UP-ARROW will animate and also save the frames to disk. Parameters ---------- pd : vtkPolyData The current analyzed object's reference geometry. ind : int The index of the current polydata in **rsurfs**. Necessary for naming directory created if animation frames are saved. """ pd.GetPointData().SetActiveVectors("Displacement") class vtkTimerCallback(object): def __init__(self): self.timer_count = 0 def execute(self, obj, event): if self.timer_count == 10: self.timer_count = 0 warpVector = vtk.vtkWarpVector() warpVector.SetInputData(pd) warpVector.SetScaleFactor(0.1 * (self.timer_count + 1)) warpVector.Update() poly = warpVector.GetPolyDataOutput() getScalars = vtk.vtkExtractVectorComponents() getScalars.SetInputData(poly) getScalars.Update() vectorNorm = vtk.vtkVectorNorm() vectorNorm.SetInputData(poly) vectorNorm.Update() scalars = [] scalars.append(getScalars.GetVzComponent()) scalars.append(vectorNorm.GetOutput()) scalars.append(getScalars.GetVxComponent()) scalars.append(getScalars.GetVyComponent()) names = ("Z", "Mag", "X", "Y") for k, a in enumerate(self.actors): calc = vtk.vtkArrayCalculator() scalars[k].GetPointData().GetScalars().SetName(names[k]) calc.SetInputData(scalars[k]) calc.AddScalarArrayName(names[k]) calc.SetResultArrayName(names[k]) calc.SetFunction("%s * 0.1 * %f" % (names[k], self.timer_count + 1)) calc.Update() mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(calc.GetOutput()) mapper.SetScalarRange(calc.GetOutput().GetScalarRange()) mapper.SetScalarModeToUsePointData() mapper.SetColorModeToMapScalars() mapper.Update() a.SetMapper(mapper) cb.scalar_bars[k].SetLookupTable(mapper.GetLookupTable()) iren = obj iren.GetRenderWindow().Render() time.sleep(0.3) if self.key == "Up": try: os.mkdir(self.directory) except: pass w2i = vtk.vtkWindowToImageFilter() w2i.SetInput(obj.GetRenderWindow()) w2i.Update() png = vtk.vtkPNGWriter() png.SetInputConnection(w2i.GetOutputPort()) png.SetFileName(self.directory + os.sep + "frame{:d}.png".format(self.timer_count)) png.Update() png.Write() self.timer_count += 1 def Keypress(self, obj, event): self.key = obj.GetKeySym() if self.key == "Right" or self.key == "Up": for i in range(10): obj.CreateOneShotTimer(1) renwin = vtk.vtkRenderWindow() iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renwin) iren.Initialize() cb = vtkTimerCallback() xmins = (0, 0.5, 0, 0.5) xmaxs = (0.5, 1, 0.5, 1) ymins = (0, 0, 0.5, 0.5) ymaxs = (0.5, 0.5, 1, 1) titles = ('Z Displacement', 'Magnitude', 'X Displacement', 'Y Displacement') cb.actors = [] cb.scalar_bars = [] cb.directory = str( os.path.normpath(self._def_dir + os.sep + "animation{:0d}".format(ind + 1))) warpVector = vtk.vtkWarpVector() warpVector.SetInputData(pd) warpVector.Update() poly = warpVector.GetPolyDataOutput() getScalars = vtk.vtkExtractVectorComponents() getScalars.SetInputData(poly) getScalars.Update() vectorNorm = vtk.vtkVectorNorm() vectorNorm.SetInputData(poly) vectorNorm.Update() scalars = [] scalars.append(getScalars.GetVzComponent()) scalars.append(vectorNorm.GetOutput()) scalars.append(getScalars.GetVxComponent()) scalars.append(getScalars.GetVyComponent()) bounds = np.zeros(6, np.float32) pd.GetBounds(bounds) length = np.min(bounds[1::2] - bounds[0:-1:2]) * 0.2 bounds[1] = bounds[0] + length bounds[3] = bounds[2] + length bounds[5] = bounds[4] + length for j in range(4): mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(scalars[j]) mapper.SetScalarRange(scalars[j].GetScalarRange()) mapper.SetScalarModeToUsePointData() mapper.SetColorModeToMapScalars() scalar_bar = vtk.vtkScalarBarActor() scalar_bar.SetLookupTable(mapper.GetLookupTable()) scalar_bar.SetTitle(titles[j]) scalar_bar.SetLabelFormat("%3.3f") cb.scalar_bars.append(scalar_bar) actor = vtk.vtkActor() actor.SetMapper(mapper) cb.actors.append(actor) renderer = vtk.vtkRenderer() renderer.SetBackground(0., 0., 0.) renwin.AddRenderer(renderer) if j == 0: camera = renderer.GetActiveCamera() else: renderer.SetActiveCamera(camera) triad = vtk.vtkCubeAxesActor() triad.SetCamera(camera) triad.SetFlyModeToStaticTriad() triad.SetBounds(bounds) triad.GetXAxesLinesProperty().SetColor(1.0, 0.0, 0.0) triad.GetYAxesLinesProperty().SetColor(0.0, 1.0, 0.0) triad.GetZAxesLinesProperty().SetColor(0.0, 0.0, 1.0) triad.GetXAxesLinesProperty().SetLineWidth(3.0) triad.GetYAxesLinesProperty().SetLineWidth(3.0) triad.GetZAxesLinesProperty().SetLineWidth(3.0) triad.XAxisLabelVisibilityOff() triad.YAxisLabelVisibilityOff() triad.ZAxisLabelVisibilityOff() triad.XAxisTickVisibilityOff() triad.YAxisTickVisibilityOff() triad.ZAxisTickVisibilityOff() triad.XAxisMinorTickVisibilityOff() triad.YAxisMinorTickVisibilityOff() triad.ZAxisMinorTickVisibilityOff() renderer.SetViewport(xmins[j], ymins[j], xmaxs[j], ymaxs[j]) renderer.AddActor(actor) renderer.AddActor2D(scalar_bar) renderer.AddActor(triad) renderer.ResetCamera() renwin.Render() iren.AddObserver('TimerEvent', cb.execute) iren.AddObserver('KeyPressEvent', cb.Keypress) iren.Start()
#### original vtk code import vtk # loading a vtk file as input reader = vtk.vtkXMLUnstructuredGridReader() reader.SetFileName("../../vel-0004.vtk") reader.Update() grid = reader.GetOutput() # grab the model centre and bounds centre = grid.GetCenter() bounds = grid.GetBounds() # grab the norm of the vectors norm = vtk.vtkVectorNorm() norm.SetInput(grid) maxNorm = grid.GetPointData().GetVectors().GetMaxNorm() # to make arrow glyphs need an arrow source arrow = vtk.vtkArrowSource() # the arrows are 3D glyphs so set that up now glyph = vtk.vtkGlyph3D() glyph.ScalingOn() glyph.SetScaleModeToScaleByScalar() glyph.SetColorModeToColorByScalar() glyph.SetVectorModeToUseVector() glyph.SetScaleFactor(0.1/maxNorm) glyph.SetInput(norm.GetOutput())
locator.SetNumberOfPointsPerBucket(2) locator.AutomaticOff() v16 = vtk.vtkVolume16Reader() v16.SetDataDimensions(64,64) v16.GetOutput().SetOrigin(0.0,0.0,0.0) v16.SetDataByteOrderToLittleEndian() v16.SetFilePrefix("" + str(VTK_DATA_ROOT) + "/Data/headsq/quarter") v16.SetImageRange(1,93) v16.SetDataSpacing(3.2,3.2,1.5) iso = vtk.vtkMarchingCubes() iso.SetInputConnection(v16.GetOutputPort()) iso.SetValue(0,1150) iso.ComputeGradientsOn() iso.ComputeScalarsOff() iso.SetLocator(locator) gradient = vtk.vtkVectorNorm() gradient.SetInputConnection(iso.GetOutputPort()) isoMapper = vtk.vtkDataSetMapper() isoMapper.SetInputConnection(gradient.GetOutputPort()) isoMapper.ScalarVisibilityOn() isoMapper.SetScalarRange(0,1200) isoMapper.ImmediateModeRenderingOn() isoActor = vtk.vtkActor() isoActor.SetMapper(isoMapper) isoProp = isoActor.GetProperty() isoProp.SetColor(antique_white) outline = vtk.vtkOutlineFilter() outline.SetInputConnection(v16.GetOutputPort()) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort()) outlineActor = vtk.vtkActor()
v16 = vtk.vtkVolume16Reader() v16.SetDataDimensions(64, 64) v16.GetOutput().SetOrigin(0.0, 0.0, 0.0) v16.SetDataByteOrderToLittleEndian() v16.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter") v16.SetImageRange(1, 93) v16.SetDataSpacing(3.2, 3.2, 1.5) iso = vtk.vtkMarchingCubes() iso.SetInputConnection(v16.GetOutputPort()) iso.SetValue(0, 1150) iso.ComputeGradientsOn() iso.ComputeScalarsOff() iso.SetLocator(locator) gradient = vtk.vtkVectorNorm() gradient.SetInputConnection(iso.GetOutputPort()) isoMapper = vtk.vtkDataSetMapper() isoMapper.SetInputConnection(gradient.GetOutputPort()) isoMapper.ScalarVisibilityOn() isoMapper.SetScalarRange(0, 1200) isoMapper.ImmediateModeRenderingOn() isoActor = vtk.vtkActor() isoActor.SetMapper(isoMapper) isoProp = isoActor.GetProperty() isoProp.SetColor(GetRGBColor('antique_white')) outline = vtk.vtkOutlineFilter()
def execute(self, obj, event): if self.timer_count == 10: self.timer_count = 0 warpVector = vtk.vtkWarpVector() warpVector.SetInputData(pd) warpVector.SetScaleFactor(0.1 * (self.timer_count + 1)) warpVector.Update() poly = warpVector.GetPolyDataOutput() getScalars = vtk.vtkExtractVectorComponents() getScalars.SetInputData(poly) getScalars.Update() vectorNorm = vtk.vtkVectorNorm() vectorNorm.SetInputData(poly) vectorNorm.Update() scalars = [] scalars.append( getScalars.GetVzComponent()) scalars.append( vectorNorm.GetOutput()) scalars.append( getScalars.GetVxComponent()) scalars.append( getScalars.GetVyComponent()) names = ("Z", "Mag", "X", "Y") for k, a in enumerate(self.actors): calc = vtk.vtkArrayCalculator() scalars[k].GetPointData().GetScalars().SetName(names[k]) calc.SetInputData(scalars[k]) calc.AddScalarArrayName(names[k]) calc.SetResultArrayName(names[k]) calc.SetFunction( "%s * 0.1 * %f" % (names[k], self.timer_count + 1)) calc.Update() mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(calc.GetOutput()) mapper.SetScalarRange(calc.GetOutput().GetScalarRange()) mapper.SetScalarModeToUsePointData() mapper.SetColorModeToMapScalars() mapper.Update() a.SetMapper(mapper) cb.scalar_bars[k].SetLookupTable(mapper.GetLookupTable()) iren = obj iren.GetRenderWindow().Render() time.sleep(0.3) if self.key == "Up": try: os.mkdir(self.directory) except: pass w2i = vtk.vtkWindowToImageFilter() w2i.SetInput(obj.GetRenderWindow()) w2i.Update() png = vtk.vtkPNGWriter() png.SetInputConnection(w2i.GetOutputPort()) png.SetFileName(self.directory + os.sep + "frame{:d}.png".format(self.timer_count)) png.Update() png.Write() self.timer_count += 1
def animate(self, pd, ind): """ Helper function called by **deformableRegistration** if **animate** is *True*. Spawns a window with an interactive 3-D rendering of the current analyzed object in its reference state. The displacements calculated from the deformable image registration can be applied to this object to animate the deformation by pressing the RIGHT-ARROW. Pressing the UP-ARROW will animate and also save the frames to disk. Parameters ---------- pd : vtkPolyData The current analyzed object's reference geometry. ind : int The index of the current polydata in **rsurfs**. Necessary for naming directory created if animation frames are saved. """ pd.GetPointData().SetActiveVectors("Displacement") class vtkTimerCallback(object): def __init__(self): self.timer_count = 0 def execute(self, obj, event): if self.timer_count == 10: self.timer_count = 0 warpVector = vtk.vtkWarpVector() warpVector.SetInputData(pd) warpVector.SetScaleFactor(0.1 * (self.timer_count + 1)) warpVector.Update() poly = warpVector.GetPolyDataOutput() getScalars = vtk.vtkExtractVectorComponents() getScalars.SetInputData(poly) getScalars.Update() vectorNorm = vtk.vtkVectorNorm() vectorNorm.SetInputData(poly) vectorNorm.Update() scalars = [] scalars.append( getScalars.GetVzComponent()) scalars.append( vectorNorm.GetOutput()) scalars.append( getScalars.GetVxComponent()) scalars.append( getScalars.GetVyComponent()) names = ("Z", "Mag", "X", "Y") for k, a in enumerate(self.actors): calc = vtk.vtkArrayCalculator() scalars[k].GetPointData().GetScalars().SetName(names[k]) calc.SetInputData(scalars[k]) calc.AddScalarArrayName(names[k]) calc.SetResultArrayName(names[k]) calc.SetFunction( "%s * 0.1 * %f" % (names[k], self.timer_count + 1)) calc.Update() mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(calc.GetOutput()) mapper.SetScalarRange(calc.GetOutput().GetScalarRange()) mapper.SetScalarModeToUsePointData() mapper.SetColorModeToMapScalars() mapper.Update() a.SetMapper(mapper) cb.scalar_bars[k].SetLookupTable(mapper.GetLookupTable()) iren = obj iren.GetRenderWindow().Render() time.sleep(0.3) if self.key == "Up": try: os.mkdir(self.directory) except: pass w2i = vtk.vtkWindowToImageFilter() w2i.SetInput(obj.GetRenderWindow()) w2i.Update() png = vtk.vtkPNGWriter() png.SetInputConnection(w2i.GetOutputPort()) png.SetFileName(self.directory + os.sep + "frame{:d}.png".format(self.timer_count)) png.Update() png.Write() self.timer_count += 1 def Keypress(self, obj, event): self.key = obj.GetKeySym() if self.key == "Right" or self.key == "Up": for i in range(10): obj.CreateOneShotTimer(1) renwin = vtk.vtkRenderWindow() iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renwin) iren.Initialize() cb = vtkTimerCallback() xmins = (0, 0.5, 0, 0.5) xmaxs = (0.5, 1, 0.5, 1) ymins = (0, 0, 0.5, 0.5) ymaxs = (0.5, 0.5, 1, 1) titles = ('Z Displacement', 'Magnitude', 'X Displacement', 'Y Displacement') cb.actors = [] cb.scalar_bars = [] cb.directory = str(os.path.normpath( self._def_dir + os.sep + "animation{:0d}".format(ind + 1))) warpVector = vtk.vtkWarpVector() warpVector.SetInputData(pd) warpVector.Update() poly = warpVector.GetPolyDataOutput() getScalars = vtk.vtkExtractVectorComponents() getScalars.SetInputData(poly) getScalars.Update() vectorNorm = vtk.vtkVectorNorm() vectorNorm.SetInputData(poly) vectorNorm.Update() scalars = [] scalars.append( getScalars.GetVzComponent()) scalars.append( vectorNorm.GetOutput()) scalars.append( getScalars.GetVxComponent()) scalars.append( getScalars.GetVyComponent()) bounds = np.zeros(6, np.float32) pd.GetBounds(bounds) length = np.min(bounds[1::2] - bounds[0:-1:2]) * 0.2 bounds[1] = bounds[0] + length bounds[3] = bounds[2] + length bounds[5] = bounds[4] + length for j in range(4): mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(scalars[j]) mapper.SetScalarRange(scalars[j].GetScalarRange()) mapper.SetScalarModeToUsePointData() mapper.SetColorModeToMapScalars() scalar_bar = vtk.vtkScalarBarActor() scalar_bar.SetLookupTable(mapper.GetLookupTable()) scalar_bar.SetTitle(titles[j]) scalar_bar.SetLabelFormat("%3.3f") cb.scalar_bars.append(scalar_bar) actor = vtk.vtkActor() actor.SetMapper(mapper) cb.actors.append(actor) renderer = vtk.vtkRenderer() renderer.SetBackground(0., 0., 0.) renwin.AddRenderer(renderer) if j == 0: camera = renderer.GetActiveCamera() else: renderer.SetActiveCamera(camera) triad = vtk.vtkCubeAxesActor() triad.SetCamera(camera) triad.SetFlyModeToStaticTriad() triad.SetBounds(bounds) triad.GetXAxesLinesProperty().SetColor(1.0, 0.0, 0.0) triad.GetYAxesLinesProperty().SetColor(0.0, 1.0, 0.0) triad.GetZAxesLinesProperty().SetColor(0.0, 0.0, 1.0) triad.GetXAxesLinesProperty().SetLineWidth(3.0) triad.GetYAxesLinesProperty().SetLineWidth(3.0) triad.GetZAxesLinesProperty().SetLineWidth(3.0) triad.XAxisLabelVisibilityOff() triad.YAxisLabelVisibilityOff() triad.ZAxisLabelVisibilityOff() triad.XAxisTickVisibilityOff() triad.YAxisTickVisibilityOff() triad.ZAxisTickVisibilityOff() triad.XAxisMinorTickVisibilityOff() triad.YAxisMinorTickVisibilityOff() triad.ZAxisMinorTickVisibilityOff() renderer.SetViewport(xmins[j], ymins[j], xmaxs[j], ymaxs[j]) renderer.AddActor(actor) renderer.AddActor2D(scalar_bar) renderer.AddActor(triad) renderer.ResetCamera() renwin.Render() iren.AddObserver('TimerEvent', cb.execute) iren.AddObserver('KeyPressEvent', cb.Keypress) iren.Start()
def initialize (self): debug ("In ExtractVectorNorm::initialize ()") self.fil = vtk.vtkVectorNorm () self.fil.SetInput (self.prev_fil.GetOutput ()) self.fil.Update ()
i = 128 while i < 256: lut.SetTableValue(i, (i - 128.0) / 128.0, (i - 128.0) / 128.0, (i - 128.0) / 128.0, 1) i += 1 plateMapper = vtk.vtkDataSetMapper() plateMapper.SetInputConnection(color.GetOutputPort()) plateMapper.SetLookupTable(lut) plateMapper.SetScalarRange(-1, 1) plateActor = vtk.vtkActor() plateActor.SetMapper(plateMapper) color2 = vtk.vtkVectorNorm() color2.SetInputConnection(plate.GetOutputPort()) color2.NormalizeOn() plateMapper2 = vtk.vtkDataSetMapper() plateMapper2.SetInputConnection(color2.GetOutputPort()) plateMapper2.SetLookupTable(lut) plateMapper2.SetScalarRange(0, 1) plateActor2 = vtk.vtkActor() plateActor2.SetMapper(plateMapper2) # Add the actors to the renderer, set the background and size # ren1.SetViewport(0, 0, .5, 1) ren2.SetViewport(.5, 0, 1, 1)