All files / platform/core/src/utils combineFrameInstance.ts

75% Statements 48/64
71.42% Branches 25/35
100% Functions 8/8
74.19% Lines 46/62

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209                                      204x           8583370x   8583370x 62x     8583308x 8583308x     8583308x     8583308x           152072x 99x       152072x   152072x 1774x 1774x   5322x 10074x 1774x     1774x 261x           261x           261x   261x             261x           152072x 48x             152072x         152072x           152072x     152072x           152072x       8431236x                                                                                         8431236x                 304144x 292833x     11311x 11311x         11311x 1798x   9513x     47514x 47514x       9513x 38001x       38001x 85540x     9513x        
import { vec3 } from 'gl-matrix';
import { dicomSplit } from './dicomSplit';
 
/**
 * Combine the Per instance frame data, the shared frame data
 * and the root data objects.
 * The data is combined by taking nested sequence objects within
 * the functional group sequences.  Data that is directly contained
 * within the functional group sequences, such as private creators
 * will be ignored.
 * This can be safely called with an undefined frame in order to handle
 * single frame data. (eg frame is undefined is the same as frame===1).
 *
 * Note: instances carry non-enumerable runtime props (frameNumber, imageId,
 * url, wadoRoot, ...). This is intentional: dcmjs serialization skips them and
 * spreads/copies (including of anything from `metaData.get('instance', ...)`)
 * deliberately do not carry them — frameNumber in particular must not be
 * copied onto other objects. Read runtime props off the original instance.
 */
const combineFrameInstance = (frame, instance) => {
  const {
    PerFrameFunctionalGroupsSequence,
    SharedFunctionalGroupsSequence,
    NumberOfFrames,
    ImageType,
  } = instance;
 
  if (NumberOfFrames < 2) {
    return instance;
  }
 
  instance.ImageType = dicomSplit(ImageType);
  const frameNumber = Number.parseInt(frame || 1);
 
  const hasDetectorButMissingSpatialInfo =
    instance.DetectorInformationSequence &&
    (!instance.ImagePositionPatient || !instance.ImageOrientationPatient);
 
  if (
    (PerFrameFunctionalGroupsSequence && SharedFunctionalGroupsSequence) ||
    hasDetectorButMissingSpatialInfo || NumberOfFrames > 1
  ) {
    // this is to fix NM multiframe datasets with position and orientation
    // information inside DetectorInformationSequence
    if (!instance.ImageOrientationPatient && instance.DetectorInformationSequence) {
      instance.ImageOrientationPatient =
        instance.DetectorInformationSequence[0].ImageOrientationPatient;
    }
 
    let ImagePositionPatientToUse = instance.ImagePositionPatient;
 
    if (!instance.ImagePositionPatient && instance.DetectorInformationSequence) {
      let imagePositionPatient = instance.DetectorInformationSequence[0].ImagePositionPatient;
      let imageOrientationPatient = instance.ImageOrientationPatient;
 
      imagePositionPatient = imagePositionPatient?.map(it => Number(it));
      imageOrientationPatient = imageOrientationPatient?.map(it => Number(it));
      const SpacingBetweenSlices = Number(instance.SpacingBetweenSlices);
 
      // Calculate the position for the current frame
      if (imageOrientationPatient && SpacingBetweenSlices) {
        const rowOrientation = vec3.fromValues(
          imageOrientationPatient[0],
          imageOrientationPatient[1],
          imageOrientationPatient[2]
        );
 
        const colOrientation = vec3.fromValues(
          imageOrientationPatient[3],
          imageOrientationPatient[4],
          imageOrientationPatient[5]
        );
 
        const normalVector = vec3.cross(vec3.create(), rowOrientation, colOrientation);
 
        const position = vec3.scaleAndAdd(
          vec3.create(),
          imagePositionPatient,
          normalVector,
          SpacingBetweenSlices * (frameNumber - 1)
        );
 
        ImagePositionPatientToUse = [position[0], position[1], position[2]];
      }
    }
 
    // Cache the _parentInstance at the top level as a full copy to prevent
    // setting values hard.
    if (!instance._parentInstance) {
      Object.defineProperty(instance, '_parentInstance', {
        value: { ...instance },
        enumerable: false,
        writable: false,
        configurable: false,
      });
    }
    const sharedInstance = createCombinedValue(
      instance._parentInstance,
      SharedFunctionalGroupsSequence?.[0],
      '_shared'
    );
    const newInstance = createCombinedValue(
      sharedInstance,
      PerFrameFunctionalGroupsSequence?.[frameNumber - 1],
      frameNumber
    );
 
    newInstance.ImagePositionPatient = ImagePositionPatientToUse ??
      newInstance.ImagePositionPatient ?? [0, 0, frameNumber];
 
    Object.defineProperty(newInstance, 'frameNumber', {
      value: frameNumber,
      writable: true,
      enumerable: false,
      configurable: true,
    });
    return newInstance;
  }
 
  // For RTDOSE datasets
  Iif (instance.GridFrameOffsetVector) {
    Iif (!instance._parentInstance) {
      Object.defineProperty(instance, '_parentInstance', {
        value: { ...instance },
        enumerable: false,
        writable: false,
        configurable: false,
      });
    }
 
    const sharedInstance = createCombinedValue(
      instance._parentInstance,
      SharedFunctionalGroupsSequence?.[0],
      '_shared'
    );
 
    const newInstance = createCombinedValue(
      sharedInstance,
      PerFrameFunctionalGroupsSequence?.[frameNumber - 1],
      frameNumber
    );
 
    const origin = newInstance.ImagePositionPatient?.map(Number);
    const orientation = newInstance.ImageOrientationPatient?.map(Number);
    const offset = Number(instance.GridFrameOffsetVector[frameNumber - 1]);
 
    Iif (origin && orientation && !Number.isNaN(offset)) {
      const row = vec3.fromValues(orientation[0], orientation[1], orientation[2]);
      const col = vec3.fromValues(orientation[3], orientation[4], orientation[5]);
      const normal = vec3.cross(vec3.create(), row, col);
 
      const position = vec3.scaleAndAdd(vec3.create(), vec3.fromValues(...origin), normal, offset);
      newInstance.ImagePositionPatient = [position[0], position[1], position[2]];
    }
 
    Object.defineProperty(newInstance, 'frameNumber', {
      value: frameNumber,
      writable: true,
      enumerable: false,
      configurable: true,
    });
 
    return newInstance;
  }
 
  return instance;
};
 
/**
 * Creates a combined instance stored in the parent object which
 * inherits from the parent instance the attributes in the functional groups.
 * The storage key in the parent is in key
 */
function createCombinedValue(parent, functionalGroups, key) {
  if (parent[key]) {
    return parent[key];
  }
  // Exclude any proxying values
  const newInstance = Object.create(parent);
  Object.defineProperty(parent, key, {
    value: newInstance,
    writable: false,
    enumerable: false,
  });
  if (!functionalGroups) {
    return newInstance;
  }
  const shared = functionalGroups
    ? Object.values(functionalGroups)
        .filter(Boolean)
        .map(it => it[0])
        .filter(it => typeof it === 'object')
    : [];
 
  // merge the shared first then the per frame to override
  [...shared].forEach(item => {
    Iif (item.SOPInstanceUID) {
      // This sub-item is a previous value information item, so don't merge it
      return;
    }
    Object.entries(item).forEach(([key, value]) => {
      newInstance[key] = value;
    });
  });
  return newInstance;
}
 
export default combineFrameInstance;