toEuler method

void toEuler(
  1. Vec3 target, [
  2. Order order = Order.xyz
])

Convert the quaternion to euler angle representation. Order: YZX, as this page describes: https://www.euclideanspace.com/maths/standards/index.htm @param order Three-character string, defaults to "YZX"

Implementation

void toEuler(Vec3 target, [Order order = Order.xyz]) {
  double? heading;
  late double attitude;
  late double bank;

  switch (order) {
    case Order.xyz:
      final test = x * y + z * w;
      if (test > 0.499) {
        // singularity at north pole
        heading = 2 * math.atan2(x, w);
        attitude = math.pi / 2;
        bank = 0;
      }
      if (test < -0.499) {
        // singularity at south pole
        heading = -2 * math.atan2(x, w);
        attitude = -math.pi / 2;
        bank = 0;
      }
      if (heading == null) {
        final sqx = x * x;
        final sqy = y * y;
        final sqz = z * z;
        heading = math.atan2(2 * y * w - 2 * x * z, 1 - 2 * sqy - 2 * sqz); // Heading
        attitude = math.asin(2 * test); // attitude
        bank = math.atan2(2 * x * w - 2 * y * z, 1 - 2 * sqx - 2 * sqz); // bank
      }
      break;
    default:
      throw('Euler order $order not supported yet.');
  }

  target.y = heading;
  target.z = attitude;
  target.x = bank;
}