All files / Sources/Proxy/Representations/SliceRepresentationProxy index.js

21.13% Statements 26/123
10.86% Branches 5/46
27.27% Functions 3/11
21.31% Lines 26/122

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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310                                                                                                                                                                                                            1x   1x 1x 1x     1x 1x                                                                             1x 1x       1x     1x 1x       1x                   1x         1x                         1x             1x                                                                                                               1x 1x                           1x             1x         1x     1x 1x     1x     1x                     1x                
import macro from 'vtk.js/Sources/macros';
import vtkImageSlice from 'vtk.js/Sources/Rendering/Core/ImageSlice';
import vtkImageMapper from 'vtk.js/Sources/Rendering/Core/ImageMapper';
 
import vtkAbstractRepresentationProxy from 'vtk.js/Sources/Proxy/Core/AbstractRepresentationProxy';
 
// ----------------------------------------------------------------------------
 
function sum(a, b) {
  return a + b;
}
 
// ----------------------------------------------------------------------------
 
function mean(...array) {
  return array.reduce(sum, 0) / array.length;
}
 
// ----------------------------------------------------------------------------
 
function updateDomains(dataset, dataArray, model, updateProp) {
  const dataRange = dataArray.getRange();
  const spacing = dataset.getSpacing();
  const bounds = dataset.getBounds();
  const extent = dataset.getExtent();
 
  let sliceMin;
  let sliceMax;
  let stepVal;
  let axisIndex;
  const sliceMode = model.mapper.getSlicingMode();
  const sliceModeLabel = 'IJKXYZ'[sliceMode];
  switch (sliceMode) {
    case vtkImageMapper.SlicingMode.I:
    case vtkImageMapper.SlicingMode.J:
    case vtkImageMapper.SlicingMode.K:
      axisIndex = 'IJK'.indexOf(sliceModeLabel);
      sliceMin = extent[axisIndex * 2];
      sliceMax = extent[axisIndex * 2 + 1];
      stepVal = 1;
      break;
    case vtkImageMapper.SlicingMode.X:
    case vtkImageMapper.SlicingMode.Y:
    case vtkImageMapper.SlicingMode.Z:
      {
        axisIndex = 'XYZ'.indexOf(sliceModeLabel);
        sliceMin = bounds[axisIndex * 2];
        sliceMax = bounds[axisIndex * 2 + 1];
        const { ijkMode } = model.mapper.getClosestIJKAxis();
        stepVal = spacing[ijkMode];
      }
      break;
    default:
      break;
  }
 
  const propToUpdate = {
    slice: {
      domain: {
        min: sliceMin,
        max: sliceMax,
        step: stepVal,
      },
    },
    windowWidth: {
      domain: {
        min: 0,
        max: dataRange[1] - dataRange[0],
        step: 'any',
      },
    },
    windowLevel: {
      domain: {
        min: dataRange[0],
        max: dataRange[1],
        step: 'any',
      },
    },
  };
 
  updateProp('slice', propToUpdate.slice);
  updateProp('windowWidth', propToUpdate.windowWidth);
  updateProp('windowLevel', propToUpdate.windowLevel);
 
  return {
    slice: mean(propToUpdate.slice.domain.min, propToUpdate.slice.domain.max),
    windowWidth: propToUpdate.windowWidth.domain.max,
    windowLevel: Math.floor(
      mean(
        propToUpdate.windowLevel.domain.min,
        propToUpdate.windowLevel.domain.max
      )
    ),
  };
}
 
// ----------------------------------------------------------------------------
// vtkSliceRepresentationProxy methods
// ----------------------------------------------------------------------------
 
