All files / Sources/Widgets/Representations/CroppingOutlineRepresentation index.js

14.28% Statements 4/28
0% Branches 0/5
0% Functions 0/4
14.28% Lines 4/28

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                1x     1x                                                                                                                                                 1x                                     1x                
import macro from 'vtk.js/Sources/macros';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkContextRepresentation from 'vtk.js/Sources/Widgets/Representations/ContextRepresentation';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import { origin } from 'vtk.js/Sources/Widgets/Representations/GlyphRepresentation';
import { allocateArray } from 'vtk.js/Sources/Widgets/Representations/WidgetRepresentation';
 
const { vtkErrorMacro } = macro;
 
// prettier-ignore
const OUTLINE_ARRAY = [
  2, 0, 1,
  2, 0, 2,
  2, 0, 4,
  2, 1, 3,
  2, 1, 5,
  2, 2, 3,
  2, 2, 6,
  2, 3, 7,
  2, 4, 5,
  2, 4, 6,
  2, 5, 7,
  2, 6, 7,
];
 
// ----------------------------------------------------------------------------
// vtkCroppingOutlineRepresentation methods
// ----------------------------------------------------------------------------
 
// Represents a box outline given 8 points as corners.
// Does not work with an arbitrary set of points. An oriented bounding box
// algorithm may be implemented in the future.
function vtkCroppingOutlineRepresentation(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkCroppingOutlineRepresentation');
 
  // --------------------------------------------------------------------------
  // Internal polydata dataset
  // --------------------------------------------------------------------------
 
  model.internalPolyData = vtkPolyData.newInstance({ mtime: 0 });
  allocateArray(model.internalPolyData, 'lines', OUTLINE_ARRAY.length)
    .getData()
    .set(OUTLINE_ARRAY);
 
  const applyOrigin = origin(publicAPI, model);
 
  // --------------------------------------------------------------------------
  // Generic rendering pipeline
  // --------------------------------------------------------------------------
 
  model.mapper = vtkMapper.newInstance({
    scalarVisibility: false,
  });
  model.actor = vtkActor.newInstance({ parentProp: publicAPI });
  model.actor.getProperty().setEdgeColor(...model.edgeColor);
  model.mapper.setInputConnection(publicAPI.getOutputPort());
  model.actor.setMapper(model.mapper);
 
  publicAPI.addActor(model.actor);
 
  // --------------------------------------------------------------------------
 
  publicAPI.requestData = (inData, outData) => {
    const list = publicAPI
      .getRepresentationStates(inData[0])
      .filter((state) => state.getOrigin && state.getOrigin());
    if (list.length === 8) {
      applyOrigin(model.internalPolyData, list);
      model.internalPolyData.getPoints().modified();
      model.internalPolyData.modified();
 
      outData[0] = model.internalPolyData;
    } else {
      vtkErrorMacro('CroppingOutlineRepresentation did not get 8 states');
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  edgeColor: [1, 1, 1],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkContextRepresentation.extend(publicAPI, model, initialValues);
  macro.setGetArray(publicAPI, model, ['edgeColor'], 3);
  macro.get(publicAPI, model, ['mapper', 'actor']);
 
  // Object specific methods
  vtkCroppingOutlineRepresentation(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkCroppingOutlineRepresentation'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };