All files geometry_helper.ts

88.88% Statements 56/63
66.66% Branches 2/3
100% Functions 2/2
88.88% Lines 56/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 641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x               2x 2x 2x  
/**
 * @fileOverview
 * Defines various geometry related helper functions
 */
 
import {
  apply as matrixApply,
  sum as matrixSum,
  divide as matrixDivide,
  size,
  index,
  subset,
  det,
  dotMultiply
} from 'mathjs';
 
 
 
 
 
/**
 * computes centroid given a set of points
 * @param points
 * @returns centroid of 'points'
 */
export function getCentroid(points: number[][]) {
  const pointsSize = size(points).valueOf() as Array<number>;
  const numPoints = pointsSize[0];
  const pointsSum = matrixApply(points, 0, matrixSum);
  const centroid = matrixDivide(
    pointsSum,
    numPoints
  ).valueOf() as Array<number>;
  return centroid;
}
 
 
 
 
/**
 * Matrix denotes the basis vectors of the linear space. The orientation of the
 * basis vector should not change after rotating. So after computing rotation
 * matrix, we should check if the orientation is the same as before. Sign of
 * determinant of the matrix gives the orientation of the basis vectors.
 * Therefore, if the sign of the determinant is negative, we multiply the third
 * column of rotation matrix by -1, so that the orientation changes and sign of
 * determinant becomes positive again.
 * @param rotationalMatrix
 * @returns 3x3 matrix
 */
export function checkOrientation(rotationalMatrix: number[][]) {
  const determinant = det(rotationalMatrix);
  if (determinant < 0) {
    const thirdColumn = subset(rotationalMatrix, index([0, 1, 2], 2));
    rotationalMatrix = subset(
      rotationalMatrix,
      index([0, 1, 2], 2),
      dotMultiply(thirdColumn, -1)
    );
  }
 
  return rotationalMatrix;
}