@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.
- package/dist/cjs/eventDispatchers/keyboardEventHandlers/keyDown.js +2 -1
- package/dist/cjs/eventDispatchers/keyboardEventHandlers/keyDown.js.map +1 -1
- package/dist/cjs/stateManagement/annotation/annotationState.js +3 -0
- package/dist/cjs/stateManagement/annotation/annotationState.js.map +1 -1
- package/dist/cjs/tools/annotation/BidirectionalTool.js +7 -6
- package/dist/cjs/tools/annotation/BidirectionalTool.js.map +1 -1
- package/dist/cjs/types/index.d.ts +2 -1
- package/dist/cjs/utilities/contours/AnnotationToPointData.d.ts +11 -0
- package/dist/cjs/utilities/contours/AnnotationToPointData.js +44 -0
- package/dist/cjs/utilities/contours/AnnotationToPointData.js.map +1 -0
- package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.d.ts +6 -0
- package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.js +43 -0
- package/dist/cjs/utilities/contours/RectangleROIStartEndThreshold.js.map +1 -0
- package/dist/cjs/utilities/contours/contourFinder.d.ts +7 -0
- package/dist/cjs/utilities/contours/contourFinder.js +68 -0
- package/dist/cjs/utilities/contours/contourFinder.js.map +1 -0
- package/dist/cjs/utilities/contours/detectContourHoles.d.ts +5 -0
- package/dist/cjs/utilities/contours/detectContourHoles.js +78 -0
- package/dist/cjs/utilities/contours/detectContourHoles.js.map +1 -0
- package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.d.ts +4 -0
- package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.js +124 -0
- package/dist/cjs/utilities/contours/generateContourSetsFromLabelmap.js.map +1 -0
- package/dist/cjs/utilities/contours/index.d.ts +6 -0
- package/dist/cjs/utilities/contours/index.js +17 -0
- package/dist/cjs/utilities/contours/index.js.map +1 -0
- package/dist/cjs/utilities/contours/mergePoints.d.ts +8 -0
- package/dist/cjs/utilities/contours/mergePoints.js +77 -0
- package/dist/cjs/utilities/contours/mergePoints.js.map +1 -0
- package/dist/cjs/utilities/index.d.ts +2 -1
- package/dist/cjs/utilities/index.js +3 -1
- package/dist/cjs/utilities/index.js.map +1 -1
- package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.d.ts +1 -0
- package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.js +31 -0
- package/dist/cjs/utilities/segmentation/contourAndFindLargestBidirectional.js.map +1 -0
- package/dist/cjs/utilities/segmentation/createBidirectionalToolData.d.ts +14 -0
- package/dist/cjs/utilities/segmentation/createBidirectionalToolData.js +43 -0
- package/dist/cjs/utilities/segmentation/createBidirectionalToolData.js.map +1 -0
- package/dist/cjs/utilities/segmentation/findLargestBidirectional.d.ts +1 -0
- package/dist/cjs/utilities/segmentation/findLargestBidirectional.js +94 -0
- package/dist/cjs/utilities/segmentation/findLargestBidirectional.js.map +1 -0
- package/dist/cjs/utilities/segmentation/index.d.ts +4 -1
- package/dist/cjs/utilities/segmentation/index.js +7 -1
- package/dist/cjs/utilities/segmentation/index.js.map +1 -1
- package/dist/cjs/utilities/segmentation/isLineInSegment.d.ts +9 -0
- package/dist/cjs/utilities/segmentation/isLineInSegment.js +55 -0
- package/dist/cjs/utilities/segmentation/isLineInSegment.js.map +1 -0
- package/dist/cjs/utilities/segmentation/segmentContourAction.d.ts +17 -0
- package/dist/cjs/utilities/segmentation/segmentContourAction.js +122 -0
- package/dist/cjs/utilities/segmentation/segmentContourAction.js.map +1 -0
- package/dist/esm/eventDispatchers/keyboardEventHandlers/keyDown.js +2 -1
- package/dist/esm/eventDispatchers/keyboardEventHandlers/keyDown.js.map +1 -1
- package/dist/esm/stateManagement/annotation/annotationState.js +3 -0
- package/dist/esm/stateManagement/annotation/annotationState.js.map +1 -1
- package/dist/esm/tools/annotation/BidirectionalTool.js +7 -6
- package/dist/esm/tools/annotation/BidirectionalTool.js.map +1 -1
- package/dist/esm/utilities/contours/AnnotationToPointData.js +39 -0
- package/dist/esm/utilities/contours/AnnotationToPointData.js.map +1 -0
- package/dist/esm/utilities/contours/RectangleROIStartEndThreshold.js +41 -0
- package/dist/esm/utilities/contours/RectangleROIStartEndThreshold.js.map +1 -0
- package/dist/esm/utilities/contours/contourFinder.js +63 -0
- package/dist/esm/utilities/contours/contourFinder.js.map +1 -0
- package/dist/esm/utilities/contours/detectContourHoles.js +74 -0
- package/dist/esm/utilities/contours/detectContourHoles.js.map +1 -0
- package/dist/esm/utilities/contours/generateContourSetsFromLabelmap.js +117 -0
- package/dist/esm/utilities/contours/generateContourSetsFromLabelmap.js.map +1 -0
- package/dist/esm/utilities/contours/index.js +7 -0
- package/dist/esm/utilities/contours/index.js.map +1 -0
- package/dist/esm/utilities/contours/mergePoints.js +73 -0
- package/dist/esm/utilities/contours/mergePoints.js.map +1 -0
- package/dist/esm/utilities/index.js +2 -1
- package/dist/esm/utilities/index.js.map +1 -1
- package/dist/esm/utilities/segmentation/contourAndFindLargestBidirectional.js +25 -0
- package/dist/esm/utilities/segmentation/contourAndFindLargestBidirectional.js.map +1 -0
- package/dist/esm/utilities/segmentation/createBidirectionalToolData.js +40 -0
- package/dist/esm/utilities/segmentation/createBidirectionalToolData.js.map +1 -0
- package/dist/esm/utilities/segmentation/findLargestBidirectional.js +96 -0
- package/dist/esm/utilities/segmentation/findLargestBidirectional.js.map +1 -0
- package/dist/esm/utilities/segmentation/index.js +4 -1
- package/dist/esm/utilities/segmentation/index.js.map +1 -1
- package/dist/esm/utilities/segmentation/isLineInSegment.js +50 -0
- package/dist/esm/utilities/segmentation/isLineInSegment.js.map +1 -0
- package/dist/esm/utilities/segmentation/segmentContourAction.js +98 -0
- package/dist/esm/utilities/segmentation/segmentContourAction.js.map +1 -0
- package/dist/types/eventDispatchers/keyboardEventHandlers/keyDown.d.ts.map +1 -1
- package/dist/types/stateManagement/annotation/annotationState.d.ts.map +1 -1
- package/dist/types/tools/annotation/BidirectionalTool.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +2 -1
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/utilities/contours/AnnotationToPointData.d.ts +12 -0
- package/dist/types/utilities/contours/AnnotationToPointData.d.ts.map +1 -0
- package/dist/types/utilities/contours/RectangleROIStartEndThreshold.d.ts +7 -0
- package/dist/types/utilities/contours/RectangleROIStartEndThreshold.d.ts.map +1 -0
- package/dist/types/utilities/contours/contourFinder.d.ts +8 -0
- package/dist/types/utilities/contours/contourFinder.d.ts.map +1 -0
- package/dist/types/utilities/contours/detectContourHoles.d.ts +6 -0
- package/dist/types/utilities/contours/detectContourHoles.d.ts.map +1 -0
- package/dist/types/utilities/contours/generateContourSetsFromLabelmap.d.ts +5 -0
- package/dist/types/utilities/contours/generateContourSetsFromLabelmap.d.ts.map +1 -0
- package/dist/types/utilities/contours/index.d.ts +7 -0
- package/dist/types/utilities/contours/index.d.ts.map +1 -0
- package/dist/types/utilities/contours/mergePoints.d.ts +9 -0
- package/dist/types/utilities/contours/mergePoints.d.ts.map +1 -0
- package/dist/types/utilities/index.d.ts +2 -1
- package/dist/types/utilities/index.d.ts.map +1 -1
- package/dist/types/utilities/segmentation/contourAndFindLargestBidirectional.d.ts +2 -0
- package/dist/types/utilities/segmentation/contourAndFindLargestBidirectional.d.ts.map +1 -0
- package/dist/types/utilities/segmentation/createBidirectionalToolData.d.ts +15 -0
- package/dist/types/utilities/segmentation/createBidirectionalToolData.d.ts.map +1 -0
- package/dist/types/utilities/segmentation/findLargestBidirectional.d.ts +2 -0
- package/dist/types/utilities/segmentation/findLargestBidirectional.d.ts.map +1 -0
- package/dist/types/utilities/segmentation/index.d.ts +4 -1
- package/dist/types/utilities/segmentation/index.d.ts.map +1 -1
- package/dist/types/utilities/segmentation/isLineInSegment.d.ts +10 -0
- package/dist/types/utilities/segmentation/isLineInSegment.d.ts.map +1 -0
- package/dist/types/utilities/segmentation/segmentContourAction.d.ts +18 -0
- package/dist/types/utilities/segmentation/segmentContourAction.d.ts.map +1 -0
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.map +1 -1
- package/package.json +3 -3
- package/src/eventDispatchers/keyboardEventHandlers/keyDown.ts +7 -1
- package/src/stateManagement/annotation/annotationState.ts +3 -0
- package/src/tools/annotation/BidirectionalTool.ts +9 -5
- package/src/types/index.ts +2 -0
- package/src/utilities/contours/AnnotationToPointData.ts +61 -0
- package/src/utilities/contours/RectangleROIStartEndThreshold.ts +60 -0
- package/src/utilities/contours/contourFinder.ts +78 -0
- package/src/utilities/contours/detectContourHoles.ts +147 -0
- package/src/utilities/contours/generateContourSetsFromLabelmap.ts +160 -0
- package/src/utilities/contours/index.ts +14 -0
- package/src/utilities/contours/mergePoints.ts +108 -0
- package/src/utilities/index.ts +2 -0
- package/src/utilities/segmentation/contourAndFindLargestBidirectional.ts +46 -0
- package/src/utilities/segmentation/createBidirectionalToolData.ts +68 -0
- package/src/utilities/segmentation/findLargestBidirectional.ts +159 -0
- package/src/utilities/segmentation/index.ts +6 -0
- package/src/utilities/segmentation/isLineInSegment.ts +84 -0
- 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.
|
|
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.
|
|
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": "
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -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
|
+
};
|