1#!/usr/bin/env python 2import vtk 3from vtk.test import Testing 4from vtk.util.misc import vtkGetDataRoot 5VTK_DATA_ROOT = vtkGetDataRoot() 6 7# this example tests the warping of PolyData using thin plate splines 8# and with grid transforms using different interpolation modes 9# create a rendering window 10renWin = vtk.vtkRenderWindow() 11renWin.SetSize(600,300) 12sphere = vtk.vtkSphereSource() 13sphere.SetThetaResolution(20) 14sphere.SetPhiResolution(20) 15ap = vtk.vtkPolyDataNormals() 16ap.SetInputConnection(sphere.GetOutputPort()) 17#--------------------------- 18# thin plate spline transform 19spoints = vtk.vtkPoints() 20spoints.SetNumberOfPoints(10) 21spoints.SetPoint(0,0.000,0.000,0.500) 22spoints.SetPoint(1,0.000,0.000,-0.500) 23spoints.SetPoint(2,0.433,0.000,0.250) 24spoints.SetPoint(3,0.433,0.000,-0.250) 25spoints.SetPoint(4,-0.000,0.433,0.250) 26spoints.SetPoint(5,-0.000,0.433,-0.250) 27spoints.SetPoint(6,-0.433,-0.000,0.250) 28spoints.SetPoint(7,-0.433,-0.000,-0.250) 29spoints.SetPoint(8,0.000,-0.433,0.250) 30spoints.SetPoint(9,0.000,-0.433,-0.250) 31tpoints = vtk.vtkPoints() 32tpoints.SetNumberOfPoints(10) 33tpoints.SetPoint(0,0.000,0.000,0.800) 34tpoints.SetPoint(1,0.000,0.000,-0.200) 35tpoints.SetPoint(2,0.433,0.000,0.350) 36tpoints.SetPoint(3,0.433,0.000,-0.150) 37tpoints.SetPoint(4,-0.000,0.233,0.350) 38tpoints.SetPoint(5,-0.000,0.433,-0.150) 39tpoints.SetPoint(6,-0.433,-0.000,0.350) 40tpoints.SetPoint(7,-0.433,-0.000,-0.150) 41tpoints.SetPoint(8,0.000,-0.233,0.350) 42tpoints.SetPoint(9,0.000,-0.433,-0.150) 43thin = vtk.vtkThinPlateSplineTransform() 44thin.SetSourceLandmarks(spoints) 45thin.SetTargetLandmarks(tpoints) 46thin.SetBasisToR2LogR() 47# thin Inverse 48t1 = vtk.vtkGeneralTransform() 49t1.SetInput(thin) 50f11 = vtk.vtkTransformPolyDataFilter() 51f11.SetInputConnection(ap.GetOutputPort()) 52f11.SetTransform(t1) 53m11 = vtk.vtkDataSetMapper() 54m11.SetInputConnection(f11.GetOutputPort()) 55a11 = vtk.vtkActor() 56a11.SetMapper(m11) 57a11.RotateY(90) 58a11.GetProperty().SetColor(1,0,0) 59#[a11 GetProperty] SetRepresentationToWireframe 60ren11 = vtk.vtkRenderer() 61ren11.SetViewport(0.0,0.5,0.25,1.0) 62ren11.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 63ren11.AddActor(a11) 64renWin.AddRenderer(ren11) 65# inverse thin plate spline transform 66f12 = vtk.vtkTransformPolyDataFilter() 67f12.SetInputConnection(ap.GetOutputPort()) 68f12.SetTransform(t1.GetInverse()) 69m12 = vtk.vtkDataSetMapper() 70m12.SetInputConnection(f12.GetOutputPort()) 71a12 = vtk.vtkActor() 72a12.SetMapper(m12) 73a12.RotateY(90) 74a12.GetProperty().SetColor(0.9,0.9,0) 75#[a12 GetProperty] SetRepresentationToWireframe 76ren12 = vtk.vtkRenderer() 77ren12.SetViewport(0.0,0.0,0.25,0.5) 78ren12.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 79ren12.AddActor(a12) 80renWin.AddRenderer(ren12) 81#-------------------------- 82# grid transform, cubic interpolation 83gridTrans = vtk.vtkTransformToGrid() 84gridTrans.SetInput(t1) 85gridTrans.SetGridOrigin(-1.5,-1.5,-1.5) 86gridTrans.SetGridExtent(0,60,0,60,0,60) 87gridTrans.SetGridSpacing(0.05,0.05,0.05) 88t2 = vtk.vtkGridTransform() 89t2.SetDisplacementGridConnection(gridTrans.GetOutputPort()) 90t2.SetInterpolationModeToCubic() 91f21 = vtk.vtkTransformPolyDataFilter() 92f21.SetInputConnection(ap.GetOutputPort()) 93f21.SetTransform(t2) 94m21 = vtk.vtkDataSetMapper() 95m21.SetInputConnection(f21.GetOutputPort()) 96a21 = vtk.vtkActor() 97a21.SetMapper(m21) 98a21.RotateY(90) 99a21.GetProperty().SetColor(1,0,0) 100#[a21 GetProperty] SetRepresentationToWireframe 101ren21 = vtk.vtkRenderer() 102ren21.SetViewport(0.25,0.5,0.50,1.0) 103ren21.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 104ren21.AddActor(a21) 105renWin.AddRenderer(ren21) 106# inverse 107f22 = vtk.vtkTransformPolyDataFilter() 108f22.SetInputConnection(ap.GetOutputPort()) 109f22.SetTransform(t2.GetInverse()) 110m22 = vtk.vtkDataSetMapper() 111m22.SetInputConnection(f22.GetOutputPort()) 112a22 = vtk.vtkActor() 113a22.SetMapper(m22) 114a22.RotateY(90) 115a22.GetProperty().SetColor(0.9,0.9,0) 116#[a22 GetProperty] SetRepresentationToWireframe 117ren22 = vtk.vtkRenderer() 118ren22.SetViewport(0.25,0.0,0.50,0.5) 119ren22.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 120ren22.AddActor(a22) 121renWin.AddRenderer(ren22) 122#-------------------------- 123# grid transform, linear 124t3 = vtk.vtkGridTransform() 125t3.SetDisplacementGridConnection(gridTrans.GetOutputPort()) 126t3.SetInterpolationModeToLinear() 127f31 = vtk.vtkTransformPolyDataFilter() 128f31.SetInputConnection(ap.GetOutputPort()) 129f31.SetTransform(t3) 130m31 = vtk.vtkDataSetMapper() 131m31.SetInputConnection(f31.GetOutputPort()) 132a31 = vtk.vtkActor() 133a31.SetMapper(m31) 134a31.RotateY(90) 135a31.GetProperty().SetColor(1,0,0) 136#[a31 GetProperty] SetRepresentationToWireframe 137ren31 = vtk.vtkRenderer() 138ren31.SetViewport(0.50,0.5,0.75,1.0) 139ren31.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 140ren31.AddActor(a31) 141renWin.AddRenderer(ren31) 142# inverse 143f32 = vtk.vtkTransformPolyDataFilter() 144f32.SetInputConnection(ap.GetOutputPort()) 145f32.SetTransform(t3.GetInverse()) 146m32 = vtk.vtkDataSetMapper() 147m32.SetInputConnection(f32.GetOutputPort()) 148a32 = vtk.vtkActor() 149a32.SetMapper(m32) 150a32.RotateY(90) 151a32.GetProperty().SetColor(0.9,0.9,0) 152#[a32 GetProperty] SetRepresentationToWireframe 153ren32 = vtk.vtkRenderer() 154ren32.SetViewport(0.5,0.0,0.75,0.5) 155ren32.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 156ren32.AddActor(a32) 157renWin.AddRenderer(ren32) 158#-------------------------- 159# grid transform, nearest 160t4 = vtk.vtkGridTransform() 161t4.SetDisplacementGridConnection(gridTrans.GetOutputPort()) 162t4.SetInterpolationModeToNearestNeighbor() 163t4.SetInverseTolerance(0.05) 164f41 = vtk.vtkTransformPolyDataFilter() 165f41.SetInputConnection(ap.GetOutputPort()) 166f41.SetTransform(t4) 167m41 = vtk.vtkDataSetMapper() 168m41.SetInputConnection(f41.GetOutputPort()) 169a41 = vtk.vtkActor() 170a41.SetMapper(m41) 171a41.RotateY(90) 172a41.GetProperty().SetColor(1,0,0) 173#[a41 GetProperty] SetRepresentationToWireframe 174ren41 = vtk.vtkRenderer() 175ren41.SetViewport(0.75,0.5,1.0,1.0) 176ren41.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 177ren41.AddActor(a41) 178renWin.AddRenderer(ren41) 179#inverse 180f42 = vtk.vtkTransformPolyDataFilter() 181f42.SetInputConnection(ap.GetOutputPort()) 182f42.SetTransform(t4.GetInverse()) 183m42 = vtk.vtkDataSetMapper() 184m42.SetInputConnection(f42.GetOutputPort()) 185a42 = vtk.vtkActor() 186a42.SetMapper(m42) 187a42.RotateY(90) 188a42.GetProperty().SetColor(0.9,0.9,0) 189#[a42 GetProperty] SetRepresentationToWireframe 190ren42 = vtk.vtkRenderer() 191ren42.SetViewport(0.75,0.0,1.0,0.5) 192ren42.ResetCamera(-0.5,0.5,-0.5,0.5,-1,1) 193ren42.AddActor(a42) 194renWin.AddRenderer(ren42) 195t1.RotateX(-100) 196t1.PostMultiply() 197t1.RotateX(+100) 198renWin.Render() 199# --- end of script -- 200