File size: 10,422 Bytes
899c526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

#ifndef RxSO3_HEADER
#define RxSO3_HEADER

#include <stdio.h>
#include <Eigen/Dense>
#include <Eigen/Geometry> 

#include "common.h"

template <typename Scalar>
class RxSO3 {
  public:
    const static int constexpr K = 4; // manifold dimension
    const static int constexpr N = 5; // embedding dimension

    using Vector3 = Eigen::Matrix<Scalar,3,1>;
    using Vector4 = Eigen::Matrix<Scalar,4,1>;
    using Matrix3 = Eigen::Matrix<Scalar,3,3>;

    using Tangent = Eigen::Matrix<Scalar,K,1>;
    using Data = Eigen::Matrix<Scalar,N,1>;

    using Point = Eigen::Matrix<Scalar,3,1>;
    using Point4 = Eigen::Matrix<Scalar,4,1>;

    using Quaternion = Eigen::Quaternion<Scalar>;
    using Transformation = Eigen::Matrix<Scalar,3,3>;
    using Adjoint = Eigen::Matrix<Scalar,4,4>;


    EIGEN_DEVICE_FUNC RxSO3(Quaternion const& q, Scalar const s) 
        : unit_quaternion(q), scale(s) {
      unit_quaternion.normalize();
    };

    EIGEN_DEVICE_FUNC RxSO3(const Scalar *data) : unit_quaternion(data), scale(data[4]) {
      unit_quaternion.normalize();
    };

    EIGEN_DEVICE_FUNC RxSO3() {
      unit_quaternion = Quaternion::Identity();
      scale = Scalar(1.0);
    }

    EIGEN_DEVICE_FUNC RxSO3<Scalar> inv() {
      return RxSO3<Scalar>(unit_quaternion.conjugate(), 1.0/scale);
    }

    EIGEN_DEVICE_FUNC Data data() const {
      Data data_vec; data_vec << unit_quaternion.coeffs(), scale;
      return data_vec;
    }

    EIGEN_DEVICE_FUNC RxSO3<Scalar> operator*(RxSO3<Scalar> const& other) {
      return RxSO3<Scalar>(unit_quaternion * other.unit_quaternion, scale * other.scale);
    }

    EIGEN_DEVICE_FUNC Point operator*(Point const& p) const {
      const Quaternion& q = unit_quaternion;
      Point uv = q.vec().cross(p); uv += uv;
      return scale * (p + q.w()*uv + q.vec().cross(uv));
    }

    EIGEN_DEVICE_FUNC Point4 act4(Point4 const& p) const {
      Point4 p1; p1 << this->operator*(p.template segment<3>(0)), p(3);
      return p1;
    }

    EIGEN_DEVICE_FUNC Adjoint Adj() const {
      Adjoint Ad = Adjoint::Identity();
      Ad.template block<3,3>(0,0) = unit_quaternion.toRotationMatrix();
      return Ad;
    }

    EIGEN_DEVICE_FUNC Transformation Matrix() const {
      return scale * unit_quaternion.toRotationMatrix();
    }

    EIGEN_DEVICE_FUNC Eigen::Matrix<Scalar,4,4> Matrix4x4() const {
      Eigen::Matrix<Scalar,4,4> T;
      T = Eigen::Matrix<Scalar,4,4>::Identity();
      T.template block<3,3>(0,0) = Matrix();
      return T;
    }

    EIGEN_DEVICE_FUNC Eigen::Matrix<Scalar,5,5> orthogonal_projector() const {
      // jacobian action on a point
      Eigen::Matrix<Scalar,5,5> J = Eigen::Matrix<Scalar,5,5>::Zero();
      
      J.template block<3,3>(0,0) = 0.5 * (
        unit_quaternion.w() * Matrix3::Identity() + 
        SO3<Scalar>::hat(-unit_quaternion.vec())
      );
      
      J.template block<1,3>(3,0) = 0.5 * (-unit_quaternion.vec());

      // scale
      J(4,3) = scale;

      return J;
    }

    EIGEN_DEVICE_FUNC Transformation Rotation() const {
      return unit_quaternion.toRotationMatrix();
    }

    EIGEN_DEVICE_FUNC Tangent Adj(Tangent const& a) const {
      return Adj() * a;
    }

