Robot Control Library
quaternion.h
Go to the documentation of this file.
1/**
2 * <rc/math/quaternion.h>
3 *
4 * @brief Functions for quaternion manipulation.
5 *
6 * All functions have two versions, one that acts on an rc_vector_t of length 4
7 * and another that acts on normal C arrays, also of length 4. They are
8 * functionally identical and just give the user options depending on how they
9 * represent quaternions in their code.
10 *
11 * @author James Strawson
12 * @date 2016
13 *
14 * @addtogroup Quaternion
15 * @ingroup Math
16 * @{
17 */
18
19
20#ifndef RC_QUATERNION_H
21#define RC_QUATERNION_H
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#include <rc/math/vector.h>
28#include <rc/math/matrix.h>
29
30/**
31 * @brief Returns the length of a quaternion vector by finding its 2-norm.
32 *
33 * @param[in] q The quarternion in form of a vector of length 4
34 *
35 * @return Returns the norm, or prints an error message and returns -1.0f on
36 * error.
37 */
39
40/**
41 * @brief Returns the length of a quaternion vector by finding its 2-norm.
42 *
43 * @param[in] q The quarternion in form of an array of length 4
44 *
45 * @return Returns the norm, or prints an error message and returns -1.0f on
46 * error.
47 */
48double rc_quaternion_norm_array(double q[4]);
49
50/**
51 * @brief Normalizes a quaternion in-place to have length 1.0
52 *
53 * @param q The quarternion in form of a vector of lenth 4
54 *
55 * @return Returns 0 on success or -1 on failure
56 */
58
59/**
60 * @brief Normalizes a quaternion in-place to have length 1.0
61 *
62 * @param q The quarternion in form of an array of length 4
63 *
64 * @return Returns 0 on success or -1 on failure
65 */
67
68/**
69 * @brief Calculates 321 Tait Bryan angles in array order XYZ with
70 * operation order 321(yaw-Z, pitch-Y, roll-x).
71 *
72 * If tb is already allocated and of length 3 then the new values are written in
73 * place, otherwise any existing memory is freed and a new vector of length 3 is
74 * allocated for tb.
75 *
76 * @param[in] q The quarternion in form of a vector of lenth 4
77 * @param tb Output tait-bryan angles
78 *
79 * @return Returns 0 on success or -1 on failure
80 */
82
83/**
84 * @brief Calculates 321 Tait Bryan angles in array order XYZ with
85 * operation order 321(yaw-Z, pitch-Y, roll-x).
86 *
87 * @param[in] q The quarternion in form of an array of lenth 4
88 * @param[out] tb Output tait-bryan angles
89 *
90 * @return Returns 0 on success or -1 on failure
91 */
92int rc_quaternion_to_tb_array(double q[4], double tb[3]);
93
94/**
95 * @brief Calculates quaternion vector q from tait-bryan angles tb.
96 *
97 * If q is already of length 4 then old contents are simply overwritten.
98 * Otherwise q'd existing memory is freed and new memory is allocated to aint
99 * memory leaks. tb angles are 321 Tait Bryan angles in array order XYZ with
100 * operation order 321(yaw-Z, pitch-Y, roll-x).
101 *
102 * @param[in] tb input tait-bryan angles
103 * @param[out] q output quaternion
104 *
105 * @return Returns 0 on success or -1 on failure
106 */
108
109/**
110 * @brief Calculates quaternion vector q from tait-bryan angles tb.
111 *
112 * tb angles are 321 Tait Bryan angles in array order XYZ with operation order
113 * 321(yaw-Z, pitch-Y, roll-x).
114 *
115 * @param[in] tb input tait-bryan angles
116 * @param[out] q output quaternion
117 *
118 * @return Returns 0 on success or -1 on failure
119 */
120int rc_quaternion_from_tb_array(double tb[3], double q[4]);
121
122/**
123 * @brief Calculates conjugate of quaternion q.
124 *
125 * Populates quaternion vector c with the conjugate of quaternion q where the 3
126 * imaginary parts ijk are multiplied by -1. If c is already of length 4 then
127 * the old values are overwritten. Otherwise the old memory in c is freed and
128 * new memory is allocated to help prevent memory leaks.
129 *
130 * @param[in] q The quarter
131 * @param[out] c output conjugate
132 *
133 * @return Returns 0 on success or -1 on failure.
134 */
136
137/**
138 * @brief Calculates conjugate of quaternion q and overwrites the old q.
139 *
140 * Populates quaternion vector q with its conjugate where the 3 imaginary parts
141 * ijk are multiplied by -1.
142 *
143 * @param[in] q The quarternion
144 *
145 * @return Returns 0 on success or -1 on failure.
146 */
148
149/**
150 * @brief Calculates conjugate of quaternion q.
151 *
152 * Populates quaternion vector c with the conjugate of quaternion q where the 3
153 * imaginary parts ijk are multiplied by -1.
154 *
155 * @param[in] q The quarter
156 * @param[out] c output conjugate
157 *
158 * @return Returns 0 on success or -1 on failure.
159 */
160int rc_quaternion_conjugate_array(double q[4], double c[4]);
161
162/**
163 * @brief Calculates conjugate of quaternion q and overwrites the old q.
164 *
165 * Populates quaternion vector q with its conjugate where the 3 imaginary parts
166 * ijk are multiplied by -1.
167 *
168 * @param[in] q The quarternion
169 *
170 * @return Returns 0 on success or -1 on failure.
171 */
173
174/**
175 * @brief Populates vector i with the imaginary components ijk of of
176 * quaternion vector q.
177 *
178 * If img is already of length 3 then its original contents are overwritten.
179 * Otherwise the original allocated memory is freed and new memory is allocated.
180 *
181 * @param[in] q The quarternion
182 * @param[out] img imaginary part
183 *
184 * @return Returns 0 on success or -1 on failure.
185 */
187
188/**
189 * @brief Calculates the quaternion Hamilton product ab=c
190 *
191 * Places the result in vector argument c. If c is already of length 4 then the
192 * old values are overwritten. Otherwise the old memory in c is freed and new
193 * memory is allocated to help prevent memory leaks.
194 *
195 * @param[in] a First input
196 * @param[in] b second input
197 * @param[out] c output
198 *
199 * @return Returns 0 on success or -1 on failure.
200 */
202
203/**
204 * @brief Calculates the quaternion Hamilton product ab=c
205 *
206 * Places the result in vector argument c.
207 *
208 * @param[in] a First input
209 * @param[in] b second input
210 * @param[out] c output
211 *
212 * @return Returns 0 on success or -1 on failure.
213 */
214int rc_quaternion_multiply_array(double a[4], double b[4], double c[4]);
215
216/**
217 * @brief Rotates the quaternion p by quaternion q with the operation
218 * p'=qpq*
219 *
220 * p is modified in place, q is unmodified.
221 *
222 * @param p quaternion to be rotated
223 * @param[in] q rotation quarternion
224 *
225 * @return Returns 0 on success or -1 on failure.
226 */
228
229/**
230 * @brief Rotates the quaternion p by quaternion q with the operation
231 * p'=qpq*
232 *
233 * p is modified in place, q is unmodified.
234 *
235 * @param p quaternion to be rotated
236 * @param[in] q rotation quarternion
237 *
238 * @return Returns 0 on success or -1 on failure.
239 */
240int rc_quaternion_rotate_array(double p[4], double q[4]);
241
242/**
243 * @brief Rotate a 3D vector v in-place about the origin by quaternion q by
244 * converting v to a quaternion and performing the operation p'=qpq*
245 *
246 * v is modified in place, q is unmodified.
247 *
248 * @param v vectpr to be rotated
249 * @param[in] q rotation quarternion
250 *
251 * @return Returns 0 on success or -1 on failure.
252 */
254
255/**
256 * @brief Rotate a 3D vector v in-place about the origin by quaternion q by
257 * converting v to a quaternion and performing the operation p'=qpq*
258 *
259 * v is modified in place, q is unmodified.
260 *
261 * @param v vectpr to be rotated
262 * @param[in] q rotation quarternion
263 *
264 * @return Returns 0 on success or -1 on failure.
265 */
266int rc_quaternion_rotate_vector_array(double v[3], double q[4]);
267
268/**
269 * @brief Converts a normalized quaternion to a 3x3 orthogonal rotation
270 * matrix.
271 *
272 * q must be normalized before calling this! The orthogonal matrix corresponds
273 * to a rotation by the unit quaternion q when post-multiplied with a column
274 * vector as such: v_rotated=mv.
275 *
276 * If m is already 3x3 then its contents are overwritten, otherwise its existing
277 * memory is freed and new memory is allocated.
278 *
279 * @param[in] q The quarter
280 * @param m output 3x3 rotation matrix
281 *
282 * @return Returns 0 on success or -1 on failure.
283 */
285
286
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif // RC_QUATERNION_H
293
294/** @} end group math*/
int rc_normalize_quaternion_array(double q[4])
Normalizes a quaternion in-place to have length 1.0.
int rc_quaternion_rotate(rc_vector_t *p, rc_vector_t q)
Rotates the quaternion p by quaternion q with the operation p'=qpq*.
int rc_quaternion_to_rotation_matrix(rc_vector_t q, rc_matrix_t *m)
Converts a normalized quaternion to a 3x3 orthogonal rotation matrix.
int rc_quaternion_conjugate_inplace(rc_vector_t *q)
Calculates conjugate of quaternion q and overwrites the old q.
int rc_quaternion_from_tb_array(double tb[3], double q[4])
Calculates quaternion vector q from tait-bryan angles tb.
int rc_quaternion_multiply_array(double a[4], double b[4], double c[4])
Calculates the quaternion Hamilton product ab=c.
int rc_quaternion_rotate_vector(rc_vector_t *v, rc_vector_t q)
Rotate a 3D vector v in-place about the origin by quaternion q by converting v to a quaternion and pe...
int rc_quaternion_from_tb(rc_vector_t tb, rc_vector_t *q)
Calculates quaternion vector q from tait-bryan angles tb.
int rc_quaternion_to_tb(rc_vector_t q, rc_vector_t *tb)
Calculates 321 Tait Bryan angles in array order XYZ with operation order 321(yaw-Z,...
int rc_quaternion_rotate_array(double p[4], double q[4])
Rotates the quaternion p by quaternion q with the operation p'=qpq*.
int rc_quaternion_conjugate_array_inplace(double q[4])
Calculates conjugate of quaternion q and overwrites the old q.
int rc_quaternion_multiply(rc_vector_t a, rc_vector_t b, rc_vector_t *c)
Calculates the quaternion Hamilton product ab=c.
int rc_quaternion_to_tb_array(double q[4], double tb[3])
Calculates 321 Tait Bryan angles in array order XYZ with operation order 321(yaw-Z,...
int rc_quaternion_imaginary_part(rc_vector_t q, rc_vector_t *img)
Populates vector i with the imaginary components ijk of of quaternion vector q.
double rc_quaternion_norm(rc_vector_t q)
Returns the length of a quaternion vector by finding its 2-norm.
int rc_normalize_quaternion(rc_vector_t *q)
Normalizes a quaternion in-place to have length 1.0.
int rc_quaternion_conjugate_array(double q[4], double c[4])
Calculates conjugate of quaternion q.
double rc_quaternion_norm_array(double q[4])
Returns the length of a quaternion vector by finding its 2-norm.
int rc_quaternion_rotate_vector_array(double v[3], double q[4])
Rotate a 3D vector v in-place about the origin by quaternion q by converting v to a quaternion and pe...
int rc_quaternion_conjugate(rc_vector_t q, rc_vector_t *c)
Calculates conjugate of quaternion q.
Struct containing the state of a matrix and a pointer to dynamically allocated memory to hold its con...
Definition: matrix.h:32
Struct containing the state of a vector and a pointer to dynamically allocated memory to hold its con...
Definition: vector.h:41