All files / Sources/Common/Core/MatrixBuilder index.js

90.76% Statements 59/65
78.94% Branches 15/19
83.33% Functions 15/18
90.47% Lines 57/63

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            137x   1x       347x 347x 347x       57x 57x 57x   57x 57x 57x 57x 57x 57x 43x     14x 14x     1x 1x 1x     14x 14x   14x       136x 136x 136x 136x                           13x 13x       188x 188x 188x       12x 12x 12x                 1x                                   1x       1x 1x       3x 3x           358x   48x       310x 310x 18694x 18694x 18694x 18694x 18694x       310x       3x       13x 13x   13x         25x       322x              
import { vec3, mat4, glMatrix } from 'gl-matrix';
 
// eslint-disable-next-line import/no-cycle
import { areMatricesEqual } from 'vtk.js/Sources/Common/Core/Math';
import { IDENTITY } from 'vtk.js/Sources/Common/Core/Math/Constants';
 
const NoOp = (v) => v;
 
const EPSILON = 1e-6;
 
class Transform {
  constructor(useDegree = false) {
    this.matrix = mat4.identity(new Float64Array(16));
    this.tmp = new Float64Array(3);
    this.angleConv = useDegree ? glMatrix.toRadian : NoOp;
  }
 
  rotateFromDirections(originDirection, targetDirection) {
    const src = new Float64Array(3);
    const dst = new Float64Array(3);
    const transf = new Float64Array(16);
 
    vec3.set(src, originDirection[0], originDirection[1], originDirection[2]);
    vec3.set(dst, targetDirection[0], targetDirection[1], targetDirection[2]);
    vec3.normalize(src, src);
    vec3.normalize(dst, dst);
    const cosAlpha = vec3.dot(src, dst);
    if (cosAlpha >= 1) {
      return this;
    }
 
    vec3.cross(this.tmp, src, dst);
    if (vec3.length(this.tmp) < EPSILON) {
      // cross product is 0, so pick arbitrary axis perpendicular
      // to originDirection.
      vec3.cross(this.tmp, [1, 0, 0], originDirection);
      if (vec3.length(this.tmp) < EPSILON) {
        vec3.cross(this.tmp, [0, 1, 0], originDirection);
      }
    }
    mat4.fromRotation(transf, Math.acos(cosAlpha), this.tmp);
    mat4.multiply(this.matrix, this.matrix, transf);
 
    return this;
  }
 
  rotate(angle, axis) {
    vec3.set(this.tmp, ...axis);
    vec3.normalize(this.tmp, this.tmp);
    mat4.rotate(this.matrix, this.matrix, this.angleConv(angle), this.tmp);
    return this;
  }
 
  rotateX(angle) {
    mat4.rotateX(this.matrix, this.matrix, this.angleConv(angle));
    return this;
  }
 
  rotateY(angle) {
    mat4.rotateY(this.matrix, this.matrix, this.angleConv(angle));
    return this;
  }
 
  rotateZ(angle) {
    mat4.rotateZ(this.matrix, this.matrix, this.angleConv(angle));
    return this;
  }
 
  translate(x, y, z) {
    vec3.set(this.tmp, x, y, z);
    mat4.translate(this.matrix, this.matrix, this.tmp);
    return this;
  }
 
  scale(sx, sy, sz) {
    vec3.set(this.tmp, sx, sy, sz);
    mat4.scale(this.matrix, this.matrix, this.tmp);
    return this;
  }
 
  multiply(mat4x4) {
    mat4.multiply(this.matrix, this.matrix, mat4x4);
    return this;
  }
 
  multiply3x3(mat3x3) {
    mat4.multiply(this.matrix, this.matrix, [
      mat3x3[0],
      mat3x3[1],
      mat3x3[2],
      0,
      mat3x3[3],
      mat3x3[4],
      mat3x3[5],
      0,
      mat3x3[6],
      mat3x3[7],
      mat3x3[8],
      0,
      0,
      0,
      0,
      1,
    ]);
    return this;
  }
 
  invert() {
    mat4.invert(this.matrix, this.matrix);
    return this;
  }
 
  identity() {
    mat4.identity(this.matrix);
    return this;
  }
 
  //-----------
 
  apply(typedArray, offset = 0, nbIterations = -1) {
    if (areMatricesEqual(IDENTITY, this.matrix)) {
      // Make sure we can chain apply...
      return this;
    }
 
    const size =
      nbIterations === -1 ? typedArray.length : offset + nbIterations * 3;
    for (let i = offset; i < size; i += 3) {
      vec3.set(this.tmp, typedArray[i], typedArray[i + 1], typedArray[i + 2]);
      vec3.transformMat4(this.tmp, this.tmp, this.matrix);
      typedArray[i] = this.tmp[0];
      typedArray[i + 1] = this.tmp[1];
      typedArray[i + 2] = this.tmp[2];
    }
 
    // Make sure we can chain apply...
    return this;
  }
 
  getMatrix() {
    return this.matrix;
  }
 
  setMatrix(mat4x4) {
    if (!!mat4x4 && mat4x4.length === 16) {
      mat4.copy(this.matrix, mat4x4);
    }
    return this;
  }
}
 
function buildFromDegree() {
  return new Transform(true);
}
 
function buildFromRadian() {
  return new Transform(false);
}
 
export default {
  buildFromDegree,
  buildFromRadian,
};