All files / Sources/Rendering/Core/ImageArrayMapper index.js

86.79% Statements 92/106
63.41% Branches 26/41
85% Functions 17/20
86.27% Lines 88/102

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 210 211 212 213 214 215 216 217 218 219 220              1x 1x 1x               5x         5x 54x 54x                 54x       5x 8x 8x 8x 16x   8x     5x 5x       5x 5x         5x 4x 4x     5x         5x 17x 17x 1x 16x     16x 16x       1x     5x 5x 5x     5x 4x     1x     1x 1x 1x 1x     5x       1x 1x     1x 1x 1x 1x 1x     5x         5x 1x 1x 1x 1x 3x 3x 3x     1x     5x 21x 21x       5x 3x 3x           3x 3x 3x 3x                   5x 1x 1x 4x       5x 3x 3x     5x 9x 9x     11x   5x     5x         1x           5x     5x     5x   5x     5x         1x                    
import macro from 'vtk.js/Sources/macros';
import vtkAbstractImageMapper from 'vtk.js/Sources/Rendering/Core/AbstractImageMapper';
import vtkImageMapper from 'vtk.js/Sources/Rendering/Core/ImageMapper';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import * as pickingHelper from 'vtk.js/Sources/Rendering/Core/AbstractImageMapper/helper';
import CoincidentTopologyHelper from 'vtk.js/Sources/Rendering/Core/Mapper/CoincidentTopologyHelper';
 
const { staticOffsetAPI, otherStaticMethods } = CoincidentTopologyHelper;
const { vtkErrorMacro, vtkWarningMacro } = macro;
const { SlicingMode } = vtkImageMapper;
 
// ----------------------------------------------------------------------------
// vtkImageArrayMapper methods
// ----------------------------------------------------------------------------
 
