@cornerstonejs/tools 1.41.0 → 1.42.0

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 (137) hide show
  1. package/dist/cjs/eventDispatchers/keyboardEventHandlers/keyDown.js +2 -1
  2. package/dist/cjs/eventDispatchers/keyboardEventHandlers/keyDown.js.map +1 -1
  3. package/dist/cjs/stateManagement/annotation/annotationState.js +3 -0
  4. package/dist/cjs/stateManagement/annotation/annotationState.js.map +1 -1
  5. package/dist/cjs/tools/annotation/BidirectionalTool.js +7 -6
  6. package/dist/cjs/tools/annotation/BidirectionalTool.js.map +1 -1
  7. package/dist/cjs/types/index.d.ts +2 -1
  8. package/dist/cjs/utilities/contours/AnnotationToPointData.d.ts +11 -0
  9. package/dist/cjs/utilities/contours/AnnotationToPointData.js +44 -0
  10. package/dist/cjs/utilities/contours/AnnotationToPointData.js.map +1 -0
  11. package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.d.ts +6 -0
  12. package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.js +43 -0
  13. package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.js.map +1 -0
  14. package/dist/cjs/utilities/contours/contourFinder.d.ts +7 -0
  15. package/dist/cjs/utilities/contours/contourFinder.js +68 -0
  16. package/dist/cjs/utilities/contours/contourFinder.js.map +1 -0
  17. package/dist/cjs/utilities/contours/detectContourHoles.d.ts +5 -0
  18. package/dist/cjs/utilities/contours/detectContourHoles.js +78 -0
  19. package/dist/cjs/utilities/contours/detectContourHoles.js.map +1 -0
  20. package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.d.ts +4 -0
  21. package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.js +124 -0
  22. package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.js.map +1 -0
  23. package/dist/cjs/utilities/contours/index.d.ts +6 -0
  24. package/dist/cjs/utilities/contours/index.js +17 -0
  25. package/dist/cjs/utilities/contours/index.js.map +1 -0
  26. package/dist/cjs/utilities/contours/mergePoints.d.ts +8 -0
  27. package/dist/cjs/utilities/contours/mergePoints.js +77 -0
  28. package/dist/cjs/utilities/contours/mergePoints.js.map +1 -0
  29. package/dist/cjs/utilities/index.d.ts +2 -1
  30. package/dist/cjs/utilities/index.js +3 -1
  31. package/dist/cjs/utilities/index.js.map +1 -1
  32. package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.d.ts +1 -0
  33. package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.js +31 -0
  34. package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.js.map +1 -0
  35. package/dist/cjs/utilities/segmentation/createBidirectionalToolData.d.ts +14 -0
  36. package/dist/cjs/utilities/segmentation/createBidirectionalToolData.js +43 -0
  37. package/dist/cjs/utilities/segmentation/createBidirectionalToolData.js.map +1 -0
  38. package/dist/cjs/utilities/segmentation/findLargestBidirectional.d.ts +1 -0
  39. package/dist/cjs/utilities/segmentation/findLargestBidirectional.js +94 -0
  40. package/dist/cjs/utilities/segmentation/findLargestBidirectional.js.map +1 -0
  41. package/dist/cjs/utilities/segmentation/index.d.ts +4 -1
  42. package/dist/cjs/utilities/segmentation/index.js +7 -1
  43. package/dist/cjs/utilities/segmentation/index.js.map +1 -1
  44. package/dist/cjs/utilities/segmentation/isLineInSegment.d.ts +9 -0
  45. package/dist/cjs/utilities/segmentation/isLineInSegment.js +55 -0
  46. package/dist/cjs/utilities/segmentation/isLineInSegment.js.map +1 -0
  47. package/dist/cjs/utilities/segmentation/segmentContourAction.d.ts +17 -0
  48. package/dist/cjs/utilities/segmentation/segmentContourAction.js +122 -0
  49. package/dist/cjs/utilities/segmentation/segmentContourAction.js.map +1 -0
  50. package/dist/esm/eventDispatchers/keyboardEventHandlers/keyDown.js +2 -1
  51. package/dist/esm/eventDispatchers/keyboardEventHandlers/keyDown.js.map +1 -1
  52. package/dist/esm/stateManagement/annotation/annotationState.js +3 -0
  53. package/dist/esm/stateManagement/annotation/annotationState.js.map +1 -1
  54. package/dist/esm/tools/annotation/BidirectionalTool.js +7 -6
  55. package/dist/esm/tools/annotation/BidirectionalTool.js.map +1 -1
  56. package/dist/esm/utilities/contours/AnnotationToPointData.js +39 -0
  57. package/dist/esm/utilities/contours/AnnotationToPointData.js.map +1 -0
  58. package/dist/esm/utilities/contours/RectangleROIStartEndThreshold.js +41 -0
  59. package/dist/esm/utilities/contours/RectangleROIStartEndThreshold.js.map +1 -0
  60. package/dist/esm/utilities/contours/contourFinder.js +63 -0
  61. package/dist/esm/utilities/contours/contourFinder.js.map +1 -0
  62. package/dist/esm/utilities/contours/detectContourHoles.js +74 -0
  63. package/dist/esm/utilities/contours/detectContourHoles.js.map +1 -0
  64. package/dist/esm/utilities/contours/generateContourSetsFromLabelmap.js +117 -0
  65. package/dist/esm/utilities/contours/generateContourSetsFromLabelmap.js.map +1 -0
  66. package/dist/esm/utilities/contours/index.js +7 -0
  67. package/dist/esm/utilities/contours/index.js.map +1 -0
  68. package/dist/esm/utilities/contours/mergePoints.js +73 -0
  69. package/dist/esm/utilities/contours/mergePoints.js.map +1 -0
  70. package/dist/esm/utilities/index.js +2 -1
  71. package/dist/esm/utilities/index.js.map +1 -1
  72. package/dist/esm/utilities/segmentation/contourAndFindLargestBidirectional.js +25 -0
  73. package/dist/esm/utilities/segmentation/contourAndFindLargestBidirectional.js.map +1 -0
  74. package/dist/esm/utilities/segmentation/createBidirectionalToolData.js +40 -0
  75. package/dist/esm/utilities/segmentation/createBidirectionalToolData.js.map +1 -0
  76. package/dist/esm/utilities/segmentation/findLargestBidirectional.js +96 -0
  77. package/dist/esm/utilities/segmentation/findLargestBidirectional.js.map +1 -0
  78. package/dist/esm/utilities/segmentation/index.js +4 -1
  79. package/dist/esm/utilities/segmentation/index.js.map +1 -1
  80. package/dist/esm/utilities/segmentation/isLineInSegment.js +50 -0
  81. package/dist/esm/utilities/segmentation/isLineInSegment.js.map +1 -0
  82. package/dist/esm/utilities/segmentation/segmentContourAction.js +98 -0
  83. package/dist/esm/utilities/segmentation/segmentContourAction.js.map +1 -0
  84. package/dist/types/eventDispatchers/keyboardEventHandlers/keyDown.d.ts.map +1 -1
  85. package/dist/types/stateManagement/annotation/annotationState.d.ts.map +1 -1
  86. package/dist/types/tools/annotation/BidirectionalTool.d.ts.map +1 -1
  87. package/dist/types/types/index.d.ts +2 -1
  88. package/dist/types/types/index.d.ts.map +1 -1
  89. package/dist/types/utilities/contours/AnnotationToPointData.d.ts +12 -0
  90. package/dist/types/utilities/contours/AnnotationToPointData.d.ts.map +1 -0
  91. package/dist/types/utilities/contours/RectangleROIStartEndThreshold.d.ts +7 -0
  92. package/dist/types/utilities/contours/RectangleROIStartEndThreshold.d.ts.map +1 -0
  93. package/dist/types/utilities/contours/contourFinder.d.ts +8 -0
  94. package/dist/types/utilities/contours/contourFinder.d.ts.map +1 -0
  95. package/dist/types/utilities/contours/detectContourHoles.d.ts +6 -0
  96. package/dist/types/utilities/contours/detectContourHoles.d.ts.map +1 -0
  97. package/dist/types/utilities/contours/generateContourSetsFromLabelmap.d.ts +5 -0
  98. package/dist/types/utilities/contours/generateContourSetsFromLabelmap.d.ts.map +1 -0
  99. package/dist/types/utilities/contours/index.d.ts +7 -0
  100. package/dist/types/utilities/contours/index.d.ts.map +1 -0
  101. package/dist/types/utilities/contours/mergePoints.d.ts +9 -0
  102. package/dist/types/utilities/contours/mergePoints.d.ts.map +1 -0
  103. package/dist/types/utilities/index.d.ts +2 -1
  104. package/dist/types/utilities/index.d.ts.map +1 -1
  105. package/dist/types/utilities/segmentation/contourAndFindLargestBidirectional.d.ts +2 -0
  106. package/dist/types/utilities/segmentation/contourAndFindLargestBidirectional.d.ts.map +1 -0
  107. package/dist/types/utilities/segmentation/createBidirectionalToolData.d.ts +15 -0
  108. package/dist/types/utilities/segmentation/createBidirectionalToolData.d.ts.map +1 -0
  109. package/dist/types/utilities/segmentation/findLargestBidirectional.d.ts +2 -0
  110. package/dist/types/utilities/segmentation/findLargestBidirectional.d.ts.map +1 -0
  111. package/dist/types/utilities/segmentation/index.d.ts +4 -1
  112. package/dist/types/utilities/segmentation/index.d.ts.map +1 -1
  113. package/dist/types/utilities/segmentation/isLineInSegment.d.ts +10 -0
  114. package/dist/types/utilities/segmentation/isLineInSegment.d.ts.map +1 -0
  115. package/dist/types/utilities/segmentation/segmentContourAction.d.ts +18 -0
  116. package/dist/types/utilities/segmentation/segmentContourAction.d.ts.map +1 -0
  117. package/dist/umd/index.js +1 -1
  118. package/dist/umd/index.js.map +1 -1
  119. package/package.json +3 -3
  120. package/src/eventDispatchers/keyboardEventHandlers/keyDown.ts +7 -1
  121. package/src/stateManagement/annotation/annotationState.ts +3 -0
  122. package/src/tools/annotation/BidirectionalTool.ts +9 -5
  123. package/src/types/index.ts +2 -0
  124. package/src/utilities/contours/AnnotationToPointData.ts +61 -0
  125. package/src/utilities/contours/RectangleROIStartEndThreshold.ts +60 -0
  126. package/src/utilities/contours/contourFinder.ts +78 -0
  127. package/src/utilities/contours/detectContourHoles.ts +147 -0
  128. package/src/utilities/contours/generateContourSetsFromLabelmap.ts +160 -0
  129. package/src/utilities/contours/index.ts +14 -0
  130. package/src/utilities/contours/mergePoints.ts +108 -0
  131. package/src/utilities/index.ts +2 -0
  132. package/src/utilities/segmentation/contourAndFindLargestBidirectional.ts +46 -0
  133. package/src/utilities/segmentation/createBidirectionalToolData.ts +68 -0
  134. package/src/utilities/segmentation/findLargestBidirectional.ts +159 -0
  135. package/src/utilities/segmentation/index.ts +6 -0
  136. package/src/utilities/segmentation/isLineInSegment.ts +84 -0
  137. package/src/utilities/segmentation/segmentContourAction.ts +169 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cornerstonejs/tools",
3
- "version": "1.41.0",
3
+ "version": "1.42.0",
4
4
  "description": "Cornerstone3D Tools",
5
5
  "main": "src/index.ts",
6
6
  "types": "dist/types/index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "webpack:watch": "webpack --mode development --progress --watch --config ./.webpack/webpack.dev.js"
30
30
  },
31
31
  "dependencies": {
32
- "@cornerstonejs/core": "^1.41.0",
32
+ "@cornerstonejs/core": "^1.42.0",
33
33
  "comlink": "^4.4.1",
34
34
  "lodash.clonedeep": "4.5.0",
35
35
  "lodash.get": "^4.4.2"
@@ -53,5 +53,5 @@
53
53
  "type": "individual",
54
54
  "url": "https://ohif.org/donate"
55
55
  },
56
- "gitHead": "a151cd133119685c23a9aa7b739e5b7a55312bd1"
56
+ "gitHead": "75102c80b6bc3714f00590b92ccd8d4d52bf1ca2"
57
57
  }
@@ -34,7 +34,13 @@ export default function keyDown(evt: KeyDownEventType): void {
34
34
  if (activeToolsWithEventBinding?.size) {
35
35
  const { element } = evt.detail;
36
36
  for (const [key, value] of [...activeToolsWithEventBinding.entries()]) {
37
- key[value.method](element);
37
+ // Calls the method that implements the action, which can be a string
38
+ // in which case it belongs to the tool instance, or a function
39
+ // Call it on the tool instance, with the element and configuration value
40
+ // so that the method can depend on the specific configuration in use.
41
+ const method =
42
+ typeof value.method === 'function' ? value.method : key[value.method];
43
+ method.call(key, element, value);
38
44
  }
39
45
  }
40
46
  }