    EIGEN_DEVICE_FUNC Tangent AdjT(Tangent const& a) const {
      return Adj().transpose() * a;
    }

    EIGEN_DEVICE_FUNC static Transformation hat(Tangent const& phi_sigma) {
      Vector3 const phi = phi_sigma.template segment<3>(0);
      return SO3<Scalar>::hat(phi) + phi(3) * Transformation::Identity();
    }

    EIGEN_DEVICE_FUNC static Adjoint adj(Tangent const& phi_sigma) {
      Vector3 const phi = phi_sigma.template segment<3>(0);
      Matrix3 const Phi = SO3<Scalar>::hat(phi);

      Adjoint ad = Adjoint::Zero();
      ad.template block<3,3>(0,0) = Phi;

      return ad;
    }

    EIGEN_DEVICE_FUNC Tangent Log() const {
      using std::abs;
      using std::atan;
      using std::sqrt;

      Scalar squared_n = unit_quaternion.vec().squaredNorm();
      Scalar w = unit_quaternion.w();
      Scalar two_atan_nbyw_by_n;

      /// Atan-based log thanks to
      ///
      /// C. Hertzberg et al.:
      /// "Integrating Generic Sensor Fusion Algorithms with Sound State
      /// Representation through Encapsulation of Manifolds"
      /// Information Fusion, 2011

      if (squared_n < EPS * EPS) {
        two_atan_nbyw_by_n = Scalar(2) / w - Scalar(2.0/3.0) * (squared_n) / (w * w * w);
      } else {
        Scalar n = sqrt(squared_n);
        if (abs(w) < EPS) {
          if (w > Scalar(0)) {
            two_atan_nbyw_by_n = PI / n;
          } else {
            two_atan_nbyw_by_n = -PI / n;
          }
        } else {
          two_atan_nbyw_by_n = Scalar(2) * atan(n / w) / n;
        }
      }

      Tangent phi_sigma;
      phi_sigma << two_atan_nbyw_by_n * unit_quaternion.vec(), log(scale);

      return phi_sigma;
    }

    EIGEN_DEVICE_FUNC static RxSO3<Scalar> Exp(Tangent const& phi_sigma) {
      Vector3 phi = phi_sigma.template segment<3>(0);
      Scalar scale = exp(phi_sigma(3));

      Scalar theta2 = phi.squaredNorm();
      Scalar theta = sqrt(theta2);
      Scalar imag_factor;
      Scalar real_factor;

      if (theta < EPS) {
        Scalar theta4 = theta2 * theta2;
        imag_factor = Scalar(0.5) - Scalar(1.0/48.0) * theta2 + Scalar(1.0/3840.0) * theta4;
        real_factor = Scalar(1) - Scalar(1.0/8.0) * theta2 + Scalar(1.0/384.0) * theta4;
      } else {
        imag_factor = sin(.5 * theta) / theta;
        real_factor = cos(.5 * theta);
      }

      Quaternion q(real_factor, imag_factor*phi.x(), imag_factor*phi.y(), imag_factor*phi.z());
      return RxSO3<Scalar>(q, scale);
    }
    
    EIGEN_DEVICE_FUNC static Matrix3 calcW(Tangent const& phi_sigma) {
      // left jacobian
      using std::abs;
      Matrix3 const I = Matrix3::Identity();
      Scalar const one(1);
      Scalar const half(0.5);
      
      Vector3 const phi = phi_sigma.template segment<3>(0);
      Scalar const sigma = phi_sigma(3);
      Scalar const theta = phi.norm();

      Matrix3 const Phi = SO3<Scalar>::hat(phi);
      Matrix3 const Phi2 = Phi * Phi;
      Scalar const scale = exp(sigma);

      Scalar A, B, C;
      if (abs(sigma) < EPS) {
        C = one;
        if (abs(theta) < EPS) {
          A = half;
          B = Scalar(1. / 6.);
        } else {
          Scalar theta_sq = theta * theta;
          A = (one - cos(theta)) / theta_sq;
          B = (theta - sin(theta)) / (theta_sq * theta);
        }
      } else {
        C = (scale - one) / sigma;
        if (abs(theta) < EPS) {
          Scalar sigma_sq = sigma * sigma;
          A = ((sigma - one) * scale + one) / sigma_sq;
          B = (scale * half * sigma_sq + scale - one - sigma * scale) /
              (sigma_sq * sigma);
        } else {
          Scalar theta_sq = theta * theta;
          Scalar a = scale * sin(theta);
          Scalar b = scale * cos(theta);
          Scalar c = theta_sq + sigma * sigma;
          A = (a * sigma + (one - b) * theta) / (theta * c);
          B = (C - ((b - one) * sigma + a * theta) / (c)) * one / (theta_sq);
        }
      }
      return A * Phi + B * Phi2 + C * I;
    }

