All files / ai/src LabelmapSlicePropagationTool.ts

0% Statements 0/50
0% Branches 0/22
0% Functions 0/8
0% Lines 0/50

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                                                                                                                                                                                                                                                                                                           
import { getEnabledElementByViewportId } from '@cornerstonejs/core';
import { LabelmapBaseTool } from '@cornerstonejs/tools';
import ONNXSegmentationController from './ONNXSegmentationController';
 
/**
 * Represents a tool used for segment selection and AI-assisted segmentation.
 * Integrates with ONNX models for automated segmentation tasks.
 */
class LabelmapSlicePropagationTool extends LabelmapBaseTool {
  static toolName = 'LabelmapSlicePropagation';
  private segmentAI: ONNXSegmentationController;
  private _initialized = false;
  private _modelInitialized = false;
 
  constructor(
    toolProps = {},
    defaultToolProps = {
      supportedInteractionTypes: ['Mouse', 'Touch'],
      configuration: {
        sourceViewportId: '',
        autoSegmentMode: true,
        modelName: 'sam_b',
        enabled: false,
        models: {
          sam_b: [
            {
              name: 'sam-b-encoder',
              url: 'https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b_01ec64.encoder-fp16.onnx',
              size: 180,
              key: 'encoder',
            },
            {
              name: 'sam-b-decoder',
              url: 'https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b_01ec64.decoder.onnx',
              size: 17,
              key: 'decoder',
            },
          ],
        },
        numRandomPoints: 5,
        searchBreadth: 10,
      },
    }
  ) {
    super(toolProps, defaultToolProps);
  }
 
  _init = async () => {
    const { configuration } = this;
 
    if (!configuration.enabled || !configuration.sourceViewportId) {
      return;
    }
 
    // Create the controller only once
    if (!this.segmentAI) {
      this.segmentAI = new ONNXSegmentationController({
        autoSegmentMode: configuration.autoSegmentMode,
        models: configuration.models,
        modelName: configuration.modelName,
        numRandomPoints: configuration.numRandomPoints,
        searchBreadth: configuration.searchBreadth,
      });
    }
 
    // Enable the controller
    this.segmentAI.enabled = true;
 
    // Initialize the model only once
    if (!this._modelInitialized) {
      await this.segmentAI.initModel();
      this._modelInitialized = true;
    }
 
    const { sourceViewportId } = this.configuration;
    const enabledElement = getEnabledElementByViewportId(sourceViewportId);
 
    if (!enabledElement) {
      console.debug(
        'No enabled element found for viewportId:',
        sourceViewportId
      );
      return;
    }
 
    const { viewport } = enabledElement;
    this.segmentAI.initViewport(viewport);
    this._initialized = true;
  };
 
  onSetToolConfiguration = (): void => {
    this._init();
  };
 
  onSetToolEnabled = async (): Promise<void> => {
    this.configuration.enabled = true;
    // Just enable the existing controller without reloading the model
    if (this.segmentAI) {
      this.segmentAI.enabled = true;
    }
    this._init();
  };
 
  onSetToolDisabled = (): void => {
    this.configuration.enabled = false;
    if (this.segmentAI) {
      this.segmentAI.enabled = false;
    }
  };
 
  onSetToolPassive = (): void => {
    this.configuration.enabled = false;
    if (this.segmentAI) {
      this.segmentAI.enabled = false;
    }
  };
 
  /**
   * Accept the current segmentation preview
   */
  acceptPreview = (): void => {
    const { sourceViewportId } = this.configuration;
    const enabledElement = getEnabledElementByViewportId(sourceViewportId);
 
    if (!enabledElement) {
      return;
    }
 
    const { viewport } = enabledElement;
 
    this.segmentAI.tool.acceptPreview(viewport.element);
  };
 
  /**
   * Reject the current segmentation preview
   */
  rejectPreview = (): void => {
    const { sourceViewportId } = this.configuration;
    const enabledElement = getEnabledElementByViewportId(sourceViewportId);
 
    if (!enabledElement) {
      return;
    }
 
    this.segmentAI.rejectPreview(enabledElement.viewport.element);
  };
}
 
export default LabelmapSlicePropagationTool;