-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_distance_dental_3_files.py
688 lines (553 loc) · 21.8 KB
/
find_distance_dental_3_files.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# find_distance_dental_3_files.py
#
# This script performs ICP (iterative closest point) registration between two sets of STL
# files, the T1, T2 and the impression. T1 is reigstered to T2, and the impression is also
# registered to T2.
# The mean absolute distance is calculated and displayed between the two sets of meshes.
# A screenshot and the distance mesh in the vtk format are currently saved to disk for each
# registration.
#
# Notes for resources:
# https://github.com/Kitware/VTK/blob/master/Examples/GUI/Python/ImplicitPlaneWidget.py
# https://github.com/Kitware/VTK/blob/master/Examples/VisualizationAlgorithms/Python/ClipCow.py
# https://gitlab.kitware.com/bxa/vtk/blob/a7996f418f2e1375aa91cbd98ab952a7162b4a26/Examples/Annotation/Python/annotatePick.py
# http://vtk.1045678.n5.nabble.com/Picking-and-moving-a-vertex-td1233640.html
# https://vtk.org/Wiki/VTK/Examples/Python/Visualization/AssignColorsCellFromLUT
# https://github.com/Kitware/VTK/blob/master/Examples/GUI/Python/BoxWidget.py
# https://python.hotexamples.com/examples/vtk/-/vtkPointPicker/python-vtkpointpicker-function-examples.html
#
#
# Deepa Krishnaswamy
# Brigham and Women's Hospital
# September 2022
#########################################################################
# -*- coding: utf-8 -*-
import numpy as np
import vtk
import sys
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
print (str(vtk.VTK_MAJOR_VERSION) + '.' + str(vtk.VTK_MINOR_VERSION))
from PyQt5.QtWidgets import QApplication, QFileDialog
from vtk.util.colors import peacock, tomato
from vtk.util.colors import brown_ochre, tomato, banana, azure
import re
#########################################################################
def calculate_ICP(source, target):
icp = vtk.vtkIterativeClosestPointTransform()
icp.SetSource(source)
icp.SetTarget(target)
icp.GetLandmarkTransform().SetModeToRigidBody()
#icp.DebugOn()
# icp.SetMaximumNumberOfIterations(20)
icp.SetMaximumNumberOfIterations(150)
icp.StartByMatchingCentroidsOn()
icp.Modified()
icp.Update()
icpTransformFilter = vtk.vtkTransformPolyDataFilter()
if vtk.VTK_MAJOR_VERSION <= 5:
icpTransformFilter.SetInput(source)
else:
icpTransformFilter.SetInputData(source)
icpTransformFilter.SetTransform(icp)
icpTransformFilter.Update()
transformedSource = icpTransformFilter.GetOutput()
return transformedSource
def calculate_MAD(mesh_A, mesh_B):
'''Calculates the mean absolute distance (MAD) between two meshes'''
'''The distance is calculated between the cells'''
### calculate MAD metric ###
distance_filter = vtk.vtkDistancePolyDataFilter()
distance_filter.SetSignedDistance(0)
# added
distance_filter.ComputeSecondDistanceOff()
distance_filter.SetComputeSecondDistance(0)
# distance between ref and my mesh
if vtk.VTK_MAJOR_VERSION <= 5:
distance_filter.SetInput(0,mesh_A)
distance_filter.SetInput(1,mesh_B)
else:
distance_filter.SetInputData(0,mesh_A)
distance_filter.SetInputData(1,mesh_B)
distance_filter.Update()
# get distance
# distances = vtk_to_numpy(distance_filter.GetOutput().GetPointData().GetScalars())
distances = vtk_to_numpy(distance_filter.GetOutput().GetCellData().GetScalars())
mean_distance = np.mean(distances)
# get distance meshes
distance_mesh = vtk.vtkPolyData()
distance_mesh = distance_filter.GetOutput()
return distance_mesh, distances, mean_distance
#########################################################################
app = QApplication(sys.argv)
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fnames = QFileDialog.getOpenFileNames(None,"Select STL File","","STL Files (*.stl);;All Files (*)", "", options)[0]
if len(fnames) == 3:
# Sort filenames so T1 is first, T2 is second
fnames.sort(key=lambda f: int(re.sub('\D', '', f)))
mesh1_filename = fnames[0]
mesh2_filename = fnames[1]
mesh3_filename = fnames[2]
else:
sys.exit(app.exec_())
# Outputs
output_mesh_filename_1 = 'distance_mesh_T1.vtk'
output_txt_filename_1 = 'mean_absolute_distance_in_mm_T1.txt'
output_jpg_filename = 'distance_mesh.jpg'
output_mesh_filename_3 = 'distance_mesh_impression.vtk'
output_txt_filename_3 = 'mean_absolute_distance_in_mm_impression.txt'
print ('mesh1_filename: ' + str(mesh1_filename))
print ('mesh2_filename: ' + str(mesh2_filename))
print ('mesh3_filename: ' + str(mesh3_filename))
#########################################################################
# read mesh
reader1 = vtk.vtkSTLReader()
reader1.SetFileName(mesh1_filename)
reader1.Update()
mesh1 = reader1.GetOutput()
# read gt mesh
reader2 = vtk.vtkSTLReader()
reader2.SetFileName(mesh2_filename)
reader2.Update()
mesh2 = reader2.GetOutput()
# read 3rd mesh
reader3 = vtk.vtkSTLReader()
reader3.SetFileName(mesh3_filename)
reader3.Update()
mesh3 = reader3.GetOutput()
mapper1, mapper2, mapper1t, mapper3, mapper3t = [vtk.vtkPolyDataMapper() for i in range(5)]
actor1, actor2, actor1t, actor3, actor3t = [vtk.vtkActor() for i in range(5)]
mapper1.SetInputData(mesh1)
mapper2.SetInputData(mesh2)
mapper3.SetInputData(mesh3)
actor1.SetMapper(mapper1)
actor2.SetMapper(mapper2)
actor3.SetMapper(mapper3)
actor1.GetProperty().SetColor(0.8,0.4,0.2) # orange = T1
actor2.GetProperty().SetColor(0.4,0.8,0.2) # green = T2
actor3.GetProperty().SetColor(1.0,0.2,0.4) # red = impression
# calculate MAD
mesh1t = calculate_ICP(mesh1, mesh2)
distance_mesh, distances, mean_distance = calculate_MAD(mesh2, mesh1t)
mesh3t = calculate_ICP(mesh3, mesh2)
distance_mesh_3, distances_3, mean_distance_3 = calculate_MAD(mesh2, mesh3t)
# print maximum distance
print ('max distance: ' + str(np.max(distances)))
distance_meshb, distancesb, mean_distanceb = calculate_MAD(mesh1t, mesh2)
dist_min = distance_mesh.GetPointData().GetScalars().GetRange()[0]
dist_minb = distance_meshb.GetPointData().GetScalars().GetRange()[0]
dist_max = distance_mesh.GetPointData().GetScalars().GetRange()[1]
dist_maxb = distance_meshb.GetPointData().GetScalars().GetRange()[1]
# save distance mesh
polydata_writer = vtk.vtkPolyDataWriter()
polydata_writer.SetFileName(output_mesh_filename_1)
polydata_writer.SetInputData(distance_mesh)
polydata_writer.Write()
####### for 3 ##########
distance_meshb_3, distancesb_3, mean_distanceb_3 = calculate_MAD(mesh3t, mesh2)
dist_min_3 = distance_mesh_3.GetPointData().GetScalars().GetRange()[0]
dist_minb_3 = distance_meshb_3.GetPointData().GetScalars().GetRange()[0]
dist_max_3 = distance_mesh_3.GetPointData().GetScalars().GetRange()[1]
dist_maxb_3 = distance_meshb_3.GetPointData().GetScalars().GetRange()[1]
# save the MAD to a text file
with open(output_txt_filename_1, 'w') as f:
f.write(str(np.round(mean_distance,4)))
# save the MAD to a text file
with open(output_txt_filename_3, 'w') as f:
f.write(str(np.round(mean_distance_3,4)))
# Create renderers
renWin = vtk.vtkRenderWindow()
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
mapper1t.SetInputData(mesh1t)
actor1t.SetMapper(mapper1t)
actor1t.GetProperty().SetColor(0.8,0.4,0.2)
mapper, mapperb = [vtk.vtkPolyDataMapper() for i in range(2)]
actor, actorb = [vtk.vtkActor() for i in range(2)]
ren1, ren1b, ren2, ren3 = [vtk.vtkRenderer() for i in range(4)]
mapper3t.SetInputData(mesh3t)
actor3t.SetMapper(mapper3t)
actor3t.GetProperty().SetColor(1.0,0.2,0.4)
mapper3, mapper3b = [vtk.vtkPolyDataMapper() for i in range(2)]
actor3, actor3b = [vtk.vtkActor() for i in range(2)]
mapper3.SetInputData(mesh3)
actor3.SetMapper(mapper3)
actor3.GetProperty().SetColor(1.0,0.2,0.4)
# mapper3b.SetInputData(mesh3)
# actor3b.SetMapper(mapper3b)
# actor3b.GetProperty().SetColor(1.0,0.2,0.4)
if vtk.VTK_MAJOR_VERSION <= 5:
mapper.SetInput(distance_mesh)
mapperb.SetInput(distance_mesh_3)
# mapperb.SetInput(distance_meshb)
# mapper3.SetInput(distance_mesh_3)
# mapper3b.SetInput(distance_meshb_3)
else:
mapper.SetInputData(distance_mesh)
mapperb.SetInputData(distance_mesh_3)
# mapperb.SetInputData(distance_meshb)
# mapper3.SetInputData(distance_mesh_3)
# mapper3b.SetInputData(distance_meshb_3)
# Set range between 0 and 3
# mapper.SetScalarRange(0, 3)
# mapper.SetScalarRange(0, 1)
# mapper.SetScalarRange(0, 1.6)
mapper.SetScalarRange(0, 2)
# mapper.SetScalarRange(0, 2.4)
# Create own look up table with set colors
hueLut = vtk.vtkLookupTable()
# hueLut.SetNumberOfTableValues(6)
hueLut.SetNumberOfTableValues(5)
hueLut.Build()
nc = vtk.vtkNamedColors()
# hueLut.SetTableValue(0,nc.GetColor4d("Blue"))
# hueLut.SetTableValue(1,nc.GetColor4d("Purple"))
# hueLut.SetTableValue(2,nc.GetColor4d("Green"))
# hueLut.SetTableValue(3,nc.GetColor4d("Yellow"))
# hueLut.SetTableValue(4,nc.GetColor4d("Orange"))
# hueLut.SetTableValue(5,nc.GetColor4d("Red"))
hueLut.SetTableValue(0,nc.GetColor4d("Blue"))
hueLut.SetTableValue(1,nc.GetColor4d("Green"))
hueLut.SetTableValue(2,nc.GetColor4d("Yellow"))
hueLut.SetTableValue(3,nc.GetColor4d("Orange"))
hueLut.SetTableValue(4,nc.GetColor4d("Red"))
mapper.SetLookupTable(hueLut)
# Set the scalar bar
scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetLookupTable(hueLut)
scalarBar.SetTitle("mean distance")
# scalarBar.SetMaximumNumberOfColors(6); # 0.5 between each
# scalarBar.SetNumberOfLabels(7);
scalarBar.SetMaximumNumberOfColors(5) # 0.2 between each
scalarBar.SetNumberOfLabels(6)
# Set range between 0 and 3 for b
# mapperb.SetScalarRange(0, 3)
# mapperb.SetScalarRange(0, 1)
# mapperb.SetScalarRange(0, 1.6)
mapperb.SetScalarRange(0, 2)
# mapperb.SetScalarRange(0, 2.4)
# Create own look up table with set colors for b
hueLutb = vtk.vtkLookupTable()
# hueLutb.SetNumberOfTableValues(6)
hueLutb.SetNumberOfTableValues(5)
hueLutb.Build()
nc = vtk.vtkNamedColors()
# hueLutb.SetTableValue(0,nc.GetColor4d("Blue"))
# hueLutb.SetTableValue(1,nc.GetColor4d("Purple"))
# hueLutb.SetTableValue(2,nc.GetColor4d("Green"))
# hueLutb.SetTableValue(3,nc.GetColor4d("Yellow"))
# hueLutb.SetTableValue(4,nc.GetColor4d("Orange"))
# hueLutb.SetTableValue(5,nc.GetColor4d("Red"))
hueLutb.SetTableValue(0,nc.GetColor4d("Blue"))
hueLutb.SetTableValue(1,nc.GetColor4d("Green"))
hueLutb.SetTableValue(2,nc.GetColor4d("Yellow"))
hueLutb.SetTableValue(3,nc.GetColor4d("Orange"))
hueLutb.SetTableValue(4,nc.GetColor4d("Red"))
mapperb.SetLookupTable(hueLutb)
# Set the scalar bar b
scalarBarb = vtk.vtkScalarBarActor()
scalarBarb.SetLookupTable(hueLutb)
scalarBarb.SetTitle("mean distance")
# scalarBarb.SetMaximumNumberOfColors(6); # 0.5 between each
# scalarBarb.SetNumberOfLabels(7);
scalarBarb.SetMaximumNumberOfColors(5); # 0.2 between each
scalarBarb.SetNumberOfLabels(6);
# Add both actors to renderer
ren1.AddActor2D(scalarBar)
ren1b.AddActor2D(scalarBarb)
#####################################################
### Set up cutting box - 3 planes and the clipper ###
#####################################################
### For mesh 2 and mesh1t ###
normals = vtk.vtkPolyDataNormals()
normals.SetInputData(distance_mesh)
normals.Update()
points = vtk_to_numpy(distance_mesh.GetPoints().GetData())
bounds_min = np.min(points,axis=0)
bounds_max = np.max(points,axis=0)
planes = vtk.vtkPlanes()
planes.SetBounds(bounds_min[0], bounds_max[0], bounds_min[1], bounds_max[1], bounds_min[2], bounds_max[2])
clipper = vtk.vtkClipPolyData()
clipper.SetInputData(distance_mesh)
clipper.SetClipFunction(planes)
clipper.InsideOutOn()
clipper.GenerateClippedOutputOn()
clipMapper = vtk.vtkPolyDataMapper()
clipMapper.SetInputConnection(clipper.GetOutputPort())
clipMapper.ScalarVisibilityOn()
# clipMapper.SetScalarRange(0, 3)
# clipMapper.SetScalarRange(0, 1)
# clipMapper.SetScalarRange(0, 1.6)
clipMapper.SetScalarRange(0, 2)
# clipMapper.SetScalarRange(0, 2.4)
clipMapper.SetLookupTable(hueLut)
clipActor = vtk.vtkActor()
clipActor.SetMapper(clipMapper)
# create a MAD annotation
corner_MAD = vtk.vtkCornerAnnotation()
corner_MAD.SetMinimumFontSize(16)
corner_MAD.SetMaximumFontSize(16)
corner_MAD.SetText(7, "MAD: " + str(np.round(mean_distance,4)))
boxWidget = vtk.vtkBoxWidget()
boxWidget.SetCurrentRenderer(ren1)
boxWidget.SetInteractor(iren)
boxWidget.SetPlaceFactor(1.25)
### For mesh2 and mesh3t ###
normals_3 = vtk.vtkPolyDataNormals()
normals_3.SetInputData(distance_mesh_3)
normals_3.Update()
points_3 = vtk_to_numpy(distance_mesh_3.GetPoints().GetData())
bounds_min_3 = np.min(points_3,axis=0)
bounds_max_3 = np.max(points_3,axis=0)
planes_3 = vtk.vtkPlanes()
planes_3.SetBounds(bounds_min_3[0], bounds_max_3[0], bounds_min_3[1], bounds_max_3[1], bounds_min_3[2], bounds_max_3[2])
clipper_3 = vtk.vtkClipPolyData()
clipper_3.SetInputData(distance_mesh_3)
clipper_3.SetClipFunction(planes_3)
clipper_3.InsideOutOn()
clipper_3.GenerateClippedOutputOn()
clipMapper_3 = vtk.vtkPolyDataMapper()
clipMapper_3.SetInputConnection(clipper_3.GetOutputPort())
clipMapper_3.ScalarVisibilityOn()
# clipMapper.SetScalarRange(0, 3)
# clipMapper.SetScalarRange(0, 1)
# clipMapper.SetScalarRange(0, 1.6)
clipMapper_3.SetScalarRange(0, 2)
# clipMapper.SetScalarRange(0, 2.4)
clipMapper_3.SetLookupTable(hueLut)
clipActor_3 = vtk.vtkActor()
clipActor_3.SetMapper(clipMapper_3)
# create a MAD annotation
corner_MAD_3 = vtk.vtkCornerAnnotation()
corner_MAD_3.SetMinimumFontSize(16)
corner_MAD_3.SetMaximumFontSize(16)
corner_MAD_3.SetText(7, "MAD: " + str(np.round(mean_distance_3,4)))
boxWidget_3 = vtk.vtkBoxWidget()
boxWidget_3.SetCurrentRenderer(ren1b)
boxWidget_3.SetInteractor(iren)
boxWidget_3.SetPlaceFactor(1.25)
def myCallback(obj, event):
'''Callback function for the plane'''
global clipActor, planes
obj.GetPlanes(planes)
clipActor.VisibilityOn()
### Update the distance mesh here by the new clip plane ###
# Don't need to update scalar bars since they are set always
# from 0 to 3.
# Clip mesh2 and mesh1t using the same clip plane and
# calculate a new distance mesh
global mesh2
global mesh1t
global distance_mesh
clipper_mesh2 = vtk.vtkClipPolyData()
clipper_mesh2.SetInputData(mesh2)
clipper_mesh2.SetClipFunction(planes)
# clipper_mesh2.InsideOutOn()
clipper_mesh2.GenerateClippedOutputOn()
clipper_mesh2.Update()
mesh2_clipped = vtk.vtkPolyData()
mesh2_clipped = clipper_mesh2.GetClippedOutput()
clipper_mesh1t = vtk.vtkClipPolyData()
clipper_mesh1t.SetInputData(mesh1t)
clipper_mesh1t.SetClipFunction(planes)
# clipper_mesh1t.InsideOutOn()
clipper_mesh1t.GenerateClippedOutputOn()
clipper_mesh1t.Update()
mesh1t_clipped = vtk.vtkPolyData()
mesh1t_clipped = clipper_mesh1t.GetClippedOutput()
distance_mesh, distances, mean_distance = calculate_MAD(mesh2_clipped, mesh1t_clipped)
global corner_MAD
corner_MAD.SetMinimumFontSize(14)
corner_MAD.SetMaximumFontSize(14)
corner_MAD.SetText(7, "MAD: " + str(np.round(mean_distance,4)))
# save distance mesh that has been clipped
polydata_writer = vtk.vtkPolyDataWriter()
polydata_writer.SetFileName(output_mesh_filename_1)
polydata_writer.SetInputData(distance_mesh)
polydata_writer.Write()
# save the MAD to a text file
with open(output_txt_filename_1, 'w') as f:
f.write(str(np.round(mean_distance,4)))
def myCallback_3(obj, event):
'''Callback function for the plane'''
global clipActor_3, planes_3
obj.GetPlanes(planes_3)
clipActor_3.VisibilityOn()
### Update the distance mesh here by the new clip plane ###
# Don't need to update scalar bars since they are set always
# from 0 to 3.
# Clip mesh2 and mesh1t using the same clip plane and
# calculate a new distance mesh
global mesh2
global mesh3t
global distance_mesh_3
clipper_mesh2_3 = vtk.vtkClipPolyData()
clipper_mesh2_3.SetInputData(mesh2)
clipper_mesh2_3.SetClipFunction(planes_3)
# clipper_mesh2.InsideOutOn()
clipper_mesh2_3.GenerateClippedOutputOn()
clipper_mesh2_3.Update()
mesh2_clipped_3 = vtk.vtkPolyData()
mesh2_clipped_3 = clipper_mesh2_3.GetClippedOutput()
clipper_mesh3t = vtk.vtkClipPolyData()
clipper_mesh3t.SetInputData(mesh3t)
clipper_mesh3t.SetClipFunction(planes_3)
# clipper_mesh1t.InsideOutOn()
clipper_mesh3t.GenerateClippedOutputOn()
clipper_mesh3t.Update()
mesh3t_clipped = vtk.vtkPolyData()
mesh3t_clipped = clipper_mesh3t.GetClippedOutput()
distance_mesh_3, distances_3, mean_distance_3 = calculate_MAD(mesh2_clipped, mesh3t_clipped)
global corner_MAD_3
corner_MAD_3.SetMinimumFontSize(14)
corner_MAD_3.SetMaximumFontSize(14)
corner_MAD_3.SetText(7, "MAD: " + str(np.round(mean_distance_3,4)))
# save distance mesh that has been clipped
polydata_writer = vtk.vtkPolyDataWriter()
polydata_writer.SetFileName(output_mesh_filename_3)
polydata_writer.SetInputData(distance_mesh_3)
polydata_writer.Write()
# save the MAD to a text file
with open(output_txt_filename_3, 'w') as f:
f.write(str(np.round(mean_distance_3,4)))
# Interaction event
boxWidget.SetInputData(distance_mesh)
boxWidget.PlaceWidget()
boxWidget.On()
boxWidget.AddObserver("EndInteractionEvent", myCallback) # should be faster compared to "InteractionEvent"
# Interaction event
boxWidget_3.SetInputData(distance_mesh_3)
boxWidget_3.PlaceWidget()
boxWidget_3.On()
boxWidget_3.AddObserver("EndInteractionEvent", myCallback_3) # should be faster compared to "InteractionEvent"
##############################################################
### Set up the clicking of a point to display the distance ###
##############################################################
# # Create the point picker
# picker = vtk.vtkPointPicker()
# # create a text actor
# txt1 = vtk.vtkTextActor()
#
# def annotate_pick(obj, event):
# '''Callback function for displaying the scalar value of a clicked point'''
#
# global ren1
# global iren
# global txt_actor
# global distance_mesh
#
# pointPicker = vtk.vtkPointPicker()
# eventPosition = obj.GetEventPosition()
# result = picker.Pick(float(eventPosition[0]),float(eventPosition[1]),0.0,ren1)
# pickId = picker.GetPointId()
#
# # Get scalar that is associated with this pickId
# scalars = vtk_to_numpy(distance_mesh.GetPointData().GetScalars())
# dist_value = scalars[pickId]
#
# # Set as input to text actor
# txt1.SetInput("Picked point distance value: " + str(np.round(dist_value,4)))
# Create the cell picker
picker = vtk.vtkCellPicker()
# create a text actor
txt1 = vtk.vtkTextActor()
txt3 = vtk.vtkTextActor()
def annotate_pick(obj, event):
'''Callback function for displaying the scalar value of a clicked point'''
global ren1
global iren
global txt_actor
global distance_mesh
# pointPicker = vtk.vtkPointPicker()
cellPicker = vtk.vtkCellPicker()
eventPosition = obj.GetEventPosition()
result = picker.Pick(float(eventPosition[0]),float(eventPosition[1]),0.0,ren1)
pickId = picker.GetCellId()
# Get scalar that is associated with this pickId
scalars = vtk_to_numpy(distance_mesh.GetCellData().GetScalars())
dist_value = scalars[pickId]
# Set as input to text actor
txt1.SetInput("Picked point distance value: " + str(np.round(dist_value,4)))
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.AddObserver("RightButtonPressEvent", annotate_pick)
# Text actor properties
txtprop=txt1.GetTextProperty()
txtprop.SetFontFamilyToArial()
txtprop.SetFontSize(14)
txtprop.SetColor(1,1,1)
txt1.SetTextProperty(txtprop)
txt1.SetPosition2(20,30)
corner1 = vtk.vtkCornerAnnotation()
corner1.SetMinimumFontSize(14)
corner1.SetMaximumFontSize(14)
corner1.SetText(2, "Mean absolute distance \n Rotate with left mouse \n Move object or box planes by middle mouse button \n Turn on/off box planes by pressing i \n Move planes with left mouse \n Select cell on condyle with right mouse")
corner2 = vtk.vtkCornerAnnotation()
corner2.SetMinimumFontSize(14)
corner2.SetMaximumFontSize(14)
# corner2.SetText(2,"Before alignment \n T1 (orange) and T2 (green)")
corner2.SetText(2,"Before alignment \n scan 1 (orange) and scan 2 (green) and scan 3 impression (red)")
corner3 = vtk.vtkCornerAnnotation()
corner3.SetMinimumFontSize(14)
corner3.SetMaximumFontSize(14)
# corner3.SetText(2,"After alignment \n T1 (orange) and T2 (green)")
corner3.SetText(2,"After alignment \n scan 1 (orange) and scan 2 (green) and scan 3 impression (red)")
#################
### Rendering ###
#################
# actor.SetMapper(mapper)
# ren1.AddActor(actor) # This is the original distance mesh
### upper right - ren1 - Clip 1t and 2 ###
ren1.AddActor(clipActor)
ren1.AddActor2D(txt1)
ren1.AddActor2D(corner1)
ren1.AddActor2D(corner_MAD)
ren1.SetViewport(0.5, 0.5, 1.0, 1.0)
### (previously) Same as above 1 and 2 from other side ###
# actorb.SetMapper(mapperb)
# ren1b.AddActor(actorb)
# actor3b.SetMapper(mapper3b)
# ren1b.AddActor(actor3b)
### lower right - ren1b - Clip 2 and 3t ###
ren1b.AddActor(clipActor_3)
ren1b.AddActor(txt3)
ren1b.AddActor2D(corner1)
ren1b.AddActor2D(corner_MAD_3)
# ren1b.AddActor(actor2)
# ren1b.AddActor(actor3t)
ren1b.SetViewport(0.5, 0.0, 1.0, 0.5)
#### upper left - ren2 - The original ###
ren2.AddActor(actor1)
ren2.AddActor(actor2)
ren2.AddActor(actor3)
ren2.AddActor2D(corner2)
ren2.SetViewport(0.0, 0.5, 0.5, 1.0)
### lower left - ren3 - The transformed ###
ren3.AddActor(actor1t)
ren3.AddActor(actor2)
ren3.AddActor(actor3t)
ren3.AddActor2D(corner3)
ren3.SetViewport(0.0, 0.0, 0.5, 0.5)
ren1b.SetBackground(0.05, 0.05, 0.05)
ren3.SetBackground(0.1, 0.1, 0.1)
ren2.SetBackground(0.15, 0.15, 0.15)
renWin.AddRenderer(ren1)
renWin.AddRenderer(ren1b)
renWin.AddRenderer(ren2)
renWin.AddRenderer(ren3)
renWin.Render()
renWin.SetSize(1920,1080)
renWin.Render()
# write out screenshot
w2if = vtk.vtkWindowToImageFilter()
w2if.SetInput(renWin)
w2if.Update()
writer = vtk.vtkJPEGWriter()
writer.SetFileName(output_jpg_filename)
writer.SetInputConnection(w2if.GetOutputPort())
writer.Write()
# iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
# added
# iren.SetPicker(picker)
iren.Initialize()
iren.Start()
# sys.exit(app.exec_())