Skip to content

Commit b8e954d

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

6 files changed

Lines changed: 35 additions & 44 deletions

File tree

src/components/ProcessControls.vue

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
<script setup lang="ts">
1818
import { computed } from 'vue';
19-
import {
20-
ProcessType,
21-
useProcessStore,
22-
type ProcessAlgorithm,
23-
} from '../store/tools/process';
19+
import { ProcessType, useProcessStore } from '../store/tools/process';
2420
import ProcessTypeSelector from './ProcessTypeSelector.vue';
2521
import ProcessWorkflow from './ProcessWorkflow.vue';
2622
import FillBetweenParameterControls from './FillBetweenParameterControls.vue';
@@ -34,16 +30,6 @@ const gaussianSmoothStore = useGaussianSmoothStore();
3430
3531
const activeProcessType = computed(() => processStore.activeProcessType);
3632
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-
};
33+
const fillBetweenAlgorithm = fillBetweenStore.computeAlgorithm;
34+
const gaussianSmoothAlgorithm = gaussianSmoothStore.computeAlgorithm;
4935
</script>

src/components/ProcessWorkflow.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ function handleToggleChange(value: number | string) {
7878
7979
function handleConfirm() {
8080
if (showingOriginal.value) {
81-
// User is viewing original - cancel the process
8281
processStore.cancelProcess();
8382
} else {
84-
// User is viewing processed - confirm the process
8583
processStore.confirmProcess();
8684
}
8785
}

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

Lines changed: 9 additions & 13 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;
@@ -129,13 +130,12 @@ function gaussianFilter3D(
129130
const kernelX = generateGaussianKernel(sigmaPixels[0], radiusFactor);
130131
const kernelY = generateGaussianKernel(sigmaPixels[1], radiusFactor);
131132
const kernelZ = generateGaussianKernel(sigmaPixels[2], radiusFactor);
132-
const temp1 = new Float32Array(totalSize);
133-
const temp2 = new Float32Array(totalSize);
133+
const temp = new Float32Array(totalSize);
134134
const output = new Float32Array(totalSize);
135135

136-
convolve1D(inputData, temp1, dimensions, kernelX, 0);
137-
convolve1D(temp1, temp2, dimensions, kernelY, 1);
138-
convolve1D(temp2, output, dimensions, kernelZ, 2);
136+
convolve1D(inputData, output, dimensions, kernelX, 0);
137+
convolve1D(output, temp, dimensions, kernelY, 1);
138+
convolve1D(temp, output, dimensions, kernelZ, 2);
139139

140140
return output;
141141
}
@@ -237,7 +237,7 @@ function copySubVolumeBack(
237237

238238
if (origLabel === label || origLabel === 0) {
239239
// eslint-disable-next-line no-param-reassign
240-
(originalData as any)[origIndex] = subValue > 127.5 ? label : 0;
240+
originalData[origIndex] = subValue > 127.5 ? label : 0;
241241
}
242242
subIndex++;
243243
}
@@ -274,9 +274,7 @@ export function gaussianSmoothLabelMapWorker(input: {
274274
}
275275

276276
if (originalLabelCount === 0) {
277-
const outputData = new (originalData.constructor as any)(
278-
originalData.length
279-
);
277+
const outputData = createTypedArrayLike(originalData, originalData.length);
280278
for (let i = 0; i < originalData.length; i++) {
281279
outputData[i] = originalData[i];
282280
}
@@ -291,9 +289,7 @@ export function gaussianSmoothLabelMapWorker(input: {
291289

292290
const bounds = calculateBoundingBox(originalData, dimensions, label);
293291
if (!bounds) {
294-
const outputData = new (originalData.constructor as any)(
295-
originalData.length
296-
);
292+
const outputData = createTypedArrayLike(originalData, originalData.length);
297293
for (let i = 0; i < originalData.length; i++) {
298294
outputData[i] = originalData[i];
299295
}
@@ -320,7 +316,7 @@ export function gaussianSmoothLabelMapWorker(input: {
320316
1.5
321317
);
322318

323-
const outputData = new (originalData.constructor as any)(originalData.length);
319+
const outputData = createTypedArrayLike(originalData, originalData.length);
324320
for (let i = 0; i < originalData.length; i++) {
325321
outputData[i] = originalData[i];
326322
}

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)