Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
pf_vector.c
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
8  * License as published by the Free Software Foundation; either
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 "nav2_amcl/pf/pf_vector.hpp"
34 #include "nav2_amcl/pf/eig3.hpp"
35 
36 
37 // Return a zero vector
38 pf_vector_t pf_vector_zero(void)
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
51 // int pf_vector_finite(pf_vector_t a)
52 // {
53 // int i;
54 
55 // for (i = 0; i < 3; i++) {
56 // if (!isfinite(a.v[i])) {
57 // return 0;
58 // }
59 // }
60 
61 // return 1;
62 // }
63 
64 
65 // Print a vector
66 // void pf_vector_fprintf(pf_vector_t a, FILE * file, const char * fmt)
67 // {
68 // int i;
69 
70 // for (i = 0; i < 3; i++) {
71 // fprintf(file, fmt, a.v[i]);
72 // fprintf(file, " ");
73 // }
74 // fprintf(file, "\n");
75 // }
76 
77 
78 // // Simple vector addition
79 // pf_vector_t pf_vector_add(pf_vector_t a, pf_vector_t b)
80 // {
81 // pf_vector_t c;
82 
83 // c.v[0] = a.v[0] + b.v[0];
84 // c.v[1] = a.v[1] + b.v[1];
85 // c.v[2] = a.v[2] + b.v[2];
86 
87 // return c;
88 // }
89 
90 
91 // Simple vector subtraction
92 pf_vector_t pf_vector_sub(pf_vector_t a, pf_vector_t b)
93 {
94  pf_vector_t c;
95 
96  c.v[0] = a.v[0] - b.v[0];
97  c.v[1] = a.v[1] - b.v[1];
98  c.v[2] = a.v[2] - b.v[2];
99 
100  return c;
101 }
102 
103 
104 // Transform from local to global coords (a + b)
105 pf_vector_t pf_vector_coord_add(pf_vector_t a, pf_vector_t b)
106 {
107  pf_vector_t c;
108 
109  c.v[0] = b.v[0] + a.v[0] * cos(b.v[2]) - a.v[1] * sin(b.v[2]);
110  c.v[1] = b.v[1] + a.v[0] * sin(b.v[2]) + a.v[1] * cos(b.v[2]);
111  c.v[2] = b.v[2] + a.v[2];
112  c.v[2] = atan2(sin(c.v[2]), cos(c.v[2]));
113 
114  return c;
115 }
116 
117 
118 // // Transform from global to local coords (a - b)
119 // pf_vector_t pf_vector_coord_sub(pf_vector_t a, pf_vector_t b)
120 // {
121 // pf_vector_t c;
122 
123 // c.v[0] = +(a.v[0] - b.v[0]) * cos(b.v[2]) + (a.v[1] - b.v[1]) * sin(b.v[2]);
124 // c.v[1] = -(a.v[0] - b.v[0]) * sin(b.v[2]) + (a.v[1] - b.v[1]) * cos(b.v[2]);
125 // c.v[2] = a.v[2] - b.v[2];
126 // c.v[2] = atan2(sin(c.v[2]), cos(c.v[2]));
127 
128 // return c;
129 // }
130 
131 
132 // Return a zero matrix
133 pf_matrix_t pf_matrix_zero(void)
134 {
135  int i, j;
136  pf_matrix_t c;
137 
138  for (i = 0; i < 3; i++) {
139  for (j = 0; j < 3; j++) {
140  c.m[i][j] = 0.0;
141  }
142  }
143 
144  return c;
145 }
146 
147 
148 // // Check for NAN or INF in any component
149 // int pf_matrix_finite(pf_matrix_t a)
150 // {
151 // int i, j;
152 
153 // for (i = 0; i < 3; i++) {
154 // for (j = 0; j < 3; j++) {
155 // if (!isfinite(a.m[i][j])) {
156 // return 0;
157 // }
158 // }
159 // }
160 
161 // return 1;
162 // }
163 
164 
165 // Print a matrix
166 // void pf_matrix_fprintf(pf_matrix_t a, FILE * file, const char * fmt)
167 // {
168 // int i, j;
169 
170 // for (i = 0; i < 3; i++) {
171 // for (j = 0; j < 3; j++) {
172 // fprintf(file, fmt, a.m[i][j]);
173 // fprintf(file, " ");
174 // }
175 // fprintf(file, "\n");
176 // }
177 // }
178 
179 
180 /*
181 // Compute the matrix inverse
182 pf_matrix_t pf_matrix_inverse(pf_matrix_t a, double *det)
183 {
184  double lndet;
185  int signum;
186  gsl_permutation *p;
187  gsl_matrix_view A, Ai;
188 
189  pf_matrix_t ai;
190 
191  A = gsl_matrix_view_array((double*) a.m, 3, 3);
192  Ai = gsl_matrix_view_array((double*) ai.m, 3, 3);
193 
194  // Do LU decomposition
195  p = gsl_permutation_alloc(3);
196  gsl_linalg_LU_decomp(&A.matrix, p, &signum);
197 
198  // Check for underflow
199  lndet = gsl_linalg_LU_lndet(&A.matrix);
200  if (lndet < -1000)
201  {
202  //printf("underflow in matrix inverse lndet = %f", lndet);
203  gsl_matrix_set_zero(&Ai.matrix);
204  }
205  else
206  {
207  // Compute inverse
208  gsl_linalg_LU_invert(&A.matrix, p, &Ai.matrix);
209  }
210 
211  gsl_permutation_free(p);
212 
213  if (det)
214  *det = exp(lndet);
215 
216  return ai;
217 }
218 */
219 
220 
221 // Decompose a covariance matrix [a] into a rotation matrix [r] and a diagonal
222 // matrix [d] such that a = r d r^T.
223 void pf_matrix_unitary(pf_matrix_t * r, pf_matrix_t * d, pf_matrix_t a)
224 {
225  int i, j;
226  /*
227  gsl_matrix *aa;
228  gsl_vector *eval;
229  gsl_matrix *evec;
230  gsl_eigen_symmv_workspace *w;
231 
232  aa = gsl_matrix_alloc(3, 3);
233  eval = gsl_vector_alloc(3);
234  evec = gsl_matrix_alloc(3, 3);
235  */
236 
237  double aa[3][3];
238  double eval[3];
239  double evec[3][3];
240 
241  for (i = 0; i < 3; i++) {
242  for (j = 0; j < 3; j++) {
243  // gsl_matrix_set(aa, i, j, a.m[i][j]);
244  aa[i][j] = a.m[i][j];
245  }
246  }
247 
248  // Compute eigenvectors/values
249  /*
250  w = gsl_eigen_symmv_alloc(3);
251  gsl_eigen_symmv(aa, eval, evec, w);
252  gsl_eigen_symmv_free(w);
253  */
254 
255  eigen_decomposition(aa, evec, eval);
256 
257  *d = pf_matrix_zero();
258  for (i = 0; i < 3; i++) {
259  // d->m[i][i] = gsl_vector_get(eval, i);
260  d->m[i][i] = eval[i];
261  for (j = 0; j < 3; j++) {
262  // r->m[i][j] = gsl_matrix_get(evec, i, j);
263  r->m[i][j] = evec[i][j];
264  }
265  }
266 
267  // gsl_matrix_free(evec);
268  // gsl_vector_free(eval);
269  // gsl_matrix_free(aa);
270 }