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

3.5% Statements 2/57
0% Branches 0/11
0% Functions 0/5
3.57% Lines 2/56

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                                                                                                                                                                                                          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 { allocateArray } from 'vtk.js/Sources/Widgets/Representations/WidgetRepresentation';
 
import { vec3 } from 'gl-matrix';
 
// ----------------------------------------------------------------------------
// vtkRectangleContextRepresentation methods
// ----------------------------------------------------------------------------
 
function vtkRectangleContextRepresentation(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkRectangleContextRepresentation');
 
  // --------------------------------------------------------------------------
  // Generic rendering pipeline
  // --------------------------------------------------------------------------
  model.internalPolyData = vtkPolyData.newInstance({ mtime: 0 });
 
  model.mapper = vtkMapper.newInstance();
  model.actor = vtkActor.newInstance({ parentProp: publicAPI });
 
  model.mapper.setInputConnection(publicAPI.getOutputPort());
  model.actor.setMapper(model.mapper);
  model.actor.getProperty().setOpacity(0.2);
  model.actor.getProperty().setColor(0, 1, 0);
 
  publicAPI.addActor(model.actor);
 
  // --------------------------------------------------------------------------
 
  publicAPI.setOpacity = (opacity) => {
    model.actor.getProperty().setOpacity(opacity);
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.requestData = (inData, outData) => {
    if (model.deleted) {
      return;
    }
 
    const list = publicAPI.getRepresentationStates(inData[0]);
    // FIXME: support list > 1.
    const state = list[0];
 
    if (state?.getVisible() && state?.getOrigin()) {
      const point1 = state.getOrigin();
      const point2 = state.getCorner();
      const diagonal = [0, 0, 0];
      vec3.subtract(diagonal, point2, point1);
      const up = state.getUp();
      const upComponent = vec3.dot(diagonal, up);
 
      const points = allocateArray(
        model.internalPolyData,
        'points',
        4
      ).getData();
 
      points[0] = point1[0];
      points[1] = point1[1];
      points[2] = point1[2];
      points[3] = point1[0] + upComponent * up[0];
      points[4] = point1[1] + upComponent * up[1];
      points[5] = point1[2] + upComponent * up[2];
      points[6] = point2[0];
      points[7] = point2[1];
      points[8] = point2[2];
      points[9] = point2[0] - upComponent * up[0];
      points[10] = point2[1] - upComponent * up[1];
      points[11] = point2[2] - upComponent * up[2];
 
      if (model.drawFace) {
        const polys = new Uint32Array([4, 0, 1, 2, 3]);
        model.internalPolyData.getPolys().setData(polys, 1);
      }
      if (model.drawBorder) {
        const line = new Uint32Array([5, 0, 1, 2, 3, 0]);
        model.internalPolyData.getLines().setData(line, 1);
      }
    } else {
      model.internalPolyData.getPoints().setData([], 0);
      model.internalPolyData.getPolys().setData([], 0);
      model.internalPolyData.getLines().setData([], 0);
    }
 
    model.internalPolyData.modified();
    outData[0] = model.internalPolyData;
  };
 
  publicAPI.getSelectedState = (prop, compositeID) => model.state;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  drawBorder: false,
  drawFace: true,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkContextRepresentation.extend(publicAPI, model, initialValues);
  macro.setGetArray(publicAPI, model, ['color'], 1);
 
  macro.setGet(publicAPI, model, ['drawBorder', 'drawFace']);
  macro.get(publicAPI, model, ['mapper', 'actor']);
 
  // Object specific methods
  vtkRectangleContextRepresentation(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkRectangleContextRepresentation'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };