Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pprz_algebra_float.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2014 The Paparazzi Team
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
31 #ifndef PPRZ_ALGEBRA_FLOAT_H
32 #define PPRZ_ALGEBRA_FLOAT_H
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include "pprz_algebra.h"
39 #include "message_pragmas.h"
40 
41 #include <math.h>
42 #include <float.h> // for FLT_MIN
43 
44 /* this seems to be missing for some arch */
45 #ifndef M_SQRT2
46 #define M_SQRT2 1.41421356237309504880
47 #endif
48 
49 struct FloatVect2 {
50  float x;
51  float y;
52 };
53 
54 struct FloatVect3 {
55  float x;
56  float y;
57  float z;
58 };
59 
63 struct FloatQuat {
64  float qi;
65  float qx;
66  float qy;
67  float qz;
68 };
69 
70 struct FloatMat33 {
71  float m[3 * 3];
72 };
73 
77 struct FloatRMat {
78  float m[3 * 3];
79 };
80 
84 struct FloatEulers {
85  float phi;
86  float theta;
87  float psi;
88 };
89 
93 struct FloatRates {
94  float p;
95  float q;
96  float r;
97 };
98 
99 #define FLOAT_ANGLE_NORMALIZE(_a) { \
100  while (_a > M_PI) _a -= (2.*M_PI); \
101  while (_a < -M_PI) _a += (2.*M_PI); \
102  }
103 
104 //
105 //
106 // Vector algebra
107 //
108 //
109 
110 
111 /*
112  * Dimension 2 Vectors
113  */
114 
115 #define FLOAT_VECT2_ZERO(_v) VECT2_ASSIGN(_v, 0., 0.)
116 
117 /* macros also usable if _v is not a FloatVect2, but a different struct with x,y members */
118 #define FLOAT_VECT2_NORM(_v) sqrtf(VECT2_NORM2(_v))
119 
120 static inline float float_vect2_norm2(struct FloatVect2 *v)
121 {
122  return v->x * v->x + v->y * v->y;
123 }
124 
125 static inline float float_vect2_norm(struct FloatVect2 *v)
126 {
127  return sqrtf(float_vect2_norm2(v));
128 }
129 
131 static inline void float_vect2_normalize(struct FloatVect2 *v)
132 {
133  const float n = float_vect2_norm(v);
134  if (n > 0) {
135  v->x /= n;
136  v->y /= n;
137  }
138 }
139 
140 #define FLOAT_VECT2_NORMALIZE(_v) float_vect2_normalize(&(_v))
141 
142 
143 /*
144  * Dimension 3 Vectors
145  */
146 
147 #define FLOAT_VECT3_ZERO(_v) VECT3_ASSIGN(_v, 0., 0., 0.)
148 
149 /* macros also usable if _v is not a FloatVect3, but a different struct with x,y,z members */
150 #define FLOAT_VECT3_NORM(_v) sqrtf(VECT3_NORM2(_v))
151 
152 static inline float float_vect3_norm2(struct FloatVect3 *v)
153 {
154  return v->x * v->x + v->y * v->y + v->z * v->z;
155 }
156 
157 static inline float float_vect3_norm(struct FloatVect3 *v)
158 {
159  return sqrtf(float_vect3_norm2(v));
160 }
161 
163 static inline void float_vect3_normalize(struct FloatVect3 *v)
164 {
165  const float n = float_vect3_norm(v);
166  if (n > 0) {
167  v->x /= n;
168  v->y /= n;
169  v->z /= n;
170  }
171 }
172 
173 #define FLOAT_VECT3_NORMALIZE(_v) float_vect3_normalize(&(_v))
174 
175 
176 
177 #define FLOAT_RATES_ZERO(_r) { \
178  RATES_ASSIGN(_r, 0., 0., 0.); \
179  }
180 
181 #define FLOAT_RATES_NORM(_v) (sqrtf((_v).p*(_v).p + (_v).q*(_v).q + (_v).r*(_v).r))
182 
183 #define FLOAT_RATES_LIN_CMB(_ro, _r1, _s1, _r2, _s2) { \
184  _ro.p = _s1 * _r1.p + _s2 * _r2.p; \
185  _ro.q = _s1 * _r1.q + _s2 * _r2.q; \
186  _ro.r = _s1 * _r1.r + _s2 * _r2.r; \
187  }
188 
189 
190 extern void float_vect3_integrate_fi(struct FloatVect3 *vec, struct FloatVect3 *dv,
191  float dt);
192 
193 extern void float_rates_integrate_fi(struct FloatRates *r, struct FloatRates *dr,
194  float dt);
195 
196 extern void float_rates_of_euler_dot(struct FloatRates *r, struct FloatEulers *e,
197  struct FloatEulers *edot);
198 
199 /* defines for backwards compatibility */
200 #define FLOAT_VECT3_INTEGRATE_FI(_vo, _dv, _dt) WARNING("FLOAT_VECT3_INTEGRATE_FI macro is deprecated, use the lower case function instead") float_vect3_integrate_fi(&(_vo), &(_dv), _dt)
201 #define FLOAT_RATES_INTEGRATE_FI(_ra, _racc, _dt) WARNING("FLOAT_RATES_INTEGRATE_FI macro is deprecated, use the lower case function instead") float_rates_integrate_fi(&(_ra), &(_racc), _dt)
202 #define FLOAT_RATES_OF_EULER_DOT(_ra, _e, _ed) WARNING("FLOAT_RATES_OF_EULER_DOT macro is deprecated, use the lower case function instead") float_rates_of_euler_dot(&(_ra), &(_e), &(_ed))
203 
204 
205 /*
206  * 3x3 matrices
207  */
208 #define FLOAT_MAT33_ZERO(_m) { \
209  MAT33_ELMT(_m, 0, 0) = 0.; \
210  MAT33_ELMT(_m, 0, 1) = 0.; \
211  MAT33_ELMT(_m, 0, 2) = 0.; \
212  MAT33_ELMT(_m, 1, 0) = 0.; \
213  MAT33_ELMT(_m, 1, 1) = 0.; \
214  MAT33_ELMT(_m, 1, 2) = 0.; \
215  MAT33_ELMT(_m, 2, 0) = 0.; \
216  MAT33_ELMT(_m, 2, 1) = 0.; \
217  MAT33_ELMT(_m, 2, 2) = 0.; \
218  }
219 
220 #define FLOAT_MAT33_DIAG(_m, _d00, _d11, _d22) { \
221  MAT33_ELMT(_m, 0, 0) = _d00; \
222  MAT33_ELMT(_m, 0, 1) = 0.; \
223  MAT33_ELMT(_m, 0, 2) = 0.; \
224  MAT33_ELMT(_m, 1, 0) = 0.; \
225  MAT33_ELMT(_m, 1, 1) = _d11; \
226  MAT33_ELMT(_m, 1, 2) = 0.; \
227  MAT33_ELMT(_m, 2, 0) = 0.; \
228  MAT33_ELMT(_m, 2, 1) = 0.; \
229  MAT33_ELMT(_m, 2, 2) = _d22; \
230  }
231 
232 
233 //
234 //
235 // Rotation Matrices
236 //
237 //
238 
239 
241 static inline void float_rmat_identity(struct FloatRMat *rm)
242 {
243  FLOAT_MAT33_DIAG(*rm, 1., 1., 1.);
244 }
245 
249 extern void float_rmat_inv(struct FloatRMat *m_b2a, struct FloatRMat *m_a2b);
250 
254 extern void float_rmat_comp(struct FloatRMat *m_a2c, struct FloatRMat *m_a2b,
255  struct FloatRMat *m_b2c);
256 
260 extern void float_rmat_comp_inv(struct FloatRMat *m_a2b, struct FloatRMat *m_a2c,
261  struct FloatRMat *m_b2c);
262 
264 extern float float_rmat_norm(struct FloatRMat *rm);
265 
269 extern void float_rmat_vmult(struct FloatVect3 *vb, struct FloatRMat *m_a2b,
270  struct FloatVect3 *va);
271 
275 extern void float_rmat_transp_vmult(struct FloatVect3 *vb, struct FloatRMat *m_b2a,
276  struct FloatVect3 *va);
277 
281 extern void float_rmat_mult(struct FloatEulers *rb, struct FloatRMat *m_a2b,
282  struct FloatEulers *ra);
283 
287 extern void float_rmat_transp_mult(struct FloatEulers *rb, struct FloatRMat *m_b2a,
288  struct FloatEulers *ra);
289 
293 extern void float_rmat_ratemult(struct FloatRates *rb, struct FloatRMat *m_a2b,
294  struct FloatRates *ra);
295 
299 extern void float_rmat_transp_ratemult(struct FloatRates *rb, struct FloatRMat *m_b2a,
300  struct FloatRates *ra);
301 
303 extern void float_rmat_of_axis_angle(struct FloatRMat *rm, struct FloatVect3 *uv, float angle);
304 
317 extern void float_rmat_of_eulers_321(struct FloatRMat *rm, struct FloatEulers *e);
318 extern void float_rmat_of_eulers_312(struct FloatRMat *rm, struct FloatEulers *e);
319 #define float_rmat_of_eulers float_rmat_of_eulers_321
320 
321 extern void float_rmat_of_quat(struct FloatRMat *rm, struct FloatQuat *q);
323 extern void float_rmat_integrate_fi(struct FloatRMat *rm, struct FloatRates *omega, float dt);
324 extern float float_rmat_reorthogonalize(struct FloatRMat *rm);
325 
326 /* defines for backwards compatibility */
327 #define FLOAT_RMAT_INV(_m_b2a, _m_a2b) WARNING("FLOAT_RMAT_INV macro is deprecated, use the lower case function instead") float_rmat_inv(&(_m_b2a), &(_m_a2b))
328 #define FLOAT_RMAT_NORM(_m) WARNING("FLOAT_RMAT_NORM macro is deprecated, use the lower case function instead") float_rmat_norm(&(_m))
329 #define FLOAT_RMAT_COMP(_m_a2c, _m_a2b, _m_b2c) WARNING("FLOAT_RMAT_COMP macro is deprecated, use the lower case function instead") float_rmat_comp(&(_m_a2c), &(_m_a2b), &(_m_b2c))
330 #define FLOAT_RMAT_COMP_INV(_m_a2b, _m_a2c, _m_b2c) WARNING("FLOAT_RMAT_COMP_INV macro is deprecated, use the lower case function instead") float_rmat_comp_inv(&(_m_a2b), &(_m_a2c), &(_m_b2c))
331 #define FLOAT_RMAT_VMULT(_vb, _m_a2b, _va) WARNING("FLOAT_RMAT_VMULT macro is deprecated, use the lower case function instead") float_rmat_vmult(&(_vb), &(_m_a2b), &(_va))
332 #define FLOAT_RMAT_TRANSP_VMULT(_vb, _m_b2a, _va) WARNING("FLOAT_RMAT_TRANSP_VMULT macro is deprecated, use the lower case function instead") float_rmat_transp_vmult(&(_vb), &(_m_b2a), &(_va))
333 #define FLOAT_RMAT_RATEMULT(_rb, _m_a2b, _ra) WARNING("FLOAT_RMAT_RATEMULT macro is deprecated, use the lower case function instead") float_rmat_ratemult(&(_rb), &(_m_a2b), &(_ra))
334 #define FLOAT_RMAT_TRANSP_RATEMULT(_rb, _m_b2a, _ra) WARNING("FLOAT_RMAT_TRANSP_RATEMULT macro is deprecated, use the lower case function instead") float_rmat_ratemult(&(_rb), &(_m_b2a), &(_ra))
335 #define FLOAT_RMAT_OF_AXIS_ANGLE(_rm, _uv, _an) WARNING("FLOAT_RMAT_OF_AXIS_ANGLE macro is deprecated, use the lower case function instead") float_rmat_of_axis_angle(&(_rm), &(_uv), _an)
336 #define FLOAT_RMAT_OF_EULERS(_rm, _e) WARNING("FLOAT_RMAT_OF_EULERS macro is deprecated, use the lower case function instead") float_rmat_of_eulers_321(&(_rm), &(_e))
337 #define FLOAT_RMAT_OF_EULERS_321(_rm, _e) WARNING("FLOAT_RMAT_OF_EULERS_321 macro is deprecated, use the lower case function instead") float_rmat_of_eulers_321(&(_rm), &(_e))
338 #define FLOAT_RMAT_OF_EULERS_312(_rm, _e) WARNING("FLOAT_RMAT_OF_EULERS_312 macro is deprecated, use the lower case function instead") float_rmat_of_eulers_312(&(_rm), &(_e))
339 #define FLOAT_RMAT_OF_QUAT(_rm, _q) WARNING("FLOAT_RMAT_OF_QUAT macro is deprecated, use the lower case function instead") float_rmat_of_quat(&(_rm), &(_q))
340 #define FLOAT_RMAT_INTEGRATE_FI(_rm, _omega, _dt) WARNING("FLOAT_RMAT_INTEGRATE_FI macro is deprecated, use the lower case function instead") float_rmat_integrate_fi(&(_rm), &(_omega), &(_dt))
341 
342 
343 
344 //
345 //
346 // Quaternion algebras
347 //
348 //
349 
351 static inline void float_quat_identity(struct FloatQuat *q)
352 {
353  q->qi = 1.0;
354  q->qx = 0;
355  q->qy = 0;
356  q->qz = 0;
357 }
358 
359 #define FLOAT_QUAT_NORM2(_q) (SQUARE((_q).qi) + SQUARE((_q).qx) + SQUARE((_q).qy) + SQUARE((_q).qz))
360 
361 static inline float float_quat_norm(struct FloatQuat *q)
362 {
363  return sqrtf(SQUARE(q->qi) + SQUARE(q->qx) + SQUARE(q->qy) + SQUARE(q->qz));
364 }
365 
366 static inline void float_quat_normalize(struct FloatQuat *q)
367 {
368  float qnorm = float_quat_norm(q);
369  if (qnorm > FLT_MIN) {
370  q->qi = q->qi / qnorm;
371  q->qx = q->qx / qnorm;
372  q->qy = q->qy / qnorm;
373  q->qz = q->qz / qnorm;
374  }
375 }
376 
377 static inline void float_quat_invert(struct FloatQuat *qo, struct FloatQuat *qi)
378 {
379  QUAT_INVERT(*qo, *qi);
380 }
381 
382 static inline void float_quat_wrap_shortest(struct FloatQuat *q)
383 {
384  if (q->qi < 0.) {
385  QUAT_EXPLEMENTARY(*q, *q);
386  }
387 }
388 
389 #define FLOAT_QUAT_EXTRACT(_vo, _qi) QUAT_EXTRACT_Q(_vo, _qi)
390 
391 
395 extern void float_quat_comp(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c);
396 
400 extern void float_quat_comp_inv(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c);
401 
405 extern void float_quat_inv_comp(struct FloatQuat *b2c, struct FloatQuat *a2b, struct FloatQuat *a2c);
406 
410 extern void float_quat_comp_norm_shortest(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c);
411 
415 extern void float_quat_comp_inv_norm_shortest(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c);
416 
420 extern void float_quat_inv_comp_norm_shortest(struct FloatQuat *b2c, struct FloatQuat *a2b, struct FloatQuat *a2c);
421 
427 extern void float_quat_derivative(struct FloatQuat *qd, struct FloatRates *r, struct FloatQuat *q);
428 
434 extern void float_quat_derivative_lagrange(struct FloatQuat *qd, struct FloatRates *r, struct FloatQuat *q);
435 
438 extern void float_quat_differential(struct FloatQuat *q_out, struct FloatRates *w, float dt);
439 
441 extern void float_quat_integrate_fi(struct FloatQuat *q, struct FloatRates *omega, float dt);
442 
444 extern void float_quat_integrate(struct FloatQuat *q, struct FloatRates *omega, float dt);
445 
449 extern void float_quat_vmult(struct FloatVect3 *v_out, struct FloatQuat *q, const struct FloatVect3 *v_in);
450 
452 extern void float_quat_of_eulers(struct FloatQuat *q, struct FloatEulers *e);
453 
455 extern void float_quat_of_axis_angle(struct FloatQuat *q, const struct FloatVect3 *uv, float angle);
456 
460 extern void float_quat_of_orientation_vect(struct FloatQuat *q, const struct FloatVect3 *ov);
461 
463 extern void float_quat_of_rmat(struct FloatQuat *q, struct FloatRMat *rm);
464 
465 
466 /* defines for backwards compatibility */
467 #define FLOAT_QUAT_ZERO(_q) WARNING("FLOAT_QUAT_ZERO macro is deprecated, use the lower case function instead") float_quat_identity(&(_q))
468 #define FLOAT_QUAT_INVERT(_qo, _qi) WARNING("FLOAT_QUAT_INVERT macro is deprecated, use the lower case function instead") float_quat_invert(&(_qo), &(_qi))
469 #define FLOAT_QUAT_WRAP_SHORTEST(_q) WARNING("FLOAT_QUAT_WRAP_SHORTEST macro is deprecated, use the lower case function instead") float_quat_wrap_shortest(&(_q))
470 #define FLOAT_QUAT_NORM(_q) WARNING("FLOAT_QUAT_NORM macro is deprecated, use the lower case function instead") float_quat_norm(&(_q))
471 #define FLOAT_QUAT_NORMALIZE(_q) WARNING("FLOAT_QUAT_NORMALIZE macro is deprecated, use the lower case function instead") float_quat_normalize(&(_q))
472 #define FLOAT_QUAT_COMP(_a2c, _a2b, _b2c) WARNING("FLOAT_QUAT_COMP macro is deprecated, use the lower case function instead") float_quat_comp(&(_a2c), &(_a2b), &(_b2c))
473 #define FLOAT_QUAT_MULT(_a2c, _a2b, _b2c) WARNING("FLOAT_QUAT_MULT macro is deprecated, use the lower case function instead") float_quat_comp(&(_a2c), &(_a2b), &(_b2c))
474 #define FLOAT_QUAT_INV_COMP(_b2c, _a2b, _a2c) WARNING("FLOAT_QUAT_INV_COMP macro is deprecated, use the lower case function instead") float_quat_inv_comp(&(_b2c), &(_a2b), &(_a2c))
475 #define FLOAT_QUAT_COMP_INV(_a2b, _a2c, _b2c) WARNING("FLOAT_QUAT_COMP_INV macro is deprecated, use the lower case function instead") float_quat_comp_inv(&(_a2b), &(_a2c), &(_b2c))
476 #define FLOAT_QUAT_COMP_NORM_SHORTEST(_a2c, _a2b, _b2c) WARNING("FLOAT_QUAT_COMP_NORM_SHORTEST macro is deprecated, use the lower case function instead") float_quat_comp_norm_shortest(&(_a2c), &(_a2b), &(_b2c))
477 #define FLOAT_QUAT_COMP_INV_NORM_SHORTEST(_a2b, _a2c, _b2c) WARNING("FLOAT_QUAT_COMP_INV_NORM_SHORTEST macro is deprecated, use the lower case function instead") float_quat_comp_inv_norm_shortest(&(_a2b), &(_a2c), &(_b2c))
478 #define FLOAT_QUAT_INV_COMP_NORM_SHORTEST(_b2c, _a2b, _a2c) WARNING("FLOAT_QUAT_INV_COMP_NORM_SHORTEST macro is deprecated, use the lower case function instead") float_quat_inv_comp_norm_shortest(&(_b2c), &(_a2b), &(_a2c))
479 #define FLOAT_QUAT_DIFFERENTIAL(q_out, w, dt) WARNING("FLOAT_QUAT_DIFFERENTIAL macro is deprecated, use the lower case function instead") float_quat_differential(&(q_out), &(w), dt)
480 #define FLOAT_QUAT_INTEGRATE(_q, _omega, _dt) WARNING("FLOAT_QUAT_INTEGRATE macro is deprecated, use the lower case function instead") float_quat_integrate(&(_q), &(_omega), _dt)
481 #define FLOAT_QUAT_VMULT(v_out, q, v_in) WARNING("FLOAT_QUAT_VMULT macro is deprecated, use the lower case function instead") float_quat_vmult(&(v_out), &(q), &(v_in))
482 #define FLOAT_QUAT_DERIVATIVE(_qd, _r, _q) WARNING("FLOAT_QUAT_DERIVATIVE macro is deprecated, use the lower case function instead") float_quat_derivative(&(_qd), &(_r), &(_q))
483 #define FLOAT_QUAT_DERIVATIVE_LAGRANGE(_qd, _r, _q) WARNING("FLOAT_QUAT_DERIVATIVE_LAGRANGE macro is deprecated, use the lower case function instead") float_quat_derivative_lagrange(&(_qd), &(_r), &(_q))
484 #define FLOAT_QUAT_OF_EULERS(_q, _e) WARNING("FLOAT_QUAT_OF_EULERS macro is deprecated, use the lower case function instead") float_quat_of_eulers(&(_q), &(_e))
485 #define FLOAT_QUAT_OF_AXIS_ANGLE(_q, _uv, _an) WARNING("FLOAT_QUAT_OF_AXIS_ANGLE macro is deprecated, use the lower case function instead") float_quat_of_axis_angle(&(_q), &(_uv), _an)
486 #define FLOAT_QUAT_OF_ORIENTATION_VECT(_q, _ov) WARNING("FLOAT_QUAT_OF_ORIENTATION_VECT macro is deprecated, use the lower case function instead") float_quat_of_orientation_vect(&(_q), &(_ov))
487 #define FLOAT_QUAT_OF_RMAT(_q, _r) WARNING("FLOAT_QUAT_OF_RMAT macro is deprecated, use the lower case function instead") float_quat_of_rmat(&(_q), &(_r))
488 
489 
490 
491 //
492 //
493 // Euler angles
494 //
495 //
496 
497 #define FLOAT_EULERS_ZERO(_e) EULERS_ASSIGN(_e, 0., 0., 0.);
498 
499 static inline float float_eulers_norm(struct FloatEulers *e)
500 {
501  return sqrtf(SQUARE(e->phi) + SQUARE(e->theta) + SQUARE(e->psi));
502 }
503 extern void float_eulers_of_rmat(struct FloatEulers *e, struct FloatRMat *rm);
504 extern void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q);
505 
506 /* defines for backwards compatibility */
507 #define FLOAT_EULERS_OF_RMAT(_e, _rm) WARNING("FLOAT_EULERS_OF_RMAT macro is deprecated, use the lower case function instead") float_eulers_of_rmat(&(_e), &(_rm))
508 #define FLOAT_EULERS_OF_QUAT(_e, _q) WARNING("FLOAT_EULERS_OF_QUAT macro is deprecated, use the lower case function instead") float_eulers_of_quat(&(_e), &(_q))
509 #define FLOAT_EULERS_NORM(_e) WARNING("FLOAT_EULERS_NORM macro is deprecated, use the lower case function instead") float_eulers_norm(&(_e))
510 
511 //
512 //
513 // Generic vector algebra
514 //
515 //
516 
518 static inline void float_vect_zero(float *a, const int n)
519 {
520  int i;
521  for (i = 0; i < n; i++) { a[i] = 0.; }
522 }
523 
525 static inline void float_vect_copy(float *a, const float *b, const int n)
526 {
527  int i;
528  for (i = 0; i < n; i++) { a[i] = b[i]; }
529 }
530 
532 static inline void float_vect_sum(float *o, const float *a, const float *b, const int n)
533 {
534  int i;
535  for (i = 0; i < n; i++) { o[i] = a[i] + b[i]; }
536 }
537 
539 static inline void float_vect_diff(float *o, const float *a, const float *b, const int n)
540 {
541  int i;
542  for (i = 0; i < n; i++) { o[i] = a[i] - b[i]; }
543 }
544 
546 static inline void float_vect_mul(float *o, const float *a, const float *b, const int n)
547 {
548  int i;
549  for (i = 0; i < n; i++) { o[i] = a[i] * b[i]; }
550 }
551 
553 static inline void float_vect_add(float *a, const float *b, const int n)
554 {
555  int i;
556  for (i = 0; i < n; i++) { a[i] += b[i]; }
557 }
558 
560 static inline void float_vect_sub(float *a, const float *b, const int n)
561 {
562  int i;
563  for (i = 0; i < n; i++) { a[i] -= b[i]; }
564 }
565 
567 static inline void float_vect_smul(float *o, const float *a, const float s, const int n)
568 {
569  int i;
570  for (i = 0; i < n; i++) { o[i] = a[i] * s; }
571 }
572 
574 static inline void float_vect_sdiv(float *o, const float *a, const float s, const int n)
575 {
576  int i;
577  if (fabs(s) > 1e-5) {
578  for (i = 0; i < n; i++) { o[i] = a[i] / s; }
579  }
580 }
581 
583 static inline float float_vect_norm(const float *a, const int n)
584 {
585  int i;
586  float sum = 0;
587  for (i = 0; i < n; i++) { sum += a[i] * a[i]; }
588  return sqrtf(sum);
589 }
590 
592 static inline void float_vect_scale(float *a, const float s, const int n)
593 {
594  int i;
595  for (i = 0; i < n; i++) { a[i] *= s; }
596 }
597 
599 static inline float float_vect_dot_product(const float *a, const float *b, const int n)
600 {
601  int i;
602  float dot = 0.f;
603  for (i = 0; i < n; i++) { dot += a[i] * b[i]; }
604  return dot;
605 }
606 
607 //
608 //
609 // Generic matrix algebra
610 //
611 //
612 
614 #define MAKE_MATRIX_PTR(_ptr, _mat, _rows) \
615  float * _ptr[_rows]; \
616  { \
617  int __i; \
618  for (__i = 0; __i < _rows; __i++) { _ptr[__i] = &_mat[__i][0]; } \
619  }
620 
622 static inline void float_mat_zero(float **a, int m, int n)
623 {
624  int i, j;
625  for (i = 0; i < m; i++) {
626  for (j = 0; j < n; j++) { a[i][j] = 0.; }
627  }
628 }
629 
631 static inline void float_mat_copy(float **a, float **b, int m, int n)
632 {
633  int i, j;
634  for (i = 0; i < m; i++) {
635  for (j = 0; j < n; j++) { a[i][j] = b[i][j]; }
636  }
637 }
638 
640 static inline void float_mat_sum(float **o, float **a, float **b, int m, int n)
641 {
642  int i, j;
643  for (i = 0; i < m; i++) {
644  for (j = 0; j < n; j++) { o[i][j] = a[i][j] + b[i][j]; }
645  }
646 }
647 
649 static inline void float_mat_diff(float **o, float **a, float **b, int m, int n)
650 {
651  int i, j;
652  for (i = 0; i < m; i++) {
653  for (j = 0; j < n; j++) { o[i][j] = a[i][j] - b[i][j]; }
654  }
655 }
656 
658 static inline void float_mat_transpose(float **a, int n)
659 {
660  int i, j;
661  for (i = 0; i < n; i++) {
662  for (j = 0; j < i; j++) {
663  float t = a[i][j];
664  a[i][j] = a[j][i];
665  a[j][i] = t;
666  }
667  }
668 }
669 
676 static inline void float_mat_mul(float **o, float **a, float **b, int m, int n, int l)
677 {
678  int i, j, k;
679  for (i = 0; i < m; i++) {
680  for (j = 0; j < l; j++) {
681  o[i][j] = 0.;
682  for (k = 0; k < n; k++) {
683  o[i][j] += a[i][k] * b[k][j];
684  }
685  }
686  }
687 }
688 
695 static inline void float_mat_minor(float **o, float **a, int m, int n, int d)
696 {
697  int i, j;
698  float_mat_zero(o, m, n);
699  for (i = 0; i < d; i++) { o[i][i] = 1.0; }
700  for (i = d; i < m; i++) {
701  for (j = d; j < n; j++) {
702  o[i][j] = a[i][j];
703  }
704  }
705 }
706 
708 static inline void float_mat_vmul(float **o, float *v, int n)
709 {
710  int i, j;
711  for (i = 0; i < n; i++) {
712  for (j = 0; j < n; j++) {
713  o[i][j] = -2. * v[i] * v[j];
714  }
715  }
716  for (i = 0; i < n; i++) {
717  o[i][i] += 1.;
718  }
719 }
720 
722 static inline void float_mat_col(float *o, float **a, int m, int c)
723 {
724  int i;
725  for (i = 0; i < m; i++) {
726  o[i] = a[i][c];
727  }
728 }
729 
730 extern void float_mat_inv_4d(float invOut[16], float mat_in[16]);
731 
732 #ifdef __cplusplus
733 } /* extern "C" */
734 #endif
735 
736 #endif /* PPRZ_ALGEBRA_FLOAT_H */
737 
static float float_eulers_norm(struct FloatEulers *e)
void float_quat_comp_inv(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions.
static void float_vect_sum(float *o, const float *a, const float *b, const int n)
o = a + b
static void float_mat_transpose(float **a, int n)
transpose square matrix
void float_rmat_inv(struct FloatRMat *m_b2a, struct FloatRMat *m_a2b)
Inverse/transpose of a rotation matrix.
static float float_vect_norm(const float *a, const int n)
||a||
void float_quat_of_eulers(struct FloatQuat *q, struct FloatEulers *e)
Quaternion from Euler angles.
float phi
in radians
void float_rmat_mult(struct FloatEulers *rb, struct FloatRMat *m_a2b, struct FloatEulers *ra)
rotate angle by rotation matrix.
void float_quat_comp(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions.
void float_vect3_integrate_fi(struct FloatVect3 *vec, struct FloatVect3 *dv, float dt)
in place first order integration of a 3D-vector
static void float_mat_copy(float **a, float **b, int m, int n)
a = b
static void float_vect_add(float *a, const float *b, const int n)
a += b
void float_eulers_of_rmat(struct FloatEulers *e, struct FloatRMat *rm)
static void float_mat_col(float *o, float **a, int m, int c)
o = c-th column of matrix a[m x n]
static void float_quat_identity(struct FloatQuat *q)
initialises a quaternion to identity
#define QUAT_INVERT(_qo, _qi)
Definition: pprz_algebra.h:574
float r
in rad/s
static void float_vect2_normalize(struct FloatVect2 *v)
normalize 2D vector in place
float float_rmat_norm(struct FloatRMat *rm)
Norm of a rotation matrix.
#define QUAT_EXPLEMENTARY(b, a)
Definition: pprz_algebra.h:550
float psi
in radians
#define FLOAT_MAT33_DIAG(_m, _d00, _d11, _d22)
static void float_mat_diff(float **o, float **a, float **b, int m, int n)
o = a - b
void float_rmat_comp_inv(struct FloatRMat *m_a2b, struct FloatRMat *m_a2c, struct FloatRMat *m_b2c)
Composition (multiplication) of two rotation matrices.
static void float_vect_sub(float *a, const float *b, const int n)
a -= b
static void float_mat_vmul(float **o, float *v, int n)
o = I - v v^T
#define SQUARE(_a)
Definition: pprz_algebra.h:47
void float_mat_inv_4d(float invOut[16], float mat_in[16])
4x4 Matrix inverse
static void float_rmat_identity(struct FloatRMat *rm)
initialises a rotation matrix to identity
float q
in rad/s
float p
in rad/s
void float_quat_inv_comp(struct FloatQuat *b2c, struct FloatQuat *a2b, struct FloatQuat *a2c)
Composition (multiplication) of two quaternions.
void float_rmat_transp_mult(struct FloatEulers *rb, struct FloatRMat *m_b2a, struct FloatEulers *ra)
rotate angle by transposed rotation matrix.
static void float_vect_sdiv(float *o, const float *a, const float s, const int n)
o = a / s
static float float_vect3_norm2(struct FloatVect3 *v)
euler angles
static void float_mat_minor(float **o, float **a, int m, int n, int d)
matrix minor
static void float_vect3_normalize(struct FloatVect3 *v)
normalize 3D vector in place
Roation quaternion.
void float_rmat_integrate_fi(struct FloatRMat *rm, struct FloatRates *omega, float dt)
in place first order integration of a rotation matrix
static float float_vect3_norm(struct FloatVect3 *v)
void float_rmat_of_eulers_312(struct FloatRMat *rm, struct FloatEulers *e)
float theta
in radians
void float_rmat_vmult(struct FloatVect3 *vb, struct FloatRMat *m_a2b, struct FloatVect3 *va)
rotate 3D vector by rotation matrix.
void float_rmat_comp(struct FloatRMat *m_a2c, struct FloatRMat *m_a2b, struct FloatRMat *m_b2c)
Composition (multiplication) of two rotation matrices.
void float_rmat_transp_vmult(struct FloatVect3 *vb, struct FloatRMat *m_b2a, struct FloatVect3 *va)
rotate 3D vector by transposed rotation matrix.
static void float_vect_mul(float *o, const float *a, const float *b, const int n)
o = a * b (element wise)
static float float_vect2_norm(struct FloatVect2 *v)
void float_rates_integrate_fi(struct FloatRates *r, struct FloatRates *dr, float dt)
in place first order integration of angular rates
void float_rmat_of_quat(struct FloatRMat *rm, struct FloatQuat *q)
Paparazzi generic algebra macros.
void float_quat_integrate(struct FloatQuat *q, struct FloatRates *omega, float dt)
in place quaternion integration with constant rotational velocity
static void float_vect_zero(float *a, const int n)
a = 0
static void float_mat_zero(float **a, int m, int n)
a = 0
void float_quat_vmult(struct FloatVect3 *v_out, struct FloatQuat *q, const struct FloatVect3 *v_in)
rotate 3D vector by quaternion.
static void float_quat_normalize(struct FloatQuat *q)
void float_quat_comp_inv_norm_shortest(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions with normalization.
static void float_quat_invert(struct FloatQuat *qo, struct FloatQuat *qi)
void float_quat_of_rmat(struct FloatQuat *q, struct FloatRMat *rm)
Quaternion from rotation matrix.
static void float_vect_smul(float *o, const float *a, const float s, const int n)
o = a * s
static void float_vect_diff(float *o, const float *a, const float *b, const int n)
o = a - b
void float_rates_of_euler_dot(struct FloatRates *r, struct FloatEulers *e, struct FloatEulers *edot)
void float_quat_derivative(struct FloatQuat *qd, struct FloatRates *r, struct FloatQuat *q)
Quaternion derivative from rotational velocity.
float m[3 *3]
float float_rmat_reorthogonalize(struct FloatRMat *rm)
void float_rmat_transp_ratemult(struct FloatRates *rb, struct FloatRMat *m_b2a, struct FloatRates *ra)
rotate anglular rates by transposed rotation matrix.
void float_rmat_of_eulers_321(struct FloatRMat *rm, struct FloatEulers *e)
Rotation matrix from 321 Euler angles (float).
void float_rmat_of_axis_angle(struct FloatRMat *rm, struct FloatVect3 *uv, float angle)
initialises a rotation matrix from unit vector axis and angle
void float_quat_of_orientation_vect(struct FloatQuat *q, const struct FloatVect3 *ov)
Quaternion from orientation vector.
void float_quat_of_axis_angle(struct FloatQuat *q, const struct FloatVect3 *uv, float angle)
Quaternion from unit vector and angle.
static void float_quat_wrap_shortest(struct FloatQuat *q)
void float_quat_comp_norm_shortest(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions with normalization.
void float_quat_inv_comp_norm_shortest(struct FloatQuat *b2c, struct FloatQuat *a2b, struct FloatQuat *a2c)
Composition (multiplication) of two quaternions with normalization.
static void float_vect_scale(float *a, const float s, const int n)
a *= s
static void float_vect_copy(float *a, const float *b, const int n)
a = b
void float_quat_derivative_lagrange(struct FloatQuat *qd, struct FloatRates *r, struct FloatQuat *q)
Quaternion derivative from rotational velocity with Lagrange multiplier.
rotation matrix
static void float_mat_sum(float **o, float **a, float **b, int m, int n)
o = a + b
float m[3 *3]
static float float_quat_norm(struct FloatQuat *q)
void float_rmat_ratemult(struct FloatRates *rb, struct FloatRMat *m_a2b, struct FloatRates *ra)
rotate anglular rates by rotation matrix.
void float_quat_differential(struct FloatQuat *q_out, struct FloatRates *w, float dt)
Delta rotation quaternion with constant angular rates.
static float float_vect2_norm2(struct FloatVect2 *v)
void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q)
void float_quat_integrate_fi(struct FloatQuat *q, struct FloatRates *omega, float dt)
in place first order quaternion integration with constant rotational velocity
angular rates
static void float_mat_mul(float **o, float **a, float **b, int m, int n, int l)
o = a * b
static float float_vect_dot_product(const float *a, const float *b, const int n)
a.b