Skip to content

Commit

Permalink
fix(polygon): use pixel size during line decimation
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed May 1, 2024
1 parent e00b284 commit f49f04d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/utils/frameOfReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ export function frameOfReferenceToImageSliceAndAxis(

return { axis, slice };
}

export function getPixelSizeSquared(
frame: FrameOfReference,
metadata: ImageMetadata
): number | null {
const toolImageFrame = frameOfReferenceToImageSliceAndAxis(frame, metadata);
if (!toolImageFrame) return null;
const axisIndex = metadata.lpsOrientation[toolImageFrame.axis];
const spacing = [...metadata.spacing].toSpliced(axisIndex, 1);
return spacing[0] * spacing[0] + spacing[1] * spacing[1];
}
34 changes: 28 additions & 6 deletions src/vtk/PolygonWidget/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { Vector3 } from '@kitware/vtk.js/types';
import vtkAnnotationWidgetState from '@/src/vtk/ToolWidgetUtils/annotationWidgetState';
import { Polygon } from '@/src/types/polygon';
import { AnnotationToolType } from '@/src/store/tools/types';
import { getImageMetadata } from '@/src/composables/useCurrentImage';
import { getPixelSizeSquared } from '@/src/utils/frameOfReference';
import createPointState from '../ToolWidgetUtils/pointState';
import { watchState } from '../ToolWidgetUtils/utils';
import decimate from './decimate';

export const MoveHandleLabel = 'moveHandle';
export const HandlesLabel = 'handles';

const PIXEL_SIZE = 20;
const HANDLE_PIXEL_SIZE = 20;
const DECIMATE_PIXEL_SIZE_FACTOR = 0.01;

type VtkObjectModel = {
classHierarchy: string[];
Expand Down Expand Up @@ -66,7 +69,9 @@ function vtkPolygonWidgetState(publicAPI: any, model: any) {
};
vtkWidgetState.extend(handlePublicAPI, handleModel, {});
visibleMixin.extend(handlePublicAPI, handleModel, { visible: true });
scale1Mixin.extend(handlePublicAPI, handleModel, { scale1: PIXEL_SIZE });
scale1Mixin.extend(handlePublicAPI, handleModel, {
scale1: HANDLE_PIXEL_SIZE,
});
const handleModelPromoted = handleModel as HandleModel;
handleModelPromoted.classHierarchy.push('vtkPolygonHandleState');

Expand Down Expand Up @@ -122,11 +127,28 @@ function vtkPolygonWidgetState(publicAPI: any, model: any) {

publicAPI.setPlacing = (placing: boolean) => {
const tool = getTool();
const optimizedLine = decimate(tool.points);
publicAPI.clearHandles();
tool.points = optimizedLine;
addPointsAsHandles();
tool.placing = placing;
if (placing) return;

// Decimate points
const imageMeta = getImageMetadata(tool.imageID);
const pixelSizeSquared = getPixelSizeSquared(
tool.frameOfReference,
imageMeta
);
if (pixelSizeSquared) {
const optimizedLine = decimate(
tool.points,
pixelSizeSquared * DECIMATE_PIXEL_SIZE_FACTOR
);
publicAPI.clearHandles();
tool.points = optimizedLine;
addPointsAsHandles();
} else {
console.error(
'Off LPS axis pixel sizing not implemented. Not decimating line.'
);
}
};

// Setup after deserialization
Expand Down

0 comments on commit f49f04d

Please sign in to comment.