-
Notifications
You must be signed in to change notification settings - Fork 918
Expand file tree
/
Copy pathProductLieGroup.h
More file actions
762 lines (667 loc) · 25.3 KB
/
ProductLieGroup.h
File metadata and controls
762 lines (667 loc) · 25.3 KB
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
* @file ProductLieGroup.h
* @date May, 2015
* @author Frank Dellaert
* @brief Group product of two Lie Groups
*/
#pragma once
#include <gtsam/base/Lie.h>
#include <gtsam/base/Testable.h>
#include <array>
#include <iostream>
#include <stdexcept>
#include <string>
#include <utility> // pair
namespace gtsam {
/**
* @brief Template to construct the product Lie group of two other Lie groups
* Assumes Lie group structure for G and H
*/
template <typename G, typename H>
class ProductLieGroup : public std::pair<G, H> {
GTSAM_CONCEPT_ASSERT(IsLieGroup<G>);
GTSAM_CONCEPT_ASSERT(IsLieGroup<H>);
GTSAM_CONCEPT_ASSERT(IsTestable<G>);
GTSAM_CONCEPT_ASSERT(IsTestable<H>);
public:
/// Base pair type
typedef std::pair<G, H> Base;
protected:
/// Dimensions of the two subgroups
inline constexpr static int dimension1 = traits<G>::dimension;
inline constexpr static int dimension2 = traits<H>::dimension;
inline constexpr static bool firstDynamic = dimension1 == Eigen::Dynamic;
inline constexpr static bool secondDynamic = dimension2 == Eigen::Dynamic;
public:
/// Manifold dimension
inline constexpr static int dimension =
firstDynamic || secondDynamic ? Eigen::Dynamic : dimension1 + dimension2;
/// Tangent vector type
using TangentVector = std::conditional_t<dimension == Eigen::Dynamic, Vector,
Eigen::Matrix<double, dimension, 1>>;
/// Chart Jacobian type
using ChartJacobian =
std::conditional_t<dimension == Eigen::Dynamic,
OptionalJacobian<Eigen::Dynamic, Eigen::Dynamic>,
OptionalJacobian<dimension, dimension>>;
/// Jacobian types for internal use
using Jacobian =
std::conditional_t<dimension == Eigen::Dynamic, Matrix,
Eigen::Matrix<double, dimension, dimension>>;
using Jacobian1 = typename traits<G>::Jacobian;
using Jacobian2 = typename traits<H>::Jacobian;
public:
/// @name Standard Constructors
/// @{
/// Default constructor yields identity
ProductLieGroup() : Base(defaultIdentity<G>(), defaultIdentity<H>()) {}
/// Construct from two subgroup elements
ProductLieGroup(const G& g, const H& h) : Base(g, h) {}
/// Construct from base pair
ProductLieGroup(const Base& base) : Base(base) {}
/// @}
/// @name Group Operations
/// @{
typedef multiplicative_group_tag group_flavor;
/// Identity element
static ProductLieGroup Identity() { return ProductLieGroup(); }
/// Group multiplication
ProductLieGroup operator*(const ProductLieGroup& other) const {
checkMatchingDimensions(firstDim(), secondDim(), other.firstDim(),
other.secondDim(), "operator*");
return ProductLieGroup(traits<G>::Compose(this->first, other.first),
traits<H>::Compose(this->second, other.second));
}
/// Group inverse
ProductLieGroup inverse() const {
return ProductLieGroup(traits<G>::Inverse(this->first),
traits<H>::Inverse(this->second));
}
/// Compose with another element (same as operator*)
ProductLieGroup compose(const ProductLieGroup& g) const {
return (*this) * g;
}
/// Calculate relative transformation
ProductLieGroup between(const ProductLieGroup& g) const {
return this->inverse() * g;
}
/// @}
/// @name Manifold Operations
/// @{
/// Manifold dimension
inline constexpr static int manifoldDimension = dimension;
/// Return manifold dimension
static constexpr int Dim() { return manifoldDimension; }
/// Return manifold dimension
size_t dim() const { return combinedDimension(firstDim(), secondDim()); }
/// Retract to manifold
ProductLieGroup retract(const TangentVector& v, ChartJacobian H1 = {},
ChartJacobian H2 = {}) const {
if (H1 || H2) {
throw std::runtime_error(
"ProductLieGroup::retract derivatives not implemented yet");
}
const size_t firstDimension = firstDim();
const size_t secondDimension = secondDim();
if (static_cast<size_t>(v.size()) !=
combinedDimension(firstDimension, secondDimension)) {
throw std::invalid_argument(
"ProductLieGroup::retract tangent dimension does not match product "
"dimension");
}
G g = traits<G>::Retract(this->first,
tangentSegment<G>(v, 0, firstDimension));
H h = traits<H>::Retract(
this->second, tangentSegment<H>(v, firstDimension, secondDimension));
return ProductLieGroup(g, h);
}
/// Local coordinates on manifold
TangentVector localCoordinates(const ProductLieGroup& g,
ChartJacobian H1 = {},
ChartJacobian H2 = {}) const {
if (H1 || H2) {
throw std::runtime_error(
"ProductLieGroup::localCoordinates derivatives not implemented yet");
}
checkMatchingDimensions(firstDim(), secondDim(), g.firstDim(),
g.secondDim(), "localCoordinates");
const size_t firstDimension = firstDim();
const size_t secondDimension = secondDim();
typename traits<G>::TangentVector v1 =
traits<G>::Local(this->first, g.first);
typename traits<H>::TangentVector v2 =
traits<H>::Local(this->second, g.second);
return makeTangentVector(v1, v2, firstDimension, secondDimension);
}
/// @}
/// @name Lie Group Operations
/// @{
/// Compose with Jacobians
ProductLieGroup compose(const ProductLieGroup& other, ChartJacobian H1,
ChartJacobian H2 = {}) const {
checkMatchingDimensions(firstDim(), secondDim(), other.firstDim(),
other.secondDim(), "compose");
const size_t firstDimension = firstDim();
const size_t secondDimension = secondDim();
const size_t productDimension =
combinedDimension(firstDimension, secondDimension);
Jacobian1 D_g_first;
Jacobian2 D_h_second;
G g =
traits<G>::Compose(this->first, other.first, H1 ? &D_g_first : nullptr);
H h = traits<H>::Compose(this->second, other.second,
H1 ? &D_h_second : nullptr);
if (H1) {
*H1 = zeroJacobian(productDimension);
H1->block(0, 0, firstDimension, firstDimension) = D_g_first;
H1->block(firstDimension, firstDimension, secondDimension,
secondDimension) = D_h_second;
}
if (H2) *H2 = identityJacobian(productDimension);
return ProductLieGroup(g, h);
}
/// Between with Jacobians
ProductLieGroup between(const ProductLieGroup& other, ChartJacobian H1,
ChartJacobian H2 = {}) const {
checkMatchingDimensions(firstDim(), secondDim(), other.firstDim(),
other.secondDim(), "between");
const size_t firstDimension = firstDim();
const size_t secondDimension = secondDim();
const size_t productDimension =
combinedDimension(firstDimension, secondDimension);
Jacobian1 D_g_first;
Jacobian2 D_h_second;
G g =
traits<G>::Between(this->first, other.first, H1 ? &D_g_first : nullptr);
H h = traits<H>::Between(this->second, other.second,
H1 ? &D_h_second : nullptr);
if (H1) {
*H1 = zeroJacobian(productDimension);
H1->block(0, 0, firstDimension, firstDimension) = D_g_first;
H1->block(firstDimension, firstDimension, secondDimension,
secondDimension) = D_h_second;
}
if (H2) *H2 = identityJacobian(productDimension);
return ProductLieGroup(g, h);
}
/// Inverse with Jacobian
ProductLieGroup inverse(ChartJacobian D) const {
const size_t firstDimension = firstDim();
const size_t secondDimension = secondDim();
const size_t productDimension =
combinedDimension(firstDimension, secondDimension);
Jacobian1 D_g_first;
Jacobian2 D_h_second;
G g = traits<G>::Inverse(this->first, D ? &D_g_first : nullptr);
H h = traits<H>::Inverse(this->second, D ? &D_h_second : nullptr);
if (D) {
*D = zeroJacobian(productDimension);
D->block(0, 0, firstDimension, firstDimension) = D_g_first;
D->block(firstDimension, firstDimension, secondDimension,
secondDimension) = D_h_second;
}
return ProductLieGroup(g, h);
}
/// Exponential map
static ProductLieGroup Expmap(const TangentVector& v, ChartJacobian Hv = {}) {
if constexpr (firstDynamic && secondDynamic) {
if (v.size() == 0) {
if (Hv) *Hv = Matrix::Zero(0, 0);
return ProductLieGroup();
}
throw std::invalid_argument(
"ProductLieGroup::Expmap requires split tangent vectors when both "
"factors are dynamic");
} else if constexpr (firstDynamic) {
if (v.size() < dimension2) {
throw std::invalid_argument(
"ProductLieGroup::Expmap tangent dimension is too small for the "
"fixed second factor");
}
const size_t firstDimension = static_cast<size_t>(v.size() - dimension2);
return expmapWithDimensions(v, firstDimension,
static_cast<size_t>(dimension2), Hv);
} else if constexpr (secondDynamic) {
if (v.size() < dimension1) {
throw std::invalid_argument(
"ProductLieGroup::Expmap tangent dimension is too small for the "
"fixed first factor");
}
const size_t secondDimension = static_cast<size_t>(v.size() - dimension1);
return expmapWithDimensions(v, static_cast<size_t>(dimension1),
secondDimension, Hv);
} else {
return expmapWithDimensions(v, static_cast<size_t>(dimension1),
static_cast<size_t>(dimension2), Hv);
}
}
/// Exponential map from subgroup tangent vectors
static ProductLieGroup Expmap(
const Eigen::Ref<const typename traits<G>::TangentVector>& v1,
const Eigen::Ref<const typename traits<H>::TangentVector>& v2,
ChartJacobian Hv) {
const size_t firstDimension = static_cast<size_t>(v1.size());
const size_t secondDimension = static_cast<size_t>(v2.size());
Jacobian1 D_g_first;
Jacobian2 D_h_second;
G g = traits<G>::Expmap(v1, Hv ? &D_g_first : nullptr);
H h = traits<H>::Expmap(v2, Hv ? &D_h_second : nullptr);
if (Hv) {
*Hv = zeroJacobian(combinedDimension(firstDimension, secondDimension));
Hv->block(0, 0, firstDimension, firstDimension) = D_g_first;
Hv->block(firstDimension, firstDimension, secondDimension,
secondDimension) = D_h_second;
}
return ProductLieGroup(g, h);
}
/// Exponential map from subgroup tangent vectors
static ProductLieGroup Expmap(
const Eigen::Ref<const typename traits<G>::TangentVector>& v1,
const Eigen::Ref<const typename traits<H>::TangentVector>& v2) {
return Expmap(v1, v2, {});
}
/// Logarithmic map
static TangentVector Logmap(const ProductLieGroup& p, ChartJacobian Hp = {}) {
const size_t firstDimension = p.firstDim();
const size_t secondDimension = p.secondDim();
const size_t productDimension =
combinedDimension(firstDimension, secondDimension);
Jacobian1 D_g_first;
Jacobian2 D_h_second;
typename traits<G>::TangentVector v1 =
traits<G>::Logmap(p.first, Hp ? &D_g_first : nullptr);
typename traits<H>::TangentVector v2 =
traits<H>::Logmap(p.second, Hp ? &D_h_second : nullptr);
TangentVector v =
makeTangentVector(v1, v2, firstDimension, secondDimension);
if (Hp) {
*Hp = zeroJacobian(productDimension);
Hp->block(0, 0, firstDimension, firstDimension) = D_g_first;
Hp->block(firstDimension, firstDimension, secondDimension,
secondDimension) = D_h_second;
}
return v;
}
/// Local coordinates (same as Logmap)
static TangentVector LocalCoordinates(const ProductLieGroup& p,
ChartJacobian Hp = {}) {
return Logmap(p, Hp);
}
/// Right multiplication by exponential map
ProductLieGroup expmap(const TangentVector& v) const {
return compose(expmapWithDimensions(v, firstDim(), secondDim()));
}
/// Logarithmic map for relative transformation
TangentVector logmap(const ProductLieGroup& g) const {
return ProductLieGroup::Logmap(between(g));
}
/// Adjoint map
Jacobian AdjointMap() const {
const auto adjG = traits<G>::AdjointMap(this->first);
const auto adjH = traits<H>::AdjointMap(this->second);
const size_t d1 = static_cast<size_t>(adjG.rows());
const size_t d2 = static_cast<size_t>(adjH.rows());
Jacobian adj = zeroJacobian(d1 + d2);
adj.block(0, 0, d1, d1) = adjG;
adj.block(d1, d1, d2, d2) = adjH;
return adj;
}
/// @}
protected:
template <typename T>
static T defaultIdentity() {
if constexpr (traits<T>::dimension == Eigen::Dynamic) {
return T();
} else {
return traits<T>::Identity();
}
}
size_t firstDim() const { return traits<G>::GetDimension(this->first); }
size_t secondDim() const { return traits<H>::GetDimension(this->second); }
static size_t combinedDimension(size_t d1, size_t d2) { return d1 + d2; }
template <typename T, int Dim = traits<T>::dimension>
static typename traits<T>::TangentVector tangentSegment(
const TangentVector& v, size_t start, size_t runtimeDimension) {
const int startIndex = static_cast<int>(start);
const int runtimeIndex = static_cast<int>(runtimeDimension);
if constexpr (Dim == Eigen::Dynamic) {
return v.segment(startIndex, runtimeIndex);
} else {
static_cast<void>(runtimeDimension);
return v.template segment<Dim>(startIndex);
}
}
static TangentVector makeTangentVector(
const typename traits<G>::TangentVector& v1,
const typename traits<H>::TangentVector& v2, size_t firstDimension,
size_t secondDimension) {
const int firstIndex = static_cast<int>(firstDimension);
const int secondIndex = static_cast<int>(secondDimension);
if constexpr (dimension == Eigen::Dynamic) {
TangentVector v(combinedDimension(firstDimension, secondDimension));
v.segment(0, firstIndex) = v1;
v.segment(firstIndex, secondIndex) = v2;
return v;
} else {
static_cast<void>(firstDimension);
static_cast<void>(secondDimension);
TangentVector v;
v << v1, v2;
return v;
}
}
static Jacobian zeroJacobian(size_t productDimension) {
if constexpr (dimension == Eigen::Dynamic) {
return Jacobian::Zero(productDimension, productDimension);
} else {
static_cast<void>(productDimension);
return Jacobian::Zero();
}
}
static Jacobian identityJacobian(size_t productDimension) {
if constexpr (dimension == Eigen::Dynamic) {
return Jacobian::Identity(productDimension, productDimension);
} else {
static_cast<void>(productDimension);
return Jacobian::Identity();
}
}
static void checkMatchingDimensions(size_t first1, size_t second1,
size_t first2, size_t second2,
const char* operation) {
if (first1 != first2 || second1 != second2) {
throw std::invalid_argument(std::string("ProductLieGroup::") + operation +
" requires matching component dimensions");
}
}
static ProductLieGroup expmapWithDimensions(const TangentVector& v,
size_t firstDimension,
size_t secondDimension,
ChartJacobian Hv = {}) {
if (static_cast<size_t>(v.size()) !=
combinedDimension(firstDimension, secondDimension)) {
throw std::invalid_argument(
"ProductLieGroup::Expmap tangent dimension does not match requested "
"component dimensions");
}
Jacobian1 D_g_first;
Jacobian2 D_h_second;
G g = traits<G>::Expmap(tangentSegment<G>(v, 0, firstDimension),
Hv ? &D_g_first : nullptr);
H h =
traits<H>::Expmap(tangentSegment<H>(v, firstDimension, secondDimension),
Hv ? &D_h_second : nullptr);
if (Hv) {
*Hv = zeroJacobian(combinedDimension(firstDimension, secondDimension));
Hv->block(0, 0, firstDimension, firstDimension) = D_g_first;
Hv->block(firstDimension, firstDimension, secondDimension,
secondDimension) = D_h_second;
}
return ProductLieGroup(g, h);
}
public:
/// @name Testable interface
/// @{
void print(const std::string& s = "") const {
std::cout << s << "ProductLieGroup" << std::endl;
traits<G>::Print(this->first, " first");
traits<H>::Print(this->second, " second");
}
bool equals(const ProductLieGroup& other, double tol = 1e-9) const {
return traits<G>::Equals(this->first, other.first, tol) &&
traits<H>::Equals(this->second, other.second, tol);
}
/// @}
};
/**
* @brief Template to construct the N-fold power of a Lie group
* Represents the group G^N = G x G x ... x G (N times)
* Assumes Lie group structure for G and N >= 2
*/
template <typename G, size_t N>
class PowerLieGroup : public std::array<G, N> {
static_assert(N >= 1, "PowerLieGroup requires N >= 1");
GTSAM_CONCEPT_ASSERT(IsLieGroup<G>);
GTSAM_CONCEPT_ASSERT(IsTestable<G>);
public:
/// Base array type
typedef std::array<G, N> Base;
protected:
/// Dimension of the base group
static constexpr size_t baseDimension = traits<G>::dimension;
public:
/// @name Standard Constructors
/// @{
/// Default constructor yields identity
PowerLieGroup() { this->fill(traits<G>::Identity()); }
/// Construct from array of group elements
PowerLieGroup(const Base& elements) : Base(elements) {}
/// Construct from initializer list
PowerLieGroup(const std::initializer_list<G>& elements) {
if (elements.size() != N) {
throw std::invalid_argument(
"PowerLieGroup: initializer list size must equal N");
}
std::copy(elements.begin(), elements.end(), this->begin());
}
/// @}
/// @name Group Operations
/// @{
typedef multiplicative_group_tag group_flavor;
/// Identity element
static PowerLieGroup Identity() { return PowerLieGroup(); }
/// Group multiplication
PowerLieGroup operator*(const PowerLieGroup& other) const {
PowerLieGroup result;
for (size_t i = 0; i < N; ++i) {
result[i] = traits<G>::Compose((*this)[i], other[i]);
}
return result;
}
/// Group inverse
PowerLieGroup inverse() const {
PowerLieGroup result;
for (size_t i = 0; i < N; ++i) {
result[i] = traits<G>::Inverse((*this)[i]);
}
return result;
}
/// Compose with another element (same as operator*)
PowerLieGroup compose(const PowerLieGroup& g) const { return (*this) * g; }
/// Calculate relative transformation
PowerLieGroup between(const PowerLieGroup& g) const {
return this->inverse() * g;
}
/// @}
/// @name Manifold Operations
/// @{
/// Manifold dimension
static constexpr size_t dimension = N * baseDimension;
/// Return manifold dimension
static size_t Dim() { return dimension; }
/// Return manifold dimension
size_t dim() const { return dimension; }
/// Tangent vector type
typedef Eigen::Matrix<double, dimension, 1> TangentVector;
/// Chart Jacobian type
typedef OptionalJacobian<dimension, dimension> ChartJacobian;
/// Retract to manifold
PowerLieGroup retract(const TangentVector& v, ChartJacobian H1 = {},
ChartJacobian H2 = {}) const {
if (H1 || H2) {
throw std::runtime_error(
"PowerLieGroup::retract derivatives not implemented yet");
}
PowerLieGroup result;
for (size_t i = 0; i < N; ++i) {
const auto vi = v.template segment<baseDimension>(i * baseDimension);
result[i] = traits<G>::Retract((*this)[i], vi);
}
return result;
}
/// Local coordinates on manifold
TangentVector localCoordinates(const PowerLieGroup& g, ChartJacobian H1 = {},
ChartJacobian H2 = {}) const {
if (H1 || H2) {
throw std::runtime_error(
"PowerLieGroup::localCoordinates derivatives not implemented yet");
}
TangentVector v;
for (size_t i = 0; i < N; ++i) {
const auto vi = traits<G>::Local((*this)[i], g[i]);
v.template segment<baseDimension>(i * baseDimension) = vi;
}
return v;
}
/// @}
/// @name Lie Group Operations
/// @{
public:
/// Jacobian types for internal use
typedef Eigen::Matrix<double, dimension, dimension> Jacobian;
typedef Eigen::Matrix<double, baseDimension, baseDimension> BaseJacobian;
/// Compose with Jacobians
PowerLieGroup compose(const PowerLieGroup& other, ChartJacobian H1,
ChartJacobian H2 = {}) const {
std::array<BaseJacobian, N> jacobians;
PowerLieGroup result;
for (size_t i = 0; i < N; ++i) {
result[i] = traits<G>::Compose((*this)[i], other[i],
H1 ? &jacobians[i] : nullptr);
}
if (H1) {
H1->setZero();
for (size_t i = 0; i < N; ++i) {
H1->template block<baseDimension, baseDimension>(
i * baseDimension, i * baseDimension) = jacobians[i];
}
}
if (H2) *H2 = Jacobian::Identity();
return result;
}
/// Between with Jacobians
PowerLieGroup between(const PowerLieGroup& other, ChartJacobian H1,
ChartJacobian H2 = {}) const {
std::array<BaseJacobian, N> jacobians;
PowerLieGroup result;
for (size_t i = 0; i < N; ++i) {
result[i] = traits<G>::Between((*this)[i], other[i],
H1 ? &jacobians[i] : nullptr);
}
if (H1) {
H1->setZero();
for (size_t i = 0; i < N; ++i) {
H1->template block<baseDimension, baseDimension>(
i * baseDimension, i * baseDimension) = jacobians[i];
}
}
if (H2) *H2 = Jacobian::Identity();
return result;
}
/// Inverse with Jacobian
PowerLieGroup inverse(ChartJacobian D) const {
std::array<BaseJacobian, N> jacobians;
PowerLieGroup result;
for (size_t i = 0; i < N; ++i) {
result[i] = traits<G>::Inverse((*this)[i], D ? &jacobians[i] : nullptr);
}
if (D) {
D->setZero();
for (size_t i = 0; i < N; ++i) {
D->template block<baseDimension, baseDimension>(
i * baseDimension, i * baseDimension) = jacobians[i];
}
}
return result;
}
/// Exponential map
static PowerLieGroup Expmap(const TangentVector& v, ChartJacobian Hv = {}) {
std::array<BaseJacobian, N> jacobians;
PowerLieGroup result;
for (size_t i = 0; i < N; ++i) {
const auto vi = v.template segment<baseDimension>(i * baseDimension);
result[i] = traits<G>::Expmap(vi, Hv ? &jacobians[i] : nullptr);
}
if (Hv) {
Hv->setZero();
for (size_t i = 0; i < N; ++i) {
Hv->template block<baseDimension, baseDimension>(
i * baseDimension, i * baseDimension) = jacobians[i];
}
}
return result;
}
/// Logarithmic map
static TangentVector Logmap(const PowerLieGroup& p, ChartJacobian Hp = {}) {
std::array<BaseJacobian, N> jacobians;
TangentVector v;
for (size_t i = 0; i < N; ++i) {
const auto vi = traits<G>::Logmap(p[i], Hp ? &jacobians[i] : nullptr);
v.template segment<baseDimension>(i * baseDimension) = vi;
}
if (Hp) {
Hp->setZero();
for (size_t i = 0; i < N; ++i) {
Hp->template block<baseDimension, baseDimension>(
i * baseDimension, i * baseDimension) = jacobians[i];
}
}
return v;
}
/// Local coordinates (same as Logmap)
static TangentVector LocalCoordinates(const PowerLieGroup& p,
ChartJacobian Hp = {}) {
return Logmap(p, Hp);
}
/// Right multiplication by exponential map
PowerLieGroup expmap(const TangentVector& v) const {
return compose(PowerLieGroup::Expmap(v));
}
/// Logarithmic map for relative transformation
TangentVector logmap(const PowerLieGroup& g) const {
return PowerLieGroup::Logmap(between(g));
}
/// Adjoint map
Jacobian AdjointMap() const {
Jacobian adj = Jacobian::Zero();
for (size_t i = 0; i < N; ++i) {
const auto adjGi = traits<G>::AdjointMap((*this)[i]);
adj.template block<baseDimension, baseDimension>(
i * baseDimension, i * baseDimension) = adjGi;
}
return adj;
}
/// @}
/// @name Testable interface
/// @{
void print(const std::string& s = "") const {
std::cout << s << "PowerLieGroup" << std::endl;
for (size_t i = 0; i < N; ++i) {
traits<G>::Print((*this)[i], " component[" + std::to_string(i) + "]");
}
}
bool equals(const PowerLieGroup& other, double tol = 1e-9) const {
for (size_t i = 0; i < N; ++i) {
if (!traits<G>::Equals((*this)[i], other[i], tol)) {
return false;
}
}
return true;
}
/// @}
};
/// Traits specialization for ProductLieGroup
template <typename G, typename H>
struct traits<ProductLieGroup<G, H>>
: internal::LieGroup<ProductLieGroup<G, H>> {};
/// Traits specialization for PowerLieGroup
template <typename G, size_t N>
struct traits<PowerLieGroup<G, N>> : internal::LieGroup<PowerLieGroup<G, N>> {};
} // namespace gtsam