pf_vector.c
Go to the documentation of this file.
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000 Brian Gerkey & Kasper Stoy
4  * gerkey@usc.edu kaspers@robotics.usc.edu
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 /**************************************************************************
22  * Desc: Vector functions
23  * Author: Andrew Howard
24  * Date: 10 Dec 2002
25  * CVS: \$Id: pf_vector.c 6345 2008-04-17 01:36:39Z gerkey \$
26  *************************************************************************/
27
28 #include <math.h>
29 //#include <gsl/gsl_matrix.h>
30 //#include <gsl/gsl_eigen.h>
31 //#include <gsl/gsl_linalg.h>
32
33 #include "amcl/pf/pf_vector.h"
34 #include "amcl/pf/eig3.h"
35
36
37 // Return a zero vector
39 {
40  pf_vector_t c;
41
42  c.v[0] = 0.0;
43  c.v[1] = 0.0;
44  c.v[2] = 0.0;
45
46  return c;
47 }
48
49
50 // Check for NAN or INF in any component
52 {
53  int i;
54
55  for (i = 0; i < 3; i++)
56  if (!finite(a.v[i]))
57  return 0;
58
59  return 1;
60 }
61
62
63 // Print a vector
64 void pf_vector_fprintf(pf_vector_t a, FILE *file, const char *fmt)
65 {
66  int i;
67
68  for (i = 0; i < 3; i++)
69  {
70  fprintf(file, fmt, a.v[i]);
71  fprintf(file, " ");
72  }
73  fprintf(file, "\n");
74
75  return;
76 }
77
78
81 {
82  pf_vector_t c;
83
84  c.v[0] = a.v[0] + b.v[0];
85  c.v[1] = a.v[1] + b.v[1];
86  c.v[2] = a.v[2] + b.v[2];
87
88  return c;
89 }
90
91
92 // Simple vector subtraction
94 {
95  pf_vector_t c;
96
97  c.v[0] = a.v[0] - b.v[0];
98  c.v[1] = a.v[1] - b.v[1];
99  c.v[2] = a.v[2] - b.v[2];
100
101  return c;
102 }
103
104
105 // Transform from local to global coords (a + b)
107 {
108  pf_vector_t c;
109
110  c.v[0] = b.v[0] + a.v[0] * cos(b.v[2]) - a.v[1] * sin(b.v[2]);
111  c.v[1] = b.v[1] + a.v[0] * sin(b.v[2]) + a.v[1] * cos(b.v[2]);
112  c.v[2] = b.v[2] + a.v[2];
113  c.v[2] = atan2(sin(c.v[2]), cos(c.v[2]));
114
115  return c;
116 }
117
118
119 // Transform from global to local coords (a - b)
121 {
122  pf_vector_t c;
123
124  c.v[0] = +(a.v[0] - b.v[0]) * cos(b.v[2]) + (a.v[1] - b.v[1]) * sin(b.v[2]);
125  c.v[1] = -(a.v[0] - b.v[0]) * sin(b.v[2]) + (a.v[1] - b.v[1]) * cos(b.v[2]);
126  c.v[2] = a.v[2] - b.v[2];
127  c.v[2] = atan2(sin(c.v[2]), cos(c.v[2]));
128
129  return c;
130 }
131
132
133 // Return a zero matrix
135 {
136  int i, j;
137  pf_matrix_t c;
138
139  for (i = 0; i < 3; i++)
140  for (j = 0; j < 3; j++)
141  c.m[i][j] = 0.0;
142
143  return c;
144 }
145
146
147 // Check for NAN or INF in any component
149 {
150  int i, j;
151
152  for (i = 0; i < 3; i++)
153  for (j = 0; j < 3; j++)
154  if (!finite(a.m[i][j]))
155  return 0;
156
157  return 1;
158 }
159
160
161 // Print a matrix
162 void pf_matrix_fprintf(pf_matrix_t a, FILE *file, const char *fmt)
163 {
164  int i, j;
165
166  for (i = 0; i < 3; i++)
167  {
168  for (j = 0; j < 3; j++)
169  {
170  fprintf(file, fmt, a.m[i][j]);
171  fprintf(file, " ");
172  }
173  fprintf(file, "\n");
174  }
175  return;
176 }
177
178
179 /*
180 // Compute the matrix inverse
181 pf_matrix_t pf_matrix_inverse(pf_matrix_t a, double *det)
182 {
183  double lndet;
184  int signum;
185  gsl_permutation *p;
186  gsl_matrix_view A, Ai;
187
188  pf_matrix_t ai;
189
190  A = gsl_matrix_view_array((double*) a.m, 3, 3);
191  Ai = gsl_matrix_view_array((double*) ai.m, 3, 3);
192
193  // Do LU decomposition
194  p = gsl_permutation_alloc(3);
195  gsl_linalg_LU_decomp(&A.matrix, p, &signum);
196
197  // Check for underflow
198  lndet = gsl_linalg_LU_lndet(&A.matrix);
199  if (lndet < -1000)
200  {
201  //printf("underflow in matrix inverse lndet = %f", lndet);
202  gsl_matrix_set_zero(&Ai.matrix);
203  }
204  else
205  {
206  // Compute inverse
207  gsl_linalg_LU_invert(&A.matrix, p, &Ai.matrix);
208  }
209
210  gsl_permutation_free(p);
211
212  if (det)
213  *det = exp(lndet);
214
215  return ai;
216 }
217 */
218
219
220 // Decompose a covariance matrix [a] into a rotation matrix [r] and a diagonal
221 // matrix [d] such that a = r d r^T.
223 {
224  int i, j;
225  /*
226  gsl_matrix *aa;
227  gsl_vector *eval;
228  gsl_matrix *evec;
229  gsl_eigen_symmv_workspace *w;
230
231  aa = gsl_matrix_alloc(3, 3);
232  eval = gsl_vector_alloc(3);
233  evec = gsl_matrix_alloc(3, 3);
234  */
235
236  double aa[3][3];
237  double eval[3];
238  double evec[3][3];
239
240  for (i = 0; i < 3; i++)
241  {
242  for (j = 0; j < 3; j++)
243  {
244  //gsl_matrix_set(aa, i, j, a.m[i][j]);
245  aa[i][j] = a.m[i][j];
246  }
247  }
248
249  // Compute eigenvectors/values
250  /*
251  w = gsl_eigen_symmv_alloc(3);
252  gsl_eigen_symmv(aa, eval, evec, w);
253  gsl_eigen_symmv_free(w);
254  */
255
256  eigen_decomposition(aa,evec,eval);
257
258  *d = pf_matrix_zero();
259  for (i = 0; i < 3; i++)
260  {
261  //d->m[i][i] = gsl_vector_get(eval, i);
262  d->m[i][i] = eval[i];
263  for (j = 0; j < 3; j++)
264  {
265  //r->m[i][j] = gsl_matrix_get(evec, i, j);
266  r->m[i][j] = evec[i][j];
267  }
268  }
269
270  //gsl_matrix_free(evec);
271  //gsl_vector_free(eval);
272  //gsl_matrix_free(aa);
273
274  return;
275 }
276
void pf_matrix_fprintf(pf_matrix_t a, FILE *file, const char *fmt)
Definition: pf_vector.c:162
double v[3]
Definition: pf_vector.h:40
pf_vector_t pf_vector_sub(pf_vector_t a, pf_vector_t b)
Definition: pf_vector.c:93
int pf_matrix_finite(pf_matrix_t a)
Definition: pf_vector.c:148
int pf_vector_finite(pf_vector_t a)
Definition: pf_vector.c:51
void eigen_decomposition(double A[3][3], double V[3][3], double d[3])
pf_vector_t pf_vector_zero()
Definition: pf_vector.c:38
double m[3][3]
Definition: pf_vector.h:47
void pf_matrix_unitary(pf_matrix_t *r, pf_matrix_t *d, pf_matrix_t a)
Definition: pf_vector.c:222
pf_matrix_t pf_matrix_zero()
Definition: pf_vector.c:134
void pf_vector_fprintf(pf_vector_t a, FILE *file, const char *fmt)
Definition: pf_vector.c:64
pf_vector_t pf_vector_coord_sub(pf_vector_t a, pf_vector_t b)
Definition: pf_vector.c:120