pprz_algebra_float.c 35.4 KB
Newer Older
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
/*
 * Copyright (C) 2008-2014 The Paparazzi Team
 *
 * This file is part of paparazzi.
 *
 * paparazzi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * paparazzi 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with paparazzi; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/**
 * @file pprz_algebra_float.c
 * @brief Paparazzi floating point algebra.
 *
 */

#include "pprz_algebra_float.h"

29
/** in place first order integration of a 3D-vector */
30
void float_vect3_integrate_fi(struct FloatVect3 *vec, struct FloatVect3 *dv, float dt)
31 32 33 34 35 36 37
{
  vec->x += dv->x * dt;
  vec->y += dv->y * dt;
  vec->z += dv->z * dt;
}

/** in place first order integration of angular rates */
38
void float_rates_integrate_fi(struct FloatRates *r, struct FloatRates *dr, float dt)
39 40 41 42 43 44
{
  r->p += dr->p * dt;
  r->q += dr->q * dt;
  r->r += dr->r * dt;
}

45
void float_rates_of_euler_dot(struct FloatRates *r, struct FloatEulers *e, struct FloatEulers *edot)
46 47 48 49 50 51
{
  r->p = edot->phi                             -                sinf(e->theta) * edot->psi;
  r->q =            cosf(e->phi) * edot->theta + sinf(e->phi) * cosf(e->theta) * edot->psi;
  r->r =           -sinf(e->phi) * edot->theta + cosf(e->phi) * cosf(e->theta) * edot->psi;
}

52 53


Felix Ruess's avatar
Felix Ruess committed
54

55
void float_rmat_inv(struct FloatRMat *m_b2a, struct FloatRMat *m_a2b)
Felix Ruess's avatar
Felix Ruess committed
56
{
57 58 59 60 61 62 63 64 65 66
  RMAT_ELMT(*m_b2a, 0, 0) = RMAT_ELMT(*m_a2b, 0, 0);
  RMAT_ELMT(*m_b2a, 0, 1) = RMAT_ELMT(*m_a2b, 1, 0);
  RMAT_ELMT(*m_b2a, 0, 2) = RMAT_ELMT(*m_a2b, 2, 0);
  RMAT_ELMT(*m_b2a, 1, 0) = RMAT_ELMT(*m_a2b, 0, 1);
  RMAT_ELMT(*m_b2a, 1, 1) = RMAT_ELMT(*m_a2b, 1, 1);
  RMAT_ELMT(*m_b2a, 1, 2) = RMAT_ELMT(*m_a2b, 2, 1);
  RMAT_ELMT(*m_b2a, 2, 0) = RMAT_ELMT(*m_a2b, 0, 2);
  RMAT_ELMT(*m_b2a, 2, 1) = RMAT_ELMT(*m_a2b, 1, 2);
  RMAT_ELMT(*m_b2a, 2, 2) = RMAT_ELMT(*m_a2b, 2, 2);
}
67

68
float float_rmat_norm(struct FloatRMat *rm)
69
{
70 71 72
  return sqrtf(SQUARE(rm->m[0]) + SQUARE(rm->m[1]) + SQUARE(rm->m[2]) +
               SQUARE(rm->m[3]) + SQUARE(rm->m[4]) + SQUARE(rm->m[5]) +
               SQUARE(rm->m[6]) + SQUARE(rm->m[7]) + SQUARE(rm->m[8]));
73 74
}

75
/** Composition (multiplication) of two rotation matrices.
Felix Ruess's avatar
Felix Ruess committed
76
 * m_a2c = m_a2b comp m_b2c , aka  m_a2c = m_b2c * m_a2b
77
 */
78
void float_rmat_comp(struct FloatRMat *m_a2c, struct FloatRMat *m_a2b, struct FloatRMat *m_b2c)
Felix Ruess's avatar
Felix Ruess committed
79
{
80 81 82 83 84 85 86 87 88 89 90
  m_a2c->m[0] = m_b2c->m[0] * m_a2b->m[0] + m_b2c->m[1] * m_a2b->m[3] + m_b2c->m[2] * m_a2b->m[6];
  m_a2c->m[1] = m_b2c->m[0] * m_a2b->m[1] + m_b2c->m[1] * m_a2b->m[4] + m_b2c->m[2] * m_a2b->m[7];
  m_a2c->m[2] = m_b2c->m[0] * m_a2b->m[2] + m_b2c->m[1] * m_a2b->m[5] + m_b2c->m[2] * m_a2b->m[8];
  m_a2c->m[3] = m_b2c->m[3] * m_a2b->m[0] + m_b2c->m[4] * m_a2b->m[3] + m_b2c->m[5] * m_a2b->m[6];
  m_a2c->m[4] = m_b2c->m[3] * m_a2b->m[1] + m_b2c->m[4] * m_a2b->m[4] + m_b2c->m[5] * m_a2b->m[7];
  m_a2c->m[5] = m_b2c->m[3] * m_a2b->m[2] + m_b2c->m[4] * m_a2b->m[5] + m_b2c->m[5] * m_a2b->m[8];
  m_a2c->m[6] = m_b2c->m[6] * m_a2b->m[0] + m_b2c->m[7] * m_a2b->m[3] + m_b2c->m[8] * m_a2b->m[6];
  m_a2c->m[7] = m_b2c->m[6] * m_a2b->m[1] + m_b2c->m[7] * m_a2b->m[4] + m_b2c->m[8] * m_a2b->m[7];
  m_a2c->m[8] = m_b2c->m[6] * m_a2b->m[2] + m_b2c->m[7] * m_a2b->m[5] + m_b2c->m[8] * m_a2b->m[8];
}

91
/** Composition (multiplication) of two rotation matrices.
Felix Ruess's avatar
Felix Ruess committed
92
 * m_a2b = m_a2c comp_inv m_b2c , aka  m_a2b = inv(_m_b2c) * m_a2c
93
 */
94
void float_rmat_comp_inv(struct FloatRMat *m_a2b, struct FloatRMat *m_a2c, struct FloatRMat *m_b2c)
Felix Ruess's avatar
Felix Ruess committed
95
{
96 97 98 99 100 101 102 103 104 105 106
  m_a2b->m[0] = m_b2c->m[0] * m_a2c->m[0] + m_b2c->m[3] * m_a2c->m[3] + m_b2c->m[6] * m_a2c->m[6];
  m_a2b->m[1] = m_b2c->m[0] * m_a2c->m[1] + m_b2c->m[3] * m_a2c->m[4] + m_b2c->m[6] * m_a2c->m[7];
  m_a2b->m[2] = m_b2c->m[0] * m_a2c->m[2] + m_b2c->m[3] * m_a2c->m[5] + m_b2c->m[6] * m_a2c->m[8];
  m_a2b->m[3] = m_b2c->m[1] * m_a2c->m[0] + m_b2c->m[4] * m_a2c->m[3] + m_b2c->m[7] * m_a2c->m[6];
  m_a2b->m[4] = m_b2c->m[1] * m_a2c->m[1] + m_b2c->m[4] * m_a2c->m[4] + m_b2c->m[7] * m_a2c->m[7];
  m_a2b->m[5] = m_b2c->m[1] * m_a2c->m[2] + m_b2c->m[4] * m_a2c->m[5] + m_b2c->m[7] * m_a2c->m[8];
  m_a2b->m[6] = m_b2c->m[2] * m_a2c->m[0] + m_b2c->m[5] * m_a2c->m[3] + m_b2c->m[8] * m_a2c->m[6];
  m_a2b->m[7] = m_b2c->m[2] * m_a2c->m[1] + m_b2c->m[5] * m_a2c->m[4] + m_b2c->m[8] * m_a2c->m[7];
  m_a2b->m[8] = m_b2c->m[2] * m_a2c->m[2] + m_b2c->m[5] * m_a2c->m[5] + m_b2c->m[8] * m_a2c->m[8];
}

