source: mystic/examples_other/CubeSection.py @ 855

Revision 855, 6.6 KB checked in by mmckerns, 5 months ago (diff)

updated copyright to 2016

Line 
1#!/usr/bin/env python
2#
3# Author: Patrick Hung (patrickh @caltech)
4# Copyright (c) 1997-2016 California Institute of Technology.
5# License: 3-clause BSD.  The full license text is available at:
6#  - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE
7#
8# This example is a derivative of vtk's ClipCow
9# It is a visualization of Prince Rupert's problem
10
11import vtk, Tkinter
12from vtk.tk.vtkTkRenderWindowInteractor import \
13     vtkTkRenderWindowInteractor
14from numpy import array, cross
15from vtk.util.colors import peacock, tomato
16
17#cube = vtk.vtkBYUReader()
18#cube.SetGeometryFileName("./cube.g")
19cube = vtk.vtkCubeSource()
20cube.SetCenter(0.5,0.5,0.5)
21
22cubeNormals = vtk.vtkPolyDataNormals()
23cubeNormals.SetInputConnection(cube.GetOutputPort())
24cube = cubeNormals
25
26def PlaneNormal(a, b, c):
27    assert(a >= 0 and a <= 1)
28    assert(b >= 0 and b <= 1)
29    assert(c >= 0 and c <= 2)
30    A = array([a, 0.0, 0.0])
31    B = array([0.0, b, 0.0])
32    if c < 1:
33        C = array([c, 0.0, 1.0])
34    else:
35        C = array([1.0, c-1.0, 1.0])
36    BA = B-A
37    CA = C-A
38    return cross(BA, CA)
39
40def CuttingPlane(a, b, c):
41    N = PlaneNormal(a,b,c)
42    plane = vtk.vtkPlane()
43    plane.SetOrigin(a, 0, 0)
44    plane.SetNormal(*N)
45    return plane
46
47def getAxes(Origin, scale = 1):
48    axes = vtk.vtkAxes()
49    axes.SetOrigin(*Origin)
50    axes.SetScaleFactor(scale)
51
52    axesTubes = vtk.vtkTubeFilter()
53
54    axesTubes.SetInputConnection(axes.GetOutputPort())
55    axesTubes.SetRadius(0.01)
56    axesTubes.SetNumberOfSides(6)
57
58    axesMapper = vtk.vtkPolyDataMapper()
59    axesMapper.SetInputConnection(axesTubes.GetOutputPort())
60
61    axesActor = vtk.vtkActor()
62    axesActor.SetMapper(axesMapper)
63
64    XText = vtk.vtkVectorText()
65    XText.SetText("x")
66
67    XTextMapper = vtk.vtkPolyDataMapper()
68    XTextMapper.SetInputConnection(XText.GetOutputPort())
69
70    XActor = vtk.vtkFollower()
71    XActor.SetMapper(XTextMapper)
72    XActor.SetScale(.1, .1, .1)
73    XActor.SetPosition(1, Origin[1], Origin[2])
74    XActor.GetProperty().SetColor(0, 0, 0)
75
76    YText = vtk.vtkVectorText()
77    YText.SetText("y")
78
79    YTextMapper = vtk.vtkPolyDataMapper()
80    YTextMapper.SetInputConnection(YText.GetOutputPort())
81
82    YActor = vtk.vtkFollower()
83    YActor.SetMapper(YTextMapper)
84    YActor.SetScale(.1, .1, .1)
85    YActor.SetPosition(Origin[0], 1, Origin[2])
86    YActor.GetProperty().SetColor(0, 0, 0)
87    return axesActor, XActor, YActor
88
89axesActor, XActor, YActor = getAxes([-.1, -.1, -.1])
90
91
92P = (0.75, 0.75, 1.25)
93
94# vtkClipPolyData requires an implicit function to define what it is to
95# clip with. Any implicit function, including complex boolean combinations
96# can be used. Notice that we can specify the value of the implicit function
97# with the SetValue method.
98clipper = vtk.vtkClipPolyData()
99clipper.SetInputConnection(cubeNormals.GetOutputPort())
100clipper.SetClipFunction(CuttingPlane(*P))
101
102clipper.GenerateClipScalarsOn()
103clipper.GenerateClippedOutputOn()
104clipper.SetValue(0)
105clipMapper = vtk.vtkPolyDataMapper()
106clipMapper.SetInputConnection(clipper.GetOutputPort())
107clipMapper.ScalarVisibilityOff()
108backProp = vtk.vtkProperty()
109backProp.SetDiffuseColor(tomato)
110clipActor = vtk.vtkActor()
111clipActor.SetMapper(clipMapper)
112clipActor.GetProperty().SetColor(peacock)
113clipActor.SetBackfaceProperty(backProp)
114
115# Here we are cutting the cube. Cutting creates lines where the cut
116# function intersects the model. (Clipping removes a portion of the
117# model but the dimension of the data does not change.)
118#
119# The reason we are cutting is to generate a closed polygon at the
120# boundary of the clipping process. The cutter generates line
121# segments, the stripper then puts them together into polylines. We
122# then pull a trick and define polygons using the closed line
123# segements that the stripper created.
124cutEdges = vtk.vtkCutter()
125cutEdges.SetInputConnection(cubeNormals.GetOutputPort())
126cutEdges.SetCutFunction(CuttingPlane(*P))
127cutEdges.GenerateCutScalarsOn()
128cutEdges.SetValue(0, 0)
129cutStrips = vtk.vtkStripper()
130cutStrips.SetInputConnection(cutEdges.GetOutputPort())
131cutStrips.Update()
132cutPoly = vtk.vtkPolyData()
133cutPoly.SetPoints(cutStrips.GetOutput().GetPoints())
134cutPoly.SetPolys(cutStrips.GetOutput().GetLines())
135
136# Triangle filter is robust enough to ignore the duplicate point at
137# the beginning and end of the polygons and triangulate them.
138cutTriangles = vtk.vtkTriangleFilter()
139cutTriangles.SetInput(cutPoly)
140cutMapper = vtk.vtkPolyDataMapper()
141cutMapper.SetInput(cutPoly)
142cutMapper.SetInputConnection(cutTriangles.GetOutputPort())
143cutActor = vtk.vtkActor()
144cutActor.SetMapper(cutMapper)
145cutActor.GetProperty().SetColor(peacock)
146
147# The clipped part of the cube is rendered wireframe.
148restMapper = vtk.vtkPolyDataMapper()
149restMapper.SetInput(clipper.GetClippedOutput())
150restMapper.ScalarVisibilityOff()
151restActor = vtk.vtkActor()
152restActor.SetMapper(restMapper)
153restActor.GetProperty().SetRepresentationToWireframe()
154
155# Create graphics stuff
156renWin = vtk.vtkRenderWindow()
157ren = vtk.vtkRenderer()
158ren.SetBackground(tomato)
159renWin.AddRenderer(ren)
160
161# Add the actors to the renderer, set the background and size
162ren.AddActor(clipActor)
163ren.AddActor(cutActor)
164ren.AddActor(restActor)
165ren.AddActor(axesActor)
166ren.AddActor(XActor)
167ren.AddActor(YActor)
168ren.SetBackground(1, 1, 1)
169ren.ResetCamera()
170camera = ren.GetActiveCamera()
171camera.SetFocalPoint(0.489125, 0.481143, 0.445)
172camera.SetPosition(-0.870854, -1.51779, 3.14336)
173camera.SetParallelScale(1.00818)
174camera.SetParallelProjection(1)
175camera.SetViewUp(-0.239476, 0.833984, 0.497114)
176ren.ResetCameraClippingRange()
177
178renWin.SetSize(400, 400)
179
180def Cut(v):
181    Q = (P[0], P[1], v)
182    cp = CuttingPlane(*Q)
183    pn = PlaneNormal(*Q)
184    clipper.SetClipFunction(cp)
185    clipper.SetValue(0)
186    cutEdges.SetCutFunction(cp)
187    cutEdges.SetValue(0, 0)
188    cutStrips.Update()
189    cutPoly.SetPoints(cutStrips.GetOutput().GetPoints())
190    cutPoly.SetPolys(cutStrips.GetOutput().GetLines())
191    cutMapper.Update()
192    renWin.Render()
193 
194root = Tkinter.Tk()
195vtkw = vtkTkRenderWindowInteractor(root, rw=renWin, width=800)
196
197def set_cut(sz):
198    sz = float(sz)
199    # print ren.GetActiveCamera()
200    Cut(sz)
201
202# propagate this GUI setting to the corresponding VTK object.
203size_slider = Tkinter.Scale(root, from_=0.0,
204                            to=2.0, res=0.01,
205                            orient='horizontal', label="Clipping Center",
206                            command=set_cut)
207
208size_slider.set(P[2])
209vtkw.Initialize()
210size_slider.pack(side="top", fill="both")
211vtkw.pack(side="top", fill='both', expand=1)
212
213
214# Define a quit method that exits cleanly.
215def quit(obj=root):
216    obj.quit()
217
218root.protocol("WM_DELETE_WINDOW", quit)
219
220renWin.Render()
221vtkw.Start()
222
223# start the Tkinter event loop.
224root.mainloop()
225
226# end of file
Note: See TracBrowser for help on using the repository browser.