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

21.51% Statements 17/79
0% Branches 0/31
25% Functions 2/8
22.66% Lines 17/75

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            4x   4x 4x   4x 4x                                                                                               4x                 4x                         4x                                                     4x                                                               1x               4x   4x   4x   4x 4x   4x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCell from 'vtk.js/Sources/Common/DataModel/Cell';
import vtkLine from 'vtk.js/Sources/Common/DataModel/Line';
import { vec3 } from 'gl-matrix';
 
function vtkPolyLine(publicAPI, model) {
  model.classHierarchy.push('vtkPolyLine');
 
  const line = vtkLine.newInstance();
  line.getPoints().setNumberOfPoints(2);
 
  publicAPI.getCellDimension = () => 1;
  publicAPI.intersectWithLine = (t1, t2, p1, p2, tol, x, pcoords) => {
    const outObj = {
      intersect: 0,
      t: Number.MAX_VALUE,
      subId: 0,
      betweenPoints: null,
    };
 
    const numLines = publicAPI.getNumberOfPoints() - 1;
    let pDistMin = Number.MAX_VALUE;
    const minXYZ = [0, 0, 0];
    const minPCoords = [0, 0, 0];
    for (let subId = 0; subId < numLines; subId++) {
      const pCoords = [0, 0, 0];
 
      line
        .getPoints()
        .getData()
        .set(model.points.getData().subarray(3 * subId, 3 * (subId + 2)));
 
      const lineIntersected = line.intersectWithLine(p1, p2, tol, x, pcoords);
 
      if (
        lineIntersected.intersect === 1 &&
        lineIntersected.t <= outObj.t + tol &&
        lineIntersected.t >= t1 &&
        lineIntersected.t <= t2
      ) {
        outObj.intersect = 1;
        const pDist = line.getParametricDistance(pCoords);
        if (
          pDist < pDistMin ||
          (pDist === pDistMin && lineIntersected.t < outObj.t)
        ) {
          outObj.subId = subId;
          outObj.t = lineIntersected.t;
          pDistMin = pDist;
          for (let k = 0; k < 3; k++) {
            minXYZ[k] = x[k];
            minPCoords[k] = pCoords[k];
          }
        }
      }
    }
 
    return outObj;
  };
 
  publicAPI.evaluateLocation = (subId, pcoords, x, weights) => {
    line
      .getPoints()
      .getData()
      .set(model.points.getData().subarray(3 * subId, 3 * (subId + 2)));
 
    return line.evaluateLocation(pcoords, x, weights);
  };
 
  publicAPI.evaluateOrientation = (subId, pcoords, q, weights) => {
    if (model.orientations) {
      line.setOrientations([
        model.orientations[subId],
        model.orientations[subId + 1],
      ]);
    } else {
      line.setOrientations(null);
    }
 
    return line.evaluateOrientation(pcoords, q, weights);
  };
 
  publicAPI.getDistancesToFirstPoint = () => {
    const dTime = model.distancesTime.getMTime();
    if (dTime < model.points.getMTime() || dTime < publicAPI.getMTime()) {
      const numPoints = publicAPI.getNumberOfPoints();
      if (!model.distances) {
        model.distances = new Array(numPoints);
      } else {
        model.distances.length = numPoints;
      }
      if (numPoints > 0) {
        const previousPoint = new Array(3);
        const currentPoint = new Array(3);
        let totalDistance = 0;
        model.distances[0] = totalDistance;
        model.points.getPoint(0, previousPoint);
        for (let i = 1; i < numPoints; ++i) {
          model.points.getPoint(i, currentPoint);
          totalDistance += model.distanceFunction(previousPoint, currentPoint);
          model.distances[i] = totalDistance;
          vec3.copy(previousPoint, currentPoint);
        }
      }
      model.distancesTime.modified();
    }
    return model.distances;
  };
 
  publicAPI.findPointIdAtDistanceFromFirstPoint = (distance) => {
    const distances = publicAPI.getDistancesToFirstPoint();
    // At least two points to return an ID
    if (distances.length < 2) {
      return -1;
    }
    // Binary search in the distance array
    let minId = 0;
    let maxId = distances.length - 1;
    if (
      distance < distances[minId] ||
      distance > distances[maxId] ||
      distances[maxId] === 0
    ) {
      return -1;
    }
    while (maxId - minId > 1) {
      const midId = Math.floor((minId + maxId) / 2);
      if (distances[midId] <= distance) {
        minId = midId;
      } else {
        maxId = midId;
      }
    }
    return minId;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  orientations: null, // an array of quat or null
  distanceFunction: vec3.dist,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkCell.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, ['orientations', 'distanceFunction']);
 
  model.distancesTime = {};
  macro.obj(model.distancesTime, { mtime: 0 });
 
  vtkPolyLine(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPolyLine');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };