All files / Sources/Common/DataModel/Spline3D index.js

80.43% Statements 37/46
38.46% Branches 5/13
100% Functions 8/8
78.04% Lines 32/41

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            1x               4x         12x           12x       12x   12x 12x   12x 12x 12x 12x 60x           12x 12x                         4x 20x 20x 20x   4x 4x 4x         512x                   4x 4x         4x         4x                                   1x                             4x     4x 4x 4x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCardinalSpline1D from 'vtk.js/Sources/Common/DataModel/CardinalSpline1D';
import vtkKochanekSpline1D from 'vtk.js/Sources/Common/DataModel/KochanekSpline1D';
 
import { splineKind } from 'vtk.js/Sources/Common/DataModel/Spline3D/Constants';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkSpline3D methods
// ----------------------------------------------------------------------------
 
function vtkSpline3D(publicAPI, model) {
  // Set our classname
  model.classHierarchy.push('vtkSpline3D');
 
  // --------------------------------------------------------------------------
 
  function computeCoefficients1D(spline, points, boundaryConditionValue) {
    Iif (points.length === 0) {
      vtkErrorMacro('Splines require at least one points');
    }
 
    // If we have only one point we create a spline
    // which two extremities are the same point
    Iif (points.length === 1) {
      points.push(points[0]);
    }
 
    const size = points.length;
 
    let work = null;
    let intervals = null;
 
    work = new Float32Array(size);
    if (model.intervals.length === 0) {
      intervals = new Float32Array(size);
      for (let i = 0; i < intervals.length; i++) {
        intervals[i] = i;
      }
    } else E{
      intervals = model.intervals;
    }
 
    if (model.close) {
      spline.computeCloseCoefficients(size, work, intervals, points);
    } else E{
      spline.computeOpenCoefficients(size, work, intervals, points, {
        leftConstraint: model.boundaryCondition,
        leftValue: boundaryConditionValue,
        rightConstraint: model.boundaryCondition,
        rightValue: boundaryConditionValue,
      });
    }
  }
 
  // --------------------------------------------------------------------------
 
  publicAPI.computeCoefficients = (points) => {
    const x = points.map((pt) => pt[0]);
    const y = points.map((pt) => pt[1]);
    const z = points.map((pt) => pt[2]);
 
    computeCoefficients1D(model.splineX, x, model.boundaryConditionValues[0]);
    computeCoefficients1D(model.splineY, y, model.boundaryConditionValues[1]);
    computeCoefficients1D(model.splineZ, z, model.boundaryConditionValues[2]);
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.getPoint = (intervalIndex, t) => [
    model.splineX.getValue(intervalIndex, t),
    model.splineY.getValue(intervalIndex, t),
    model.splineZ.getValue(intervalIndex, t),
  ];
 
  // --------------------------------------------------------------------------
  // initialization
  // --------------------------------------------------------------------------
 
  if (model.kind === splineKind.KOCHANEK_SPLINE) {
    model.splineX = vtkKochanekSpline1D.newInstance({
      tension: model.tension,
      continuity: model.continuity,
      bias: model.bias,
    });
    model.splineY = vtkKochanekSpline1D.newInstance({
      tension: model.tension,
      continuity: model.continuity,
      bias: model.bias,
    });
    model.splineZ = vtkKochanekSpline1D.newInstance({
      tension: model.tension,
      continuity: model.continuity,
      bias: model.bias,
    });
  } else Eif (model.kind === splineKind.CARDINAL_SPLINE) {
    model.splineX = vtkCardinalSpline1D.newInstance();
    model.splineY = vtkCardinalSpline1D.newInstance();
    model.splineZ = vtkCardinalSpline1D.newInstance();
  } else {
    vtkErrorMacro(`Unknown spline type ${model.kind}`);
  }
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  close: false,
  intervals: [],
  kind: splineKind.KOCHANEK_SPLINE,
  boundaryConditionValues: [0, 0, 0],
 
  // Passed to the vtkKochanekSpline1D
  tension: 0,
  continuity: 0,
  bias: 0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, ['close', 'intervals']);
  vtkSpline3D(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkSpline3D');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };