Newer
Older
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# !/usr/bin/env python3
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import (
VTK_UNSIGNED_CHAR,
vtkDoubleArray,
vtkLookupTable,
vtkPoints,
vtkUnsignedCharArray
)
from vtkmodules.vtkCommonDataModel import (
vtkImageData,
vtkPlanes,
vtkRectilinearGrid
)
from vtkmodules.vtkFiltersGeneral import vtkTableBasedClipDataSet
from vtkmodules.vtkImagingCore import vtkImageMapToColors
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderer,
vtkRenderWindow,
vtkRenderWindowInteractor,
)
def main():
# The number of checkerboard squares on a side.
image_size = 64
# Offsets for clipping planes with normals in the X and Y directions.
x_offset = 8
y_offset = 8
colors = vtkNamedColors()
renderer = vtkRenderer(background=colors.GetColor3d('Wheat'), use_hidden_line_removal=True)
render_window = vtkRenderWindow(size=(640, 480), window_name='TableBasedClipDataSetWithPolyData2')
render_window.AddRenderer(renderer)
interactor = vtkRenderWindowInteractor()
# Since we import vtkmodules.vtkInteractionStyle we can do this
# because vtkInteractorStyleSwitch is automatically imported:
interactor.interactor_style.SetCurrentStyleToTrackballCamera()
interactor.render_window = render_window
image = make_image(image_size)
# Clipping planes in the X and Y direction.
normals = vtkDoubleArray()
clip_pts = vtkPoints()
normals.SetNumberOfComponents(3)
xnorm = [-1.0, 0.0, 0.0]
ynorm = [0.0, -1.0, 0.0]
xpt = [x_offset, 0.0, 0.0]
ypt = [0.0, y_offset, 0.0]
normals.InsertNextTuple(xnorm)
normals.InsertNextTuple(ynorm)
clip_pts.InsertNextPoint(xpt)
clip_pts.InsertNextPoint(ypt)
clip_planes = vtkPlanes(normals=normals, points=clip_pts)
clipper = vtkTableBasedClipDataSet(clip_function=clip_planes, input_data=image)
image_mapper = vtkDataSetMapper()
clipper >> image_mapper
image_actor = vtkActor(mapper=image_mapper)
renderer.AddViewProp(image_actor)
renderer.ResetCamera()
renderer.active_camera.Azimuth(120)
renderer.active_camera.Elevation(30)
renderer.ResetCameraClippingRange()
render_window.Render()
interactor.Start()
# Make the image data. A checkerboard pattern is used for simplicity.
def make_image(n):
cube_size = 20.0 # physical linear dimension of entire system.
# This is a simplification of a program that uses actual image data
# as a source for the rectilinear grid. In order to recreate the
# same vtk calls, create a dummy image here.
image0 = vtkImageData()
image0.SetDimensions(n, n, n)
image0.AllocateScalars(VTK_UNSIGNED_CHAR, 1)
image0.SetSpacing(cube_size / n, cube_size / n, cube_size / n)
checker_size = n // 8
scalars = vtkUnsignedCharArray()
for z in range(0, n):
for y in range(0, n):
for x in range(0, n):
v = (x // checker_size + y // checker_size + z // checker_size) % 2
scalars.InsertNextValue(v)
image0.GetPointData().SetScalars(scalars)
colors = vtkNamedColors()
lut = vtkLookupTable(number_of_table_values=2, table_range=(0, 1))
lut.Build()
lut.SetTableValue(0, colors.GetColor4d('Thistle'))
lut.SetTableValue(1, colors.GetColor4d('DarkSlateBlue'))
map_colors = vtkImageMapToColors()
map_colors.SetLookupTable(lut)
map_colors.SetOutputFormatToRGBA()
map_colors.SetInputData(image0)
map_colors.update()
image = map_colors.output
extent = list(image.GetExtent())
for i in range(1, len(extent), 2):
extent[i] += 1
rect_grid = vtkRectilinearGrid(extent=extent)
xcoords = vtkDoubleArray()
ycoords = vtkDoubleArray()
zcoords = vtkDoubleArray()
xcoords.SetNumberOfValues(n + 1)
ycoords.SetNumberOfValues(n + 1)
zcoords.SetNumberOfValues(n + 1)
spacing = image.GetSpacing()
for i in range(0, n + 1):
xcoords.InsertValue(i, i * spacing[0])
ycoords.InsertValue(i, i * spacing[1])
zcoords.InsertValue(i, i * spacing[2])
rect_grid.SetXCoordinates(xcoords)
rect_grid.SetYCoordinates(ycoords)
rect_grid.SetZCoordinates(zcoords)
point_data = image.GetPointData()
cell_data = rect_grid.GetCellData()
cell_data.ShallowCopy(point_data)
return rect_grid
if __name__ == '__main__':
main()