Skip to content

Commit 8c56ed1

Browse files
committed
refactor(gaussianSmooth): simplfy process interface
1 parent b5de3d6 commit 8c56ed1

5 files changed

Lines changed: 30 additions & 33 deletions

File tree

src/components/ProcessControls.vue

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { computed } from 'vue';
1919
import {
2020
ProcessType,
2121
useProcessStore,
22-
type ProcessAlgorithm,
2322
} from '../store/tools/process';
2423
import ProcessTypeSelector from './ProcessTypeSelector.vue';
2524
import ProcessWorkflow from './ProcessWorkflow.vue';
@@ -34,16 +33,6 @@ const gaussianSmoothStore = useGaussianSmoothStore();
3433
3534
const activeProcessType = computed(() => processStore.activeProcessType);
3635
37-
// Create algorithm adapters for the ProcessWorkflow component
38-
const fillBetweenAlgorithm: ProcessAlgorithm = {
39-
compute: async (segImage, activeSegment) => {
40-
return fillBetweenStore.computeAlgorithm(segImage, activeSegment);
41-
},
42-
};
43-
44-
const gaussianSmoothAlgorithm: ProcessAlgorithm = {
45-
compute: async (segImage, activeSegment) => {
46-
return gaussianSmoothStore.computeAlgorithm(segImage, activeSegment);
47-
},
48-
};
36+
const fillBetweenAlgorithm = fillBetweenStore.computeAlgorithm;
37+
const gaussianSmoothAlgorithm = gaussianSmoothStore.computeAlgorithm;
4938
</script>

src/core/tools/paint/gaussianSmooth.worker.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Comlink from 'comlink';
22
import { TypedArray } from '@kitware/vtk.js/types';
3+
import { createTypedArrayLike } from '@/src/utils';
34

45
export interface GaussianSmoothParams {
56
sigma: number;
@@ -237,7 +238,7 @@ function copySubVolumeBack(
237238

238239
if (origLabel === label || origLabel === 0) {
239240
// eslint-disable-next-line no-param-reassign
240-
(originalData as any)[origIndex] = subValue > 127.5 ? label : 0;
241+
originalData[origIndex] = subValue > 127.5 ? label : 0;
241242
}
242243
subIndex++;
243244
}
@@ -274,9 +275,7 @@ export function gaussianSmoothLabelMapWorker(input: {
274275
}
275276

276277
if (originalLabelCount === 0) {
277-
const outputData = new (originalData.constructor as any)(
278-
originalData.length
279-
);
278+
const outputData = createTypedArrayLike(originalData, originalData.length);
280279
for (let i = 0; i < originalData.length; i++) {
281280
outputData[i] = originalData[i];
282281
}
@@ -291,9 +290,7 @@ export function gaussianSmoothLabelMapWorker(input: {
291290

292291
const bounds = calculateBoundingBox(originalData, dimensions, label);
293292
if (!bounds) {
294-
const outputData = new (originalData.constructor as any)(
295-
originalData.length
296-
);
293+
const outputData = createTypedArrayLike(originalData, originalData.length);
297294
for (let i = 0; i < originalData.length; i++) {
298295
outputData[i] = originalData[i];
299296
}
@@ -320,7 +317,7 @@ export function gaussianSmoothLabelMapWorker(input: {
320317
1.5
321318
);
322319

323-
const outputData = new (originalData.constructor as any)(originalData.length);
320+
const outputData = createTypedArrayLike(originalData, originalData.length);
324321
for (let i = 0; i < originalData.length; i++) {
325322
outputData[i] = originalData[i];
326323
}

src/store/tools/gaussianSmooth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineStore } from 'pinia';
22
import { ref } from 'vue';
3-
import { TypedArray } from '@kitware/vtk.js/types';
43
import * as Comlink from 'comlink';
54
import vtkLabelMap from '@/src/vtk/LabelMap';
65
import { gaussianSmoothLabelMapWorker } from '@/src/core/tools/paint/gaussianSmooth.worker';
@@ -62,7 +61,7 @@ export const useGaussianSmoothStore = defineStore('gaussianSmooth', () => {
6261
async function computeAlgorithm(
6362
segImage: vtkLabelMap,
6463
activeSegment: number
65-
): Promise<TypedArray> {
64+
) {
6665
const params = {
6766
sigma: sigma.value,
6867
label: activeSegment,

src/store/tools/process.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ type PreviewingState = {
3636

3737
type ProcessState = StartState | ComputingState | PreviewingState;
3838

39-
export interface ProcessAlgorithm {
40-
compute: (
41-
segImage: vtkLabelMap,
42-
activeSegment: number
43-
) => Promise<TypedArray>;
44-
}
39+
export type ProcessAlgorithm = (
40+
segImage: vtkLabelMap,
41+
activeSegment: number
42+
) => Promise<TypedArray | number[]>;
4543

4644
export const useProcessStore = defineStore('process', () => {
4745
const processState = ref<ProcessState>({ step: 'start' });
@@ -117,10 +115,7 @@ export const useProcessStore = defineStore('process', () => {
117115
}
118116

119117
try {
120-
const outputScalars = await algorithm.compute(
121-
segImage,
122-
paintStore.activeSegment
123-
);
118+
const outputScalars = await algorithm(segImage, paintStore.activeSegment);
124119

125120
// If the state changed during the async operation, stop processing
126121
if (processState.value.step !== 'computing') {

src/utils/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,23 @@ export const TypedArrayConstructorNames = [
258258
'Float64Array',
259259
];
260260

261+
/**
262+
* Creates a new typed array of the same type as the source array.
263+
* This utility handles the TypeScript typing issues when using array.constructor.
264+
*
265+
* @param sourceArray The source array to match the type of
266+
* @param arrayLength The length of the new array
267+
* @returns A new array of the same type as sourceArray
268+
*/
269+
export function createTypedArrayLike<T extends TypedArray | number[]>(
270+
sourceArray: T,
271+
arrayLength: number
272+
): T {
273+
return new (sourceArray.constructor as new (length: number) => T)(
274+
arrayLength
275+
);
276+
}
277+
261278
// https://stackoverflow.com/a/74823834
262279
type Entries<T> = {
263280
[K in keyof T]-?: [K, T[K]];

0 commit comments

Comments
 (0)