All files / Sources/Rendering/Core/PointPicker index.js

66.29% Statements 59/89
54.05% Branches 20/37
100% Functions 4/4
67.05% Lines 57/85

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        1x               6x   6x 3x   3x 1x 1x 1x 1x   2x 2x     3x     6x   2x     2x 2x   2x       2x 2x 6x     2x 2x           2x 2x 2x 2x 2x 2x 2x   2x                                                                                                   2x 22x   22x                   22x 8x 8x 24x 24x 24x 8x     8x   5x 5x 5x 5x 5x 5x           2x 2x               1x                 6x     6x   6x 6x 6x   6x         1x          
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkPicker from 'vtk.js/Sources/Rendering/Core/Picker';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkPointPicker methods
// ----------------------------------------------------------------------------
 
function vtkPointPicker(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPointPicker');
 
  model.intersectWithLine = (p1, p2, tolerance, prop, mapper) => {
    let tMin = Number.MAX_VALUE;
 
    if (mapper.isA('vtkImageMapper') || mapper.isA('vtkImageArrayMapper')) {
      const pickData = mapper.intersectWithLineForPointPicking(p1, p2);
      if (pickData) {
        tMin = pickData.t;
        model.pointIJK = pickData.ijk;
      }
    } else if (mapper.isA('vtkMapper')) {
      tMin = model.intersectActorWithLine(p1, p2, tolerance, mapper);
    }
 
    return tMin;
  };
 
  model.intersectActorWithLine = (p1, p2, tolerance, mapper) => {
    // Get dataset
    const input = mapper.getInputData();
 
    // Determine appropriate info
    let ptId = 0;
    const numPts = input.getPoints().getNumberOfPoints();
 
    Iif (numPts <= ptId) {
      return 2.0;
    }
 
    const ray = [];
    for (let i = 0; i < 3; i++) {
      ray[i] = p2[i] - p1[i];
    }
 
    const rayFactor = vtkMath.dot(ray, ray);
    Iif (rayFactor === 0.0) {
      vtkErrorMacro('Cannot process points');
      return 2.0;
    }
 
    let t;
    let minPtId = -1;
    let tMin = Number.MAX_VALUE;
    let minPtDist = Number.MAX_VALUE;
    const projXYZ = [];
    const minXYZ = [];
    const x = [];
    const points = input.getPoints();
 
    Iif (model.useCells) {
      const cellData = input.getPolys().getData();
      const nbPointsPerCell = cellData[0];
      const nbCells = input.getPolys().getNumberOfCells();
 
      for (let cellID = 0; cellID < nbCells; cellID++) {
        const firstPointIndex = cellID * nbPointsPerCell + 1;
        const lastPointIndex = firstPointIndex + nbPointsPerCell;
 
        for (
          let pointIndex = firstPointIndex;
          pointIndex < lastPointIndex;
          pointIndex++
        ) {
          const pointDataIndex = cellData[pointIndex];
          points.getPoint(pointDataIndex, x);
 
          t =
            (ray[0] * (x[0] - p1[0]) +
              ray[1] * (x[1] - p1[1]) +
              ray[2] * (x[2] - p1[2])) /
            rayFactor;
 
          // If we find a point closer than we currently have, see whether it
          // lies within the pick tolerance and clipping planes. We keep track
          // of the point closest to the line (use a fudge factor for points
          // nearly the same distance away.)
          if (t >= 0.0 && t <= 1.0 && t <= tMin + model.tolerance) {
            let maxDist = 0.0;
            for (let i = 0; i < 3; i++) {
              projXYZ[i] = p1[i] + t * ray[i];
              const dist = Math.abs(x[i] - projXYZ[i]);
              if (dist > maxDist) {
                maxDist = dist;
              }
            } // end for i
            if (maxDist <= tolerance && maxDist < minPtDist) {
              // within tolerance
              minPtId = ptId;
              minXYZ[0] = x[0];
              minXYZ[1] = x[1];
              minXYZ[2] = x[2];
              minPtDist = maxDist;
              tMin = t;
            }
          }
        } // end for pointIndex
      } // end for cellID
    } else {
      // end if model.useCells
      for (ptId = 0; ptId < numPts; ptId++) {
        points.getPoint(ptId, x);
 
        t =
          (ray[0] * (x[0] - p1[0]) +
            ray[1] * (x[1] - p1[1]) +
            ray[2] * (x[2] - p1[2])) /
          rayFactor;
 
        // If we find a point closer than we currently have, see whether it
        // lies within the pick tolerance and clipping planes. We keep track
        // of the point closest to the line (use a fudge factor for points
        // nearly the same distance away.)
        if (t >= 0.0 && t <= 1.0 && t <= tMin + model.tolerance) {
          let maxDist = 0.0;
          for (let i = 0; i < 3; i++) {
            projXYZ[i] = p1[i] + t * ray[i];
            const dist = Math.abs(x[i] - projXYZ[i]);
            if (dist > maxDist) {
              maxDist = dist;
            }
          } // end for i
          if (maxDist <= tolerance && maxDist < minPtDist) {
            // within tolerance
            minPtId = ptId;
            minXYZ[0] = x[0];
            minXYZ[1] = x[1];
            minXYZ[2] = x[2];
            minPtDist = maxDist;
            tMin = t;
          }
        }
      }
    }
 
    model.pointId = minPtId;
    return tMin;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  pointId: -1,
  pointIJK: [],
  useCells: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkPicker.extend(publicAPI, model, initialValues);
 
  macro.getArray(publicAPI, model, ['pointIJK']);
  macro.get(publicAPI, model, ['pointId']);
  macro.setGet(publicAPI, model, ['useCells']);
 
  vtkPointPicker(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPointPicker');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };