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# define a Single Cube 16Scalars = vtk.vtkFloatArray() 17Scalars.InsertNextValue(1.0) 18Scalars.InsertNextValue(1.0) 19Scalars.InsertNextValue(0.0) 20Scalars.InsertNextValue(0.0) 21Scalars.InsertNextValue(0.0) 22Scalars.InsertNextValue(0.0) 23Scalars.InsertNextValue(0.0) 24Scalars.InsertNextValue(0.0) 25 26Points = vtk.vtkPoints() 27Points.InsertNextPoint(0, 0, 0) 28Points.InsertNextPoint(1, 0, 0) 29Points.InsertNextPoint(1, 1, 0) 30Points.InsertNextPoint(0, 1, 0) 31Points.InsertNextPoint(0, 0, 1) 32Points.InsertNextPoint(1, 0, 1) 33Points.InsertNextPoint(1, 1, 1) 34Points.InsertNextPoint(0, 1, 1) 35 36Ids = vtk.vtkIdList() 37Ids.InsertNextId(0) 38Ids.InsertNextId(1) 39Ids.InsertNextId(2) 40Ids.InsertNextId(3) 41Ids.InsertNextId(4) 42Ids.InsertNextId(5) 43Ids.InsertNextId(6) 44Ids.InsertNextId(7) 45 46Grid = vtk.vtkUnstructuredGrid() 47Grid.Allocate(10, 10) 48Grid.InsertNextCell(12, Ids) 49Grid.SetPoints(Points) 50Grid.GetPointData().SetScalars(Scalars) 51 52# Clip the hex 53clipper = vtk.vtkClipDataSet() 54clipper.SetInputData(Grid) 55clipper.SetValue(0.5) 56 57# build tubes for the triangle edges 58# 59tetEdges = vtk.vtkExtractEdges() 60tetEdges.SetInputConnection(clipper.GetOutputPort()) 61 62tetEdgeTubes = vtk.vtkTubeFilter() 63tetEdgeTubes.SetInputConnection(tetEdges.GetOutputPort()) 64tetEdgeTubes.SetRadius(.005) 65tetEdgeTubes.SetNumberOfSides(6) 66tetEdgeTubes.UseDefaultNormalOn() 67tetEdgeTubes.SetDefaultNormal(.577, .577, .577) 68 69tetEdgeMapper = vtk.vtkPolyDataMapper() 70tetEdgeMapper.SetInputConnection(tetEdgeTubes.GetOutputPort()) 71tetEdgeMapper.ScalarVisibilityOff() 72 73tetEdgeActor = vtk.vtkActor() 74tetEdgeActor.SetMapper(tetEdgeMapper) 75tetEdgeActor.GetProperty().SetDiffuseColor(GetRGBColor('lamp_black')) 76tetEdgeActor.GetProperty().SetSpecular(.4) 77tetEdgeActor.GetProperty().SetSpecularPower(10) 78 79# shrink the triangles so we can see each one 80aShrinker = vtk.vtkShrinkFilter() 81aShrinker.SetShrinkFactor(1) 82aShrinker.SetInputConnection(clipper.GetOutputPort()) 83 84aMapper = vtk.vtkDataSetMapper() 85aMapper.ScalarVisibilityOff() 86aMapper.SetInputConnection(aShrinker.GetOutputPort()) 87 88Tets = vtk.vtkActor() 89Tets.SetMapper(aMapper) 90Tets.GetProperty().SetDiffuseColor(GetRGBColor('banana')) 91 92# build a model of the cube 93CubeModel = vtk.vtkCubeSource() 94CubeModel.SetCenter(.5, .5, .5) 95 96Edges = vtk.vtkExtractEdges() 97Edges.SetInputConnection(CubeModel.GetOutputPort()) 98 99Tubes = vtk.vtkTubeFilter() 100Tubes.SetInputConnection(Edges.GetOutputPort()) 101Tubes.SetRadius(.01) 102Tubes.SetNumberOfSides(6) 103Tubes.UseDefaultNormalOn() 104Tubes.SetDefaultNormal(.577, .577, .577) 105 106TubeMapper = vtk.vtkPolyDataMapper() 107TubeMapper.SetInputConnection(Tubes.GetOutputPort()) 108 109CubeEdges = vtk.vtkActor() 110CubeEdges.SetMapper(TubeMapper) 111CubeEdges.GetProperty().SetDiffuseColor(GetRGBColor('khaki')) 112CubeEdges.GetProperty().SetSpecular(.4) 113CubeEdges.GetProperty().SetSpecularPower(10) 114 115# build the vertices of the cube 116# 117Sphere = vtk.vtkSphereSource() 118Sphere.SetRadius(0.04) 119Sphere.SetPhiResolution(20) 120Sphere.SetThetaResolution(20) 121 122ThresholdIn = vtk.vtkThresholdPoints() 123ThresholdIn.SetInputData(Grid) 124ThresholdIn.ThresholdByUpper(.5) 125 126Vertices = vtk.vtkGlyph3D() 127Vertices.SetInputConnection(ThresholdIn.GetOutputPort()) 128Vertices.SetSourceConnection(Sphere.GetOutputPort()) 129 130SphereMapper = vtk.vtkPolyDataMapper() 131SphereMapper.SetInputConnection(Vertices.GetOutputPort()) 132SphereMapper.ScalarVisibilityOff() 133 134CubeVertices = vtk.vtkActor() 135CubeVertices.SetMapper(SphereMapper) 136CubeVertices.GetProperty().SetDiffuseColor(GetRGBColor('tomato')) 137 138# define the text for the labels 139caseLabel = vtk.vtkVectorText() 140caseLabel.SetText("Case 2") 141 142aLabelTransform = vtk.vtkTransform() 143aLabelTransform.Identity() 144aLabelTransform.Translate(-.2, 0, 1.25) 145aLabelTransform.Scale(.05, .05, .05) 146 147labelTransform = vtk.vtkTransformPolyDataFilter() 148labelTransform.SetTransform(aLabelTransform) 149labelTransform.SetInputConnection(caseLabel.GetOutputPort()) 150 151labelMapper = vtk.vtkPolyDataMapper() 152labelMapper.SetInputConnection(labelTransform.GetOutputPort()) 153 154labelActor = vtk.vtkActor() 155labelActor.SetMapper(labelMapper) 156 157# define the base 158baseModel = vtk.vtkCubeSource() 159baseModel.SetXLength(1.5) 160baseModel.SetYLength(.01) 161baseModel.SetZLength(1.5) 162 163baseMapper = vtk.vtkPolyDataMapper() 164baseMapper.SetInputConnection(baseModel.GetOutputPort()) 165 166base = vtk.vtkActor() 167base.SetMapper(baseMapper) 168 169# Create the RenderWindow, Renderer and both Actors 170# 171ren1 = vtk.vtkRenderer() 172renWin = vtk.vtkRenderWindow() 173renWin.AddRenderer(ren1) 174iren = vtk.vtkRenderWindowInteractor() 175iren.SetRenderWindow(renWin) 176 177# position the base 178base.SetPosition(.5, -.09, .5) 179ren1.AddActor(tetEdgeActor) 180ren1.AddActor(base) 181ren1.AddActor(labelActor) 182ren1.AddActor(CubeEdges) 183ren1.AddActor(CubeVertices) 184ren1.AddActor(Tets) 185ren1.SetBackground(GetRGBColor('slate_grey')) 186 187def case2(scalars, IN, OUT, caseLabel): 188 scalars.InsertValue(0, IN) 189 scalars.InsertValue(1, IN) 190 scalars.InsertValue(2, OUT) 191 scalars.InsertValue(3, OUT) 192 scalars.InsertValue(4, OUT) 193 scalars.InsertValue(5, OUT) 194 scalars.InsertValue(6, OUT) 195 scalars.InsertValue(7, OUT) 196 if IN == 1: 197 caseLabel.SetText("Case 2 - 00000011") 198 else: 199 caseLabel.SetText("Case 2c - 11111100") 200 201case2(Scalars, 1, 0, caseLabel) 202renWin.SetSize(400, 400) 203 204ren1.ResetCamera() 205ren1.GetActiveCamera().Dolly(1.2) 206ren1.GetActiveCamera().Azimuth(30) 207ren1.GetActiveCamera().Elevation(20) 208ren1.ResetCameraClippingRange() 209 210renWin.Render() 211 212iren.Initialize() 213#iren.Start() 214