-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.hpp
253 lines (184 loc) · 5.98 KB
/
group.hpp
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
#ifndef GEOMETRY_HPP
#define GEOMETRY_HPP
#include <Eigen/Core>
#include <Eigen/Geometry>
// types
template<class U, int M=Eigen::Dynamic, int N=Eigen::Dynamic>
using matrix = Eigen::Matrix<U, M, N>;
template<class U, int N=Eigen::Dynamic>
using vector = matrix<U, N, 1>;
template<class U, int N=Eigen::Dynamic>
using covector = matrix<U, 1, N>;
template<class U>
using quaternion = Eigen::Quaternion<U>;
template<class G>
struct group;
// rigid bodies
template<class U>
struct rigid {
quaternion<U> rotation;
vector<U, 3> translation;
rigid(): rotation(quaternion<U>::Identity()), translation(0, 0, 0) { }
};
// quaternion group
template<class U>
struct group<quaternion<U>> {
using type = quaternion<U>;
using algebra = vector<U, 3>;
static type id() { return type::Identity(); }
static type inv(const type& self) { return self.conjugate(); }
static type prod(const type& lhs, const type& rhs) { return lhs * rhs; }
static type exp(const algebra& self);
static algebra log(const type& self);
static algebra dexp(const algebra& self, const algebra& dself);
static algebra dlog(const type& self, const algebra& dself);
static algebra Ad(const type& self, const algebra& omega) { return self * omega; }
static algebra ad(const algebra& self, const algebra& omega) { return self.cross(omega); }
};
// product group, same types
template<class G, int N>
struct group<vector<G, N>> {
static_assert(N != Eigen::Dynamic, "size must be known at compile-time");
using type = vector<G, N>;
using algebra = vector<typename group<G>::algebra, N>;
template<class F, class ... Self>
static auto map(F f, const Self& ... self) {
using result_type = typename std::result_of<F(Self...)>::type;
vector<result_type, N> result;
for(int i = 0; i < N; ++i) {
result(i) = f(self(i)...);
}
return result;
}
static type id() {
return type::Constant(group<G>::id());
}
static type inv(const type& self) {
return map(group<G>::inv, self);
}
static type prod(const type& lhs, const type& rhs) {
return map(group<G>::prod, lhs, rhs);
}
static type exp(const algebra& self) {
return map(group<G>::exp, self);
}
static algebra log(const type& self) {
return map(group<G>::log, self);
}
static algebra dexp(const algebra& self, const algebra& dself) {
return map(group<G>::dexp, self, dself);
}
static algebra dlog(const type& self, const algebra& dself) {
return map(group<G>::dlog, self, dself);
}
static algebra Ad(const type& self, const algebra& omega) {
return map(group<G>::Ad, self, omega);
}
static algebra ad(const algebra& self, const algebra& omega) {
return map(group<G>::ad, self, omega);
}
};
// product group, different types
template<class G, class A, class ... Gs>
struct product {
using type = G;
using algebra = A;
static type id() {
return group<G>::pack(group<Gs>::id()...);
}
static type inv(const type& self) {
return group<G>::unpack(self, [](const Gs&...gs) {
return group<G>::pack(group<Gs>::inv(gs)...);
});
}
static type prod(const type& lhs, const type& rhs) {
return group<G>::unpack(lhs, [&](const Gs& ... lhs) {
return group<G>::unpack(rhs, [&](const Gs& ... rhs) {
return group<G>::pack(group<Gs>::prod(lhs, rhs)...);
});
});
}
static type exp(const algebra& self) {
return group<G>::unpack(self, [](const typename group<Gs>::algebra& ... ws) {
return group<G>::pack(group<Gs>::exp(ws)...);
});
}
static algebra log(const type& self) {
return group<G>::unpack(self, [](const typename group<Gs>::algebra& ... ws) {
return group<G>::pack(group<Gs>::log(ws)...);
});
}
static algebra dexp(const algebra& self, const algebra& dself) {
return group<G>::unpack(self, [&](const typename group<Gs>::algebra& ... ws) {
return group<G>::unpack(dself, [&](const typename group<Gs>::algebra& ... dws) {
return group<G>::pack(group<Gs>::dexp(ws, dws)...);
});
});
}
static algebra dlog(const type& self, const algebra& dself) {
return group<G>::unpack(self, [&](const Gs& ... gs) {
return group<G>::unpack(dself, [&](const typename group<Gs>::algebra& ... dgs) {
group<G>::pack(group<Gs>::dlog(gs, dgs)...);
});
});
}
static algebra Ad(const type& self, const algebra& dself) {
return group<G>::unpack(self, [&](const Gs& ... gs) {
return group<G>::unpack(dself, [&](const typename group<Gs>::algebra& ... dgs) {
return group<G>::pack(group<Gs>::Ad(gs, dgs)...);
});
});
}
static algebra ad(const type& self, const algebra& dself) {
return group<G>::unpack(self, [&](const Gs& ... gs) {
return group<G>::unpack(dself, [&](const typename group<Gs>::algebra& ... dgs) {
group<G>::pack(group<Gs>::ad(gs, dgs)...);
});
});
}
};
// eucliean spaces
template<class E>
struct euclidean {
using type = E;
using algebra = E;
static type inv(const type& self) {
return -self;
}
static type prod(const type& lhs, const type& rhs) {
return lhs + rhs;
}
static type exp(const algebra& self) {
return self;
}
static algebra log(const type& self) {
return self;
}
static algebra dexp(const algebra& self, const algebra& dself) {
return dself;
}
static algebra dlog(const type& self, const algebra& dself) {
return dself;
}
static algebra Ad(const type& self, const algebra& omega) {
return omega;
}
static algebra ad(const algebra& self, const algebra& omega) {
return group<E>::id();
}
};
// scalars
template<> struct group<double> : euclidean<double> {
static double id() { return 0; }
};
// shorthands for real types
using real = double;
using vec = vector<real>;
using vec2 = vector<real, 2>;
using vec3 = vector<real, 3>;
using vec6 = vector<real, 6>;
using mat22 = matrix<real, 2, 2>;
using mat33 = matrix<real, 3, 3>;
using mat66 = matrix<real, 6, 6>;
using quat = quaternion<real>;
#endif