All files / utils Box3.ts

64.71% Statements 11/17
100% Branches 0/0
33.33% Functions 1/3
64.71% Lines 11/17

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        9x   9x                     24x 24x 24x 32x 32x 32x 32x 32x 32x                    
/*!
 * Copyright 2020 Cognite AS
 */
 
import { vec3, mat4 } from 'gl-matrix';
 
export class Box3 {
  public readonly min: vec3;
  public readonly max: vec3;
 
  public get center(): vec3 {
    const result = vec3.create();
    const sum = vec3.add(result, this.min, this.max);
    return vec3.scale(result, sum, 0.5);
  }
 
  constructor(points: vec3[]) {
    this.min = vec3.fromValues(Infinity, Infinity, Infinity);
    this.max = vec3.fromValues(-Infinity, -Infinity, -Infinity);
    for (const p of points) {
      this.min[0] = Math.min(p[0], this.min[0]);
      this.min[1] = Math.min(p[1], this.min[1]);
      this.min[2] = Math.min(p[2], this.min[2]);
      this.max[0] = Math.max(p[0], this.max[0]);
      this.max[1] = Math.max(p[1], this.max[1]);
      this.max[2] = Math.max(p[2], this.max[2]);
    }
  }
 
  createTransformed(matrix: mat4): Box3 {
    const pMin = vec3.transformMat4(vec3.create(), this.min, matrix);
    const pMax = vec3.transformMat4(vec3.create(), this.max, matrix);
    return new Box3([pMin, pMax]);
  }
}