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

80% Statements 28/35
42.85% Branches 3/7
100% Functions 4/4
79.41% Lines 27/34

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                    21x   21x 21x     21x             182x       182x 182x   182x 182x 182x 182x 182x 182x 182x   182x 438x 438x 438x   438x                 438x     182x               1x               21x     21x   21x     21x         1x                
import { mat4, vec3 } from 'gl-matrix';
 
import macro from 'vtk.js/Sources/macros';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
 
// ----------------------------------------------------------------------------
// vtkPixelSpaceCallbackMapper methods
// ----------------------------------------------------------------------------
 
function vtkPixelSpaceCallbackMapper(publicAPI, model) {
  model.classHierarchy.push('vtkPixelSpaceCallbackMapper');
 
  if (!model.callback) {
    model.callback = () => {};
  }
 
  publicAPI.invokeCallback = (
    dataset,
    camera,
    aspect,
    windowSize,
    depthValues
  ) => {
    Iif (!model.callback) {
      return;
    }
 
    const matrix = camera.getCompositeProjectionMatrix(aspect, -1, 1);
    mat4.transpose(matrix, matrix);
 
    const dataPoints = dataset.getPoints();
    const result = new Float64Array(3);
    const width = windowSize.usize;
    const height = windowSize.vsize;
    const hw = width / 2;
    const hh = height / 2;
    const coords = [];
 
    for (let pidx = 0; pidx < dataPoints.getNumberOfPoints(); pidx += 1) {
      const point = dataPoints.getPoint(pidx);
      vec3.transformMat4(result, point, matrix);
      const coord = [result[0] * hw + hw, result[1] * hh + hh, result[2], 0];
 
      Iif (depthValues) {
        const linIdx = Math.floor(coord[1]) * width + Math.floor(coord[0]);
        const idx = linIdx * 4;
        const r = depthValues[idx] / 255;
        const g = depthValues[idx + 1] / 255;
        const z = (r * 256 + g) / 257;
        coord[3] = z * 2 - 1;
      }
 
      coords.push(coord);
    }
 
    model.callback(coords, camera, aspect, depthValues, [width, height]);
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  callback: null,
  useZValues: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkMapper.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, ['callback', 'useZValues']);
 
  // Object methods
  vtkPixelSpaceCallbackMapper(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkPixelSpaceCallbackMapper'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };