{"size": 2139, "ext": "js", "max_stars_repo_path": "packages/modeling/src/maths/mat4/invert.js", "max_stars_repo_name": "heristhesiya/OpenJSCAD.org", "max_stars_repo_stars_event_min_datetime": "2017-05-09T01:51:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T12:35:31.000Z", "max_issues_repo_path": "packages/modeling/src/maths/mat4/invert.js", "max_issues_repo_name": "heristhesiya/OpenJSCAD.org", "max_issues_repo_issues_event_min_datetime": "2017-05-07T11:32:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T22:44:27.000Z", "max_forks_repo_path": "packages/modeling/src/maths/mat4/invert.js", "max_forks_repo_name": "heristhesiya/OpenJSCAD.org", "max_forks_repo_forks_event_min_datetime": "2017-05-09T21:42:26.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T16:37:23.000Z"}
{"max_stars_count": 1435, "max_issues_count": 634, "max_forks_count": 313, "avg_line_length": 29.7083333333, "max_line_length": 73, "alphanum_fraction": 0.5376344086}
/**
 * Creates a invert copy of the given matrix.
 * @author Julian Lloyd
 * code from https://github.com/jlmakes/rematrix/blob/master/src/index.js
 *
 * @param {mat4} out - receiving matrix
 * @param {mat4} m - matrix to invert
 * @returns {mat4} out
 * @alias module:modeling/maths/mat4.invert
 */
const invert = (out, a) => {
  const a00 = a[0]
  const a01 = a[1]
  const a02 = a[2]
  const a03 = a[3]
  const a10 = a[4]
  const a11 = a[5]
  const a12 = a[6]
  const a13 = a[7]
  const a20 = a[8]
  const a21 = a[9]
  const a22 = a[10]
  const a23 = a[11]
  const a30 = a[12]
  const a31 = a[13]
  const a32 = a[14]
  const a33 = a[15]

  const b00 = a00 * a11 - a01 * a10
  const b01 = a00 * a12 - a02 * a10
  const b02 = a00 * a13 - a03 * a10
  const b03 = a01 * a12 - a02 * a11
  const b04 = a01 * a13 - a03 * a11
  const b05 = a02 * a13 - a03 * a12
  const b06 = a20 * a31 - a21 * a30
  const b07 = a20 * a32 - a22 * a30
  const b08 = a20 * a33 - a23 * a30
  const b09 = a21 * a32 - a22 * a31
  const b10 = a21 * a33 - a23 * a31
  const b11 = a22 * a33 - a23 * a32

  // Calculate the determinant
  let det =
    b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06

  if (!det) {
    return null
  }
  det = 1.0 / det

  out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det
  out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det
  out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det
  out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det
  out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det
  out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det
  out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det
  out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det
  out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det
  out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det
  out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det
  out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det
  out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det
  out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det
  out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det
  out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det

  return out
}

module.exports = invert