function vtkSliceRepresentationProxy(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkSliceRepresentationProxy');
 
  model.mapper = vtkImageMapper.newInstance();
  model.actor = vtkImageSlice.newInstance();
  model.property = model.actor.getProperty();
 
  // connect rendering pipeline
  model.actor.setMapper(model.mapper);
  model.actors.push(model.actor);
 
  function setInputData(inputDataset) {
    const state = updateDomains(
      inputDataset,
      publicAPI.getDataArray(),
      model,
      publicAPI.updateProxyProperty
    );
    publicAPI.set(state);
 
    // Init slice location
    const bounds = inputDataset.getBounds();
    const extent = inputDataset.getExtent();
    switch (model.mapper.getSlicingMode()) {
      case vtkImageMapper.SlicingMode.I:
        publicAPI.setSlice(Math.floor(mean(extent[0], extent[1])));
        break;
      case vtkImageMapper.SlicingMode.J:
        publicAPI.setSlice(Math.floor(mean(extent[2], extent[3])));
        break;
      case vtkImageMapper.SlicingMode.K:
        publicAPI.setSlice(Math.floor(mean(extent[4], extent[5])));
        break;
      case vtkImageMapper.SlicingMode.X:
        publicAPI.setSlice(mean(bounds[0], bounds[1]));
        break;
      case vtkImageMapper.SlicingMode.Y:
        publicAPI.setSlice(mean(bounds[2], bounds[3]));
        break;
      case vtkImageMapper.SlicingMode.Z:
        publicAPI.setSlice(mean(bounds[4], bounds[5]));
        break;
      default:
        break;
    }
  }
 
  // Keep things updated
  model.sourceDependencies.push(model.mapper);
  model.sourceDependencies.push({ setInputData });
 
  // keeps the slicing mode and domain info updated
  function updateSlicingMode(mode) {
    model.mapper.setSlicingMode(vtkImageMapper.SlicingMode[mode]);
 
    // Update to previously set position
    const modelKey = `${mode.toLowerCase()}Slice`;
    Iif (modelKey in model && model[modelKey] !== undefined) {
      model.mapper.setSlice(model[modelKey]);
    }
 
    Iif (model.input) {
      // Update domains for UI...
      const state = updateDomains(
        publicAPI.getInputDataSet(),
        publicAPI.getDataArray(),
        model,
        publicAPI.updateProxyProperty
      );
      publicAPI.set(state);
    }
    publicAPI.modified();
  }
 
  // API ----------------------------------------------------------------------
 
  publicAPI.setSlicingMode = (mode) => {
    if (!mode) {
      return false;
    }
    if (model.slicingMode !== mode) {
      // Update Mode
      model.slicingMode = mode;
      updateSlicingMode(mode);
      return true;
    }
    return false;
  };
 
  publicAPI.getSliceIndex = () => {
    if ('XYZ'.indexOf(model.slicingMode) !== -1) {
      return model.mapper.getSliceAtPosition(model.mapper.getSlice());
    }
    return model.mapper.getSlice();
  };
 
  publicAPI.getAnnotations = () => {
    const dynamicAddOn = {};
    const sliceIndex = publicAPI.getSliceIndex();
    const sliceBounds = model.mapper.getBoundsForSlice();
    const sliceNormal = model.mapper.getSlicingModeNormal();
    const { ijkMode } = model.mapper.getClosestIJKAxis();
    const sliceOrigin = [
      (sliceBounds[0] + sliceBounds[1]) * 0.5,
      (sliceBounds[2] + sliceBounds[3]) * 0.5,
      (sliceBounds[4] + sliceBounds[5]) * 0.5,
    ];
    let slicePosition = 0;
    if (sliceBounds[1] - sliceBounds[0] < Number.EPSILON) {
      slicePosition = sliceBounds[0];
    }
    if (sliceBounds[3] - sliceBounds[2] < Number.EPSILON) {
      slicePosition = sliceBounds[2];
    }
    if (sliceBounds[5] - sliceBounds[4] < Number.EPSILON) {
      slicePosition = sliceBounds[4];
    }
 
    const imageData = model.mapper.getInputData();
    if (imageData) {
      dynamicAddOn.sliceSpacing = imageData.getSpacing()[ijkMode];
      dynamicAddOn.dimensions = imageData.getDimensions();
      dynamicAddOn.sliceCount = imageData.getDimensions()[ijkMode];
      const ijkOrientation = [];
      for (let i = 0; i < 3; i++) {
        const extent = [0, 0, 0, 0, 0, 0];
        extent[i * 2 + 1] = 1;
        const tmpBounds = imageData.extentToBounds(extent);
        if (tmpBounds[1] - tmpBounds[0] > Number.EPSILON) {
          ijkOrientation[0] = 'IJK'[i];
        }
        if (tmpBounds[3] - tmpBounds[2] > Number.EPSILON) {
          ijkOrientation[1] = 'IJK'[i];
        }
        if (tmpBounds[5] - tmpBounds[4] > Number.EPSILON) {
          ijkOrientation[2] = 'IJK'[i];
        }
      }
      dynamicAddOn.ijkOrientation = ijkOrientation.join('');
    }
 
    return {
      ijkMode,
      sliceBounds,
      sliceIndex,
      sliceNormal,
      sliceOrigin,
      slicePosition,
      ...dynamicAddOn,
    };
  };
 
  const parentSetColorBy = publicAPI.setColorBy;
  publicAPI.setColorBy = (arrayName, arrayLocation, componentIndex = -1) => {
    if (arrayName === null) {
      model.property.setRGBTransferFunction(null);
      model.property.setPiecewiseFunction(null);
    } else {
      parentSetColorBy(arrayName, arrayLocation, componentIndex);
      const lutProxy = publicAPI.getLookupTableProxy(arrayName);
      const pwfProxy = publicAPI.getPiecewiseFunctionProxy(arrayName);
      model.property.setRGBTransferFunction(lutProxy.getLookupTable());
      model.property.setPiecewiseFunction(pwfProxy.getPiecewiseFunction());
    }
  };
 
  // Initialize slicing mode
  updateSlicingMode(model.slicingMode || 'X');
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  vtkAbstractRepresentationProxy.extend(publicAPI, model, initialValues);
  macro.get(publicAPI, model, ['slicingMode']);
 
  // Object specific methods
  vtkSliceRepresentationProxy(publicAPI, model);
 
  // Proxyfy
  macro.proxyPropertyMapping(publicAPI, model, {
    visibility: { modelKey: 'actor', property: 'visibility' },
    windowWidth: { modelKey: 'property', property: 'colorWindow' },
    windowLevel: { modelKey: 'property', property: 'colorLevel' },
    interpolationType: { modelKey: 'property', property: 'interpolationType' },
    slice: { modelKey: 'mapper', property: 'slice' },
  });
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkSliceRepresentationProxy'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };