All files / tools/src config.ts

76.92% Statements 10/13
75% Branches 3/4
75% Functions 3/4
76.92% Lines 10/13

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                                                                                                                                                                                                                428x           8x             428x                   428x             907x               907x 907x 16x 16x     907x    
import type { ContourSegmentationData } from './types';
import type { Types } from '@cornerstonejs/core';
import type { LabelmapSegmentationData } from './types/LabelmapTypes';
import type { SurfaceSegmentationData } from './types/SurfaceTypes';
import type SegmentationRepresentations from './enums/SegmentationRepresentations';
import { eventTarget, triggerEvent } from '@cornerstonejs/core';
import Events from './enums/Events';
 
export type SurfacesInfo = {
  id: string;
  points: number[];
  polys: number[];
  segmentIndex: number;
};
 
/**
 * Options for converting between segmentation representations using PolySeg
 * set
 */
export type PolySegConversionOptions = {
  /** Optional array of segment indices to convert */
  segmentIndices?: number[];
  /** ID of the segmentation to convert */
  segmentationId?: string;
  /** Viewport to use for conversion */
  viewport?: Types.IStackViewport | Types.IVolumeViewport;
};
 
/**
 * Interface for the PolySeg add-on that handles segmentation representation conversions
 */
type ComputeRepresentationFn<T> = (
  segmentationId: string,
  options: PolySegConversionOptions
) => Promise<T>;
 
/**
 * The result of the surface clipping
 */
export type SurfaceClipResult = {
  points: number[];
  lines: number[];
  numberOfCells: number;
};
 
type PolySegAddOn = {
  /** Checks if a representation type can be computed for a segmentation */
  canComputeRequestedRepresentation: (
    segmentationId: string,
    representationType: SegmentationRepresentations
  ) => boolean;
 
  /** Initializes the PolySeg add-on */
  init: () => void;
 
  /** Computes different segmentation representation data */
  computeContourData: ComputeRepresentationFn<ContourSegmentationData>;
  computeLabelmapData: ComputeRepresentationFn<LabelmapSegmentationData>;
  computeSurfaceData: ComputeRepresentationFn<SurfaceSegmentationData>;
 
  /** Updates different segmentation representation data */
  updateSurfaceData: ComputeRepresentationFn<void>;
 
  /** Clips and caches surfaces for a viewport */
  clipAndCacheSurfacesForViewport: (
    surfacesInfo: SurfacesInfo[],
    viewport: Types.IVolumeViewport
  ) => Promise<Map<number, Map<string, SurfaceClipResult>>>;
 
  /** Extracts contour data from the given polyDataCache */
  extractContourData: (
    polyDataCache: Map<number, Map<string, SurfaceClipResult>>
  ) => Map<number, SurfaceClipResult[]>;
 
  /** Creates and adds contour segmentations from clipped surfaces */
  createAndAddContourSegmentationsFromClippedSurfaces: (
    rawContourData: Map<number, SurfaceClipResult[]>,
    viewport: Types.IStackViewport | Types.IVolumeViewport,
    segmentationId: string
  ) => Map<number, Set<string>>;
};
 
/**
 * Available add-ons that can be configured
 */
type AddOns = {
  polySeg: PolySegAddOn;
};
 
type ComputeWorkerConfig = {
  autoTerminateOnIdle?: {
    enabled: boolean;
    idleTimeThreshold?: number;
  };
};
 
/**
 * Configuration type containing add-ons
 */
export type Config = {
  addons: AddOns;
  computeWorker?: ComputeWorkerConfig;
};
 
let config = {} as Config;
 
/**
 * Gets the current configuration
 */
export function getConfig(): Config {
  return config;
}
 
/**
 * Sets a new configuration
 */
export function setConfig(newConfig: Config): void {
  config = newConfig;
}
 
/**
 * Gets configured add-ons
 */
export function getAddOns(): AddOns {
  return config.addons;
}
 
let polysegInitialized = false;
 
/**
 * Gets the PolySeg add-on, initializing it if needed
 * @returns The PolySeg add-on instance or null if not configured
 */
export function getPolySeg() {
  Iif (!config.addons?.polySeg) {
    console.warn(
      'PolySeg add-on not configured. This will prevent automatic conversion between segmentation representations (labelmap, contour, surface). To enable these features, install @cornerstonejs/polymorphic-segmentation and register it during initialization: cornerstoneTools.init({ addons: { polySeg } }).'
    );
 
    return null;
  }
 
  const polyseg = config.addons.polySeg;
  if (!polysegInitialized) {
    polyseg.init();
    polysegInitialized = true;
  }
 
  return polyseg;
}