    EIGEN_DEVICE_FUNC static Matrix3 calcWInv(Tangent const& phi_sigma) {
      // left jacobian inverse
      Matrix3 const I = Matrix3::Identity();
      Scalar const half(0.5);
      Scalar const one(1);
      Scalar const two(2);
      
      Vector3 const phi = phi_sigma.template segment<3>(0);
      Scalar const sigma = phi_sigma(3);
      Scalar const theta = phi.norm();
      Scalar const scale = exp(sigma);

      Matrix3 const Phi = SO3<Scalar>::hat(phi);
      Matrix3 const Phi2 = Phi * Phi;
      Scalar const scale_sq = scale * scale;
      Scalar const theta_sq = theta * theta;
      Scalar const sin_theta = sin(theta);
      Scalar const cos_theta = cos(theta);

      Scalar a, b, c;
      if (abs(sigma * sigma) < EPS) {
        c = one - half * sigma;
        a = -half;
        if (abs(theta_sq) < EPS) {
          b = Scalar(1. / 12.);
        } else {
          b = (theta * sin_theta + two * cos_theta - two) /
              (two * theta_sq * (cos_theta - one));
        }
      } else {
        Scalar const scale_cu = scale_sq * scale;
        c = sigma / (scale - one);
        if (abs(theta_sq) < EPS) {
          a = (-sigma * scale + scale - one) / ((scale - one) * (scale - one));
          b = (scale_sq * sigma - two * scale_sq + scale * sigma + two * scale) /
              (two * scale_cu - Scalar(6) * scale_sq + Scalar(6) * scale - two);
        } else {
          Scalar const s_sin_theta = scale * sin_theta;
          Scalar const s_cos_theta = scale * cos_theta;
          a = (theta * s_cos_theta - theta - sigma * s_sin_theta) /
              (theta * (scale_sq - two * s_cos_theta + one));
          b = -scale *
              (theta * s_sin_theta - theta * sin_theta + sigma * s_cos_theta -
              scale * sigma + sigma * cos_theta - sigma) /
              (theta_sq * (scale_cu - two * scale * s_cos_theta - scale_sq +
                          two * s_cos_theta + scale - one));
        }
      }
      return a * Phi + b * Phi2 + c * I;
    }

    EIGEN_DEVICE_FUNC static Adjoint left_jacobian(Tangent const& phi_sigma) {
      // left jacobian
      Adjoint J = Adjoint::Identity();
      Vector3 phi = phi_sigma.template segment<3>(0);
      J.template block<3,3>(0,0) = SO3<Scalar>::left_jacobian(phi);
      return J;
    }

  EIGEN_DEVICE_FUNC static Adjoint left_jacobian_inverse(Tangent const& phi_sigma) {
      // left jacobian inverse
      Adjoint Jinv = Adjoint::Identity();
      Vector3 phi = phi_sigma.template segment<3>(0);
      Jinv.template block<3,3>(0,0) = SO3<Scalar>::left_jacobian_inverse(phi);
      return Jinv;
    }

    EIGEN_DEVICE_FUNC static Eigen::Matrix<Scalar,3,4> act_jacobian(Point const& p) {
      // jacobian action on a point
      Eigen::Matrix<Scalar,3,4> Ja; 
      Ja << SO3<Scalar>::hat(-p), p;
      return Ja;
    }

    EIGEN_DEVICE_FUNC static Eigen::Matrix<Scalar,4,4> act4_jacobian(Point4 const& p) {
      // jacobian action on a point
      Eigen::Matrix<Scalar,4,4> J = Eigen::Matrix<Scalar,4,4>::Zero();
      J.template block<3,3>(0,0) = SO3<Scalar>::hat(-p.template segment<3>(0));
      J.template block<3,1>(0,3) = p.template segment<3>(0); 
      return J;
    }

  private:
    Quaternion unit_quaternion;
    Scalar scale;
};

#endif