Jetson Inference
DNN Vision Library
mat33.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __MATRIX_33_H_
24 #define __MATRIX_33_H_
25 
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <math.h>
29 
30 
35 template<typename T1, typename T2>
36 inline void mat33_cast( T1 dst[3][3], const T2 src[3][3] )
37 {
38  for( uint32_t i=0; i < 3; i++ )
39  for( uint32_t j=0; j < 3; j++ )
40  dst[i][j] = (T1)src[i][j];
41 }
42 
43 
48 template<typename T>
49 inline void mat33_copy( T dst[3][3], const T src[3][3] )
50 {
51  memcpy(dst, src, sizeof(T) * 9);
52 }
53 
54 
59 template <typename T>
60 inline T mat33_det( const T src[3][3] )
61 {
62  return src[0][0] * (src[1][1] * src[2][2] - src[1][2] * src[2][1])
63  - src[0][1] * (src[1][0] * src[2][2] - src[1][2] * src[2][0])
64  + src[0][2] * (src[1][0] * src[2][1] - src[1][1] * src[2][0]);
65 }
66 
67 
72 template<typename T>
73 inline void mat33_identity( T dst[3][3] )
74 {
75  dst[0][0] = 1; dst[0][1] = 0; dst[0][2] = 0;
76  dst[1][0] = 0; dst[1][1] = 1; dst[1][2] = 0;
77  dst[2][0] = 0; dst[2][1] = 0; dst[2][2] = 1;
78 }
79 
80 
86 template<typename T>
87 inline void mat33_inverse( T dst[3][3], const T src[3][3] )
88 {
89  T inv[3][3];
90 
91  // invert
92  const T det = mat33_det(src);
93 
94  inv[0][0] = + (src[1][1] * src[2][2] - src[1][2] * src[2][1]);
95  inv[0][1] = - (src[0][1] * src[2][2] - src[0][2] * src[2][1]);
96  inv[0][2] = + (src[0][1] * src[1][2] - src[0][2] * src[1][1]);
97  inv[1][0] = - (src[1][0] * src[2][2] - src[1][2] * src[2][0]);
98  inv[1][1] = + (src[0][0] * src[2][2] - src[0][2] * src[2][0]);
99  inv[1][2] = - (src[0][0] * src[1][2] - src[0][2] * src[1][0]);
100  inv[2][0] = + (src[1][0] * src[2][1] - src[1][1] * src[2][0]);
101  inv[2][1] = - (src[0][0] * src[2][1] - src[0][1] * src[2][0]);
102  inv[2][2] = + (src[0][0] * src[1][1] - src[0][1] * src[1][0]);
103 
104  // scale by determinant
105  for( uint32_t i=0; i < 3; i++ )
106  for( uint32_t k=0; k < 3; k++ )
107  dst[i][k] = inv[i][k] / det;
108 }
109 
110 
115 template<typename T>
116 inline void mat33_multiply( T dst[3][3], const T a[3][3], const T b[3][3] )
117 {
118  for( int i=0; i < 3; i++ )
119  {
120  for( int j=0; j < 3; j++ )
121  {
122  dst[i][j] = 0;
123 
124  for( int k=0; k < 3; k++ )
125  dst[i][j] = dst[i][j] + a[i][k] * b[k][j];
126  }
127  }
128 }
129 
130 
135 template<typename T>
136 inline void mat33_print( const T src[3][3], const char* name=NULL )
137 {
138  if( name != NULL )
139  printf("%s = \n", name);
140 
141  printf(" [ ");
142 
143  for( uint32_t i=0; i < 3; i++ )
144  {
145  for( uint32_t j=0; j < 3; j++ )
146  printf("%f ", src[i][j]);
147 
148  if( i < 2 )
149  printf("\n ");
150  else
151  printf("]\n");
152  }
153 }
154 
155 
160 template<typename T>
161 inline int mat33_rank( const T src[3][3] )
162 {
163  T mat[3][3];
164 
165  // reducing to row-echelon form alters the matrix
166  mat33_copy(mat, src);
167 
168  // iteratively compute the rank
169  int rank = 3; // (= #columns)
170 
171  for( int row=0; row < rank; row++ )
172  {
173  if( mat[row][row] != 0 )
174  {
175  for( int col=0; col < 3; col++ )
176  {
177  if (col != row)
178  {
179  const T mult = mat[col][row] / mat[row][row];
180 
181  for( int i = 0; i < rank; i++ )
182  mat[col][i] -= mult * mat[row][i];
183  }
184  }
185  }
186  else
187  {
188  bool reduce = true;
189 
190  for( int i=row+1; i < 3; i++ )
191  {
192  if( mat[i][row] != 0 )
193  {
194  for( int k=0; k < rank; k++ )
195  {
196  const T tmp = mat[row][k];
197  mat[row][k] = mat[i][k];
198  mat[i][k] = tmp;
199  }
200 
201  reduce = false;
202  break ;
203  }
204  }
205 
206  if( reduce )
207  {
208  rank--;
209 
210  for( int i=0; i < 3; i++ )
211  mat[i][row] = mat[i][rank];
212  }
213 
214  row--;
215  }
216  }
217 
218  return rank;
219 }
220 
221 
226 template<typename T>
227 inline void mat33_rotation( T dst[3][3], T degrees )
228 {
229  mat33_identity(dst);
230 
231  const T rad = 0.01745329251 * degrees;
232 
233  const T c = cos(rad);
234  const T s = sin(rad);
235 
236  dst[0][0] = c;
237  dst[0][1] = -s;
238  dst[1][0] = s;
239  dst[1][1] = c;
240 }
241 
242 
247 template<typename T>
248 inline void mat33_rotation( T dst[3][3], T src[3][3], T degrees )
249 {
250  T m[3][3];
251 
252  mat33_rotation(m, degrees);
253  mat33_multiply(dst, src, m);
254 }
255 
256 
261 template<typename T>
262 inline void mat33_scale( T dst[3][3], T sx, T sy )
263 {
264  mat33_identity(dst);
265 
266  dst[0][0] = sx;
267  dst[1][1] = sy;
268 }
269 
270 
275 template<typename T>
276 inline void mat33_scale( T dst[3][3], T src[3][3], T sx, T sy )
277 {
278  T m[3][3];
279 
280  mat33_scale(m, sx, sy);
281  mat33_multiply(dst, src, m);
282 }
283 
284 
289 template<typename T>
290 inline void mat33_shear( T dst[3][3], T sx, T sy )
291 {
292  mat33_identity(dst);
293 
294  dst[0][1] = sx;
295  dst[1][0] = sy;
296 }
297 
298 
303 template<typename T>
304 inline void mat33_shear( T dst[3][3], T src[3][3], T sx, T sy )
305 {
306  T m[3][3];
307 
308  mat33_shear(m, sx, sy);
309  mat33_multiply(dst, src, m);
310 }
311 
312 
317 template<typename T>
318 inline void mat33_swap( T a[3][3], T b[3][3] )
319 {
320  T c[3][3];
321 
322  mat33_copy(c, a);
323  mat33_copy(a, b);
324  mat33_copy(b, c);
325 }
326 
327 
332 template<typename T>
333 inline T mat33_trace( const T src[3][3] )
334 {
335  return src[0][0] + src[1][1] + src[2][2];
336 }
337 
338 
343 template<typename T>
344 inline void mat33_translate( T dst[3][3], T x, T y )
345 {
346  mat33_identity(dst);
347 
348  dst[0][2] = x;
349  dst[1][2] = y;
350 }
351 
352 
357 template<typename T>
358 inline void mat33_translate( T dst[3][3], T src[3][3], T x, T y )
359 {
360  T m[3][3];
361 
362  mat33_translate(m, x, y);
363  mat33_multiply(dst, src, m);
364 }
365 
366 
371 template<typename T>
372 inline void mat33_transform( T& x_out, T& y_out, const T x_in, const T y_in, const T mat[3][3] )
373 {
374  const T x = mat[0][0] * x_in + mat[0][1] * y_in + mat[0][2];
375  const T y = mat[1][0] * x_in + mat[1][1] * y_in + mat[1][2];
376  const T z = mat[2][0] * x_in + mat[2][1] * y_in + mat[2][2];
377 
378  x_out = x; // / z;
379  y_out = y; // / z;
380 }
381 
382 
387 template<typename T>
388 inline void mat33_transform( T dst[2], const T src[2], const T mat[3][3] )
389 {
390  mat33_transform(dst[0], dst[1], src[0], src[1], mat);
391 }
392 
393 
398 template<typename T>
399 inline void mat33_transform( T* dst, const T* src, const int N, const T mat[3][3] )
400 {
401  for( uint32_t n=0; n < N; n++ )
402  mat33_transform(dst[n*2], dst[n*2+1], src[n*2], src[n*2+1], mat);
403 }
404 
409 template<typename T>
410 inline void mat33_transpose( T dst[3][3], const T src[3][3] )
411 {
412  for( uint32_t i=0; i < 3; i++ )
413  for( uint32_t j=0; j < 3; j++ )
414  dst[i][j] = src[j][i];
415 }
416 
417 
422 template<typename T>
423 inline void mat33_zero( T dst[3][3] )
424 {
425  memset(dst, 0, sizeof(T) * 9);
426 }
427 
428 #endif
429 
void mat33_cast(T1 dst[3][3], const T2 src[3][3])
Cast a 3x3 matrix from one type to another.
Definition: mat33.h:36
void mat33_transpose(T dst[3][3], const T src[3][3])
Transpose a 3x3 matrix, dst=src^T
Definition: mat33.h:410
void mat33_transform(T &x_out, T &y_out, const T x_in, const T y_in, const T mat[3][3])
Transform a 2D vector by a 3x3 matrix.
Definition: mat33.h:372
void mat33_shear(T dst[3][3], T sx, T sy)
Initialize a 3x3 shear matrix.
Definition: mat33.h:290
void mat33_zero(T dst[3][3])
Set a 3x3 matrix to all zero&#39;s.
Definition: mat33.h:423
void mat33_print(const T src[3][3], const char *name=NULL)
Print out a 3x3 matrix to stdout.
Definition: mat33.h:136
void mat33_swap(T a[3][3], T b[3][3])
Swap two 3x3 matrices inline, a=b and b=a
Definition: mat33.h:318
void mat33_scale(T dst[3][3], T sx, T sy)
Initialize a 3x3 scaling matrix.
Definition: mat33.h:262
T mat33_trace(const T src[3][3])
Compute the trace of a 3x3 matrix, returns tr(src)
Definition: mat33.h:333
void mat33_rotation(T dst[3][3], T degrees)
Initialize a 3x3 rotation matrix.
Definition: mat33.h:227
void mat33_identity(T dst[3][3])
Initialize a 3x3 identity matrix.
Definition: mat33.h:73
void mat33_multiply(T dst[3][3], const T a[3][3], const T b[3][3])
Multiply two 3x3 matrices, dst=a*b
Definition: mat33.h:116
void mat33_copy(T dst[3][3], const T src[3][3])
Copy src input matrix to dst output.
Definition: mat33.h:49
int mat33_rank(const T src[3][3])
Determine the rank of a 3x3 matrix.
Definition: mat33.h:161
T mat33_det(const T src[3][3])
Compute the determinant of a 3x3 matrix, returns |src|
Definition: mat33.h:60
void mat33_translate(T dst[3][3], T x, T y)
Initialize a 3x3 translation matrix.
Definition: mat33.h:344
void mat33_inverse(T dst[3][3], const T src[3][3])
Compute the inverse of a 3x3 matrix, dst=src^-1 It is safe to have dst and src be the same memory...
Definition: mat33.h:87