setFromRotationMatrix method

Quaternion setFromRotationMatrix(
  1. dynamic m
)

Implementation

Quaternion setFromRotationMatrix(m) {
  // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm

  // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)

  final te = m.elements;
  double m11 = te[0];
  double m12 = te[4];
  double m13 = te[8];
  double m21 = te[1];
  double m22 = te[5];
  double m23 = te[9];
  double m31 = te[2];
  double m32 = te[6];
  double m33 = te[10];
  double trace = m11 + m22 + m33;

  if (trace > 0) {
    var s = 0.5 / Math.sqrt(trace + 1.0);

    _w = 0.25 / s;
    _x = (m32 - m23) * s;
    _y = (m13 - m31) * s;
    _z = (m21 - m12) * s;
  } else if (m11 > m22 && m11 > m33) {
    var s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);

    _w = (m32 - m23) / s;
    _x = 0.25 * s;
    _y = (m12 + m21) / s;
    _z = (m13 + m31) / s;
  } else if (m22 > m33) {
    var s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);

    _w = (m13 - m31) / s;
    _x = (m12 + m21) / s;
    _y = 0.25 * s;
    _z = (m23 + m32) / s;
  } else {
    var s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);

    _w = (m21 - m12) / s;
    _x = (m13 + m31) / s;
    _y = (m23 + m32) / s;
    _z = 0.25 * s;
  }

  onChangeCallback();

  return this;
}