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

41.81% Statements 23/55
0% Branches 0/23
40% Functions 2/5
41.81% Lines 23/55

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                              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 vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkCutter from 'vtk.js/Sources/Filters/Core/Cutter';
import vtkImageMapper from 'vtk.js/Sources/Rendering/Core/ImageMapper';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import vtkPlane from 'vtk.js/Sources/Common/DataModel/Plane';
 
import vtkAbstractRepresentationProxy from 'vtk.js/Sources/Proxy/Core/AbstractRepresentationProxy';
 
// ----------------------------------------------------------------------------
// vtkSlicedGeometryRepresentationProxy methods
// ----------------------------------------------------------------------------
 
function vtkSlicedGeometryRepresentationProxy(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkSlicedGeometryRepresentationProxy');
 
  // Internals
  model.plane = vtkPlane.newInstance();
  model.cutter = vtkCutter.newInstance();
  model.cutter.setCutFunction(model.plane);
  model.mapper = vtkMapper.newInstance();
  model.actor = vtkActor.newInstance();
  model.property = model.actor.getProperty();
  model.property.setLighting(false);
 
  // connect rendering pipeline
  model.mapper.setInputConnection(model.cutter.getOutputPort());
  model.actor.setMapper(model.mapper);
  model.actors.push(model.actor);
 
  // Keep things updated
  model.sourceDependencies.push(model.cutter);
 
  // API ----------------------------------------------------------------------
  publicAPI.setSlice = (slice = 0) => {
    const stateModified = model.slice !== slice;
    model.slice = slice;
    const n = model.plane.getNormal();
    const planeModified = model.plane.setOrigin(
      n[0] * slice,
      n[1] * slice,
      n[2] * slice
    );
    if (planeModified || stateModified) {
      publicAPI.modified();
      return true;
    }
    return false;
  };
 
  publicAPI.setOffset = (offset = 0) => {
    const stateModified = model.offset !== offset;
    model.offset = offset;
    const normal = model.plane.getNormal();
    const actorModified = model.actor.setPosition(
      offset * normal[0],
      offset * normal[1],
      offset * normal[2]
    );
    if (actorModified || stateModified) {
      publicAPI.modified();
      return true;
    }
    return false;
  };
 
  publicAPI.setSlicingMode = (mode) => {
    if (model.slicingMode === mode || !mode) {
      console.log('skip setSlicingMode', mode);
      return;
    }
    model.slicingMode = mode;
    switch (vtkImageMapper.SlicingMode[mode]) {
      case vtkImageMapper.SlicingMode.X:
        model.plane.setNormal(1, 0, 0);
        break;
      case vtkImageMapper.SlicingMode.Y:
        model.plane.setNormal(0, 1, 0);
        break;
      case vtkImageMapper.SlicingMode.Z:
        model.plane.setNormal(0, 0, 1);
        break;
      default:
        return;
    }
    // Reslice properly along that new axis
    let alreadyModified = publicAPI.setSlice(model.slice);
    alreadyModified = publicAPI.setOffset(model.offset) || alreadyModified;
 
    // Update pipeline
    if (!alreadyModified) {
      publicAPI.modified();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  slicingMode: vtkImageMapper.SlicingMode.NONE,
  slice: 0,
  offset: 0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  vtkAbstractRepresentationProxy.extend(publicAPI, model, initialValues);
  macro.get(publicAPI, model, ['slicingMode', 'slice', 'offset']);
 
  // Object specific methods
  vtkSlicedGeometryRepresentationProxy(publicAPI, model);
 
  // Map proxy properties
  macro.proxyPropertyState(publicAPI, model);
  macro.proxyPropertyMapping(publicAPI, model, {
    opacity: { modelKey: 'property', property: 'opacity' },
    visibility: { modelKey: 'actor', property: 'visibility' },
    color: { modelKey: 'property', property: 'diffuseColor' },
    useShadow: { modelKey: 'property', property: 'lighting' },
    useBounds: { modelKey: 'actor', property: 'useBounds' },
  });
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkSlicedGeometryRepresentationProxy'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };