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

87.22% Statements 157/180
55% Branches 22/40
100% Functions 11/11
88.3% Lines 151/171

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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443            1x 1x               18x   18x     11x   11x 11x   11x 11x 11x   11x 11x   11x                           11x   11x 11x 11x   11x 11x   11x         11x             11x 11x   11x         11x             11x 33x         11x                       11x 11x   11x 11x   11x     11x 11x           11x 12x     12x     12x       12x   1x           11x 11x   11x   11x 11x   11x 11x   11x               11x       11x 11x   11x                   11x 11x 11x 11x   11x   11x                     11x 11x             11x 11x 11x                 11x 1x 1x     1x 1x 1x   11x 11x 11x 11x 11x           18x 5x       5x 5x   5x 5x           5x       5x       18x 7x       7x         7x   7x 7x 7x   7x 7x 7x 7x   7x 7x       7x 7x 7x   7x 7x   7x         7x   7x 7x           7x         7x     7x         7x             7x 21x             7x 7x 21x     7x 7x 21x     7x   7x   7x         7x       7x               7x 7x 7x 21x 21x     7x 7x     7x   7x     18x 4x       4x 4x   4x   4x 4x   4x         4x     4x     4x               1x                         18x     18x   18x 18x 18x           18x   18x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkAbstractPicker from 'vtk.js/Sources/Rendering/Core/AbstractPicker';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import { mat4, vec3, vec4 } from 'gl-matrix';
 
