toAxisAngle method

AxisAngle toAxisAngle(
  1. Vec3? targetAxis
)

Converts the quaternion to axis, angle representation. @param targetAxis A vector object to reuse for storing the axis. @return An array, first element is the axis and the second is the angle in radians.

Implementation

AxisAngle toAxisAngle(Vec3? targetAxis){
  targetAxis ??= Vec3();
  normalize(); // if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
  final angle = 2 * math.acos(w);
  final s = math.sqrt(1 - w * w); // assuming quaternion normalised then w is less than 1, so term always positive.
  if (s < 0.001) {
    // test to avoid divide by zero, s is always positive due to sqrt
    // if s close to zero then direction of axis not important
    targetAxis.x = x; // if it is important that axis is normalised then replace with x=1; y=z=0;
    targetAxis.y = y;
    targetAxis.z = z;
  } else {
    targetAxis.x = x / s; // normalise axis
    targetAxis.y = y / s;
    targetAxis.z = z / s;
  }
  return AxisAngle(targetAxis, angle);
}