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

98.14% Statements 53/54
66.66% Branches 10/15
100% Functions 4/4
98.14% Lines 53/54

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                    2x     2x   2x 7x 7x 6x 6x     7x     2x   6x 4x 4x 4x   4x 4x 4x 4x   4x   3x 3x 3x       3x 3x     3x 3x 3x       3x 3x 3x     3x 3x   3x 3x 3x   3x 3x 3x   3x 3x 3x   3x     4x         4x     4x 4x                 1x                 2x     2x   2x     2x   2x     2x         1x          
import { vec3, mat4 } from 'gl-matrix';
import macro from 'vtk.js/Sources/macros';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
 
// ----------------------------------------------------------------------------
// vtkFollower methods
// ----------------------------------------------------------------------------
 
function vtkFollower(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkFollower');
 
  // Capture 'parentClass' api for internal use
  const superClass = { ...publicAPI };
 
  publicAPI.getMTime = () => {
    let mt = superClass.getMTime();
    if (model.camera !== null) {
      const time = model.camera.getMTime();
      mt = time > mt ? time : mt;
    }
 
    return mt;
  };
 
  publicAPI.computeMatrix = () => {
    // check whether or not need to rebuild the matrix
    if (publicAPI.getMTime() > model.matrixMTime.getMTime()) {
      mat4.identity(model.matrix);
      if (model.userMatrix) {
        mat4.multiply(model.matrix, model.matrix, model.userMatrix);
      }
      mat4.translate(model.matrix, model.matrix, model.origin);
      mat4.translate(model.matrix, model.matrix, model.position);
      mat4.multiply(model.matrix, model.matrix, model.rotation);
      mat4.scale(model.matrix, model.matrix, model.scale);
 
      if (model.camera) {
        // first compute our target viewUp
        const vup = new Float64Array(model.viewUp);
        if (!model.useViewUp) {
          vec3.set(vup, ...model.camera.getViewUp());
        }
 
        // compute a vpn
        const vpn = new Float64Array(3);
        Iif (model.camera.getParallelProjection()) {
          vec3.set(vpn, ...model.camera.getViewPlaneNormal());
        } else {
          vec3.set(vpn, ...model.position);
          vec3.subtract(vpn, model.camera.getPosition(), vpn);
          vec3.normalize(vpn, vpn);
        }
 
        // compute vright
        const vright = new Float64Array(3);
        vec3.cross(vright, vup, vpn);
        vec3.normalize(vright, vright);
 
        // now recompute the vpn so that it is orthogonal to vup
        vec3.cross(vpn, vright, vup);
        vec3.normalize(vpn, vpn);
 
        model.followerMatrix[0] = vright[0];
        model.followerMatrix[1] = vright[1];
        model.followerMatrix[2] = vright[2];
 
        model.followerMatrix[4] = vup[0];
        model.followerMatrix[5] = vup[1];
        model.followerMatrix[6] = vup[2];
 
        model.followerMatrix[8] = vpn[0];
        model.followerMatrix[9] = vpn[1];
        model.followerMatrix[10] = vpn[2];
 
        mat4.multiply(model.matrix, model.matrix, model.followerMatrix);
      }
 
      mat4.translate(model.matrix, model.matrix, [
        -model.origin[0],
        -model.origin[1],
        -model.origin[2],
      ]);
      mat4.transpose(model.matrix, model.matrix);
 
      // check for identity
      model.isIdentity = false;
      model.matrixMTime.modified();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  viewUp: [0, 1, 0],
  useViewUp: false,
  camera: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkActor.extend(publicAPI, model, initialValues);
 
  model.followerMatrix = mat4.identity(new Float64Array(16));
 
  // Build VTK API
  macro.setGet(publicAPI, model, ['useViewUp', 'camera']);
 
  macro.setGetArray(publicAPI, model, ['viewUp'], 3);
 
  // Object methods
  vtkFollower(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkFollower');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };