-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtk_actor_translate.py
134 lines (107 loc) · 4.67 KB
/
vtk_actor_translate.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from vtk import vtkActor
from vtk import vtkPolyDataMapper
from vtk import vtkConeSource, vtkCubeSource, vtkParametricEllipsoid, vtkParametricFunctionSource, vtkParametricTorus
class VTKSpecificActor:
def __init__(self, geometry: str, dimensions, **kwargs):
"""
geometry: string of what to draw
available options: "cone", "rectprism", "ellipsoid", "torus"
dimensions: dimensions of the bounding box. ideally a tuple with three
arguments
"""
self.geometry = geometry
self.kwargs = kwargs
if len(dimensions) == 3:
self.dim = dimensions
elif len(dimensions) == 2: # x and y dimensions are same/square base box
self.dim[0] = dimensions[0]
self.dim[1] = dimensions[0]
self.dim[2] = dimensions[1]
elif len(dimensions) == 1:
self.dim = [dimensions for _ in range(3)] # creates a cube bounding box
else:
raise ValueError("Bounding box has " + str(len(dimensions)) + " dimensions?")
# all of the generate functions technically do run at every class instance
# but the non-matching geometries generate empty actors
_command_dict = {
"cone": self._generate_cone(),
"rectprism": self._generate_rectprism(),
"ellipsoid": self._generate_ellipsoid(),
"torus": self._generate_torus()
}
self.actor = _command_dict[geometry]
def _other_vtk_settings(self):
self._mapper = vtkPolyDataMapper()
def _generate_cone(self):
if self.geometry != "cone":
return vtkActor()
else:
_actor = vtkActor()
self._other_vtk_settings()
_source = vtkConeSource()
_source.SetHeight(self.dim[2])
_source.SetRadius(self.dim[0] / 2)
if 'resolution' in self.kwargs:
_source.SetResolution(self.kwargs['resolution'])
elif 'center' in self.kwargs:
_source.SetCenter(self.kwargs['center'])
elif 'direction' in self.kwargs:
_source.SetDirection(self.kwargs['direction'])
self._mapper.SetInputConnection(_source.GetOutputPort())
_actor.SetMapper(self._mapper)
return _actor
def _generate_rectprism(self):
if self.geometry != "rectprism":
return vtkActor()
else:
_actor = vtkActor()
self._other_vtk_settings()
_source = vtkCubeSource()
_source.SetXLength(self.dim[0])
_source.SetYLength(self.dim[1])
_source.SetZLength(self.dim[2])
_source.Update()
if 'center' in self.kwargs:
_source.SetCenter(self.kwargs['center'])
self._mapper.SetInputConnection(_source.GetOutputPort())
_actor.SetMapper(self._mapper)
return _actor
def _generate_ellipsoid(self):
if self.geometry != "ellipsoid":
return vtkActor()
else:
_actor = vtkActor()
self._other_vtk_settings()
_param_ellipsoid = vtkParametricEllipsoid()
_param_ellipsoid.SetXRadius(self.dim[0]/2)
_param_ellipsoid.SetYRadius(self.dim[1]/2)
_param_ellipsoid.SetZRadius(self.dim[2]/2)
_source = vtkParametricFunctionSource()
_source.SetParametricFunction(_param_ellipsoid)
if 'center' in self.kwargs:
_source.SetCenter(self.kwargs['center'])
elif 'resolution' in self.kwargs:
_source.SetUResolution(self.kwargs['resolution'])
_source.SetVResolution(self.kwargs['resolution'])
self._mapper.SetInputConnection(_source.GetOutputPort())
_actor.SetMapper(self._mapper)
return _actor
def _generate_torus(self):
if self.geometry != "torus":
return vtkActor()
else:
_actor = vtkActor()
self._other_vtk_settings()
_param_torus = vtkParametricTorus()
_param_torus.SetRingRadius(self.dim[0] / 2)
_param_torus.SetCrossSectionRadius(self.dim[2] / 2)
_source = vtkParametricFunctionSource()
_source.SetParametricFunction(_param_torus)
if 'center' in self.kwargs:
_source.SetCenter(self.kwargs['resolution'])
elif 'resolution' in self.kwargs:
_source.SetUResolution(self.kwargs['resolution'])
_source.SetVResolution(self.kwargs['resolution'])
self._mapper.SetInputConnection(_source.GetOutputPort())
_actor.SetMapper(self._mapper)
return _actor