@@ -126,6 +126,9 @@ function getNumberOfAnnotations(
126
126
  * @param annotationUID - The unique identifier for the annotation.
127
127
  */
128
128
  function removeAnnotation(annotationUID: string): void {
129
+ if (!annotationUID) {
130
+ return;
131
+ }
129
132
  const manager = getAnnotationManager();
130
133
  const annotation = manager.getAnnotation(annotationUID);
131
134
 
@@ -1348,19 +1348,23 @@ class BidirectionalTool extends AnnotationTool {
1348
1348
  }
1349
1349
 
1350
1350
  function defaultGetTextLines(data, targetId): string[] {
1351
- const { cachedStats } = data;
1351
+ const { cachedStats, label } = data;
1352
1352
  const { length, width, unit } = cachedStats[targetId];
1353
1353
 
1354
+ const textLines = [];
1355
+ if (label) {
1356
+ textLines.push(label);
1357
+ }
1354
1358
  if (length === undefined) {
1355
- return;
1359
+ return textLines;
1356
1360
  }
1357
1361
 
1358
1362
  // spaceBetweenSlices & pixelSpacing &
1359
1363
  // magnitude in each direction? Otherwise, this is "px"?
1360
- const textLines = [
1364
+ textLines.push(
1361
1365
  `L: ${roundNumber(length)} ${unit}`,
1362
- `W: ${roundNumber(width)} ${unit}`,
1363
- ];
1366
+ `W: ${roundNumber(width)} ${unit}`
1367
+ );
1364
1368
 
1365
1369
  return textLines;
1366
1370
  }
@@ -73,11 +73,13 @@ import type { ISpline } from './ISpline';
73
73
  import type { SplineCurveSegment } from './SplineCurveSegment';
74
74
  import type { SplineLineSegment } from './SplineLineSegment';
75
75
  import type { SplineProps } from './SplineProps';
76
+ import type { BidirectionalData } from '../utilities/segmentation/createBidirectionalToolData';
76
77
 
77
78
  export type {
78
79
  // AnnotationState
79
80
  Annotation,
80
81
  Annotations,
82
+ BidirectionalData,
81
83
  CanvasCoordinates,
82
84
  IAnnotationManager,
83
85
  GroupSpecificAnnotations,
@@ -0,0 +1,61 @@
1
+ import RectangleROIStartEndThreshold from './RectangleROIStartEndThreshold';
2
+
3
+ function validateAnnotation(annotation) {
4
+ if (!annotation?.data) {
5
+ throw new Error('Tool data is empty');
6
+ }
7
+
8
+ if (!annotation.metadata || annotation.metadata.referenceImageId) {
9
+ throw new Error('Tool data is not associated with any imageId');
10
+ }
11
+ }
12
+
13
+ class AnnotationToPointData {
14
+ static TOOL_NAMES: Record<string, any> = {};
15
+
16
+ constructor() {
17
+ // empty
18
+ }
19
+
20
+ static convert(annotation, index, metadataProvider) {
21
+ validateAnnotation(annotation);
22
+
23
+ const { toolName } = annotation.metadata;
24
+ const toolClass = AnnotationToPointData.TOOL_NAMES[toolName];
25
+
26
+ if (!toolClass) {
27
+ throw new Error(
28
+ `Unknown tool type: ${toolName}, cannot convert to RTSSReport`
29
+ );
30
+ }
31
+
32
+ // Each toolData should become a list of contours, ContourSequence
33
+ // contains a list of contours with their pointData, their geometry
34
+ // type and their length.
35
+ const ContourSequence = toolClass.getContourSequence(
36
+ annotation,
37
+ metadataProvider
38
+ );
39
+
40
+ // Todo: random rgb color for now, options should be passed in
41
+ const color = [
42
+ Math.floor(Math.random() * 255),
43
+ Math.floor(Math.random() * 255),
44
+ Math.floor(Math.random() * 255),
45
+ ];
46
+
47
+ return {
48
+ ReferencedROINumber: index + 1,
49
+ ROIDisplayColor: color,
50
+ ContourSequence,
51
+ };
52
+ }
53
+
54
+ static register(toolClass) {
55
+ AnnotationToPointData.TOOL_NAMES[toolClass.toolName] = toolClass;
56
+ }
57
+ }
58
+
59
+ AnnotationToPointData.register(RectangleROIStartEndThreshold);
60
+
61
+ export default AnnotationToPointData;
@@ -0,0 +1,60 @@
1
+ // comment
2
+ class RectangleROIStartEndThreshold {
3
+ static toolName: string;
4
+
5
+ constructor() {
6
+ // empty
7
+ }
8
+
9
+ static getContourSequence(toolData, metadataProvider) {
10
+ const { data } = toolData;
11
+ const { projectionPoints, projectionPointsImageIds } = data.cachedStats;
12
+
13
+ return projectionPoints.map((point, index) => {
14
+ const ContourData = getPointData(point);
15
+ const ContourImageSequence = getContourImageSequence(
16
+ projectionPointsImageIds[index],
17
+ metadataProvider
18
+ );
19
+
20
+ return {
21
+ NumberOfContourPoints: ContourData.length / 3,
22
+ ContourImageSequence,
23
+ ContourGeometricType: 'CLOSED_PLANAR',
24
+ ContourData,
25
+ };
26
+ });
27
+ }
28
+ }
29
+
30
+ RectangleROIStartEndThreshold.toolName = 'RectangleROIStartEndThreshold';
31
+
32
+ function getPointData(points) {
33
+ // Since this is a closed contour, the order of the points is important.
34
+ // re-order the points to be in the correct order clockwise
35
+ // Spread to make sure Float32Arrays are converted to arrays
36
+ const orderedPoints = [
37
+ ...points[0],
38
+ ...points[1],
39
+ ...points[3],
40
+ ...points[2],
41
+ ];
42
+ const pointsArray = orderedPoints.flat();
43
+
44
+ // reduce the precision of the points to 2 decimal places
45
+ const pointsArrayWithPrecision = pointsArray.map((point) => {
46
+ return point.toFixed(2);
47
+ });
48
+
49
+ return pointsArrayWithPrecision;
50
+ }
51
+
52
+ function getContourImageSequence(imageId, metadataProvider) {
53
+ const sopCommon = metadataProvider.get('sopCommonModule', imageId);
54
+
55
+ return {
56
+ ReferencedSOPClassUID: sopCommon.sopClassUID,
57
+ ReferencedSOPInstanceUID: sopCommon.sopInstanceUID,
58
+ };
59
+ }
60
+ export default RectangleROIStartEndThreshold;
@@ -0,0 +1,78 @@
1
+ function findNextLink(line, lines, contourPoints) {
2
+ let index = -1;
3
+ lines.forEach((cell, i) => {
4
+ if (index >= 0) {
5
+ return;
6
+ }
7
+
8
+ if (cell.a == line.b) {
9
+ index = i;
10
+ }
11
+ });
12
+
13
+ if (index >= 0) {
14
+ const nextLine = lines[index];
15
+ lines.splice(index, 1);
16
+
17
+ contourPoints.push(nextLine.b);
18
+
19
+ if (contourPoints[0] == nextLine.b) {
20
+ return {
21
+ remainingLines: lines,
22
+ contourPoints,
23
+ type: 'CLOSED_PLANAR',
24
+ //type: 'CLOSEDPLANAR_XOR',
25
+ };
26
+ }
27
+
28
+ return findNextLink(nextLine, lines, contourPoints);
29
+ }
30
+
31
+ return {
32
+ remainingLines: lines,
33
+ contourPoints,
34
+ type: 'OPEN_PLANAR',
35
+ };
36
+ }
37
+
38
+ /**
39
+ *
40
+ * @param {*} lines
41
+ */
42
+ export function findContours(lines) {
43
+ if (lines.length == 0) {
44
+ return [];
45
+ }
46
+
47
+ const contourPoints = [];
48
+
49
+ const firstCell = lines.shift();
50
+ contourPoints.push(firstCell.a);
51
+ contourPoints.push(firstCell.b);
52
+ const result = findNextLink(firstCell, lines, contourPoints);
53
+
54
+ if (result.remainingLines.length == 0) {
55
+ return [
56
+ {
57
+ type: result.type,
58
+ contourPoints: result.contourPoints,
59
+ },
60
+ ];
61
+ } else {
62
+ const extraContours = findContours(result.remainingLines);
63
+ extraContours.push({
64
+ type: result.type,
65
+ contourPoints: result.contourPoints,
66
+ });
67
+ return extraContours;
68
+ }
69
+ }
70
+
71
+ export function findContoursFromReducedSet(lines) {
72
+ return findContours(lines);
73
+ }
74
+
75
+ export default {
76
+ findContours,
77
+ findContoursFromReducedSet,
78
+ };
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Functions for detecting and dealing with holes within contours.
3
+ * Expected Contour format:
4
+ * {
5
+ * type,
6
+ * contourPoints
7
+ * }
8
+ */
9
+
10
+ /**
11
+ * Checks if point is inside polygon defined by vertices array
12
+ * Code from
13
+ * https://stackoverflow.com/questions/22521982/check-if-point-is-inside-a-polygon
14
+ * most original version based on:
15
+ * https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html
16
+ * @param {*} point
17
+ * @param {*} vertices
18
+ * @returns
19
+ */
20
+ const getIsPointInsidePolygon = (point, vertices) => {
21
+ const x = point[0];
22
+ const y = point[1];
23
+
24
+ let inside = false;
25
+ for (let i = 0, j = vertices.length - 1; i < vertices.length; j = i++) {
26
+ const xi = vertices[i][0],
27
+ yi = vertices[i][1];
28
+ const xj = vertices[j][0],
29
+ yj = vertices[j][1];
30
+
31
+ const intersect =
32
+ yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
33
+ if (intersect) {
34
+ inside = !inside;
35
+ }
36
+ }
37
+
38
+ return inside;
39
+ };
40
+
41
+ /**
42
+ * Check if inner contour is completely surrounded by outer contour.
43
+ * @param {*} outerContour
44
+ * @param {*} innerContour
45
+ * @returns
46
+ */
47
+ function checkEnclosed(outerContour, innerContour, points) {
48
+ const vertices = [];
49
+ outerContour.contourPoints.forEach((point) => {
50
+ vertices.push([points[point][0], points[point][1]]);
51
+ });
52
+
53
+ let pointsNotEnclosed = 0;
54
+ innerContour.contourPoints.forEach((point) => {
55
+ const result = getIsPointInsidePolygon(
56
+ [points[point][0], points[point][1]],
57
+ vertices
58
+ );
59
+ //console.log(result);
60
+
61
+ if (!result) {
62
+ pointsNotEnclosed++;
63
+ }
64
+ });
65
+
66
+ return pointsNotEnclosed === 0;
67
+ }
68
+
69
+ /**
70
+ * Check if contours have holes, if so update contour accordingly
71
+ * @param {*} polyData
72
+ * @param {*} bypass
73
+ */
74
+ export function processContourHoles(contours, points, useXOR = true) {
75
+ //console.log(points);
76
+
77
+ // Add non-closed planars to contour list
78
+ const retContours = contours.filter(
79
+ (contour) => contour.type !== 'CLOSED_PLANAR'
80
+ );
81
+
82
+ // Find closed planar contours
83
+ const closedContours = contours.filter(
84
+ (contour) => contour.type === 'CLOSED_PLANAR'
85
+ );
86
+
87
+ // Iterate through each contour in list check for contours that have holes
88
+ const contourWithHoles = [];
89
+ let contourWithoutHoles = [];
90
+ closedContours.forEach((contour, index) => {
91
+ const holes = [];
92
+
93
+ // Check if any other contour is a hole surrounded by current contour
94
+ closedContours.forEach((hContour, hIndex) => {
95
+ if (index != hIndex) {
96
+ // Check if inner loop contour is a hole of outer loop contour
97
+ if (checkEnclosed(contour, hContour, points)) {
98
+ holes.push(hIndex);
99
+ }
100
+ }
101
+ });
102
+
103
+ // Check if holes were found
104
+ if (holes.length > 0) {
105
+ // Note current contour and reference of its holes
106
+ contourWithHoles.push({
107
+ contour,
108
+ holes,
109
+ });
110
+ } else {
111
+ // Note contour index without holes
112
+ contourWithoutHoles.push(index);
113
+ }
114
+ });
115
+
116
+ if (useXOR) {
117
+ // XOR method
118
+ contourWithHoles.forEach((contourHoleSet) => {
119
+ // Modify contour with hole to type CLOSEDPLANAR_XOR
120
+ contourHoleSet.contour.type = 'CLOSEDPLANAR_XOR';
121
+ retContours.push(contourHoleSet.contour);
122
+
123
+ contourHoleSet.holes.forEach((holeIndex) => {
124
+ // Modify hole type to CLOSEDPLANAR_XOR
125
+ // and add to contour list to be returned
126
+ closedContours[holeIndex].type = 'CLOSEDPLANAR_XOR';
127
+ retContours.push(closedContours[holeIndex]);
128
+
129
+ // Remove hole from list of contours without holes
130
+ contourWithoutHoles = contourWithoutHoles.filter((contourIndex) => {
131
+ return contourIndex !== holeIndex;
132
+ });
133
+ });
134
+ });
135
+
136
+ // Add remaining contours to list (neither hole nor have holes)
137
+ contourWithoutHoles.forEach((contourIndex) => {
138
+ retContours.push(closedContours[contourIndex]);
139
+ });
140
+ } else {
141
+ // Keyhole method, not implemented
142
+ }
143
+
144
+ return retContours;
145
+ }
146
+
147
+ export default { processContourHoles };
@@ -0,0 +1,160 @@
1
+ import { cache as cornerstoneCache } from '@cornerstonejs/core';
2
+ import vtkImageMarchingSquares from '@kitware/vtk.js/Filters/General/ImageMarchingSquares';
3
+ import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
4
+ import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
5
+
6
+ import { removeDuplicatePoints } from './mergePoints';
7
+ import { findContoursFromReducedSet } from './contourFinder';
8
+ import SegmentationRepresentations from '../../enums/SegmentationRepresentations';
9
+
10
+ const { Labelmap } = SegmentationRepresentations;
11
+
12
+ function generateContourSetsFromLabelmap({ segmentations }) {
13
+ const { representationData, segments = [0, 1] } = segmentations;
14
+ const { volumeId: segVolumeId } = representationData[Labelmap];
15
+
16
+ // Get segmentation volume
17
+ const vol = cornerstoneCache.getVolume(segVolumeId);
18
+ if (!vol) {
19
+ console.warn(`No volume found for ${segVolumeId}`);
20
+ return;
21
+ }
22
+
23
+ const numSlices = vol.dimensions[2];
24
+
25
+ // Get image volume segmentation references
26
+ const imageVol = cornerstoneCache.getVolume(vol.referencedVolumeId);
27
+ if (!imageVol) {
28
+ console.warn(`No volume found for ${vol.referencedVolumeId}`);
29
+ return;
30
+ }
31
+
32
+ // NOTE: Workaround for marching squares not finding closed contours at
33
+ // boundary of image volume, clear pixels along x-y border of volume
34
+ const segData = vol.imageData.getPointData().getScalars().getData();
35
+ const pixelsPerSlice = vol.dimensions[0] * vol.dimensions[1];
36
+
37
+ for (let z = 0; z < numSlices; z++) {
38
+ for (let y = 0; y < vol.dimensions[1]; y++) {
39
+ const index = y * vol.dimensions[0] + z * pixelsPerSlice;
40
+ segData[index] = 0;
41
+ segData[index + vol.dimensions[0] - 1] = 0;
42
+ }
43
+ }
44
+
45
+ // end workaround
46
+ //
47
+ //
48
+ const ContourSets = [];
49
+
50
+ const { FrameOfReferenceUID } = imageVol.metadata;
51
+ // Iterate through all segments in current segmentation set
52
+ const numSegments = segments.length;
53
+ for (let segIndex = 0; segIndex < numSegments; segIndex++) {
54
+ const segment = segments[segIndex];
55
+
56
+ // Skip empty segments
57
+ if (!segment) {
58
+ continue;
59
+ }
60
+
61
+ const sliceContours = [];
62
+ const scalars = vtkDataArray.newInstance({
63
+ name: 'Scalars',
64
+ numberOfComponents: 1,
65
+ size: pixelsPerSlice * numSlices,
66
+ dataType: 'Uint8Array',
67
+ });
68
+ const { containedSegmentIndices } = segment;
69
+ for (let sliceIndex = 0; sliceIndex < numSlices; sliceIndex++) {
70
+ // Check if the slice is empty before running marching cube
71
+ if (
72
+ isSliceEmptyForSegment(sliceIndex, segData, pixelsPerSlice, segIndex)
73
+ ) {
74
+ continue;
75
+ }
76
+ const frameStart = sliceIndex * pixelsPerSlice;
77
+
78
+ try {
79
+ // Modify segData for this specific segment directly
80
+ for (let i = 0; i < pixelsPerSlice; i++) {
81
+ const value = segData[i + frameStart];
82
+ if (value === segIndex || containedSegmentIndices?.has(value)) {
83
+ (scalars as any).setValue(i + frameStart, 1);
84
+ } else {
85
+ (scalars as any).setValue(i, 0);
86
+ }
87
+ }
88
+
89
+ const mSquares = vtkImageMarchingSquares.newInstance({
90
+ slice: sliceIndex,
91
+ });
92
+
93
+ // filter out the scalar data so that only it has background and
94
+ // the current segment index
95
+ const imageDataCopy = vtkImageData.newInstance();
96
+
97
+ imageDataCopy.shallowCopy(vol.imageData);
98
+ imageDataCopy.getPointData().setScalars(scalars);
99
+
100
+ // Connect pipeline
101
+ mSquares.setInputData(imageDataCopy);
102
+ const cValues = [1];
103
+ mSquares.setContourValues(cValues);
104
+ mSquares.setMergePoints(false);
105
+
106
+ // Perform marching squares
107
+ const msOutput = mSquares.getOutputData();
108
+
109
+ // Clean up output from marching squares
110
+ const reducedSet = removeDuplicatePoints(msOutput);
111
+ if (reducedSet.points?.length) {
112
+ const contours = findContoursFromReducedSet(reducedSet.lines);
113
+
114
+ sliceContours.push({
115
+ referencedImageId: imageVol.imageIds[sliceIndex],
116
+ contours,
117
+ polyData: reducedSet,
118
+ FrameNumber: sliceIndex + 1,
119
+ sliceIndex,
120
+ FrameOfReferenceUID,
121
+ });
122
+ }
123
+ } catch (e) {
124
+ console.warn(sliceIndex);
125
+ console.warn(e);
126
+ }
127
+ }
128
+
129
+ const metadata = {
130
+ referencedImageId: imageVol.imageIds[0], // just use 0 for overall
131
+ FrameOfReferenceUID,
132
+ };
133
+
134
+ const ContourSet = {
135
+ label: segment.label,
136
+ color: segment.color,
137
+ metadata,
138
+ sliceContours,
139
+ };
140
+
141
+ ContourSets.push(ContourSet);
142
+ }
143
+
144
+ return ContourSets;
145
+ }
146
+
147
+ function isSliceEmptyForSegment(sliceIndex, segData, pixelsPerSlice, segIndex) {
148
+ const startIdx = sliceIndex * pixelsPerSlice;
149
+ const endIdx = startIdx + pixelsPerSlice;
150
+
151
+ for (let i = startIdx; i < endIdx; i++) {
152
+ if (segData[i] === segIndex) {
153
+ return false;
154
+ }
155
+ }
156
+
157
+ return true;
158
+ }
159
+
160
+ export { generateContourSetsFromLabelmap };
@@ -0,0 +1,14 @@
1
+ import contourFinder from './contourFinder';
2
+ import mergePoints from './mergePoints';
3
+ import detectContourHoles from './detectContourHoles';
4
+ import { generateContourSetsFromLabelmap } from './generateContourSetsFromLabelmap';
5
+
6
+ import AnnotationToPointData from './AnnotationToPointData';
7
+
8
+ export {
9
+ contourFinder,
10
+ mergePoints,
11
+ detectContourHoles,
12
+ generateContourSetsFromLabelmap,
13
+ AnnotationToPointData,
14
+ };