All files / Sources/Widgets/Widgets3D/ImageCroppingWidget helpers.js

36.36% Statements 8/22
100% Branches 7/7
33.33% Functions 2/6
33.33% Lines 7/21

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      1x                                               78x 26x 8x   18x 12x   6x                                      
import { quat, mat4, vec3 } from 'gl-matrix';
 
// Labels used to encode handle position in the handle state's name property
export const AXES = ['-', '=', '+'];
 
// ----------------------------------------------------------------------------
 
export function transformVec3(ain, transform) {
  const vout = new Float64Array(3);
  vec3.transformMat4(vout, ain, transform);
  return vout;
}
 
// ----------------------------------------------------------------------------
 
export function rotateVec3(vec, transform) {
  // transform is a mat4
  const out = vec3.create();
  const q = quat.create();
  mat4.getRotation(q, transform);
  vec3.transformQuat(out, vec, q);
  return out;
}
 
// ----------------------------------------------------------------------------
 
export function handleTypeFromName(name) {
  const [i, j, k] = name.split('').map((l) => AXES.indexOf(l) - 1);
  if (i * j * k !== 0) {
    return 'corners';
  }
  if (i * j !== 0 || j * k !== 0 || k * i !== 0) {
    return 'edges';
  }
  return 'faces';
}
 
export function calculateCropperCenter(planes, transform) {
  // get center of current crop box
  const center = [
    (planes[0] + planes[1]) / 2,
    (planes[2] + planes[3]) / 2,
    (planes[4] + planes[5]) / 2,
  ];
  return transformVec3(center, transform);
}
 
export function calculateDirection(v1, v2) {
  const direction = vec3.create();
  vec3.subtract(direction, v1, v2);
  vec3.normalize(direction, direction);
  return direction;
}