slerp method

Quaternion slerp(
  1. Quaternion to,
  2. double weight
)

Implementation

Quaternion slerp(Quaternion to, double weight) {
  double cosine = dot(to);
  if (cosine.abs() < 1.0 - 1e-3 /* epsilon */) {
    // Spherical interpolation.
    double sine = sqrt(1.0 - cosine * cosine);
    double angle = atan2(sine, cosine);
    double sineInverse = 1.0 / sine;
    double c0 = sin((1.0 - weight) * angle) * sineInverse;
    double c1 = sin(weight * angle) * sineInverse;
    return scaled(c0) + to.scaled(c1);
  } else {
    // Linear interpolation.
    return (scaled(1.0 - weight) + to.scaled(weight)).normalized();
  }
}