sp_matrix.c
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
27 /*
28  * This program is free software; you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation; either version 2 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41  *
42  */
43 
44 #include <stdio.h>
45 #include "sp_matrix.h"
46 
52 /*****************************************************************************/
53  MATRIX create_matrix (int rows, int cols)
54 
55 /*****************************************************************************
56  Creates a MATRIX of dimensions (rows, cols) and initializaes it to zeros.
57 ******************************************************************************/
58 {
59  MATRIX m;
60 
61  MROWS (m) = rows;
62  MCOLS (m) = cols;
63 
64  {
65  int i, j;
66 
67  for (i = 0; i < MROWS (m); i++)
68  for (j = 0; j < MCOLS (m); j++)
69  MDATA (m, i, j) = 0;
70  }
71 
72  return m;
73 }
74 
75 /*****************************************************************************/
76  void initialize_matrix (MATRIX *m, int rows, int cols)
77 
78 /*****************************************************************************
79  Initializes a MATRIX to dimensions (rows, cols) and content zeros.
80 ******************************************************************************/
81 {
82  MROWS (*m) = rows;
83  MCOLS (*m) = cols;
84 
85  {
86  int i, j;
87 
88  for (i = 0; i < MROWS (*m); i++)
89  for (j = 0; j < MCOLS (*m); j++)
90  MDATA (*m, i, j) = 0;
91  }
92 
93 }
94 
95 
96 /*****************************************************************************/
97  void print_matrix (char *message, MATRIX const *m)
98 
99 /*****************************************************************************
100  Print to stdout the contents of MATRIX m.
101 ******************************************************************************/
102 {
103  int i, j;
104 
105  printf ("%s\n",message);
106  printf("%d %d \n",MROWS (*m),MCOLS (*m));
107  if ((MROWS (*m) <= MAX_ROWS) && (MCOLS (*m) <= MAX_COLS))
108  for (i = 0; i < MROWS (*m); i++)
109  {
110  for (j = 0; j < MCOLS (*m); j++)
111  printf ("%10.5f ", MDATA (*m, i, j));
112  printf ("\n");
113  }
114  else printf ("Dimension incorrecta!");
115  printf ("\n");
116 }
117 
118 /*****************************************************************************/
119  VECTOR create_vector (int elements)
120 
121 /*****************************************************************************
122  Initializes a VECTOR to dimension (elements) and its contents to zeros.
123 ******************************************************************************/
124 {
125  VECTOR v;
126 
127  VELEMENTS (v) = elements;
128 
129  {
130  int i;
131 
132  for (i = 0; i < VELEMENTS (v); i++)
133  VDATA (v, i) = 0;
134  }
135 
136  return v;
137 }
138 
139 /*****************************************************************************/
140  void initialize_vector (VECTOR *v, int elements)
141 
142 /*****************************************************************************
143  Initializes a VECTOR to dimension (elements) and its contents to zeros.
144 ******************************************************************************/
145 {
146  VELEMENTS (*v) = elements;
147 
148  {
149  int i;
150 
151  for (i = 0; i < VELEMENTS (*v); i++)
152  VDATA (*v, i) = 0;
153  }
154 }
155 
156 /*****************************************************************************/
157  void print_vector (char *message, VECTOR const *v)
158 
159 /*****************************************************************************
160  Print to stdout the contents of VECTOR m.
161 ******************************************************************************/
162 {
163  int i;
164 
165  printf ("%s\n",message);
166  if (VELEMENTS (*v) <= MAX_ROWS)
167  for (i = 0; i < VELEMENTS (*v); i++)
168  {
169  printf ("%f ", VDATA (*v, i));
170  printf ("\n");
171  }
172  else printf ("Dimension incorrecta!");
173  printf ("\n");
174 }
175 
176 /*****************************************************************************/
177  float cross_product (MATRIX const *m, int f1, int c1, int f2, int c2)
178 
179 /*****************************************************************************
180 ******************************************************************************/
181 {
182  return MDATA (*m, f1, c1) * MDATA (*m, f2, c2) - MDATA (*m, f1, c2) * MDATA (*m, f2, c1);
183 }
184 
185 /*****************************************************************************/
186 int determinant (MATRIX const *m, float *result)
187 /*****************************************************************************
188 ******************************************************************************/
189 {
190  if (!M_SQUARE (*m))
191  {
192  printf ("ERROR (determinant): MATRIX must be square!\n");
193  print_matrix ("MATRIX:", m);
194  return -1;
195  }
196  else
197  {
198 
199  if (MROWS (*m) == 1)
200  *result = MDATA (*m, 0, 0);
201  else if (MROWS (*m) == 2)
202  *result = cross_product (m, 0, 0, 1, 1);
203  else
204  *result = MDATA (*m, 0, 0) * cross_product (m, 1, 1, 2, 2)
205  - MDATA (*m, 0, 1) * cross_product (m, 1, 0, 2, 2)
206  + MDATA (*m, 0, 2) * cross_product (m, 1, 0, 2, 1);
207 
208 
209  return 1;
210  }
211 }
212 
213 /*****************************************************************************/
214  int inverse_matrix (MATRIX const *m, MATRIX *n)
215 
216 /*****************************************************************************
217 ******************************************************************************/
218 {
219  if (!M_SQUARE (*m))
220  {
221  printf ("ERROR (inverse_matrix): MATRIX must be square!\n");
222  print_matrix ("MATRIX:", m);
223  n->cols=0; n->rows=0;
224  return -1;
225  }
226  else
227  {
228  float det;
229  int res;
230 
231  res = determinant (m,&det);
232 
233  if (res == -1)
234  {
235  printf ("ERROR (inverse_matrix): singular MATRIX!\n");
236  print_matrix ("MATRIX:", m);
237  return -1;
238  }
239  else
240  {
241  initialize_matrix (n, MROWS (*m), MCOLS (*m));
242  if (MROWS (*m) == 1)
243  {
244  MDATA (*n, 0, 0) = 1 / det ;
245  }
246  else if (MROWS (*m) == 2)
247  {
248  MDATA (*n, 0, 0) = MDATA (*m, 1, 1) / det ;
249  MDATA (*n, 0, 1) = -MDATA (*m, 0, 1) / det ;
250  MDATA (*n, 1, 0) = -MDATA (*m, 1, 0) / det ;
251  MDATA (*n, 1, 1) = MDATA (*m, 0, 0) / det ;
252  }
253  else
254  {
255  MDATA (*n, 0, 0) = cross_product (m, 1, 1, 2, 2) / det ;
256  MDATA (*n, 0, 1) = -cross_product (m, 0, 1, 2, 2) / det ;
257  MDATA (*n, 0, 2) = cross_product (m, 0, 1, 1, 2) / det ;
258  MDATA (*n, 1, 0) = -cross_product (m, 1, 0, 2, 2) / det ;
259  MDATA (*n, 1, 1) = cross_product (m, 0, 0, 2, 2) / det ;
260  MDATA (*n, 1, 2) = -cross_product (m, 0, 0, 1, 2) / det ;
261  MDATA (*n, 2, 0) = cross_product (m, 1, 0, 2, 1) / det ;
262  MDATA (*n, 2, 1) = -cross_product (m, 0, 0, 2, 1) / det ;
263  MDATA (*n, 2, 2) = cross_product (m, 0, 0, 1, 1) / det ;
264  }
265  }
266  }
267  return 1;
268 }
269 
270 /*****************************************************************************/
271  int multiply_matrix_vector (MATRIX const *m, VECTOR const *v, VECTOR *r)
272 
273 /*****************************************************************************
274  Returns the VECTOR-MATRIX product of m and v in r.
275 ******************************************************************************/
276 {
277  if (! (MV_COMPAT_DIM (*m, *v)))
278  {
279  printf ("ERROR (multiply_matrix_vector): MATRIX and VECTOR dimensions incompatible!\n");
280  print_matrix ("MATRIX:", m);
281  print_vector ("VECTOR:", v);
282  return -1; /*added 1996-07*/
283  }
284  else
285  {
286  int i, j;
287  float datum;
288 
289  VELEMENTS (*r) = MROWS (*m);
290 
291  for (i = 0; i < MROWS (*m); i++)
292  {
293  datum = 0;
294  for (j = 0; j < VELEMENTS (*v); j++)
295  datum = datum + MDATA (*m, i, j) * VDATA (*v, j);
296  VDATA (*r, i) = datum;
297  }
298  }
299  return 1;
300 }
301 
void initialize_matrix(MATRIX *m, int rows, int cols)
Definition: sp_matrix.c:76
MATRIX create_matrix(int rows, int cols)
Definition: sp_matrix.c:53
int cols
Definition: sp_matrix.h:70
#define MDATA(m, i, j)
Definition: sp_matrix.h:88
void print_matrix(char *message, MATRIX const *m)
Definition: sp_matrix.c:97
void print_vector(char *message, VECTOR const *v)
Definition: sp_matrix.c:157
#define MAX_ROWS
Definition: sp_matrix.h:65
#define MAX_COLS
Definition: sp_matrix.h:66
#define VDATA(v, i)
Definition: sp_matrix.h:91
#define MROWS(m)
Definition: sp_matrix.h:86
#define MV_COMPAT_DIM(m, v)
Definition: sp_matrix.h:97
VECTOR create_vector(int elements)
Definition: sp_matrix.c:119
#define M_SQUARE(m)
Definition: sp_matrix.h:93
int inverse_matrix(MATRIX const *m, MATRIX *n)
Definition: sp_matrix.c:214
Matrix class auxiliary functions declaration.
#define VELEMENTS(v)
Definition: sp_matrix.h:90
int multiply_matrix_vector(MATRIX const *m, VECTOR const *v, VECTOR *r)
Definition: sp_matrix.c:271
void initialize_vector(VECTOR *v, int elements)
Definition: sp_matrix.c:140
float cross_product(MATRIX const *m, int f1, int c1, int f2, int c2)
Definition: sp_matrix.c:177
int rows
Definition: sp_matrix.h:69
int determinant(MATRIX const *m, float *result)
Definition: sp_matrix.c:186
#define MCOLS(m)
Definition: sp_matrix.h:87


lidar_egomotion
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:10