1#!/usr/bin/env python 2import vtk 3from vtk.test import Testing 4from vtk.util.misc import vtkGetDataRoot 5VTK_DATA_ROOT = vtkGetDataRoot() 6 7# create two boxes and interpolate between them 8# 9pts = vtk.vtkPoints() 10pts.InsertNextPoint(-1,-1,-1) 11pts.InsertNextPoint(1,-1,-1) 12pts.InsertNextPoint(1,1,-1) 13pts.InsertNextPoint(-1,1,-1) 14pts.InsertNextPoint(-1,-1,1) 15pts.InsertNextPoint(1,-1,1) 16pts.InsertNextPoint(1,1,1) 17pts.InsertNextPoint(-1,1,1) 18faces = vtk.vtkCellArray() 19faces.InsertNextCell(4) 20faces.InsertCellPoint(0) 21faces.InsertCellPoint(3) 22faces.InsertCellPoint(2) 23faces.InsertCellPoint(1) 24faces.InsertNextCell(4) 25faces.InsertCellPoint(4) 26faces.InsertCellPoint(5) 27faces.InsertCellPoint(6) 28faces.InsertCellPoint(7) 29faces.InsertNextCell(4) 30faces.InsertCellPoint(0) 31faces.InsertCellPoint(1) 32faces.InsertCellPoint(5) 33faces.InsertCellPoint(4) 34faces.InsertNextCell(4) 35faces.InsertCellPoint(1) 36faces.InsertCellPoint(2) 37faces.InsertCellPoint(6) 38faces.InsertCellPoint(5) 39faces.InsertNextCell(4) 40faces.InsertCellPoint(2) 41faces.InsertCellPoint(3) 42faces.InsertCellPoint(7) 43faces.InsertCellPoint(6) 44faces.InsertNextCell(4) 45faces.InsertCellPoint(3) 46faces.InsertCellPoint(0) 47faces.InsertCellPoint(4) 48faces.InsertCellPoint(7) 49faceColors = vtk.vtkUnsignedCharArray() 50faceColors.SetNumberOfComponents(3) 51faceColors.SetNumberOfTuples(3) 52faceColors.InsertComponent(0,0,255) 53faceColors.InsertComponent(0,1,0) 54faceColors.InsertComponent(0,2,0) 55faceColors.InsertComponent(1,0,0) 56faceColors.InsertComponent(1,1,255) 57faceColors.InsertComponent(1,2,0) 58faceColors.InsertComponent(2,0,255) 59faceColors.InsertComponent(2,1,255) 60faceColors.InsertComponent(2,2,0) 61faceColors.InsertComponent(3,0,0) 62faceColors.InsertComponent(3,1,0) 63faceColors.InsertComponent(3,2,255) 64faceColors.InsertComponent(4,0,255) 65faceColors.InsertComponent(4,1,0) 66faceColors.InsertComponent(4,2,255) 67faceColors.InsertComponent(5,0,0) 68faceColors.InsertComponent(5,1,255) 69faceColors.InsertComponent(5,2,255) 70cube = vtk.vtkPolyData() 71cube.SetPoints(pts) 72cube.SetPolys(faces) 73cube.GetCellData().SetScalars(faceColors) 74t1 = vtk.vtkTransform() 75t1.Translate(1,2,3) 76t1.RotateX(15) 77t1.Scale(4,2,1) 78tpdf1 = vtk.vtkTransformPolyDataFilter() 79tpdf1.SetInputData(cube) 80tpdf1.SetTransform(t1) 81cube1Mapper = vtk.vtkPolyDataMapper() 82cube1Mapper.SetInputConnection(tpdf1.GetOutputPort()) 83cube1 = vtk.vtkActor() 84cube1.SetMapper(cube1Mapper) 85t2 = vtk.vtkTransform() 86t2.Translate(5,10,15) 87t2.RotateX(22.5) 88t2.RotateY(15) 89t2.RotateZ(85) 90t2.Scale(1,2,4) 91tpdf2 = vtk.vtkTransformPolyDataFilter() 92tpdf2.SetInputData(cube) 93tpdf2.SetTransform(t2) 94cube2Mapper = vtk.vtkPolyDataMapper() 95cube2Mapper.SetInputConnection(tpdf2.GetOutputPort()) 96cube2 = vtk.vtkActor() 97cube2.SetMapper(cube2Mapper) 98t3 = vtk.vtkTransform() 99t3.Translate(5,-10,15) 100t3.RotateX(13) 101t3.RotateY(72) 102t3.RotateZ(-15) 103t3.Scale(2,4,1) 104tpdf3 = vtk.vtkTransformPolyDataFilter() 105tpdf3.SetInputData(cube) 106tpdf3.SetTransform(t3) 107cube3Mapper = vtk.vtkPolyDataMapper() 108cube3Mapper.SetInputConnection(tpdf3.GetOutputPort()) 109cube3 = vtk.vtkActor() 110cube3.SetMapper(cube3Mapper) 111t4 = vtk.vtkTransform() 112t4.Translate(10,-5,5) 113t4.RotateX(66) 114t4.RotateY(19) 115t4.RotateZ(24) 116t4.Scale(2,.5,1) 117tpdf4 = vtk.vtkTransformPolyDataFilter() 118tpdf4.SetInputData(cube) 119tpdf4.SetTransform(t4) 120cube4Mapper = vtk.vtkPolyDataMapper() 121cube4Mapper.SetInputConnection(tpdf4.GetOutputPort()) 122cube4 = vtk.vtkActor() 123cube4.SetMapper(cube4Mapper) 124# Interpolate the transformation 125cubeMapper = vtk.vtkPolyDataMapper() 126cubeMapper.SetInputData(cube) 127cubeActor = vtk.vtkActor() 128cubeActor.SetMapper(cubeMapper) 129# Interpolate some transformations, test along the way 130interpolator = vtk.vtkTransformInterpolator() 131#interpolator SetInterpolationTypeToLinear 132interpolator.SetInterpolationTypeToSpline() 133interpolator.AddTransform(0.0,cube1) 134interpolator.AddTransform(8.0,cube2) 135interpolator.AddTransform(18.2,cube3) 136interpolator.AddTransform(24.4,cube4) 137interpolator.Initialize() 138#puts [interpolator GetNumberOfTransforms] 139interpolator.AddTransform(0.0,t1) 140interpolator.AddTransform(8.0,t2) 141interpolator.AddTransform(18.2,t3) 142interpolator.AddTransform(24.4,t4) 143#puts [interpolator GetNumberOfTransforms] 144# Create the RenderWindow, Renderer and both Actors 145# 146ren1 = vtk.vtkRenderer() 147renWin = vtk.vtkRenderWindow() 148renWin.AddRenderer(ren1) 149iren = vtk.vtkRenderWindowInteractor() 150iren.SetRenderWindow(renWin) 151# Add the actors to the renderer, set the background and size 152# 153ren1.AddActor(cube1) 154ren1.AddActor(cube2) 155ren1.AddActor(cube3) 156ren1.AddActor(cube4) 157ren1.AddActor(cubeActor) 158ren1.SetBackground(0,0,0) 159renWin.SetSize(300,300) 160ren1.SetBackground(0.1,0.2,0.4) 161# render the image 162# 163camera = vtk.vtkCamera() 164camera.SetClippingRange(31.2977,81.697) 165camera.SetFocalPoint(3.0991,-2.00445,9.78648) 166camera.SetPosition(-44.8481,-25.871,10.0645) 167camera.SetViewAngle(30) 168camera.SetViewUp(-0.0356378,0.0599728,-0.997564) 169ren1.SetActiveCamera(camera) 170renWin.Render() 171# prevent the tk window from showing up then start the event loop 172xform = vtk.vtkTransform() 173def animate(): 174 numSteps = 250 175 min = interpolator.GetMinimumT() 176 max = interpolator.GetMaximumT() 177 i = 0 178 while i <= numSteps: 179 t = float(i)*(max-min)/float(numSteps) 180 interpolator.InterpolateTransform(t,xform) 181 cubeActor.SetUserMatrix(xform.GetMatrix()) 182 renWin.Render() 183 i = i + 1 184 185 186interpolator.InterpolateTransform(13.2,xform) 187cubeActor.SetUserMatrix(xform.GetMatrix()) 188renWin.Render() 189#animate() 190# --- end of script -- 191