-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotation.h
More file actions
295 lines (248 loc) · 8.03 KB
/
Rotation.h
File metadata and controls
295 lines (248 loc) · 8.03 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
/*
* This file is part of ALVAR, A Library for Virtual and Augmented Reality.
*
* Copyright 2007-2012 VTT Technical Research Centre of Finland
*
* Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
* <http://www.vtt.fi/multimedia/alvar.html>
*
* ALVAR is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ALVAR; if not, see
* <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
*/
#ifndef ROTATION_H
#define ROTATION_H
#include <iostream>
/**
* \file Rotation.h
*
* \brief This file implements a parametrized rotation.
*/
#include "Alvar.h"
#include "Util.h"
namespace alvar {
// Use Boost::quaternion? All this seems pretty archaic. Revisit when working for cleanup. TTC!!
/*****************************************************************************
*
*** class Rotation
*
* Rotation structure and transformations between different parameterizations.
*
* Rotation is represented as a quaternion. Rather than use quaterions to
* perform the rotation math, this class is used to convert between
* quaternions and rotation matrices in various forms.
*
*****************************************************************************/
class ALVAR_EXPORT Rotation
{
public:
using Quaternion = std::array<double, 4>;
/**
* \brief Rotation can be represented in four ways: quaternion (QUAT), matrix (MAT), euler angles (EUL) and
* exponential map (ROD).
* Only used in constructor Rotation(cv::Mat& data, RotationType t)
*/
enum class RotationType { QUAT, MAT, EUL, ROD };
Rotation();
/**
* \brief Constructor.
* \param data Rotation data stored in cv::Mat. With RotationType::MAT both 3x3 and 4x4 matrices are allowed.
* \param t Rotation type that corresponds to data.
*/
Rotation(cv::Mat_<double>& mat, RotationType t);
Rotation(const Rotation& src) : quaternion(src.quaternion)
{
return;
}
~Rotation() = default;
Rotation& operator = (const Rotation& rhs)
{
if (this != &rhs)
{
quaternion = rhs.quaternion;
} // end if
return (*this);
}
Rotation& operator += (const Rotation& v);
//Rotation& operator + () {return *this;}
double& operator[](size_t i)
{
return (quaternion[i]);
}
double operator[](size_t i) const
{
return (quaternion[i]);
}
void Transpose();
/**
* \brief Simple function to mirror a rotation matrix in different directions.
* \param mat Matrix to be mirrored.
* \param x
* \param y
* \param z
*/
static void MirrorMat(cv::Mat& mat, bool x, bool y, bool z);
/**
* \brief Mirrors the rotation in selected directions.
* \param x
* \param y
* \param z
*/
void Mirror(bool x, bool y, bool z);
/**
* \brief Resets the rotation into identity.
*/
void Reset();
/**
* \brief Converts 3x3 rotation matrix into Rodrigues representation.
* \param mat 3x3 rotation matrix.
* \param rod Resulting 3x1 rotation vector.
*/
static cv::Mat_<double> Mat9ToRod(const cv::Mat_<double>& mat);
/**
* \brief Converts 3x1 rotation vector into 3x3 rotation matrix using Rodrigues' formula.
* \param rod 3x1 rotation vector.
* \param Resulting 3x3 rotation matrix.
*/
static cv::Mat_<double> RodToMat9(const cv::Mat_<double>& rod);
/**
* \brief Inverts unit quaternion.
* \param q Unit quaternion to be inverted.
* \param qi Resulting quaternion.
*/
void Conjugate();
void Conjugate(Rotation& conj) const
{
conj = *this;
conj.Conjugate();
return;
}
/**
* \brief Normalizes a quaternion.
* \param q Quaternion to be normalized.
*/
void Normalize();
/**
* \brief Quaternion multiplication.
* \param q1
* \param q2
* \param result
*/
static void Mul(const Rotation& q1, const Rotation& q2, Rotation& result);
//% The quaternion has to be normalized!!!
/**
* \brief Converts a rotation described by a quaternion into 3x3 rotation matrix.
* \param quat Rotation in quaternion form.
* \param mat Corresponding 3x3 rotation matrix.
*/
cv::Mat_<double> ToMat9() const;
/**
* \brief Converts a rotation described by a quaternion into 4x4 OpenGL-like transformation matrix.
* \param quat Rotation in quaternion form.
* \param mat Resulting 4x4 transformation matrix.
*/
cv::Mat_<double> ToMat16() const;
/**
* \brief Converts a rotation described by a quaternion into Euler angles.
* \param q Rotation in quaternion form.
* \param eul Resulting Euler angles.
*/
cv::Mat_<double> ToEul() const;
/**
* \brief Converts a 3x3 rotation martix into quaternion form.
* \param mat 3x3 rotation matrix.
* \param quat Resulting quaternion.
*/
void FromMat9(const cv::Mat_<double>& mat9);
/**
* \brief Converts a rotation described by Euler angles into quaternion form.
* \param eul Rotation in Euler angles.
* \param quat Resulting quaternion.
*/
void FromEul(const cv::Mat_<double>& eul);
/**
* \brief Sets the rotation from given quaternion.
* \param mat Input quaternion (4x1 cv::Mat).
*/
void SetQuaternion(const cv::Mat_<double>& mat);
void SetQuaternion(const Rotation& rot);
/**
* \brief Sets the rotation from given Euler angles.
* \param mat Input Euler angles (3x1 cv::Mat).
*/
void SetEuler(const cv::Mat_<double>& eul);
/**
* \brief Sets the rotation from given rotation vector.
* \param mat Input rotation vector (3x1 cv::Mat).
*/
void SetRodrigues(cv::Mat_<double>& rod)
{
assert((rod.rows == 3) && (rod.cols == 1));
// Not sure why we have to go to a Mat9 here. Just need to compute the components of the quaternion.
FromMat9(RodToMat9(rod));
return;
}
/**
* \brief Sets the rotation from given rotation matrix. 3x3 and 4x4 matrices are allowed.
* \param mat Input rotation matrix (3x3 or 4x4 cv::Mat).
*/
void SetMatrix(const cv::Mat& mat);
/**
* \brief Returns the rotation in 3x3 matrix form.
* \param mat The rotation is stored here.
*/
cv::Mat_<double> GetMatrix() const
{
return (ToMat9());
}
/**
* \brief Returns the rotation in 4x4 homogeneous matrix form (Translation is zero).
* \param mat The rotation is stored here.
*/
cv::Mat_<double> GetHomogeneousMatrix() const
{
return (ToMat16());
}
/**
* \brief Returns the rotation in rotation vector form.
* \param mat The rotation is stored here (3x1 cv::Mat).
*/
cv::Mat_<double> GetRodrigues() const
{
return (Rotation::Mat9ToRod(ToMat9()));
}
/**
* \brief Returns the rotation in Euler angles.
* \param mat The rotation is stored here (3x1 cv::Mat).
*/
cv::Mat_<double> GetEuler() const
{
return (ToEul());
}
/**
* \brief Returns the rotation in quaternion form.
* \param mat The rotation is stored here (4x1 cv::Mat).
*/
void GetQuaternion(Quaternion& quat) const
{
quat = quaternion;
return;
}
// Dump the quaternion values to cout
void Output() const;
protected:
// Storage for the 4 elements
Quaternion quaternion;
}; // end of class Rotation
} // namespace alvar
#endif