-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicod_demo.py
376 lines (329 loc) · 12.3 KB
/
icod_demo.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""Created on 2 Oct 2021
Interactive Collision Detection (ICoD) Demo.
@author: Alex Grimwood
run from the command line:
python icod_demo.py
See requirements.txt for dependencies
Ask the author for the necessary .stl files
Based upon previous work by kpiqu
"""
import vtkmodules.all as vtk
from vedo import *
import argparse
import os
import numpy as np
def get_program_parameters():
''' Set input parameters from command line '''
description = 'Collision detection demo.'
parser = argparse.ArgumentParser(description=description,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--gantry', default='Gantry.stl', type=str, help='gantry stl file name')
parser.add_argument('--collimator', default='Collimator.stl', type=str, help='collimator stl file name')
parser.add_argument('--couch', default='TableTop.stl', type=str, help='couch-top stl file name')
parser.add_argument('--body', default='Body.stl', type=str, help='patient stl file name')
parser.add_argument('--machine_dir', default='.\stl\VarianTrueBeamSTx', type=str, help='machine stl directory')
parser.add_argument('--patient_dir', default='.\stl\Patient', type=str, help='patient stl directory')
args = parser.parse_args()
return args
def collisionfilter(mesh1, mesh2, clean1=True, clean2=True):
''' Create a vtk collision detection filter between two mesh objects '''
# extract polydata from mesh objects
if clean1 is True:
poly1 = mesh1.clean().polydata()
else:
poly1 = mesh1.polydata()
if clean2 is True:
poly2 = mesh2.clean().polydata()
else:
poly2 = mesh2.polydata()
# object transformation inputs
tform1 = vtk.vtkTransform()
tform2 = vtk.vtkTransform()
# collision detector
collide = vtk.vtkCollisionDetectionFilter()
collide.SetInputData(0, poly1)
collide.SetTransform(0, tform1)
collide.SetInputData(1, poly2)
collide.SetTransform(1, tform2)
collide.SetBoxTolerance(0.0)
collide.SetCellTolerance(0.0)
collide.SetNumberOfCellsPerNode(2)
collide.SetCollisionModeToAllContacts()
collide.GenerateScalarsOn()
# collision mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(collide.GetContactsOutputPort())
mapper.SetResolveCoincidentTopologyToPolygonOffset()
# collision actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor((1,0,0))
actor.GetProperty().SetOpacity(0.85)
actor.GetProperty().SetLineWidth(20.0)
# poly1 mapper
mapper1 = vtk.vtkPolyDataMapper()
mapper1.SetInputConnection(collide.GetOutputPort(0))
mapper1.ScalarVisibilityOff()
# poly1 actor
actor1 = vtk.vtkActor()
actor1.SetMapper(mapper1)
actor1.GetProperty().BackfaceCullingOn()
actor1.SetUserTransform(tform1)
actor1.GetProperty().SetColor(mesh1.GetProperty().GetDiffuseColor())
actor1.GetProperty().SetOpacity(0.85)
# poly2 mapper
mapper2 = vtk.vtkPolyDataMapper()
mapper2.SetInputConnection(collide.GetOutputPort(1))
mapper2.ScalarVisibilityOff()
# poly2 actor
actor2 = vtk.vtkActor()
actor2.SetMapper(mapper2)
actor2.GetProperty().BackfaceCullingOn()
actor2.SetUserTransform(tform2)
actor2.GetProperty().SetColor(mesh2.GetProperty().GetDiffuseColor())
actor2.GetProperty().SetOpacity(0.85)
return collide, actor, actor1, actor2, tform1, tform2
def main():
# Load CLI args
args = get_program_parameters()
# Initialise display window
plt = Plotter(title="Collision Demo")
# Define colors
colors = vtk.vtkNamedColors()
# Specify stl directories
machine_dir = os.path.normpath(args.machine_dir)
patient_dir = os.path.normpath(args.patient_dir)
# Specify machine stl files
fileGantry = os.path.join(machine_dir,args.gantry)
fileCollimator = os.path.join(machine_dir,args.collimator)
fileCouch = os.path.join(machine_dir,args.couch)
# Specify patient stl file
filePatient = os.path.join(patient_dir,args.body)
# Specify model colours
gantry_colour = (0.678431, 0.847059, 0.901961)
collimator_colour = (0, 0, 0.545098)
couchtop_colour = (0, 0, 0.545098)
body_colour = (0.564706, 0.933333, 0.564706)
# Load machine stl
gantry = Mesh(fileGantry).c(gantry_colour)
collimator = Mesh(fileCollimator).c(collimator_colour)
couchtop = Mesh(fileCouch).c(couchtop_colour)
gantry.name = "gantry"
collimator.name = "collimator"
couchtop.name = "couchtop"
# Load patient stl
body = Mesh(filePatient).c(body_colour)
body.name = "body"
# gantry opacity
gantry.GetProperty().SetOpacity(0.2)
# Adjust machine to realistic orientation
initial_machine_rot = 90
initial_couch_offset = [0., -325, -500]
gantry.rotateX(initial_machine_rot)
collimator.rotateX(initial_machine_rot)
couchtop.rotateX(initial_machine_rot)
couchtop.SetPosition(initial_couch_offset)
# Create collision filters
collide0, actor0, actor0_snout, actor0_body, tform0_snout, tform0_body = collisionfilter(
mesh1=collimator, mesh2=body)
collide1, actor1, actor1_snout, actor1_couch, tform1_2, tform1_1 = collisionfilter(
mesh1=collimator, mesh2=couchtop)
# Model colour selection function
def reset_colours(actors, colours):
""" Change actor colours """
for actor, colour in zip(actors, colours):
actor.GetProperty().SetColor(colour)
actor.GetProperty().SetOpacity(0.85)
# Collision details reporting function
keys = ["Gantry", "Snout", "BodyX", "BodyY", "BodyZ", "CouchY"]
global geometry
geometry = {key: 0 for key in keys}
def collision_vis(collisionfilter, actors):
''' Identify collision objects and their geometries '''
if collisionfilter.GetNumberOfContacts() > 0:
print("Collision: "+str(geometry))
for actor in actors:
actor.GetProperty().SetOpacity(0.5)
# Initialise gantry and snout values
global g_theta, s_ext
g_theta = 0
s_ext = 0
# Slider callback functions
def slider_x(widget, event):
""" Moves patient LEFT-RIGHT """
value = widget.GetRepresentation().GetValue()
# reset mesh colours
reset_colours([actor0_snout, actor1_snout, actor0_body, actor1_couch],
[collimator_colour, collimator_colour, body_colour, couchtop_colour])
# move patient
body.x(value) # set patient x position
tform0_body.SetMatrix(body.GetMatrix())
# record new geometry
global geometry
geometry["BodyX"]=round(value,1)
# detect collisions
collision_vis(collide0,[actor0_body,actor0_snout,actor1_snout])
def slider_y(widget, event):
""" Moves patient ANT-POST """
value = widget.GetRepresentation().GetValue()
# reset mesh colours
reset_colours([actor0_snout, actor1_snout, actor0_body, actor1_couch],
[collimator_colour, collimator_colour, body_colour, couchtop_colour])
# move patient
body.y(value) # set patient y position
tform0_body.SetMatrix(body.GetMatrix())
# record new geometry
global geometry
geometry["BodyY"]=round(value,1)
# detect collisions
collision_vis(collide0,[actor0_body,actor0_snout,actor1_snout])
def slider_z(widget, event):
""" Moves patient SUP-INF """
value = widget.GetRepresentation().GetValue()
# reset mesh colours
reset_colours([actor0_snout, actor1_snout, actor0_body, actor1_couch],
[collimator_colour, collimator_colour, body_colour, couchtop_colour])
# move patient
body.z(value) # set patient z position
tform0_body.SetMatrix(body.GetMatrix())
# record new geometry
global geometry
geometry["BodyZ"]=round(value,1)
# detect collisions
collision_vis(collide0,[actor0_body,actor0_snout,actor1_snout])
def slider_c(widget, event):
""" Moves couch ANT-POST """
value = widget.GetRepresentation().GetValue()
# reset mesh colours
reset_colours([actor0_snout, actor1_snout, actor0_body, actor1_couch],
[collimator_colour, collimator_colour, body_colour, couchtop_colour])
# move couch
T = vtk.vtkTransform()
T.Translate(0., value, 0.)
couchtop.SetUserMatrix(T.GetMatrix())
tform1_1.SetMatrix(couchtop.GetUserMatrix())
# record new geometry
global geometry
geometry["CouchY"]=round(value,1)
# detect collisions
collision_vis(collide1,[actor1_couch,actor0_snout,actor1_snout])
def slider_g(widget, event):
""" Rotates gantry and collimator """
value = widget.GetRepresentation().GetValue()
# reset mesh colours
reset_colours([actor0_snout, actor1_snout, actor0_body, actor1_couch],
[collimator_colour, collimator_colour, body_colour, couchtop_colour])
# rotate gantry
global g_theta
g_theta = value
T = vtk.vtkTransform()
T.RotateZ(value)
gantry.SetUserMatrix(T.GetMatrix())
# transform colimator
global s_ext
T = vtk.vtkTransform()
T.RotateZ(g_theta)
T.Translate(0., s_ext, 0.)
collimator.SetUserMatrix(T.GetMatrix())
tform0_snout.SetMatrix(collimator.GetUserMatrix())
tform1_2.SetMatrix(collimator.GetUserMatrix())
# record new geometry
global geometry
geometry["Gantry"]=round(value,1)
# detect collisions
collision_vis(collide0,[actor0_body,actor0_snout, actor1_snout])
collision_vis(collide1,[actor1_couch,actor0_snout, actor1_snout])
def slider_s(widget, event):
""" Extends collimator """
value = widget.GetRepresentation().GetValue()
# reset mesh colours
reset_colours([actor0_snout, actor1_snout, actor0_body, actor1_couch],
[collimator_colour, collimator_colour, body_colour, couchtop_colour])
# transform colimator
global g_theta, s_ext
s_ext = value
T = vtk.vtkTransform()
T.RotateZ(g_theta)
T.Translate(0., s_ext, 0.)
collimator.SetUserMatrix(T.GetMatrix())
tform0_snout.SetMatrix(collimator.GetUserMatrix())
tform1_2.SetMatrix(collimator.GetUserMatrix())
# record new geometry
global geometry
geometry["Snout"]=round(value,1)
# detect collisions
collision_vis(collide0,[actor0_body,actor0_snout, actor1_snout])
collision_vis(collide1,[actor1_couch,actor0_snout, actor1_snout])
plt.addSlider3D(
slider_x,
pos1=[-450., 1250., -2050.],
pos2=[-450.+1000, 1250., -2050.],
xmin=-500,
xmax=500,
value=0,
s=0.08,
c="r",
rotation=180,
title="x patient (mm)",
)
plt.addSlider3D(
slider_y,
pos1=[600., -800., -2050.],
pos2=[600., -800.+2000, -2050.],
xmin=-500,
xmax=500,
value=0,
s=0.08,
c="g",
rotation=90,
title="y patient (mm)",
)
plt.addSlider3D(
slider_z,
pos1=[600., 1250., -2000.],
pos2=[600., 1250., -2000+2000.],
xmin=-500,
xmax=500,
value=0,
s=0.08,
c="b",
rotation=180,
title="z patient (mm)",
)
plt.addSlider2D(
slider_g,
xmin=-180,
xmax=180,
value=0,
pos=4,
title='gantry angle (deg)',
titleSize=1,
c='m',
showValue=True,
)
plt.addSlider2D(
slider_s,
xmin=0,
xmax=450,
value=0,
pos=3,
title='snout extension (mm)',
titleSize=1,
c='c',
showValue=True,
)
plt.addSlider2D(
slider_c,
xmin=-450,
xmax=450,
value=0,
pos=([0.05, 0.25],[0.05, 0.75]),
title='couch height (mm)',
titleSize=1,
c='y',
showValue=True,
)
# Visualise
plt.show([gantry, actor0, actor1, actor0_body, actor0_snout, actor1_couch, actor1_snout], axes=1, camera={"pos": (0.,0.,-12000.), "viewup": [0,-1,0], "focalPoint": (0,0,0)}, bg='black', bg2='dark grey').close()
if __name__ == '__main__':
main()