Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
md-prog committed Mar 27, 2023
1 parent 687b277 commit 0ada1b7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 63 deletions.
32 changes: 8 additions & 24 deletions src/tools/annotation/CircleRoiTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
import calculateSUV from './../../util/calculateSUV.js';
import { calculateEllipseStatistics } from './../../util/ellipse/index.js';
import getROITextBoxCoords from '../../util/getROITextBoxCoords.js';
import numbersWithCommas from './../../util/numbersWithCommas.js';
import throttle from './../../util/throttle.js';
import { getLogger } from '../../util/logger.js';
import getPixelSpacing from '../../util/getPixelSpacing';
Expand Down Expand Up @@ -369,30 +368,15 @@ function _createTextBoxContent(
hasPixelSpacing,
options
);
const { radius = 0, perimeter = 0 } = circleRoiContent;

const circleTextLines = [textLines[0]];

if (radius) {
circleTextLines.push(_formatLength(radius, 'Radius', hasPixelSpacing));
}
if (perimeter) {
circleTextLines.push(
_formatLength(perimeter, 'Perimeter', hasPixelSpacing)
);
}
textLines.forEach((l, i) => i !== 0 && circleTextLines.push(l));

return circleTextLines;
}

function _formatLength(value, name, hasPixelSpacing) {
if (!value) {
return '';
}
const suffix = hasPixelSpacing ? ' mm' : ' px';

return `${name}: ${numbersWithCommas(value.toFixed(1))}${suffix}`;
return [
textLines.area,
textLines.radius,
textLines.perimeter,
textLines.mean,
textLines.stdDev,
textLines.minMax,
].filter(t => Boolean(t));
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/tools/annotation/EllipticalRoiTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,21 @@ function _createTextBoxContent(
hasPixelSpacing,
options = {}
) {
return createTextBoxContent(
const textLines = createTextBoxContent(
context,
isColorImage,
ellipticalRoiContent,
modality,
hasPixelSpacing,
options
);

return [
textLines.area,
textLines.mean,
textLines.stdDev,
textLines.minMax,
].filter(t => Boolean(t));
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/tools/annotation/RectangleRoiTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,19 @@ function _createTextBoxContent(
hasPixelSpacing,
options = {}
) {
return createTextBoxContent(
const textLines = createTextBoxContent(
context,
isColorImage,
rectangleRoiContent,
modality,
hasPixelSpacing,
options
);

return [
textLines.area,
textLines.mean,
textLines.stdDev,
textLines.minMax,
].filter(t => Boolean(t));
}
96 changes: 59 additions & 37 deletions src/tools/annotation/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,78 @@ export function getUnit(modality, showHounsfieldUnits) {
export function createTextBoxContent(
context,
isColorImage,
{ area = 0, mean = 0, stdDev = 0, min = 0, max = 0, meanStdDevSUV = 0 } = {},
{ area, mean, stdDev, min, max, meanStdDevSUV, radius, perimeter } = {},
modality,
hasPixelSpacing,
options = {}
) {
const showMinMax = options.showMinMax || false;
const textLines = [];
const textLines = {};

// Don't display mean/standardDev for color images
const otherLines = [];
textLines.area = _formatArea(area, hasPixelSpacing);
if (radius) {
textLines.radius = _formatLength(radius, 'Radius', hasPixelSpacing);
}
if (perimeter) {
textLines.perimeter = _formatLength(radius, 'perimeter', hasPixelSpacing);
}

if (!isColorImage) {
const hasStandardUptakeValues = meanStdDevSUV && meanStdDevSUV.mean !== 0;
const unit = getUnit(modality, options.showHounsfieldUnits);
const unit = ` ${getUnit(modality, options.showHounsfieldUnits)}`;

let meanString = `Mean: ${numbersWithCommas(mean.toFixed(2))} ${unit}`;
const stdDevString = `Std Dev: ${numbersWithCommas(
stdDev.toFixed(2)
)} ${unit}`;
let meanStr = _formatTextLine('Mean', mean, unit, 2);
const stdDevStr = _formatTextLine('Std Dev', stdDev, unit, 2);

// If this image has SUV values to display, concatenate them to the text line
if (hasStandardUptakeValues) {
const SUVtext = ' SUV: ';

const meanSuvString = `${SUVtext}${numbersWithCommas(
meanStdDevSUV.mean.toFixed(2)
)}`;
const stdDevSuvString = `${SUVtext}${numbersWithCommas(
meanStdDevSUV.stdDev.toFixed(2)
)}`;
const meanSuvStr = _formatTextLine(SUVtext, meanStdDevSUV.mean, '', 2);
const stdDevSuvStr = _formatTextLine(
SUVtext,
meanStdDevSUV.stdDev,
'',
2
);

const targetStringLength = Math.floor(
context.measureText(`${stdDevString} `).width
const targetStrLength = Math.floor(
context.measureText(`${stdDevStr} `).width
);

while (context.measureText(meanString).width < targetStringLength) {
meanString += ' ';
}
meanStr = _padSpace(meanStr, context, targetStrLength);

otherLines.push(`${meanString}${meanSuvString}`);
otherLines.push(`${stdDevString} ${stdDevSuvString}`);
textLines.mean = `${meanStr}${meanSuvStr}`;
textLines.stdDev = `${stdDevStr} ${stdDevSuvStr}`;
} else {
otherLines.push(`${meanString}`);
otherLines.push(`${stdDevString}`);
textLines.mean = `${meanStr}`;
textLines.stdDev = `${stdDevStr}`;
}

if (showMinMax) {
let minString = `Min: ${min} ${unit}`;
const maxString = `Max: ${max} ${unit}`;
const targetStringLength = hasStandardUptakeValues
? Math.floor(context.measureText(`${stdDevString} `).width)
: Math.floor(context.measureText(`${meanString} `).width);
let minStr = _formatTextLine('Min', min, unit, 2);
const maxStr = _formatTextLine('Max', max, unit, 2);
const targetStrLength = hasStandardUptakeValues
? Math.floor(context.measureText(`${stdDevStr} `).width)
: Math.floor(context.measureText(`${meanStr} `).width);

while (context.measureText(minString).width < targetStringLength) {
minString += ' ';
}
minStr = _padSpace(minStr, context, targetStrLength);

otherLines.push(`${minString}${maxString}`);
textLines.minMax = `${minStr}${maxStr}`;
}
}

textLines.push(_formatArea(area, hasPixelSpacing));
otherLines.forEach(x => textLines.push(x));

return textLines;
}

function _padSpace(str, context, targetLength) {
while (context.measureText(str).width < targetLength) {
str += ' ';
}

return str;
}

/**
*
*
Expand All @@ -98,5 +103,22 @@ function _formatArea(area, hasPixelSpacing) {
? ` mm${String.fromCharCode(178)}`
: ` px${String.fromCharCode(178)}`;

return `Area: ${numbersWithCommas(area.toFixed(2))}${suffix}`;
return _formatTextLine('Area', area, 2, suffix);
}

function _formatLength(value, name, hasPixelSpacing) {
if (value === null || value === undefined) {
return '';
}
const suffix = hasPixelSpacing ? ' mm' : ' px';

return _formatTextLine(name, value, suffix, 1);
}

function _formatTextLine(name, value, suffix, round = 2) {
if (value === null || value === undefined) {
return '';
}

return `${name}: ${numbersWithCommas(value.toFixed(round))}${suffix}`;
}

0 comments on commit 0ada1b7

Please sign in to comment.