function vtkImageArrayMapper(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageArrayMapper');
 
  //------------------
  // Private functions
 
  const _computeSliceToSubSliceMap = () => {
    const inputCollection = publicAPI.getInputData();
    Iif (!inputCollection || inputCollection.empty()) {
      // clear the map
      if (model.sliceToSubSliceMap.length !== 0) {
        model.sliceToSubSliceMap.length = 0;
        publicAPI.modified();
      }
      return;
    }
 
    if (
      model.sliceToSubSliceMap.length === 0 ||
      inputCollection.getMTime() > publicAPI.getMTime()
    ) {
      const perImageMap = inputCollection.map((image, index) => {
        const dim = image.getDimensions();
        const out = new Array(dim[model.slicingMode]);
        for (let i = 0; i < out.length; ++i) {
          out[i] = { imageIndex: index, subSlice: i };
        }
        return out;
      });
 
      model.sliceToSubSliceMap = perImageMap.flat();
      publicAPI.modified();
    }
  };
 
  const _superSetInputData = publicAPI.setInputData;
  const _superSetInputConnection = publicAPI.setInputConnection;
 
  //------------------
  // Public functions
 
  publicAPI.setInputData = (inputData) => {
    _superSetInputData(inputData);
    _computeSliceToSubSliceMap();
  };
 
  publicAPI.setInputConnection = (outputPort, port = 0) => {
    _superSetInputConnection(outputPort, port);
    _computeSliceToSubSliceMap();
  };
 
  publicAPI.getImage = (slice = publicAPI.getSlice()) => {
    const inputCollection = publicAPI.getInputData();
    if (!inputCollection) {
      vtkWarningMacro('No input set.');
    } else Iif (slice < 0 || slice >= publicAPI.getTotalSlices()) {
      vtkWarningMacro('Invalid slice number.');
    } else {
      _computeSliceToSubSliceMap();
      return inputCollection.getItem(
        model.sliceToSubSliceMap[slice].imageIndex
      );
    }
    return null;
  };
 
  publicAPI.getBounds = () => {
    const image = publicAPI.getCurrentImage();
    Iif (!image) {
      return vtkMath.createUninitializedBounds();
    }
    if (!model.useCustomExtents) {
      return image.getBounds();
    }
 
    const ex = model.customDisplayExtent.slice();
    // use sub-slice of the current image,
    // which is the k-coordinate.
    const nSlice = publicAPI.getSubSlice();
    ex[4] = nSlice;
    ex[5] = nSlice;
    return image.extentToBounds(ex);
  };
 
  publicAPI.getBoundsForSlice = (
    slice = publicAPI.getSlice(),
    halfThickness = 0
  ) => {
    const image = publicAPI.getImage(slice);
    Iif (!image) {
      return vtkMath.createUninitializedBounds();
    }
    const extent = image.getSpatialExtent();
    const nSlice = publicAPI.getSubSlice(slice);
    extent[4] = nSlice - halfThickness;
    extent[5] = nSlice + halfThickness;
    return image.extentToBounds(extent);
  };
 
  publicAPI.getClosestIJKAxis = () => ({
    ijkMode: model.slicingMode,
    flip: false,
  });
 
  publicAPI.computeTotalSlices = () => {
    const inputCollection = publicAPI.getInputData();
    const collectionLength = inputCollection.getNumberOfItems();
    let slicesCount = 0;
    for (let i = 0; i < collectionLength; ++i) {
      const image = inputCollection.getItem(i);
      if (image) {
        slicesCount += image.getDimensions()[model.slicingMode];
      }
    }
    return slicesCount;
  };
 
  publicAPI.getTotalSlices = () => {
    _computeSliceToSubSliceMap();
    return model.sliceToSubSliceMap.length;
  };
 
  // set slice number in terms of imageIndex and subSlice number.
  publicAPI.setSlice = (slice) => {
    const inputCollection = publicAPI.getInputData();
    Iif (!inputCollection) {
      // No input is set
      vtkWarningMacro('No input set.');
      return;
    }
 
    const totalSlices = publicAPI.getTotalSlices();
    if (slice >= 0 && slice < totalSlices) {
      model.slice = slice;
      publicAPI.modified();
    } else E{
      vtkErrorMacro(
        `Slice number out of range. Acceptable range is: [0, ${
          totalSlices > 0 ? totalSlices - 1 : 0
        }]slice <= totalSlices`
      );
    }
  };
 
  publicAPI.computeSlice = (imageIndex, subSlice) => {
    _computeSliceToSubSliceMap();
    return model.sliceToSubSliceMap.findIndex(
      (x) => x.imageIndex === imageIndex && x.subSlice === subSlice
    );
  };
 
  publicAPI.getImageIndex = (slice = publicAPI.getSlice()) => {
    _computeSliceToSubSliceMap();
    return model.sliceToSubSliceMap[slice]?.imageIndex;
  };
 
  publicAPI.getSubSlice = (slice = publicAPI.getSlice()) => {
    _computeSliceToSubSliceMap();
    return model.sliceToSubSliceMap[slice]?.subSlice;
  };
 
  publicAPI.getCurrentImage = () => publicAPI.getImage(publicAPI.getSlice());
 
  publicAPI.intersectWithLineForPointPicking = (p1, p2) =>
    pickingHelper.intersectWithLineForPointPicking(p1, p2, publicAPI);
 
  publicAPI.intersectWithLineForCellPicking = (p1, p2) =>
    pickingHelper.intersectWithLineForCellPicking(p1, p2, publicAPI);
}
 
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {
  slicingMode: SlicingMode.K,
  sliceToSubSliceMap: [],
};
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  vtkAbstractImageMapper.extend(publicAPI, model, initialValues);
 
  // Build VTK API
  macro.get(publicAPI, model, ['slicingMode']);
 
  CoincidentTopologyHelper.implementCoincidentTopologyMethods(publicAPI, model);
 
  // Object methods
  vtkImageArrayMapper(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageArrayMapper');
 
// ----------------------------------------------------------------------------
 
export default {
  newInstance,
  extend,
  ...staticOffsetAPI,
  ...otherStaticMethods,
};