aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/quat_slerp.cpp
blob: bffb3bf11bc842616c976477ab18f38dd4e60fe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

#include <iostream>
#include <Eigen/Geometry>
#include <bench/BenchTimer.h>
using namespace Eigen;
using namespace std;



template<typename Q>
EIGEN_DONT_INLINE Q nlerp(const Q& a, const Q& b, typename Q::Scalar t)
{
  return Q((a.coeffs() * (1.0-t) + b.coeffs() * t).normalized());
}

template<typename Q>
EIGEN_DONT_INLINE Q slerp_eigen(const Q& a, const Q& b, typename Q::Scalar t)
{
  return a.slerp(t,b);
}

template<typename Q>
EIGEN_DONT_INLINE Q slerp_legacy(const Q& a, const Q& b, typename Q::Scalar t)
{
  typedef typename Q::Scalar Scalar;
  static const Scalar one = Scalar(1) - dummy_precision<Scalar>();
  Scalar d = a.dot(b);
  Scalar absD = internal::abs(d);
  if (absD>=one)
    return a;

  // theta is the angle between the 2 quaternions
  Scalar theta = std::acos(absD);
  Scalar sinTheta = internal::sin(theta);

  Scalar scale0 = internal::sin( ( Scalar(1) - t ) * theta) / sinTheta;
  Scalar scale1 = internal::sin( ( t * theta) ) / sinTheta;
  if (d<0)
    scale1 = -scale1;

  return Q(scale0 * a.coeffs() + scale1 * b.coeffs());
}

template<typename Q>
EIGEN_DONT_INLINE Q slerp_legacy_nlerp(const Q& a, const Q& b, typename Q::Scalar t)
{
  typedef typename Q::Scalar Scalar;
  static const Scalar one = Scalar(1) - epsilon<Scalar>();
  Scalar d = a.dot(b);
  Scalar absD = internal::abs(d);
  
  Scalar scale0;
  Scalar scale1;
  
  if (absD>=one)
  {
    scale0 = Scalar(1) - t;
    scale1 = t;
  }
  else
  {
    // theta is the angle between the 2 quaternions
    Scalar theta = std::acos(absD);
    Scalar sinTheta = internal::sin(theta);

    scale0 = internal::sin( ( Scalar(1) - t ) * theta) / sinTheta;
    scale1 = internal::sin( ( t * theta) ) / sinTheta;
    if (d<0)
      scale1 = -scale1;
  }

  return Q(scale0 * a.coeffs() + scale1 * b.coeffs());
}

template<typename T>
inline T sin_over_x(T x)
{
  if (T(1) + x*x == T(1))
    return T(1);
  else
    return std::sin(x)/x;
}

template<typename Q>
EIGEN_DONT_INLINE Q slerp_rw(const Q& a, const Q& b, typename Q::Scalar t)
{
  typedef typename Q::Scalar Scalar;
  
  Scalar d = a.dot(b);
  Scalar theta;
  if (d<0.0)
    theta = /*M_PI -*/ Scalar(2)*std::asin( (a.coeffs()+b.coeffs()).norm()/2 );
  else
    theta = Scalar(2)*std::asin( (a.coeffs()-b.coeffs()).norm()/2 );
  
  // theta is the angle between the 2 quaternions
//   Scalar theta = std::acos(absD);
  Scalar sinOverTheta = sin_over_x(theta);

  Scalar scale0 = (Scalar(1)-t)*sin_over_x( ( Scalar(1) - t ) * theta) / sinOverTheta;
  Scalar scale1 = t * sin_over_x( ( t * theta) ) / sinOverTheta;
  if (d<0)
    scale1 = -scale1;

  return Quaternion<Scalar>(scale0 * a.coeffs() + scale1 * b.coeffs());
}

