ISMatrix.h
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2020 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in 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 :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 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 AUTHORS 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 IN THE SOFTWARE.
11 */
12 
13 #ifndef MATRIX_H_
14 #define MATRIX_H_
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #include "ISConstants.h"
21 
22 #include <string.h>
23 
24 //_____ M A C R O S ________________________________________________________
25 
26 
27 // Magnitude Squared or Dot Product of vector w/ itself
28 #define dot_Vec2(v) ((v)[0]*(v)[0] + (v)[1]*(v)[1])
29 #define dot_Vec3(v) ((v)[0]*(v)[0] + (v)[1]*(v)[1] + (v)[2]*(v)[2])
30 #define dot_Vec4(v) ((v)[0]*(v)[0] + (v)[1]*(v)[1] + (v)[2]*(v)[2] + (v)[3]*(v)[3])
31 
32 // Magnitude or Norm
33 #define mag_Vec2(v) (_SQRT(dot_Vec2(v)))
34 #define mag_Vec3(v) (_SQRT(dot_Vec3(v)))
35 #define mag_Vec4(v) (_SQRT(dot_Vec4(v)))
36 #define mag_Vec2d(v) (sqrt(dot_Vec2(v)))
37 #define mag_Vec3d(v) (sqrt(dot_Vec3(v)))
38 #define mag_Vec4d(v) (sqrt(dot_Vec4(v)))
39 
40 #if 1
41 # define recipNorm_Vec2(v) (1.0f/mag_Vec2(v))
42 # define recipNorm_Vec3(v) (1.0f/mag_Vec3(v))
43 # define recipNorm_Vec4(v) (1.0f/mag_Vec4(v))
44 #else // Use fast inverse square root. 0.175% less accurate
45 # define recipNorm_Vec2(v) (invSqrt(dot_Vec2(v)))
46 # define recipNorm_Vec3(v) (invSqrt(dot_Vec3(v)))
47 # define recipNorm_Vec4(v) (invSqrt(dot_Vec4(v)))
48 #endif
49 # define recipNorm_Vec3d(v) (1.0/mag_Vec3d(v))
50 # define recipNorm_Vec4d(v) (1.0/mag_Vec4d(v))
51 
52 #define unwrap_Vec3(v) {UNWRAP_RAD_F32(v[0]); UNWRAP_RAD_F32(v[1]); UNWRAP_RAD_F32(v[2]) }
53 
54 #define Vec3_OneLessThan_X(v,x) ( ((v[0])<(x)) || ((v[1])<(x)) || ((v[2])<(x)) )
55 #define Vec3_OneGrtrThan_X(v,x) ( ((v[0])>(x)) || ((v[1])>(x)) || ((v[2])>(x)) )
56 #define Vec3_AllLessThan_X(v,x) ( ((v[0])<(x)) && ((v[1])<(x)) && ((v[2])<(x)) )
57 #define Vec3_AllGrtrThan_X(v,x) ( ((v[0])>(x)) && ((v[1])>(x)) && ((v[2])>(x)) )
58 #define Vec3_IsAllZero(v) ( ((v[0])==(0.0f)) && ((v[1])==(0.0f)) && ((v[2])==(0.0f)) )
59 #define Vec3_IsAnyZero(v) ( ((v[0])==(0.0f)) || ((v[1])==(0.0f)) || ((v[2])==(0.0f)) )
60 #define Vec3_IsAnyNonZero(v) ( ((v[0])!=(0.0f)) || ((v[1])!=(0.0f)) || ((v[2])!=(0.0f)) )
61 
62 #define set_Vec3_X(v,x) { (v[0])=(x); (v[1])=(x); (v[2])=(x); }
63 #define set_Vec4_X(v,x) { (v[0])=(x); (v[1])=(x); (v[2])=(x); (v[3])=(x); }
64 
65 // Zero order low-pass filter
66 typedef struct
67 {
69  f_t alpha; // alpha gain
70  f_t beta; // beta gain
71 } sLpfO0;
72 
73 // First order low-pass filter
74 typedef struct
75 {
78  f_t alpha; // alpha gain
79  f_t beta; // beta gain
80 } sLpfO1;
81 
82 //_____ G L O B A L S ______________________________________________________
83 
84 //_____ P R O T O T Y P E S ________________________________________________
85 
86 /*
87  * We can avoid expensive floating point operations if one of the
88  * values in question is zero. This provides an easy way to check
89  * to see if it is actually a zero without using any floating point
90  * code.
91  */
92 static __inline char is_zero( const f_t * f )
93 {
94  const unsigned int * x = (const unsigned int*) f;
95 
96  if (*x == 0)
97  return 1;
98  return 0;
99 }
100 
101 
102 #if 0
103 // Fast inverse square-root
104 // See: http://en.wikipedia.org/wiki/Fast_inverse_square_root
105 static __inline float invSqrt(float number)
106 {
107  volatile long i;
108  volatile float x, y;
109  volatile const float f = 1.5F;
110 
111  x = number * 0.5F;
112  y = number;
113  i = * (( long * ) &y);
114  i = 0x5f375a86 - ( i >> 1 );
115  y = * (( float * ) &i);
116  y = y * ( f - ( x * y * y ) );
117  return y;
118 }
119 #endif
120 
121 
122 /*
123  * Perform the matrix multiplication A[m,n] * B[n,p], storing the
124  * result in result[m,p].
125  *
126  * If transpose_B is set, B is assumed to be a [p,n] matrix that is
127  * transversed in column major order instead of row major. This
128  * has the effect of transposing B during the computation.
129  *
130  * If add == 0, OUT = A * B.
131  * If add > 0, OUT += A * B.
132  * If add < 0, OUT -= A * B.
133  */
134 void mul_MatMxN( void * result, const void * A_ptr, const void * B_ptr, i_t m, i_t n, i_t p, char transpose_B, char add );
135 
136 /* Initialize square matrix as identity (0's with 1's in the diagonal)
137 * result(nxn) = eye(nxn)
138 */
139 void eye_MatN( f_t *A, i_t n );
140 
141 /* Matrix Inverse
142 * result(nxn) = M(nxn)^-1
143 */
144 char inv_MatN( f_t *result, const f_t *M, i_t n );
145 
146 
147 /* Matrix Transpose: M[m x n] -> result[n x m]
148 */
149 void trans_MatMxN( f_t *result, const f_t *M, int m, int n );
150 
151 
152 /* Matrix Multiply
153  * result(3x3) = m1(3x3) * m2(3x3)
154  */
155 void mul_Mat3x3_Mat3x3( Matrix3 result, const Matrix3 m1, const Matrix3 m2 );
156 void mul_Mat3x3_Mat3x3_d(Matrix3d result, const Matrix3d m1, const Matrix3d m2);
157 
158 /* Matrix Multiply w/ Transpose
159  * result(3x3) = m1.T(3x3) * m2(3x3)
160  */
161 void mul_Mat3x3_Trans_Mat3x3( Matrix3 result, const Matrix3 m1, const Matrix3 m2 );
162 void mul_Mat3x3_Trans_Mat3x3_d(Matrix3d result, const Matrix3d m1, const Matrix3d m2);
163 
164 /* Matrix Multiply w/ Transpose
165  * result(3x3) = m1(3x3) * m2.T(3x3)
166  */
167 void mul_Mat3x3_Mat3x3_Trans( Matrix3 result, const Matrix3 m1, const Matrix3 m2 );
168 void mul_Mat3x3_Mat3x3_Trans_d(Matrix3d result, const Matrix3d m1, const Matrix3d m2);
169 
170 /* Matrix Multiply
171  * result(1x2) = m(2x2) * v(2x1)
172  */
173 void mul_Mat2x2_Vec2x1( Vector2 result, const Matrix2 m, const Vector2 v );
174 
175 /* Matrix Multiply w/ Transpose
176  * result(1x2) = m(2x2).T * v(2x1)
177  */
178 void mul_Mat2x2_Trans_Vec2x1( Vector2 result, const Matrix2 m, const Vector2 v );
179 
180 /* Matrix Multiply
181  * result(1x3) = m(3x3) * v(3x1)
182  * (9 multiplies, 6 adds)
183  */
184 void mul_Mat3x3_Vec3x1( Vector3 result, const Matrix3 m, const Vector3 v );
185 
186 /* Matrix Multiply w/ Transpose
187  * result(1x3) = m(3x3).T * v(3x1)
188  */
189 void mul_Mat3x3_Trans_Vec3x1( Vector3 result, const Matrix3 m, const Vector3 v );
190 
191 /* Matrix Multiply
192  * result(1x4) = m(4x4) * v(4x1)
193  */
194 void mul_Mat4x4_Vec4x1( Vector4 result, const Matrix4 m, const Vector4 v );
195 
196 /* Matrix Multiply w/ Transpose
197  * result(1x4) = m(4x4).T * v(4x1)
198  */
199 void mul_Mat4x4_Trans_Vec4x1( Vector4 result, const Matrix4 m, const Vector4 v );
200 
201 /* Negate
202 */
203 void neg_Mat3x3(Matrix3 result, const Matrix3 m);
204 
205 /* Multiply
206  * result(3x3) = v1(3x1) * v2(1x3)
207  */
208 void mul_Vec3x1_Vec1x3( Matrix3 result, const Vector3 v1, const Vector3 v2 );
209 
210 /* Multiply
211  * result(3) = v1(3) * v2(3)
212  */
213 void mul_Vec3_Vec3( Vector3 result, const Vector3 v1, const Vector3 v2 );
214 
215 /* Multiply
216  * result(4) = v1(4) .* v2(4)
217  */
218 void mul_Vec4_Vec4( Vector4 result, const Vector4 v1, const Vector4 v2 );
219 
220 /* Square Root
221  * result(3) = .sqrt(v(3))
222  */
223 void sqrt_Vec3( Vector3 result, const Vector3 v );
224 
225 /* Square Root
226  * result(4) = .sqrt(v(4))
227  */
228 void sqrt_Vec4( Vector4 result, const Vector4 v );
229 
230 /* Absolute Value
231  * result(n) = .abs(v(n))
232  */
233 void abs_Vec2( Vector2 result, const Vector2 v );
234 void abs_Vec3(Vector3 result, const Vector3 v);
235 void abs_Vec4( Vector4 result, const Vector4 v );
236 
237 void abs_Vec2d(Vector2d result, const Vector2d v);
238 void abs_Vec3d(Vector3d result, const Vector3d v);
239 void abs_Vec4d(Vector4d result, const Vector4d v);
240 
241 /* Dot product
242  * result = v1(n) dot v2(n)
243  */
244 f_t dot_Vec2_Vec2(const Vector2 v1, const Vector2 v2 );
245 f_t dot_Vec3_Vec3(const Vector3 v1, const Vector3 v2 );
246 f_t dot_Vec4_Vec4(const Vector4 v1, const Vector4 v2 );
247 double dot_Vec3d_Vec3d(const Vector3d v1, const Vector3d v2);
248 
249 /* Cross product
250  * result(3) = v1(3) x v2(3)
251  */
252 void cross_Vec3( Vector3 result, const Vector3 v1, const Vector3 v2 );
253 void crossd_Vec3( Vector3d result, const Vector3 v1, const Vector3 v2 );
254 
255 // /* Vector length
256 // * result(3) = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] )
257 // */
258 // f_t length_Vec3( Vector3 v );
259 
260 /* Multiply
261  * result(2x1) = v(2) .* x
262  */
263 void mul_Vec2_X( Vector2 result, const Vector2 v, const f_t x );
264 void mul_Vec2d_X( Vector2d result, const Vector2d v, const double x );
265 
266 /* Multiply
267  * result(3x1) = v(3) .* x
268  */
269 void mul_Vec3_X( Vector3 result, const Vector3 v, const f_t x );
270 void mul_Vec3d_X( Vector3d result, const Vector3d v, const double x );
271 
272 /* Multiply
273  * result(4x1) = v(4) .* x
274  */
275 void mul_Vec4_X( Vector4 result, const Vector4 v, const f_t x );
276 void mul_Vec4d_X( Vector4d result, const Vector4d v, const double x );
277 
278 /* Divide
279  * result(3x1) = v(3) ./ x
280  */
281 void div_Vec3_X( Vector3 result, const Vector3 v, const f_t x );
282 
283 /* Divide
284  * result(4x1) = v(4) ./ x
285  */
286 void div_Vec4_X( Vector4 result, const Vector4 v, const f_t x );
287 void div_Vec4d_X( Vector4d result, const Vector4d v, const double x );
288 
289 /* Add
290  * result(3) = v1(3) + v2(3)
291  */
292 void add_Vec3_Vec3( Vector3 result, const Vector3 v1, const Vector3 v2 );
293 void add_Vec3d_Vec3d( Vector3d result, const Vector3d v1, const Vector3d v2 );
294 
295 /* Add
296  * result(3) = k1*v1(3) + k2*v2(3)
297  */
298 void add_K1Vec3_K2Vec3(Vector3 result, const Vector3 v1, const Vector3 v2, float k1, float k2);
299 
300 /* Add
301  * result(4) = v1(4) + v2(4)
302  */
303 void add_Vec4_Vec4( Vector4 result, const Vector4 v1, const Vector4 v2 );
304 void add_Vec4d_Vec4d( Vector4d result, const Vector4d v1, const Vector4d v2 );
305 
306 /* Subtract
307  * result(3) = v1(3) - v2(3)
308  */
309 void sub_Vec3_Vec3( Vector3 result, const Vector3 v1, const Vector3 v2 );
310 void sub_Vec3d_Vec3d( Vector3d result, const Vector3d v1, const Vector3d v2 );
311 
312 /* Subtract
313  * result(4) = v1(4) +- v2(4)
314  */
315 void sub_Vec4_Vec4( Vector4 result, const Vector4 v1, const Vector4 v2 );
316 
317 /* Divide
318  * result(3) = v1(3) ./ v2(3)
319  */
320 void div_Vec3_Vec3( Vector3 result, const Vector3 v1, const Vector3 v2 );
321 
322 /* Divide
323  * result(4) = v1(4) ./ v2(4)
324  */
325 void div_Vec4_Vec4( Vector4 result, const Vector4 v1, const Vector4 v2 );
326 
327 /* Negate*/
328 void neg_Vec3(Vector3 result, const Vector3 v);
329 
330 /* Min of vector elements
331  * = min( v[0], v[1], v[2] }
332  */
333 static __inline f_t min_Vec3_X(const Vector3 v )
334 {
335  f_t val = v[0];
336 
337  if( val > v[1] )
338  val = v[1];
339 
340  if( val > v[2] )
341  val = v[2];
342 
343  return val;
344 }
345 
346 /* Max of vector elements
347  * = max( v[0], v[1], v[2] }
348  */
349 static __inline f_t max_Vec3_X(const Vector3 v )
350 {
351  f_t val = v[0];
352 
353  if( val < v[1] )
354  val = v[1];
355 
356  if( val < v[2] )
357  val = v[2];
358 
359  return val;
360 }
361 
362 /* Max of vector elements
363  * = max( v1, v[1], v[2] }
364  */
365 static __inline void min_Vec3( Vector3 result, const Vector3 v1, const Vector3 v2 )
366 {
367  result[0] = _MIN(v1[0], v2[0]);
368  result[1] = _MIN(v1[1], v2[1]);
369  result[2] = _MIN(v1[2], v2[2]);
370 }
371 
372 /* Max of vector elements
373  * = max( v1, v[1], v[2] }
374  */
375 static __inline void max_Vec3( Vector3 result, const Vector3 v1, const Vector3 v2 )
376 {
377  result[0] = _MAX(v1[0], v2[0]);
378  result[1] = _MAX(v1[1], v2[1]);
379  result[2] = _MAX(v1[2], v2[2]);
380 }
381 
382 /* Zero vector
383  * v(3) = { 0, 0, 0 }
384  */
385 static __inline void zero_Vec3( Vector3 v )
386 {
387  memset( v, 0, sizeof(Vector3) );
388 }
389 static __inline void zero_Vec3d( Vector3d v )
390 {
391  memset( v, 0, sizeof(Vector3d) );
392 }
393 
394 /* Zero vector
395  * v(4) = { 0, 0, 0 }
396  */
397 static __inline void zero_Vec4( Vector4 v )
398 {
399  memset( v, 0, sizeof(Vector4) );
400 }
401 static __inline void zero_Vec4d( Vector4d v )
402 {
403  memset( v, 0, sizeof(Vector4d) );
404 }
405 
406 /* Zero vector
407 * v(n) = { 0, ..., 0 }
408 */
409 static __inline void zero_VecN( f_t *v, i_t n )
410 {
411  memset( v, 0, sizeof( f_t )*n );
412 }
413 
414 /* Zero matrix
415 * m(m,n) = { 0, ..., 0 }
416 */
417 static __inline void zero_MatMxN( f_t *M, i_t m, i_t n )
418 {
419  memset( M, 0, sizeof( f_t )*m*n );
420 }
421 
422 
423 /* Copy vector
424  * result(3) = v(3)
425  */
426 static __inline void cpy_Vec3_Vec3( Vector3 result, const Vector3 v )
427 {
428  memcpy( result, v, sizeof(Vector3) );
429 }
430 static __inline void cpy_Vec3d_Vec3d( Vector3d result, const Vector3d v )
431 {
432  memcpy( result, v, sizeof(Vector3d) );
433 }
434 static __inline void cpy_Vec3d_Vec3( Vector3d result, const Vector3 v )
435 {
436  result[0] = (double)v[0];
437  result[1] = (double)v[1];
438  result[2] = (double)v[2];
439 }
440 static __inline void cpy_Vec3_Vec3d( Vector3 result, const Vector3d v )
441 {
442  result[0] = (f_t)v[0];
443  result[1] = (f_t)v[1];
444  result[2] = (f_t)v[2];
445 }
446 
447 /* Copy vector
448 * result(n) = v(n)
449 */
450 static __inline void cpy_VecN_VecN( f_t *result, const f_t *v, i_t n )
451 {
452  memcpy( result, v, sizeof( f_t )*n );
453 }
454 
455 /* Copy vector
456  * result(4) = v(4)
457  */
458 static __inline void cpy_Vec4_Vec4( Vector4 result, const Vector4 v )
459 {
460  memcpy( result, v, sizeof(Vector4) );
461 }
462 static __inline void cpy_Vec4d_Vec4d( Vector4d result, const Vector4d v )
463 {
464  memcpy( result, v, sizeof(Vector4d) );
465 }
466 static __inline void cpy_Vec4d_Vec4( Vector4d result, const Vector4 v )
467 {
468  result[0] = (double)v[0];
469  result[1] = (double)v[1];
470  result[2] = (double)v[2];
471  result[3] = (double)v[3];
472 }
473 static __inline void cpy_Vec4_Vec4d( Vector4 result, const Vector4d v )
474 {
475  result[0] = (f_t)v[0];
476  result[1] = (f_t)v[1];
477  result[2] = (f_t)v[2];
478  result[3] = (f_t)v[3];
479 }
480 
481 /* Copy matrix
482 * result(mxn) = M(mxn)
483 */
484 static __inline void cpy_MatMxN( f_t *result, const f_t *M, i_t m, i_t n )
485 {
486  memcpy( result, M, sizeof( f_t )*m*n );
487 }
488 
489 /* Copy matrix A(mxn) into result(rxc) matrix, starting at offset_r, offset_c. Matrix A must fit inside result matrix dimensions.
490 * result(rxc) <= A(mxn)
491 */
492 void cpy_MatRxC_MatMxN( f_t *result, i_t r, i_t c, i_t r_offset, i_t c_offset, f_t *A, i_t m, i_t n );
493 
494 
495 /* Matrix Transpose
496  * result(2x2) = m(2x2)'
497  */
498 void transpose_Mat2( Matrix2 result, const Matrix2 m );
499 
500 /* Matrix Transpose
501  * result(3x3) = m(3x3)'
502  */
503 void transpose_Mat3( Matrix3 result, const Matrix3 m );
504 
505 /* Matrix Transpose
506  * result(4x4) = m(4x4)'
507  */
508 void transpose_Mat4( Matrix4 result, const Matrix4 m );
509 
510 /*
511  * Invert a 2x2 matrix.
512  */
513 char inv_Mat2( Matrix2 result, Matrix2 m );
514 
515 /* Matrix Inverse
516  * result(3x3) = m(3x3)^-1
517  * return 0 on success, -1 on numerical error
518  */
519 char inv_Mat3( Matrix3 result, const Matrix3 m );
520 
521 /* Matrix Inverse
522  * result(4x4) = m(4x4)^-1
523  * return 0 on success, -1 on numerical error
524  */
525 char inv_Mat4( Matrix4 result, const Matrix4 m );
526 
527 /*
528  * Normalize 2 dimensional vector
529  */
530 static __inline void normalize_Vec2( Vector2 v )
531 {
532  // Normalize vector
533  mul_Vec2_X( v, v, recipNorm_Vec2(v) );
534 }
535 
536 /*
537  * Normalize 3 dimensional vector
538  */
539 static __inline void normalize_Vec3( Vector3 result, const Vector3 v )
540 {
541  // Normalize vector
542  mul_Vec3_X( result, v, recipNorm_Vec3(v) );
543 }
544 
545 /*
546  * Normalize 4 dimensional vector
547  */
548 static __inline void normalize_Vec4( Vector4 result, const Vector4 v )
549 {
550  // Normalize vector
551  mul_Vec4_X( result, v, recipNorm_Vec4(v) );
552 }
553 static __inline void normalize_Vec4d( Vector4d result, const Vector4d v )
554 {
555  // Normalize vector
556  mul_Vec4d_X( result, v, recipNorm_Vec4d(v) );
557 }
558 
559 /*
560 * Check if 3 dimensional vectors are equal
561 */
562 static __inline int is_equal_Vec3(const Vector3 v1, const Vector3 v2)
563 {
564  return
565  (v1[0] == v2[0]) &&
566  (v1[1] == v2[1]) &&
567  (v1[2] == v2[2]);
568 }
569 
570 /*
571 * Check if 4 dimensional vectors are equal
572 */
573 static __inline int is_equal_Vec4(const Vector4 v1, const Vector4 v2)
574 {
575  return
576  (v1[0] == v2[0]) &&
577  (v1[1] == v2[1]) &&
578  (v1[2] == v2[2]) &&
579  (v1[3] == v2[3]);
580 }
581 
582 /*
583 * Limit 3 dimensional vector to +- specified limit
584 */
585 static __inline void limit_Vec3( Vector3 v, f_t limit )
586 {
587  _LIMIT( v[0], limit );
588  _LIMIT( v[1], limit );
589  _LIMIT( v[2], limit );
590 }
591 
592 /*
593 * Limit 3 dimensional vector to min and max values
594 */
595 static __inline void limit2_Vec3( Vector3 v, f_t min, f_t max )
596 {
597  _LIMIT2( v[0], min, max );
598  _LIMIT2( v[1], min, max );
599  _LIMIT2( v[2], min, max );
600 }
601 
602 
603 /* Array contains NAN
604  * return 1 if NAN found in array, 0 if not
605  */
606 static __inline int isNan_array( f_t *a, int size )
607 {
608  int i;
609 
610  for( i=0; i<size; i++ )
611  {
612  if( a[i] != a[i] )
613  return 1;
614  }
615 
616  return 0;
617 }
618 
619 
620 /* Array contains NAN
621  * return 1 if NAN found in double array, 0 if not
622  */
623 static __inline int isNan_array_d( double *a, int size )
624 {
625  int i;
626 
627  for( i=0; i<size; i++ )
628  {
629  if( a[i] != a[i] )
630  return 1;
631  }
632 
633  return 0;
634 }
635 
636 #if defined(PLATFORM_IS_WINDOWS)
637 #pragma warning( push )
638 #pragma warning( disable : 4723)
639 #endif
640 
641 /* Array contains INF
642  * return 1 if INF found in array, 0 if not
643  */
644 static __inline int isInf_array( f_t *a, int size )
645 {
646  int i;
647 
648  f_t tmp = 1.0f;
649  f_t inf = 1.0f / ( tmp - 1.0f);
650 
651  for( i=0; i<size; i++ )
652  {
653  if( a[i] == inf )
654  return 1;
655  }
656 
657  return 0;
658 }
659 
660 
661 /* Array contains INF
662 * return 1 if INF found in double array, 0 if not
663 */
664 static __inline int isInf_array_d(double *a, int size)
665 {
666  int i;
667 
668  double tmp = 1.0f;
669  double inf = 1.0f / (tmp - 1.0f);
670 
671  for (i = 0; i<size; i++)
672  {
673  if (a[i] == inf)
674  return 1;
675  }
676 
677  return 0;
678 }
679 
680 #if defined(PLATFORM_IS_WINDOWS)
681 #pragma warning( pop )
682 #endif
683 
684 /* Array does not contain NAN or INF
685  * return 0 if NAN or INF found in array, 1 if not
686  */
687 static __inline int isFinite_array( f_t *a, int size )
688 {
689  if( isNan_array(a, size) )
690  return 0;
691 
692  if( isInf_array(a, size) )
693  return 0;
694 
695  return 1;
696 }
697 
698 
699 /* Array does not contain NAN or INF
700 * return 0 if NAN or INF found in double array, 1 if not
701 */
702 static __inline int isFinite_array_d(double *a, int size)
703 {
704  if (isNan_array_d(a, size))
705  return 0;
706 
707  if (isInf_array_d(a, size))
708  return 0;
709 
710  return 1;
711 }
712 
713 
714 // Low-Pass Alpha Filter
715 void LPFO0_init_Vec3( sLpfO0 *lpf, f_t dt, f_t cornerFreqHz, const Vector3 initVal );
716 // output[n+1] = beta*output[n] + alpha*input
717 void LPFO0_Vec3( sLpfO0 *lpf, const Vector3 input );
718 
719 // Zero order Low-Pass Filter
720 static __inline void O0_LPF_Vec3( Vector3 result, const Vector3 input, f_t alph, f_t beta )
721 {
722  Vector3 tmp3;
723 
724  // val[n+1] = beta*val[n] + alpha*input
725  mul_Vec3_X( tmp3, input, alph );
726  mul_Vec3_X( result, result, beta );
727  add_Vec3_Vec3( result, result, tmp3 );
728 }
729 
730 
731 // First order Low-Pass Filter
732 static __inline void O1_LPF_Vec3( Vector3 result, const Vector3 input, Vector3 c1, f_t alph, f_t beta, f_t dt )
733 {
734  Vector3 tmp3;
735 
736  // Estimate next models coefficients: d1 = (input - result) / dt
737  sub_Vec3_Vec3( tmp3, input, result );
738  div_Vec3_X( tmp3, tmp3, dt );
739 
740  // LPF these coefficients: c1 = beta*c1 + alph*d1
741  O0_LPF_Vec3( c1, tmp3, alph, beta );
742 
743  // Current state estimates: est = (last result) + c1*dt
744  mul_Vec3_X( tmp3, c1, dt );
745  add_Vec3_Vec3( result, result, tmp3 );
746 
747  // LPF input into state estimates: result = beta*est + alph*input
748  O0_LPF_Vec3( result, input, alph, beta );
749 }
750 
751 
752 
753 #ifdef __cplusplus
754 }
755 #endif
756 
757 #endif /* MATRIX_H_ */
#define _MIN(a, b)
Definition: ISConstants.h:298
static __inline void limit_Vec3(Vector3 v, f_t limit)
Definition: ISMatrix.h:585
double Vector2d[2]
Definition: ISConstants.h:788
void mul_Mat3x3_Mat3x3_d(Matrix3d result, const Matrix3d m1, const Matrix3d m2)
Definition: ISMatrix.c:338
#define recipNorm_Vec4d(v)
Definition: ISMatrix.h:50
void mul_Mat3x3_Mat3x3_Trans(Matrix3 result, const Matrix3 m1, const Matrix3 m2)
Definition: ISMatrix.c:386
def lpf(data, cornerFreqHz, dt=None, time=None, initVal=None)
Definition: filterTools.py:89
static __inline int isFinite_array_d(double *a, int size)
Definition: ISMatrix.h:702
float f_t
Definition: ISConstants.h:786
void mul_Mat2x2_Trans_Vec2x1(Vector2 result, const Matrix2 m, const Vector2 v)
Definition: ISMatrix.c:424
void LPFO0_Vec3(sLpfO0 *lpf, const Vector3 input)
Definition: ISMatrix.c:1036
static __inline void cpy_Vec3d_Vec3(Vector3d result, const Vector3 v)
Definition: ISMatrix.h:434
f
static __inline void zero_MatMxN(f_t *M, i_t m, i_t n)
Definition: ISMatrix.h:417
void abs_Vec4d(Vector4d result, const Vector4d v)
Definition: ISMatrix.c:556
void abs_Vec4(Vector4 result, const Vector4 v)
Definition: ISMatrix.c:548
void mul_Vec3_X(Vector3 result, const Vector3 v, const f_t x)
Definition: ISMatrix.c:620
static __inline void zero_Vec4d(Vector4d v)
Definition: ISMatrix.h:401
static __inline void max_Vec3(Vector3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.h:375
Vector3 c1
Definition: ISMatrix.h:77
void transpose_Mat3(Matrix3 result, const Matrix3 m)
Definition: ISMatrix.c:788
static __inline void cpy_Vec4d_Vec4d(Vector4d result, const Vector4d v)
Definition: ISMatrix.h:462
static __inline f_t min_Vec3_X(const Vector3 v)
Definition: ISMatrix.h:333
static __inline void zero_Vec3d(Vector3d v)
Definition: ISMatrix.h:389
void trans_MatMxN(f_t *result, const f_t *M, int m, int n)
Definition: ISMatrix.c:298
#define recipNorm_Vec4(v)
Definition: ISMatrix.h:43
void add_Vec4_Vec4(Vector4 result, const Vector4 v1, const Vector4 v2)
Definition: ISMatrix.c:696
f_t Vector2[2]
Definition: ISConstants.h:789
void sub_Vec3_Vec3(Vector3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:712
static __inline void min_Vec3(Vector3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.h:365
#define recipNorm_Vec2(v)
Definition: ISMatrix.h:41
static __inline void zero_Vec3(Vector3 v)
Definition: ISMatrix.h:385
void add_Vec3_Vec3(Vector3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:675
static __inline int isInf_array(f_t *a, int size)
Definition: ISMatrix.h:644
int i_t
Definition: ISConstants.h:787
void mul_Vec3d_X(Vector3d result, const Vector3d v, const double x)
Definition: ISMatrix.c:627
#define max(a, b)
Takes the maximal value of a and b.
Definition: compiler.h:823
void div_Vec4_X(Vector4 result, const Vector4 v, const f_t x)
Definition: ISMatrix.c:658
f_t Matrix2[4]
Definition: ISConstants.h:798
static __inline int isFinite_array(f_t *a, int size)
Definition: ISMatrix.h:687
static __inline void normalize_Vec4(Vector4 result, const Vector4 v)
Definition: ISMatrix.h:548
void mul_Mat3x3_Vec3x1(Vector3 result, const Matrix3 m, const Vector3 v)
Definition: ISMatrix.c:430
bool add(const actionlib::TwoIntsGoal &req, actionlib::TwoIntsResult &res)
void sqrt_Vec3(Vector3 result, const Vector3 v)
Definition: ISMatrix.c:507
void LPFO0_init_Vec3(sLpfO0 *lpf, f_t dt, f_t cornerFreqHz, const Vector3 initVal)
Definition: ISMatrix.c:1023
void div_Vec4d_X(Vector4d result, const Vector4d v, const double x)
Definition: ISMatrix.c:666
void eye_MatN(f_t *A, i_t n)
Definition: ISMatrix.c:119
f_t beta
Definition: ISMatrix.h:79
static __inline char is_zero(const f_t *f)
Definition: ISMatrix.h:92
void abs_Vec3(Vector3 result, const Vector3 v)
Definition: ISMatrix.c:534
void add_K1Vec3_K2Vec3(Vector3 result, const Vector3 v1, const Vector3 v2, float k1, float k2)
Definition: ISMatrix.c:689
#define _LIMIT2(x, xmin, xmax)
Definition: ISConstants.h:314
static __inline int isNan_array(f_t *a, int size)
Definition: ISMatrix.h:606
void abs_Vec3d(Vector3d result, const Vector3d v)
Definition: ISMatrix.c:541
static __inline int is_equal_Vec3(const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.h:562
char inv_Mat2(Matrix2 result, Matrix2 m)
Definition: ISMatrix.c:828
double dot_Vec3d_Vec3d(const Vector3d v1, const Vector3d v2)
Definition: ISMatrix.c:577
f_t Matrix4[16]
Definition: ISConstants.h:800
void mul_Vec3_Vec3(Vector3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:492
void add_Vec3d_Vec3d(Vector3d result, const Vector3d v1, const Vector3d v2)
Definition: ISMatrix.c:682
static __inline void cpy_MatMxN(f_t *result, const f_t *M, i_t m, i_t n)
Definition: ISMatrix.h:484
void mul_Vec2d_X(Vector2d result, const Vector2d v, const double x)
Definition: ISMatrix.c:614
f_t Vector4[4]
Definition: ISConstants.h:793
void neg_Mat3x3(Matrix3 result, const Matrix3 m)
Definition: ISMatrix.c:460
static __inline void normalize_Vec4d(Vector4d result, const Vector4d v)
Definition: ISMatrix.h:553
Vector3 v
Definition: ISMatrix.h:68
void mul_Mat2x2_Vec2x1(Vector2 result, const Matrix2 m, const Vector2 v)
Definition: ISMatrix.c:418
void abs_Vec2(Vector2 result, const Vector2 v)
Definition: ISMatrix.c:522
void mul_Mat3x3_Trans_Mat3x3(Matrix3 result, const Matrix3 m1, const Matrix3 m2)
Definition: ISMatrix.c:354
f_t dot_Vec3_Vec3(const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:570
void cpy_MatRxC_MatMxN(f_t *result, i_t r, i_t c, i_t r_offset, i_t c_offset, f_t *A, i_t m, i_t n)
Definition: ISMatrix.c:756
void sub_Vec3d_Vec3d(Vector3d result, const Vector3d v1, const Vector3d v2)
Definition: ISMatrix.c:719
void mul_Vec4_X(Vector4 result, const Vector4 v, const f_t x)
Definition: ISMatrix.c:634
static __inline void normalize_Vec2(Vector2 v)
Definition: ISMatrix.h:530
void mul_Mat3x3_Mat3x3(Matrix3 result, const Matrix3 m1, const Matrix3 m2)
Definition: ISMatrix.c:322
f_t beta
Definition: ISMatrix.h:70
void mul_Mat4x4_Trans_Vec4x1(Vector4 result, const Matrix4 m, const Vector4 v)
Definition: ISMatrix.c:452
static __inline void zero_VecN(f_t *v, i_t n)
Definition: ISMatrix.h:409
char inv_Mat3(Matrix3 result, const Matrix3 m)
Definition: ISMatrix.c:847
void div_Vec4_Vec4(Vector4 result, const Vector4 v1, const Vector4 v2)
Definition: ISMatrix.c:741
void sub_Vec4_Vec4(Vector4 result, const Vector4 v1, const Vector4 v2)
Definition: ISMatrix.c:726
void mul_Vec3x1_Vec1x3(Matrix3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:476
static __inline void cpy_Vec4_Vec4d(Vector4 result, const Vector4d v)
Definition: ISMatrix.h:473
static __inline void cpy_Vec3_Vec3(Vector3 result, const Vector3 v)
Definition: ISMatrix.h:426
#define _LIMIT(x, lim)
Definition: ISConstants.h:310
static __inline void O0_LPF_Vec3(Vector3 result, const Vector3 input, f_t alph, f_t beta)
Definition: ISMatrix.h:720
void mul_Vec2_X(Vector2 result, const Vector2 v, const f_t x)
Definition: ISMatrix.c:608
f_t Matrix3[9]
Definition: ISConstants.h:799
#define _MAX(a, b)
Definition: ISConstants.h:294
static __inline void cpy_Vec4d_Vec4(Vector4d result, const Vector4 v)
Definition: ISMatrix.h:466
double Vector3d[3]
Definition: ISConstants.h:790
#define recipNorm_Vec3(v)
Definition: ISMatrix.h:42
double Matrix3d[9]
Definition: ISConstants.h:802
void mul_Vec4d_X(Vector4d result, const Vector4d v, const double x)
Definition: ISMatrix.c:642
void sqrt_Vec4(Vector4 result, const Vector4 v)
Definition: ISMatrix.c:514
void add_Vec4d_Vec4d(Vector4d result, const Vector4d v1, const Vector4d v2)
Definition: ISMatrix.c:704
void transpose_Mat4(Matrix4 result, const Matrix4 m)
Definition: ISMatrix.c:804
void neg_Vec3(Vector3 result, const Vector3 v)
Definition: ISMatrix.c:749
void cross_Vec3(Vector3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:594
static __inline f_t max_Vec3_X(const Vector3 v)
Definition: ISMatrix.h:349
f_t dot_Vec2_Vec2(const Vector2 v1, const Vector2 v2)
Definition: ISMatrix.c:564
static __inline void cpy_VecN_VecN(f_t *result, const f_t *v, i_t n)
Definition: ISMatrix.h:450
static __inline void normalize_Vec3(Vector3 result, const Vector3 v)
Definition: ISMatrix.h:539
static __inline int isNan_array_d(double *a, int size)
Definition: ISMatrix.h:623
f_t dot_Vec4_Vec4(const Vector4 v1, const Vector4 v2)
Definition: ISMatrix.c:584
void abs_Vec2d(Vector2d result, const Vector2d v)
Definition: ISMatrix.c:528
static __inline int is_equal_Vec4(const Vector4 v1, const Vector4 v2)
Definition: ISMatrix.h:573
static __inline void limit2_Vec3(Vector3 v, f_t min, f_t max)
Definition: ISMatrix.h:595
f_t alpha
Definition: ISMatrix.h:78
f_t alpha
Definition: ISMatrix.h:69
static __inline void cpy_Vec3_Vec3d(Vector3 result, const Vector3d v)
Definition: ISMatrix.h:440
char inv_MatN(f_t *result, const f_t *M, i_t n)
Definition: ISMatrix.c:258
void mul_Mat3x3_Mat3x3_Trans_d(Matrix3d result, const Matrix3d m1, const Matrix3d m2)
Definition: ISMatrix.c:402
static __inline void cpy_Vec3d_Vec3d(Vector3d result, const Vector3d v)
Definition: ISMatrix.h:430
static __inline int isInf_array_d(double *a, int size)
Definition: ISMatrix.h:664
char inv_Mat4(Matrix4 result, const Matrix4 m)
Definition: ISMatrix.c:876
void crossd_Vec3(Vector3d result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:601
static __inline void O1_LPF_Vec3(Vector3 result, const Vector3 input, Vector3 c1, f_t alph, f_t beta, f_t dt)
Definition: ISMatrix.h:732
Vector3 v
Definition: ISMatrix.h:76
void mul_Mat4x4_Vec4x1(Vector4 result, const Matrix4 m, const Vector4 v)
Definition: ISMatrix.c:444
void mul_Mat3x3_Trans_Mat3x3_d(Matrix3d result, const Matrix3d m1, const Matrix3d m2)
Definition: ISMatrix.c:370
void div_Vec3_X(Vector3 result, const Vector3 v, const f_t x)
Definition: ISMatrix.c:650
void mul_Vec4_Vec4(Vector4 result, const Vector4 v1, const Vector4 v2)
Definition: ISMatrix.c:499
void transpose_Mat2(Matrix2 result, const Matrix2 m)
Definition: ISMatrix.c:778
static __inline void cpy_Vec4_Vec4(Vector4 result, const Vector4 v)
Definition: ISMatrix.h:458
void div_Vec3_Vec3(Vector3 result, const Vector3 v1, const Vector3 v2)
Definition: ISMatrix.c:734
void mul_Mat3x3_Trans_Vec3x1(Vector3 result, const Matrix3 m, const Vector3 v)
Definition: ISMatrix.c:437
void mul_MatMxN(void *result, const void *A_ptr, const void *B_ptr, i_t m, i_t n, i_t p, char transpose_B, char add)
Definition: ISMatrix.c:22
static __inline void zero_Vec4(Vector4 v)
Definition: ISMatrix.h:397
double Vector4d[4]
Definition: ISConstants.h:792


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04