@cornerstonejs/tools 4.17.0 → 4.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/esm/drawingSvg/draw.js +4 -0
  2. package/dist/esm/drawingSvg/drawTextBox.js +8 -4
  3. package/dist/esm/tools/annotation/AngleTool.js +15 -29
  4. package/dist/esm/tools/annotation/ArrowAnnotateTool.js +9 -28
  5. package/dist/esm/tools/annotation/BidirectionalTool.js +10 -31
  6. package/dist/esm/tools/annotation/CircleROITool.js +12 -31
  7. package/dist/esm/tools/annotation/CobbAngleTool.js +14 -32
  8. package/dist/esm/tools/annotation/EllipticalROITool.js +11 -31
  9. package/dist/esm/tools/annotation/HeightTool.js +11 -31
  10. package/dist/esm/tools/annotation/LengthTool.js +10 -30
  11. package/dist/esm/tools/annotation/LivewireContourTool.js +20 -20
  12. package/dist/esm/tools/annotation/PlanarFreehandROITool.js +8 -21
  13. package/dist/esm/tools/annotation/ProbeTool.js +3 -5
  14. package/dist/esm/tools/annotation/RectangleROITool.js +10 -30
  15. package/dist/esm/tools/annotation/SplineROITool.js +20 -20
  16. package/dist/esm/tools/annotation/UltrasoundDirectionalTool.js +15 -29
  17. package/dist/esm/tools/base/AnnotationTool.d.ts +11 -0
  18. package/dist/esm/tools/base/AnnotationTool.js +50 -0
  19. package/dist/esm/tools/segmentation/CircleROIStartEndThresholdTool.js +15 -31
  20. package/dist/esm/tools/segmentation/RectangleROIStartEndThresholdTool.js +10 -30
  21. package/dist/esm/tools/segmentation/SegmentBidirectionalTool.js +10 -32
  22. package/dist/esm/utilities/drawing/getTextBoxCoordsCanvas.d.ts +1 -1
  23. package/dist/esm/utilities/drawing/getTextBoxCoordsCanvas.js +113 -3
  24. package/dist/esm/utilities/drawing/textBoxOverlapRegistry.d.ts +9 -0
  25. package/dist/esm/utilities/drawing/textBoxOverlapRegistry.js +20 -0
  26. package/dist/esm/version.d.ts +1 -1
  27. package/dist/esm/version.js +1 -1
  28. package/package.json +3 -3
  29. package/dist/esm/tools/segmentation/strategies/__tests__/fillCircle.spec.d.ts +0 -1
  30. package/dist/esm/tools/segmentation/strategies/__tests__/fillCircle.spec.js +0 -27
