All files / views/threejs utilities.ts

86.76% Statements 59/68
66.67% Branches 6/9
85.71% Functions 6/7
86.76% Lines 59/68

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        6x 6x 6x     6x 1x 1x   1x 1x 1x   1x 1x   1x 1x   1x 1x         1x     6x       6x         25x 22x 22x   3x 3x 3x 3x     6x         6x 7x 7x             6x 3x 3x       3x     6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x         6x         6x 9x 9x 9x     6x         6x            
/*!
 * Copyright 2020 Cognite AS
 */
 
import * as THREE from 'three';
import { Box3 } from '../../utils/Box3';
import { vec3, mat4 } from 'gl-matrix';
import { SectorModelTransformation } from '../../models/cad/types';
 
export function fitCameraToBoundingBox(camera: THREE.Camera, bounds: Box3, radiusFactor: number = 4) {
  const min = new THREE.Vector3(bounds.min[0], bounds.min[1], bounds.min[2]);
  const max = new THREE.Vector3(bounds.max[0], bounds.max[1], bounds.max[2]);
 
  const threeBounds = new THREE.Box3(min, max);
  const boundingSphere = new THREE.Sphere();
  threeBounds.getBoundingSphere(boundingSphere);
 
  const target = boundingSphere.center;
  const distance = boundingSphere.radius * radiusFactor;
 
  const direction = new THREE.Vector3(0, 0, -1);
  direction.applyQuaternion(camera.quaternion);
 
  const position = new THREE.Vector3();
  position
    .copy(direction)
    .multiplyScalar(-distance)
    .add(target);
 
  camera.position.set(position.x, position.y, position.z);
}
 
const toThreeVector3Vars = {
  result: vec3.create()
};
 
export function toThreeVector3(
  out: THREE.Vector3,
  v: vec3,
  modelTransformation?: SectorModelTransformation
): THREE.Vector3 {
  if (!modelTransformation) {
    out.set(v[0], v[1], v[2]);
    return out;
  }
  const { result } = toThreeVector3Vars;
  vec3.transformMat4(result, v, modelTransformation.modelMatrix);
  out.set(result[0], result[1], result[2]);
  return out;
}
 
const toThreeMatrix4Vars = {
  result: mat4.create()
};
 
// TODO add out parameter
export function toThreeMatrix4(m: mat4, modelTransformation?: SectorModelTransformation): THREE.Matrix4 {
  Eif (!modelTransformation) {
    return new THREE.Matrix4().fromArray(m);
  }
  const { result } = toThreeMatrix4Vars;
  mat4.multiply(result, modelTransformation.modelMatrix, m);
  return new THREE.Matrix4().fromArray(result);
}
 
export function fromThreeVector3(out: vec3, m: THREE.Vector3, modelTransformation?: SectorModelTransformation): vec3 {
  const original = vec3.set(out, m.x, m.y, m.z);
  Iif (!modelTransformation) {
    return original;
  }
  // the fourth component is implicitly 1 in transformMat4
  return vec3.transformMat4(out, original, modelTransformation.inverseModelMatrix);
}
 
export function fromThreeMatrix(out: mat4, m: THREE.Matrix4, modelTransformation?: SectorModelTransformation): mat4 {
  out[0] = m.elements[0];
  out[1] = m.elements[1];
  out[2] = m.elements[2];
  out[3] = m.elements[3];
  out[4] = m.elements[4];
  out[5] = m.elements[5];
  out[6] = m.elements[6];
  out[7] = m.elements[7];
  out[8] = m.elements[8];
  out[9] = m.elements[9];
  out[10] = m.elements[10];
  out[11] = m.elements[11];
  out[12] = m.elements[12];
  out[13] = m.elements[13];
  out[14] = m.elements[14];
  out[15] = m.elements[15];
  Eif (modelTransformation) {
    return mat4.multiply(out, modelTransformation.inverseModelMatrix, out);
  }
  return out;
}
 
const toThreeJsBox3Vars = {
  outMin: new THREE.Vector3(),
  outMax: new THREE.Vector3()
};
 
export function toThreeJsBox3(out: THREE.Box3, box: Box3, modelTransformation?: SectorModelTransformation): THREE.Box3 {
  const { outMin, outMax } = toThreeJsBox3Vars;
  out.set(toThreeVector3(outMin, box.min, modelTransformation), toThreeVector3(outMax, box.max, modelTransformation));
  return out;
}
 
const fromThreeJsBox3Vars = {
  min: vec3.create(),
  max: vec3.create()
};
 
export function fromThreeJsBox3(box: THREE.Box3): Box3 {
  const { min, max } = fromThreeJsBox3Vars;
  fromThreeVector3(min, box.min);
  fromThreeVector3(max, box.max);
  return new Box3([min, max]);
}