| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133 |
- import { Point3, Point2, fromPoint3ToPoint2 } from './mathUtils';
- import {
- utilities as csUtils,
- Types as CoreTypes,
- getEnabledElement,
- } from '@cornerstonejs/core';
- import {
- AnnotationTool,
- utilities,
- Types,
- annotation,
- drawing,
- } from '@cornerstonejs/tools';
- const {
- drawHandles,
- drawLinkedTextBox,
- drawLine: drawLineSvg,
- } = drawing;
- import {
- calculateMidpoint,
- vectorSubtract,
- vectorAdd,
- calculateDistance,
- } from './mathUtils';
- import {
- PublicToolProps,
- ToolProps,
- EventTypes,
- SVGDrawingHelper,
- } from '@cornerstonejs/tools/dist/esm/types';
- // 注解数据接口
- interface MidlineMeasurementAnnotation extends Types.Annotation {
- data: {
- handles: {
- points: CoreTypes.Point3[]; // [A, B, C, D] - 4个点
- activeHandleIndex: number | null;
- };
- cachedStats?: {
- [targetId: string]: {
- midPointM1: CoreTypes.Point3; // AB中点
- midPointM2: CoreTypes.Point3; // CD中点
- midlineLength: number; // 中线长度
- };
- };
- };
- // 选中状态
- isSelected?: boolean;
- }
- // 类型断言函数
- function isMidlineMeasurementAnnotation(annotation: Types.Annotation): annotation is MidlineMeasurementAnnotation {
- return annotation.metadata?.toolName === 'MidlineMeasurementTool';
- }
- // 导出的注解数据接口
- export interface ExportedMidlineData {
- points: CoreTypes.Point3[]; // [A, B, C, D] 4个点的世界坐标
- midline: {
- midPointM1: CoreTypes.Point3; // AB中点
- midPointM2: CoreTypes.Point3; // CD中点
- length: number; // 中线长度
- };
- metadata: {
- viewPlaneNormal: CoreTypes.Point3;
- viewUp: CoreTypes.Point3;
- FrameOfReferenceUID: string;
- referencedImageId: string;
- };
- }
- export default class MidlineMeasurementTool extends AnnotationTool {
- static toolName = 'MidlineMeasurementTool';
- editData: {
- annotation: Types.Annotation;
- viewportIdsToRender: string[];
- handleIndex?: number;
- newAnnotation?: boolean;
- hasMoved?: boolean;
- movingWholeTool?: boolean; // 是否移动整个工具
- wholeToolOffset?: CoreTypes.Point2; // 工具移动偏移量
- } | null = null;
- isDrawing: boolean = false;
- /**
- * 创建一个预设的注解,带有4个默认点
- * @param element HTML元素
- * @param viewport Viewport实例
- */
- static createDefaultAnnotation(
- element: HTMLDivElement,
- viewport: CoreTypes.IStackViewport | CoreTypes.IVolumeViewport
- ): MidlineMeasurementAnnotation {
- const enabledElement = getEnabledElement(element);
- if (!enabledElement) {
- throw new Error('Element is not enabled');
- }
- // 获取viewport的尺寸
- const canvas = viewport.canvas;
- const { width, height } = canvas;
- const centerX = width / 2;
- const centerY = height / 2;
- // 创建4个canvas坐标点
- // 线段AB: 水平线,在中心上方
- const lineAY = centerY - 100;
- const lineAStartX = centerX - 80;
- const lineAEndX = centerX + 80;
- // 线段CD: 水平线,在中心下方
- const lineCY = centerY + 100;
- const lineCStartX = centerX - 60;
- const lineCEndX = centerX + 60;
- const canvasPoints: CoreTypes.Point2[] = [
- // 线段AB: 点A, 点B
- [lineAStartX, lineAY],
- [lineAEndX, lineAY],
- // 线段CD: 点C, 点D
- [lineCStartX, lineCY],
- [lineCEndX, lineCY],
- ];
- // 转换为world坐标
- const worldPoints = canvasPoints.map((canvasPoint) =>
- viewport.canvasToWorld(canvasPoint)
- );
- const camera = viewport.getCamera();
- const { viewPlaneNormal, viewUp } = camera;
- if (viewPlaneNormal === undefined) {
- throw new Error('viewPlaneNormal is undefined');
- }
- if (viewUp === undefined) {
- throw new Error('viewUp is undefined');
- }
- const annotationData = {
- invalidated: true,
- highlighted: false,
- metadata: {
- viewPlaneNormal: [...viewPlaneNormal] as CoreTypes.Point3,
- viewUp: [...viewUp] as CoreTypes.Point3,
- FrameOfReferenceUID: viewport.getFrameOfReferenceUID(),
- referencedImageId: viewport.getCurrentImageId?.() || '',
- toolName: MidlineMeasurementTool.toolName,
- },
- data: {
- label: '',
- handles: {
- points: worldPoints,
- activeHandleIndex: null,
- },
- cachedStats: {},
- },
- } as MidlineMeasurementAnnotation;
- return annotationData;
- }
- /**
- * 导出注解数据
- * @param annotation 要导出的注解
- * @returns 导出的数据对象
- */
- static exportAnnotationData(
- annotation: MidlineMeasurementAnnotation,
- viewport: CoreTypes.IStackViewport | CoreTypes.IVolumeViewport
- ): ExportedMidlineData | null {
- const targetId = `imageId:${viewport.getCurrentImageId?.() || ''}`;
- const cachedStats = annotation.data.cachedStats?.[targetId];
- if (!cachedStats) {
- return null;
- }
- return {
- points: [...annotation.data.handles.points],
- midline: {
- midPointM1: cachedStats.midPointM1,
- midPointM2: cachedStats.midPointM2,
- length: cachedStats.midlineLength,
- },
- metadata: {
- viewPlaneNormal: annotation.metadata?.viewPlaneNormal || [0, 0, 1],
- viewUp: annotation.metadata?.viewUp || [0, 1, 0],
- FrameOfReferenceUID: annotation.metadata?.FrameOfReferenceUID || '',
- referencedImageId: annotation.metadata?.referencedImageId || '',
- },
- };
- }
- /**
- * 从导出的数据恢复注解
- * @param exportedData 导出的数据
- * @param element HTML元素
- * @param viewport Viewport实例
- * @returns 恢复的注解
- */
- static restoreFromExportedData(
- exportedData: ExportedMidlineData,
- element: HTMLDivElement,
- viewport: CoreTypes.IStackViewport | CoreTypes.IVolumeViewport
- ): MidlineMeasurementAnnotation {
- const enabledElement = getEnabledElement(element);
- if (!enabledElement) {
- throw new Error('Element is not enabled');
- }
- const annotationData = {
- invalidated: true,
- highlighted: false,
- metadata: {
- ...exportedData.metadata,
- toolName: MidlineMeasurementTool.toolName,
- },
- data: {
- label: '',
- handles: {
- points: [...exportedData.points],
- activeHandleIndex: null,
- },
- cachedStats: {},
- },
- } as MidlineMeasurementAnnotation;
- return annotationData;
- }
- constructor(
- toolProps: PublicToolProps = {},
- defaultToolProps: ToolProps = {
- supportedInteractionTypes: ['Mouse', 'Touch'],
- configuration: {
- shadow: true,
- preventHandleOutsideImage: false,
- },
- }
- ) {
- super(toolProps, defaultToolProps);
- }
- /**
- * 添加新注解 - 禁用此功能,因为我们只使用预设的注解
- */
- addNewAnnotation(
- evt: EventTypes.InteractionEventType
- ): Types.Annotation {
- // 不创建新注解,直接返回空对象
- evt.preventDefault();
- return {} as Types.Annotation;
- }
- /**
- * 处理 控制点/端点 选中回调
- */
- handleSelectedCallback(
- evt: EventTypes.InteractionEventType,
- annotation: Types.Annotation
- ): void {
- // 实现手柄选中逻辑
- if (isMidlineMeasurementAnnotation(annotation)) {
- annotation.highlighted = true;
- }
- }
- /**
- * 工具选中回调
- */
- toolSelectedCallback(
- evt: EventTypes.InteractionEventType,
- annotation: Types.Annotation
- ): void {
- // 实现工具选中逻辑
- if (isMidlineMeasurementAnnotation(annotation)) {
- annotation.highlighted = true;
- console.log(`选中了中线测量工具`);
- }
- }
- /**
- * 检查点是否靠近工具
- */
- isPointNearTool(
- element: HTMLDivElement,
- annotation: Types.Annotation,
- canvasCoords: CoreTypes.Point2,
- proximity: number
- ): boolean {
- if (!isMidlineMeasurementAnnotation(annotation)) {
- return false;
- }
- const enabledElement = getEnabledElement(element);
- if (!enabledElement) {
- return false;
- }
- const { viewport } = enabledElement;
- const points = annotation.data.handles.points;
- // 检查是否靠近任意一个手柄点
- for (let i = 0; i < points.length; i++) {
- const point = points[i];
- const canvasPoint = viewport.worldToCanvas(point);
- const distance = Math.sqrt(
- Math.pow(canvasPoint[0] - canvasCoords[0], 2) +
- Math.pow(canvasPoint[1] - canvasCoords[1], 2)
- );
- if (distance < proximity) {
- return true;
- }
- }
- // 检查是否靠近线段
- if (points.length >= 2) {
- for (let i = 0; i < points.length - 1; i += 2) {
- if (i + 1 < points.length) {
- const p1Canvas = viewport.worldToCanvas(points[i]);
- const p2Canvas = viewport.worldToCanvas(points[i + 1]);
- const dist = this._distanceToSegment(canvasCoords, p1Canvas, p2Canvas);
- if (dist < proximity) {
- return true;
- }
- }
- }
- }
- // 检查是否靠近中线
- if (points.length >= 4) {
- const midA = calculateMidpoint(points[0], points[1]);
- const midC = calculateMidpoint(points[2], points[3]);
- const midACanvas = viewport.worldToCanvas(midA);
- const midCCanvas = viewport.worldToCanvas(midC);
- const dist = this._distanceToSegment(canvasCoords, midACanvas, midCCanvas);
- if (dist < proximity) {
- return true;
- }
- }
- return false;
- }
- /**
- * 计算点到线段的距离
- */
- private _distanceToSegment(
- point: CoreTypes.Point2,
- lineStart: CoreTypes.Point2,
- lineEnd: CoreTypes.Point2
- ): number {
- const x = point[0];
- const y = point[1];
- const x1 = lineStart[0];
- const y1 = lineStart[1];
- const x2 = lineEnd[0];
- const y2 = lineEnd[1];
- const A = x - x1;
- const B = y - y1;
- const C = x2 - x1;
- const D = y2 - y1;
- const dot = A * C + B * D;
- const lenSq = C * C + D * D;
- let param = -1;
- if (lenSq !== 0) {
- param = dot / lenSq;
- }
- let xx, yy;
- if (param < 0) {
- xx = x1;
- yy = y1;
- } else if (param > 1) {
- xx = x2;
- yy = y2;
- } else {
- xx = x1 + param * C;
- yy = y1 + param * D;
- }
- const dx = x - xx;
- const dy = y - yy;
- return Math.sqrt(dx * dx + dy * dy);
- }
- /**
- * 取消操作
- */
- cancel(element: HTMLDivElement): string {
- if (this.isDrawing) {
- this.isDrawing = false;
- this._deactivateDraw(element);
- this._deactivateModify(element);
- const enabledElement = getEnabledElement(element);
- if (enabledElement) {
- const { renderingEngine } = enabledElement;
- const viewportIdsToRender =
- utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- }
- this.editData = null;
- return this.getToolName();
- }
- return '';
- }
- /**
- * 激活绘制模式
- */
- _activateDraw(element: HTMLDivElement): void {
- element.addEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_DRAG',
- this._dragCallback as EventListener
- );
- element.addEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_UP',
- this._endCallback as EventListener
- );
- element.addEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_CLICK',
- this._endCallback as EventListener
- );
- }
- /**
- * 取消激活绘制模式
- */
- _deactivateDraw(element: HTMLDivElement): void {
- element.removeEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_DRAG',
- this._dragCallback as EventListener
- );
- element.removeEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_UP',
- this._endCallback as EventListener
- );
- element.removeEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_CLICK',
- this._endCallback as EventListener
- );
- }
- /**
- * 激活修改模式
- */
- _activateModify(element: HTMLDivElement): void {
- element.addEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_DOWN',
- this._mouseDownModifyCallback as EventListener
- );
- element.addEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_DRAG',
- this._mouseDragModifyCallback as EventListener
- );
- element.addEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_UP',
- this._mouseUpModifyCallback as EventListener
- );
- element.addEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_MOVE',
- this._mouseMoveModifyCallback as EventListener
- );
- // 添加键盘事件监听用于删除功能
- element.addEventListener(
- 'keydown',
- this._keyDownCallback as EventListener
- );
- }
- /**
- * 取消激活修改模式
- */
- _deactivateModify(element: HTMLDivElement): void {
- element.removeEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_DOWN',
- this._mouseDownModifyCallback as EventListener
- );
- element.removeEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_DRAG',
- this._mouseDragModifyCallback as EventListener
- );
- element.removeEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_UP',
- this._mouseUpModifyCallback as EventListener
- );
- element.removeEventListener(
- 'CORNERSTONE_TOOLS_MOUSE_MOVE',
- this._mouseMoveModifyCallback as EventListener
- );
- // 移除键盘事件监听
- element.removeEventListener(
- 'keydown',
- this._keyDownCallback as EventListener
- );
- }
- /**
- * 键盘按下回调 - 用于删除功能
- */
- _keyDownCallback = (evt: KeyboardEvent): void => {
- const element = evt.currentTarget as HTMLDivElement;
- if (evt.key === 'Delete' || evt.key === 'Backspace') {
- const enabledElement = getEnabledElement(element);
- if (!enabledElement) {
- return;
- }
- const annotations = annotation.state.getAnnotations(this.getToolName(), element);
- if (!annotations || annotations.length === 0) {
- return;
- }
- // 查找被选中的注解
- const selectedAnnotations = annotations.filter(
- (ann) => isMidlineMeasurementAnnotation(ann) && ann.highlighted
- );
- if (selectedAnnotations.length === 0) {
- return;
- }
- // 删除所有选中的注解
- for (const ann of selectedAnnotations) {
- annotation.state.removeAnnotation(ann.annotationUID!);
- }
- // 触发渲染更新
- const viewportIdsToRender =
- utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- evt.preventDefault();
- evt.stopPropagation();
- }
- };
- /**
- * 鼠标移动回调 - 用于修改模式,处理悬停检测
- */
- _mouseMoveModifyCallback = (evt: EventTypes.InteractionEventType): void => {
- const eventDetail = evt.detail;
- const { element, currentPoints } = eventDetail;
- if (!currentPoints || !currentPoints.canvas) {
- return;
- }
- const canvasCoords = currentPoints.canvas;
- const enabledElement = getEnabledElement(element);
- if (!enabledElement) {
- return;
- }
- const annotations = annotation.state.getAnnotations(this.getToolName(), element);
- if (!annotations || annotations.length === 0) {
- element.style.cursor = 'default';
- return;
- }
- let isHovering = false;
- // 检查是否悬停在手柄或线段上
- for (const ann of annotations) {
- if (!isMidlineMeasurementAnnotation(ann)) continue;
- // 检查是否靠近手柄
- const handle = this.getHandleNearImagePoint(element, ann, canvasCoords, 6);
- if (handle) {
- element.style.cursor = 'crosshair';
- ann.highlighted = true;
- isHovering = true;
- break;
- }
- // 检查是否靠近线段
- if (this.isPointNearTool(element, ann, canvasCoords, 10)) {
- element.style.cursor = 'crosshair';
- ann.highlighted = true;
- isHovering = true;
- break;
- }
- }
- // 如果没有悬停在任何地方,重置高亮
- if (!isHovering) {
- for (const ann of annotations) {
- if (isMidlineMeasurementAnnotation(ann)) {
- ann.highlighted = false;
- }
- }
- element.style.cursor = 'default';
- }
- // 触发渲染以更新高亮状态
- const viewportIdsToRender = utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- };
- /**
- * 鼠标按下回调 - 用于修改模式
- */
- _mouseDownModifyCallback = (evt: EventTypes.InteractionEventType): void => {
- const eventDetail = evt.detail;
- const { element, currentPoints } = eventDetail;
- const canvasCoords = currentPoints.canvas;
- const enabledElement = getEnabledElement(element);
- if (!enabledElement) {
- return;
- }
- const annotations = annotation.state.getAnnotations(this.getToolName(), element);
- if (!annotations || annotations.length === 0) {
- return;
- }
- // 查找最近的手柄
- for (const ann of annotations) {
- if (!isMidlineMeasurementAnnotation(ann)) continue;
- const handle = this.getHandleNearImagePoint(
- element,
- ann,
- canvasCoords,
- 6
- );
- if (handle) {
- const viewportIdsToRender =
- utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- this.editData = {
- annotation: ann,
- viewportIdsToRender,
- handleIndex: ann.data.handles.activeHandleIndex || 0,
- hasMoved: false,
- };
- ann.isSelected = true;
- ann.highlighted = true;
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- evt.preventDefault();
- evt.stopPropagation();
- return;
- }
- }
- // 检查是否点击在工具的其他位置(线段上)
- for (const ann of annotations) {
- if (!isMidlineMeasurementAnnotation(ann)) continue;
- if (this.isPointNearTool(element, ann, canvasCoords, 10)) {
- const viewportIdsToRender =
- utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- // 计算鼠标点击位置相对于工具的偏移量
- const points = ann.data.handles.points;
- if (points.length >= 4) {
- const midPoint = calculateMidpoint(points[0], points[1]);
- const midCanvas = enabledElement.viewport.worldToCanvas(midPoint);
- const wholeToolOffset: CoreTypes.Point2 = [
- canvasCoords[0] - midCanvas[0],
- canvasCoords[1] - midCanvas[1],
- ];
- this.editData = {
- annotation: ann,
- viewportIdsToRender,
- hasMoved: false,
- movingWholeTool: true,
- wholeToolOffset: wholeToolOffset,
- };
- ann.isSelected = true;
- ann.highlighted = true;
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- evt.preventDefault();
- evt.stopPropagation();
- return;
- }
- }
- }
- // 如果没有点击在工具上,取消所有工具的选中状态
- for (const ann of annotations) {
- if (isMidlineMeasurementAnnotation(ann)) {
- ann.isSelected = false;
- ann.highlighted = false;
- }
- }
- const viewportIdsToRender =
- utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- };
- /**
- * 鼠标拖拽回调 - 用于修改模式
- */
- _mouseDragModifyCallback = (evt: EventTypes.InteractionEventType): void => {
- if (!this.editData) {
- return;
- }
- const eventDetail = evt.detail;
- const { currentPoints } = eventDetail;
- const canvasCoords = currentPoints.canvas;
- const enabledElement = getEnabledElement(eventDetail.element);
- if (!enabledElement) {
- return;
- }
- const { annotation: ann, viewportIdsToRender, movingWholeTool } = this.editData;
- if (!isMidlineMeasurementAnnotation(ann)) return;
- const { data } = ann;
- // 如果正在移动整个工具
- if (movingWholeTool && this.editData.wholeToolOffset) {
- const newMidCanvas: CoreTypes.Point2 = [
- canvasCoords[0] - this.editData.wholeToolOffset[0],
- canvasCoords[1] - this.editData.wholeToolOffset[1],
- ];
- const newMidWorld = enabledElement.viewport.canvasToWorld(newMidCanvas);
- const points = data.handles.points;
- if (points.length >= 4) {
- const oldMid = calculateMidpoint(points[0], points[1]);
- const offset = vectorSubtract(newMidWorld, oldMid);
- // 移动所有点
- for (let i = 0; i < points.length; i++) {
- points[i] = vectorAdd(points[i], offset);
- }
- // 重新计算统计数据
- this._updateCachedStats(ann, enabledElement);
- this.editData.hasMoved = true;
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- evt.preventDefault();
- evt.stopPropagation();
- return;
- }
- }
- // 手柄拖拽
- const worldPos = currentPoints.world;
- const activeHandleIndex = data.handles.activeHandleIndex;
- if (activeHandleIndex !== null && activeHandleIndex >= 0 && activeHandleIndex < data.handles.points.length) {
- data.handles.points[activeHandleIndex] = worldPos;
- this._updateCachedStats(ann, enabledElement);
- this.editData.hasMoved = true;
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- evt.preventDefault();
- evt.stopPropagation();
- }
- };
- /**
- * 鼠标释放回调 - 用于修改模式
- */
- _mouseUpModifyCallback = (evt: EventTypes.InteractionEventType): void => {
- if (!this.editData) {
- return;
- }
- const { annotation: ann, hasMoved, movingWholeTool } = this.editData;
- if (!isMidlineMeasurementAnnotation(ann)) return;
- ann.data.handles.activeHandleIndex = null;
- // 如果没有拖拽且点击在线段上,则保持选中状态
- if (!hasMoved && movingWholeTool) {
- ann.highlighted = true;
- }
- const eventDetail = evt.detail;
- const { element } = eventDetail;
- const viewportIdsToRender =
- utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- this.editData = null;
- evt.preventDefault();
- evt.stopPropagation();
- };
- /**
- * 处理鼠标拖拽
- */
- _dragCallback = (evt: EventTypes.InteractionEventType): void => {
- this.isDrawing = true;
- const eventDetail = evt.detail;
- const { element } = eventDetail;
- const enabledElement = getEnabledElement(element);
- if (!enabledElement || !this.editData) {
- return;
- }
- const { annotation, viewportIdsToRender } = this.editData;
- if (!isMidlineMeasurementAnnotation(annotation)) return;
- const { data } = annotation;
- if (annotation) {
- const { currentPoints } = eventDetail;
- const worldPos = currentPoints.world;
- const points = data.handles.points;
- const activeHandleIndex = data.handles.activeHandleIndex;
- if (activeHandleIndex !== null && activeHandleIndex < points.length) {
- points[activeHandleIndex] = worldPos;
- this._updateCachedStats(
- annotation,
- enabledElement
- );
- this.editData!.hasMoved = true;
- }
- utilities.triggerAnnotationRenderForViewportIds(viewportIdsToRender);
- }
- };
- /**
- * 处理鼠标抬起/点击
- */
- _endCallback = (evt: EventTypes.InteractionEventType): void => {
- const eventDetail = evt.detail;
- const { element } = eventDetail;
- if (!this.editData) {
- return;
- }
- const { annotation, viewportIdsToRender, newAnnotation, hasMoved } =
- this.editData;
- if (!isMidlineMeasurementAnnotation(annotation)) return;
- const { data } = annotation;
- if (newAnnotation && !hasMoved) {
- const points = data.handles.points;
- if (points.length < 4) {
- const { currentPoints } = eventDetail;
- const worldPos = currentPoints.world;
- if (points.length > 0) {
- points[points.length - 1] = worldPos;
- }
- if (points.length < 4) {
- points.push(worldPos);
- data.handles.activeHandleIndex = points.length - 1;
- }
- if (points.length === 4) {
- this.isDrawing = false;
- data.handles.activeHandleIndex = null;
- annotation.highlighted = false;
- this.editData.newAnnotation = false;
- this._deactivateDraw(element);
- }
- const enabledElement = getEnabledElement(element);
- if (enabledElement) {
- this._updateCachedStats(
- annotation,
- enabledElement
- );
- const newViewportIdsToRender =
- utilities.viewportFilters.getViewportIdsWithToolToRender(
- element,
- this.getToolName()
- );
- utilities.triggerAnnotationRenderForViewportIds(newViewportIdsToRender);
- }
- }
- } else if (hasMoved) {
- this.editData.hasMoved = false;
- }
- };
- /**
- * 获取靠近图像点的手柄
- */
- getHandleNearImagePoint(
- element: HTMLDivElement,
- annotation: Types.Annotation,
- canvasCoords: CoreTypes.Point2,
- proximity: number
- ): Types.ToolHandle | undefined {
- if (!isMidlineMeasurementAnnotation(annotation)) return undefined;
- const enabledElement = getEnabledElement(element);
- if (!enabledElement) {
- return undefined;
- }
- const { viewport } = enabledElement;
- const points = annotation.data.handles.points;
- const handleProximity = Math.max(proximity, 15);
- for (let i = 0; i < points.length; i++) {
- const point = points[i];
- const canvasPoint = viewport.worldToCanvas(point);
- const distance = Math.sqrt(
- Math.pow(canvasPoint[0] - canvasCoords[0], 2) +
- Math.pow(canvasPoint[1] - canvasCoords[1], 2)
- );
- if (distance < handleProximity) {
- annotation.data.handles.activeHandleIndex = i;
- return {
- worldPosition: point,
- } as Types.ToolHandle;
- }
- }
- annotation.data.handles.activeHandleIndex = null;
- return undefined;
- }
- /**
- * 更新缓存的统计数据
- */
- _updateCachedStats(
- annotation: MidlineMeasurementAnnotation,
- enabledElement: CoreTypes.IEnabledElement
- ): void {
- const { viewport } = enabledElement;
- const { data } = annotation;
- const points = data.handles.points;
- const targetId = this.getTargetId(viewport);
- if (!targetId) {
- return;
- }
- if (!data.cachedStats) {
- data.cachedStats = {};
- }
- if (!data.cachedStats[targetId]) {
- data.cachedStats[targetId] = {
- midPointM1: [0, 0, 0] as CoreTypes.Point3,
- midPointM2: [0, 0, 0] as CoreTypes.Point3,
- midlineLength: 0,
- };
- }
- const stats = data.cachedStats[targetId];
- if (points.length >= 4) {
- // 计算中点
- const midPointM1 = calculateMidpoint(points[0], points[1]);
- const midPointM2 = calculateMidpoint(points[2], points[3]);
- // 计算中线长度
- const midlineLength = calculateDistance(midPointM1, midPointM2);
- // 存储计算结果
- stats.midPointM1 = midPointM1;
- stats.midPointM2 = midPointM2;
- stats.midlineLength = midlineLength;
- }
- }
- /**
- * 渲染注解
- */
- renderAnnotation = (
- enabledElement: CoreTypes.IEnabledElement,
- svgDrawingHelper: SVGDrawingHelper
- ): boolean => {
- let renderStatus = false;
- const { viewport } = enabledElement;
- const { element } = viewport;
- let annotations = annotation.state.getAnnotations(this.getToolName(), element);
- if (!annotations?.length) {
- return renderStatus;
- }
- for (let i = 0; i < annotations.length; i++) {
- const annotation = annotations[i];
- if (!isMidlineMeasurementAnnotation(annotation)) continue;
- const { annotationUID, data } = annotation;
- const points = data.handles.points;
- if (points.length < 4) {
- continue;
- }
- const targetId = this.getTargetId(viewport);
- const cachedStats = targetId ? data.cachedStats?.[targetId] : undefined;
- // 转换所有点为canvas坐标
- const canvasPoints = points.map((p) => viewport.worldToCanvas(p));
- // 绘制线段AB (蓝色)
- if (annotationUID) {
- const lineABUID = `${annotationUID}-lineAB`;
- drawLineSvg(
- svgDrawingHelper,
- annotationUID,
- lineABUID,
- canvasPoints[0],
- canvasPoints[1],
- {
- color: 'rgb(0, 0, 255)',
- width: 2,
- }
- );
- }
- // 绘制线段CD (红色)
- if (annotationUID) {
- const lineCDUID = `${annotationUID}-lineCD`;
- drawLineSvg(
- svgDrawingHelper,
- annotationUID,
- lineCDUID,
- canvasPoints[2],
- canvasPoints[3],
- {
- color: 'rgb(255, 0, 0)',
- width: 2,
- }
- );
- }
- // 绘制中线 (绿色)
- if (cachedStats?.midPointM1 && cachedStats?.midPointM2 && annotationUID) {
- const midM1Canvas = viewport.worldToCanvas(cachedStats.midPointM1);
- const midM2Canvas = viewport.worldToCanvas(cachedStats.midPointM2);
- const midlineUID = `${annotationUID}-midline`;
- drawLineSvg(
- svgDrawingHelper,
- annotationUID,
- midlineUID,
- midM1Canvas,
- midM2Canvas,
- {
- color: 'rgb(0, 255, 0)',
- width: 2,
- }
- );
- }
- // 绘制手柄点
- if (annotationUID) {
- const handleGroupUID = '0';
- const handleRadius = annotation.highlighted ? 12 : 8;
- drawHandles(
- svgDrawingHelper,
- annotationUID,
- handleGroupUID,
- canvasPoints,
- {
- color: 'rgb(255, 255, 255)',
- handleRadius: handleRadius,
- }
- );
- }
- renderStatus = true;
- }
- return renderStatus;
- };
- }
|