Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | import { cache, Types } from '@cornerstonejs/core'; import * as SegmentationState from '../../../stateManagement/segmentation/segmentationState'; import { getSegmentationRepresentations } from '../../../stateManagement/segmentation/segmentationState'; import { ToolGroupSpecificRepresentation } from '../../../types/SegmentationStateTypes'; import { triggerSegmentationRepresentationModified } from '../triggerSegmentationEvents'; import SegmentationRepresentations from '../../../enums/SegmentationRepresentations'; function getSegmentationIndices(segmentationId) { const segmentation = SegmentationState.getSegmentation(segmentationId); if (segmentation.type === SegmentationRepresentations.Labelmap) { const volume = cache.getVolume(segmentationId); const scalarData = volume.getScalarData(); const keySet = {}; for (let i = 0; i < scalarData.length; i++) { const segmentIndex = scalarData[i]; if (segmentIndex !== 0 && !keySet[segmentIndex]) { keySet[segmentIndex] = true; } } return Object.keys(keySet).map((it) => parseInt(it, 10)); } else if (segmentation.type === SegmentationRepresentations.Contour) { const geometryIds = segmentation.representationData.CONTOUR?.geometryIds; if (!geometryIds) { throw new Error( `No geometryIds found for segmentationId ${segmentationId}` ); } return geometryIds.map((geometryId) => { const geometry = cache.getGeometry(geometryId) as Types.IGeometry; return (geometry.data as Types.IContourSet).getSegmentIndex(); }); } } /** * Set the visibility of a segmentation representation for a given tool group. It fires * a SEGMENTATION_REPRESENTATION_MODIFIED event. Visibility true will show all segments * and visibility false will hide all segments" * * @triggers SEGMENTATION_REPRESENTATION_MODIFIED * @param toolGroupId - The Id of the tool group that contains the segmentation. * @param segmentationRepresentationUID - The id of the segmentation representation to modify its visibility. * @param visibility - boolean */ function setSegmentationVisibility( toolGroupId: string, segmentationRepresentationUID: string, visibility: boolean ): void { const toolGroupSegmentationRepresentations = getSegmentationRepresentations(toolGroupId); if (!toolGroupSegmentationRepresentations) { return; } const representation = toolGroupSegmentationRepresentations.find( (representation: ToolGroupSpecificRepresentation) => representation.segmentationRepresentationUID === segmentationRepresentationUID ); if (!representation) { return; } const { segmentsHidden, segmentationId } = representation; const indices = getSegmentationIndices(segmentationId); // if visibility is set to be true, we need to remove all the segments // from the segmentsHidden set, otherwise we need to add all the segments // to the segmentsHidden set if (visibility) { segmentsHidden.clear(); } else { indices.forEach((index) => { segmentsHidden.add(index); }); } triggerSegmentationRepresentationModified( toolGroupId, representation.segmentationRepresentationUID ); } /** * Get the visibility of a segmentation data for a given tool group. * * @param toolGroupId - The Id of the tool group that the segmentation * data belongs to. * @param segmentationRepresentationUID - The id of the segmentation data to get * @returns A boolean value that indicates whether the segmentation data is visible or * not on the toolGroup */ function getSegmentationVisibility( toolGroupId: string, segmentationRepresentationUID: string ): boolean | undefined { const toolGroupSegmentationRepresentations = getSegmentationRepresentations(toolGroupId); const representation = toolGroupSegmentationRepresentations.find( (representation: ToolGroupSpecificRepresentation) => representation.segmentationRepresentationUID === segmentationRepresentationUID ); if (!representation) { return; } const { segmentsHidden } = representation; return segmentsHidden.size === 0; } /** * Set the visibility of the given segment indices to the given visibility. This * is a helper to set the visibility of multiple segments at once and reduces * the number of events fired. * * @param toolGroupId - The tool group id of the segmentation representation. * @param segmentationRepresentationUID - The UID of the segmentation * representation. * @param segmentIndices - The indices of the segments to be hidden/shown. * @param visibility - The visibility to set the segments to. * */ function setSegmentsVisibility( toolGroupId: string, segmentationRepresentationUID: string, segmentIndices: number[], visibility: boolean ): void { const segRepresentation = SegmentationState.getSegmentationRepresentationByUID( toolGroupId, segmentationRepresentationUID ); if (!segRepresentation) { return; } segmentIndices.forEach((segmentIndex) => { visibility ? segRepresentation.segmentsHidden.delete(segmentIndex) : segRepresentation.segmentsHidden.add(segmentIndex); }); triggerSegmentationRepresentationModified( toolGroupId, segmentationRepresentationUID ); } function setSegmentVisibility( toolGroupId: string, segmentationRepresentationUID: string, segmentIndex: number, visibility: boolean ): void { const segRepresentation = SegmentationState.getSegmentationRepresentationByUID( toolGroupId, segmentationRepresentationUID ); if (!segRepresentation) { return; } visibility ? segRepresentation.segmentsHidden.delete(segmentIndex) : segRepresentation.segmentsHidden.add(segmentIndex); triggerSegmentationRepresentationModified( toolGroupId, segmentationRepresentationUID ); } export { setSegmentationVisibility, getSegmentationVisibility, setSegmentVisibility, setSegmentsVisibility, }; |