@cornerstonejs/tools 1.40.3 → 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 (211) hide show
  1. package/dist/cjs/drawingSvg/drawHandle.d.ts +4 -0
  2. package/dist/cjs/drawingSvg/drawHandle.js +66 -0
  3. package/dist/cjs/drawingSvg/drawHandle.js.map +1 -0
  4. package/dist/cjs/drawingSvg/drawHandles.js +4 -60
  5. package/dist/cjs/drawingSvg/drawHandles.js.map +1 -1
  6. package/dist/cjs/drawingSvg/index.d.ts +2 -1
  7. package/dist/cjs/drawingSvg/index.js +3 -1
  8. package/dist/cjs/drawingSvg/index.js.map +1 -1
  9. package/dist/cjs/eventDispatchers/keyboardEventHandlers/keyDown.js +2 -1
  10. package/dist/cjs/eventDispatchers/keyboardEventHandlers/keyDown.js.map +1 -1
  11. package/dist/cjs/index.d.ts +2 -2
  12. package/dist/cjs/index.js +3 -2
  13. package/dist/cjs/index.js.map +1 -1
  14. package/dist/cjs/stateManagement/annotation/annotationState.js +3 -0
  15. package/dist/cjs/stateManagement/annotation/annotationState.js.map +1 -1
  16. package/dist/cjs/tools/annotation/AngleTool.js.map +1 -1
  17. package/dist/cjs/tools/annotation/BidirectionalTool.js +7 -6
  18. package/dist/cjs/tools/annotation/BidirectionalTool.js.map +1 -1
  19. package/dist/cjs/tools/annotation/LengthTool.js +4 -3
  20. package/dist/cjs/tools/annotation/LengthTool.js.map +1 -1
  21. package/dist/cjs/tools/annotation/ProbeTool.js +24 -3
  22. package/dist/cjs/tools/annotation/ProbeTool.js.map +1 -1
  23. package/dist/cjs/tools/annotation/UltrasoundDirectionalTool.d.ts +36 -0
  24. package/dist/cjs/tools/annotation/UltrasoundDirectionalTool.js +483 -0
  25. package/dist/cjs/tools/annotation/UltrasoundDirectionalTool.js.map +1 -0
  26. package/dist/cjs/tools/index.d.ts +2 -1
  27. package/dist/cjs/tools/index.js +3 -1
  28. package/dist/cjs/tools/index.js.map +1 -1
  29. package/dist/cjs/types/ToolSpecificAnnotationTypes.d.ts +28 -0
  30. package/dist/cjs/types/index.d.ts +2 -1
  31. package/dist/cjs/utilities/contours/AnnotationToPointData.d.ts +11 -0
  32. package/dist/cjs/utilities/contours/AnnotationToPointData.js +44 -0
  33. package/dist/cjs/utilities/contours/AnnotationToPointData.js.map +1 -0
  34. package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.d.ts +6 -0
  35. package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.js +43 -0
  36. package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.js.map +1 -0
  37. package/dist/cjs/utilities/contours/contourFinder.d.ts +7 -0
  38. package/dist/cjs/utilities/contours/contourFinder.js +68 -0
  39. package/dist/cjs/utilities/contours/contourFinder.js.map +1 -0
  40. package/dist/cjs/utilities/contours/detectContourHoles.d.ts +5 -0
  41. package/dist/cjs/utilities/contours/detectContourHoles.js +78 -0
  42. package/dist/cjs/utilities/contours/detectContourHoles.js.map +1 -0
  43. package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.d.ts +4 -0
  44. package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.js +124 -0
  45. package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.js.map +1 -0
  46. package/dist/cjs/utilities/contours/index.d.ts +6 -0
  47. package/dist/cjs/utilities/contours/index.js +17 -0
  48. package/dist/cjs/utilities/contours/index.js.map +1 -0
  49. package/dist/cjs/utilities/contours/mergePoints.d.ts +8 -0
  50. package/dist/cjs/utilities/contours/mergePoints.js +77 -0
  51. package/dist/cjs/utilities/contours/mergePoints.js.map +1 -0
  52. package/dist/cjs/utilities/getCalibratedUnits.d.ts +16 -2
  53. package/dist/cjs/utilities/getCalibratedUnits.js +127 -5
  54. package/dist/cjs/utilities/getCalibratedUnits.js.map +1 -1
  55. package/dist/cjs/utilities/index.d.ts +2 -1
  56. package/dist/cjs/utilities/index.js +3 -1
  57. package/dist/cjs/utilities/index.js.map +1 -1
  58. package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.d.ts +1 -0
  59. package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.js +31 -0
  60. package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.js.map +1 -0
  61. package/dist/cjs/utilities/segmentation/createBidirectionalToolData.d.ts +14 -0
  62. package/dist/cjs/utilities/segmentation/createBidirectionalToolData.js +43 -0
  63. package/dist/cjs/utilities/segmentation/createBidirectionalToolData.js.map +1 -0
  64. package/dist/cjs/utilities/segmentation/findLargestBidirectional.d.ts +1 -0
  65. package/dist/cjs/utilities/segmentation/findLargestBidirectional.js +94 -0
  66. package/dist/cjs/utilities/segmentation/findLargestBidirectional.js.map +1 -0
  67. package/dist/cjs/utilities/segmentation/index.d.ts +4 -1
  68. package/dist/cjs/utilities/segmentation/index.js +7 -1
  69. package/dist/cjs/utilities/segmentation/index.js.map +1 -1
  70. package/dist/cjs/utilities/segmentation/isLineInSegment.d.ts +9 -0
  71. package/dist/cjs/utilities/segmentation/isLineInSegment.js +55 -0
  72. package/dist/cjs/utilities/segmentation/isLineInSegment.js.map +1 -0
  73. package/dist/cjs/utilities/segmentation/segmentContourAction.d.ts +17 -0
  74. package/dist/cjs/utilities/segmentation/segmentContourAction.js +122 -0
  75. package/dist/cjs/utilities/segmentation/segmentContourAction.js.map +1 -0
  76. package/dist/esm/drawingSvg/drawHandle.js +61 -0
  77. package/dist/esm/drawingSvg/drawHandle.js.map +1 -0
  78. package/dist/esm/drawingSvg/drawHandles.js +4 -60
  79. package/dist/esm/drawingSvg/drawHandles.js.map +1 -1
  80. package/dist/esm/drawingSvg/index.js +2 -1
  81. package/dist/esm/drawingSvg/index.js.map +1 -1
  82. package/dist/esm/eventDispatchers/keyboardEventHandlers/keyDown.js +2 -1
  83. package/dist/esm/eventDispatchers/keyboardEventHandlers/keyDown.js.map +1 -1
  84. package/dist/esm/index.js +2 -2
  85. package/dist/esm/index.js.map +1 -1
  86. package/dist/esm/stateManagement/annotation/annotationState.js +3 -0
  87. package/dist/esm/stateManagement/annotation/annotationState.js.map +1 -1
  88. package/dist/esm/tools/annotation/AngleTool.js.map +1 -1
  89. package/dist/esm/tools/annotation/BidirectionalTool.js +7 -6
  90. package/dist/esm/tools/annotation/BidirectionalTool.js.map +1 -1
  91. package/dist/esm/tools/annotation/LengthTool.js +5 -4
  92. package/dist/esm/tools/annotation/LengthTool.js.map +1 -1
  93. package/dist/esm/tools/annotation/ProbeTool.js +24 -3
  94. package/dist/esm/tools/annotation/ProbeTool.js.map +1 -1
  95. package/dist/esm/tools/annotation/UltrasoundDirectionalTool.js +478 -0
  96. package/dist/esm/tools/annotation/UltrasoundDirectionalTool.js.map +1 -0
  97. package/dist/esm/tools/index.js +2 -1
  98. package/dist/esm/tools/index.js.map +1 -1
  99. package/dist/esm/utilities/contours/AnnotationToPointData.js +39 -0
  100. package/dist/esm/utilities/contours/AnnotationToPointData.js.map +1 -0
  101. package/dist/esm/utilities/contours/RectangleROIStartEndThreshold.js +41 -0
  102. package/dist/esm/utilities/contours/RectangleROIStartEndThreshold.js.map +1 -0
  103. package/dist/esm/utilities/contours/contourFinder.js +63 -0
  104. package/dist/esm/utilities/contours/contourFinder.js.map +1 -0
  105. package/dist/esm/utilities/contours/detectContourHoles.js +74 -0
  106. package/dist/esm/utilities/contours/detectContourHoles.js.map +1 -0
  107. package/dist/esm/utilities/contours/generateContourSetsFromLabelmap.js +117 -0
  108. package/dist/esm/utilities/contours/generateContourSetsFromLabelmap.js.map +1 -0
  109. package/dist/esm/utilities/contours/index.js +7 -0
  110. package/dist/esm/utilities/contours/index.js.map +1 -0
  111. package/dist/esm/utilities/contours/mergePoints.js +73 -0
  112. package/dist/esm/utilities/contours/mergePoints.js.map +1 -0
  113. package/dist/esm/utilities/getCalibratedUnits.js +125 -6
  114. package/dist/esm/utilities/getCalibratedUnits.js.map +1 -1
  115. package/dist/esm/utilities/index.js +2 -1
  116. package/dist/esm/utilities/index.js.map +1 -1
  117. package/dist/esm/utilities/segmentation/contourAndFindLargestBidirectional.js +25 -0
  118. package/dist/esm/utilities/segmentation/contourAndFindLargestBidirectional.js.map +1 -0
  119. package/dist/esm/utilities/segmentation/createBidirectionalToolData.js +40 -0
  120. package/dist/esm/utilities/segmentation/createBidirectionalToolData.js.map +1 -0
  121. package/dist/esm/utilities/segmentation/findLargestBidirectional.js +96 -0
  122. package/dist/esm/utilities/segmentation/findLargestBidirectional.js.map +1 -0
  123. package/dist/esm/utilities/segmentation/index.js +4 -1
  124. package/dist/esm/utilities/segmentation/index.js.map +1 -1
  125. package/dist/esm/utilities/segmentation/isLineInSegment.js +50 -0
  126. package/dist/esm/utilities/segmentation/isLineInSegment.js.map +1 -0
  127. package/dist/esm/utilities/segmentation/segmentContourAction.js +98 -0
  128. package/dist/esm/utilities/segmentation/segmentContourAction.js.map +1 -0
  129. package/dist/types/drawingSvg/drawHandle.d.ts +5 -0
  130. package/dist/types/drawingSvg/drawHandle.d.ts.map +1 -0
  131. package/dist/types/drawingSvg/drawHandles.d.ts.map +1 -1
  132. package/dist/types/drawingSvg/index.d.ts +2 -1
  133. package/dist/types/drawingSvg/index.d.ts.map +1 -1
  134. package/dist/types/eventDispatchers/keyboardEventHandlers/keyDown.d.ts.map +1 -1
  135. package/dist/types/index.d.ts +2 -2
  136. package/dist/types/index.d.ts.map +1 -1
  137. package/dist/types/stateManagement/annotation/annotationState.d.ts.map +1 -1
  138. package/dist/types/tools/annotation/AngleTool.d.ts.map +1 -1
  139. package/dist/types/tools/annotation/BidirectionalTool.d.ts.map +1 -1
  140. package/dist/types/tools/annotation/LengthTool.d.ts.map +1 -1
  141. package/dist/types/tools/annotation/ProbeTool.d.ts.map +1 -1
  142. package/dist/types/tools/annotation/UltrasoundDirectionalTool.d.ts +37 -0
  143. package/dist/types/tools/annotation/UltrasoundDirectionalTool.d.ts.map +1 -0
  144. package/dist/types/tools/index.d.ts +2 -1
  145. package/dist/types/tools/index.d.ts.map +1 -1
  146. package/dist/types/types/ToolSpecificAnnotationTypes.d.ts +28 -0
  147. package/dist/types/types/ToolSpecificAnnotationTypes.d.ts.map +1 -1
  148. package/dist/types/types/index.d.ts +2 -1
  149. package/dist/types/types/index.d.ts.map +1 -1
  150. package/dist/types/utilities/contours/AnnotationToPointData.d.ts +12 -0
  151. package/dist/types/utilities/contours/AnnotationToPointData.d.ts.map +1 -0
  152. package/dist/types/utilities/contours/RectangleROIStartEndThreshold.d.ts +7 -0
  153. package/dist/types/utilities/contours/RectangleROIStartEndThreshold.d.ts.map +1 -0
  154. package/dist/types/utilities/contours/contourFinder.d.ts +8 -0
  155. package/dist/types/utilities/contours/contourFinder.d.ts.map +1 -0
  156. package/dist/types/utilities/contours/detectContourHoles.d.ts +6 -0
  157. package/dist/types/utilities/contours/detectContourHoles.d.ts.map +1 -0
  158. package/dist/types/utilities/contours/generateContourSetsFromLabelmap.d.ts +5 -0
  159. package/dist/types/utilities/contours/generateContourSetsFromLabelmap.d.ts.map +1 -0
  160. package/dist/types/utilities/contours/index.d.ts +7 -0
  161. package/dist/types/utilities/contours/index.d.ts.map +1 -0
  162. package/dist/types/utilities/contours/mergePoints.d.ts +9 -0
  163. package/dist/types/utilities/contours/mergePoints.d.ts.map +1 -0
  164. package/dist/types/utilities/getCalibratedUnits.d.ts +16 -2
  165. package/dist/types/utilities/getCalibratedUnits.d.ts.map +1 -1
  166. package/dist/types/utilities/index.d.ts +2 -1
  167. package/dist/types/utilities/index.d.ts.map +1 -1
  168. package/dist/types/utilities/segmentation/contourAndFindLargestBidirectional.d.ts +2 -0
  169. package/dist/types/utilities/segmentation/contourAndFindLargestBidirectional.d.ts.map +1 -0
  170. package/dist/types/utilities/segmentation/createBidirectionalToolData.d.ts +15 -0
  171. package/dist/types/utilities/segmentation/createBidirectionalToolData.d.ts.map +1 -0
  172. package/dist/types/utilities/segmentation/findLargestBidirectional.d.ts +2 -0
  173. package/dist/types/utilities/segmentation/findLargestBidirectional.d.ts.map +1 -0
  174. package/dist/types/utilities/segmentation/index.d.ts +4 -1
  175. package/dist/types/utilities/segmentation/index.d.ts.map +1 -1
  176. package/dist/types/utilities/segmentation/isLineInSegment.d.ts +10 -0
  177. package/dist/types/utilities/segmentation/isLineInSegment.d.ts.map +1 -0
  178. package/dist/types/utilities/segmentation/segmentContourAction.d.ts +18 -0
  179. package/dist/types/utilities/segmentation/segmentContourAction.d.ts.map +1 -0
  180. package/dist/umd/index.js +1 -1
  181. package/dist/umd/index.js.map +1 -1
  182. package/package.json +3 -3
  183. package/src/drawingSvg/drawHandle.ts +88 -0
  184. package/src/drawingSvg/drawHandles.ts +9 -75
  185. package/src/drawingSvg/index.ts +2 -0
  186. package/src/eventDispatchers/keyboardEventHandlers/keyDown.ts +7 -1
  187. package/src/index.ts +2 -0
  188. package/src/stateManagement/annotation/annotationState.ts +3 -0
  189. package/src/tools/annotation/AngleTool.ts +0 -1
  190. package/src/tools/annotation/BidirectionalTool.ts +9 -5
  191. package/src/tools/annotation/LengthTool.ts +6 -8
  192. package/src/tools/annotation/ProbeTool.ts +31 -7
  193. package/src/tools/annotation/UltrasoundDirectionalTool.ts +916 -0
  194. package/src/tools/index.ts +2 -0
  195. package/src/types/ToolSpecificAnnotationTypes.ts +29 -0
  196. package/src/types/index.ts +2 -0
  197. package/src/utilities/contours/AnnotationToPointData.ts +61 -0
  198. package/src/utilities/contours/RectangleROIStartEndThreshold.ts +60 -0
  199. package/src/utilities/contours/contourFinder.ts +78 -0
  200. package/src/utilities/contours/detectContourHoles.ts +147 -0
  201. package/src/utilities/contours/generateContourSetsFromLabelmap.ts +160 -0
  202. package/src/utilities/contours/index.ts +14 -0
  203. package/src/utilities/contours/mergePoints.ts +108 -0
  204. package/src/utilities/getCalibratedUnits.ts +203 -7
  205. package/src/utilities/index.ts +2 -0
  206. package/src/utilities/segmentation/contourAndFindLargestBidirectional.ts +46 -0
  207. package/src/utilities/segmentation/createBidirectionalToolData.ts +68 -0
  208. package/src/utilities/segmentation/findLargestBidirectional.ts +159 -0
  209. package/src/utilities/segmentation/index.ts +6 -0
  210. package/src/utilities/segmentation/isLineInSegment.ts +84 -0
  211. package/src/utilities/segmentation/segmentContourAction.ts +169 -0
