All files / Sources/Common/Transform/Transform index.js

29.41% Statements 15/51
0% Branches 0/5
18.18% Functions 2/11
31.91% Lines 15/47

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                          12x           12x         12x                                         12x                   12x       12x                     12x                                     12x                     1x               12x 12x   12x 12x   12x       1x            
import { IDENTITY } from 'vtk.js/Sources/Common/Core/Math/Constants';
import { mat4, vec3 } from 'gl-matrix';
import macro from 'vtk.js/Sources/macros';
import vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
// ----------------------------------------------------------------------------
// vtkTransform methods
// ----------------------------------------------------------------------------
// eslint-disable-next-line import/no-mutable-exports
let newInstance;
 
function vtkTransform(publicAPI, model) {
  // Set our className
  model.classHierarchy.push(
    'vtkAbstractTransform',
    'vtkHomogeneousTransform',
    'vtkTransform'
  );
 
  publicAPI.transformPoint = (point, out) => {
    vec3.transformMat4(out, point, model.matrix);
    return out;
  };
 
  publicAPI.transformPoints = (points, out) => {
    const inPoint = new Float64Array(3);
    const outPoint = new Float64Array(3);
    for (let i = 0; i < points.length; i += 3) {
      inPoint[0] = points[i];
      inPoint[1] = points[i + 1];
      inPoint[2] = points[i + 2];
      vec3.transformMat4(outPoint, inPoint, model.matrix);
      out[i] = outPoint[0];
      out[i + 1] = outPoint[1];
      out[i + 2] = outPoint[2];
    }
    return out;
  };
 
  /**
   * Sets the internal state of the transform to PreMultiply.
   * All subsequent operations will occur before those already represented in the current transformation.
   * In homogeneous matrix notation, M = M*A where M is the current transformation matrix and A is the applied matrix.
   * The default is PreMultiply.
   */
  publicAPI.preMultiply = () => {
    publicAPI.setPreMultiplyFlag(true);
  };
 
  /**
   * Sets the internal state of the transform to PostMultiply.
   * All subsequent operations will occur after those already represented in the current transformation.
   * In homogeneous matrix notation, M = A*M where M is the current transformation matrix and A is the applied matrix.
   * The default is PreMultiply.
   */
  publicAPI.postMultiply = () => {
    publicAPI.setPreMultiplyFlag(false);
  };
 
  publicAPI.transformMatrix = (matrix, out) => {
    if (model.preMultiplyFlag) {
      mat4.multiply(out, model.matrix, matrix);
    } else {
      mat4.multiply(out, matrix, model.matrix);
    }
    return out;
  };
 
  // Apply the transform to each matrix in the same way as transformMatrix
  // `matrices` can be a contiguous array of float or an array of array
  publicAPI.transformMatrices = (matrices, out) => {
    const inMat = new Float64Array(16);
    const outMat = new Float64Array(16);
    const transform = model.preMultiplyFlag
      ? () => mat4.multiply(outMat, model.matrix, inMat)
      : () => mat4.multiply(outMat, inMat, model.matrix);
 
    for (let i = 0; i < matrices.length; i += 16) {
      for (let j = 0; j < 16; ++j) {
        inMat[j] = matrices[i + j];
      }
      transform();
      for (let j = 0; j < 16; ++j) {
        out[i + j] = outMat[j];
      }
    }
    return out;
  };
 
  publicAPI.getInverse = () =>
    newInstance({
      matrix: vtkMath.invertMatrix(Array.from(model.matrix), [], 4),
      preMultiplyFlag: model.preMultiplyFlag,
    });
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  preMultiplyFlag: false,
  matrix: [...IDENTITY],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
  macro.obj(publicAPI, model);
 
  macro.setGet(publicAPI, model, ['preMultiplyFlag']);
  macro.setGetArray(publicAPI, model, ['matrix'], 16);
 
  vtkTransform(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
newInstance = macro.newInstance(extend, 'vtkTransform');
export { newInstance };
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };