forked from arpg/CarPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SE3.h
285 lines (242 loc) · 8.04 KB
/
SE3.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
#pragma once
/*
* \file SE3.h
*
* Helper routines for dealing with SE3.
*
* $Id$
*/
#include <math.h>
#include <Eigen/Core>
namespace Eigen
{
typedef Matrix<double,6,1> Vector6d;
}
namespace fusion
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline void FillRotationMatrixFixedFrameXYZ(
Eigen::Matrix3d& R,
const double& r,
const double& p,
const double& q
)
{
// psi = roll, th = pitch, phi = yaw
double cq, cp, cr, sq, sp, sr;
cr = cos( r );
cp = cos( p );
cq = cos( q );
sr = sin( r );
sp = sin( p );
sq = sin( q );
R(0,0) = cp*cq;
R(0,1) = -cr*sq+sr*sp*cq;
R(0,2) = sr*sq+cr*sp*cq;
R(1,0) = cp*sq;
R(1,1) = cr*cq+sr*sp*sq;
R(1,2) = -sr*cq+cr*sp*sq;
R(2,0) = -sp;
R(2,1) = sr*cp;
R(2,2) = cr*cp;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline void FillRotationMatrixFixedFrameXYZ(
Eigen::Matrix4d& T,
const double& r,
const double& p,
const double& q
)
{
// psi = roll, th = pitch, phi = yaw
double cq, cp, cr, sq, sp, sr;
cr = cos( r );
cp = cos( p );
cq = cos( q );
sr = sin( r );
sp = sin( p );
sq = sin( q );
T(0,0) = cp*cq;
T(0,1) = -cr*sq+sr*sp*cq;
T(0,2) = sr*sq+cr*sp*cq;
T(1,0) = cp*sq;
T(1,1) = cr*cq+sr*sp*sq;
T(1,2) = -sr*cq+cr*sp*sq;
T(2,0) = -sp;
T(2,1) = sr*cp;
T(2,2) = cr*cp;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Vector3d R2Cart(
const Eigen::Matrix3d& R
)
{
Eigen::Vector3d rpq;
// roll
rpq[0] = atan2( R(2,1), R(2,2) );
// pitch
double det = -R(2,0) * R(2,0) + 1.0;
if (det <= 0) {
if (R(2,0) > 0){
rpq[1] = -M_PI / 2.0;
}
else{
rpq[1] = M_PI / 2.0;
}
}
else{
rpq[1] = -asin(R(2,0));
}
// yaw
rpq[2] = atan2(R(1,0), R(0,0));
return rpq;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Vector6d T2Cart(
double* dT
)
{
Eigen::Vector6d Cart;
Eigen::Matrix4d T(dT);
Eigen::Vector3d rpq = R2Cart( T.block<3,3>(0,0) );
Cart[0] = T(0,3);
Cart[1] = T(1,3);
Cart[2] = T(2,3);
Cart[3] = rpq[0];
Cart[4] = rpq[1];
Cart[5] = rpq[2];
return Cart;
}
inline Eigen::Vector6d T2Cart(
const Eigen::Matrix4d& T
)
{
Eigen::Vector6d Cart;
Eigen::Vector3d rpq = R2Cart( T.block<3,3>(0,0) );
Cart[0] = T(0,3);
Cart[1] = T(1,3);
Cart[2] = T(2,3);
Cart[3] = rpq[0];
Cart[4] = rpq[1];
Cart[5] = rpq[2];
return Cart;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Matrix3d Cart2R(
const double roll, //< Input:
const double pitch, //< Input:
const double yaw //< Input:
)
{
Eigen::Matrix3d R;
FillRotationMatrixFixedFrameXYZ( R, roll, pitch, yaw );
return R;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Matrix3d Cart2R(
const Eigen::Vector3d& rpy //< Input:
)
{
return Cart2R( rpy[0], rpy[1], rpy[2] );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Matrix4d Cart2T(
const Eigen::Vector6d& Cart
)
{
Eigen::Matrix4d T;
FillRotationMatrixFixedFrameXYZ( T, Cart[3], Cart[4], Cart[5] );
T(0,3) = Cart[0];
T(1,3) = Cart[1];
T(2,3) = Cart[2];
T.row(3) = Eigen::Vector4d( 0.0, 0.0, 0.0, 1.0 );
return T;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Matrix4d Cart2T(
double x,
double y,
double z,
double r,
double p,
double q
)
{
Eigen::Matrix4d T;
// psi = roll, th = pitch, phi = yaw
double cq, cp, cr, sq, sp, sr;
cr = cos( r );
cp = cos( p );
cq = cos( q );
sr = sin( r );
sp = sin( p );
sq = sin( q );
T(0,0) = cp*cq;
T(0,1) = -cr*sq+sr*sp*cq;
T(0,2) = sr*sq+cr*sp*cq;
T(1,0) = cp*sq;
T(1,1) = cr*cq+sr*sp*sq;
T(1,2) = -sr*cq+cr*sp*sq;
T(2,0) = -sp;
T(2,1) = sr*cp;
T(2,2) = cr*cp;
T(0,3) = x;
T(1,3) = y;
T(2,3) = z;
T.row(3) = Eigen::Vector4d( 0.0, 0.0, 0.0, 1.0 );
return T;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Matrix4d TInv( Eigen::Matrix4d T )
{
// calc Hji = [ Hij(1:3,1:3).' -Hij(1:3,1:3).'*Hij(1:3,4); 0 0 0 1 ];
Eigen::Matrix4d invT;
invT.block<3,3>(0,0) = T.block<3,3>(0,0).transpose();
invT.block<3,1>(0,3) = -T.block<3,3>(0,0).transpose() * T.block<3,1>(0,3);
invT.row(3) = Eigen::Vector4d( 0, 0, 0, 1 );
return invT;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Vector3d TComp( Eigen::Matrix4d T, Eigen::Vector3d p )
{
Eigen::Vector4d tmp( p[0], p[1], p[2], 1 );
return T.block<3,4>(0,0)*tmp;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Eigen::Matrix4d TComp( Eigen::Matrix4d Tab, Eigen::Matrix4d Tbc )
{
return Tab*Tbc;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Make sure top-left 3x3 matrix is orthonormal.
inline void MakeOrthonormal( Eigen::Matrix4d& T )
{
Eigen::Vector3d rpy = R2Cart( T.topLeftCorner<3,3>() );
T.topLeftCorner<3,3>() = Cart2R( rpy );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Make sure top-left 3x3 matrix is orthonormal.
inline void MakeOrthonormal( Eigen::Matrix3d& R )
{
Eigen::Vector3d rpy = R2Cart( R );
R = Cart2R( rpy );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Make sure top-left 3x3 matrix is orthonormal.
inline bool IsValidTransform( Eigen::Matrix4d& T )
{
if( T(3,0) != 0 || T(3,1) != 0 || T(3,2) != 0 || T(3,3) != 1 ){
return false;
}
// Check the determinant.
double det = T.topLeftCorner<3,3>().determinant();
if( fabs( det - 1.0 ) < 1e-12 ){ // TODO: find better check
return true; // good, it's an orthonormal right handed bases
} else if( fabs( det + 1.0 ) < 1e-12 ){
fprintf(stderr, "WARNING: basis is left handed!\n");
return true;
}
fprintf(stderr, "WARNING: matrix determinant is%e \n", det );
return false;
}
}