getRotation function

List<double> getRotation(
  1. List<double> out,
  2. List<double> mat
)

Returns a quaternion representing the rotational component of a transformation matrix. If a matrix is built with fromRotationTranslation, the returned quaternion will be the same as the quaternion originally supplied. @param {quat} out Quaternion to receive the rotation component @param {ReadonlyMat4} mat Matrix to be decomposed (input) @return {quat} out

Implementation

List<double> getRotation(List<double> out, List<double> mat) {
  final scaling = <double>[];
  getScaling(scaling, mat);

  final is1 = 1 / scaling[0];
  final is2 = 1 / scaling[1];
  final is3 = 1 / scaling[2];

  final sm11 = mat[0] * is1;
  final sm12 = mat[1] * is2;
  final sm13 = mat[2] * is3;
  final sm21 = mat[4] * is1;
  final sm22 = mat[5] * is2;
  final sm23 = mat[6] * is3;
  final sm31 = mat[8] * is1;
  final sm32 = mat[9] * is2;
  final sm33 = mat[10] * is3;

  final trace = sm11 + sm22 + sm33;
  double S = 0;

  if (trace > 0) {
    S = math.sqrt(trace + 1.0) * 2;
    out[3] = 0.25 * S;
    out[0] = (sm23 - sm32) / S;
    out[1] = (sm31 - sm13) / S;
    out[2] = (sm12 - sm21) / S;
  } else if (sm11 > sm22 && sm11 > sm33) {
    S = math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
    out[3] = (sm23 - sm32) / S;
    out[0] = 0.25 * S;
    out[1] = (sm12 + sm21) / S;
    out[2] = (sm31 + sm13) / S;
  } else if (sm22 > sm33) {
    S = math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
    out[3] = (sm31 - sm13) / S;
    out[0] = (sm12 + sm21) / S;
    out[1] = 0.25 * S;
    out[2] = (sm23 + sm32) / S;
  } else {
    S = math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
    out[3] = (sm12 - sm21) / S;
    out[0] = (sm31 + sm13) / S;
    out[1] = (sm23 + sm32) / S;
    out[2] = 0.25 * S;
  }

  return out;
}