def initialize (self): debug ("In ExtractVectorComponents::initialize ()") self.fil = vtk.vtkExtractVectorComponents () self.fil.SetInput (self.prev_fil.GetOutput ()) self.comp_var = Tkinter.IntVar () self.comp_var.set (0) self.fil.Update ()
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkExtractVectorComponents(), 'Processing.', ('vtkDataSet',), ('vtkDataSet', 'vtkDataSet', 'vtkDataSet'), replaceDoc=True, inputFunctions=None, outputFunctions=None)
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()
# Time execution timer = vtk.vtkTimerLog() timer.StartTimer() curv.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Points processed: {0}".format(pts.GetOutput().GetNumberOfPoints())) print(" Time to generate curvature: {0}".format(time)) # Break out the curvature into three separate arrays assign = vtk.vtkAssignAttribute() assign.SetInputConnection(curv.GetOutputPort()) assign.Assign("PCACurvature", "VECTORS", "POINT_DATA") extract = vtk.vtkExtractVectorComponents() extract.SetInputConnection(assign.GetOutputPort()) extract.Update() print(extract.GetOutput(0).GetScalarRange()) print(extract.GetOutput(1).GetScalarRange()) print(extract.GetOutput(2).GetScalarRange()) # Three different outputs for different curvatures subMapper = vtk.vtkPointGaussianMapper() subMapper.SetInputConnection(extract.GetOutputPort(0)) subMapper.EmissiveOff() subMapper.SetScaleFactor(0.0) subActor = vtk.vtkActor() subActor.SetMapper(subMapper) subActor.AddPosition(0, 2.25, 0)
# ren1 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # create pipeline # pl3d = vtk.vtkMultiBlockPLOT3DReader() pl3d.SetXYZFileName("" + str(VTK_DATA_ROOT) + "/Data/combxyz.bin") pl3d.SetQFileName("" + str(VTK_DATA_ROOT) + "/Data/combq.bin") pl3d.SetScalarFunctionNumber(100) pl3d.SetVectorFunctionNumber(202) pl3d.Update() output = pl3d.GetOutput().GetBlock(0) vx = vtk.vtkExtractVectorComponents() vx.SetInputData(output) vx.Update() isoVx = vtk.vtkContourFilter() isoVx.SetInputData(vx.GetVxComponent()) isoVx.SetValue(0, .38) normalsVx = vtk.vtkPolyDataNormals() normalsVx.SetInputConnection(isoVx.GetOutputPort()) normalsVx.SetFeatureAngle(45) isoVxMapper = vtk.vtkPolyDataMapper() isoVxMapper.SetInputConnection(normalsVx.GetOutputPort()) isoVxMapper.ScalarVisibilityOff() isoVxMapper.ImmediateModeRenderingOn() isoVxActor = vtk.vtkActor() isoVxActor.SetMapper(isoVxMapper) isoVxActor.GetProperty().SetColor(1, 0.7, 0.6)
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()
# ren1 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # create pipeline # pl3d = vtk.vtkMultiBlockPLOT3DReader() pl3d.SetXYZFileName("" + str(VTK_DATA_ROOT) + "/Data/combxyz.bin") pl3d.SetQFileName("" + str(VTK_DATA_ROOT) + "/Data/combq.bin") pl3d.SetScalarFunctionNumber(100) pl3d.SetVectorFunctionNumber(202) pl3d.Update() output = pl3d.GetOutput().GetBlock(0) vx = vtk.vtkExtractVectorComponents() vx.SetInputData(output) vx.Update() isoVx = vtk.vtkContourFilter() isoVx.SetInputData(vx.GetVxComponent()) isoVx.SetValue(0,.38) normalsVx = vtk.vtkPolyDataNormals() normalsVx.SetInputConnection(isoVx.GetOutputPort()) normalsVx.SetFeatureAngle(45) isoVxMapper = vtk.vtkPolyDataMapper() isoVxMapper.SetInputConnection(normalsVx.GetOutputPort()) isoVxMapper.ScalarVisibilityOff() isoVxMapper.ImmediateModeRenderingOn() isoVxActor = vtk.vtkActor() isoVxActor.SetMapper(isoVxMapper) isoVxActor.GetProperty().SetColor(1,0.7,0.6)
# Time execution timer = vtk.vtkTimerLog() timer.StartTimer() curv.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Points processed: {0}".format(NPts)) print(" Time to generate curvature: {0}".format(time)) # Break out the curvature into thress separate arrays assign = vtk.vtkAssignAttribute() assign.SetInputConnection(curv.GetOutputPort()) assign.Assign("PCACurvature", "VECTORS", "POINT_DATA") extract = vtk.vtkExtractVectorComponents() extract.SetInputConnection(assign.GetOutputPort()) extract.Update() print(extract.GetOutput(0).GetScalarRange()) print(extract.GetOutput(1).GetScalarRange()) print(extract.GetOutput(2).GetScalarRange()) # Three different outputs for different curvatures subMapper = vtk.vtkPointGaussianMapper() subMapper.SetInputConnection(extract.GetOutputPort(0)) subMapper.EmissiveOff() subMapper.SetScaleFactor(0.0) subActor = vtk.vtkActor() subActor.SetMapper(subMapper) subActor.AddPosition(0,2.25,0)