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

85.52% Statements 65/76
55% Branches 11/20
88.88% Functions 8/9
86.11% Lines 62/72

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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                    6x           6x   6x                             6x 6x 6x 6x   6x 6x 6x 6x 6x     6x 6x 4x 20x     6x 4x       4x 4x   4x 16x 4x         4x   4x     4x     4x                 4x   4x         4x 4x 4x   4x 16x 512x 512x   512x 512x 512x 512x       4x 4x                   4x 4x     4x       4x 4x   4x 4x                   6x     4x     6x   6x 4x               1x                       6x   6x 6x 6x           6x     6x         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 vtkSpline3D from 'vtk.js/Sources/Common/DataModel/Spline3D';
import vtkTriangleFilter from 'vtk.js/Sources/Filters/General/TriangleFilter';
import vtkLineFilter from 'vtk.js/Sources/Filters/General/LineFilter';
import vtkWidgetRepresentation, {
  allocateArray,
} from 'vtk.js/Sources/Widgets/Representations/WidgetRepresentation';
 
// ----------------------------------------------------------------------------
// vtkSplineContextRepresentation methods
// ----------------------------------------------------------------------------
 
function vtkSplineContextRepresentation(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkSplineContextRepresentation');
 
  // --------------------------------------------------------------------------
  // Generic rendering pipeline
  // --------------------------------------------------------------------------
 
  model.internalPolyData = vtkPolyData.newInstance({ mtime: 0 });
 
  model._pipelines = {
    area: {
      source: publicAPI,
      filter: vtkTriangleFilter.newInstance(),
      mapper: vtkMapper.newInstance(),
      actor: vtkActor.newInstance({ parentProp: publicAPI }),
    },
    border: {
      source: publicAPI,
      filter: vtkLineFilter.newInstance(),
      mapper: vtkMapper.newInstance(),
      actor: vtkActor.newInstance({ parentProp: publicAPI }),
    },
  };
 
  vtkWidgetRepresentation.connectPipeline(model._pipelines.area);
  model._pipelines.area.actor.getProperty().setOpacity(0.2);
  model._pipelines.area.actor.getProperty().setColor(0, 1, 0);
  publicAPI.addActor(model._pipelines.area.actor);
 
  vtkWidgetRepresentation.connectPipeline(model._pipelines.border);
  model._pipelines.border.actor.getProperty().setOpacity(1);
  model._pipelines.border.actor.getProperty().setColor(0.1, 1, 0.1);
  model._pipelines.border.actor.setVisibility(model.outputBorder);
  publicAPI.addActor(model._pipelines.border.actor);
 
  // --------------------------------------------------------------------------
  const superGetRepresentationStates = publicAPI.getRepresentationStates;
  publicAPI.getRepresentationStates = (input = model.inputData[0]) =>
    superGetRepresentationStates(input).filter(
      (state) => state.getOrigin?.() && state.isVisible?.()
    );
 
  publicAPI.requestData = (inData, outData) => {
    Iif (model.deleted) {
      return;
    }
 
    const widgetState = inData[0];
    const closed = widgetState.getSplineClosed();
 
    const list = publicAPI.getRepresentationStates(widgetState);
    const inPoints = list.map((state) => state.getOrigin());
    Iif (inPoints.length <= 1) {
      outData[0] = model.internalPolyData;
      return;
    }
 
    let numVertices = inPoints.length;
 
    Iif (!closed) {
      --numVertices;
    } else {
      inPoints.push(inPoints[0]);
    }
 
    const spline = vtkSpline3D.newInstance({
      close: widgetState.getSplineClosed(),
      kind: widgetState.getSplineKind(),
      tension: widgetState.getSplineTension(),
      bias: widgetState.getSplineBias(),
      continuity: widgetState.getSplineContinuity(),
      boundaryCondition: widgetState.getSplineBoundaryCondition(),
      boundaryConditionValues: widgetState.getSplineBoundaryConditionValues(),
    });
    spline.computeCoefficients(inPoints);
 
    const outPoints = allocateArray(
      model.internalPolyData,
      'points',
      (numVertices + !closed) * model.resolution
    ).getData();
    const outCells = new Uint32Array(numVertices * model.resolution + 2);
    outCells[0] = numVertices * model.resolution + 1;
    outCells[numVertices * model.resolution + 1] = 0;
 
    for (let i = 0; i < numVertices; i++) {
      for (let j = 0; j < model.resolution; j++) {
        const t = j / model.resolution;
        const point = spline.getPoint(i, t);
 
        outPoints[3 * (i * model.resolution + j) + 0] = point[0];
        outPoints[3 * (i * model.resolution + j) + 1] = point[1];
        outPoints[3 * (i * model.resolution + j) + 2] = point[2];
        outCells[i * model.resolution + j + 1] = i * model.resolution + j;
      }
    }
 
    if (closed) {
      outCells[numVertices * model.resolution + 1] = 0;
    } else E{
      const lastPointIndex = numVertices * model.resolution + 1;
      const lastPoint = spline.getPoint(numVertices, 0);
      outPoints[3 * lastPointIndex + 0] = lastPoint[0];
      outPoints[3 * lastPointIndex + 1] = lastPoint[1];
      outPoints[3 * lastPointIndex + 2] = lastPoint[2];
      outCells[numVertices * model.resolution + 1] = lastPointIndex;
    }
 
    if (model.fill) {
      model.internalPolyData.getPolys().setData(outCells);
    }
 
    model.internalPolyData
      .getLines()
      .setData(model.outputBorder ? outCells : []);
 
    model.internalPolyData.modified();
    outData[0] = model.internalPolyData;
 
    model._pipelines.area.filter.update();
    model._pipelines.border.actor
      .getProperty()
      .setColor(
        ...(inPoints.length <= 3 ||
        model._pipelines.area.filter.getErrorCount() === 0
          ? model.borderColor
          : model.errorBorderColor)
      );
  };
 
  publicAPI.getSelectedState = (prop, compositeID) => model.state;
 
  function updateAreaVisibility() {
    model._pipelines.area.actor.setVisibility(model.fill);
  }
 
  publicAPI.setFill = macro.chain(publicAPI.setFill, updateAreaVisibility);
 
  publicAPI.setOutputBorder = macro.chain(publicAPI.setOutputBorder, (v) =>
    model._pipelines.border.actor.setVisibility(v)
  );
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  resolution: 16,
  fill: true,
  // boundaryCondition : BoundaryCondition.DEFAULT
  outputBorder: false,
  borderColor: [0.1, 1, 0.1],
  errorBorderColor: [1, 0, 0],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkContextRepresentation.extend(publicAPI, model, initialValues);
  macro.get(publicAPI, model, ['mapper']);
  macro.setGet(publicAPI, model, [
    'resolution',
    'boundaryCondition',
    'fill',
    'outputBorder',
  ]);
  macro.setGetArray(publicAPI, model, ['borderColor', 'errorBorderColor'], 3);
 
  // Object specific methods
  vtkSplineContextRepresentation(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkSplineContextRepresentation'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };