Skip to content

Commit d3b607c

Browse files
fix: planar freehand roi perimeter calculation (#2341)
* fix: planar freehand roi perimeter calculation * fix: Perimeter calculation --------- Co-authored-by: Bill Wallace <[email protected]>
1 parent 77c3c65 commit d3b607c

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

packages/tools/src/tools/annotation/PlanarFreehandROITool.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -865,13 +865,11 @@ class PlanarFreehandROITool extends ContourSegmentationBaseTool {
865865
} else {
866866
this.updateOpenCachedStats({
867867
metadata,
868-
canvasCoordinates,
869868
targetId,
870869
cachedStats,
871870
modalityUnit,
872871
calibratedScale,
873-
deltaInX,
874-
deltaInY,
872+
points,
875873
});
876874
}
877875
}
@@ -946,8 +944,7 @@ class PlanarFreehandROITool extends ContourSegmentationBaseTool {
946944
// Convert from canvas_pixels ^2 to mm^2
947945
area *= deltaInX * deltaInY;
948946

949-
let perimeter = calculatePerimeter(canvasCoordinates, closed) / scale;
950-
perimeter *= Math.sqrt(Math.pow(deltaInX, 2) + Math.pow(deltaInY, 2));
947+
const perimeter = calculatePerimeter(points, closed) / scale;
951948

952949
// Expand bounding box
953950
const iDelta = 0.01 * (iMax - iMin);
@@ -1043,17 +1040,14 @@ class PlanarFreehandROITool extends ContourSegmentationBaseTool {
10431040
protected updateOpenCachedStats({
10441041
targetId,
10451042
metadata,
1046-
canvasCoordinates,
10471043
cachedStats,
10481044
modalityUnit,
10491045
calibratedScale,
1050-
deltaInX,
1051-
deltaInY,
1046+
points,
10521047
}) {
10531048
const { scale, unit } = calibratedScale;
10541049

1055-
let length = calculatePerimeter(canvasCoordinates, closed) / scale;
1056-
length *= Math.sqrt(Math.pow(deltaInX, 2) + Math.pow(deltaInY, 2));
1050+
const length = calculatePerimeter(points, closed) / scale;
10571051

10581052
cachedStats[targetId] = {
10591053
Modality: metadata.Modality,

packages/tools/src/utilities/contours/calculatePerimeter.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import type { Types } from '@cornerstonejs/core';
2+
import { vec3 } from 'gl-matrix';
3+
14
/**
25
* Calculates the perimeter of a polyline.
36
*
47
* @param polyline - The polyline represented as an array of points.
58
* @param closed - Indicates whether the polyline is closed or not.
69
* @returns The perimeter of the polyline.
710
*/
8-
function calculatePerimeter(polyline: number[][], closed: boolean): number {
11+
export function calculatePerimeter(
12+
polyline: number[][],
13+
closed: boolean
14+
): number {
915
let perimeter = 0;
1016

1117
for (let i = 0; i < polyline.length - 1; i++) {
12-
const point1 = polyline[i];
13-
const point2 = polyline[i + 1];
14-
perimeter += Math.sqrt(
15-
Math.pow(point2[0] - point1[0], 2) + Math.pow(point2[1] - point1[1], 2)
16-
);
18+
const point1 = polyline[i] as Types.Point3;
19+
const point2 = polyline[i + 1] as Types.Point3;
20+
21+
perimeter += vec3.dist(point1, point2);
1722
}
1823

1924
if (closed) {
20-
const firstPoint = polyline[0];
21-
const lastPoint = polyline[polyline.length - 1];
22-
perimeter += Math.sqrt(
23-
Math.pow(lastPoint[0] - firstPoint[0], 2) +
24-
Math.pow(lastPoint[1] - firstPoint[1], 2)
25-
);
25+
const firstPoint = polyline[0] as Types.Point3;
26+
const lastPoint = polyline[polyline.length - 1] as Types.Point3;
27+
perimeter += vec3.dist(firstPoint, lastPoint);
2628
}
2729

2830
return perimeter;

0 commit comments

Comments
 (0)