inverse method Null safety

double inverse(
  1. Float32List matrix,
  2. int offset
)

Compute the inverse of the 4x4 matrix starting at offset.

Implementation

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

  if (det == 0.0) {
    return det;
  }

  final invDet = 1.0 / det;

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

  return det;
}