/
lie_group_base.h
721 lines (614 loc) · 18.9 KB
/
lie_group_base.h
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#ifndef _MANIF_MANIF_LIE_GROUP_BASE_H_
#define _MANIF_MANIF_LIE_GROUP_BASE_H_
#include "manif/impl/macro.h"
#include "manif/impl/traits.h"
#include "manif/impl/eigen.h"
#include "manif/impl/tangent_base.h"
#include "manif/impl/assignment_assert.h"
#include "manif/impl/cast.h"
#include "manif/constants.h"
#include <tl/optional.hpp>
namespace manif {
/**
* @brief Base class for Lie groups.
* Defines the minimum common API.
* @see TangentBase.
*/
template <class _Derived>
struct LieGroupBase
{
static constexpr int Dim = internal::traits<_Derived>::Dim;
static constexpr int DoF = internal::traits<_Derived>::DoF;
static constexpr int RepSize = internal::traits<_Derived>::RepSize;
using Scalar = typename internal::traits<_Derived>::Scalar;
using LieGroup = typename internal::traits<_Derived>::LieGroup;
using DataType = typename internal::traits<_Derived>::DataType;
using Tangent = typename internal::traits<_Derived>::Tangent;
using Jacobian = typename internal::traits<_Derived>::Jacobian;
using Vector = typename internal::traits<_Derived>::Vector;
using OptJacobianRef = tl::optional<Eigen::Ref<Jacobian>>;
template <typename _Scalar>
using LieGroupTemplate = typename internal::traitscast<LieGroup, _Scalar>::cast;
public:
//! @brief Helper for skipping an optional parameter.
static const OptJacobianRef _;
protected:
MANIF_DEFAULT_CONSTRUCTOR(LieGroupBase)
public:
/**
* @brief Assignment operator.
* @param[in] An element of the Lie group.
* @return A reference to this.
* @note This is a special case of the templated operator=. Its purpose is to
* prevent a default operator= from hiding the templated operator=.
*/
_Derived& operator =(const LieGroupBase& m);
/**
* @brief Assignment operator.
* @param[in] An element of the Lie group.
* @return A reference to this.
*/
template <typename _DerivedOther>
_Derived& operator =(const LieGroupBase<_DerivedOther>& m);
/**
* @brief Assignment operator given Eigen object.
* @param[in] An element of the Lie group.
* @return A reference to this.
*/
template <typename _EigenDerived>
_Derived& operator =(const Eigen::MatrixBase<_EigenDerived>& data);
//! @brief Access the underlying data by const reference
DataType& coeffs();
//! @brief Access the underlying data by const reference
const DataType& coeffs() const;
//! @brief Access the underlying data by pointer
Scalar* data();
//! @brief Access the underlying data by const pointer
const Scalar* data() const;
//! @brief Cast the LieGroup object to a copy
//! of a different scalar type
template <class _NewScalar>
LieGroupTemplate<_NewScalar> cast() const;
/// @todo 'cast' across groups
/// SO3 so3 = so2.as<SO3>()
// template <class _DerivedOther>
// LieGroupTemplate<_DerivedOther> as() const;
/**
* @brief Set the LieGroup object this to Identity.
* @return A reference to this.
* @see Eq. (2).
*/
_Derived& setIdentity();
/**
* @brief Set the LieGroup object this to a random value.
* @return A reference to this.
* @note Randomization happens in the tangent space so that
* M = Log(tau.random)
*/
_Derived& setRandom();
// Minimum API
// Those functions must be implemented in the Derived class !
/**
* @brief Get the inverse of the LieGroup object this.
* @param[out] -optional- J_m_t Jacobian of the inverse wrt this.
* @return The Inverse of this.
* @note See Eq. (3).
* @see TangentBase.
*/
LieGroup inverse(OptJacobianRef J_m_t = {}) const;
/**
* @brief Get the corresponding Lie algebra element in vector form.
* @param[out] -optional- J_t_m Jacobian of the tangent wrt this.
* @return The tangent element in vector form.
* @note This is the log() map in vector form.
* @see Eq. (24).
*/
Tangent log(OptJacobianRef J_t_m = {}) const;
/**
* @brief This function is deprecated.
* Please considere using
* @ref log instead.
*/
MANIF_DEPRECATED
Tangent lift(OptJacobianRef J_t_m = {}) const;
/**
* @brief Composition of this and another element of the same Lie group.
* @param[in] m Another element of the same Lie group.
* @param[out] -optional- J_mc_ma Jacobian of the composition wrt this.
* @param[out] -optional- J_mc_mb Jacobian of the composition wrt m.
* @return The composition of 'this . m'.
* @note See Eqs. (1,2,3,4).
*/
template <typename _DerivedOther>
LieGroup compose(const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_mc_ma = {},
OptJacobianRef J_mc_mb = {}) const;
/**
* @brief Get the action of the Lie group object on a point.
* @param[in] v A point.
* @param[out] -optional- J_vout_m Jacobian of the new object wrt this.
* @param[out] -optional- J_vout_v Jacobian of the new object wrt input object.
* @return A point acted upon by the object.
*/
template <typename _EigenDerived>
Vector act(const Eigen::MatrixBase<_EigenDerived>& v,
tl::optional<Eigen::Ref<Eigen::Matrix<Scalar, Dim, DoF>>> J_vout_m = {},
tl::optional<Eigen::Ref<Eigen::Matrix<Scalar, Dim, Dim>>> J_vout_v = {}) const;
/**
* @brief Get the Adjoint of the Lie group element this.
* @note See Eq. (29).
*/
Jacobian adj() const;
// Deduced API
/**
* @brief Right oplus operation of the Lie group.
* @param[in] t An element of the tangent of the Lie group.
* @param[out] -optional- J_mout_m Jacobian of the oplus operation wrt this.
* @param[out] -optional- J_mout_t Jacobian of the oplus operation wrt the tangent element.
* @return An element of the Lie group.
* @note See Eq. (25).
*/
template <typename _DerivedOther>
LieGroup rplus(const TangentBase<_DerivedOther>& t,
OptJacobianRef J_mout_m = {},
OptJacobianRef J_mout_t = {}) const;
/**
* @brief Left oplus operation of the Lie group.
* @param[in] t An element of the tangent of the Lie group.
* @param[out] -optional- J_mout_m Jacobian of the oplus operation wrt this.
* @param[out] -optional- J_mout_t Jacobian of the oplus operation wrt the tangent element.
* @return An element of the Lie group.
* @note See Eq. (27).
*/
template <typename _DerivedOther>
LieGroup lplus(const TangentBase<_DerivedOther>& t,
OptJacobianRef J_mout_m = {},
OptJacobianRef J_mout_t = {}) const;
/**
* @brief An alias for the right oplus operation.
* @see rplus
*/
template <typename _DerivedOther>
LieGroup plus(const TangentBase<_DerivedOther>& t,
OptJacobianRef J_mout_m = {},
OptJacobianRef J_mout_t = {}) const;
/**
* @brief Right ominus operation of the Lie group.
* @param[in] m Another element of the same Lie group.
* @param[out] -optional- J_t_ma Jacobian of the ominus operation wrt this.
* @param[out] -optional- J_t_mb Jacobian of the ominus operation wrt the other element.
* @return An element of the tangent space of the Lie group.
* @note See Eq. (26).
*/
template <typename _DerivedOther>
Tangent rminus(const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_t_ma = {},
OptJacobianRef J_t_mb = {}) const;
/**
* @brief Left ominus operation of the Lie group.
* @param[in] m Another element of the same Lie group.
* @param[out] -optional- J_t_ma Jacobian of the ominus operation wrt this.
* @param[out] -optional- J_t_mb Jacobian of the ominus operation wrt the other element.
* @return An element of the tangent space of the Lie group.
* @note See Eq. (28).
*/
template <typename _DerivedOther>
Tangent lminus(const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_t_ma = {},
OptJacobianRef J_t_mb = {}) const;
/**
* @brief An alias for the right ominus operation.
* @see rminus
*/
template <typename _DerivedOther>
Tangent minus(const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_t_ma = {},
OptJacobianRef J_t_mb = {}) const;
/**
* @brief
* @param[in] m [description]
* @param[out] -optional- J_mc_ma [description]
* @param[out] -optional- J_mc_mb [description]
* @return [description]
*/
template <typename _DerivedOther>
LieGroup between(const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_mc_ma = {},
OptJacobianRef J_mc_mb = {}) const;
/**
* @brief Evaluate whether this and m are 'close'.
* @param[in] m An element of the same Lie Group.
* @param[in] eps Threshold for equality comparison.
* @return true if the Lie group element m is 'close' to this,
* false otherwise.
* @see TangentBase::isApprox
*/
template <typename _DerivedOther>
bool isApprox(const LieGroupBase<_DerivedOther>& m,
const Scalar eps = Constants<Scalar>::eps) const;
// Some operators
/**
* @brief Equality operator.
* @param[in] An element of the same Lie group.
* @return true if the Lie group element m is 'close' to this,
* false otherwise.
* @see isApprox.
*/
template <typename _DerivedOther>
bool operator ==(const LieGroupBase<_DerivedOther>& m) const;
/**
* @brief Right oplus operator.
* @see rplus.
*/
template <typename _DerivedOther>
LieGroup operator +(const TangentBase<_DerivedOther>& t) const;
/**
* @brief Right in-place oplus operator.
* @see rplus.
*/
template <typename _DerivedOther>
_Derived& operator +=(const TangentBase<_DerivedOther>& t);
/**
* @brief Right ominus operator.
* @see rminus.
*/
template <typename _DerivedOther>
Tangent operator -(const LieGroupBase<_DerivedOther>& m) const;
/**
* @brief Lie group composition operator.
* @see compose.
*/
template <typename _DerivedOther>
LieGroup operator *(const LieGroupBase<_DerivedOther>& m) const;
/**
* @brief Lie group in-place composition operator.
* @see compose.
*/
template <typename _DerivedOther>
_Derived& operator *=(const LieGroupBase<_DerivedOther>& m);
//! Access the ith coeffs
auto operator [](const unsigned int i) const -> decltype(coeffs()[i]){
return coeffs()[i];
}
//! Access the ith coeffs
auto operator [](const unsigned int i) -> decltype(coeffs()[i]){
return coeffs()[i];
}
//! @brief The size of the underlying vector
constexpr unsigned int size() const {
return RepSize;
}
// Some static helpers
//! Static helper to create a Lie group object set at Identity.
static LieGroup Identity();
//! Static helper to create a random object of the Lie group.
static LieGroup Random();
protected:
inline _Derived& derived() & noexcept { return *static_cast< _Derived* >(this); }
inline const _Derived& derived() const & noexcept { return *static_cast< const _Derived* >(this); }
};
template <typename _Derived>
constexpr int LieGroupBase<_Derived>::Dim;
template <typename _Derived>
constexpr int LieGroupBase<_Derived>::DoF;
template <typename _Derived>
constexpr int LieGroupBase<_Derived>::RepSize;
template <typename _Derived>
const typename LieGroupBase<_Derived>::OptJacobianRef
LieGroupBase<_Derived>::_ = {};
// Copy
template <typename _Derived>
_Derived&
LieGroupBase<_Derived>::operator =(const LieGroupBase& m)
{
derived().coeffs() = m.coeffs();
return derived();
}
template <typename _Derived>
template <typename _DerivedOther>
_Derived&
LieGroupBase<_Derived>::operator =(const LieGroupBase<_DerivedOther>& m)
{
derived().coeffs() = m.coeffs();
return derived();
}
template <typename _Derived>
template <typename _EigenDerived>
_Derived&
LieGroupBase<_Derived>::operator =(const Eigen::MatrixBase<_EigenDerived>& data)
{
internal::AssignmentEvaluator<
typename internal::traits<_Derived>::Base>().run(data);
derived().coeffs() = data;
return derived();
}
template <typename _Derived>
typename LieGroupBase<_Derived>::DataType&
LieGroupBase<_Derived>::coeffs()
{
return derived().coeffs();
}
template <typename _Derived>
const typename LieGroupBase<_Derived>::DataType&
LieGroupBase<_Derived>::coeffs() const
{
return derived().coeffs();
}
template <typename _Derived>
typename LieGroupBase<_Derived>::Scalar*
LieGroupBase<_Derived>::data()
{
return derived().coeffs().data();
}
template <typename _Derived>
const typename LieGroupBase<_Derived>::Scalar*
LieGroupBase<_Derived>::data() const
{
return derived().coeffs().data();
}
template <typename _Derived>
template <class _NewScalar>
typename LieGroupBase<_Derived>::template LieGroupTemplate<_NewScalar>
LieGroupBase<_Derived>::cast() const
{
return internal::CastEvaluator<
typename internal::traits<_Derived>::Base, _NewScalar
>(derived()).run();
}
template <typename _Derived>
_Derived&
LieGroupBase<_Derived>::setIdentity()
{
const static Tangent zero = Tangent::Zero();
derived() = zero.exp();
return derived();
}
template <typename _Derived>
_Derived&
LieGroupBase<_Derived>::setRandom()
{
internal::RandomEvaluator<
typename internal::traits<_Derived>::Base>(
derived()).run();
return derived();
}
template <typename _Derived>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::inverse(OptJacobianRef J_m_t) const
{
return derived().inverse(J_m_t);
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::rplus(
const TangentBase<_DerivedOther>& t,
OptJacobianRef J_mout_m,
OptJacobianRef J_mout_t) const
{
if (J_mout_t)
{
(*J_mout_t) = t.rjac();
}
return compose(t.exp(), J_mout_m, _);
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::lplus(
const TangentBase<_DerivedOther>& t,
OptJacobianRef J_mout_m,
OptJacobianRef J_mout_t) const
{
if (J_mout_t)
{
J_mout_t->noalias() = inverse().adj() * t.rjac();
}
if (J_mout_m)
{
J_mout_m->setIdentity();
}
return t.exp().compose(derived());
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::plus(
const TangentBase<_DerivedOther>& t,
OptJacobianRef J_mout_m,
OptJacobianRef J_mout_t) const
{
return derived().rplus(t, J_mout_m, J_mout_t);
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::Tangent
LieGroupBase<_Derived>::rminus(
const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_t_ma,
OptJacobianRef J_t_mb) const
{
const Tangent t = m.inverse().compose(derived()).log();
if (J_t_ma)
{
(*J_t_ma) = t.rjacinv();
}
if (J_t_mb)
{
(*J_t_mb) = -(-t).rjacinv();
}
return t;
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::Tangent
LieGroupBase<_Derived>::lminus(
const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_t_ma,
OptJacobianRef J_t_mb) const
{
const Tangent t = compose(m.inverse()).log();
if (J_t_ma)
{
J_t_ma->noalias() = t.rjacinv() * m.adj();
if (J_t_mb)
{
*J_t_mb = -(*J_t_ma);
}
}
else if (J_t_mb)
{
J_t_mb->noalias() = -(t.rjacinv() * m.adj());
}
return t;
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::Tangent
LieGroupBase<_Derived>::minus(
const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_t_ma,
OptJacobianRef J_t_mb) const
{
return derived().rminus(m, J_t_ma, J_t_mb);
}
template <typename _Derived>
typename LieGroupBase<_Derived>::Tangent
LieGroupBase<_Derived>::log(OptJacobianRef J_t_m) const
{
return derived().log(J_t_m);
}
template <typename _Derived>
typename LieGroupBase<_Derived>::Tangent
LieGroupBase<_Derived>::lift(OptJacobianRef J_t_m) const
{
return derived().log(J_t_m);
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::compose(
const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_mc_ma,
OptJacobianRef J_mc_mb) const
{
return derived().compose(m, J_mc_ma, J_mc_mb);
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::between(
const LieGroupBase<_DerivedOther>& m,
OptJacobianRef J_mc_ma,
OptJacobianRef J_mc_mb) const
{
const LieGroup mc = inverse().compose(m);
if (J_mc_ma)
{
*J_mc_ma = -(mc.inverse().adj());
}
if (J_mc_mb)
{
J_mc_mb->setIdentity();
}
return mc;
}
template <typename _Derived>
template <typename _DerivedOther>
bool LieGroupBase<_Derived>::isApprox(const LieGroupBase<_DerivedOther>& m,
const Scalar eps) const
{
return rminus(m).isApprox(Tangent::Zero(), eps);
}
template <typename _Derived>
typename LieGroupBase<_Derived>::Jacobian
LieGroupBase<_Derived>::adj() const
{
return derived().adj();
}
template <typename _Derived>
template <typename _EigenDerived>
typename LieGroupBase<_Derived>::Vector
LieGroupBase<_Derived>::act(
const Eigen::MatrixBase<_EigenDerived>& v,
tl::optional<Eigen::Ref<Eigen::Matrix<Scalar, Dim, DoF>>> J_vout_m,
tl::optional<Eigen::Ref<Eigen::Matrix<Scalar, Dim, Dim>>> J_vout_v
) const
{
return derived().act(v, J_vout_m, J_vout_v);
}
// Operators
template <typename _Derived>
template <typename _DerivedOther>
bool LieGroupBase<_Derived>::operator ==(
const LieGroupBase<_DerivedOther>& m) const
{
return isApprox(m);
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::operator +(
const TangentBase<_DerivedOther>& t) const
{
return derived().rplus(t);
}
template <typename _Derived>
template <typename _DerivedOther>
_Derived&
LieGroupBase<_Derived>::operator +=(
const TangentBase<_DerivedOther>& t)
{
derived() = derived().rplus(t);
return derived();
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::Tangent
LieGroupBase<_Derived>::operator -(
const LieGroupBase<_DerivedOther>& m) const
{
return derived().rminus(m);
}
template <typename _Derived>
template <typename _DerivedOther>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::operator *(
const LieGroupBase<_DerivedOther>& m) const
{
return derived().compose(m);
}
template <typename _Derived>
template <typename _DerivedOther>
_Derived&
LieGroupBase<_Derived>::operator *=(
const LieGroupBase<_DerivedOther>& m)
{
derived() = derived().compose(m);
return derived();
}
// Static helpers
template <typename _Derived>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::Identity()
{
const static LieGroup I(LieGroup().setIdentity());
return I;
}
template <typename _Derived>
typename LieGroupBase<_Derived>::LieGroup
LieGroupBase<_Derived>::Random()
{
return LieGroup().setRandom();
}
// Utils
template <typename _Stream, typename _Derived>
_Stream& operator << (
_Stream& s,
const manif::LieGroupBase<_Derived>& m)
{
s << m.coeffs().transpose();
return s;
}
} /* namespace manif */
#endif /* _MANIF_MANIF_LIE_GROUP_BASE_H_ */