ISMatrix.h
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2021 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(ixMatrix3 result, const ixMatrix3 m1, const ixMatrix3 m2 );
156 void mul_Mat3x3_Mat3x3_d(ixMatrix3d result, const ixMatrix3d m1, const ixMatrix3d m2);
157 
158 /* Matrix Multiply w/ Transpose
159  * result(3x3) = m1.T(3x3) * m2(3x3)
160  */
161 void mul_Mat3x3_Trans_Mat3x3(ixMatrix3 result, const ixMatrix3 m1, const ixMatrix3 m2 );
162 void mul_Mat3x3_Trans_Mat3x3_d(ixMatrix3d result, const ixMatrix3d m1, const ixMatrix3d m2);
163 
164 /* Matrix Multiply w/ Transpose
165  * result(3x3) = m1(3x3) * m2.T(3x3)
166  */
167 void mul_Mat3x3_Mat3x3_Trans(ixMatrix3 result, const ixMatrix3 m1, const ixMatrix3 m2 );
168 void mul_Mat3x3_Mat3x3_Trans_d(ixMatrix3d result, const ixMatrix3d m1, const ixMatrix3d m2);
169 
170 /* Matrix Multiply
171  * result(1x2) = m(2x2) * v(2x1)
172  */
173 void mul_Mat2x2_Vec2x1( ixVector2 result, const ixMatrix2 m, const ixVector2 v );
174 
175 /* Matrix Multiply w/ Transpose
176  * result(1x2) = m(2x2).T * v(2x1)
177  */
178 void mul_Mat2x2_Trans_Vec2x1( ixVector2 result, const ixMatrix2 m, const ixVector2 v );
179 
180 /* Matrix Multiply
181  * result(1x3) = m(3x3) * v(3x1)
182  * (9 multiplies, 6 adds)
183  */
184 void mul_Mat3x3_Vec3x1( ixVector3 result, const ixMatrix3 m, const ixVector3 v );
185 
186 /* Matrix Multiply w/ Transpose
187  * result(1x3) = m(3x3).T * v(3x1)
188  */
189 void mul_Mat3x3_Trans_Vec3x1( ixVector3 result, const ixMatrix3 m, const ixVector3 v );
190 
191 /* Matrix Multiply
192  * result(1x4) = m(4x4) * v(4x1)
193  */
194 void mul_Mat4x4_Vec4x1( ixVector4 result, const ixMatrix4 m, const ixVector4 v );
195 
196 /* Matrix Multiply w/ Transpose
197  * result(1x4) = m(4x4).T * v(4x1)
198  */
199 void mul_Mat4x4_Trans_Vec4x1( ixVector4 result, const ixMatrix4 m, const ixVector4 v );
200 
201 /* Negate
202 */
203 void neg_Mat3x3(ixMatrix3 result, const ixMatrix3 m);
204 
205 /* Multiply
206  * result(3x3) = v1(3x1) * v2(1x3)
207  */
208 void mul_Vec3x1_Vec1x3( ixMatrix3 result, const ixVector3 v1, const ixVector3 v2 );
209 
210 /* Multiply
211  * result(3) = v1(3) * v2(3)
212  */
213 void mul_Vec3_Vec3( ixVector3 result, const ixVector3 v1, const ixVector3 v2 );
214 
215 /* Multiply
216  * result(4) = v1(4) .* v2(4)
217  */
218 void mul_Vec4_Vec4( ixVector4 result, const ixVector4 v1, const ixVector4 v2 );
219 
220 /* Square Root
221  * result(3) = .sqrt(v(3))
222  */
223 void sqrt_Vec3( ixVector3 result, const ixVector3 v );
224 
225 /* Square Root
226  * result(4) = .sqrt(v(4))
227  */
228 void sqrt_Vec4( ixVector4 result, const ixVector4 v );
229 
230 /* Absolute Value
231  * result(n) = .abs(v(n))
232  */
233 void abs_Vec2(ixVector2 result, const ixVector2 v );
234 void abs_Vec3(ixVector3 result, const ixVector3 v);
235 void abs_Vec4(ixVector4 result, const ixVector4 v );
236 
237 void abs_Vec2d(ixVector2d result, const ixVector2d v);
238 void abs_Vec3d(ixVector3d result, const ixVector3d v);
239 void abs_Vec4d(ixVector4d result, const ixVector4d v);
240 
241 /* Dot product
242  * result = v1(n) dot v2(n)
243  */
244 f_t dot_Vec2_Vec2(const ixVector2 v1, const ixVector2 v2 );
245 f_t dot_Vec3_Vec3(const ixVector3 v1, const ixVector3 v2 );
246 f_t dot_Vec4_Vec4(const ixVector4 v1, const ixVector4 v2 );
247 double dot_Vec3d_Vec3d(const ixVector3d v1, const ixVector3d v2);
248 
249 /* Cross product
250  * result(3) = v1(3) x v2(3)
251  */
252 void cross_Vec3( ixVector3 result, const ixVector3 v1, const ixVector3 v2 );
253 void crossd_Vec3( ixVector3d result, const ixVector3 v1, const ixVector3 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( ixVector3 v );
259 
260 /* Multiply
261  * result(2x1) = v(2) .* x
262  */
263 void mul_Vec2_X( ixVector2 result, const ixVector2 v, const f_t x );
264 void mul_Vec2d_X( ixVector2d result, const ixVector2d v, const double x );
265 
266 /* Multiply
267  * result(3x1) = v(3) .* x
268  */
269 void mul_Vec3_X( ixVector3 result, const ixVector3 v, const f_t x );
270 void mul_Vec3d_X( ixVector3d result, const ixVector3d v, const double x );
271 
272 /* Multiply
273  * result(4x1) = v(4) .* x
274  */
275 void mul_Vec4_X( ixVector4 result, const ixVector4 v, const f_t x );
276 void mul_Vec4d_X( ixVector4d result, const ixVector4d v, const double x );
277 
278 /* Divide
279  * result(3x1) = v(3) ./ x
280  */
281 void div_Vec3_X( ixVector3 result, const ixVector3 v, const f_t x );
282 
283 /* Divide
284  * result(4x1) = v(4) ./ x
285  */
286 void div_Vec4_X( ixVector4 result, const ixVector4 v, const f_t x );
287 void div_Vec4d_X( ixVector4d result, const ixVector4d v, const double x );
288 
289 /* Add
290  * result(3) = v1(3) + v2(3)
291  */
292 void add_Vec3_Vec3( ixVector3 result, const ixVector3 v1, const ixVector3 v2 );
293 void add_Vec3d_Vec3d( ixVector3d result, const ixVector3d v1, const ixVector3d v2 );
294 
295 /* Add
296  * result(3) = k1*v1(3) + k2*v2(3)
297  */
298 void add_K1Vec3_K2Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2, float k1, float k2);
299 
300 /* Add
301  * result(4) = v1(4) + v2(4)
302  */
303 void add_Vec4_Vec4( ixVector4 result, const ixVector4 v1, const ixVector4 v2 );
304 void add_Vec4d_Vec4d( ixVector4d result, const ixVector4d v1, const ixVector4d v2 );
305 
306 /* Subtract
307  * result(3) = v1(3) - v2(3)
308  */
309 void sub_Vec3_Vec3( ixVector3 result, const ixVector3 v1, const ixVector3 v2 );
310 void sub_Vec3d_Vec3d( ixVector3d result, const ixVector3d v1, const ixVector3d v2 );
311 
312 /* Subtract
313  * result(4) = v1(4) +- v2(4)
314  */
315 void sub_Vec4_Vec4( ixVector4 result, const ixVector4 v1, const ixVector4 v2 );
316 
317 /* Divide
318  * result(3) = v1(3) ./ v2(3)
319  */
320 void div_Vec3_Vec3( ixVector3 result, const ixVector3 v1, const ixVector3 v2 );
321 
322 /* Divide
323  * result(4) = v1(4) ./ v2(4)
324  */
325 void div_Vec4_Vec4( ixVector4 result, const ixVector4 v1, const ixVector4 v2 );
326 
327 /* Negate*/
328 void neg_Vec3(ixVector3 result, const ixVector3 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 ixVector3 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 ixVector3 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( ixVector3 result, const ixVector3 v1, const ixVector3 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( ixVector3 result, const ixVector3 v1, const ixVector3 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( ixVector3 v )
386 {
387  memset( v, 0, sizeof(ixVector3) );
388 }
389 static __inline void zero_Vec3d( ixVector3d v )
390 {
391  memset( v, 0, sizeof(ixVector3d) );
392 }
393 
394 /* Zero vector
395  * v(4) = { 0, 0, 0 }
396  */
397 static __inline void zero_Vec4( ixVector4 v )
398 {
399  memset( v, 0, sizeof(ixVector4) );
400 }
401 static __inline void zero_Vec4d( ixVector4d v )
402 {
403  memset( v, 0, sizeof(ixVector4d) );
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( ixVector3 result, const ixVector3 v )
427 {
428  memcpy( result, v, sizeof(ixVector3) );
429 }
430 static __inline void cpy_Vec3d_Vec3d( ixVector3d result, const ixVector3d v )
431 {
432  memcpy( result, v, sizeof(ixVector3d) );
433 }
434 static __inline void cpy_Vec3d_Vec3( ixVector3d result, const ixVector3 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( ixVector3 result, const ixVector3d 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( ixVector4 result, const ixVector4 v )
459 {
460  memcpy( result, v, sizeof(ixVector4) );
461 }
462 static __inline void cpy_Vec4d_Vec4d( ixVector4d result, const ixVector4d v )
463 {
464  memcpy( result, v, sizeof(ixVector4d) );
465 }
466 static __inline void cpy_Vec4d_Vec4( ixVector4d result, const ixVector4 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( ixVector4 result, const ixVector4d 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( ixMatrix2 result, const ixMatrix2 m );
499 
500 /* Matrix Transpose
501  * result(3x3) = m(3x3)'
502  */
503 void transpose_Mat3( ixMatrix3 result, const ixMatrix3 m );
504 
505 /* Matrix Transpose
506  * result(4x4) = m(4x4)'
507  */
508 void transpose_Mat4( ixMatrix4 result, const ixMatrix4 m );
509 
510 /*
511  * Invert a 2x2 matrix.
512  */
513 char inv_Mat2( ixMatrix2 result, ixMatrix2 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( ixMatrix3 result, const ixMatrix3 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( ixMatrix4 result, const ixMatrix4 m );
526 
527 /*
528  * Normalize 2 dimensional vector
529  */
530 static __inline void normalize_Vec2( ixVector2 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( ixVector3 result, const ixVector3 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( ixVector4 result, const ixVector4 v )
549 {
550  // Normalize vector
551  mul_Vec4_X( result, v, recipNorm_Vec4(v) );
552 }
553 static __inline void normalize_Vec4d( ixVector4d result, const ixVector4d v )
554 {
555  // Normalize vector
556  mul_Vec4d_X( result, v, recipNorm_Vec4d(v) );
557 }
558 
559 /*
560 * Check if 2 dimensional vectors are equal
561 */
562 static __inline int is_equal_Vec2(const ixVector2 v1, const ixVector2 v2)
563 {
564  return
565  (v1[0] == v2[0]) &&
566  (v1[1] == v2[1]);
567 }
568 
569 /*
570 * Check if 3 dimensional vectors are equal
571 */
572 static __inline int is_equal_Vec3(const ixVector3 v1, const ixVector3 v2)
573 {
574  return
575  (v1[0] == v2[0]) &&
576  (v1[1] == v2[1]) &&
577  (v1[2] == v2[2]);
578 }
579 
580 /*
581 * Check if 4 dimensional vectors are equal
582 */
583 static __inline int is_equal_Vec4(const ixVector4 v1, const ixVector4 v2)
584 {
585  return
586  (v1[0] == v2[0]) &&
587  (v1[1] == v2[1]) &&
588  (v1[2] == v2[2]) &&
589  (v1[3] == v2[3]);
590 }
591 
592 /*
593 * Limit 3 dimensional vector to +- specified limit
594 */
595 static __inline void limit_Vec3( ixVector3 v, f_t limit )
596 {
597  _LIMIT( v[0], limit );
598  _LIMIT( v[1], limit );
599  _LIMIT( v[2], limit );
600 }
601 
602 /*
603 * Limit 3 dimensional vector to min and max values
604 */
605 static __inline void limit2_Vec3( ixVector3 v, f_t min, f_t max )
606 {
607  _LIMIT2( v[0], min, max );
608  _LIMIT2( v[1], min, max );
609  _LIMIT2( v[2], min, max );
610 }
611 
612 
613 /* Array contains NAN
614  * return 1 if NAN found in array, 0 if not
615  */
616 static __inline int isNan_array( f_t *a, int size )
617 {
618  int i;
619 
620  for( i=0; i<size; i++ )
621  {
622  if( a[i] != a[i] )
623  return 1;
624  }
625 
626  return 0;
627 }
628 
629 
630 /* Array contains NAN
631  * return 1 if NAN found in double array, 0 if not
632  */
633 static __inline int isNan_array_d( double *a, int size )
634 {
635  int i;
636 
637  for( i=0; i<size; i++ )
638  {
639  if( a[i] != a[i] )
640  return 1;
641  }
642 
643  return 0;
644 }
645 
646 #if defined(PLATFORM_IS_WINDOWS)
647 #pragma warning( push )
648 #pragma warning( disable : 4723)
649 #endif
650 
651 /* Array contains INF
652  * return 1 if INF found in array, 0 if not
653  */
654 static __inline int isInf_array( f_t *a, int size )
655 {
656  int i;
657 
658  f_t tmp = 1.0f;
659  f_t inf = 1.0f / ( tmp - 1.0f);
660 
661  for( i=0; i<size; i++ )
662  {
663  if( a[i] == inf )
664  return 1;
665  }
666 
667  return 0;
668 }
669 
670 
671 /* Array contains INF
672 * return 1 if INF found in double array, 0 if not
673 */
674 static __inline int isInf_array_d(double *a, int size)
675 {
676  int i;
677 
678  double tmp = 1.0f;
679  double inf = 1.0f / (tmp - 1.0f);
680 
681  for (i = 0; i<size; i++)
682  {
683  if (a[i] == inf)
684  return 1;
685  }
686 
687  return 0;
688 }
689 
690 #if defined(PLATFORM_IS_WINDOWS)
691 #pragma warning( pop )
692 #endif
693 
694 /* Array does not contain NAN or INF
695  * return 0 if NAN or INF found in array, 1 if not
696  */
697 static __inline int isFinite_array( f_t *a, int size )
698 {
699  if( isNan_array(a, size) )
700  return 0;
701 
702  if( isInf_array(a, size) )
703  return 0;
704 
705  return 1;
706 }
707 
708 
709 /* Array does not contain NAN or INF
710 * return 0 if NAN or INF found in double array, 1 if not
711 */
712 static __inline int isFinite_array_d(double *a, int size)
713 {
714  if (isNan_array_d(a, size))
715  return 0;
716 
717  if (isInf_array_d(a, size))
718  return 0;
719 
720  return 1;
721 }
722 
723 
724 // Low-Pass Alpha Filter
725 void LPFO0_init_Vec3( sLpfO0 *lpf, f_t dt, f_t cornerFreqHz, const ixVector3 initVal );
726 // output[n+1] = beta*output[n] + alpha*input
727 void LPFO0_Vec3( sLpfO0 *lpf, const ixVector3 input );
728 
729 // Zero order Low-Pass Filter
730 static __inline void O0_LPF_Vec3( ixVector3 result, const ixVector3 input, f_t alph, f_t beta )
731 {
732  ixVector3 tmp3;
733 
734  // val[n+1] = beta*val[n] + alpha*input
735  mul_Vec3_X( tmp3, input, alph );
736  mul_Vec3_X( result, result, beta );
737  add_Vec3_Vec3( result, result, tmp3 );
738 }
739 
740 
741 // First order Low-Pass Filter
742 static __inline void O1_LPF_Vec3( ixVector3 result, const ixVector3 input, ixVector3 c1, f_t alph, f_t beta, f_t dt )
743 {
744  ixVector3 tmp3;
745 
746  // Estimate next models coefficients: d1 = (input - result) / dt
747  sub_Vec3_Vec3( tmp3, input, result );
748  div_Vec3_X( tmp3, tmp3, dt );
749 
750  // LPF these coefficients: c1 = beta*c1 + alph*d1
751  O0_LPF_Vec3( c1, tmp3, alph, beta );
752 
753  // Current state estimates: est = (last result) + c1*dt
754  mul_Vec3_X( tmp3, c1, dt );
755  add_Vec3_Vec3( result, result, tmp3 );
756 
757  // LPF input into state estimates: result = beta*est + alph*input
758  O0_LPF_Vec3( result, input, alph, beta );
759 }
760 
761 
762 
763 #ifdef __cplusplus
764 }
765 #endif
766 
767 #endif /* MATRIX_H_ */
#define _MIN(a, b)
Definition: ISConstants.h:298
double ixVector2d[2]
Definition: ISConstants.h:788
void mul_Vec4_X(ixVector4 result, const ixVector4 v, const f_t x)
Definition: ISMatrix.c:634
static __inline void cpy_Vec3d_Vec3d(ixVector3d result, const ixVector3d v)
Definition: ISMatrix.h:430
#define recipNorm_Vec4d(v)
Definition: ISMatrix.h:50
void mul_Mat4x4_Trans_Vec4x1(ixVector4 result, const ixMatrix4 m, const ixVector4 v)
Definition: ISMatrix.c:452
static __inline void cpy_Vec3d_Vec3(ixVector3d result, const ixVector3 v)
Definition: ISMatrix.h:434
void abs_Vec2(ixVector2 result, const ixVector2 v)
Definition: ISMatrix.c:522
def lpf(data, cornerFreqHz, dt=None, time=None, initVal=None)
Definition: filterTools.py:89
void mul_Mat3x3_Trans_Vec3x1(ixVector3 result, const ixMatrix3 m, const ixVector3 v)
Definition: ISMatrix.c:437
void mul_Vec2d_X(ixVector2d result, const ixVector2d v, const double x)
Definition: ISMatrix.c:614
f_t ixVector3[3]
Definition: ISConstants.h:791
static __inline int isFinite_array_d(double *a, int size)
Definition: ISMatrix.h:712
float f_t
Definition: ISConstants.h:786
void add_Vec3_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:675
ixVector3 v
Definition: ISMatrix.h:68
void abs_Vec4(ixVector4 result, const ixVector4 v)
Definition: ISMatrix.c:548
static __inline int is_equal_Vec2(const ixVector2 v1, const ixVector2 v2)
Definition: ISMatrix.h:562
static __inline void zero_MatMxN(f_t *M, i_t m, i_t n)
Definition: ISMatrix.h:417
void sub_Vec3d_Vec3d(ixVector3d result, const ixVector3d v1, const ixVector3d v2)
Definition: ISMatrix.c:719
void sub_Vec3_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:712
static __inline f_t max_Vec3_X(const ixVector3 v)
Definition: ISMatrix.h:349
f_t dot_Vec2_Vec2(const ixVector2 v1, const ixVector2 v2)
Definition: ISMatrix.c:564
void mul_Mat2x2_Trans_Vec2x1(ixVector2 result, const ixMatrix2 m, const ixVector2 v)
Definition: ISMatrix.c:424
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 div_Vec3_X(ixVector3 result, const ixVector3 v, const f_t x)
Definition: ISMatrix.c:650
void abs_Vec3d(ixVector3d result, const ixVector3d v)
Definition: ISMatrix.c:541
#define recipNorm_Vec2(v)
Definition: ISMatrix.h:41
static __inline void cpy_Vec3_Vec3d(ixVector3 result, const ixVector3d v)
Definition: ISMatrix.h:440
static __inline void limit2_Vec3(ixVector3 v, f_t min, f_t max)
Definition: ISMatrix.h:605
void mul_Mat3x3_Trans_Mat3x3(ixMatrix3 result, const ixMatrix3 m1, const ixMatrix3 m2)
Definition: ISMatrix.c:354
void mul_Mat2x2_Vec2x1(ixVector2 result, const ixMatrix2 m, const ixVector2 v)
Definition: ISMatrix.c:418
f_t ixMatrix4[16]
Definition: ISConstants.h:800
static __inline int isInf_array(f_t *a, int size)
Definition: ISMatrix.h:654
int i_t
Definition: ISConstants.h:787
void add_Vec3d_Vec3d(ixVector3d result, const ixVector3d v1, const ixVector3d v2)
Definition: ISMatrix.c:682
static __inline void O1_LPF_Vec3(ixVector3 result, const ixVector3 input, ixVector3 c1, f_t alph, f_t beta, f_t dt)
Definition: ISMatrix.h:742
#define max(a, b)
Takes the maximal value of a and b.
Definition: compiler.h:823
static __inline int isFinite_array(f_t *a, int size)
Definition: ISMatrix.h:697
void mul_Mat3x3_Mat3x3_Trans_d(ixMatrix3d result, const ixMatrix3d m1, const ixMatrix3d m2)
Definition: ISMatrix.c:402
bool add(const actionlib::TwoIntsGoal &req, actionlib::TwoIntsResult &res)
void mul_Mat4x4_Vec4x1(ixVector4 result, const ixMatrix4 m, const ixVector4 v)
Definition: ISMatrix.c:444
f_t ixVector2[2]
Definition: ISConstants.h:789
void mul_Mat3x3_Mat3x3(ixMatrix3 result, const ixMatrix3 m1, const ixMatrix3 m2)
Definition: ISMatrix.c:322
void eye_MatN(f_t *A, i_t n)
Definition: ISMatrix.c:119
void mul_Vec3_X(ixVector3 result, const ixVector3 v, const f_t x)
Definition: ISMatrix.c:620
f_t beta
Definition: ISMatrix.h:79
double ixVector3d[3]
Definition: ISConstants.h:790
void mul_Vec3d_X(ixVector3d result, const ixVector3d v, const double x)
Definition: ISMatrix.c:627
f_t dot_Vec4_Vec4(const ixVector4 v1, const ixVector4 v2)
Definition: ISMatrix.c:584
static __inline char is_zero(const f_t *f)
Definition: ISMatrix.h:92
f_t ixMatrix2[4]
Definition: ISConstants.h:798
static __inline f_t min_Vec3_X(const ixVector3 v)
Definition: ISMatrix.h:333
char inv_Mat4(ixMatrix4 result, const ixMatrix4 m)
Definition: ISMatrix.c:876
void crossd_Vec3(ixVector3d result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:601
double ixVector4d[4]
Definition: ISConstants.h:792
void add_Vec4_Vec4(ixVector4 result, const ixVector4 v1, const ixVector4 v2)
Definition: ISMatrix.c:696
#define _LIMIT2(x, xmin, xmax)
Definition: ISConstants.h:314
void transpose_Mat2(ixMatrix2 result, const ixMatrix2 m)
Definition: ISMatrix.c:778
static __inline int isNan_array(f_t *a, int size)
Definition: ISMatrix.h:616
static __inline void min_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.h:365
void div_Vec4d_X(ixVector4d result, const ixVector4d v, const double x)
Definition: ISMatrix.c:666
static __inline void cpy_MatMxN(f_t *result, const f_t *M, i_t m, i_t n)
Definition: ISMatrix.h:484
static __inline void normalize_Vec3(ixVector3 result, const ixVector3 v)
Definition: ISMatrix.h:539
void abs_Vec2d(ixVector2d result, const ixVector2d v)
Definition: ISMatrix.c:528
void LPFO0_init_Vec3(sLpfO0 *lpf, f_t dt, f_t cornerFreqHz, const ixVector3 initVal)
Definition: ISMatrix.c:1023
void mul_Mat3x3_Mat3x3_Trans(ixMatrix3 result, const ixMatrix3 m1, const ixMatrix3 m2)
Definition: ISMatrix.c:386
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
ixVector3 v
Definition: ISMatrix.h:76
double ixMatrix3d[9]
Definition: ISConstants.h:802
f_t beta
Definition: ISMatrix.h:70
static __inline void zero_Vec3d(ixVector3d v)
Definition: ISMatrix.h:389
double dot_Vec3d_Vec3d(const ixVector3d v1, const ixVector3d v2)
Definition: ISMatrix.c:577
static __inline int is_equal_Vec3(const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.h:572
static __inline void zero_VecN(f_t *v, i_t n)
Definition: ISMatrix.h:409
static __inline int is_equal_Vec4(const ixVector4 v1, const ixVector4 v2)
Definition: ISMatrix.h:583
void div_Vec3_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:734
static __inline void cpy_Vec4d_Vec4d(ixVector4d result, const ixVector4d v)
Definition: ISMatrix.h:462
static __inline void cpy_Vec4d_Vec4(ixVector4d result, const ixVector4 v)
Definition: ISMatrix.h:466
void div_Vec4_Vec4(ixVector4 result, const ixVector4 v1, const ixVector4 v2)
Definition: ISMatrix.c:741
f_t ixMatrix3[9]
Definition: ISConstants.h:799
void mul_Vec4_Vec4(ixVector4 result, const ixVector4 v1, const ixVector4 v2)
Definition: ISMatrix.c:499
#define _LIMIT(x, lim)
Definition: ISConstants.h:310
void mul_Vec4d_X(ixVector4d result, const ixVector4d v, const double x)
Definition: ISMatrix.c:642
static __inline void zero_Vec4(ixVector4 v)
Definition: ISMatrix.h:397
static __inline void limit_Vec3(ixVector3 v, f_t limit)
Definition: ISMatrix.h:595
static __inline void normalize_Vec2(ixVector2 v)
Definition: ISMatrix.h:530
#define _MAX(a, b)
Definition: ISConstants.h:294
static __inline void O0_LPF_Vec3(ixVector3 result, const ixVector3 input, f_t alph, f_t beta)
Definition: ISMatrix.h:730
char inv_Mat3(ixMatrix3 result, const ixMatrix3 m)
Definition: ISMatrix.c:847
void mul_Mat3x3_Trans_Mat3x3_d(ixMatrix3d result, const ixMatrix3d m1, const ixMatrix3d m2)
Definition: ISMatrix.c:370
void transpose_Mat4(ixMatrix4 result, const ixMatrix4 m)
Definition: ISMatrix.c:804
void sub_Vec4_Vec4(ixVector4 result, const ixVector4 v1, const ixVector4 v2)
Definition: ISMatrix.c:726
void div_Vec4_X(ixVector4 result, const ixVector4 v, const f_t x)
Definition: ISMatrix.c:658
char inv_Mat2(ixMatrix2 result, ixMatrix2 m)
Definition: ISMatrix.c:828
void mul_Vec3_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:492
void abs_Vec3(ixVector3 result, const ixVector3 v)
Definition: ISMatrix.c:534
#define recipNorm_Vec3(v)
Definition: ISMatrix.h:42
static __inline void cpy_Vec4_Vec4(ixVector4 result, const ixVector4 v)
Definition: ISMatrix.h:458
void transpose_Mat3(ixMatrix3 result, const ixMatrix3 m)
Definition: ISMatrix.c:788
void mul_Vec2_X(ixVector2 result, const ixVector2 v, const f_t x)
Definition: ISMatrix.c:608
static __inline void zero_Vec3(ixVector3 v)
Definition: ISMatrix.h:385
static __inline void max_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.h:375
void sqrt_Vec4(ixVector4 result, const ixVector4 v)
Definition: ISMatrix.c:514
static __inline void cpy_Vec4_Vec4d(ixVector4 result, const ixVector4d v)
Definition: ISMatrix.h:473
static __inline void cpy_VecN_VecN(f_t *result, const f_t *v, i_t n)
Definition: ISMatrix.h:450
static __inline int isNan_array_d(double *a, int size)
Definition: ISMatrix.h:633
void add_Vec4d_Vec4d(ixVector4d result, const ixVector4d v1, const ixVector4d v2)
Definition: ISMatrix.c:704
static __inline void zero_Vec4d(ixVector4d v)
Definition: ISMatrix.h:401
void LPFO0_Vec3(sLpfO0 *lpf, const ixVector3 input)
Definition: ISMatrix.c:1036
void neg_Vec3(ixVector3 result, const ixVector3 v)
Definition: ISMatrix.c:749
static __inline void normalize_Vec4d(ixVector4d result, const ixVector4d v)
Definition: ISMatrix.h:553
f_t alpha
Definition: ISMatrix.h:78
f_t alpha
Definition: ISMatrix.h:69
void abs_Vec4d(ixVector4d result, const ixVector4d v)
Definition: ISMatrix.c:556
char inv_MatN(f_t *result, const f_t *M, i_t n)
Definition: ISMatrix.c:258
void cross_Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:594
void mul_Mat3x3_Vec3x1(ixVector3 result, const ixMatrix3 m, const ixVector3 v)
Definition: ISMatrix.c:430
f_t ixVector4[4]
Definition: ISConstants.h:793
static __inline int isInf_array_d(double *a, int size)
Definition: ISMatrix.h:674
void add_K1Vec3_K2Vec3(ixVector3 result, const ixVector3 v1, const ixVector3 v2, float k1, float k2)
Definition: ISMatrix.c:689
static __inline void normalize_Vec4(ixVector4 result, const ixVector4 v)
Definition: ISMatrix.h:548
ixVector3 c1
Definition: ISMatrix.h:77
void mul_Mat3x3_Mat3x3_d(ixMatrix3d result, const ixMatrix3d m1, const ixMatrix3d m2)
Definition: ISMatrix.c:338
void neg_Mat3x3(ixMatrix3 result, const ixMatrix3 m)
Definition: ISMatrix.c:460
f_t dot_Vec3_Vec3(const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:570
void sqrt_Vec3(ixVector3 result, const ixVector3 v)
Definition: ISMatrix.c:507
static __inline void cpy_Vec3_Vec3(ixVector3 result, const ixVector3 v)
Definition: ISMatrix.h:426
void mul_Vec3x1_Vec1x3(ixMatrix3 result, const ixVector3 v1, const ixVector3 v2)
Definition: ISMatrix.c:476
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


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57