@@ -1,16 +1,116 @@
1
- export default function getTextBoxCoordsCanvas(annotationCanvasPoints) {
1
+ import { getRegisteredTextBoxes, } from './textBoxOverlapRegistry';
2
+ import intersectAABB from '../math/aabb/intersectAABB';
3
+ const VIEWPORT_ELEMENT = 'viewport-element';
4
+ const TEXT_BOX_GAP = 6;
5
+ export default function getTextBoxCoordsCanvas(annotationCanvasPoints, element, textLines = []) {
6
+ if (!annotationCanvasPoints?.length || !annotationCanvasPoints[0]) {
7
+ return [0, 0];
8
+ }
2
9
  const corners = _determineCorners(annotationCanvasPoints);
3
10
  const centerY = (corners.top[1] + corners.bottom[1]) / 2;
4
- const textBoxCanvas = [corners.right[0], centerY];
5
- return textBoxCanvas;
11
+ const defaultTextBoxCanvas = [corners.right[0], centerY];
12
+ if (!element) {
13
+ return defaultTextBoxCanvas;
14
+ }
15
+ const { width: textBoxWidth, height: textBoxHeight } = _estimateTextBoxSize(textLines);
16
+ const margin = 4;
17
+ const maxX = element.clientWidth - margin;
18
+ const maxY = element.clientHeight - margin;
19
+ let x = corners.right[0];
20
+ let y = centerY - textBoxHeight / 2;
21
+ if (x + textBoxWidth > maxX) {
22
+ x = corners.left[0] - textBoxWidth;
23
+ }
24
+ x = Math.max(margin, Math.min(x, maxX - textBoxWidth));
25
+ y = Math.max(margin, Math.min(y, maxY - textBoxHeight));
26
+ const svgLayer = _findSvgLayer(element);
27
+ if (svgLayer) {
28
+ const existingBoxes = getRegisteredTextBoxes(svgLayer);
29
+ if (existingBoxes.length > 0) {
30
+ const resolved = _resolveOverlap(x, y, textBoxWidth, textBoxHeight, existingBoxes, margin, maxX, maxY);
31
+ x = resolved[0];
32
+ y = resolved[1];
33
+ }
34
+ }
35
+ return [x, y];
36
+ }
37
+ function _resolveOverlap(x, y, width, height, existingBoxes, margin, maxX, maxY) {
38
+ if (!_overlapsAny(x, y, width, height, existingBoxes)) {
39
+ return [x, y];
40
+ }
41
+ let candidateY = y;
42
+ for (let i = 0; i < 30; i++) {
43
+ const blocker = _findFirstOverlap(x, candidateY, width, height, existingBoxes);
44
+ if (!blocker) {
45
+ break;
46
+ }
47
+ candidateY = blocker.y + blocker.height + TEXT_BOX_GAP;
48
+ if (candidateY + height > maxY) {
49
+ candidateY = Infinity;
50
+ break;
51
+ }
52
+ }
53
+ if (candidateY !== Infinity &&
54
+ !_overlapsAny(x, candidateY, width, height, existingBoxes)) {
55
+ return [
56
+ x,
57
+ Math.max(margin, Math.min(candidateY, maxY - height)),
58
+ ];
59
+ }
60
+ candidateY = y;
61
+ for (let i = 0; i < 30; i++) {
62
+ const blocker = _findFirstOverlap(x, candidateY, width, height, existingBoxes);
63
+ if (!blocker) {
64
+ break;
65
+ }
66
+ candidateY = blocker.y - height - TEXT_BOX_GAP;
67
+ if (candidateY < margin) {
68
+ candidateY = -Infinity;
69
+ break;
70
+ }
71
+ }
72
+ if (candidateY !== -Infinity &&
73
+ !_overlapsAny(x, candidateY, width, height, existingBoxes)) {
74
+ return [
75
+ x,
76
+ Math.max(margin, Math.min(candidateY, maxY - height)),
77
+ ];
78
+ }
79
+ return [x, y];
80
+ }
81
+ function _overlapsAny(x, y, w, h, boxes) {
82
+ const candidate = _toTextBoxAABB({ x, y, width: w, height: h });
83
+ return boxes.some((box) => intersectAABB(candidate, _toTextBoxAABB(box, TEXT_BOX_GAP / 2)));
84
+ }
85
+ function _findFirstOverlap(x, y, w, h, boxes) {
86
+ const candidate = _toTextBoxAABB({ x, y, width: w, height: h });
87
+ return boxes.find((box) => intersectAABB(candidate, _toTextBoxAABB(box, TEXT_BOX_GAP / 2)));
88
+ }
89
+ function _toTextBoxAABB(rect, inflate = 0) {
90
+ return {
91
+ minX: rect.x - inflate,
92
+ minY: rect.y - inflate,
93
+ maxX: rect.x + rect.width + inflate,
94
+ maxY: rect.y + rect.height + inflate,
95
+ };
96
+ }
97
+ function _findSvgLayer(element) {
98
+ const internalDiv = element.querySelector(`.${VIEWPORT_ELEMENT}`);
99
+ return internalDiv?.querySelector(':scope > .svg-layer') || null;
6
100
  }
7
101
  function _determineCorners(canvasPoints) {
102
+ const p0 = canvasPoints[0];
103
+ if (!p0 || canvasPoints.length < 2) {
104
+ return { left: p0, right: p0, top: p0, bottom: p0 };
105
+ }
8
106
  const handlesLeftToRight = [canvasPoints[0], canvasPoints[1]].sort(_compareX);
9
107
  const handlesTopToBottom = [canvasPoints[0], canvasPoints[1]].sort(_compareY);
108
+ const left = handlesLeftToRight[0];
10
109
  const right = handlesLeftToRight[handlesLeftToRight.length - 1];
11
110
  const top = handlesTopToBottom[0];
12
111
  const bottom = handlesTopToBottom[handlesTopToBottom.length - 1];
13
112
  return {
113
+ left,
14
114
  top,
15
115
  bottom,
16
116
  right,
@@ -22,3 +122,13 @@ function _determineCorners(canvasPoints) {
22
122
  return a[1] < b[1] ? -1 : 1;
23
123
  }
24
124
  }
125
+ function _estimateTextBoxSize(textLines) {
126
+ const estimatedPadding = 25;
127
+ const estimatedCharWidth = 8;
128
+ const estimatedLineHeight = 17;
129
+ const longestLineLength = textLines.reduce((max, line) => Math.max(max, line?.length ?? 0), 0);
130
+ const lineCount = Math.max(textLines.length, 1);
131
+ const width = longestLineLength * estimatedCharWidth + estimatedPadding * 2;
132
+ const height = lineCount * estimatedLineHeight + estimatedPadding * 2;
133
+ return { width, height };
134
+ }
@@ -0,0 +1,9 @@
1
+ export interface TextBoxRect {
2
+ x: number;
3
+ y: number;
4
+ width: number;
5
+ height: number;
6
+ }
7
+ export declare function clearTextBoxRegistry(svgLayerElement: Element): void;
8
+ export declare function registerTextBox(svgLayerElement: Element, rect: TextBoxRect): void;
9
+ export declare function getRegisteredTextBoxes(svgLayerElement: Element): TextBoxRect[];
@@ -0,0 +1,20 @@
1
+ const registry = new WeakMap();
2
+ export function clearTextBoxRegistry(svgLayerElement) {
3
+ registry.set(svgLayerElement, []);
4
+ }
5
+ export function registerTextBox(svgLayerElement, rect) {
6
+ let boxes = registry.get(svgLayerElement);
7
+ if (!boxes) {
8
+ boxes = [];
9
+ registry.set(svgLayerElement, boxes);
10
+ }
11
+ boxes.push({
12
+ x: rect.x,
13
+ y: rect.y,
14
+ width: rect.width,
15
+ height: rect.height,
16
+ });
17
+ }
18
+ export function getRegisteredTextBoxes(svgLayerElement) {
19
+ return registry.get(svgLayerElement) || [];
20
+ }
@@ -1 +1 @@
1
- export declare const version = "4.17.0";
1
+ export declare const version = "4.17.2";
@@ -1 +1 @@
1
- export const version = '4.17.0';
1
+ export const version = '4.17.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cornerstonejs/tools",
3
- "version": "4.17.0",
3
+ "version": "4.17.2",
4
4
  "description": "Cornerstone3D Tools",
5
5
  "types": "./dist/esm/index.d.ts",
6
6
  "module": "./dist/esm/index.js",
@@ -108,7 +108,7 @@
108
108
  "canvas": "3.2.0"
109
109
  },
110
110
  "peerDependencies": {
111
- "@cornerstonejs/core": "4.17.0",
111
+ "@cornerstonejs/core": "4.17.2",
112
112
  "@kitware/vtk.js": "34.15.1",
113
113
  "@types/d3-array": "3.2.1",
114
114
  "@types/d3-interpolate": "3.0.4",
@@ -127,5 +127,5 @@
127
127
  "type": "individual",
128
128
  "url": "https://ohif.org/donate"
129
129
  },
130
- "gitHead": "f5a86e32f08c55617ab0c6213607ce341bb7b02c"
130
+ "gitHead": "1bf66a49411068a5351010e818f5a4ea3916434a"
131
131
  }
@@ -1,27 +0,0 @@
1
- import { createPointInEllipse } from '../fillCircle';
2
- describe('createPointInEllipse', () => {
3
- const corners = [
4
- [-1, 1, 0],
5
- [1, -1, 0],
6
- [-1, -1, 0],
7
- [1, 1, 0],
8
- ];
9
- it('detects points inside the base circle', () => {
10
- const predicate = createPointInEllipse(corners);
11
- expect(predicate([0, 0, 0])).toBe(true);
12
- expect(predicate([0.5, 0.5, 0])).toBe(true);
13
- expect(predicate([1.2, 0, 0])).toBe(false);
14
- });
15
- it('covers interpolated stroke segments', () => {
16
- const predicate = createPointInEllipse(corners, {
17
- strokePointsWorld: [
18
- [-2, 0, 0],
19
- [2, 0, 0],
20
- ],
21
- radius: 1,
22
- });
23
- expect(predicate([0, 0, 0])).toBe(true);
24
- expect(predicate([1.5, 0, 0])).toBe(true);
25
- expect(predicate([3.2, 0, 0])).toBe(false);
26
- });
27
- });