All files / packages/tools/src/utilities/segmentation createMergedLabelmapForIndex.ts

0% Statements 0/16
0% Branches 0/10
0% Functions 0/3
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import { Types } from '@cornerstonejs/core';
import { volumeLoader, utilities as csUtils } from '@cornerstonejs/core';
 
/**
 * Given a list of labelmaps (with the possibility of overlapping regions), and
 * a segmentIndex it creates a new labelmap with the same dimensions as the input labelmaps,
 * but merges them into a single labelmap for the segmentIndex. It wipes out
 * all other segment Indices. This is useful for calculating statistics regarding
 * a specific segment when there are overlapping regions between labelmap (e.g. TMTV)
 *
 * @param labelmaps - Array of labelmaps
 * @param segmentIndex - The segment index to merge
 * @returns Merged labelmap
 */
function createMergedLabelmapForIndex(
  labelmaps: Array<Types.IImageVolume>,
  segmentIndex = 1,
  volumeId = 'mergedLabelmap'
): Types.IImageVolume {
  labelmaps.forEach(({ direction, dimensions, origin, spacing }) => {
    if (
      !csUtils.isEqual(dimensions, labelmaps[0].dimensions) ||
      !csUtils.isEqual(direction, labelmaps[0].direction) ||
      !csUtils.isEqual(spacing, labelmaps[0].spacing) ||
      !csUtils.isEqual(origin, labelmaps[0].origin)
    ) {
      throw new Error('labelmaps must have the same size and shape');
    }
  });
 
  const labelmap = labelmaps[0];
 
  const arrayType = (labelmap.getScalarData() as any).constructor;
  const outputData = new arrayType(labelmap.getScalarData().length);
 
  labelmaps.forEach((labelmap) => {
    const scalarData = labelmap.getScalarData();
    for (let i = 0; i < scalarData.length; i++) {
      if (scalarData[i] === segmentIndex) {
        outputData[i] = segmentIndex;
      }
    }
  });
 
  const options = {
    scalarData: outputData,
    metadata: labelmap.metadata,
    spacing: labelmap.spacing,
    origin: labelmap.origin,
    direction: labelmap.direction,
    dimensions: labelmap.dimensions,
  };
 
  const preventCache = true;
  // Todo: following should be async
  const mergedVolume = volumeLoader.createLocalVolume(
    options,
    volumeId,
    preventCache
  );
 
  return mergedVolume;
}
 
export default createMergedLabelmapForIndex;