gtsam
Loading...
Searching...
No Matches
Group.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
20
21#pragma once
22
23#include <gtsam/base/Testable.h>
24
25#include <utility>
26#include <type_traits>
27
28namespace gtsam {
29
31struct group_tag {};
32
36
37template <typename T> struct traits;
38
42template<typename G>
43class IsGroup {
44public:
45 typedef typename traits<G>::structure_category structure_category_tag;
46 typedef typename traits<G>::group_flavor flavor_tag;
47 //typedef typename traits<G>::identity::value_type identity_value_type;
48
49 GTSAM_CONCEPT_USAGE(IsGroup) {
50 static_assert(
51 (std::is_base_of_v<group_tag, structure_category_tag>),
52 "This type's structure_category trait does not assert it as a group (or derived)");
54 e = traits<G>::Compose(g, h);
55 e = traits<G>::Between(g, h);
56 e = traits<G>::Inverse(g);
57
58 if constexpr (std::is_same_v<flavor_tag, multiplicative_group_tag>) {
59 e = g * h;
60 //e = -g; // todo this should work, but it is failing for Quaternions
61 } else if constexpr (std::is_same_v<flavor_tag, additive_group_tag>) {
62 e = g + h;
63 e = h - g;
64 e = -g;
65 }
66 // todo: how do we test the act concept? or do we even need to?
67 }
68
69private:
70 G e, g, h;
71 bool b;
72};
73
75template<typename G>
76GTSAM_CONCEPT_REQUIRES(IsGroup<G>,bool) //
77check_group_invariants(const G& a, const G& b, double tol = 1e-9) {
78 G e = traits<G>::Identity();
82}
83
84namespace internal {
85
88template<class Class>
90 typedef group_tag structure_category;
91 typedef multiplicative_group_tag group_flavor;
92 static Class Identity() { return Class::Identity(); }
93 static Class Compose(const Class &g, const Class & h) { return g * h;}
94 static Class Between(const Class &g, const Class & h) { return g.inverse() * h;}
95 static Class Inverse(const Class &g) { return g.inverse();}
96};
97
99template<class Class>
101
104template<class Class>
106 typedef group_tag structure_category;
107 typedef additive_group_tag group_flavor;
108 static Class Identity() { return Class::Identity(); }
109 static Class Compose(const Class &g, const Class & h) { return g + h;}
110 static Class Between(const Class &g, const Class & h) { return h - g;}
111 static Class Inverse(const Class &g) { return -g;}
112};
113
115template<class Class>
116struct AdditiveGroup : AdditiveGroupTraits<Class>, Testable<Class> {};
117
118} // namespace internal
119
121template<typename G>
122GTSAM_CONCEPT_REQUIRES(IsGroup<G>,G) //
123compose_pow(const G& g, size_t n) {
124 if (n == 0) return traits<G>::Identity();
125 else if (n == 1) return g;
126 else return traits<G>::Compose(compose_pow(g, n - 1), g);
127}
128
131template<typename G, typename H>
132class DirectProduct: public std::pair<G, H> {
133 GTSAM_CONCEPT_ASSERT(IsGroup<G>);
134 GTSAM_CONCEPT_ASSERT(IsGroup<H>);
135
136public:
138 DirectProduct():std::pair<G,H>(traits<G>::Identity(),traits<H>::Identity()) {}
139
140 // Construct from two subgroup elements
141 DirectProduct(const G& g, const H& h):std::pair<G,H>(g,h) {}
142
143 // identity
144 static DirectProduct Identity() { return DirectProduct(); }
145
146 DirectProduct operator*(const DirectProduct& other) const {
147 return DirectProduct(traits<G>::Compose(this->first, other.first),
148 traits<H>::Compose(this->second, other.second));
149 }
150 DirectProduct inverse() const {
151 return DirectProduct(this->first.inverse(), this->second.inverse());
152 }
153};
154
155// Define any direct product group to be a model of the multiplicative Group concept
156template<typename G, typename H>
157struct traits<DirectProduct<G, H> > :
158 internal::MultiplicativeGroupTraits<DirectProduct<G, H> > {};
159
162template<typename G, typename H>
163class DirectSum: public std::pair<G, H> {
164 GTSAM_CONCEPT_ASSERT(IsGroup<G>); // TODO(frank): check additive
165 GTSAM_CONCEPT_ASSERT(IsGroup<H>); // TODO(frank): check additive
166
167 const G& g() const { return this->first; }
168 const H& h() const { return this->second;}
169
170public:
172 DirectSum():std::pair<G,H>(traits<G>::Identity(),traits<H>::Identity()) {}
173
174 // Construct from two subgroup elements
175 DirectSum(const G& g, const H& h):std::pair<G,H>(g,h) {}
176
177 // identity
178 static DirectSum Identity() { return DirectSum(); }
179
180 DirectSum operator+(const DirectSum& other) const {
181 return DirectSum(g()+other.g(), h()+other.h());
182 }
183 DirectSum operator-(const DirectSum& other) const {
184 return DirectSum(g()-other.g(), h()-other.h());
185 }
186 DirectSum operator-() const {
187 return DirectSum(- g(), - h());
188 }
189};
190
191// Define direct sums to be a model of the Additive Group concept
192template<typename G, typename H>
193struct traits<DirectSum<G, H> > :
194 internal::AdditiveGroupTraits<DirectSum<G, H> > {};
195
196} // namespace gtsam
197
206#define GTSAM_CONCEPT_GROUP_INST(T) template class gtsam::IsGroup<T>;
207#define GTSAM_CONCEPT_GROUP_TYPE(T) typedef gtsam::IsGroup<T> _gtsam_IsGroup_##T;
Concept check for values that can be used in unit tests.
STL namespace.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
compose_pow(const G &g, size_t n)
compose multiple times
Definition Group.h:123
check_group_invariants(const G &a, const G &b, double tol=1e-9)
Check invariants.
Definition Group.h:77
tag to assert a type is a group
Definition Group.h:31
Group operator syntax flavors.
Definition Group.h:34
Definition Group.h:35
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Group Concept.
Definition Group.h:43
A helper class that implements the traits interface for multiplicative groups.
Definition Group.h:89
Both multiplicative group traits and Testable.
Definition Group.h:100
A helper class that implements the traits interface for additive groups.
Definition Group.h:105
Both additive group traits and Testable.
Definition Group.h:116
Template to construct the direct product of two arbitrary groups Assumes nothing except group structu...
Definition Group.h:132
DirectProduct()
Default constructor yields identity.
Definition Group.h:138
Template to construct the direct sum of two additive groups Assumes existence of three additive opera...
Definition Group.h:163
DirectSum()
Default constructor yields identity.
Definition Group.h:172
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152