All files / packages/tools/src/stateManagement/segmentation/config segmentationVisibility.ts

30% Statements 12/40
11.11% Branches 2/18
30% Functions 3/10
30.76% Lines 12/39

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 196 197 198 199 200 201 202 203                                                                                                                                                  2x   2x   2x       2x       2x 2x     2x     2x     2x                                                                                                                                                                               2x         2x       2x                    
import * as SegmentationState from '../../../stateManagement/segmentation/segmentationState';
import { getSegmentationRepresentations } from '../../../stateManagement/segmentation/segmentationState';
import { ToolGroupSpecificRepresentation } from '../../../types/SegmentationStateTypes';
import { getUniqueSegmentIndices } from '../../../utilities/segmentation';
import { triggerSegmentationRepresentationModified } from '../triggerSegmentationEvents';
 
/**
 * 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 = getUniqueSegmentIndices(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
  );
 
  Iif (!representation) {
    return;
  }
 
  const { segmentsHidden, segmentationId } = representation;
  const indices = getUniqueSegmentIndices(segmentationId);
 
  // Create a set that contains all segments indices
  const indicesSet = new Set(indices);
 
  // Remove a indices that are hidden
  segmentsHidden.forEach((segmentIndex) => indicesSet.delete(segmentIndex));
 
  // Check if there is at least one segment visible
  return !!indicesSet.size;
}
 
/**
 * 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
  );
}
 
/**
 * @param toolGroupId - The Id of the tool group that contains the segmentation
 * @param segmentationRepresentationUID - The id of the segmentation representation that contains the segment
 * @param segmentIndex - Index of the segment that will be updated
 * @param visibility - True to show the segment or false to hide it
 * @returns True if the segment is visible or false otherwise
 */
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
  );
}
 
/**
 * @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 segmentIndex - Index of the segment
 * @returns True if the segment is visible or false otherwise
 */
function getSegmentVisibility(
  toolGroupId: string,
  segmentationRepresentationUID: string,
  segmentIndex: number
): boolean {
  const segRepresentation =
    SegmentationState.getSegmentationRepresentationByUID(
      toolGroupId,
      segmentationRepresentationUID
    );
 
  Iif (!segRepresentation) {
    return false;
  }
 
  return !segRepresentation.segmentsHidden.has(segmentIndex);
}
 
export {
  setSegmentationVisibility,
  getSegmentationVisibility,
  setSegmentVisibility,
  setSegmentsVisibility,
  getSegmentVisibility,
};