All files / Sources/Widgets/Core/StateBuilder orientationMixin.js

29.16% Statements 7/24
0% Branches 0/11
33.33% Functions 2/6
29.16% Lines 7/24

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                                                                  23x   23x             23x                                     1x                 23x 23x 23x            
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
function eq(v1, v2) {
  return (
    v1.length === 3 &&
    v2.length === 3 &&
    v1[0] === v2[0] &&
    v1[1] === v2[1] &&
    v1[2] === v2[2]
  );
}
 
function isSame(o, p1, p2, before) {
  return eq(o, before.o) && eq(p1, before.p1) && eq(p2, before.p2);
}
 
// function axis(o, p1, p2) {
//   if (o[0] === p1[0] && p1[0] === p2[0]) {
//     return 'X';
//   }
//   if (o[1] === p1[1] && p1[1] === p2[1]) {
//     return 'Y';
//   }
//   if (o[2] === p1[2] && p1[2] === p2[2]) {
//     return 'Z';
//   }
//   return '?';
// }
 
// ----------------------------------------------------------------------------
 
function vtkOrientationMixin(publicAPI, model) {
  const previousPoints = { o: [], p1: [], p2: [] };
 
  publicAPI.normalize = () => {
    vtkMath.normalize(model.up);
    vtkMath.normalize(model.right);
    vtkMath.normalize(model.direction);
    publicAPI.modified();
  };
 
  publicAPI.updateFromOriginRightUp = (o, p1, p2) => {
    if (isSame(o, p1, p2, previousPoints)) {
      return;
    }
    previousPoints.o = o.slice();
    previousPoints.p1 = p1.slice();
    previousPoints.p2 = p2.slice();
 
    model.up = [p2[0] - o[0], p2[1] - o[1], p2[2] - o[2]];
    model.right = [p1[0] - o[0], p1[1] - o[1], p1[2] - o[2]];
    vtkMath.cross(model.up, model.right, model.direction);
    vtkMath.cross(model.direction, model.up, model.right);
    publicAPI.normalize();
    publicAPI.modified();
  };
}
 
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  up: [0, 1, 0],
  right: [1, 0, 0],
  direction: [0, 0, 1],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
  macro.setGetArray(publicAPI, model, ['up', 'right', 'direction'], 3);
  vtkOrientationMixin(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export default { extend };