template<typename Q>
EIGEN_DONT_INLINE Q slerp_gael(const Q& a, const Q& b, typename Q::Scalar t)
{
  typedef typename Q::Scalar Scalar;
  
  Scalar d = a.dot(b);
  Scalar theta;
//   theta = Scalar(2) * atan2((a.coeffs()-b.coeffs()).norm(),(a.coeffs()+b.coeffs()).norm());
//   if (d<0.0)
//     theta = M_PI-theta;
  
  if (d<0.0)
    theta = /*M_PI -*/ Scalar(2)*std::asin( (-a.coeffs()-b.coeffs()).norm()/2 );
  else
    theta = Scalar(2)*std::asin( (a.coeffs()-b.coeffs()).norm()/2 );
  
  
  Scalar scale0;
  Scalar scale1;
  if(theta*theta-Scalar(6)==-Scalar(6))
  {
    scale0 = Scalar(1) - t;
    scale1 = t;
  }
  else
  {
    Scalar sinTheta = std::sin(theta);
    scale0 = internal::sin( ( Scalar(1) - t ) * theta) / sinTheta;
    scale1 = internal::sin( ( t * theta) ) / sinTheta;
    if (d<0)
      scale1 = -scale1;
  }

  return Quaternion<Scalar>(scale0 * a.coeffs() + scale1 * b.coeffs());
}

int main()
{
  typedef double RefScalar;
  typedef float TestScalar;
  
  typedef Quaternion<RefScalar>  Qd;
  typedef Quaternion<TestScalar> Qf;
  
  unsigned int g_seed = (unsigned int) time(NULL);
  std::cout << g_seed << "\n";
//   g_seed = 1259932496;
  srand(g_seed);
  
  Matrix<RefScalar,Dynamic,1> maxerr(7);
  maxerr.setZero();
  
  Matrix<RefScalar,Dynamic,1> avgerr(7);
  avgerr.setZero();
  
  cout << "double=>float=>double       nlerp        eigen        legacy(snap)         legacy(nlerp)        rightway         gael's criteria\n";
  
  int rep = 100;
  int iters = 40;
  for (int w=0; w<rep; ++w)
  {
    Qf a, b;
    a.coeffs().setRandom();
    a.normalize();
    b.coeffs().setRandom();
    b.normalize();
    
    Qf c[6];
    
    Qd ar(a.cast<RefScalar>());
    Qd br(b.cast<RefScalar>());
    Qd cr;
    
    
    
    cout.precision(8);
    cout << std::scientific;
    for (int i=0; i<iters; ++i)
    {
      RefScalar t = 0.65;
      cr = slerp_rw(ar,br,t);
      
      Qf refc = cr.cast<TestScalar>();
      c[0] = nlerp(a,b,t);
      c[1] = slerp_eigen(a,b,t);
      c[2] = slerp_legacy(a,b,t);
      c[3] = slerp_legacy_nlerp(a,b,t);
      c[4] = slerp_rw(a,b,t);
      c[5] = slerp_gael(a,b,t);
      
      VectorXd err(7);
      err[0] = (cr.coeffs()-refc.cast<RefScalar>().coeffs()).norm();
//       std::cout << err[0] << "    ";
      for (int k=0; k<6; ++k)
      {
        err[k+1] = (c[k].coeffs()-refc.coeffs()).norm();
//         std::cout << err[k+1] << "    ";
      }
      maxerr = maxerr.cwise().max(err);
      avgerr += err;
//       std::cout << "\n";
      b = cr.cast<TestScalar>();
      br = cr;
    }
//     std::cout << "\n";
  }
  avgerr /= RefScalar(rep*iters);
  cout << "\n\nAccuracy:\n"
       << "  max: " << maxerr.transpose() << "\n";
  cout << "  avg: " << avgerr.transpose() << "\n";
  
  // perf bench
  Quaternionf a,b;
  a.coeffs().setRandom();
  a.normalize();
  b.coeffs().setRandom();
  b.normalize();
  //b = a;
  float s = 0.65;
    
  #define BENCH(FUNC) {\
    BenchTimer t; \
    for(int k=0; k<2; ++k) {\
      t.start(); \
      for(int i=0; i<1000000; ++i) \
        FUNC(a,b,s); \
      t.stop(); \
    } \
    cout << "  " << #FUNC << " => \t " << t.value() << "s\n"; \
  }
  
  cout << "\nSpeed:\n" << std::fixed;
  BENCH(nlerp);
  BENCH(slerp_eigen);
  BENCH(slerp_legacy);
  BENCH(slerp_legacy_nlerp);
  BENCH(slerp_rw);
  BENCH(slerp_gael);
}