LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / eigen / bench / quat_slerp.cpp
blobbffb3bf11bc842616c976477ab18f38dd4e60fe6
2 #include <iostream>
3 #include <Eigen/Geometry>
4 #include <bench/BenchTimer.h>
5 using namespace Eigen;
6 using namespace std;
10 template<typename Q>
11 EIGEN_DONT_INLINE Q nlerp(const Q& a, const Q& b, typename Q::Scalar t)
13 return Q((a.coeffs() * (1.0-t) + b.coeffs() * t).normalized());
16 template<typename Q>
17 EIGEN_DONT_INLINE Q slerp_eigen(const Q& a, const Q& b, typename Q::Scalar t)
19 return a.slerp(t,b);
22 template<typename Q>
23 EIGEN_DONT_INLINE Q slerp_legacy(const Q& a, const Q& b, typename Q::Scalar t)
25 typedef typename Q::Scalar Scalar;
26 static const Scalar one = Scalar(1) - dummy_precision<Scalar>();
27 Scalar d = a.dot(b);
28 Scalar absD = internal::abs(d);
29 if (absD>=one)
30 return a;
32 // theta is the angle between the 2 quaternions
33 Scalar theta = std::acos(absD);
34 Scalar sinTheta = internal::sin(theta);
36 Scalar scale0 = internal::sin( ( Scalar(1) - t ) * theta) / sinTheta;
37 Scalar scale1 = internal::sin( ( t * theta) ) / sinTheta;
38 if (d<0)
39 scale1 = -scale1;
41 return Q(scale0 * a.coeffs() + scale1 * b.coeffs());
44 template<typename Q>
45 EIGEN_DONT_INLINE Q slerp_legacy_nlerp(const Q& a, const Q& b, typename Q::Scalar t)
47 typedef typename Q::Scalar Scalar;
48 static const Scalar one = Scalar(1) - epsilon<Scalar>();
49 Scalar d = a.dot(b);
50 Scalar absD = internal::abs(d);
52 Scalar scale0;
53 Scalar scale1;
55 if (absD>=one)
57 scale0 = Scalar(1) - t;
58 scale1 = t;
60 else
62 // theta is the angle between the 2 quaternions
63 Scalar theta = std::acos(absD);
64 Scalar sinTheta = internal::sin(theta);
66 scale0 = internal::sin( ( Scalar(1) - t ) * theta) / sinTheta;
67 scale1 = internal::sin( ( t * theta) ) / sinTheta;
68 if (d<0)
69 scale1 = -scale1;
72 return Q(scale0 * a.coeffs() + scale1 * b.coeffs());
75 template<typename T>
76 inline T sin_over_x(T x)
78 if (T(1) + x*x == T(1))
79 return T(1);
80 else
81 return std::sin(x)/x;
84 template<typename Q>
85 EIGEN_DONT_INLINE Q slerp_rw(const Q& a, const Q& b, typename Q::Scalar t)
87 typedef typename Q::Scalar Scalar;
89 Scalar d = a.dot(b);
90 Scalar theta;
91 if (d<0.0)
92 theta = /*M_PI -*/ Scalar(2)*std::asin( (a.coeffs()+b.coeffs()).norm()/2 );
93 else
94 theta = Scalar(2)*std::asin( (a.coeffs()-b.coeffs()).norm()/2 );
96 // theta is the angle between the 2 quaternions
97 // Scalar theta = std::acos(absD);
98 Scalar sinOverTheta = sin_over_x(theta);
100 Scalar scale0 = (Scalar(1)-t)*sin_over_x( ( Scalar(1) - t ) * theta) / sinOverTheta;
101 Scalar scale1 = t * sin_over_x( ( t * theta) ) / sinOverTheta;
102 if (d<0)
103 scale1 = -scale1;
105 return Quaternion<Scalar>(scale0 * a.coeffs() + scale1 * b.coeffs());
108 template<typename Q>
109 EIGEN_DONT_INLINE Q slerp_gael(const Q& a, const Q& b, typename Q::Scalar t)
111 typedef typename Q::Scalar Scalar;
113 Scalar d = a.dot(b);
114 Scalar theta;
115 // theta = Scalar(2) * atan2((a.coeffs()-b.coeffs()).norm(),(a.coeffs()+b.coeffs()).norm());
116 // if (d<0.0)
117 // theta = M_PI-theta;
119 if (d<0.0)
120 theta = /*M_PI -*/ Scalar(2)*std::asin( (-a.coeffs()-b.coeffs()).norm()/2 );
121 else
122 theta = Scalar(2)*std::asin( (a.coeffs()-b.coeffs()).norm()/2 );
125 Scalar scale0;
126 Scalar scale1;
127 if(theta*theta-Scalar(6)==-Scalar(6))
129 scale0 = Scalar(1) - t;
130 scale1 = t;
132 else
134 Scalar sinTheta = std::sin(theta);
135 scale0 = internal::sin( ( Scalar(1) - t ) * theta) / sinTheta;
136 scale1 = internal::sin( ( t * theta) ) / sinTheta;
137 if (d<0)
138 scale1 = -scale1;
141 return Quaternion<Scalar>(scale0 * a.coeffs() + scale1 * b.coeffs());
144 int main()
146 typedef double RefScalar;
147 typedef float TestScalar;
149 typedef Quaternion<RefScalar> Qd;
150 typedef Quaternion<TestScalar> Qf;
152 unsigned int g_seed = (unsigned int) time(NULL);
153 std::cout << g_seed << "\n";
154 // g_seed = 1259932496;
155 srand(g_seed);
157 Matrix<RefScalar,Dynamic,1> maxerr(7);
158 maxerr.setZero();
160 Matrix<RefScalar,Dynamic,1> avgerr(7);
161 avgerr.setZero();
163 cout << "double=>float=>double nlerp eigen legacy(snap) legacy(nlerp) rightway gael's criteria\n";
165 int rep = 100;
166 int iters = 40;
167 for (int w=0; w<rep; ++w)
169 Qf a, b;
170 a.coeffs().setRandom();
171 a.normalize();
172 b.coeffs().setRandom();
173 b.normalize();
175 Qf c[6];
177 Qd ar(a.cast<RefScalar>());
178 Qd br(b.cast<RefScalar>());
179 Qd cr;
183 cout.precision(8);
184 cout << std::scientific;
185 for (int i=0; i<iters; ++i)
187 RefScalar t = 0.65;
188 cr = slerp_rw(ar,br,t);
190 Qf refc = cr.cast<TestScalar>();
191 c[0] = nlerp(a,b,t);
192 c[1] = slerp_eigen(a,b,t);
193 c[2] = slerp_legacy(a,b,t);
194 c[3] = slerp_legacy_nlerp(a,b,t);
195 c[4] = slerp_rw(a,b,t);
196 c[5] = slerp_gael(a,b,t);
198 VectorXd err(7);
199 err[0] = (cr.coeffs()-refc.cast<RefScalar>().coeffs()).norm();
200 // std::cout << err[0] << " ";
201 for (int k=0; k<6; ++k)
203 err[k+1] = (c[k].coeffs()-refc.coeffs()).norm();
204 // std::cout << err[k+1] << " ";
206 maxerr = maxerr.cwise().max(err);
207 avgerr += err;
208 // std::cout << "\n";
209 b = cr.cast<TestScalar>();
210 br = cr;
212 // std::cout << "\n";
214 avgerr /= RefScalar(rep*iters);
215 cout << "\n\nAccuracy:\n"
216 << " max: " << maxerr.transpose() << "\n";
217 cout << " avg: " << avgerr.transpose() << "\n";
219 // perf bench
220 Quaternionf a,b;
221 a.coeffs().setRandom();
222 a.normalize();
223 b.coeffs().setRandom();
224 b.normalize();
225 //b = a;
226 float s = 0.65;
228 #define BENCH(FUNC) {\
229 BenchTimer t; \
230 for(int k=0; k<2; ++k) {\
231 t.start(); \
232 for(int i=0; i<1000000; ++i) \
233 FUNC(a,b,s); \
234 t.stop(); \
236 cout << " " << #FUNC << " => \t " << t.value() << "s\n"; \
239 cout << "\nSpeed:\n" << std::fixed;
240 BENCH(nlerp);
241 BENCH(slerp_eigen);
242 BENCH(slerp_legacy);
243 BENCH(slerp_legacy_nlerp);
244 BENCH(slerp_rw);
245 BENCH(slerp_gael);