-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtubes.py
95 lines (73 loc) · 2.64 KB
/
tubes.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import os.path
import math
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.ren.SetBackground(0.2, 0.3, 0.4)
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
# Create Spiral tube
nV = 256 #No. of vertices
nCyc = 5 #No. of spiral cycles
rS = 2.0 #Spiral radius
h = 10.0
nTv = 8 #No. of surface elements for each tube vertex
points = vtk.vtkPoints()
for i in range(nV):
vX = rS * math.cos(2 * math.pi * nCyc * i / (nV-1))
vY = rS * math.sin(2 * math.pi * nCyc * i / (nV-1))
vZ = h * i / nV
points.InsertPoint(i, vX, vY, vZ)
lines = vtk.vtkCellArray()
lines.InsertNextCell(nV)
for i in range(nV):
lines.InsertCellPoint(i)
polyData = vtk.vtkPolyData()
polyData.SetPoints(points)
polyData.SetLines(lines)
tube = vtk.vtkTubeFilter()
tube.SetInput(polyData)
tube.SetNumberOfSides(nTv)
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(tube.GetOutputPort())
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self._initialized = False
def showEvent(self, evt):
if not self._initialized:
self.iren.Initialize()
#self.startTimer(30)
self._initialized = True
def timerEvent(self, evt):
self.ren.GetActiveCamera().Azimuth(1)
self.vtkWidget.GetRenderWindow().Render()
class MainPage(QtGui.QMainWindow):
def __init__(self, parent = None):
super(MainPage, self).__init__(parent)
self.setCentralWidget(VTKFrame())
self.setWindowTitle("Tube example")
def categories(self):
return ['Simple', 'Tube', 'Filters']
def mainClasses(self):
return ['vtkPoints', 'vtkCellArray', 'vtkTubeFilter', 'vtkPolyData']
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = MainPage()
w.show()
sys.exit(app.exec_())