107 108 109
/** rotate 3D vector by rotation matrix.
 * vb = m_a2b * va
 */
110
void float_rmat_vmult(struct FloatVect3 *vb, struct FloatRMat *m_a2b, struct FloatVect3 *va)
111 112 113 114 115 116 117 118 119
{
  vb->x = m_a2b->m[0] * va->x + m_a2b->m[1] * va->y + m_a2b->m[2] * va->z;
  vb->y = m_a2b->m[3] * va->x + m_a2b->m[4] * va->y + m_a2b->m[5] * va->z;
  vb->z = m_a2b->m[6] * va->x + m_a2b->m[7] * va->y + m_a2b->m[8] * va->z;
}

/** rotate 3D vector by transposed rotation matrix.
 * vb = m_b2a^T * va
 */
120
void float_rmat_transp_vmult(struct FloatVect3 *vb, struct FloatRMat *m_b2a, struct FloatVect3 *va)
121 122 123 124 125 126
{
  vb->x = m_b2a->m[0] * va->x + m_b2a->m[3] * va->y + m_b2a->m[6] * va->z;
  vb->y = m_b2a->m[1] * va->x + m_b2a->m[4] * va->y + m_b2a->m[7] * va->z;
  vb->z = m_b2a->m[2] * va->x + m_b2a->m[5] * va->y + m_b2a->m[8] * va->z;
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/** rotate angle by rotation matrix.
 * rb = m_a2b * ra
 */
void float_rmat_mult(struct FloatEulers *rb, struct FloatRMat *m_a2b, struct FloatEulers *ra)
{
  rb->phi = m_a2b->m[0] * ra->phi + m_a2b->m[1] * ra->theta + m_a2b->m[2] * ra->psi;
  rb->theta = m_a2b->m[3] * ra->phi + m_a2b->m[4] * ra->theta + m_a2b->m[5] * ra->psi;
  rb->psi = m_a2b->m[6] * ra->phi + m_a2b->m[7] * ra->theta + m_a2b->m[8] * ra->psi;
}

/** rotate angle by transposed rotation matrix.
 * rb = m_b2a^T * ra
 */
void float_rmat_transp_mult(struct FloatEulers *rb, struct FloatRMat *m_b2a, struct FloatEulers *ra)
{
  rb->phi = m_b2a->m[0] * ra->phi + m_b2a->m[3] * ra->theta + m_b2a->m[6] * ra->psi;
  rb->theta = m_b2a->m[1] * ra->phi + m_b2a->m[4] * ra->theta + m_b2a->m[7] * ra->psi;
  rb->psi = m_b2a->m[2] * ra->phi + m_b2a->m[5] * ra->theta + m_b2a->m[8] * ra->psi;
}

147 148 149
/** rotate anglular rates by rotation matrix.
 * rb = m_a2b * ra
 */
150
void float_rmat_ratemult(struct FloatRates *rb, struct FloatRMat *m_a2b, struct FloatRates *ra)
151 152 153 154 155 156 157 158 159
{
  rb->p = m_a2b->m[0] * ra->p + m_a2b->m[1] * ra->q + m_a2b->m[2] * ra->r;
  rb->q = m_a2b->m[3] * ra->p + m_a2b->m[4] * ra->q + m_a2b->m[5] * ra->r;
  rb->r = m_a2b->m[6] * ra->p + m_a2b->m[7] * ra->q + m_a2b->m[8] * ra->r;
}

/** rotate anglular rates by transposed rotation matrix.
 * rb = m_b2a^T * ra
 */
160
void float_rmat_transp_ratemult(struct FloatRates *rb, struct FloatRMat *m_b2a, struct FloatRates *ra)
161 162 163 164 165 166 167 168
{
  rb->p = m_b2a->m[0] * ra->p + m_b2a->m[3] * ra->q + m_b2a->m[6] * ra->r;
  rb->q = m_b2a->m[1] * ra->p + m_b2a->m[4] * ra->q + m_b2a->m[7] * ra->r;
  rb->r = m_b2a->m[2] * ra->p + m_b2a->m[5] * ra->q + m_b2a->m[8] * ra->r;
}


/** initialises a rotation matrix from unit vector axis and angle */
169
void float_rmat_of_axis_angle(struct FloatRMat *rm, struct FloatVect3 *uv, float angle)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{
  const float ux2  = uv->x * uv->x;
  const float uy2  = uv->y * uv->y;
  const float uz2  = uv->z * uv->z;
  const float uxuy = uv->x * uv->y;
  const float uyuz = uv->y * uv->z;
  const float uxuz = uv->x * uv->z;
  const float can  = cosf(angle);
  const float san  = sinf(angle);
  const float one_m_can = (1. - can);

  RMAT_ELMT(*rm, 0, 0) = ux2 + (1. - ux2) * can;
  RMAT_ELMT(*rm, 0, 1) = uxuy * one_m_can + uv->z * san;
  RMAT_ELMT(*rm, 0, 2) = uxuz * one_m_can - uv->y * san;
  RMAT_ELMT(*rm, 1, 0) = RMAT_ELMT(*rm, 0, 1);
  RMAT_ELMT(*rm, 1, 1) = uy2 + (1. - uy2) * can;
  RMAT_ELMT(*rm, 1, 2) = uyuz * one_m_can + uv->x * san;
  RMAT_ELMT(*rm, 2, 0) = RMAT_ELMT(*rm, 0, 2);
  RMAT_ELMT(*rm, 2, 1) = RMAT_ELMT(*rm, 1, 2);
  RMAT_ELMT(*rm, 2, 2) = uz2 + (1. - uz2) * can;
}

192
/* C n->b rotation matrix */
193
void float_rmat_of_eulers_321(struct FloatRMat *rm, struct FloatEulers *e)
Felix Ruess's avatar
Felix Ruess committed
194
{
195
  const float sphi   = sinf(e->phi);
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
196
  //@ assert sphi == \sin(e->phi);
197
  const float cphi   = cosf(e->phi);
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
198
  //@assert cphi == \cos(e->phi);
199 200 201 202 203
  const float stheta = sinf(e->theta);
  const float ctheta = cosf(e->theta);
  const float spsi   = sinf(e->psi);
  const float cpsi   = cosf(e->psi);

Felix Ruess's avatar
Felix Ruess committed
204 205
  RMAT_ELMT(*rm, 0, 0) = ctheta * cpsi;
  RMAT_ELMT(*rm, 0, 1) = ctheta * spsi;
206
  RMAT_ELMT(*rm, 0, 2) = -stheta;
Felix Ruess's avatar
Felix Ruess committed
207 208 209 210 211 212
  RMAT_ELMT(*rm, 1, 0) = sphi * stheta * cpsi - cphi * spsi;
  RMAT_ELMT(*rm, 1, 1) = sphi * stheta * spsi + cphi * cpsi;
  RMAT_ELMT(*rm, 1, 2) = sphi * ctheta;
  RMAT_ELMT(*rm, 2, 0) = cphi * stheta * cpsi + sphi * spsi;
  RMAT_ELMT(*rm, 2, 1) = cphi * stheta * spsi - sphi * cpsi;
  RMAT_ELMT(*rm, 2, 2) = cphi * ctheta;
213 214
}

215
void float_rmat_of_eulers_312(struct FloatRMat *rm, struct FloatEulers *e)
Felix Ruess's avatar
Felix Ruess committed
216
{
217 218 219 220 221 222 223
  const float sphi   = sinf(e->phi);
  const float cphi   = cosf(e->phi);
  const float stheta = sinf(e->theta);
  const float ctheta = cosf(e->theta);
  const float spsi   = sinf(e->psi);
  const float cpsi   = cosf(e->psi);

Felix Ruess's avatar
Felix Ruess committed
224 225
  RMAT_ELMT(*rm, 0, 0) =  ctheta * cpsi - sphi * stheta * spsi;
  RMAT_ELMT(*rm, 0, 1) =  ctheta * spsi + sphi * stheta * cpsi;
226 227 228 229
  RMAT_ELMT(*rm, 0, 2) = -cphi * stheta;
  RMAT_ELMT(*rm, 1, 0) = -cphi * spsi;
  RMAT_ELMT(*rm, 1, 1) =  cphi * cpsi;
  RMAT_ELMT(*rm, 1, 2) =  sphi;
Felix Ruess's avatar
Felix Ruess committed
230 231 232
  RMAT_ELMT(*rm, 2, 0) =  stheta * cpsi + sphi * ctheta * spsi;
  RMAT_ELMT(*rm, 2, 1) =  stheta * spsi - sphi * ctheta * cpsi;
  RMAT_ELMT(*rm, 2, 2) =  cphi * ctheta;
233 234 235 236
}


/* C n->b rotation matrix */
237
void float_rmat_of_quat(struct FloatRMat *rm, struct FloatQuat *q)
Felix Ruess's avatar
Felix Ruess committed
238
{
239
#ifndef FRAMA_C_ANALYSIS
240 241 242 243
  const float _a = M_SQRT2 * q->qi;
  const float _b = M_SQRT2 * q->qx;
  const float _c = M_SQRT2 * q->qy;
  const float _d = M_SQRT2 * q->qz;
Felix Ruess's avatar
Felix Ruess committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  const float a2_1 = _a * _a - 1;
  const float ab = _a * _b;
  const float ac = _a * _c;
  const float ad = _a * _d;
  const float bc = _b * _c;
  const float bd = _b * _d;
  const float cd = _c * _d;
  RMAT_ELMT(*rm, 0, 0) = a2_1 + _b * _b;
  RMAT_ELMT(*rm, 0, 1) = bc + ad;
  RMAT_ELMT(*rm, 0, 2) = bd - ac;
  RMAT_ELMT(*rm, 1, 0) = bc - ad;
  RMAT_ELMT(*rm, 1, 1) = a2_1 + _c * _c;
  RMAT_ELMT(*rm, 1, 2) = cd + ab;
  RMAT_ELMT(*rm, 2, 0) = bd + ac;
  RMAT_ELMT(*rm, 2, 1) = cd - ab;
  RMAT_ELMT(*rm, 2, 2) = a2_1 + _d * _d;
260 261
#else 
  const float _a = q->qi;
262
  const float _2a = 2 * _a;
263
  const float _b = q->qx;
264
  const float _2b = 2 * _b;
265 266
  const float _c = q->qy;
  const float _d = q->qz;
267 268 269 270 271 272 273 274 275
  const float _2d = 2 * _d;
  const float a2_1 = _2a * _a - 1;
  const float ab = _2a * _b;
  const float ac = _2a * _c;
  const float ad = _2a * _d;
  const float bc = _2b * _c;
  const float bd = _2b * _d;
  const float cd = _c * _2d;
  RMAT_ELMT(*rm, 0, 0) = a2_1 + _2b * _b;
276 277 278 279 280 281 282
  RMAT_ELMT(*rm, 0, 1) = bc + ad;
  RMAT_ELMT(*rm, 0, 2) = bd - ac;
  RMAT_ELMT(*rm, 1, 0) = bc - ad;
  RMAT_ELMT(*rm, 1, 1) = a2_1 + 2 * _c * _c;
  RMAT_ELMT(*rm, 1, 2) = cd + ab;
  RMAT_ELMT(*rm, 2, 0) = bd + ac;
  RMAT_ELMT(*rm, 2, 1) = cd - ab;
283
  RMAT_ELMT(*rm, 2, 2) = a2_1 + _2d * _d;
284 285 286 287 288
  //@assert \at(q->qi, Pre) == q->qi;
  //@assert \at(q->qx, Pre) == q->qx;
  //@assert \at(q->qy, Pre) == q->qy;
  //@assert \at(q->qz, Pre) == q->qz;
#endif
289 290 291
}

/** in place first order integration of a rotation matrix */
292
void float_rmat_integrate_fi(struct FloatRMat *rm, struct FloatRates *omega, float dt)
Felix Ruess's avatar
Felix Ruess committed
293
{
294
  struct FloatRMat exp_omega_dt = {
Felix Ruess's avatar
Felix Ruess committed
295
    {
296 297 298
      1.        ,  dt *omega->r, -dt *omega->q,
      -dt *omega->r,  1.        ,  dt *omega->p,
      dt *omega->q, -dt *omega->p,  1.
Felix Ruess's avatar
Felix Ruess committed
299 300
    }
  };
301
  struct FloatRMat R_tdt;
Ewoud Smeur's avatar
Ewoud Smeur committed
302
  float_rmat_comp(&R_tdt, rm, &exp_omega_dt);
303
  *rm = R_tdt;
304 305
}

POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
306 307 308
/*@
  assigns \nothing;
*/
Felix Ruess's avatar
Felix Ruess committed
309 310 311 312 313
static inline float renorm_factor(float n)
{
  if (n < 1.5625f && n > 0.64f) {
    return .5 * (3 - n);
  } else if (n < 100.0f && n > 0.01f) {
314
    return  1. / sqrtf(n);
Felix Ruess's avatar
Felix Ruess committed
315
  } else {
316
    return 0.;
Felix Ruess's avatar
Felix Ruess committed
317
  }
318 319
}

320
float float_rmat_reorthogonalize(struct FloatRMat *rm)
Felix Ruess's avatar
Felix Ruess committed
321 322 323 324 325 326 327 328 329 330
{
  const struct FloatVect3 r0 = {RMAT_ELMT(*rm, 0, 0),
          RMAT_ELMT(*rm, 0, 1),
          RMAT_ELMT(*rm, 0, 2)
  };
  const struct FloatVect3 r1 = {RMAT_ELMT(*rm, 1, 0),
          RMAT_ELMT(*rm, 1, 1),
          RMAT_ELMT(*rm, 1, 2)
  };
  float _err = -0.5 * VECT3_DOT_PRODUCT(r0, r1);
331 332 333 334 335
  struct FloatVect3 r0_t;
  VECT3_SUM_SCALED(r0_t, r0, r1, _err);
  struct FloatVect3 r1_t;
  VECT3_SUM_SCALED(r1_t,  r1, r0, _err);
  struct FloatVect3 r2_t;
336
  VECT3_CROSS_PRODUCT(r2_t, r0_t, r1_t);
Felix Ruess's avatar
Felix Ruess committed
337
  float s = renorm_factor(VECT3_NORM2(r0_t));
338
  MAT33_ROW_VECT3_SMUL(*rm, 0, r0_t, s);
Felix Ruess's avatar
Felix Ruess committed
339
  s = renorm_factor(VECT3_NORM2(r1_t));
340
  MAT33_ROW_VECT3_SMUL(*rm, 1, r1_t, s);
Felix Ruess's avatar
Felix Ruess committed
341
  s = renorm_factor(VECT3_NORM2(r2_t));
342 343 344 345 346 347 348 349 350 351 352 353
  MAT33_ROW_VECT3_SMUL(*rm, 2, r2_t, s);

  return _err;
}


/*
 *
 * Quaternion functions.
 *
 */

354
void float_quat_comp(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c)
Felix Ruess's avatar
Felix Ruess committed
355 356 357 358 359
{
  a2c->qi = a2b->qi * b2c->qi - a2b->qx * b2c->qx - a2b->qy * b2c->qy - a2b->qz * b2c->qz;
  a2c->qx = a2b->qi * b2c->qx + a2b->qx * b2c->qi + a2b->qy * b2c->qz - a2b->qz * b2c->qy;
  a2c->qy = a2b->qi * b2c->qy - a2b->qx * b2c->qz + a2b->qy * b2c->qi + a2b->qz * b2c->qx;
  a2c->qz = a2b->qi * b2c->qz + a2b->qx * b2c->qy - a2b->qy * b2c->qx + a2b->qz * b2c->qi;
360 361
}

362
void float_quat_comp_inv(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c)
Felix Ruess's avatar
Felix Ruess committed
363 364 365 366 367
{
  a2b->qi =  a2c->qi * b2c->qi + a2c->qx * b2c->qx + a2c->qy * b2c->qy + a2c->qz * b2c->qz;
  a2b->qx = -a2c->qi * b2c->qx + a2c->qx * b2c->qi - a2c->qy * b2c->qz + a2c->qz * b2c->qy;
  a2b->qy = -a2c->qi * b2c->qy + a2c->qx * b2c->qz + a2c->qy * b2c->qi - a2c->qz * b2c->qx;
  a2b->qz = -a2c->qi * b2c->qz - a2c->qx * b2c->qy + a2c->qy * b2c->qx + a2c->qz * b2c->qi;
368 369
}

370
void float_quat_inv_comp(struct FloatQuat *b2c, struct FloatQuat *a2b, struct FloatQuat *a2c)
Felix Ruess's avatar
Felix Ruess committed
371 372 373 374 375
{
  b2c->qi = a2b->qi * a2c->qi + a2b->qx * a2c->qx + a2b->qy * a2c->qy + a2b->qz * a2c->qz;
  b2c->qx = a2b->qi * a2c->qx - a2b->qx * a2c->qi - a2b->qy * a2c->qz + a2b->qz * a2c->qy;
  b2c->qy = a2b->qi * a2c->qy + a2b->qx * a2c->qz - a2b->qy * a2c->qi - a2b->qz * a2c->qx;
  b2c->qz = a2b->qi * a2c->qz - a2b->qx * a2c->qy + a2b->qy * a2c->qx - a2b->qz * a2c->qi;
376 377
}

378
void float_quat_comp_norm_shortest(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c)
Felix Ruess's avatar
Felix Ruess committed
379
{
380 381 382 383 384
  float_quat_comp(a2c, a2b, b2c);
  float_quat_wrap_shortest(a2c);
  float_quat_normalize(a2c);
}

385
void float_quat_comp_inv_norm_shortest(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c)
Felix Ruess's avatar
Felix Ruess committed
386
{
387 388 389 390 391
  float_quat_comp_inv(a2b, a2c, b2c);
  float_quat_wrap_shortest(a2b);
  float_quat_normalize(a2b);
}

392
void float_quat_inv_comp_norm_shortest(struct FloatQuat *b2c, struct FloatQuat *a2b, struct FloatQuat *a2c)
Felix Ruess's avatar
Felix Ruess committed
393
{
394 395 396 397 398
  float_quat_inv_comp(b2c, a2b, a2c);
  float_quat_wrap_shortest(b2c);
  float_quat_normalize(b2c);
}

399
void float_quat_differential(struct FloatQuat *q_out, struct FloatRates *w, float dt)
Felix Ruess's avatar
Felix Ruess committed
400 401 402 403
{
  const float v_norm = sqrtf(w->p * w->p + w->q * w->q + w->r * w->r);
  const float c2 = cos(dt * v_norm / 2.0);
  const float s2 = sin(dt * v_norm / 2.0);
404 405 406 407 408 409 410 411 412 413 414 415 416
  if (v_norm < 1e-8) {
    q_out->qi = 1;
    q_out->qx = 0;
    q_out->qy = 0;
    q_out->qz = 0;
  } else {
    q_out->qi = c2;
    q_out->qx = w->p / v_norm * s2;
    q_out->qy = w->q / v_norm * s2;
    q_out->qz = w->r / v_norm * s2;
  }
}

417
/** in place first order quaternion integration with constant rotational velocity */
418
void float_quat_integrate_fi(struct FloatQuat *q, struct FloatRates *omega, float dt)
Felix Ruess's avatar
Felix Ruess committed
419
{
420 421 422 423 424 425 426
  const float qi = q->qi;
  const float qx = q->qx;
  const float qy = q->qy;
  const float qz = q->qz;
  const float dp = 0.5 * dt * omega->p;
  const float dq = 0.5 * dt * omega->q;
  const float dr = 0.5 * dt * omega->r;
Felix Ruess's avatar
Felix Ruess committed
427 428 429 430
  q->qi = qi    - dp * qx - dq * qy - dr * qz;
  q->qx = dp * qi +    qx + dr * qy - dq * qz;
  q->qy = dq * qi - dr * qx +    qy + dp * qz;
  q->qz = dr * qi + dq * qx - dp * qy +    qz;
431 432
}

433
/** in place quaternion integration with constant rotational velocity */
434
void float_quat_integrate(struct FloatQuat *q, struct FloatRates *omega, float dt)
Felix Ruess's avatar
Felix Ruess committed
435
{
436 437
  const float no = FLOAT_RATES_NORM(*omega);
  if (no > FLT_MIN) {
Felix Ruess's avatar
Felix Ruess committed
438
    const float a  = 0.5 * no * dt;
439
    const float ca = cosf(a);
Felix Ruess's avatar
Felix Ruess committed
440
    const float sa_ov_no = sinf(a) / no;
441 442 443 444 445 446 447
    const float dp = sa_ov_no * omega->p;
    const float dq = sa_ov_no * omega->q;
    const float dr = sa_ov_no * omega->r;
    const float qi = q->qi;
    const float qx = q->qx;
    const float qy = q->qy;
    const float qz = q->qz;
Felix Ruess's avatar
Felix Ruess committed
448 449 450 451
    q->qi = ca * qi - dp * qx - dq * qy - dr * qz;
    q->qx = dp * qi + ca * qx + dr * qy - dq * qz;
    q->qy = dq * qi - dr * qx + ca * qy + dp * qz;
    q->qz = dr * qi + dq * qx - dp * qy + ca * qz;
452 453 454
  }
}

Freek van Tienen's avatar
Freek van Tienen committed
455
void float_quat_vmult(struct FloatVect3 *v_out, struct FloatQuat *q, const struct FloatVect3 *v_in)
Felix Ruess's avatar
Felix Ruess committed
456 457 458 459 460 461 462 463
{
  const float qi2_M1_2  = q->qi * q->qi - 0.5;
  const float qiqx = q->qi * q->qx;
  const float qiqy = q->qi * q->qy;
  const float qiqz = q->qi * q->qz;
  float m01  = q->qx * q->qy; /* aka qxqy */
  float m02  = q->qx * q->qz; /* aka qxqz */
  float m12  = q->qy * q->qz; /* aka qyqz */
464

Felix Ruess's avatar
Felix Ruess committed
465
  const float m00  = qi2_M1_2 + q->qx * q->qx;
466 467 468 469 470 471
  const float m10  = m01 - qiqz;
  const float m20  = m02 + qiqy;
  const float m21  = m12 - qiqx;
  m01 += qiqz;
  m02 -= qiqy;
  m12 += qiqx;
Felix Ruess's avatar
Felix Ruess committed
472 473 474 475 476
  const float m11  = qi2_M1_2 + q->qy * q->qy;
  const float m22  = qi2_M1_2 + q->qz * q->qz;
  v_out->x = 2 * (m00 * v_in->x + m01 * v_in->y + m02 * v_in->z);
  v_out->y = 2 * (m10 * v_in->x + m11 * v_in->y + m12 * v_in->z);
  v_out->z = 2 * (m20 * v_in->x + m21 * v_in->y + m22 * v_in->z);
477 478 479 480
}

/** Quaternion derivative from rotational velocity.
 * qd = -0.5*omega(r) * q
481 482
 * or equally:
 * qd = 0.5 * q * omega(r)
483
 */
484
void float_quat_derivative(struct FloatQuat *qd, struct FloatRates *r, struct FloatQuat *q)
Felix Ruess's avatar
Felix Ruess committed
485 486 487 488 489
{
  qd->qi = -0.5 * (r->p * q->qx + r->q * q->qy + r->r * q->qz);
  qd->qx = -0.5 * (-r->p * q->qi - r->r * q->qy + r->q * q->qz);
  qd->qy = -0.5 * (-r->q * q->qi + r->r * q->qx - r->p * q->qz);
  qd->qz = -0.5 * (-r->r * q->qi - r->q * q->qx + r->p * q->qy);
490 491 492 493 494
}

/** Quaternion derivative from rotational velocity.
 * qd = -0.5*omega(r) * q
 */
495
void float_quat_derivative_lagrange(struct FloatQuat *qd, struct FloatRates *r, struct FloatQuat *q)
Felix Ruess's avatar
Felix Ruess committed
496
{
497
  const float K_LAGRANGE = 1.;
Felix Ruess's avatar
Felix Ruess committed
498 499 500 501 502
  const float c = K_LAGRANGE * (1 - float_quat_norm(q)) / -0.5;
  qd->qi = -0.5 * (c * q->qi + r->p * q->qx + r->q * q->qy + r->r * q->qz);
  qd->qx = -0.5 * (-r->p * q->qi +      c * q->qx - r->r * q->qy + r->q * q->qz);
  qd->qy = -0.5 * (-r->q * q->qi + r->r * q->qx +      c * q->qy - r->p * q->qz);
  qd->qz = -0.5 * (-r->r * q->qi - r->q * q->qx + r->p * q->qy +      c * q->qz);
503 504
}

505 506 507 508 509 510
/**
 * @brief quat of euler roation 'ZYX'
 *
 * @param q Quat output
 * @param e Euler input
 */
511
void float_quat_of_eulers(struct FloatQuat *q, struct FloatEulers *e)
Felix Ruess's avatar
Felix Ruess committed
512
{
513

514 515 516
  const float phi2   = e->phi / 2.f;
  const float theta2 = e->theta / 2.f;
  const float psi2   = e->psi / 2.f;
517 518 519 520 521 522 523 524 525 526 527 528 529 530

  const float s_phi2   = sinf(phi2);
  const float c_phi2   = cosf(phi2);
  const float s_theta2 = sinf(theta2);
  const float c_theta2 = cosf(theta2);
  const float s_psi2   = sinf(psi2);
  const float c_psi2   = cosf(psi2);

  q->qi =  c_phi2 * c_theta2 * c_psi2 + s_phi2 * s_theta2 * s_psi2;
  q->qx = -c_phi2 * s_theta2 * s_psi2 + s_phi2 * c_theta2 * c_psi2;
  q->qy =  c_phi2 * s_theta2 * c_psi2 + s_phi2 * c_theta2 * s_psi2;
  q->qz =  c_phi2 * c_theta2 * s_psi2 - s_phi2 * s_theta2 * c_psi2;
}

531 532 533 534 535 536 537 538 539
/**
 * @brief quat from euler rotation 'ZXY'
 * This rotation order is useful if you need 90 deg pitch
 *
 * @param q Quat output
 * @param e Euler input
 */
void float_quat_of_eulers_zxy(struct FloatQuat *q, struct FloatEulers *e)
{
540 541 542
  const float phi2   = e->phi / 2.f;
  const float theta2 = e->theta / 2.f;
  const float psi2   = e->psi / 2.f;
543 544 545 546 547 548 549 550 551 552 553 554 555 556

  const float s_phi2   = sinf(phi2);
  const float c_phi2   = cosf(phi2);
  const float s_theta2 = sinf(theta2);
  const float c_theta2 = cosf(theta2);
  const float s_psi2   = sinf(psi2);
  const float c_psi2   = cosf(psi2);

  q->qi =  c_phi2 * c_theta2 * c_psi2 - s_phi2 * s_theta2 * s_psi2;
  q->qx =  s_phi2 * c_theta2 * c_psi2 - c_phi2 * s_theta2 * s_psi2;
  q->qy =  c_phi2 * s_theta2 * c_psi2 + s_phi2 * c_theta2 * s_psi2;
  q->qz =  s_phi2 * s_theta2 * c_psi2 + c_phi2 * c_theta2 * s_psi2;
}

557 558 559 560 561 562 563 564 565 566 567
/**
 * @brief quat from euler rotation 'YXZ'
 * This function calculates a quaternion from Euler angles with the order YXZ,
 * so pitch, roll, yaw, instead of the conventional ZYX order.
 * See https://en.wikipedia.org/wiki/Euler_angles
 *
 * @param q Quat output
 * @param e Euler input
 */
void float_quat_of_eulers_yxz(struct FloatQuat *q, struct FloatEulers *e)
{
568 569 570
  const float phi2   = e->phi / 2.f;
  const float theta2 = e->theta / 2.f;
  const float psi2   = e->psi / 2.f;
571 572 573 574 575 576 577 578 579 580 581 582 583

  const float s_phi2   = sinf(phi2);
  const float c_phi2   = cosf(phi2);
  const float s_theta2 = sinf(theta2);
  const float c_theta2 = cosf(theta2);
  const float s_psi2   = sinf(psi2);
  const float c_psi2   = cosf(psi2);

  q->qi =  c_theta2 * c_phi2 * c_psi2 + s_theta2 * s_phi2 * s_psi2;
  q->qx =  c_theta2 * s_phi2 * c_psi2 + s_theta2 * c_phi2 * s_psi2;
  q->qy =  s_theta2 * c_phi2 * c_psi2 - c_theta2 * s_phi2 * s_psi2;
  q->qz =  c_theta2 * c_phi2 * s_psi2 - s_theta2 * s_phi2 * c_psi2;
}
584

585
void float_quat_of_axis_angle(struct FloatQuat *q, const struct FloatVect3 *uv, float angle)
Felix Ruess's avatar
Felix Ruess committed
586
{
587 588
  const float san = sinf(angle / 2.f);
  q->qi = cosf(angle / 2.f);
589 590 591 592 593
  q->qx = san * uv->x;
  q->qy = san * uv->y;
  q->qz = san * uv->z;
}

594
void float_quat_of_orientation_vect(struct FloatQuat *q, const struct FloatVect3 *ov)
Felix Ruess's avatar
Felix Ruess committed
595 596
{
  const float ov_norm = sqrtf(ov->x * ov->x + ov->y * ov->y + ov->z * ov->z);
597 598 599 600 601 602
  if (ov_norm < 1e-8) {
    q->qi = 1;
    q->qx = 0;
    q->qy = 0;
    q->qz = 0;
  } else {
Felix Ruess's avatar
Felix Ruess committed
603 604
    const float s2_normalized = sinf(ov_norm / 2.0) / ov_norm;
    q->qi = cosf(ov_norm / 2.0);
605 606 607 608 609 610
    q->qx = ov->x * s2_normalized;
    q->qy = ov->y * s2_normalized;
    q->qz = ov->z * s2_normalized;
  }
}

611
void float_quat_of_rmat(struct FloatQuat *q, struct FloatRMat *rm)
Felix Ruess's avatar
Felix Ruess committed
612
{
613 614
  const float tr = RMAT_TRACE(*rm);
  if (tr > 0) {
Felix Ruess's avatar
Felix Ruess committed
615
    const float two_qi = sqrtf(1. + tr);
616 617
    const float four_qi = 2. * two_qi;
    q->qi = 0.5 * two_qi;
Felix Ruess's avatar
Felix Ruess committed
618 619 620
    q->qx = (RMAT_ELMT(*rm, 1, 2) - RMAT_ELMT(*rm, 2, 1)) / four_qi;
    q->qy = (RMAT_ELMT(*rm, 2, 0) - RMAT_ELMT(*rm, 0, 2)) / four_qi;
    q->qz = (RMAT_ELMT(*rm, 0, 1) - RMAT_ELMT(*rm, 1, 0)) / four_qi;
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
621 622 623 624 625 626 627 628 629 630 631
    //@ assert q->qi * q->qi == 0.5* 0.5 * (1 + tr);
    //@ assert \at(rm->m[0], Pre) == rm->m[0];
    //@ assert \at(rm->m[1], Pre) == rm->m[1];
    //@ assert \at(rm->m[2], Pre) == rm->m[2];
    //@ assert \at(rm->m[3], Pre) == rm->m[3];
    //@ assert \at(rm->m[4], Pre) == rm->m[4];
    //@ assert \at(rm->m[5], Pre) == rm->m[5];
    //@ assert \at(rm->m[6], Pre) == rm->m[6];
    //@ assert \at(rm->m[7], Pre) == rm->m[7];
    //@ assert \at(rm->m[8], Pre) == rm->m[8];
    //@ assert SQR(rm->m[0]) + SQR(rm->m[1]) + SQR(rm->m[2]) == 1;
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
632
    //@ assert unary_quaterion(q);
633
    /*printf("tr > 0\n");*/
Felix Ruess's avatar
Felix Ruess committed
634
  } else {
635 636
    if (RMAT_ELMT(*rm, 0, 0) > RMAT_ELMT(*rm, 1, 1) &&
        RMAT_ELMT(*rm, 0, 0) > RMAT_ELMT(*rm, 2, 2)) {
Felix Ruess's avatar
Felix Ruess committed
637 638
      const float two_qx = sqrtf(RMAT_ELMT(*rm, 0, 0) - RMAT_ELMT(*rm, 1, 1)
                                 - RMAT_ELMT(*rm, 2, 2) + 1);
639
      const float four_qx = 2. * two_qx;
Felix Ruess's avatar
Felix Ruess committed
640
      q->qi = (RMAT_ELMT(*rm, 1, 2) - RMAT_ELMT(*rm, 2, 1)) / four_qx;
641
      q->qx = 0.5 * two_qx;
Felix Ruess's avatar
Felix Ruess committed
642 643
      q->qy = (RMAT_ELMT(*rm, 0, 1) + RMAT_ELMT(*rm, 1, 0)) / four_qx;
      q->qz = (RMAT_ELMT(*rm, 2, 0) + RMAT_ELMT(*rm, 0, 2)) / four_qx;
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
644
      //@ assert unary_quaterion(q);
645
      /*printf("m00 largest\n");*/
Felix Ruess's avatar
Felix Ruess committed
646
    } else if (RMAT_ELMT(*rm, 1, 1) > RMAT_ELMT(*rm, 2, 2)) {
647 648 649
      const float two_qy =
        sqrtf(RMAT_ELMT(*rm, 1, 1) - RMAT_ELMT(*rm, 0, 0) - RMAT_ELMT(*rm, 2, 2) + 1);
      const float four_qy = 2. * two_qy;
Felix Ruess's avatar
Felix Ruess committed
650 651
      q->qi = (RMAT_ELMT(*rm, 2, 0) - RMAT_ELMT(*rm, 0, 2)) / four_qy;
      q->qx = (RMAT_ELMT(*rm, 0, 1) + RMAT_ELMT(*rm, 1, 0)) / four_qy;
652
      q->qy = 0.5 * two_qy;
Felix Ruess's avatar
Felix Ruess committed
653
      q->qz = (RMAT_ELMT(*rm, 1, 2) + RMAT_ELMT(*rm, 2, 1)) / four_qy;
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
654
      //@ assert unary_quaterion(q);
655
      /*printf("m11 largest\n");*/
Felix Ruess's avatar
Felix Ruess committed
656
    } else {
657 658 659
      const float two_qz =
        sqrtf(RMAT_ELMT(*rm, 2, 2) - RMAT_ELMT(*rm, 0, 0) - RMAT_ELMT(*rm, 1, 1) + 1);
      const float four_qz = 2. * two_qz;
Felix Ruess's avatar
Felix Ruess committed
660 661 662
      q->qi = (RMAT_ELMT(*rm, 0, 1) - RMAT_ELMT(*rm, 1, 0)) / four_qz;
      q->qx = (RMAT_ELMT(*rm, 2, 0) + RMAT_ELMT(*rm, 0, 2)) / four_qz;
      q->qy = (RMAT_ELMT(*rm, 1, 2) + RMAT_ELMT(*rm, 2, 1)) / four_qz;
663
      q->qz = 0.5 * two_qz;
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
664
      //@ assert unary_quaterion(q);
665 666 667
      /*printf("m22 largest\n");*/
    }
  }
668 669 670 671 672 673 674 675 676
  //@ assert \at(rm->m[0], Pre) == rm->m[0];
  //@ assert \at(rm->m[1], Pre) == rm->m[1];
  //@ assert \at(rm->m[2], Pre) == rm->m[2];
  //@ assert \at(rm->m[3], Pre) == rm->m[3];
  //@ assert \at(rm->m[4], Pre) == rm->m[4];
  //@ assert \at(rm->m[5], Pre) == rm->m[5];
  //@ assert \at(rm->m[6], Pre) == rm->m[6];
  //@ assert \at(rm->m[7], Pre) == rm->m[7];
  //@ assert \at(rm->m[8], Pre) == rm->m[8];
677 678 679 680 681 682 683 684 685
}


/*
 *
 * Euler angle functions.
 *
 */

686
void float_eulers_of_rmat(struct FloatEulers *e, struct FloatRMat *rm)
Felix Ruess's avatar
Felix Ruess committed
687
{
688 689
  const float dcm00 = rm->m[0];
  const float dcm01 = rm->m[1];
690
  float dcm02 = rm->m[2];
691 692
  const float dcm12 = rm->m[5];
  const float dcm22 = rm->m[8];
693 694 695 696

  // asinf does not exist outside [-1,1]
  BoundAbs(dcm02, 1.0);

697 698 699 700 701
  e->phi   = atan2f(dcm12, dcm22);
  e->theta = -asinf(dcm02);
  e->psi   = atan2f(dcm01, dcm00);
}

702 703 704 705 706 707
/**
 * @brief euler rotation 'ZYX'
 *
 * @param e Euler output
 * @param q Quat input
 */
708
void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q)
Felix Ruess's avatar
Felix Ruess committed
709
{
710 711 712 713 714 715 716 717 718
  const float qx2  = q->qx * q->qx;
  const float qy2  = q->qy * q->qy;
  const float qz2  = q->qz * q->qz;
  const float qiqx = q->qi * q->qx;
  const float qiqy = q->qi * q->qy;
  const float qiqz = q->qi * q->qz;
  const float qxqy = q->qx * q->qy;
  const float qxqz = q->qx * q->qz;
  const float qyqz = q->qy * q->qz;
Felix Ruess's avatar
Felix Ruess committed
719 720
  const float dcm00 = 1.0 - 2.*(qy2 +  qz2);
  const float dcm01 =       2.*(qxqy + qiqz);
721
  float dcm02 =       2.*(qxqz - qiqy);
Felix Ruess's avatar
Felix Ruess committed
722 723
  const float dcm12 =       2.*(qyqz + qiqx);
  const float dcm22 = 1.0 - 2.*(qx2 +  qy2);
724

725 726 727
  // asinf does not exist outside [-1,1]
  BoundAbs(dcm02, 1.0);

728 729 730 731
  e->phi = atan2f(dcm12, dcm22);
  e->theta = -asinf(dcm02);
  e->psi = atan2f(dcm01, dcm00);
}
732

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
/**
 * @brief euler rotation 'YXZ'
 * This function calculates from a quaternion the Euler angles with the order YXZ,
 * so pitch, roll, yaw, instead of the conventional ZYX order.
 * See https://en.wikipedia.org/wiki/Euler_angles
 *
 * @param e Euler output
 * @param q Quat input
 */
