1#!/usr/bin/env python 2import vtk 3from vtk.test import Testing 4from vtk.util.misc import vtkGetDataRoot 5VTK_DATA_ROOT = vtkGetDataRoot() 6 7# On older Macs, 10 is too low. Due to what looks like a driver bug 8# spectral lighting behaves sort of weird and produces small differences 9threshold = 30 10points = vtk.vtkPoints() 11points.InsertNextPoint(2,4,0) 12points.InsertNextPoint(2.6,2.6,0) 13points.InsertNextPoint(4,2,0) 14points.InsertNextPoint(1.4,4,1.4) 15points.InsertNextPoint(2,3,1) 16points.InsertNextPoint(3,2,1) 17points.InsertNextPoint(4,1.4,1.4) 18points.InsertNextPoint(0,4,2) 19points.InsertNextPoint(1,3,2) 20points.InsertNextPoint(2,2,2) 21points.InsertNextPoint(3,1,2) 22points.InsertNextPoint(4,0,2) 23points.InsertNextPoint(0,2.6,2.6) 24points.InsertNextPoint(1,2,3) 25points.InsertNextPoint(2,1,3) 26points.InsertNextPoint(2.6,0,2.6) 27points.InsertNextPoint(0,2,4) 28points.InsertNextPoint(1.4,1.4,4) 29points.InsertNextPoint(2,0,4) 30faces = vtk.vtkCellArray() 31faces.InsertNextCell(3) 32faces.InsertCellPoint(0) 33faces.InsertCellPoint(3) 34faces.InsertCellPoint(4) 35faces.InsertNextCell(3) 36faces.InsertCellPoint(0) 37faces.InsertCellPoint(4) 38faces.InsertCellPoint(1) 39faces.InsertNextCell(3) 40faces.InsertCellPoint(1) 41faces.InsertCellPoint(4) 42faces.InsertCellPoint(5) 43faces.InsertNextCell(3) 44faces.InsertCellPoint(1) 45faces.InsertCellPoint(5) 46faces.InsertCellPoint(2) 47faces.InsertNextCell(3) 48faces.InsertCellPoint(2) 49faces.InsertCellPoint(5) 50faces.InsertCellPoint(6) 51faces.InsertNextCell(3) 52faces.InsertCellPoint(3) 53faces.InsertCellPoint(7) 54faces.InsertCellPoint(8) 55faces.InsertNextCell(3) 56faces.InsertCellPoint(3) 57faces.InsertCellPoint(8) 58faces.InsertCellPoint(4) 59faces.InsertNextCell(3) 60faces.InsertCellPoint(4) 61faces.InsertCellPoint(8) 62faces.InsertCellPoint(9) 63faces.InsertNextCell(3) 64faces.InsertCellPoint(4) 65faces.InsertCellPoint(9) 66faces.InsertCellPoint(5) 67faces.InsertNextCell(3) 68faces.InsertCellPoint(5) 69faces.InsertCellPoint(9) 70faces.InsertCellPoint(10) 71faces.InsertNextCell(3) 72faces.InsertCellPoint(5) 73faces.InsertCellPoint(10) 74faces.InsertCellPoint(6) 75faces.InsertNextCell(3) 76faces.InsertCellPoint(6) 77faces.InsertCellPoint(10) 78faces.InsertCellPoint(11) 79faces.InsertNextCell(3) 80faces.InsertCellPoint(7) 81faces.InsertCellPoint(12) 82faces.InsertCellPoint(8) 83faces.InsertNextCell(3) 84faces.InsertCellPoint(8) 85faces.InsertCellPoint(12) 86faces.InsertCellPoint(13) 87faces.InsertNextCell(3) 88faces.InsertCellPoint(8) 89faces.InsertCellPoint(13) 90faces.InsertCellPoint(9) 91faces.InsertNextCell(3) 92faces.InsertCellPoint(9) 93faces.InsertCellPoint(13) 94faces.InsertCellPoint(14) 95faces.InsertNextCell(3) 96faces.InsertCellPoint(9) 97faces.InsertCellPoint(14) 98faces.InsertCellPoint(10) 99faces.InsertNextCell(3) 100faces.InsertCellPoint(10) 101faces.InsertCellPoint(14) 102faces.InsertCellPoint(15) 103faces.InsertNextCell(3) 104faces.InsertCellPoint(10) 105faces.InsertCellPoint(15) 106faces.InsertCellPoint(11) 107faces.InsertNextCell(3) 108faces.InsertCellPoint(12) 109faces.InsertCellPoint(16) 110faces.InsertCellPoint(13) 111faces.InsertNextCell(3) 112faces.InsertCellPoint(13) 113faces.InsertCellPoint(16) 114faces.InsertCellPoint(17) 115faces.InsertNextCell(3) 116faces.InsertCellPoint(13) 117faces.InsertCellPoint(17) 118faces.InsertCellPoint(14) 119faces.InsertNextCell(3) 120faces.InsertCellPoint(14) 121faces.InsertCellPoint(17) 122faces.InsertCellPoint(18) 123faces.InsertNextCell(3) 124faces.InsertCellPoint(14) 125faces.InsertCellPoint(18) 126faces.InsertCellPoint(15) 127model = vtk.vtkPolyData() 128model.SetPolys(faces) 129model.SetPoints(points) 130rn = vtk.vtkMath() 131cellColors = vtk.vtkUnsignedCharArray() 132cellColors.SetNumberOfComponents(3) 133cellColors.SetNumberOfTuples(model.GetNumberOfCells()) 134i = 0 135while i < model.GetNumberOfCells(): 136 cellColors.InsertComponent(i,0,rn.Random(100,255)) 137 cellColors.InsertComponent(i,1,rn.Random(100,255)) 138 cellColors.InsertComponent(i,2,rn.Random(100,255)) 139 i = i + 1 140 141model.GetCellData().SetScalars(cellColors) 142t0 = vtk.vtkTransform() 143t0.Identity() 144tf0 = vtk.vtkTransformPolyDataFilter() 145tf0.SetTransform(t0) 146tf0.SetInputData(model) 147t1 = vtk.vtkTransform() 148t1.Identity() 149t1.RotateZ(90) 150tf1 = vtk.vtkTransformPolyDataFilter() 151tf1.SetTransform(t1) 152tf1.SetInputData(model) 153t2 = vtk.vtkTransform() 154t2.Identity() 155t2.RotateZ(180) 156tf2 = vtk.vtkTransformPolyDataFilter() 157tf2.SetTransform(t2) 158tf2.SetInputData(model) 159t3 = vtk.vtkTransform() 160t3.Identity() 161t3.RotateZ(270) 162tf3 = vtk.vtkTransformPolyDataFilter() 163tf3.SetTransform(t3) 164tf3.SetInputData(model) 165af = vtk.vtkAppendPolyData() 166af.AddInputConnection(tf0.GetOutputPort()) 167af.AddInputConnection(tf1.GetOutputPort()) 168af.AddInputConnection(tf2.GetOutputPort()) 169af.AddInputConnection(tf3.GetOutputPort()) 170t4 = vtk.vtkTransform() 171t4.Identity() 172t4.RotateX(180) 173tf4 = vtk.vtkTransformPolyDataFilter() 174tf4.SetTransform(t4) 175tf4.SetInputConnection(af.GetOutputPort()) 176af2 = vtk.vtkAppendPolyData() 177af2.AddInputConnection(af.GetOutputPort()) 178af2.AddInputConnection(tf4.GetOutputPort()) 179t5 = vtk.vtkTransform() 180t5.Identity() 181t5.Translate(0,0,-8) 182tf5 = vtk.vtkTransformPolyDataFilter() 183tf5.SetTransform(t5) 184tf5.SetInputConnection(af2.GetOutputPort()) 185af3 = vtk.vtkAppendPolyData() 186af3.AddInputConnection(af2.GetOutputPort()) 187af3.AddInputConnection(tf5.GetOutputPort()) 188t6 = vtk.vtkTransform() 189t6.Identity() 190t6.Translate(0,-8,0) 191tf6 = vtk.vtkTransformPolyDataFilter() 192tf6.SetTransform(t6) 193tf6.SetInputConnection(af3.GetOutputPort()) 194af4 = vtk.vtkAppendPolyData() 195af4.AddInputConnection(af3.GetOutputPort()) 196af4.AddInputConnection(tf6.GetOutputPort()) 197# Create the RenderWindow, Renderer and both Actors 198# 199ren1 = vtk.vtkRenderer() 200renWin = vtk.vtkRenderWindow() 201renWin.AddRenderer(ren1) 202iren = vtk.vtkRenderWindowInteractor() 203iren.SetRenderWindow(renWin) 204clean = vtk.vtkCleanPolyData() 205clean.SetTolerance(.001) 206clean.SetInputData(model) 207clean.SetInputConnection(af2.GetOutputPort()) 208clean.SetInputConnection(af3.GetOutputPort()) 209clean.SetInputConnection(af4.GetOutputPort()) 210subdivide = vtk.vtkButterflySubdivisionFilter() 211subdivide.SetInputConnection(clean.GetOutputPort()) 212subdivide.SetNumberOfSubdivisions(3) 213mapper = vtk.vtkDataSetMapper() 214mapper.SetInputConnection(subdivide.GetOutputPort()) 215surface = vtk.vtkActor() 216surface.SetMapper(mapper) 217fe = vtk.vtkFeatureEdges() 218fe.SetInputConnection(subdivide.GetOutputPort()) 219fe.SetFeatureAngle(100) 220feStripper = vtk.vtkStripper() 221feStripper.SetInputConnection(fe.GetOutputPort()) 222feTubes = vtk.vtkTubeFilter() 223feTubes.SetInputConnection(feStripper.GetOutputPort()) 224feTubes.SetRadius(.1) 225feMapper = vtk.vtkPolyDataMapper() 226feMapper.SetInputConnection(feTubes.GetOutputPort()) 227edges = vtk.vtkActor() 228edges.SetMapper(feMapper) 229# Add the actors to the renderer, set the background and size 230# 231ren1.AddActor(surface) 232ren1.AddActor(edges) 233backP = vtk.vtkProperty() 234backP.SetDiffuseColor(1,1,.3) 235surface.SetBackfaceProperty(backP) 236edges.GetProperty().SetDiffuseColor(.2,.2,.2) 237surface.GetProperty().SetDiffuseColor(1,.4,.3) 238surface.GetProperty().SetSpecular(.4) 239surface.GetProperty().SetDiffuse(.8) 240surface.GetProperty().SetSpecularPower(40) 241ren1.SetBackground(0.1,0.2,0.4) 242renWin.SetSize(300,300) 243# render the image 244# 245ren1.ResetCamera() 246cam1 = ren1.GetActiveCamera() 247cam1.Azimuth(90) 248ren1.ResetCamera() 249cam1.Zoom(1.5) 250iren.Initialize() 251# prevent the tk window from showing up then start the event loop 252# --- end of script -- 253