Skip to content

Commit

Permalink
feat(polygons): make merge key configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed Jul 3, 2024
1 parent c4ede9b commit ca4f587
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/components/tools/polygon/PolygonTool.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
import AnnotationContextMenu from '@/src/components/tools/AnnotationContextMenu.vue';
import AnnotationInfo from '@/src/components/tools/AnnotationInfo.vue';
import { useFrameOfReference } from '@/src/composables/useFrameOfReference';
import { actionToKey } from '@/src/composables/useKeyboardShortcuts';
import { Maybe } from '@/src/types';
import { useSliceInfo } from '@/src/composables/useSliceInfo';
import { useMagicKeys, watchImmediate } from '@vueuse/core';
Expand Down Expand Up @@ -126,7 +127,11 @@ export default defineComponent({
placingTool.remove();
});
const { v: mergeKey } = useMagicKeys();
const keys = useMagicKeys();
const mergeKey = computed(
() => keys[actionToKey.value.mergeNewPolygon].value
);
const onToolPlaced = () => {
if (imageId.value) {
const newToolId = placingTool.id.value;
Expand Down
2 changes: 2 additions & 0 deletions src/composables/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ export const ACTION_TO_FUNC = {
decrementLabel: applyLabelOffset(-1),
incrementLabel: applyLabelOffset(1),

mergeNewPolygon: () => {}, // acts as a modifier key rather than immediate effect, so no-op

showKeyboardShortcuts,
} as const satisfies Record<Action, () => void>;
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export const ACTION_TO_KEY = {
crosshairs: 'c',
crop: 'b',
polygon: 'g',
mergeNewPolygon: 'g',
select: 's',

decrementLabel: 'q',
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export const ACTIONS = {
readable: 'Activate next Label',
},

mergeNewPolygon: {
readable: 'Hold down to merge new polygons with overlapping polygons',
},

showKeyboardShortcuts: {
readable: 'Show keyboard shortcuts dialog',
},
Expand Down
11 changes: 5 additions & 6 deletions src/store/tools/polygons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as polygonClipping from 'polyclip-ts';
import { intersection, union, Geom } from 'polyclip-ts';
import type { Vector3 } from '@kitware/vtk.js/types';
import { computed } from 'vue';
import {
Expand Down Expand Up @@ -42,7 +42,7 @@ export const usePolygonStore = defineAnnotationToolStore('polygon', () => {
const polygonsOverlap = (a: Tool, b: Tool) => {
const [aGeo, bGeo] = [a, b].map(toGeoJSON);
if (!aGeo || !bGeo) return false;
return polygonClipping.intersection(aGeo, bGeo).length > 0;
return intersection(aGeo, bGeo).length > 0;
};

const sameSliceAndLabel = (a: Tool, b: Tool) =>
Expand Down Expand Up @@ -82,14 +82,13 @@ export const usePolygonStore = defineAnnotationToolStore('polygon', () => {
});

function mergeToolGroup(mergeGroup: Tool[]) {
const firstTool = mergeGroup[0];

const polygons = mergeGroup.map(toGeoJSON);
if (polygons.some((p) => !p))
throw new Error('Trying to merge invalid polygons');

const [first, ...rest] = polygons as unknown as polygonClipping.Geom[];
const merged = polygonClipping.union(first, ...rest);
const [first, ...rest] = polygons as unknown as Geom[];
const merged = union(first, ...rest);
const firstTool = mergeGroup[0];
const { to3D } = getPlaneTransforms(firstTool.frameOfReference);
const points = merged.flatMap((p) => p.flatMap((ring) => ring.map(to3D)));

Expand Down

0 comments on commit ca4f587

Please sign in to comment.