1#!/usr/bin/env python
2import vtk
3from vtk.util.misc import vtkGetDataRoot
4VTK_DATA_ROOT = vtkGetDataRoot()
5
6# Create the RenderWindow, Renderer
7#
8ren = vtk.vtkRenderer()
9renWin = vtk.vtkRenderWindow()
10renWin.AddRenderer( ren )
11renWin.SetSize(600,200)
12
13iren = vtk.vtkRenderWindowInteractor()
14iren.SetRenderWindow(renWin)
15
16# Create a cube and cut it with a plane
17#
18boxL = vtk.vtkCubeSource()
19boxL.SetBounds(-2.5,-1.5, -0.5,0.5, -0.5,0.5)
20
21boxC = vtk.vtkCubeSource()
22boxC.SetBounds(-0.5,0.5, -0.5,0.5, -0.5,0.5)
23
24boxR = vtk.vtkCubeSource()
25boxR.SetBounds(1.5,2.5, -0.5,0.5, -0.5,0.5)
26
27mapperL = vtk.vtkPolyDataMapper()
28mapperL.SetInputConnection(boxL.GetOutputPort())
29
30mapperC = vtk.vtkPolyDataMapper()
31mapperC.SetInputConnection(boxC.GetOutputPort())
32
33mapperR = vtk.vtkPolyDataMapper()
34mapperR.SetInputConnection(boxR.GetOutputPort())
35
36actorL = vtk.vtkActor()
37actorL.SetMapper(mapperL)
38actorL.GetProperty().SetRepresentationToWireframe()
39actorL.GetProperty().SetAmbient(1)
40
41actorC = vtk.vtkActor()
42actorC.SetMapper(mapperC)
43actorC.GetProperty().SetRepresentationToWireframe()
44actorC.GetProperty().SetAmbient(1)
45
46actorR = vtk.vtkActor()
47actorR.SetMapper(mapperR)
48actorR.GetProperty().SetRepresentationToWireframe()
49actorR.GetProperty().SetAmbient(1)
50
51ren.AddActor(actorL)
52ren.AddActor(actorC)
53ren.AddActor(actorR)
54
55# Now clip boxes
56origin = [0,0,0]
57xout = [0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0]
58bds = [0,0, 0,0, 0,0]
59clipBox = vtk.vtkBox()
60
61# Left
62normal = [1,1,1]
63origin = boxL.GetCenter()
64pdL = vtk.vtkPolyData()
65polyL = vtk.vtkCellArray()
66ptsL = vtk.vtkPoints()
67pdL.SetPoints(ptsL)
68pdL.SetPolys(polyL)
69ptsL.SetDataTypeToDouble()
70boxL.GetBounds(bds)
71numInts = clipBox.IntersectWithPlane(bds, origin, normal, xout)
72print("Num ints: ", numInts)
73ptsL.SetNumberOfPoints(numInts)
74polyL.InsertNextCell(numInts)
75for i in range(0,numInts):
76    ptsL.SetPoint(i,xout[3*i],xout[3*i+1],xout[3*i+2])
77    polyL.InsertCellPoint(i)
78mapperPL = vtk.vtkPolyDataMapper()
79mapperPL.SetInputData(pdL)
80actorPL = vtk.vtkActor()
81actorPL.SetMapper(mapperPL)
82ren.AddActor(actorPL)
83
84# Center
85normal = [.4,.8,.4]
86origin = boxC.GetCenter()
87pdC = vtk.vtkPolyData()
88polyC = vtk.vtkCellArray()
89ptsC = vtk.vtkPoints()
90pdC.SetPoints(ptsC)
91pdC.SetPolys(polyC)
92ptsC.SetDataTypeToDouble()
93boxC.GetBounds(bds)
94numInts = clipBox.IntersectWithPlane(bds, origin, normal, xout)
95print("Num ints: ", numInts)
96ptsC.SetNumberOfPoints(numInts)
97polyC.InsertNextCell(numInts)
98for i in range(0,numInts):
99    ptsC.SetPoint(i,xout[3*i],xout[3*i+1],xout[3*i+2])
100    polyC.InsertCellPoint(i)
101mapperPC = vtk.vtkPolyDataMapper()
102mapperPC.SetInputData(pdC)
103actorPC = vtk.vtkActor()
104actorPC.SetMapper(mapperPC)
105ren.AddActor(actorPC)
106
107# Right
108normal = [0,0,1]
109origin = boxR.GetCenter()
110pdR = vtk.vtkPolyData()
111polyR = vtk.vtkCellArray()
112ptsR = vtk.vtkPoints()
113pdR.SetPoints(ptsR)
114pdR.SetPolys(polyR)
115ptsR.SetDataTypeToDouble()
116boxR.GetBounds(bds)
117numInts = clipBox.IntersectWithPlane(bds, origin, normal, xout)
118print("Num ints: ", numInts)
119ptsR.SetNumberOfPoints(numInts)
120polyR.InsertNextCell(numInts)
121for i in range(0,numInts):
122    ptsR.SetPoint(i,xout[3*i],xout[3*i+1],xout[3*i+2])
123    polyR.InsertCellPoint(i)
124mapperPR = vtk.vtkPolyDataMapper()
125mapperPR.SetInputData(pdR)
126actorPR = vtk.vtkActor()
127actorPR.SetMapper(mapperPR)
128ren.AddActor(actorPR)
129
130ren.GetActiveCamera().SetFocalPoint(0,0,0)
131ren.GetActiveCamera().SetPosition(0,0.5,1)
132ren.ResetCamera()
133ren.GetActiveCamera().Zoom(2.5)
134
135renWin.Render()
136iren.Start()
137