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


gmcl
Author(s): Mhd Ali Alshikh Khalil, adler1994@gmail.com
autogenerated on Wed Mar 2 2022 00:20:14