void float_eulers_of_quat_yxz(struct FloatEulers *e, struct FloatQuat *q)
{
  const float qx2  = q->qx * q->qx;
  const float qy2  = q->qy * q->qy;
  const float qz2  = q->qz * q->qz;
  const float qi2  = q->qi * q->qi;
  const float qiqx = q->qi * q->qx;
  const float qiqy = q->qi * q->qy;
  const float qiqz = q->qi * q->qz;
  const float qxqy = q->qx * q->qy;
  const float qxqz = q->qx * q->qz;
  const float qyqz = q->qy * q->qz;
754
  const float r11  = 2.f * (qxqz + qiqy);
755
  const float r12  = qi2 - qx2 + qy2 + qz2;
756
  float r21  = -2.f * (qyqz - qiqx);
757
  const float r31  = 2.f * (qxqy + qiqz);
758 759
  const float r32  = qi2 - qx2 + qy2 - qz2;

760 761 762
  // asinf does not exist outside [-1,1]
  BoundAbs(r21, 1.0);

763 764 765 766 767
  e->theta = atan2f(r11, r12);
  e->phi = asinf(r21);
  e->psi = atan2f(r31, r32);
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
/**
 * @brief euler rotation 'ZXY'
 * This rotation order is useful if you need 90 deg pitch
 *
 * @param e Euler output
 * @param q Quat input
 */
void float_eulers_of_quat_zxy(struct FloatEulers *e, struct FloatQuat *q)
{
  const float qx2  = q->qx * q->qx;
  const float qy2  = q->qy * q->qy;
  const float qz2  = q->qz * q->qz;
  const float qi2  = q->qi * q->qi;
  const float qiqx = q->qi * q->qx;
  const float qiqy = q->qi * q->qy;
  const float qiqz = q->qi * q->qz;
  const float qxqy = q->qx * q->qy;
  const float qxqz = q->qx * q->qz;
  const float qyqz = q->qy * q->qz;
  const float r11  = -2 * (qxqy - qiqz);
  const float r12  = qi2 - qx2 + qy2 - qz2;
789
  float r21  =  2 * (qyqz + qiqx);
790 791 792
  const float r31  = -2 * (qxqz - qiqy);
  const float r32  = qi2 - qx2 - qy2 + qz2;

793 794 795
  // asinf does not exist outside [-1,1]
  BoundAbs(r21, 1.0);

796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  e->psi = atan2f(r11, r12);
  e->phi = asinf(r21);
  e->theta = atan2f(r31, r32);
}

/**
 * @brief 2x2 matrix inverse
 *
 * @param inv_out[4] inverted matrix output
 * @param mat_in[4] matrix to be inverted
 *
 * @return success (0) or not invertible (1)
 */
bool float_mat_inv_2d(float inv_out[4], float mat_in[4])
{
  float det = mat_in[0] * mat_in[3] - mat_in[1] * mat_in[2];

  if (fabsf(det) < 1e-4) { return 1; } //not invertible

  inv_out[0] =  mat_in[3] / det;
  inv_out[1] = -mat_in[1] / det;
  inv_out[2] = -mat_in[2] / det;
  inv_out[3] =  mat_in[0] / det;

  return 0; //return success
}

/**
 * @brief Multiply 2D matrix with vector
 *
 * @param vect_out output vector
 * @param mat[4] Matrix input
 * @param vect_in Vector input
 */
void float_mat2_mult(struct FloatVect2 *vect_out, float mat[4], struct FloatVect2 vect_in)
{
  vect_out->x = mat[0] * vect_in.x + mat[1] * vect_in.y;
  vect_out->y = mat[2] * vect_in.x + mat[3] * vect_in.y;
}

836 837 838 839
/*
 * 4x4 Matrix inverse.
 * obtained from: http://rodolphe-vaillant.fr/?e=7
 */
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
840 841 842 843 844 845 846 847 848 849
/*@
  requires \valid_read(m + (0..15));
  requires 0 <= r0 <= 3;
  requires 0 <= r1 <= 3;
  requires 0 <= r2 <= 3;
  requires 0 <= c0 <= 3;
  requires 0 <= c1 <= 3;
  requires 0 <= c2 <= 3;
  assigns \nothing;
*/
850
static float float_mat_minor_4d(float m[16], int r0, int r1, int r2, int c0, int c1, int c2)
851
{
852 853 854
  return m[4 * r0 + c0] * (m[4 * r1 + c1] * m[4 * r2 + c2] - m[4 * r2 + c1] * m[4 * r1 + c2]) -
         m[4 * r0 + c1] * (m[4 * r1 + c0] * m[4 * r2 + c2] - m[4 * r2 + c0] * m[4 * r1 + c2]) +
         m[4 * r0 + c2] * (m[4 * r1 + c0] * m[4 * r2 + c1] - m[4 * r2 + c0] * m[4 * r1 + c1]);
855 856
}

POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
857 858 859 860 861
/*@
  requires \valid(adjOut + (0..15));
  requires \valid_read(m + (0..15));
  assigns adjOut[0..15];
*/
862
static void float_mat_adjoint_4d(float adjOut[16], float m[16])
863
{
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
  adjOut[ 0] =  float_mat_minor_4d(m, 1, 2, 3, 1, 2, 3);
  adjOut[ 1] = -float_mat_minor_4d(m, 0, 2, 3, 1, 2, 3);
  adjOut[ 2] =  float_mat_minor_4d(m, 0, 1, 3, 1, 2, 3);
  adjOut[ 3] = -float_mat_minor_4d(m, 0, 1, 2, 1, 2, 3);
  adjOut[ 4] = -float_mat_minor_4d(m, 1, 2, 3, 0, 2, 3);
  adjOut[ 5] =  float_mat_minor_4d(m, 0, 2, 3, 0, 2, 3);
  adjOut[ 6] = -float_mat_minor_4d(m, 0, 1, 3, 0, 2, 3);
  adjOut[ 7] =  float_mat_minor_4d(m, 0, 1, 2, 0, 2, 3);
  adjOut[ 8] =  float_mat_minor_4d(m, 1, 2, 3, 0, 1, 3);
  adjOut[ 9] = -float_mat_minor_4d(m, 0, 2, 3, 0, 1, 3);
  adjOut[10] =  float_mat_minor_4d(m, 0, 1, 3, 0, 1, 3);
  adjOut[11] = -float_mat_minor_4d(m, 0, 1, 2, 0, 1, 3);
  adjOut[12] = -float_mat_minor_4d(m, 1, 2, 3, 0, 1, 2);
  adjOut[13] =  float_mat_minor_4d(m, 0, 2, 3, 0, 1, 2);
  adjOut[14] = -float_mat_minor_4d(m, 0, 1, 3, 0, 1, 2);
  adjOut[15] =  float_mat_minor_4d(m, 0, 1, 2, 0, 1, 2);
880 881
}

POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
882 883 884 885
/*@
  requires \valid_read(m + (0..15));
  assigns \nothing;
*/
886
static float float_mat_det_4d(float m[16])
887
{
888 889 890 891
  return m[0] * float_mat_minor_4d(m, 1, 2, 3, 1, 2, 3) -
         m[1] * float_mat_minor_4d(m, 1, 2, 3, 0, 2, 3) +
         m[2] * float_mat_minor_4d(m, 1, 2, 3, 0, 1, 3) -
         m[3] * float_mat_minor_4d(m, 1, 2, 3, 0, 1, 2);
892 893 894 895 896 897 898 899
}

/**
 * 4x4 Matrix inverse
 *
 * @param invOut output array, inverse of mat_in
 * @param mat_in input array
 */
900
bool float_mat_inv_4d(float invOut[16], float mat_in[16])
901
{
902 903 904 905 906 907 908
  float_mat_adjoint_4d(invOut, mat_in);

  float det = float_mat_det_4d(mat_in);
  if (fabsf(det) < 1e-4) { return 1; } //not invertible

  float inv_det = 1.0f / det;
  int i;
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
909 910 911 912 913
  /*@
    loop invariant 0 <= i <= 16;
    loop assigns invOut[0..15], i;
    loop variant 16 - i;
  */
914 915 916
  for (i = 0; i < 16; ++i) {
    invOut[i] = invOut[i] * inv_det;
  }
917

918
  return 0; //success
919
}
920

POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
921
/* Ignoring functions that are not supported by frama-c */
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
922
#ifndef FRAMA_C_ANALYSIS
POLLIEN Baptiste's avatar
POLLIEN Baptiste committed
923

924
/** Calculate inverse of any n x n matrix (passed as C array) o = mat^-1
925
Algorithm verified with Matlab.
926 927 928 929 930 931
Thanks to: https://www.quora.com/How-do-I-make-a-C++-program-to-get-the-inverse-of-a-matrix-100-X-100
*/
void float_mat_invert(float **o, float **mat, int n)
{
  int i, j, k;
  float t;
932
  float a[n][2 * n];
933 934

  // Append an identity matrix on the right of the original matrix
935 936
  for (i = 0; i < n; i++) {
    for (j = 0; j < 2 * n; j++) {
937 938
      if (j < n) {
        a[i][j] = mat[i][j];
939
      } else if ((j >= n) && (j == i + n)) {
940
        a[i][j] = 1.0;
941
      } else {