Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotation Tool Hover #404

Merged
merged 15 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/components/tools/AnnotationInfo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script setup lang="ts" generic="ToolID extends string">
/* global ToolID:readonly */
import { AnnotationToolStore } from '@/src/store/tools/useAnnotationTool';
import { OverlayInfo } from '@/src/composables/annotationTool';
import { computed } from 'vue';

const props = defineProps<{
info: OverlayInfo<ToolID>;
toolStore: AnnotationToolStore<ToolID>;
}>();

const menu = computed(() => {
return props.info.visible;
});

const label = computed(() => {
if (!props.info.visible) return '';
return props.toolStore.toolByID[props.info.toolID].labelName;
});
</script>

<template>
<v-menu
v-model="menu"
v-if="info.visible"
class="popover-no-events"
:style="{
left: `${info.displayXY[0]}px`,
top: `${info.displayXY[1]}px`,
pointerEvents: 'none',
}"
>
<v-list density="compact">
<v-list-item>
<v-list-item-title>{{ label }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</template>

<style>
.popover-no-events > * {
floryst marked this conversation as resolved.
Show resolved Hide resolved
pointer-events: none !important;
touch-action: none;
}
</style>
75 changes: 75 additions & 0 deletions src/components/tools/BoundingRectangle.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<script setup lang="ts">
import { computed, ref, watch, toRefs } from 'vue';
import { ANNOTATION_TOOL_HANDLE_RADIUS } from '@/src/constants';
import { useViewStore } from '@/src/store/views';
import { worldToSVG } from '@/src/utils/vtk-helpers';
import { nonNullable } from '@/src/utils/index';
import vtkLPSView2DProxy from '@/src/vtk/LPSView2DProxy';
import vtkBoundingBox from '@kitware/vtk.js/Common/DataModel/BoundingBox';
import { Bounds, Vector3 } from '@kitware/vtk.js/types';
import { onVTKEvent } from '@/src/composables/onVTKEvent';

const props = defineProps<{
points: Array<Vector3>;
viewId: string;
}>();

const viewStore = useViewStore();
const viewProxy = computed(
() => viewStore.getViewProxy<vtkLPSView2DProxy>(props.viewId)!
);

const visible = computed(() => {
return props.points.length > 0;
});

const rectangle = ref({
x: 0,
y: 0,
width: 0,
height: 0,
});

const updateRectangle = () => {
const viewRenderer = viewProxy.value.getRenderer();

const screenBounds = [...vtkBoundingBox.INIT_BOUNDS] as Bounds;
props.points
.map((point) => {
const point2D = worldToSVG(point, viewRenderer);
return point2D;
})
.filter(nonNullable)
.forEach(([x, y]) => {
vtkBoundingBox.addPoint(screenBounds, x, y, 0);
});
const [x, y] = vtkBoundingBox.getMinPoint(screenBounds);
const [maxX, maxY] = vtkBoundingBox.getMaxPoint(screenBounds);
const handleRadius = ANNOTATION_TOOL_HANDLE_RADIUS / devicePixelRatio;
const handleDiameter = 2 * handleRadius;
rectangle.value = {
x: x - handleRadius,
y: y - handleRadius,
width: maxX - x + handleDiameter,
height: maxY - y + handleDiameter,
};
};

const { points } = toRefs(props);
watch([points], updateRectangle, { immediate: true, deep: true });
floryst marked this conversation as resolved.
Show resolved Hide resolved

onVTKEvent(viewProxy, 'onModified', updateRectangle);
</script>

<template>
<rect
v-if="visible"
:x="rectangle.x"
:y="rectangle.y"
:width="rectangle.width"
:height="rectangle.height"
stroke-width="2"
fill="transparent"
stroke="lightgray"
/>
</template>
2 changes: 1 addition & 1 deletion src/components/tools/polygon/PolygonSVG2D.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<g>
<!-- radius is related to the vtkRectangleWidget scale, specified in state -->
<!-- radius should match constants.ANNOTATION_TOOL_HANDLE_RADIUS and should be related to vtkHandleWidget scale. -->
<circle
v-for="({ point: [x, y], radius }, index) in handlePoints"
:key="index"
Expand Down
22 changes: 22 additions & 0 deletions src/components/tools/polygon/PolygonTool.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div class="overlay-no-events">
<svg class="overlay-no-events">
<bounding-rectangle :points="pointsToBound" :view-id="viewId" />
<polygon-widget-2D
v-for="tool in tools"
:key="tool.id"
Expand All @@ -12,9 +13,11 @@
:widget-manager="widgetManager"
@contextmenu="openContextMenu(tool.id, $event)"
@placed="onToolPlaced"
@widgetHover="onHover(tool.id, $event)"
/>
</svg>
<annotation-context-menu ref="contextMenu" :tool-store="activeToolStore" />
<annotation-info :info="overlayInfo" :tool-store="activeToolStore" />
</div>
</template>

Expand Down Expand Up @@ -43,8 +46,11 @@ import { PolygonID } from '@/src/types/polygon';
import {
useContextMenu,
useCurrentTools,
useHover,
} from '@/src/composables/annotationTool';
import AnnotationContextMenu from '@/src/components/tools/AnnotationContextMenu.vue';
import AnnotationInfo from '@/src/components/tools/AnnotationInfo.vue';
import BoundingRectangle from '@/src/components/tools/BoundingRectangle.vue';
import PolygonWidget2D from './PolygonWidget2D.vue';

type ToolID = PolygonID;
Expand Down Expand Up @@ -74,6 +80,8 @@ export default defineComponent({
components: {
PolygonWidget2D,
AnnotationContextMenu,
AnnotationInfo,
BoundingRectangle,
},
setup(props) {
const { viewDirection, currentSlice } = toRefs(props);
Expand Down Expand Up @@ -181,13 +189,27 @@ export default defineComponent({

const currentTools = useCurrentTools(activeToolStore, viewAxis);

const { onHover, overlayInfo } = useHover<ToolID>(
currentTools,
currentSlice
);

const pointsToBound = computed(() => {
floryst marked this conversation as resolved.
Show resolved Hide resolved
if (!overlayInfo.value.visible) return [];
const tool = activeToolStore.toolByID[overlayInfo.value.toolID];
return tool.points;
});

return {
tools: currentTools,
placingToolID,
onToolPlaced,
contextMenu,
openContextMenu,
activeToolStore,
onHover,
overlayInfo,
pointsToBound,
};
},
});
Expand Down
9 changes: 7 additions & 2 deletions src/components/tools/polygon/PolygonWidget2D.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { useCurrentImage } from '@/src/composables/useCurrentImage';
import { updatePlaneManipulatorFor2DView } from '@/src/utils/manipulators';
import { LPSAxisDir } from '@/src/types/lps';
import { onVTKEvent } from '@/src/composables/onVTKEvent';
import { useRightClickContextMenu } from '@/src/composables/annotationTool';
import {
useHoverEvent,
useRightClickContextMenu,
} from '@/src/composables/annotationTool';
import { usePolygonStore as useStore } from '@/src/store/tools/polygons';
import { PolygonID as ToolID } from '@/src/types/polygon';
import vtkWidgetFactory, {
Expand All @@ -27,7 +30,7 @@ import SVG2DComponent from './PolygonSVG2D.vue';

export default defineComponent({
name: 'PolygonWidget2D',
emits: ['placed', 'contextmenu'],
emits: ['placed', 'contextmenu', 'widgetHover'],
props: {
toolId: {
type: String,
Expand Down Expand Up @@ -103,6 +106,8 @@ export default defineComponent({
emit('placed');
});

useHoverEvent(emit, widget);

// --- right click handling --- //

useRightClickContextMenu(emit, widget);
Expand Down
107 changes: 106 additions & 1 deletion src/composables/annotationTool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Ref, computed, ref } from 'vue';
import { Ref, computed, ref, watch } from 'vue';
import { useDebounceFn } from '@vueuse/core';
import { Vector2 } from '@kitware/vtk.js/types';
import { useCurrentImage } from '@/src/composables/useCurrentImage';
import { frameOfReferenceToImageSliceAndAxis } from '@/src/utils/frameOfReference';
import { vtkAnnotationToolWidget } from '@/src/vtk/ToolWidgetUtils/utils';
Expand All @@ -8,6 +10,8 @@ import { AnnotationTool, ContextMenuEvent } from '../types/annotation-tool';
import { AnnotationToolStore } from '../store/tools/useAnnotationTool';
import { getCSSCoordinatesFromEvent } from '../utils/vtk-helpers';

const SHOW_OVERLAY_DELAY = 500; // milliseconds

// does the tools's frame of reference match
// the view's axis
const useDoesToolFrameMatchViewAxis = <
Expand Down Expand Up @@ -78,3 +82,104 @@ export const useRightClickContextMenu = (
}
});
};

// --- Hover --- //

export const useHoverEvent = (
emit: (event: 'widgetHover', ...args: any[]) => void,
widget: Ref<vtkAnnotationToolWidget | null>
) => {
onVTKEvent(widget, 'onHoverEvent', (eventData: any) => {
const displayXY = getCSSCoordinatesFromEvent(eventData);
if (displayXY) {
emit('widgetHover', {
displayXY,
hovering: eventData.hovering,
});
}
});
};

export type OverlayInfo<ToolID> =
| {
visible: false;
}
| {
visible: true;
toolID: ToolID;
displayXY: Vector2;
};

// Maintains list of tools' hover states.
// If one tool hovered, overlayInfo.visible === true with toolID and displayXY.
export const useHover = <ToolID extends string>(
tools: Ref<Array<AnnotationTool<ToolID>>>,
currentSlice: Ref<number>
) => {
type Info = OverlayInfo<ToolID>;
const toolHoverState = ref({}) as Ref<Record<ToolID, Info>>;

const toolsOnCurrentSlice = computed(() =>
tools.value.filter((tool) => tool.slice === currentSlice.value)
);

watch(toolsOnCurrentSlice, () => {
// keep old hover states, default to false for new tools
toolHoverState.value = toolsOnCurrentSlice.value.reduce(
(toolsHovers, { id }) => {
const state = toolHoverState.value[id] ?? {
visible: false,
};
return Object.assign(toolsHovers, {
[id]: state,
});
},
{} as Record<ToolID, Info>
);
});

const onHover = (id: ToolID, event: any) => {
toolHoverState.value[id] = event.hovering
? {
visible: true,
toolID: id,
displayXY: event.displayXY,
}
: {
visible: false,
};
};

// If hovering true, debounce showing overlay.
// Immediately hide overlay if hovering false.
const synchronousOverlayInfo = computed(() => {
const visibleToolID = Object.keys(toolHoverState.value).find(
(toolID) => toolHoverState.value[toolID as ToolID].visible
) as ToolID | undefined;

return visibleToolID
? toolHoverState.value[visibleToolID]
: ({ visible: false } as Info);
});

// Debounced output
const overlayInfo = ref(synchronousOverlayInfo.value) as Ref<Info>;

const debouncedOverlayInfo = useDebounceFn((info: Info) => {
// if we moved off the tool (syncOverlay.visible === false), don't show overlay
if (synchronousOverlayInfo.value.visible) overlayInfo.value = info;
}, SHOW_OVERLAY_DELAY);

watch(synchronousOverlayInfo, () => {
if (!synchronousOverlayInfo.value.visible)
overlayInfo.value = synchronousOverlayInfo.value;
else {
// Immediately set visible = false to hide overlay on mouse move, even if hovering true.
// Depends on widget sending hover events with every mouse move.
overlayInfo.value = { visible: false };
debouncedOverlayInfo({ ...synchronousOverlayInfo.value });
}
});

floryst marked this conversation as resolved.
Show resolved Hide resolved
return { overlayInfo, onHover };
};
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ export const Messages = {
'Lost the WebGL context! Please reload the webpage. If the problem persists, you may need to restart your web browser.',
},
} as const;

export const ANNOTATION_TOOL_HANDLE_RADIUS = 10; // pixels
Loading
Loading