27 #ifndef PPRZ_SIMPLE_MATRIX_H
28 #define PPRZ_SIMPLE_MATRIX_H
38 #define warn_message printf
40 #define warn_message(...) do { } while(0)
46 #define MAT_MUL(_i, _k, _j, C, A, B) { \
48 for (l=0; l<_i; l++) \
49 for (c=0; c<_j; c++) { \
51 for (m=0; m<_k; m++) \
52 C[l][c] += A[l][m]*B[m][c]; \
59 #define MAT_MUL_T(_i, _k, _j, C, A, B) { \
61 for (l=0; l<_i; l++) \
62 for (c=0; c<_j; c++) { \
64 for (m=0; m<_k; m++) \
65 C[l][c] += A[l][m]*B[c][m]; \
73 #define MAT_SUB(_i, _j, C, A, B) { \
75 for (l=0; l<_i; l++) \
76 for (c=0; c<_j; c++) \
77 C[l][c] = A[l][c] - B[l][c]; \
86 #define MAT_MUL_VECT(_n, o, a, b) { \
88 for (l = 0; l < _n; l++) { \
90 for (m = 0; m < _n; m++) { \
91 o[l] += a[l][m] * b[m]; \
99 #define MAT_INV33(_invS, _S) { \
100 const float m00 = _S[1][1]*_S[2][2] - _S[1][2]*_S[2][1]; \
101 const float m10 = _S[0][1]*_S[2][2] - _S[0][2]*_S[2][1]; \
102 const float m20 = _S[0][1]*_S[1][2] - _S[0][2]*_S[1][1]; \
103 const float m01 = _S[1][0]*_S[2][2] - _S[1][2]*_S[2][0]; \
104 const float m11 = _S[0][0]*_S[2][2] - _S[0][2]*_S[2][0]; \
105 const float m21 = _S[0][0]*_S[1][2] - _S[0][2]*_S[1][0]; \
106 const float m02 = _S[1][0]*_S[2][1] - _S[1][1]*_S[2][0]; \
107 const float m12 = _S[0][0]*_S[2][1] - _S[0][1]*_S[2][0]; \
108 const float m22 = _S[0][0]*_S[1][1] - _S[0][1]*_S[1][0]; \
109 float det = _S[0][0]*m00 - _S[1][0]*m10 + _S[2][0]*m20; \
110 if (fabs(det) < FLT_EPSILON) { \
112 warn_message("warning: %s:%d MAT_INV33 trying to invert non-invertable matrix '%s' and put result in '%s'.\n", __FILE__, __LINE__, #_S, #_invS); \
113 det = copysignf(FLT_EPSILON, det); \
115 _invS[0][0] = m00 / det; \
116 _invS[1][0] = -m01 / det; \
117 _invS[2][0] = m02 / det; \
118 _invS[0][1] = -m10 / det; \
119 _invS[1][1] = m11 / det; \
120 _invS[2][1] = -m12 / det; \
121 _invS[0][2] = m20 / det; \
122 _invS[1][2] = -m21 / det; \
123 _invS[2][2] = m22 / det; \
129 #define MAT_SUM(_i, _j, C, A, B) { \
131 for (l=0; l<_i; l++) \
132 for (c=0; c<_j; c++) \
133 C[l][c] = A[l][c] + B[l][c]; \
140 #define MAT_SUM_c(_i, _j, C, A, B,k_) { \
142 for (l=0; l<_i; l++) \
143 for (c=0; c<_j; c++) \
144 C[l][c] = A[l][c] + B[l][c]*k_; \
150 #define MAT_MUL_c(_i, _k, _j, C, A, B,c_) { \
152 for (l=0; l<_i; l++) \
153 for (c=0; c<_j; c++) { \
155 for (m=0; m<_k; m++) \
156 C[l][c] += c_*A[l][m]*B[m][c]; \
161 #define MAT_PRINT(_i, _j,A) { \
166 for (l=0; l<_i; l++){ \
168 for (c=0; c<_j; c++){ \
169 if(c<(_j-1)){printf("%f,",A[l][c]);} \
170 else{printf("%f",A[l][c]);} \
174 else{printf("}\n");} \