/
implicitdatasetclipping.py
108 lines (85 loc) · 3.48 KB
/
implicitdatasetclipping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import os.path
from PyQt4 import QtCore, QtGui
QtCore.Signal = QtCore.pyqtSignal
import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class VTKFrame(QtGui.QFrame):
def __init__(self, parent = None):
super(VTKFrame, self).__init__(parent)
self.vtkWidget = QVTKRenderWindowInteractor(self)
vl = QtGui.QVBoxLayout(self)
vl.addWidget(self.vtkWidget)
vl.setContentsMargins(0, 0, 0, 0)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
resolution = 10
sphere = vtk.vtkSphereSource()
sphere.SetCenter(0.75, 0, 0)
sphere.SetThetaResolution(resolution)
sphere.SetPhiResolution(resolution)
sphere.Update()
# Add ids to the points and cells of the sphere
cellIdFilter = vtk.vtkIdFilter()
cellIdFilter.SetInputConnection(sphere.GetOutputPort())
cellIdFilter.SetCellIds(True)
cellIdFilter.SetPointIds(False)
cellIdFilter.SetIdsArrayName("CellIds")
cellIdFilter.Update()
pointIdFilter = vtk.vtkIdFilter()
pointIdFilter.SetInputConnection(cellIdFilter.GetOutputPort())
pointIdFilter.SetCellIds(False)
pointIdFilter.SetPointIds(True)
pointIdFilter.SetIdsArrayName("PointIds")
pointIdFilter.Update()
sphereWithIds = pointIdFilter.GetOutput()
cube = vtk.vtkCubeSource()
cube.Update()
implicitCube = vtk.vtkBox()
implicitCube.SetBounds(cube.GetOutput().GetBounds())
clipper = vtk.vtkClipPolyData()
clipper.SetClipFunction(implicitCube)
clipper.SetInput(sphereWithIds)
clipper.InsideOutOn()
clipper.Update()
# Create a mapper and actor for clipped sphere
clippedMapper = vtk.vtkPolyDataMapper()
clippedMapper.SetInputConnection(clipper.GetOutputPort())
clippedMapper.ScalarVisibilityOff()
clippedActor = vtk.vtkActor()
clippedActor.SetMapper(clippedMapper)
clippedActor.GetProperty().SetRepresentationToWireframe()
# Create a mapper and actor for the cube
cubeMapper = vtk.vtkPolyDataMapper()
cubeMapper.SetInputConnection(cube.GetOutputPort())
cubeActor = vtk.vtkActor()
cubeActor.SetMapper(cubeMapper)
cubeActor.GetProperty().SetRepresentationToWireframe()
cubeActor.GetProperty().SetOpacity(0.5)
#create renderers and add actors of plane and cube
self.ren.AddActor(clippedActor)
self.ren.AddActor(cubeActor)
self.ren.SetBackground(0.2, 0.3, 0.4)
self.ren.ResetCamera()
self._initialized = False
def showEvent(self, evt):
if not self._initialized:
self.iren.Initialize()
self._initialized = True
class MainPage(QtGui.QMainWindow):
def __init__(self, parent = None):
super(MainPage, self).__init__(parent)
self.setCentralWidget(VTKFrame())
self.setWindowTitle("Implicit dataset clipping example")
def categories(self):
return ['Clipper', 'Implict Data Set', 'Filters']
def mainClasses(self):
return ['vtkSphereSource', 'vtkIdFilter', 'vtkBox', 'vtkClipPolyData']
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = MainPage()
w.show()
sys.exit(app.exec_())