Skip to content

Commit

Permalink
feat(polygon): decimate with smallest pixel dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed May 2, 2024
1 parent 7ff3a48 commit 253d261
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/utils/frameOfReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ export function frameOfReferenceToImageSliceAndAxis(
return { axis, slice };
}

export function getPixelSizeSquared(
export function getSmallestSpacing(
frame: FrameOfReference,
metadata: ImageMetadata
): number | null {
const toolImageFrame = frameOfReferenceToImageSliceAndAxis(frame, metadata);
if (!toolImageFrame) return null;
const axisIndex = metadata.lpsOrientation[toolImageFrame.axis];
): number {
const sliceAxis = frameOfReferenceToImageSliceAndAxis(frame, metadata);
if (!sliceAxis) return Math.min(...metadata.spacing); // off orthogonal
const axisIndex = metadata.lpsOrientation[sliceAxis.axis];
const spacing = [...metadata.spacing];
spacing.splice(axisIndex, 1);
return spacing[0] * spacing[0] + spacing[1] * spacing[1];
return Math.min(...spacing);
}
27 changes: 9 additions & 18 deletions src/vtk/PolygonWidget/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import vtkAnnotationWidgetState from '@/src/vtk/ToolWidgetUtils/annotationWidget
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 { getSmallestSpacing } from '@/src/utils/frameOfReference';
import createPointState from '../ToolWidgetUtils/pointState';
import { watchState } from '../ToolWidgetUtils/utils';
import decimate from './decimate';
Expand All @@ -17,7 +17,7 @@ export const MoveHandleLabel = 'moveHandle';
export const HandlesLabel = 'handles';

const HANDLE_PIXEL_SIZE = 20;
const DECIMATE_PIXEL_SIZE_FACTOR = 0.01;
const DECIMATE_PIXEL_SIZE_FACTOR = 0.2;

type VtkObjectModel = {
classHierarchy: string[];
Expand Down Expand Up @@ -132,23 +132,14 @@ function vtkPolygonWidgetState(publicAPI: any, model: any) {

// Decimate points
const imageMeta = getImageMetadata(tool.imageID);
const pixelSizeSquared = getPixelSizeSquared(
tool.frameOfReference,
imageMeta
const pixelScale = getSmallestSpacing(tool.frameOfReference, imageMeta);
const optimizedLine = decimate(
tool.points,
pixelScale * DECIMATE_PIXEL_SIZE_FACTOR
);
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.'
);
}
publicAPI.clearHandles();
tool.points = optimizedLine;
addPointsAsHandles();
};

// Setup after deserialization
Expand Down

0 comments on commit 253d261

Please sign in to comment.