-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
1605 lines (1386 loc) · 60.6 KB
/
main.js
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
papaya.Container.syncViewers = true;
// Remove smooth display:
papaya.utilities.UrlUtils.createCookie(papaya.viewer.Preferences.COOKIE_PREFIX + 'smoothDisplay', 'No', papaya.viewer.Preferences.COOKIE_EXPIRY_DAYS);
papaya.utilities.UrlUtils.createCookie(papaya.viewer.Preferences.COOKIE_PREFIX + 'showOrientation', 'Yes', papaya.viewer.Preferences.COOKIE_EXPIRY_DAYS);
papaya.viewer.Viewer.MAX_OVERLAYS = 20;
papaya.utilities.PlatformUtils.smallScreen = false;
// To draw / change the data of a volume:
// papayaContainers[0].viewer.screenVolumes[3].volume.imageData.data[i] = 1
papaya.Container.atlasWorldSpace = false
var params = [];
params['images'] = [];
let current_lesion_index = 0;
let image_archive = null;
let images_url = null;
let image_files = null;
let task = {};
let lesions = [];
let grid = null;
let editable_image_data = null;
// let world_space = true;
let show_loader = () => {
let loader = document.getElementById('loader')
loader.classList.remove('hide')
}
let hide_loader = () => {
let loader = document.getElementById('loader')
loader.classList.add('hide')
}
let update_best_segmentation = ()=> {
if(task.fields == null || task.fields.findIndex((e)=>e.field=='best_segmentation')<0) {
return
}
let container = document.getElementById('toggle-visibility-buttons')
let checkboxes = container.querySelectorAll('input[type="checkbox"]')
let best_segmentation_string = ''
for(let checkbox of checkboxes) {
if(checkbox.checked) {
let checkbox_id = checkbox.id
let name = checkbox_id.replace('checkbox_', '')
let image_type = checkbox.getAttribute('data-image-type')
if(image_type == 'image') {
continue
}
let slider = document.getElementById('threshold_'+name+'_number')
let plus_prefix = best_segmentation_string.length > 0 ? ' + ' : ''
best_segmentation_string += plus_prefix + name + (slider ? ':' + slider.value : '')
}
}
let best_segmentation_label = document.getElementById('best_segmentation_value')
best_segmentation_label.textContent = best_segmentation_string
lesions[current_lesion_index]['best_segmentation'] = best_segmentation_string
set_data_selected_row('best_segmentation', best_segmentation_string)
save_to_local_storage()
}
let create_checkbox = (name, image_index, visible, exclusive_button, image_type='') => {
let container = document.getElementById('toggle-visibility-buttons')
{/* <div>
<label for='checkbox_name'>name</label>
<input type='checkbox' name='name' id='checkbox_name'>
</div> */}
let div = document.createElement('div');
div.classList.add('checkbox')
let label = document.createElement('label');
label.setAttribute('for', 'checkbox_' + name)
label.innerText = name
let input = document.createElement('input');
input.setAttribute('type', 'checkbox')
input.setAttribute('data-index', image_index)
input.setAttribute('data-image-type', image_type)
input.setAttribute('id', 'checkbox_' + name)
input.setAttribute('name', name)
input.checked = visible
input.disabled = true
div.appendChild(input)
if(exclusive_button) {
let exclusive_input = document.createElement('input');
exclusive_input.setAttribute('type', 'button')
exclusive_input.setAttribute('id', 'exculsive_checkbox_' + name)
exclusive_input.setAttribute('name', name)
exclusive_input.setAttribute('value', 'only')
exclusive_input.checked = false
div.appendChild(exclusive_input)
exclusive_input.addEventListener('mousedown', (event) => {
for(let i=0 ; i<papayaContainers[1].viewer.screenVolumes.length ; i++) {
papaya.Container.hideImage(1, i)
}
if(image_index < papayaContainers[1].viewer.screenVolumes.length || papayaContainers[1].viewer.loadingVolume != null) {
papaya.Container.showImage(1, image_index)
}
})
exclusive_input.addEventListener('mouseup', (event) => {
for(let i=0 ; i<papayaContainers[1].viewer.screenVolumes.length ; i++) {
let checkbox = document.querySelector('input[type="checkbox"][data-index="'+i+'"]')
if(checkbox.checked) {
papaya.Container.showImage(1, i)
} else {
papaya.Container.hideImage(1, i)
}
}
event.preventDefault()
event.stopPropagation()
return -1
})
}
div.appendChild(label)
container.appendChild(div);
input.addEventListener('change', (event) => {
if(event.target.checked) {
if(image_index < papayaContainers[1].viewer.screenVolumes.length || papayaContainers[1].viewer.loadingVolume != null) {
papaya.Container.showImage(1, image_index)
}
} else {
if(image_index < papayaContainers[1].viewer.screenVolumes.length || papayaContainers[1].viewer.loadingVolume != null) {
papaya.Container.hideImage(1, image_index)
}
update_best_segmentation()
}
})
}
let rle_encode = (data) => {
let last = data[0]
let result = [last]
let n=1
for(let i=1 ; i<data.length ; i++) {
if(data[i]==last) {
n++
} else {
result.push(n)
n=1
last = data[i]
}
}
result.push(n)
return result
}
let rle_decode = (data) => {
let result = []
let value = data[0]
for(let i=1 ; i<data.length ; i++) {
result = result.concat(Array(data[i]).fill(value))
value = 1 - value
}
return result
}
let check_rle = (data) => {
encoded = rle_encode(data)
decoded = rle_decode(encoded)
for(let i=0 ; i<data.length ; i++) {
if(decoded[i] != data[i]) {
console.log(i, decoded[i], data[i])
break
}
}
}
let save_new_segmentation = () => {
let volumes = papayaContainers[1].viewer.screenVolumes
let editable_image_index = get_editable_image_index()
if(editable_image_index < 0) {
return
}
let volume = volumes[editable_image_index].volume
let data = volume.imageData.data
let meta_data = {
header: volume.header,
lesion: lesions[current_lesion_index],
}
first_image = lesions[current_lesion_index].images[editable_image_index]
segmentation_name = first_image.file.replace(first_image.name, 'new_segmentation').replace('.nii.gz', '')
if(window.parent != window) {
// check_rle(data)
let data_string = JSON.stringify(rle_encode(data))
window.parent.postMessage({segmentation: { data: data_string, meta_data: JSON.stringify(meta_data, null, '\t') }})
} else {
downloadBlob(data, `${segmentation_name}.bin`, 'application/octet-stream');
downloadJSON(meta_data, `${segmentation_name}.json`)
}
segmentation_is_modified = false
}
let save_new_segmentation_to_local_storage = ()=> {
// let segmentation_string = new TextDecoder('utf-8').decode(editable_image_data)
// compressed = LZString.compressToUTF16(segmentation_string)
// let command_index = localStorage.getItem('current-command')
// if(command_index == null) {
// command_index = 0
// } else {
// command_index = parseInt(command_index) + 1
// }
// localStorage.setItem('current-command', command_index)
// localStorage.setItem('history'+command_index, compressed)
// localStorage.setItem('segmentation', compressed)
}
// let execute = (command_index)=> {
// localStorage.setItem('current-command', command_index)
// let compressed = localStorage.getItem('history'+command_index)
// let segmentation_string = LZString.decompressFromUTF16(compressed)
// editable_image_data = new TextEncoder().encode(segmentation_string)
// papayaContainers[1].viewer.drawViewer(true, false)
// }
// let undo = ()=> {
// let command_index = localStorage.getItem('current-command')
// if(command_index == null) {
// return
// }
// command_index = parseInt(command_index) - 1
// execute(command_index)
// }
// let redo = ()=> {
// let command_index = localStorage.getItem('current-command')
// if(command_index == null) {
// return
// }
// command_index = parseInt(command_index) + 1
// execute(command_index)
// }
let cursor_png_size = 50
let draw_cursor = ()=> {
const canvas = document.createElement('canvas');
size = cursor_png_size
canvas.width = size
canvas.height = size
const ctx = canvas.getContext('2d');
// Draw the ellipse
let scale = 5
ctx.beginPath();
ctx.ellipse(size/2, size/2, scale*brush_size, scale*brush_size, 0, 0, 2*Math.PI);
ctx.strokeStyle = "black";
ctx.lineWidth = 1;
ctx.stroke();
ctx.beginPath();
ctx.ellipse(size/2, size/2, 1, 1, 0, 0, 2*Math.PI);
ctx.strokeStyle = "black";
ctx.lineWidth = 1;
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle = "white";
let offset = 0.5;
ctx.ellipse(size/2, size/2, scale*brush_size-2*offset, scale*brush_size-2*offset, 0, 0, 2*Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.ellipse(size/2, size/2, 2, 2, 0, 0, 2*Math.PI);
ctx.stroke();
return canvas.toDataURL();
}
let set_brush_cursor = ()=> {
$('#papayaViewer1 > canvas').css({'cursor': 'url('+draw_cursor()+') '+(cursor_png_size/2)+' '+(cursor_png_size/2)+', auto'})
}
let set_auto_cursor = ()=> {
$('#papayaViewer1 > canvas').css({'cursor': 'crosshair'})
}
let create_slider = (name, image_index, visible, parameters) => {
let container = document.getElementById('toggle-visibility-buttons')
{/* <div>
<label for='checkbox_name'>name</label>
<input type='checkbox' name='name' id='checkbox_name'>
</div> */}
let div = document.createElement('div');
div.classList.add('slider')
// let label = document.createElement('label');
// label.setAttribute('for', 'threshold_' + name)
// label.innerText = 'Threshold'
for(let input_type of ['range', 'number']) {
let input = document.createElement('input');
input.setAttribute('type', input_type)
input.setAttribute('id', 'threshold_' + name + '_' + input_type)
input.setAttribute('min', parameters != null && parameters.min != null ? parameters.min : 0)
input.setAttribute('max', parameters != null && parameters.max != null ? parameters.max : 1)
input.setAttribute('step', parameters != null && parameters.step != null ? parameters.step : 0.001)
if(parameters != null && parameters.value != null) {
input.setAttribute('value', parameters.value)
}
input.setAttribute('class', 'slider')
input.setAttribute('data-index', image_index)
input.setAttribute('name', name)
input.disabled = true
div.appendChild(input)
input.onchange = (event)=> {
let value = parseFloat(event.target.value)
let min = input.value > 0.01 ? value : 0
let max = input.value > 0.01 ? value : 1
papayaContainers[1].viewer.screenVolumes[image_index].updateMinLUT(min*papaya.viewer.ColorTable.LUT_MAX);
papayaContainers[1].viewer.screenVolumes[image_index].updateMaxLUT(max*papaya.viewer.ColorTable.LUT_MAX);
papayaContainers[1].viewer.screenVolumes[image_index].updateColorBar();
papayaContainers[1].viewer.screenVolumes[image_index].setScreenRange(min, max);
papayaContainers[1].viewer.drawViewer(true, false);
other_input_type = input_type == 'range' ? 'number' : 'range'
let other_input = document.getElementById('threshold_' + name + '_' + other_input_type)
other_input.value = value
// let name = event.target.id.replace('threshold_', '')
// let value_label = document.getElementById('threshold_label_'+name)
// value_label.innerText = value
// papaya.Container.hideImage(1, image_index)
// papaya.Container.showImage(1, image_index)
let checkbox = document.getElementById('checkbox_'+name)
checkbox.checked = true
checkbox.dispatchEvent(new Event('change'))
update_best_segmentation()
}
}
// let input = document.createElement('input');
// input.setAttribute('type', 'range')
// input.setAttribute('id', 'threshold_' + name)
// input.setAttribute('min', parameters != null && parameters.min != null ? parameters.min : 0)
// input.setAttribute('max', parameters != null && parameters.max != null ? parameters.max : 1)
// input.setAttribute('step', parameters != null && parameters.step != null ? parameters.step : 0.001)
// input.setAttribute('class', 'slider')
// input.setAttribute('name', name)
// input.disabled = true
// value_label = document.createElement('label');
// value_label.setAttribute('id', 'threshold_label_' + name)
// value_label.innerText = ''
// // div.appendChild(label)
// div.appendChild(input)
// div.appendChild(value_label)
container.appendChild(div);
}
// let create_fill_button = (name, image_index) => {
// let container = document.getElementById('toggle-visibility-buttons')
// let button = document.createElement('button');
// button.textContent = 'Fill'
// button.addEventListener('click', (event)=> {
// let viewer = papayaContainers[1].viewer
// let cc = viewer.currentCoord
// let todo = [cc]
// let seen = new Set()
// let threshold_number = document.getElementById('threshold_' + name + '_number')
// let threshold = threshold_number.value
// let volume = papayaContainers[1].viewer.screenVolumes[image_index].volume
// let orientation = volume.transform.voxelValue.orientation
// let offset = orientation.convertIndexToOffset(cc.x, cc.y, cc.z)
// seen.add(offset)
// while(todo.length > 0) {
// flood_fill(todo, seen, threshold, image_index)
// }
// papayaContainers[1].viewer.drawViewer(true, false);
// })
// container.appendChild(button);
// }
let create_toggle_button = (button_name, image_index, visible) => {
let container = document.getElementById('toggle-visibility-buttons')
let toggle_button = document.createElement('button');
toggle_button.innerHTML = visible ? 'Hide ' + button_name : 'Show ' + button_name;
container.appendChild(toggle_button);
toggle_button.setAttribute('data-visible', visible ? 'true' : 'false')
toggle_button.addEventListener('click', () => {
let visible = toggle_button.getAttribute('data-visible')
if (visible == 'true') {
papaya.Container.hideImage(1, image_index)
toggle_button.innerHTML = 'Show ' + button_name
toggle_button.setAttribute('data-visible', 'false')
} else {
papaya.Container.showImage(1, image_index)
toggle_button.innerHTML = 'Hide ' + button_name
toggle_button.setAttribute('data-visible', 'true')
}
})
}
let loaded_images = []
let current_image_index = 0
let get_editable_image_index = ()=> {
return task.lesions[current_lesion_index].images.findIndex((image)=>image.editable)
}
let load_lesion_viewer = (images, image_parameters, lesion, lesion_index) => {
let draw_button = document.getElementById('draw')
if(draw_button.classList.contains('active')) {
draw_button.classList.remove('active')
draw_button.getElementsByTagName('span')[0].textContent = 'Draw'
drawing = false
set_auto_cursor()
}
for (let li of loaded_images) {
delete window[li.name]
}
loaded_images = []
current_image_index = 0
params = {}
if(image_archive != null) {
params['encodedImages'] = []
} else if(images_url != null) {
params['images'] = images.map((name)=> images_url + name + '?v=' + Math.random() )
} else {
params['files'] = images
}
let image_index = 0;
// let screen_volumes = []
for (let image_parameter of image_parameters) {
let file_name = image_parameter.file_name
let parameters = image_parameter.parameters
let image_name = image_archive != null ? 'lesion_viewer_' + file_name.replace('/', '_').replace('.nii.gz', '') : file_name.split('/').at(-1)
if(image_archive != null) {
params['encodedImages'].push(image_name)
}
window[image_name] = images[image_index]
let display_name = image_parameter.name || file_name.split('/').at(-1)
loaded_images.push({ name: image_name, file_name: file_name, index: image_index, display_name: display_name })
params[image_name] = parameters
// if(image_parameter.fill_button) {
// create_fill_button(display_name, image_index)
// }
if(image_parameter.threshold_slider) {
create_slider(display_name, image_index, image_parameter.display, image_parameter.parameters)
}
create_checkbox(display_name, image_index, image_parameter.display, image_parameter.exclusive_button, image_parameter.image_type)
image_parameter.image_index = image_index
image_index++
}
current_image_index = loaded_images.length-1
if(task.parameters != null && task.parameters.world_space != null) {
params['worldSpace'] = task.parameters.world_space
params['ignoreNiftiTransforms'] = !task.parameters.world_space
}
params['coordinate'] = lesion['location_voxel']
params['smoothDisplay'] = false
params['loadingComplete'] = () => {
let viewer = papayaContainers[1].viewer
if(task.parameters != null && task.parameters.rotate_views != null) {
for(let i=0 ; i<task.parameters.rotate_views ; i++) {
viewer.rotateViews()
}
}
go_to_lesion(lesions[current_lesion_index])
for (let image_parameter of image_parameters) {
if (image_parameter.display != null && !image_parameter.display) {
papaya.Container.hideImage(1, image_parameter.image_index)
}
if(image_parameter.parameters != null && image_parameter.parameters.value != null) {
let file_name = image_parameter.file_name
let display_name = image_parameter.name || file_name.split('/').at(-1)
let input = document.getElementById('threshold_' + display_name + '_range')
if(input != null) {
input.onchange({target: {value: image_parameter.parameters.value}})
}
}
}
let container = document.getElementById('toggle-visibility-buttons')
let checkboxes = document.getElementsByTagName('input')
for(let checkbox of checkboxes) {
checkbox.disabled = false
}
let editable_image_index = get_editable_image_index()
if(editable_image_index < 0) {
return
}
let new_segmentation_screen_volume = viewer.screenVolumes[editable_image_index]
// new_segmentation_screen_volume.changeColorTable(viewer, 'Red Overlay')
// new_segmentation_screen_volume.setScreenRange(0, 9)
papayaContainers[1].toolbar.updateImageButtons()
let new_segmentation_volume = new_segmentation_screen_volume.volume
let data = new_segmentation_volume.imageData.data
// for (let i = 0; i < data.length; i++) {
// data[i] = 0
// }
editable_image_data = data
viewer.drawViewer(true, false);
}
let description = document.getElementById('description')
description.innerText = `${lesion['name']} - ${lesion_index + 1}/${lesions.length}`
papaya.Container.resetViewer(1, params);
if(image_archive != null) {
params['encodedImages'] = [params['encodedImages'][0]]
} else if(images_url != null) {
params['images'] = [params['images'][0]]
} else {
params['files'] = [images[0]]
}
params['loadingComplete'] = null
papaya.Container.resetViewer(0, params);
hide_loader()
let canvas = papayaContainers[1].viewer.canvas
canvas.addEventListener('mousemove', on_mouse_move, false);
canvas.addEventListener('mousedown', on_mouse_down, false);
canvas.addEventListener('mouseup', on_mouse_up, false);
}
let dragging = false
let drawing = false
let segmentation_is_modified = false
let filling = false
let brush_size = 1
let adding_voxels = true
let flood_fill = (todo, offsets, threshold, volumeIndex, slice, brush_value, fill3D)=> {
let [x, y, z] = todo.shift()
let volume = papayaContainers[1].viewer.screenVolumes[volumeIndex].volume
// let orientation = volume.transform.voxelValue.orientation
let offset = convert_coord_to_offset(x, y, z, volume)
let volume_data = volume.imageData.data
if(volume_data[offset] < threshold) {
return
}
editable_image_data[offset] = adding_voxels ? brush_value : 0
segmentation_is_modified = true
// for(let dx=-1 ; dx<=1 ; dx++) {
// for(let dy=-1 ; dy<=1 ; dy++) {
// let offset = orientation.convertIndexToOffset(x+dx, y+dy, z)
// if(!offsets.has(offset) && volume_data[offset] > threshold) {
// todo.push([x+dx, y+dy, z])
// }
// }
// }
let viewer = papayaContainers[1].viewer
let indices = fill3D ? [[-1, 0, 0], [1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]] : [[0, -1, 0], [1, 0, 0], [0, 1, 0], [-1, 0, 0]]
if(viewer.worldSpace) {
for(let coord of indices.slice()) {
indices.push(coord.map((x)=>x/2))
}
}
for(let deltas of indices) {
let [dx, dy, dz] = deltas
let [xf, yf, zf] = draw3D? [x+dx, y+dy, z+dz] : slice == 1 ? [x+dx, y+dy, z] : slice == 2 ? [x+dx, y, z+dy] : [x, y+dx, z+dy]
let offset = convert_coord_to_offset(xf, yf, zf, volume)
if(!offsets.has(offset)) {
offsets.add(offset)
todo.push([xf, yf, zf])
}
}
}
let draw_voxel = (x, y, z, volume, slice, brush_value) => {
let viewer = papayaContainers[1].viewer
let draw3Dcheckbox = document.getElementById('draw3D')
let draw3D = draw3Dcheckbox != null && draw3Dcheckbox.checked
bs = brush_size-1
for(let dx=-bs ; dx<=bs ; dx+=viewer.worldSpace ? 0.5 : 1) {
for(let dy=-bs ; dy<=bs ; dy+=viewer.worldSpace ? 0.5 : 1) {
for(let dz=draw3D?-bs:0 ; dz<=(draw3D?bs:0) ; dz+=viewer.worldSpace ? 0.5 : 1) {
if(Math.sqrt(dx*dx+dy*dy+dz*dz)>=brush_size) {
continue
}
let [xf, yf, zf] = draw3D? [x+dx, y+dy, z+dz] : slice == 1 ? [x+dx, y+dy, z] : slice == 2 ? [x+dx, y, z+dy] : [x, y+dx, z+dy]
let offset = convert_coord_to_offset(xf, yf, zf, volume)
editable_image_data[offset] = adding_voxels ? brush_value : 0
}
}
}
segmentation_is_modified = true
}
let get_xy_loc = (event)=> {
let viewer = papayaContainers[1].viewer
let currentMouseX = papaya.utilities.PlatformUtils.getMousePositionX(event);
let currentMouseY = papaya.utilities.PlatformUtils.getMousePositionY(event);
if(viewer.canvasRect == null) {
return [currentMouseX, currentMouseY]
}
let xLoc = currentMouseX - viewer.canvasRect.left;
let yLoc = currentMouseY - viewer.canvasRect.top;
return [xLoc, yLoc]
}
let get_selected_slice = (event)=> {
let selectedSlice = null
let viewer = papayaContainers[1].viewer
let [xLoc, yLoc] = get_xy_loc(event)
if(viewer.canvasRect == null) {
return viewer.mainSlice
}
if (viewer.insideScreenSlice(viewer.axialSlice, xLoc, yLoc, viewer.volume.getXDim(), viewer.volume.getYDim())) {
selectedSlice = viewer.axialSlice
} else if (viewer.insideScreenSlice(viewer.coronalSlice, xLoc, yLoc, viewer.volume.getXDim(), viewer.volume.getZDim())) {
selectedSlice = viewer.coronalSlice
} else if (viewer.insideScreenSlice(viewer.sagittalSlice, xLoc, yLoc, viewer.volume.getYDim(), viewer.volume.getZDim())) {
selectedSlice = viewer.sagittalSlice
}
return selectedSlice
}
let get_cursor_position = (event)=> {
let viewer = papayaContainers[1].viewer
let [xLoc, yLoc] = get_xy_loc(event)
let xImageLoc, yImageLoc, zImageLoc;
if (viewer.insideScreenSlice(viewer.axialSlice, xLoc, yLoc, viewer.volume.getXDim(), viewer.volume.getYDim())) {
xImageLoc = viewer.convertScreenToImageCoordinateX(xLoc, viewer.axialSlice);
yImageLoc = viewer.convertScreenToImageCoordinateY(yLoc, viewer.axialSlice);
zImageLoc = viewer.axialSlice.currentSlice;
} else if (viewer.insideScreenSlice(viewer.coronalSlice, xLoc, yLoc, viewer.volume.getXDim(), viewer.volume.getZDim())) {
xImageLoc = viewer.convertScreenToImageCoordinateX(xLoc, viewer.coronalSlice);
zImageLoc = viewer.convertScreenToImageCoordinateY(yLoc, viewer.coronalSlice);
yImageLoc = viewer.coronalSlice.currentSlice;
} else if (viewer.insideScreenSlice(viewer.sagittalSlice, xLoc, yLoc, viewer.volume.getYDim(), viewer.volume.getZDim())) {
yImageLoc = viewer.convertScreenToImageCoordinateX(xLoc, viewer.sagittalSlice);
zImageLoc = viewer.convertScreenToImageCoordinateY(yLoc, viewer.sagittalSlice);
xImageLoc = viewer.sagittalSlice.currentSlice;
}
if(viewer.worldSpace) {
let coord = viewer.getWorldCoordinateAtIndex(xImageLoc, yImageLoc, zImageLoc, new papaya.core.Coordinate(0, 0, 0));
xImageLoc = coord.x
yImageLoc = coord.y
zImageLoc = coord.z
}
return [xImageLoc, yImageLoc, zImageLoc]
}
let convert_coord_to_offset = (x, y, z, volume)=> {
let viewer = papayaContainers[1].viewer
viewerOrigin = viewer.screenVolumes[0].volume.header.origin; // base image origin
viewerVoxelDims = viewer.screenVolumes[0].volume.header.voxelDimensions;
if(viewer.worldSpace) {
// x = (x - viewerOrigin.x) * viewerVoxelDims.xSize
// y = (viewerOrigin.y - y) * viewerVoxelDims.ySize
// z = (viewerOrigin.z - z) * viewerVoxelDims.zSize
var xTrans, yTrans, zTrans;
let worldMat = volume.transform.worldMat
xTrans = Math.round((x * worldMat[0][0]) + (y * worldMat[0][1]) + (z * worldMat[0][2]) +
(worldMat[0][3]));
yTrans = Math.round((x * worldMat[1][0]) + (y * worldMat[1][1]) + (z * worldMat[1][2]) +
(worldMat[1][3]));
zTrans = Math.round((x * worldMat[2][0]) + (y * worldMat[2][1]) + (z * worldMat[2][2]) +
(worldMat[2][3]));
return volume.transform.voxelValue.orientation.convertIndexToOffsetNative(xTrans, yTrans, zTrans)
} else {
return volume.transform.voxelValue.orientation.convertIndexToOffset(x, y, z)
}
// let coord = viewer.worldSpace ? viewer.getIndexCoordinateAtWorld(x, y, z, new papaya.core.Coordinate(0, 0, 0)) : {x:x, y:y, z:z}
// return volume.transform.voxelValue.orientation.convertIndexToOffset(coord.x, coord.y, coord.z)
}
let on_mouse_move = (event) => {
let viewer = papayaContainers[1].viewer
let selectedSlice = get_selected_slice(event)
if(selectedSlice != viewer.mainImage) {
set_auto_cursor()
} else if (drawing) {
set_brush_cursor()
}
if (!drawing || !dragging) {
return
}
let [x, y, z] = get_cursor_position(event)
let brush_value = parseInt(document.getElementById('brush_value').value)
let editable_image_index = get_editable_image_index()
let volume = editable_image_index>0 ? viewer.screenVolumes[editable_image_index].volume : viewer.volume
draw_voxel(x, y, z, volume, selectedSlice.sliceDirection, brush_value)
papayaContainers[1].viewer.drawViewer(true, false);
}
let on_mouse_down = (event) => {
let viewer = papayaContainers[1].viewer
if(viewer.isAltKeyDown && !event.altKey) {
viewer.isAltKeyDown = false
}
if(!drawing && !filling || viewer.isAltKeyDown) {
return
}
if(viewer.canvasRect == null) {
return
}
let selectedSlice = get_selected_slice(event)
if(selectedSlice != viewer.mainImage) {
return
}
dragging = true
let [x, y, z] = get_cursor_position(event)
let brush_value = parseInt(document.getElementById('brush_value').value)
adding_voxels = event.button == 0
let editable_image_index = get_editable_image_index()
if(editable_image_index < 0) {
return
}
if(filling) {
let fill3Dcheckbox = document.getElementById('fill3D')
let fill3D = fill3Dcheckbox != null && fill3Dcheckbox.checked
for(let i=adding_voxels?0:editable_image_index ; adding_voxels?i<viewer.screenVolumes.length:i==editable_image_index ; i++) {
let todo = [[x, y, z]]
let volume = viewer.screenVolumes[i].volume
let checkbox = document.querySelector('#toggle-visibility-buttons input[type="checkbox"][data-index="'+i+'"]')
let input_number = document.querySelector('#toggle-visibility-buttons input[type="number"][data-index="'+i+'"]')
let image_type = checkbox != null ? checkbox.getAttribute('data-image-type') : null
let threshold = input_number != null ? parseFloat(input_number.value) : (image_type != 'image' ? 1 : -1)
if(checkbox != null && checkbox.checked && threshold > 0) {
let offset = convert_coord_to_offset(x, y, z, volume)
let offsets = new Set()
if(volume.imageData.data[offset] >= threshold) {
offsets.add(offset)
while(todo.length > 0) {
flood_fill(todo, offsets, threshold, i, selectedSlice.sliceDirection, brush_value, fill3D)
}
}
}
}
viewer.drawViewer(true, false);
} else {
let volume = editable_image_index>0 ? viewer.screenVolumes[editable_image_index].volume : viewer.volume
draw_voxel(x, y, z, volume, selectedSlice.sliceDirection, brush_value)
}
viewer.drawViewer(true, false);
}
let on_mouse_up = (event) => {
dragging = false
}
let go_to_world_coordinates = (loc) => {
let coord = papayaContainers[0].viewer.getIndexCoordinateAtWorld(loc[0], loc[1], loc[2], new papaya.core.Coordinate());
papayaContainers[0].viewer.gotoCoordinate(coord)
}
let go_to_lesion = (lesion) => {
let loc = lesion['location_voxel']
if(loc == null) {
return
}
if(typeof(loc) == 'string') {
loc = JSON.parse(loc)
}
let orientation = papayaContainers[0].viewer.screenVolumes[0].volume.header.orientation
let coord = orientation.convertCoordinate({x: loc[0], y: loc[1], z: loc[2]}, new papaya.core.Coordinate())
papayaContainers[0].viewer.gotoCoordinate(coord)
}
let capitalize_first_letter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
}
let load_lesion = (i) => {
current_lesion_index = i;
if (current_lesion_index < 0) {
current_lesion_index = lesions.length - 1;
}
if (current_lesion_index >= lesions.length) {
current_lesion_index = 0;
}
let lesion = lesions[current_lesion_index]
let comment = document.getElementById('comment_value');
let valid = document.getElementById('valid_value');
comment.value = lesion.comment != null ? lesion.comment : ''
valid.checked = lesion.valid != null ? lesion.valid : false
valid.indeterminate = lesion.valid == null
if (task.fields != null) {
let fields_element = document.getElementById('fields')
while (fields_element.hasChildNodes()) {
fields_element.firstChild.remove()
}
for (let field of task.fields) {
let field_name = field.field || field.name
let field_container = document.createElement('div')
field_container.classList.add('flex-wrap')
let fiel_label = document.createElement('label')
fiel_label.innerText = capitalize_first_letter(field_name) + ':'
let field_span = null
if (field.list || field.lv_list) {
field_span = document.createElement('ul')
field_span.style = 'max-height: 200px; overflow: auto;'
let list = JSON.parse(lesion[field_name].replaceAll("'", '"'))
for (let item of list) {
let li = document.createElement('li')
li.innerText = item
field_span.appendChild(li)
}
} else if (field.editable && field.lv_type == 'bool') {
field_span = document.createElement('input');
field_span.setAttribute('type', 'checkbox')
field_span.setAttribute('data-field-name', field_name)
field_span.checked = lesion[field_name]
field_span.disabled = false
field_span.addEventListener('change', (event) => {
let lesion = lesions[current_lesion_index]
let field_name = event.target.getAttribute('data-field-name')
lesion[field_name] = event.target.checked
set_data_selected_row(field_name, event.target.checked)
})
} else {
field_span = document.createElement('span')
field_span.innerText = lesion[field_name]
}
field_span.id = field_name + '_value'
field_container.appendChild(fiel_label)
field_container.appendChild(field_span)
fields_element.appendChild(field_container)
}
}
let image_descriptions = lesion['images']
let image_parameters = []
let need_to_load = false
for (let image_description of image_descriptions) {
if (loaded_images.length == 0 || loaded_images.findIndex((i) => i.file_name.split('/').at(-1) == image_description.file) < 0) {
need_to_load = true;
break
}
}
let editable_image_index = get_editable_image_index()
let draw_tools = document.getElementById('draw_tools')
if(editable_image_index >= 0) {
draw_tools.classList.remove('hide')
} else {
draw_tools.classList.add('hide')
}
if(need_to_load && segmentation_is_modified) {
save_new_segmentation()
load_lesion(current_lesion_index)
return
}
let description = document.getElementById('description')
if (!need_to_load) {
go_to_lesion(lesion)
description.innerText = `${lesion['name']} - ${current_lesion_index + 1}/${lesions.length}`
return
}
// if(image_descriptions.length < 8) {
// let image_description = window.structuredClone(image_descriptions[image_descriptions.length-1])
// image_description.name = 'new_segmentation'
// // image_descriptions.push(image_description)
// image_descriptions = image_descriptions.concat([image_description])
// } else {
// alert('There are 8 or more images to load, cannot display new_segmentation image on top because of Papaya.js limitation.')
// }
description.innerText = 'loading ' + lesion['name'] + '...'
let visibility_checkboxes = document.getElementById('toggle-visibility-buttons')
visibility_checkboxes.replaceChildren();
show_loader()
let image_names = []
let ni = 0
for (let image_description of image_descriptions) {
if (ni>=11) {
break
}
ni++
let file_name = image_archive != null ? Object.keys(image_archive.files).find((f) => f.split('/').at(-1) == image_description.file) : image_description.file
image_names.push(file_name)
image_parameters.push({ name: image_description.name,
file_name: file_name,
parameters: image_description.parameters,
display: image_description.display,
threshold_slider: image_description.threshold_slider,
exclusive_button: image_description.exclusive_button,
editable: image_description.editable,
image_type: image_description.image_type
// fill_button: image_description.fill_button
})
}
if(images_url != null) {
load_lesion_viewer(image_names, image_parameters, lesion, current_lesion_index)
return
}
if(image_files != null && image_archive == null) {
images_to_display = image_names.map((file_name) => { return [...image_files].find((f) => f.name == file_name) })
// for all images_to_display which have a size of 0: remove them from images_to_display and image_parameters
images_to_display = images_to_display.filter((image) => image.size != 0)
image_parameters = image_parameters.filter((parameter) => images_to_display.findIndex((file) => file.name == parameter.file_name) >= 0)
load_lesion_viewer(images_to_display, image_parameters, lesion, current_lesion_index)
return
}
let promises = image_names.map((image_name) => { return image_archive.file(image_name).async('base64')} )
Promise.all(promises).then((images) => load_lesion_viewer(images, image_parameters, lesion, current_lesion_index))
}
let create_table = () => {
let rowData = []
let i = 0
for (let lesion of lesions) {
data = { name: lesion.name, description: lesion.description, comment: lesion.comment, valid: lesion.valid }
if (task.fields != null) {
for (let field of task.fields) {
let field_name = field.field || field.name
data[field_name] = lesion[field_name]
}
}
rowData.push(data)
}
// specify the columns
let columnDefs = [
{ field: 'name', sortable: true, filter: true, width: 150, resizable: true },
{ field: 'description', sortable: true, filter: true, flex: true, resizable: true },
// { field: 'n_methods_which_detected_lesion', sortable: true, resizable: true, filter: 'agNumberColumnFilter' },
];
if (task.fields != null) {
for (let field of task.fields) {
if (field.field == null && field.name != null) { // retro compatibility
field.field = field.name
}
if(field.editable && field.lv_type == 'bool') {
field.cellRenderer = 'checkboxRenderer'
}
columnDefs.push(field)
}
}
columnDefs.push({ field: 'valid', sortable: true, filter: true, resizable: true, editable: true, cellRenderer: 'checkboxRenderer' })
columnDefs.push({ field: 'comment', sortable: true, filter: true, resizable: true, editable: true })
// let the grid know which columns and what data to use
const gridOptions = {
columnDefs: columnDefs,
rowSelection: 'single',
rowData: rowData,
onRowSelected: (event) => {
if (!event.node.isSelected()) {
return