def get_lookup_table(minimum_value, maximum_value, log=False, colorscale="jet", number_of_colors=1000): """Returrns a vtk lookup_table based on the specified matplotlib colorscale""" import matplotlib import matplotlib.cm if log: lut = _vtk.vtkLogLookupTable() else: lut = _vtk.vtkLookupTable() lut.SetTableRange(minimum_value, maximum_value) lut.SetNumberOfColors(number_of_colors) lut.Build() for i in range(number_of_colors): color = matplotlib.cm.cmap_d[colorscale](float(i) / float(number_of_colors)) lut.SetTableValue(i, color[0], color[1], color[2], 1.) lut.SetUseBelowRangeColor(True) lut.SetUseAboveRangeColor(True) return lut
plane = vtk.vtkImageDataGeometryFilter() plane.SetInputConnection(ptLoad.GetOutputPort()) plane.SetExtent(2,2,0,99,0,99) # Generate ellipsoids sphere = vtk.vtkSphereSource() sphere.SetThetaResolution(8) sphere.SetPhiResolution(8) ellipsoids = vtk.vtkTensorGlyph() ellipsoids.SetInputConnection(ptLoad.GetOutputPort()) ellipsoids.SetSourceConnection(sphere.GetOutputPort()) ellipsoids.SetScaleFactor(10) ellipsoids.ClampScalingOn() ellipNormals = vtk.vtkPolyDataNormals() ellipNormals.SetInputConnection(ellipsoids.GetOutputPort()) # Map contour lut = vtk.vtkLogLookupTable() lut.SetHueRange(.6667,0.0) ellipMapper = vtk.vtkPolyDataMapper() ellipMapper.SetInputConnection(ellipNormals.GetOutputPort()) ellipMapper.SetLookupTable(lut) plane.Update() #force update for scalar range ellipMapper.SetScalarRange(plane.GetOutput().GetScalarRange()) ellipActor = vtk.vtkActor() ellipActor.SetMapper(ellipMapper) # # Create outline around data # outline = vtk.vtkOutlineFilter() outline.SetInputConnection(ptLoad.GetOutputPort()) outlineMapper = vtk.vtkPolyDataMapper()
def createVTKPlots(dir, logFiles, disableRescaling=False, showMesh=False, showIsolines=False): outputdir = 'plots/vtk' if not os.path.exists(outputdir): os.makedirs(outputdir) if not all(l.problem == logFiles[0].problem for l in logFiles): raise (ValueError) problem = logFiles[0].problem setup = getProblemVTKSettings(problem) minVal = {} maxVal = {} fieldNames = [] for log in logFiles: reader = vtk.vtkUnstructuredGridReader() reader.SetFileName(log.vtkFile) reader.ReadAllScalarsOn() reader.ReadAllVectorsOn() reader.Update() output = reader.GetOutput() for i in range(output.GetCellData().GetNumberOfArrays()): f = output.GetCellData().GetArrayName(i) if f not in fieldNames: fieldNames.append(f) valRange = output.GetCellData().GetArray(i).GetRange() minVal[f] = valRange[ 0] #np.maximum(minVal[f] if f in minVal else -np.inf, valRange[0]) maxVal[f] = valRange[ 1] #np.minimum(maxVal[f] if f in maxVal else np.inf, valRange[1]) ctf = vtk.vtkColorTransferFunction() ctf.SetColorSpaceToRGB() cmap = cm.get_cmap('viridis', 256) ctf.AddRGBPoint(-1, 1, 0, 0) for i, rgb in enumerate(cmap.colors): ctf.AddRGBPoint(float(i / 256), rgb[0], rgb[1], rgb[2]) for log in logFiles: reader = vtk.vtkUnstructuredGridReader() reader.SetFileName(log.vtkFile) reader.ReadAllScalarsOn() reader.ReadAllVectorsOn() reader.Update() output = reader.GetOutput() if setup['logScale']: lut = vtk.vtkLogLookupTable() else: lut = vtk.vtkLookupTable() lut.SetNumberOfTableValues(256) lut.Build() for i in range(0, 256): rgb = list(ctf.GetColor(float(i) / 256)) + [1] lut.SetTableValue(i, rgb) camera = vtk.vtkCamera() camPos = setup['cameraPosition'] camFP = setup['cameraFocalPoint'] camera.SetPosition(camPos[0], camPos[1], camPos[2]) camera.SetFocalPoint(camFP[0], camFP[1], camFP[2]) for f in fieldNames: mapper = vtk.vtkDataSetMapper() mapper.SetInputData(output) mapper.ScalarVisibilityOn() mapper.SetColorModeToMapScalars() mapper.SetLookupTable(lut) mapper.SetScalarModeToUsePointFieldData() mapper.SelectColorArray(f) if disableRescaling: mapper.SetScalarRange( output.GetCellData().GetArray(f).GetRange()) else: mapper.SetScalarRange(minVal[f], maxVal[f]) scalarBar = vtk.vtkScalarBarActor() scalarBar.SetLookupTable(mapper.GetLookupTable()) scalarBar.SetTitle(latexify(f)) scalarBar.SetOrientationToHorizontal() scalarBar.SetPosition(0.1, -0.001) scalarBar.SetLabelFormat('%-#6.1e') scalarBar.SetWidth(0.8) scalarBar.SetHeight(0.1) scalarBar.SetNumberOfLabels(4) scalarBar.SetMaximumNumberOfColors(256) scalarBar.SetTitleRatio(0.6) titleprop = scalarBar.GetTitleTextProperty() titleprop.ShadowOff() titleprop.BoldOff() titleprop.SetColor(0, 0, 0) labelprop = scalarBar.GetLabelTextProperty() labelprop.ShadowOff() labelprop.BoldOff() labelprop.SetColor(0, 0, 0) scalarBar.SetLabelTextProperty(labelprop) actor = vtk.vtkActor() actor.SetMapper(mapper) if showMesh: actor.GetProperty().EdgeVisibilityOn() actor.GetProperty().SetLineWidth(1.0) if showIsolines: output.GetPointData().SetActiveScalars(f) contours = vtk.vtkContourFilter() contours.SetInputData(output) contours.GenerateValues(10, minVal[f], maxVal[f]) contMapper = vtk.vtkPolyDataMapper() contMapper.SetInputConnection(contours.GetOutputPort()) contMapper.ScalarVisibilityOff() contActor = vtk.vtkActor() contActor.SetMapper(contMapper) #contActor.GetProperty().SetColor(1,1,1) renderer = vtk.vtkRenderer() renderer.AddActor(actor) if showIsolines: renderer.AddActor(contActor) renderer.AddActor2D(scalarBar) renderer.UseFXAAOn() renderer.SetBackground(1, 1, 1) renderer.SetActiveCamera(camera) render_window = vtk.vtkRenderWindow() render_window.SetOffScreenRendering(True) render_window.AddRenderer(renderer) render_window.SetSize(800, 1000) render_window.Render() windowToImageFilter = vtk.vtkWindowToImageFilter() windowToImageFilter.SetInput(render_window) windowToImageFilter.ReadFrontBufferOff() windowToImageFilter.Update() writer = vtk.vtkPNGWriter() writer.SetFileName(outputdir + '/' + log.logFile + "_" + f + ".png") writer.SetInputConnection(windowToImageFilter.GetOutputPort()) writer.Write() print('Plot created:\t' + outputdir + '/' + log.logFile + "_" + f + ".png")
input = ptLoad.GetOutput() pass # Generate hyperstreamlines s1 = vtk.vtkHyperStreamline() s1.SetInputData(input) s1.SetStartPosition(9, 9, -9) s1.IntegrateMinorEigenvector() s1.SetMaximumPropagationDistance(18.0) s1.SetIntegrationStepLength(0.1) s1.SetStepLength(0.01) s1.SetRadius(0.25) s1.SetNumberOfSides(18) s1.SetIntegrationDirection(VTK_INTEGRATE_BOTH_DIRECTIONS) s1.Update() # Map hyperstreamlines lut = vtk.vtkLogLookupTable() lut.SetHueRange(.6667, 0.0) s1Mapper = vtk.vtkPolyDataMapper() s1Mapper.SetInputConnection(s1.GetOutputPort()) s1Mapper.SetLookupTable(lut) ptLoad.Update() #force update for scalar range s1Mapper.SetScalarRange(ptLoad.GetOutput().GetScalarRange()) s1Actor = vtk.vtkActor() s1Actor.SetMapper(s1Mapper) s2 = vtk.vtkHyperStreamline() s2.SetInputData(input) s2.SetStartPosition(-9, -9, -9) s2.IntegrateMinorEigenvector() s2.SetMaximumPropagationDistance(18.0) s2.SetIntegrationStepLength(0.1)
def main(): colors = vtk.vtkNamedColors() # Create the RenderWindow, Renderer and Interactor. # ren1 = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Generate the tensors. ptLoad = vtk.vtkPointLoad() ptLoad.SetLoadValue(100.0) ptLoad.SetSampleDimensions(20, 20, 20) ptLoad.ComputeEffectiveStressOn() ptLoad.SetModelBounds(-10, 10, -10, 10, -10, 10) ptLoad.Update() # Generate the hyperstreamlines. s1 = vtk.vtkHyperStreamline() s1.SetInputData(ptLoad.GetOutput()) s1.SetStartPosition(9, 9, -9) s1.IntegrateMinorEigenvector() s1.SetMaximumPropagationDistance(18.0) s1.SetIntegrationStepLength(0.1) s1.SetStepLength(0.01) s1.SetRadius(0.25) s1.SetNumberOfSides(18) s1.SetIntegrationDirectionToIntegrateBothDirections() s1.Update() # Map the hyperstreamlines. lut = vtk.vtkLogLookupTable() lut.SetHueRange(.6667, 0.0) s1Mapper = vtk.vtkPolyDataMapper() s1Mapper.SetInputConnection(s1.GetOutputPort()) s1Mapper.SetLookupTable(lut) s1Mapper.SetScalarRange(ptLoad.GetOutput().GetScalarRange()) s1Actor = vtk.vtkActor() s1Actor.SetMapper(s1Mapper) s2 = vtk.vtkHyperStreamline() s2.SetInputData(ptLoad.GetOutput()) s2.SetStartPosition(-9, -9, -9) s2.IntegrateMinorEigenvector() s2.SetMaximumPropagationDistance(18.0) s2.SetIntegrationStepLength(0.1) s2.SetStepLength(0.01) s2.SetRadius(0.25) s2.SetNumberOfSides(18) s2.SetIntegrationDirectionToIntegrateBothDirections() s2.Update() s2Mapper = vtk.vtkPolyDataMapper() s2Mapper.SetInputConnection(s2.GetOutputPort()) s2Mapper.SetLookupTable(lut) s2Mapper.SetScalarRange(ptLoad.GetOutput().GetScalarRange()) s2Actor = vtk.vtkActor() s2Actor.SetMapper(s2Mapper) s3 = vtk.vtkHyperStreamline() s3.SetInputData(ptLoad.GetOutput()) s3.SetStartPosition(9, -9, -9) s3.IntegrateMinorEigenvector() s3.SetMaximumPropagationDistance(18.0) s3.SetIntegrationStepLength(0.1) s3.SetStepLength(0.01) s3.SetRadius(0.25) s3.SetNumberOfSides(18) s3.SetIntegrationDirectionToIntegrateBothDirections() s3.Update() s3Mapper = vtk.vtkPolyDataMapper() s3Mapper.SetInputConnection(s3.GetOutputPort()) s3Mapper.SetLookupTable(lut) s3Mapper.SetScalarRange(ptLoad.GetOutput().GetScalarRange()) s3Actor = vtk.vtkActor() s3Actor.SetMapper(s3Mapper) s4 = vtk.vtkHyperStreamline() s4.SetInputData(ptLoad.GetOutput()) s4.SetStartPosition(-9, 9, -9) s4.IntegrateMinorEigenvector() s4.SetMaximumPropagationDistance(18.0) s4.SetIntegrationStepLength(0.1) s4.SetStepLength(0.01) s4.SetRadius(0.25) s4.SetNumberOfSides(18) s4.SetIntegrationDirectionToIntegrateBothDirections() s4.Update() s4Mapper = vtk.vtkPolyDataMapper() s4Mapper.SetInputConnection(s4.GetOutputPort()) s4Mapper.SetLookupTable(lut) s4Mapper.SetScalarRange(ptLoad.GetOutput().GetScalarRange()) s4Actor = vtk.vtkActor() s4Actor.SetMapper(s4Mapper) # A plane for context. # g = vtk.vtkImageDataGeometryFilter() g.SetInputData(ptLoad.GetOutput()) g.SetExtent(0, 100, 0, 100, 0, 0) g.Update() # for scalar range gm = vtk.vtkPolyDataMapper() gm.SetInputConnection(g.GetOutputPort()) gm.SetScalarRange(g.GetOutput().GetScalarRange()) ga = vtk.vtkActor() ga.SetMapper(gm) # Create an outline around the data. # outline = vtk.vtkOutlineFilter() outline.SetInputData(ptLoad.GetOutput()) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort()) outlineActor = vtk.vtkActor() outlineActor.SetMapper(outlineMapper) outlineActor.GetProperty().SetColor(colors.GetColor3d("Black")) # Create a cone indicating the application of the load. # coneSrc = vtk.vtkConeSource() coneSrc.SetRadius(.5) coneSrc.SetHeight(2) coneMap = vtk.vtkPolyDataMapper() coneMap.SetInputConnection(coneSrc.GetOutputPort()) coneActor = vtk.vtkActor() coneActor.SetMapper(coneMap) coneActor.SetPosition(0, 0, 11) coneActor.RotateY(90) coneActor.GetProperty().SetColor(colors.GetColor3d("Tomato")) camera = vtk.vtkCamera() camera.SetFocalPoint(0.113766, -1.13665, -1.01919) camera.SetPosition(-29.4886, -63.1488, 26.5807) camera.SetViewAngle(24.4617) camera.SetViewUp(0.17138, 0.331163, 0.927879) camera.SetClippingRange(1, 100) ren1.AddActor(s1Actor) ren1.AddActor(s2Actor) ren1.AddActor(s3Actor) ren1.AddActor(s4Actor) ren1.AddActor(outlineActor) ren1.AddActor(coneActor) ren1.AddActor(ga) ren1.SetBackground(colors.GetColor3d("SlateGray")) ren1.SetActiveCamera(camera) renWin.SetSize(640, 480) renWin.Render() iren.Start()