@@ -32,6 +32,7 @@ import LivewireContourTool from './annotation/LivewireContourTool';
32
32
  import ArrowAnnotateTool from './annotation/ArrowAnnotateTool';
33
33
  import AngleTool from './annotation/AngleTool';
34
34
  import CobbAngleTool from './annotation/CobbAngleTool';
35
+ import UltrasoundDirectionalTool from './annotation/UltrasoundDirectionalTool';
35
36
  import KeyImageTool from './annotation/KeyImageTool';
36
37
 
37
38
  // Segmentation DisplayTool
@@ -81,6 +82,7 @@ export {
81
82
  ArrowAnnotateTool,
82
83
  AngleTool,
83
84
  CobbAngleTool,
85
+ UltrasoundDirectionalTool,
84
86
  KeyImageTool,
85
87
  // Segmentations Display
86
88
  SegmentationDisplayTool,
@@ -346,6 +346,35 @@ export interface AngleAnnotation extends Annotation {
346
346
  };
347
347
  }
348
348
 
349
+ export interface UltrasoundDirectionalAnnotation extends Annotation {
350
+ data: {
351
+ handles: {
352
+ points: Types.Point3[];
353
+ activeHandleIndex: number | null;
354
+ textBox: {
355
+ hasMoved: boolean;
356
+ worldPosition: Types.Point3;
357
+ worldBoundingBox: {
358
+ topLeft: Types.Point3;
359
+ topRight: Types.Point3;
360
+ bottomLeft: Types.Point3;
361
+ bottomRight: Types.Point3;
362
+ };
363
+ };
364
+ };
365
+ label: string;
366
+ cachedStats: {
367
+ [targetId: string]: {
368
+ xValues: number[];
369
+ yValues: number[];
370
+ units: string[];
371
+ isHorizontal: boolean;
372
+ isUnitless: boolean;
373
+ };
374
+ };
375
+ };
376
+ }
377
+
349
378
  export interface CobbAngleAnnotation extends Annotation {
350
379
  data: {
351
380
  handles: {
@@ -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
+ };
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Checks if point is within array
3
+ * @param {*} array
4
+ * @param {*} pt
5
+ * @returns
6
+ */
7
+ function ptInArray(array, pt) {
8
+ let index = -1;
9
+ for (let i = 0; i < array.length; i++) {
10
+ if (isSamePoint(pt, array[i])) {
11
+ index = i;
12
+ }
13
+ }
14
+ return index;
15
+ }
16
+
17
+ /**
18
+ * Checks if point A and point B contain same values
19
+ * @param {*} ptA
20
+ * @param {*} ptB
21
+ * @returns
22
+ */
23
+ function isSamePoint(ptA, ptB) {
24
+ if (ptA[0] == ptB[0] && ptA[1] == ptB[1] && ptA[2] == ptB[2]) {
25
+ return true;
26
+ } else {
27
+ return false;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Goes through linesArray and replaces all references of old index with new index
33
+ * @param {*} linesArray
34
+ * @param {*} oldIndex
35
+ * @param {*} newIndex
36
+ */
37
+ function replacePointIndexReferences(linesArray, oldIndex, newIndex) {
38
+ for (let i = 0; i < linesArray.length; i++) {
39
+ const line = linesArray[i];
40
+ if (line.a == oldIndex) {
41
+ line.a = newIndex;
42
+ } else if (line.b == oldIndex) {
43
+ line.b = newIndex;
44
+ }
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Iterate through polyData from vtkjs and merge any points that are the same
50
+ * then update merged point references within lines array
51
+ * @param {*} polyData
52
+ * @param {*} bypass
53
+ * @returns
54
+ */
55
+ export function removeDuplicatePoints(polyData, bypass = false) {
56
+ const points = polyData.getPoints();
57
+ const lines = polyData.getLines();
58
+
59
+ const pointsArray = [];
60
+ for (let i = 0; i < points.getNumberOfPoints(); i++) {
61
+ const pt = points.getPoint(i).slice();
62
+ pointsArray.push(pt);
63
+ }
64
+ const linesArray = [];
65
+ for (let i = 0; i < lines.getNumberOfCells(); i++) {
66
+ const cell = lines.getCell(i * 3).slice();
67
+ //console.log(JSON.stringify(cell));
68
+ const a = cell[0];
69
+ const b = cell[1];
70
+ const line = {
71
+ a,
72
+ b,
73
+ };
74
+ linesArray.push(line);
75
+ }
76
+
77
+ if (bypass) {
78
+ return { points: pointsArray, lines: linesArray };
79
+ }
80
+
81
+ // Iterate through points and replace any duplicates
82
+ const newPoints = [];
83
+ for (let i = 0; i < pointsArray.length; i++) {
84
+ const pt = pointsArray[i];
85
+ let index = ptInArray(newPoints, pt);
86
+
87
+ if (index >= 0) {
88
+ // Duplicate Point -> replace references in lines
89
+ replacePointIndexReferences(linesArray, i, index);
90
+ } else {
91
+ index = newPoints.length;
92
+ newPoints.push(pt);
93
+ replacePointIndexReferences(linesArray, i, index);
94
+ }
95
+ }
96
+
97
+ // Final pass through lines, remove any that refer to exact same point
98
+ const newLines = [];
99
+ linesArray.forEach((line) => {
100
+ if (line.a != line.b) {
101
+ newLines.push(line);
102
+ }
103
+ });
104
+
105
+ return { points: newPoints, lines: newLines };
106
+ }
107
+
108
+ export default { removeDuplicatePoints };