From ed524337789f1bc6687641252a80e374202dfa3a Mon Sep 17 00:00:00 2001 From: Adithyan Dinesh Date: Mon, 8 Jun 2026 19:33:06 +0530 Subject: [PATCH] Fix PHI bounding box label numbering to handle index gaps --- .../utils/phiBoundingBoxMeasurementUtils.ts | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/extensions/ohif-gradienthealth-extension/src/utils/phiBoundingBoxMeasurementUtils.ts b/extensions/ohif-gradienthealth-extension/src/utils/phiBoundingBoxMeasurementUtils.ts index 67c5bee..4f81f54 100644 --- a/extensions/ohif-gradienthealth-extension/src/utils/phiBoundingBoxMeasurementUtils.ts +++ b/extensions/ohif-gradienthealth-extension/src/utils/phiBoundingBoxMeasurementUtils.ts @@ -34,16 +34,28 @@ export function addAutoPHIBoundingBoxToolLabeler(servicesManager) { return; } - const phiMeasurementsCount = measurementService.getMeasurements( - (m) => m.toolName === phiBoundingBoxToolName - ).length; + const otherPhiMeasurements = measurementService.getMeasurements( + // Exclude the current measurement to avoid self-referencing during the add event + (m) => + m.toolName === phiBoundingBoxToolName && m.uid !== measurement.uid + ); + + const usedIndices = new Set(); + otherPhiMeasurements.forEach((m) => { + const match = m.label && m.label.match(/PHI Bounding Box (\d+)/); + if (match) { + usedIndices.add(parseInt(match[1], 10)); + } + }); + + let nextIndex = 1; + while (usedIndices.has(nextIndex)) { + nextIndex++; + } measurementService.update( measurement.uid, - { - ...measurement, - label: `PHI Bounding Box ${phiMeasurementsCount}`, - }, + { ...measurement, label: `PHI Bounding Box ${nextIndex}` }, true ); }