invert method

Matrix3 invert()

Inverts this matrix, using the analytic method. You can not invert with a determinant of zero. If you attempt this, the method produces a zero matrix instead.

Implementation

Matrix3 invert() {
  final te = storage,
      n11 = te[0],
      n21 = te[1],
      n31 = te[2],
      n12 = te[3],
      n22 = te[4],
      n32 = te[5],
      n13 = te[6],
      n23 = te[7],
      n33 = te[8],
      t11 = n33 * n22 - n32 * n23,
      t12 = n32 * n13 - n33 * n12,
      t13 = n23 * n12 - n22 * n13,
      det = n11 * t11 + n21 * t12 + n31 * t13;

  if (det == 0) return setValues(0, 0, 0, 0, 0, 0, 0, 0, 0);

  final detInv = 1 / det;

  te[0] = t11 * detInv;
  te[1] = (n31 * n23 - n33 * n21) * detInv;
  te[2] = (n32 * n21 - n31 * n22) * detInv;

  te[3] = t12 * detInv;
  te[4] = (n33 * n11 - n31 * n13) * detInv;
  te[5] = (n31 * n12 - n32 * n11) * detInv;

  te[6] = t13 * detInv;
  te[7] = (n21 * n13 - n23 * n11) * detInv;
  te[8] = (n22 * n11 - n21 * n12) * detInv;

  return this;
}