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

41.37% Statements 24/58
0% Branches 0/13
28.57% Functions 2/7
43.63% Lines 24/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 142 143 144 145 146 147 148 149 150 151 152                                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 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 { Behavior } from 'vtk.js/Sources/Widgets/Representations/WidgetRepresentation/Constants';
import { RenderingTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';
 
// ----------------------------------------------------------------------------
// vtkPlaneHandleRepresentation methods
// ----------------------------------------------------------------------------
 
function vtkConvexFaceContextRepresentation(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkConvexFaceContextRepresentation');
 
  // --------------------------------------------------------------------------
  // Internal polydata dataset
  // --------------------------------------------------------------------------
 
  model.internalPolyData = vtkPolyData.newInstance({ mtime: 0 });
  model.points = new Float32Array(3 * 4);
  model.cells = new Uint8Array([4, 0, 1, 2, 3]);
  model.internalPolyData.getPoints().setData(model.points, 3);
  model.internalPolyData.getPolys().setData(model.cells);
 
  function allocateSize(polyData, size) {
    const points = allocateArray(polyData, 'points', size).getData();
    const oldCellsSize = polyData.getPolys().getNumberOfValues();
    const cells = allocateArray(polyData, 'polys', size + 1).getData();
    if (oldCellsSize !== cells.length) {
      cells[0] = size;
      for (let i = 0; i < size; i++) {
        cells[i + 1] = i;
      }
    }
    return points;
  }
 
  // --------------------------------------------------------------------------
  // Generic rendering pipeline
  // --------------------------------------------------------------------------
 
  model.mapper = vtkMapper.newInstance({
    scalarVisibility: false,
  });
  model.actor = vtkActor.newInstance({ parentProp: publicAPI });
  model.actor.getProperty().setOpacity(model.opacity);
 
  model.mapper.setInputConnection(publicAPI.getOutputPort());
  model.actor.setMapper(model.mapper);
 
  publicAPI.addActor(model.actor);
 
  // --------------------------------------------------------------------------
 
  publicAPI.requestData = (inData, outData) => {
    const list = publicAPI.getRepresentationStates(inData[0]);
    const validState = list.filter((state) => state.getOrigin());
 
    const points = allocateSize(model.internalPolyData, validState.length);
 
    for (let i = 0; i < validState.length; i++) {
      const coords = validState[i].getOrigin();
      points[i * 3] = coords[0];
      points[i * 3 + 1] = coords[1];
      points[i * 3 + 2] = coords[2];
    }
 
    model.internalPolyData.modified();
    outData[0] = model.internalPolyData;
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.getSelectedState = (prop, compositeID) => {
    const state = model.inputData[0];
    const list = publicAPI.getRepresentationStates(state);
 
    // Update state orientation based on face
    if (state.updateFromOriginRightUp) {
      state.updateFromOriginRightUp(
        list[0].getOrigin(),
        list[list.length - 1].getOrigin(),
        list[1].getOrigin()
      );
    }
 
    return state;
  };
 
  // --------------------------------------------------------------------------
 
  const superUpdateActorVisibility = publicAPI.updateActorVisibility;
  publicAPI.updateActorVisibility = (
    renderingType = RenderingTypes.FRONT_BUFFER,
    ctxVisible = true,
    handleVisible = true
  ) => {
    switch (model.behavior) {
      case Behavior.HANDLE:
        if (renderingType === RenderingTypes.PICKING_BUFFER) {
          model.actor.getProperty().setOpacity(1);
        } else {
          model.actor.getProperty().setOpacity(model.opacity);
        }
        break;
      case Behavior.CONTEXT:
      default:
        model.actor.getProperty().setOpacity(model.opacity);
        break;
    }
    superUpdateActorVisibility(renderingType, ctxVisible, handleVisible);
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  defaultColor: [1, 0, 0.5],
  opacity: 0.2,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkContextRepresentation.extend(publicAPI, model, initialValues);
  macro.setGetArray(publicAPI, model, ['defaultColor'], 3);
  macro.get(publicAPI, model, ['mapper', 'actor']);
  macro.setGet(publicAPI, model, ['opacity']);
 
  // Object specific methods
  vtkConvexFaceContextRepresentation(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkConvexFaceContextRepresentation'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };