Flutter Impeller
quaternion.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "quaternion.h"
6 #include <sstream>
7 
8 namespace impeller {
9 
10 Quaternion Quaternion::Slerp(const Quaternion& to, double time) const {
11  double cosine = Dot(to);
12  if (fabs(cosine) < 1.0 - 1e-3 /* epsilon */) {
13  /*
14  * Spherical Interpolation.
15  */
16  auto sine = sqrt(1.0 - cosine * cosine);
17  auto angle = atan2(sine, cosine);
18  auto sineInverse = 1.0 / sine;
19  auto c0 = sin((1.0 - time) * angle) * sineInverse;
20  auto c1 = sin(time * angle) * sineInverse;
21  return *this * c0 + to * c1;
22  } else {
23  /*
24  * Linear Interpolation.
25  */
26  return (*this * (1.0 - time) + to * time).Normalize();
27  }
28 }
29 
30 } // namespace impeller
Quaternion Slerp(const Quaternion &to, double time) const
Definition: quaternion.cc:10
Quaternion Normalize() const
Definition: quaternion.h:44
Scalar Dot(const Quaternion &q) const
Definition: quaternion.h:38