const { vtkErrorMacro } = macro;
const { vtkWarningMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkPicker methods
// ----------------------------------------------------------------------------
 
function vtkPicker(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPicker');
 
  const superClass = { ...publicAPI };
 
  function initialize() {
    superClass.initialize();
 
    model.actors = [];
    model.pickedPositions = [];
 
    model.mapperPosition[0] = 0.0;
    model.mapperPosition[1] = 0.0;
    model.mapperPosition[2] = 0.0;
 
    model.mapper = null;
    model.dataSet = null;
 
    model.globalTMin = Number.MAX_VALUE;
  }
 
  /**
   * Compute the tolerance in world coordinates.
   * Do this by determining the world coordinates of the diagonal points of the
   * window, computing the width of the window in world coordinates, and
   * multiplying by the tolerance.
   * @param {Number} selectionZ
   * @param {Number} aspect
   * @param {vtkRenderer} renderer
   * @returns {Number} the computed tolerance
   */
  function computeTolerance(selectionZ, aspect, renderer) {
    let tolerance = 0.0;
 
    const view = renderer.getRenderWindow().getViews()[0];
    const viewport = renderer.getViewport();
    const winSize = view.getSize();
 
    let x = winSize[0] * viewport[0];
    let y = winSize[1] * viewport[1];
 
    const normalizedLeftDisplay = view.displayToNormalizedDisplay(
      x,
      y,
      selectionZ
    );
    const windowLowerLeft = renderer.normalizedDisplayToWorld(
      normalizedLeftDisplay[0],
      normalizedLeftDisplay[1],
      normalizedLeftDisplay[2],
      aspect
    );
 
    x = winSize[0] * viewport[2];
    y = winSize[1] * viewport[3];
 
    const normalizedRightDisplay = view.displayToNormalizedDisplay(
      x,
      y,
      selectionZ
    );
    const windowUpperRight = renderer.normalizedDisplayToWorld(
      normalizedRightDisplay[0],
      normalizedRightDisplay[1],
      normalizedRightDisplay[2],
      aspect
    );
 
    for (let i = 0; i < 3; i++) {
      tolerance +=
        (windowUpperRight[i] - windowLowerLeft[i]) *
        (windowUpperRight[i] - windowLowerLeft[i]);
    }
 
    return Math.sqrt(tolerance);
  }
 
  /**
   * Perform picking on the given renderer, given a ray defined in world coordinates.
   * @param {*} renderer
   * @param {*} tolerance
   * @param {*} p1World
   * @param {*} p2World
   * @returns true if we picked something else false
   */
  function pick3DInternal(renderer, tolerance, p1World, p2World) {
    const p1Mapper = new Float64Array(4);
    const p2Mapper = new Float64Array(4);
 
    const ray = [];
    const hitPosition = [];
 
    const props = model.pickFromList ? model.pickList : renderer.getActors();
 
    // pre-allocate some arrays.
    const transformScale = new Float64Array(3);
    const pickedPosition = new Float64Array(3);
 
    // Loop over props.
    // Transform ray (defined from position of camera to selection point) into coordinates of mapper (not
    // transformed to actors coordinates!  Reduces overall computation!!!).
    // Note that only vtkProp3D's can be picked by vtkPicker.
    props.forEach((prop) => {
      const mapper = prop.getMapper();
 
      const propIsFullyTranslucent =
        prop.getProperty?.().getOpacity?.() === 0.0;
 
      const pickable =
        prop.getNestedPickable() &&
        prop.getNestedVisibility() &&
        !propIsFullyTranslucent;
 
      if (!pickable) {
        // prop cannot be picked
        return;
      }
 
      // The prop is candidate for picking:
      // - get its composite matrix and invert it
      // - use the inverted matrix to transform the ray points into mapper coordinates
      model.transformMatrix = prop.getMatrix().slice(0);
      mat4.transpose(model.transformMatrix, model.transformMatrix);
 
      mat4.invert(model.transformMatrix, model.transformMatrix);
 
      vec4.transformMat4(p1Mapper, p1World, model.transformMatrix);
      vec4.transformMat4(p2Mapper, p2World, model.transformMatrix);
 
      vec3.scale(p1Mapper, p1Mapper, 1 / p1Mapper[3]);
      vec3.scale(p2Mapper, p2Mapper, 1 / p2Mapper[3]);
 
      vtkMath.subtract(p2Mapper, p1Mapper, ray);
 
      // We now have the ray endpoints in mapper coordinates.
      // Compare it with the mapper bounds to check if intersection is possible.
 
      // Get the bounding box of the mapper.
      // Note that the tolerance is added to the bounding box to make sure things on the edge of the
      // bounding box are picked correctly.
      const bounds = mapper
        ? vtkBoundingBox.inflate(mapper.getBounds(), tolerance)
        : [...vtkBoundingBox.INIT_BOUNDS];
 
      if (vtkBoundingBox.intersectBox(bounds, p1Mapper, ray, hitPosition, [])) {
        mat4.getScaling(transformScale, model.transformMatrix);
 
        const t = model.intersectWithLine(
          p1Mapper,
          p2Mapper,
          tolerance *
            0.333 *
            (transformScale[0] + transformScale[1] + transformScale[2]),
          prop,
          mapper
        );
 
        if (t < Number.MAX_VALUE) {
          pickedPosition[0] = (1.0 - t) * p1World[0] + t * p2World[0];
          pickedPosition[1] = (1.0 - t) * p1World[1] + t * p2World[1];
          pickedPosition[2] = (1.0 - t) * p1World[2] + t * p2World[2];
 
          const actorIndex = model.actors.indexOf(prop);
 
          Iif (actorIndex !== -1) {
            // If already in list, compare the previous picked position with the new one.
            // Store the new one if it is closer from the ray endpoint.
            const previousPickedPosition = model.pickedPositions[actorIndex];
            if (
              vtkMath.distance2BetweenPoints(p1World, pickedPosition) <
              vtkMath.distance2BetweenPoints(p1World, previousPickedPosition)
            ) {
              model.pickedPositions[actorIndex] = pickedPosition.slice(0);
            }
          } else {
            model.actors.push(prop);
            model.pickedPositions.push(pickedPosition.slice(0));
          }
        }
      }
    });
 
    // sort array by distance
    const tempArray = [];
    for (let i = 0; i < model.pickedPositions.length; i++) {
      tempArray.push({
        actor: model.actors[i],
        pickedPosition: model.pickedPositions[i],
        distance2: vtkMath.distance2BetweenPoints(
          p1World,
          model.pickedPositions[i]
        ),
      });
    }
    tempArray.sort((a, b) => {
      const keyA = a.distance2;
      const keyB = b.distance2;
      // order the actors based on the distance2 attribute, so the near actors comes
      // first in the list
      Iif (keyA < keyB) return -1;
      Iif (keyA > keyB) return 1;
      return 0;
    });
    model.pickedPositions = [];
    model.actors = [];
    tempArray.forEach((obj) => {
      model.pickedPositions.push(obj.pickedPosition);
      model.actors.push(obj.actor);
    });
  }
 
  // Intersect data with specified ray.
  // Project the center point of the mapper onto the ray and determine its parametric value
  model.intersectWithLine = (p1, p2, tolerance, prop, mapper) => {
    Iif (!mapper) {
      return Number.MAX_VALUE;
    }
 
    const center = mapper.getCenter();
    const ray = vec3.subtract(new Float64Array(3), p2, p1);
 
    const rayFactor = vtkMath.dot(ray, ray);
    Iif (rayFactor === 0.0) {
      return 2.0;
    }
 
    // Project the center point onto the ray and determine its parametric value
    const t =
      (ray[0] * (center[0] - p1[0]) +
        ray[1] * (center[1] - p1[1]) +
        ray[2] * (center[2] - p1[2])) /
      rayFactor;
    return t;
  };
 
  // To be overridden in subclasses
  publicAPI.pick = (selection, renderer) => {
    Iif (selection.length !== 3) {
      vtkWarningMacro('vtkPicker.pick - selection needs three components');
    }
 
    Iif (!renderer) {
      vtkErrorMacro('vtkPicker.pick - renderer cannot be null');
      throw new Error('renderer cannot be null');
    }
 
    initialize();
 
    const selectionX = selection[0];
    const selectionY = selection[1];
    let selectionZ = selection[2];
 
    model.renderer = renderer;
    model.selectionPoint[0] = selectionX;
    model.selectionPoint[1] = selectionY;
    model.selectionPoint[2] = selectionZ;
 
    const p1World = new Float64Array(4);
    const p2World = new Float64Array(4);
 
    // Get camera focal point and position. Convert to display (screen)
    // coordinates. We need a depth value for z-buffer.
    const camera = renderer.getActiveCamera();
    const cameraPos = camera.getPosition();
    const cameraFP = camera.getFocalPoint();
 
    const view = renderer.getRenderWindow().getViews()[0];
    const dims = view.getViewportSize(renderer);
 
    Iif (dims[1] === 0) {
      vtkWarningMacro('vtkPicker.pick - viewport area is 0');
      return;
    }
 
    const aspect = dims[0] / dims[1];
 
    let displayCoords = [];
    displayCoords = renderer.worldToNormalizedDisplay(
      cameraFP[0],
      cameraFP[1],
      cameraFP[2],
      aspect
    );
    displayCoords = view.normalizedDisplayToDisplay(
      displayCoords[0],
      displayCoords[1],
      displayCoords[2]
    );
    selectionZ = displayCoords[2];
 
    // Convert the selection point into world coordinates.
    const normalizedDisplay = view.displayToNormalizedDisplay(
      selectionX,
      selectionY,
      selectionZ
    );
    const worldCoords = renderer.normalizedDisplayToWorld(
      normalizedDisplay[0],
      normalizedDisplay[1],
      normalizedDisplay[2],
      aspect
    );
 
    for (let i = 0; i < 3; i++) {
      model.pickPosition[i] = worldCoords[i];
    }
 
    //  Compute the ray endpoints. The ray is along the line running from
    //  the camera position to the selection point, starting where this line
    //  intersects the front clipping plane, and terminating where this
    //  line intersects the back clipping plane.
    const ray = [];
    for (let i = 0; i < 3; i++) {
      ray[i] = model.pickPosition[i] - cameraPos[i];
    }
 
    const cameraDOP = [];
    for (let i = 0; i < 3; i++) {
      cameraDOP[i] = cameraFP[i] - cameraPos[i];
    }
 
    vtkMath.normalize(cameraDOP);
 
    const rayLength = vtkMath.dot(cameraDOP, ray);
 
    Iif (rayLength === 0.0) {
      vtkWarningMacro('Picker::Pick Cannot process points');
      return;
    }
 
    const clipRange = camera.getClippingRange();
 
    let tF;
    let tB;
    Iif (camera.getParallelProjection()) {
      tF = clipRange[0] - rayLength;
      tB = clipRange[1] - rayLength;
      for (let i = 0; i < 3; i++) {
        p1World[i] = model.pickPosition[i] + tF * cameraDOP[i];
        p2World[i] = model.pickPosition[i] + tB * cameraDOP[i];
      }
    } else {
      tF = clipRange[0] / rayLength;
      tB = clipRange[1] / rayLength;
      for (let i = 0; i < 3; i++) {
        p1World[i] = cameraPos[i] + tF * ray[i];
        p2World[i] = cameraPos[i] + tB * ray[i];
      }
    }
    p1World[3] = 1.0;
    p2World[3] = 1.0;
 
    const tolerance =
      computeTolerance(selectionZ, aspect, renderer) * model.tolerance;
 
    pick3DInternal(model.renderer, tolerance, p1World, p2World);
  };
 
  publicAPI.pick3DPoint = (selectionPoint, focalPoint, renderer) => {
    Iif (!renderer) {
      throw new Error('renderer cannot be null');
    }
 
    initialize();
    model.renderer = renderer;
 
    vec3.copy(model.selectionPoint, selectionPoint);
 
    const view = renderer.getRenderWindow().getViews()[0];
    const dims = view.getViewportSize(renderer);
 
    Iif (dims[1] === 0) {
      vtkWarningMacro('vtkPicker.pick3DPoint - viewport area is 0');
      return;
    }
 
    const aspect = dims[0] / dims[1];
 
    const tolerance =
      computeTolerance(model.selectionPoint[2], aspect, renderer) *
      model.tolerance;
 
    pick3DInternal(renderer, tolerance, selectionPoint, focalPoint);
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  tolerance: 0.025,
  mapperPosition: [0.0, 0.0, 0.0],
  mapper: null,
  dataSet: null,
  actors: [],
  pickedPositions: [],
  transformMatrix: null,
  globalTMin: Number.MAX_VALUE,
};
 
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkAbstractPicker.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, ['tolerance']);
  macro.setGetArray(publicAPI, model, ['mapperPosition'], 3);
  macro.get(publicAPI, model, [
    'mapper',
    'dataSet',
    'actors',
    'pickedPositions',
  ]);
  macro.event(publicAPI, model, 'pickChange');
 
  vtkPicker(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPicker');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };