|
| 1 | +<script setup lang="ts"> |
| 2 | +import { inject, watch, computed, toRefs } from 'vue'; |
| 3 | +import type { ReadonlyVec3 } from 'gl-matrix'; |
| 4 | +import { onVTKEvent } from '@/src/composables/onVTKEvent'; |
| 5 | +import { VtkViewContext } from '@/src/components/vtk/context'; |
| 6 | +import { useCurrentImage } from '@/src/composables/useCurrentImage'; |
| 7 | +import vtkPointPicker from '@kitware/vtk.js/Rendering/Core/PointPicker'; |
| 8 | +import { useSliceRepresentation } from '@/src/core/vtk/useSliceRepresentation'; |
| 9 | +import { useImageStore } from '@/src/store/datasets-images'; |
| 10 | +import { useLayersStore } from '@/src/store/datasets-layers'; |
| 11 | +import { useSegmentGroupStore } from '@/src/store/segmentGroups'; |
| 12 | +import { useProbeStore } from '@/src/store/probe'; |
| 13 | +
|
| 14 | +type SliceRepresentationType = ReturnType<typeof useSliceRepresentation>; |
| 15 | +
|
| 16 | +const props = defineProps<{ |
| 17 | + baseRep: SliceRepresentationType; |
| 18 | + layerReps: SliceRepresentationType[]; |
| 19 | + segmentGroupsReps: SliceRepresentationType[]; |
| 20 | +}>(); |
| 21 | +
|
| 22 | +const { baseRep, layerReps, segmentGroupsReps } = toRefs(props); |
| 23 | +const view = inject(VtkViewContext); |
| 24 | +if (!view) throw new Error('No VtkView'); |
| 25 | +
|
| 26 | +const { |
| 27 | + currentImageID, |
| 28 | + currentImageData, |
| 29 | + currentImageMetadata, |
| 30 | + currentLayers, |
| 31 | +} = useCurrentImage(); |
| 32 | +const imageStore = useImageStore(); |
| 33 | +const layersStore = useLayersStore(); |
| 34 | +const segmentGroupStore = useSegmentGroupStore(); |
| 35 | +const probeStore = useProbeStore(); |
| 36 | +
|
| 37 | +// Helper functions to build a unified sample set |
| 38 | +const getBaseSlice = () => { |
| 39 | + if (!currentImageData.value || !currentImageID.value) return null; |
| 40 | + return { |
| 41 | + type: 'layer', |
| 42 | + id: currentImageID.value, |
| 43 | + name: currentImageMetadata.value.name, |
| 44 | + rep: baseRep.value, |
| 45 | + image: currentImageData.value, |
| 46 | + }; |
| 47 | +}; |
| 48 | +
|
| 49 | +const getLayers = () => |
| 50 | + layerReps.value |
| 51 | + .map((rep, index) => { |
| 52 | + const layer = currentLayers.value[index]; |
| 53 | + if (!layer) return null; |
| 54 | + return { |
| 55 | + type: 'layer', |
| 56 | + id: layer.id, |
| 57 | + name: imageStore.metadata[layer.selection].name, |
| 58 | + rep, |
| 59 | + image: layersStore.layerImages[layer.id], |
| 60 | + }; |
| 61 | + }) |
| 62 | + .filter(Boolean); |
| 63 | +
|
| 64 | +const getSegments = () => { |
| 65 | + if (!currentImageID.value) return []; |
| 66 | + const parentGroups = segmentGroupStore.orderByParent[currentImageID.value]; |
| 67 | + if (!parentGroups) return []; |
| 68 | + return segmentGroupsReps.value |
| 69 | + .map((rep, index) => { |
| 70 | + const groupId = parentGroups[index]; |
| 71 | + if (!groupId) return null; |
| 72 | + const meta = segmentGroupStore.metadataByID[groupId]; |
| 73 | + return { |
| 74 | + type: 'segmentGroup', |
| 75 | + id: groupId, |
| 76 | + name: meta.name, |
| 77 | + rep, |
| 78 | + segments: meta.segments, |
| 79 | + image: segmentGroupStore.dataIndex[groupId], |
| 80 | + }; |
| 81 | + }) |
| 82 | + .filter(Boolean); |
| 83 | +}; |
| 84 | +
|
| 85 | +const sampleSet = computed(() => { |
| 86 | + const base = getBaseSlice(); |
| 87 | + if (!base) return []; |
| 88 | + return [...getSegments(), ...getLayers(), base]; |
| 89 | +}); |
| 90 | +
|
| 91 | +const pointPicker = vtkPointPicker.newInstance(); |
| 92 | +pointPicker.setPickFromList(true); |
| 93 | +
|
| 94 | +watch( |
| 95 | + sampleSet, |
| 96 | + (samples) => { |
| 97 | + pointPicker.setPickList( |
| 98 | + samples.length > 0 && samples[0] ? [samples[0].rep.actor] : [] |
| 99 | + ); |
| 100 | + }, |
| 101 | + { immediate: true } |
| 102 | +); |
| 103 | +
|
| 104 | +const getImageSamples = (x: number, y: number) => { |
| 105 | + const firstToSample = sampleSet.value[0]; |
| 106 | + if (!firstToSample) return undefined; |
| 107 | +
|
| 108 | + pointPicker.pick([x, y, 1.0], view.renderer); |
| 109 | + if (pointPicker.getActors().length === 0) return undefined; |
| 110 | +
|
| 111 | + const ijk = pointPicker.getPointIJK() as unknown as ReadonlyVec3; |
| 112 | + const samples = sampleSet.value.map((item: any) => { |
| 113 | + const dims = item.image.getDimensions(); |
| 114 | + const scalarData = item.image.getPointData().getScalars(); |
| 115 | + const index = dims[0] * dims[1] * ijk[2] + dims[0] * ijk[1] + ijk[0]; |
| 116 | + const scalars = scalarData.getTuple(index) as number[]; |
| 117 | + const baseInfo = { id: item.id, name: item.name }; |
| 118 | +
|
| 119 | + if (item.type === 'segmentGroup') { |
| 120 | + return { |
| 121 | + ...baseInfo, |
| 122 | + displayValues: scalars.map( |
| 123 | + (v) => item.segments.byValue[v]?.name || 'Background' |
| 124 | + ), |
| 125 | + }; |
| 126 | + } |
| 127 | + return { ...baseInfo, displayValues: scalars }; |
| 128 | + }); |
| 129 | +
|
| 130 | + const position = firstToSample.image.indexToWorld(ijk); |
| 131 | +
|
| 132 | + return { |
| 133 | + pos: position, |
| 134 | + samples, |
| 135 | + }; |
| 136 | +}; |
| 137 | +
|
| 138 | +onVTKEvent(view.interactor, 'onMouseMove', (event: any) => { |
| 139 | + const samples = getImageSamples(event.position.x, event.position.y); |
| 140 | + probeStore.updateProbeData(samples); |
| 141 | +}); |
| 142 | +
|
| 143 | +onVTKEvent(view.interactor, 'onPointerLeave', () => { |
| 144 | + probeStore.clearProbeData(); |
| 145 | +}); |
| 146 | +
|
| 147 | +watch([currentImageID, sampleSet], () => { |
| 148 | + probeStore.clearProbeData(); |
| 149 | +}); |
| 150 | +</script> |
0 commit comments