1#!/usr/bin/env python 2import vtk 3from vtk.util.misc import vtkGetDataRoot 4VTK_DATA_ROOT = vtkGetDataRoot() 5 6def GetRGBColor(colorName): 7 ''' 8 Return the red, green and blue components for a 9 color as doubles. 10 ''' 11 rgb = [0.0, 0.0, 0.0] # black 12 vtk.vtkNamedColors().GetColorRGB(colorName, rgb) 13 return rgb 14 15# Create the RenderWindow, Renderer and both Actors 16# 17ren1 = vtk.vtkRenderer() 18renWin = vtk.vtkRenderWindow() 19renWin.AddRenderer(ren1) 20iren = vtk.vtkRenderWindowInteractor() 21iren.SetRenderWindow(renWin) 22 23# create a semi-cylinder 24# 25line = vtk.vtkLineSource() 26line.SetPoint1(0, 1, 0) 27line.SetPoint2(0, 1, 2) 28line.SetResolution(10) 29 30lineSweeper = vtk.vtkRotationalExtrusionFilter() 31lineSweeper.SetResolution(20) 32lineSweeper.SetInputConnection(line.GetOutputPort()) 33lineSweeper.SetAngle(270) 34 35bump = vtk.vtkBrownianPoints() 36bump.SetInputConnection(lineSweeper.GetOutputPort()) 37 38warp = vtk.vtkWarpVector() 39warp.SetInputConnection(bump.GetOutputPort()) 40warp.SetScaleFactor(.2) 41 42smooth = vtk.vtkWindowedSincPolyDataFilter() 43smooth.SetInputConnection(warp.GetOutputPort()) 44smooth.SetNumberOfIterations(20) 45smooth.BoundarySmoothingOn() 46smooth.SetFeatureAngle(120) 47smooth.SetEdgeAngle(90) 48smooth.SetPassBand(0.1) 49 50normals = vtk.vtkPolyDataNormals() 51normals.SetInputConnection(smooth.GetOutputPort()) 52 53cylMapper = vtk.vtkPolyDataMapper() 54cylMapper.SetInputConnection(normals.GetOutputPort()) 55 56cylActor = vtk.vtkActor() 57cylActor.SetMapper(cylMapper) 58cylActor.GetProperty().SetInterpolationToGouraud() 59cylActor.GetProperty().SetInterpolationToFlat() 60cylActor.GetProperty().SetColor(GetRGBColor('beige')) 61 62originalMapper = vtk.vtkPolyDataMapper() 63originalMapper.SetInputConnection(bump.GetOutputPort()) 64originalActor = vtk.vtkActor() 65originalActor.SetMapper(originalMapper) 66originalActor.GetProperty().SetInterpolationToFlat() 67 68cylActor.GetProperty().SetColor(GetRGBColor('tomato')) 69 70# Add the actors to the renderer, set the background and size 71# 72ren1.AddActor(cylActor) 73# ren1 AddActor originalActor 74ren1.SetBackground(1, 1, 1) 75 76renWin.SetSize(200, 300) 77 78camera = vtk.vtkCamera() 79camera.SetClippingRange(0.576398, 28.8199) 80camera.SetFocalPoint(0.0463079, -0.0356571, 1.01993) 81camera.SetPosition(-2.47044, 2.39516, -3.56066) 82camera.SetViewUp(0.607296, -0.513537, -0.606195) 83 84ren1.SetActiveCamera(camera) 85 86# render the image 87# 88iren.Initialize() 89iren.Start() 90