Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi committed Feb 21, 2025
1 parent aa96ee4 commit 2db10c4
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 128 deletions.
77 changes: 55 additions & 22 deletions bun.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions common/reviews/api/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,9 @@ interface FlipDirection {
flipVertical?: boolean;
}

// @public (undocumented)
function fnv1aHash(str: string): string;

// @public (undocumented)
class FrameRange {
// (undocumented)
Expand Down Expand Up @@ -3966,6 +3969,7 @@ declare namespace utilities {
scaleRGBTransferFunction as scaleRgbTransferFunction,
triggerEvent,
imageIdToURI,
fnv1aHash,
metadataProvider as calibratedPixelSpacingMetadataProvider,
clamp,
uuidv4,
Expand Down
18 changes: 18 additions & 0 deletions common/reviews/api/labelmap-interpolation.api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## API Report File for "@cornerstonejs/labelmap-interpolation"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts

// @public (undocumented)
export function interpolate({ segmentationId, segmentIndex, configuration, }: {
segmentationId: string;
segmentIndex: number;
configuration?: MorphologicalContourInterpolationOptions & {
preview: boolean;
};
}): Promise<void>;

// (No @packageDocumentation comment for this package)

```
10 changes: 0 additions & 10 deletions common/reviews/api/tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3172,15 +3172,6 @@ type InteractionTypes = 'Mouse' | 'Touch';
// @public (undocumented)
function internalAddRepresentationData({ segmentationId, type, data, }: AddRepresentationData): void;

// @public (undocumented)
function interpolateLabelmap({ segmentationId, segmentIndex, configuration, }: {
segmentationId: string;
segmentIndex: number;
configuration?: MorphologicalContourInterpolationOptions & {
preview: boolean;
};
}): Promise<void>;

// @public (undocumented)
type InterpolationROIAnnotation = ContourAnnotation & ContourSegmentationAnnotationData & {
metadata: {
Expand Down Expand Up @@ -5309,7 +5300,6 @@ declare namespace segmentation_2 {
growCut,
LabelmapMemo,
IslandRemoval,
interpolateLabelmap,
getOrCreateSegmentationVolume,
getStatistics
}
Expand Down
4 changes: 3 additions & 1 deletion packages/ai/src/ONNXSegmentationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export default class ONNXSegmentationController {
this.currentImage = null;
this.viewport = viewport;

this.tool = new LabelmapBaseTool(
const brushInstance = new LabelmapBaseTool(
{},
{
configuration: {
Expand All @@ -352,6 +352,8 @@ export default class ONNXSegmentationController {
}
);

this.tool = brushInstance;

desiredImage.imageId =
viewport.getCurrentImageId?.() || viewport.getViewReferenceId();
if (desiredImage.imageId.startsWith('volumeId:')) {
Expand Down
18 changes: 2 additions & 16 deletions packages/core/src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import imageIdToURI from '../utilities/imageIdToURI';
import eventTarget from '../eventTarget';
import Events from '../enums/Events';
import { ImageQualityStatus } from '../enums';
import fnv1aHash from '../utilities/fnv1aHash';

const ONE_GB = 1073741824;

Expand Down Expand Up @@ -52,7 +53,7 @@ class Cache {

let combinedHash = 0x811c9dc5;
for (const id of imageURIs) {
const idHash = this._fnv1aHash(id);
const idHash = fnv1aHash(id);
for (let i = 0; i < idHash.length; i++) {
combinedHash ^= idHash.charCodeAt(i);
combinedHash +=
Expand Down Expand Up @@ -1311,21 +1312,6 @@ class Cache {

return cachedGeometry.geometryLoadObject;
};

/**
* Helper function to generate a hash for a string using FNV-1a algorithm
* @param str - string to hash
* @returns the hashed string
*/
private _fnv1aHash(str: string): string {
let hash = 0x811c9dc5;
for (let i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i);
hash +=
(hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return (hash >>> 0).toString(36);
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/utilities/fnv1aHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Generates a hash for a string using FNV-1a algorithm
* @param str - string to hash
* @returns the hashed string in base 36
*/
export default function fnv1aHash(str: string): string {
let hash = 0x811c9dc5;
for (let i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i);
hash +=
(hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return (hash >>> 0).toString(36);
}
2 changes: 2 additions & 0 deletions packages/core/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import * as color from './color';
import { deepEqual } from './deepEqual';
import type { IViewport } from '../types/IViewport';
import FrameRange from './FrameRange';
import fnv1aHash from './fnv1aHash';

// solving the circular dependency issue
import { _getViewportModality } from './getViewportModality';
Expand Down Expand Up @@ -108,6 +109,7 @@ export {
scaleRgbTransferFunction,
triggerEvent,
imageIdToURI,
fnv1aHash,
calibratedPixelSpacingMetadataProvider,
clamp,
uuidv4,
Expand Down
34 changes: 16 additions & 18 deletions packages/labelmap-interpolation/src/workers/interpolationWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@ import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
/**
* Dynamically imports ITK WASM modules needed for labelmap interpolation
* @param moduleId - The module ID to import ('itk-wasm' or '@itk-wasm/morphological-contour-interpolation')
* @param enableLabelmapInterpolation - Flag indicating if labelmap interpolation is enabled
* @returns Promise that resolves to the imported module
*/
async function peerImport(moduleId) {
switch (moduleId) {
case 'itk-wasm':
return import('itk-wasm');
case '@itk-wasm/morphological-contour-interpolation':
return import('@itk-wasm/morphological-contour-interpolation');
default:
throw new Error(`Unknown module ID: ${moduleId}`);
try {
switch (moduleId) {
case 'itk-wasm':
return import('itk-wasm');
case '@itk-wasm/morphological-contour-interpolation':
return import('@itk-wasm/morphological-contour-interpolation');
default:
throw new Error(`Unknown module ID: ${moduleId}`);
}
} catch (error) {
console.warn(`Error importing ${moduleId}:`, error);
return null;
}
}

const computeWorker = {
getITKImage: async (args) => {
const { imageData, options, enableLabelmapInterpolation } = args;
const { imageData, options } = args;

const { imageName, scalarData } = options;

let Image, ImageType, IntTypes, FloatTypes, PixelTypes;

try {
const itkModule = await peerImport(
'itk-wasm',
enableLabelmapInterpolation
);
const itkModule = await peerImport('itk-wasm');
if (!itkModule) {
throw new Error('Module not found');
}
Expand Down Expand Up @@ -87,16 +88,14 @@ const computeWorker = {
return image;
},
interpolateLabelmap: async (args) => {
const { segmentationInfo, configuration, enableLabelmapInterpolation } =
args;
const { segmentationInfo, configuration } = args;
const { scalarData, dimensions, spacing, origin, direction } =
segmentationInfo;

let itkModule;
try {
itkModule = await peerImport(
'@itk-wasm/morphological-contour-interpolation',
enableLabelmapInterpolation
'@itk-wasm/morphological-contour-interpolation'
);
if (!itkModule) {
throw new Error('Module not found');
Expand Down Expand Up @@ -130,7 +129,6 @@ const computeWorker = {
imageName: 'interpolation',
scalarData: scalarData,
},
enableLabelmapInterpolation,
});

if (!inputImage) {
Expand Down
17 changes: 0 additions & 17 deletions packages/tools/src/config.ts

This file was deleted.

6 changes: 2 additions & 4 deletions packages/tools/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { annotationInterpolationEventDispatcher } from './eventDispatchers';
import * as ToolGroupManager from './store/ToolGroupManager';
import { defaultSegmentationStateManager } from './stateManagement/segmentation/SegmentationStateManager';
import segmentationRepresentationModifiedListener from './eventListeners/segmentation/segmentationRepresentationModifiedListener';
import { setConfig } from './config';

let csToolsInitialized = false;

/**
Expand All @@ -25,13 +25,11 @@ let csToolsInitialized = false;
* @param defaultConfiguration - A configuration object that will be used to
* initialize the tool.
*/
export function init(defaultConfiguration): void {
export function init(defaultConfiguration = {}): void {
if (csToolsInitialized) {
return;
}

setConfig(defaultConfiguration);

_addCornerstoneEventListeners();
_addCornerstoneToolsEventListeners();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getWebWorkerManager } from '@cornerstonejs/core';
import { getConfig } from '../../../config';

let registered = false;

Expand Down Expand Up @@ -30,7 +29,6 @@ export function registerPolySegWorker() {
enabled: true,
idleTimeThreshold: 2000,
},
enablePolySeg: getConfig().enablePolySeg,
};

workerManager.registerWorker('polySeg', workerFn, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ export default {
segmentationId,
preview,
} = operationData || {};

if (previewSegmentIndex === undefined) {
return;
}

const segmentIndex = preview?.segmentIndex ?? operationData.segmentIndex;
if (!previewVoxelManager || previewVoxelManager.modifiedSlices.size === 0) {
return;
Expand Down
2 changes: 0 additions & 2 deletions packages/tools/src/utilities/registerComputeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getWebWorkerManager } from '@cornerstonejs/core';
import { getConfig } from '../config';
let registered = false;

export function registerComputeWorker() {
Expand Down Expand Up @@ -29,7 +28,6 @@ export function registerComputeWorker() {
enabled: true,
idleTimeThreshold: 2000,
},
enableLabelmapInterpolation: getConfig().enableLabelmapInterpolation,
};

workerManager.registerWorker('compute', workerFn, options);
Expand Down
Loading

0 comments on commit 2db10c4

Please sign in to comment.