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

0% Statements 0/25
0% Branches 0/12
0% Functions 0/8
0% Lines 0/25

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                                                                                                                                                                                                                                                                                                                                                                       
import {
  getSegmentationRepresentation,
  getSegmentationRepresentations,
} from '../getSegmentationRepresentation';
import { setSegmentationRepresentationVisibility as _setSegmentationRepresentationVisibility } from '../setSegmentationRepresentationVisibility';
import { getSegmentationRepresentationVisibility as _getSegmentationRepresentationVisibility } from '../getSegmentationRepresentationVisibility';
import type { SegmentationRepresentations } from '../../../enums';
import { triggerSegmentationRenderBySegmentationId } from '../SegmentationRenderingEngine';
import { triggerSegmentationRepresentationModified } from '../triggerSegmentationEvents';
 
/**
 * Sets the visibility of a segmentation representation for a given viewport.
 *
 * @param viewportId - The ID of the viewport that the segmentation representation belongs to.
 * @param specifier - The specifier for the segmentation representation.
 * @param specifier.segmentationId - The ID of the segmentation.
 * @param specifier.type - The type of the segmentation representation.
 * @param visibility - The visibility state to set for the segmentation representation.
 * @returns void
 *
 * @remarks
 * This function sets the visibility of a specific segmentation representation for a given viewport.
 * if the type is not specified, the visibility of all representations of the segmentation will be set.
 */
function setSegmentationRepresentationVisibility(
  viewportId: string,
  specifier: {
    segmentationId: string;
    type?: SegmentationRepresentations;
  },
  visibility: boolean
): void {
  const representations = getSegmentationRepresentations(viewportId, specifier);
 
  if (!representations) {
    return;
  }
 
  representations.forEach((representation) => {
    _setSegmentationRepresentationVisibility(
      viewportId,
      {
        segmentationId: representation.segmentationId,
        type: representation.type,
      },
      visibility
    );
  });
}
 
/**
 * Gets the visibility of a segmentation representation for a given viewport.
 *
 * @param viewportId - The ID of the viewport that the segmentation representation belongs to.
 * @param segmentationId - The ID of the segmentation to get visibility for.
 * @param type - The type of segmentation representation.
 * @returns The visibility state of the segmentation representation, or undefined if not found.
 *
 */
function getSegmentationRepresentationVisibility(
  viewportId: string,
  specifier: {
    segmentationId: string;
    type: SegmentationRepresentations;
  }
): boolean | undefined {
  return _getSegmentationRepresentationVisibility(viewportId, specifier);
}
 
/**
 * Sets the visibility of a single segment for a specific viewport and segmentation representation.
 *
 * @param viewportId - The ID of the viewport.
 * @param specifier - The specifier for the segmentation representation.
 * @param specifier.segmentationId - The ID of the segmentation.
 * @param specifier.type - The type of the segmentation representation.
 * @param segmentIndex - The index of the segment to modify.
 * @param visibility - The visibility status to set for the segment.
 *
 * @remarks
 * If the type is not specified, the visibility of all representations of the segmentation will be set.
 * If the type is specified, the visibility of the exact type representation will be set.
 */
function setSegmentIndexVisibility(
  viewportId: string,
  specifier: {
    segmentationId: string;
    type?: SegmentationRepresentations;
  },
  segmentIndex: number,
  visibility: boolean
): void {
  const representations = getSegmentationRepresentations(viewportId, specifier);
 
  if (!representations) {
    return;
  }
 
  representations.forEach((representation) => {
    // If the representation does not have segments, we cannot set visibility
    if (!representation.segments || !representation.segments[segmentIndex]) {
      return;
    }
    representation.segments[segmentIndex].visible = visibility;
  });
 
  // Note: we should make sure to trigger here, since this does not go
  // through the SegmentationStateManager
  triggerSegmentationRenderBySegmentationId(specifier.segmentationId);
  triggerSegmentationRepresentationModified(
    viewportId,
    specifier.segmentationId
  );
}
 
/**
 * Retrieves the visibility of a specific segment for a given viewport and segmentation representation.
 *
 * @param viewportId - The ID of the viewport.
 * @param segmentationId - The ID of the segmentation.
 * @param type - The type of segmentation representation.
 * @param segmentIndex - The index of the segment to check.
 * @returns True if the segment is visible, false otherwise.
 */
function getSegmentIndexVisibility(
  viewportId: string,
  specifier: {
    segmentationId: string;
    type: SegmentationRepresentations;
  },
  segmentIndex: number
): boolean {
  const hiddenSegments = getHiddenSegmentIndices(viewportId, specifier);
 
  return !hiddenSegments.has(segmentIndex);
}
 
/**
 * Retrieves the hidden segment indices for a given viewport and segmentation representation.
 *
 * @param viewportId - The ID of the viewport.
 * @param specifier - The specifier for the segmentation representation.
 * @param specifier.segmentationId - The ID of the segmentation.
 * @param specifier.type - The type of the segmentation representation.
 * @returns A Set of hidden segment indices.
 */
function getHiddenSegmentIndices(
  viewportId: string,
  specifier: {
    segmentationId: string;
    type: SegmentationRepresentations;
  }
): Set<number> {
  const representation = getSegmentationRepresentation(viewportId, specifier);
 
  if (!representation) {
    return new Set();
  }
 
  const segmentsHidden = Object.entries(representation.segments).reduce(
    (acc, [segmentIndex, segment]) => {
      if (!segment.visible) {
        acc.add(Number(segmentIndex));
      }
      return acc;
    },
    new Set<number>()
  );
 
  return segmentsHidden;
}
 
export {
  setSegmentationRepresentationVisibility,
  getSegmentationRepresentationVisibility,
  setSegmentIndexVisibility,
  getSegmentIndexVisibility,
  getHiddenSegmentIndices,
};