-
Notifications
You must be signed in to change notification settings - Fork 2
/
ekf_math.c
352 lines (283 loc) · 9.95 KB
/
ekf_math.c
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (c) 2015 Thomas Roell. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Thomas Roell, nor the names of its contributors
* may be used to endorse or promote products derived from this Software
* without specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* WITH THE SOFTWARE.
*/
#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
#define NDEBUG
#endif
#include <math.h>
#include <assert.h>
#include "ekf_math.h"
__attribute__((optimize("-O3"))) void mat_init_zero(MATRIX out)
{
int i, j;
for (i = 0; i < MAT_ROWS(out); i++) {
for (j = 0; j < MAT_COLS(out); j++) {
MAT_ELEMENT(out,i,j) = 0.0;
}
}
}
__attribute__((optimize("-O3"))) void mat_init_identity(MATRIX out)
{
int i, j;
for (i = 0; i < MAT_ROWS(out); i++) {
for (j = 0; j < MAT_COLS(out); j++) {
if (i == j) {
MAT_ELEMENT(out,i,j) = 1.0;
} else {
MAT_ELEMENT(out,i,j) = 0.0;
}
}
}
}
__attribute__((optimize("-O3"))) void mat_copy(MATRIX a, MATRIX out)
{
int i, j;
assert(MAT_ROWS(a) == MAT_ROWS(out));
assert(MAT_COLS(a) == MAT_COLS(out));
for (i = 0; i < MAT_ROWS(a); i++) {
for (j = 0; j < MAT_COLS(a); j++) {
MAT_ELEMENT(out,i,j) = MAT_ELEMENT(a,i,j);
}
}
}
__attribute__((optimize("-O3"))) void mat_copy_transpose(MATRIX a, MATRIX out)
{
int i, j;
assert(MAT_ROWS(a) == MAT_COLS(out));
assert(MAT_COLS(a) == MAT_ROWS(out));
for (i = 0; i < MAT_ROWS(a); i++) {
for (j = 0; j < MAT_COLS(a); j++) {
MAT_ELEMENT(out,i,j) = MAT_ELEMENT(a,j,i);
}
}
}
__attribute__((optimize("-O3"))) void mat_add(MATRIX a, MATRIX b, MATRIX out)
{
int i, j;
assert(MAT_ROWS(a) == MAT_ROWS(out));
assert(MAT_COLS(a) == MAT_COLS(out));
assert(MAT_ROWS(b) == MAT_ROWS(out));
assert(MAT_COLS(b) == MAT_COLS(out));
for (i = 0; i < MAT_ROWS(a); i++) {
for (j = 0; j < MAT_COLS(a); j++) {
MAT_ELEMENT(out,i,j) = MAT_ELEMENT(a,i,j) + MAT_ELEMENT(b,i,j);
}
}
}
__attribute__((optimize("-O3"))) void mat_sub(MATRIX a, MATRIX b, MATRIX out)
{
int i, j;
assert(MAT_ROWS(a) == MAT_ROWS(out));
assert(MAT_COLS(a) == MAT_COLS(out));
assert(MAT_ROWS(b) == MAT_ROWS(out));
assert(MAT_COLS(b) == MAT_COLS(out));
for (i = 0; i < MAT_ROWS(a); i++) {
for (j = 0; j < MAT_COLS(a); j++) {
MAT_ELEMENT(out,i,j) = MAT_ELEMENT(a,i,j) - MAT_ELEMENT(b,i,j);
}
}
}
__attribute__((optimize("-O3"))) void mat_mul(MATRIX a, MATRIX b, MATRIX out)
{
int i, j, k;
float temp;
assert(MAT_ROWS(a) == MAT_ROWS(out));
assert(MAT_COLS(b) == MAT_COLS(out));
for (i = 0; i < MAT_ROWS(a); i++) {
for (j = 0; j < MAT_COLS(b); j++) {
for (k = 0, temp = 0.0; k < MAT_COLS(a); k++) {
temp += MAT_ELEMENT(a,i,k) * MAT_ELEMENT(b,k,j);
}
MAT_ELEMENT(out,i,j) = temp;
}
}
}
__attribute__((optimize("-O3"))) void mat_mul_transpose(MATRIX a, MATRIX b, MATRIX out)
{
int i, j, k;
float temp;
assert(MAT_ROWS(a) == MAT_ROWS(out));
assert(MAT_ROWS(b) == MAT_COLS(out));
for (i = 0; i < MAT_ROWS(a); i++) {
for (j = 0; j < MAT_ROWS(b); j++) {
for (k = 0, temp = 0.0; k < MAT_COLS(a); k++) {
temp += MAT_ELEMENT(a,i,k) * MAT_ELEMENT(b,j,k);
}
MAT_ELEMENT(out,i,j) = temp;
}
}
}
__attribute__((optimize("-O3"))) void mat_scale(MATRIX a,float b, MATRIX out)
{
int i, j;
assert(MAT_ROWS(a) == MAT_ROWS(out));
assert(MAT_COLS(a) == MAT_COLS(out));
for (i = 0; i < MAT_ROWS(a); i++) {
for (j = 0; j < MAT_COLS(a); j++) {
MAT_ELEMENT(out,i,j) = MAT_ELEMENT(a,i,j) * b;
}
}
}
#define SMALL_NUMBER 1.e-9
/*
Matrix Inversion
by Richard Carling
from "Graphics Gems", Academic Press, 1990
*/
/*
* float = mat_det2x2( a, b, c, d )
*
* calculate the determinant of a 2x2 matrix.
*/
static inline __attribute__((optimize("-O3"))) float mat_det2x2(float a, float b, float c, float d)
{
return a * d - b * c;
}
/*
* float = mat_det3x3( a1, a2, a3, b1, b2, b3, c1, c2, c3 )
*
* calculate the determinant of a 3x3 matrix
* in the form
*
* | a1, b1, c1 |
* | a2, b2, c2 |
* | a3, b3, c3 |
*/
static inline __attribute__((optimize("-O3"))) float mat_det3x3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3)
{
return (+ a1 * mat_det2x2(b2, b3, c2, c3)
- b1 * mat_det2x2(a2, a3, c2, c3)
+ c1 * mat_det2x2(a2, a3, b2, b3));
}
/*
* float = mat_det4x4( matrix )
*
* calculate the determinant of a 4x4 matrix.
*/
static inline __attribute__((optimize("-O3"))) float mat_det4x4(MATRIX m)
{
float a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4;
/* assign to individual variable names to aid selecting */
/* correct elements */
a1 = MAT_ELEMENT(m,0,0); b1 = MAT_ELEMENT(m,0,1);
c1 = MAT_ELEMENT(m,0,2); d1 = MAT_ELEMENT(m,0,3);
a2 = MAT_ELEMENT(m,1,0); b2 = MAT_ELEMENT(m,1,1);
c2 = MAT_ELEMENT(m,1,2); d2 = MAT_ELEMENT(m,1,3);
a3 = MAT_ELEMENT(m,2,0); b3 = MAT_ELEMENT(m,2,1);
c3 = MAT_ELEMENT(m,2,2); d3 = MAT_ELEMENT(m,2,3);
a4 = MAT_ELEMENT(m,3,0); b4 = MAT_ELEMENT(m,3,1);
c4 = MAT_ELEMENT(m,3,2); d4 = MAT_ELEMENT(m,3,3);
return (+ a1 * mat_det3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4)
- b1 * mat_det3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4)
+ c1 * mat_det3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4)
- d1 * mat_det3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4));
}
/*
* adjoint( original_matrix, inverse_matrix )
*
* calculate the adjoint of a 4x4 matrix
*
* Let a denote the minor determinant of matrix A obtained by
* ij
*
* deleting the ith row and jth column from A.
*
* i+j
* Let b = (-1) a
* ij ji
*
* The matrix B = (b ) is the adjoint of A
* ij
*/
static inline __attribute__((optimize("-O3"))) void mat_adjoint(MATRIX m, MATRIX out)
{
float a1, a2, a3, a4, b1, b2, b3, b4;
float c1, c2, c3, c4, d1, d2, d3, d4;
/* assign to individual variable names to aid */
/* selecting correct values */
a1 = MAT_ELEMENT(m,0,0); b1 = MAT_ELEMENT(m,0,1);
c1 = MAT_ELEMENT(m,0,2); d1 = MAT_ELEMENT(m,0,3);
a2 = MAT_ELEMENT(m,1,0); b2 = MAT_ELEMENT(m,1,1);
c2 = MAT_ELEMENT(m,1,2); d2 = MAT_ELEMENT(m,1,3);
a3 = MAT_ELEMENT(m,2,0); b3 = MAT_ELEMENT(m,2,1);
c3 = MAT_ELEMENT(m,2,2); d3 = MAT_ELEMENT(m,2,3);
a4 = MAT_ELEMENT(m,3,0); b4 = MAT_ELEMENT(m,3,1);
c4 = MAT_ELEMENT(m,3,2); d4 = MAT_ELEMENT(m,3,3);
/* row column labeling reversed since we transpose rows & columns */
MAT_ELEMENT(out,0,0) = mat_det3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4);
MAT_ELEMENT(out,1,0) = - mat_det3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4);
MAT_ELEMENT(out,2,0) = mat_det3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4);
MAT_ELEMENT(out,3,0) = - mat_det3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4);
MAT_ELEMENT(out,0,1) = - mat_det3x3(b1, b3, b4, c1, c3, c4, d1, d3, d4);
MAT_ELEMENT(out,1,1) = mat_det3x3(a1, a3, a4, c1, c3, c4, d1, d3, d4);
MAT_ELEMENT(out,2,1) = - mat_det3x3(a1, a3, a4, b1, b3, b4, d1, d3, d4);
MAT_ELEMENT(out,3,1) = mat_det3x3(a1, a3, a4, b1, b3, b4, c1, c3, c4);
MAT_ELEMENT(out,0,2) = mat_det3x3(b1, b2, b4, c1, c2, c4, d1, d2, d4);
MAT_ELEMENT(out,1,2) = - mat_det3x3(a1, a2, a4, c1, c2, c4, d1, d2, d4);
MAT_ELEMENT(out,2,2) = mat_det3x3(a1, a2, a4, b1, b2, b4, d1, d2, d4);
MAT_ELEMENT(out,3,2) = - mat_det3x3(a1, a2, a4, b1, b2, b4, c1, c2, c4);
MAT_ELEMENT(out,0,3) = - mat_det3x3(b1, b2, b3, c1, c2, c3, d1, d2, d3);
MAT_ELEMENT(out,1,3) = mat_det3x3(a1, a2, a3, c1, c2, c3, d1, d2, d3);
MAT_ELEMENT(out,2,3) = - mat_det3x3(a1, a2, a3, b1, b2, b3, d1, d2, d3);
MAT_ELEMENT(out,3,3) = mat_det3x3(a1, a2, a3, b1, b2, b3, c1, c2, c3);
}
/*
* inverse( original_matrix, inverse_matrix )
*
* calculate the inverse of a 4x4 matrix
*
* -1
* A = ___1__ adjoint A
* det A
*/
__attribute__((optimize("-O3"))) int mat_inverse(MATRIX m, MATRIX out)
{
int i, j;
float det;
assert(MAT_ROWS(m) == 4);
assert(MAT_COLS(m) == 4);
assert(MAT_ROWS(out) == 4);
assert(MAT_COLS(out) == 4);
/* calculate the adjoint matrix */
mat_adjoint(m, out);
/* calculate the 4x4 determinant
* if the determinant is zero,
* then the inverse matrix is not unique.
*/
det = mat_det4x4(m);
if (fabs(det) < SMALL_NUMBER) {
return -1;
}
/* scale the adjoint matrix to get the inverse */
for (i=0; i<4; i++) {
for(j=0; j<4; j++) {
MAT_ELEMENT(out,i,j) = MAT_ELEMENT(out,i,j) / det;
}
}
return 0;
}
/*******************************************************************************************************************************/