pf_pdf.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: Useful pdf functions
31  * Author: Andrew Howard
32  * Date: 10 Dec 2002
33  * CVS: $Id: pf_pdf.c 6348 2008-04-17 02:53:17Z gerkey $
34  *************************************************************************/
35 
36 #include <assert.h>
37 #include <math.h>
38 #include <stdlib.h>
39 #include <string.h>
40 //#include <gsl/gsl_rng.h>
41 //#include <gsl/gsl_randist.h>
42 
43 #include "gmcl/pf/pf_pdf.h"
44 #include "portable_utils.hpp"
45 
46 // Random number generator seed value
47 static unsigned int pf_pdf_seed;
48 
49 
50 /**************************************************************************
51  * Gaussian
52  *************************************************************************/
53 
54 // Create a gaussian pdf
56 {
57  pf_matrix_t cd;
58  pf_pdf_gaussian_t *pdf;
59 
60  pdf = calloc(1, sizeof(pf_pdf_gaussian_t));
61 
62  pdf->x = x;
63  pdf->cx = cx;
64  //pdf->cxi = pf_matrix_inverse(cx, &pdf->cxdet);
65 
66  // Decompose the convariance matrix into a rotation
67  // matrix and a diagonal matrix.
68  pf_matrix_unitary(&pdf->cr, &cd, pdf->cx);
69  pdf->cd.v[0] = sqrt(cd.m[0][0]);
70  pdf->cd.v[1] = sqrt(cd.m[1][1]);
71  pdf->cd.v[2] = sqrt(cd.m[2][2]);
72 
73  // Initialize the random number generator
74  //pdf->rng = gsl_rng_alloc(gsl_rng_taus);
75  //gsl_rng_set(pdf->rng, ++pf_pdf_seed);
77 
78  return pdf;
79 }
80 
81 
82 // Destroy the pdf
84 {
85  //gsl_rng_free(pdf->rng);
86  free(pdf);
87  return;
88 }
89 
90 
91 /*
92 // Compute the value of the pdf at some point [x].
93 double pf_pdf_gaussian_value(pf_pdf_gaussian_t *pdf, pf_vector_t x)
94 {
95  int i, j;
96  pf_vector_t z;
97  double zz, p;
98 
99  z = pf_vector_sub(x, pdf->x);
100 
101  zz = 0;
102  for (i = 0; i < 3; i++)
103  for (j = 0; j < 3; j++)
104  zz += z.v[i] * pdf->cxi.m[i][j] * z.v[j];
105 
106  p = 1 / (2 * M_PI * pdf->cxdet) * exp(-zz / 2);
107 
108  return p;
109 }
110 */
111 
112 
113 // Generate a sample from the pdf.
115 {
116  int i, j;
117  pf_vector_t r;
118  pf_vector_t x;
119 
120  // Generate a random vector
121  for (i = 0; i < 3; i++)
122  {
123  //r.v[i] = gsl_ran_gaussian(pdf->rng, pdf->cd.v[i]);
124  r.v[i] = pf_ran_gaussian(pdf->cd.v[i]);
125  }
126 
127  for (i = 0; i < 3; i++)
128  {
129  x.v[i] = pdf->x.v[i];
130  for (j = 0; j < 3; j++)
131  x.v[i] += pdf->cr.m[i][j] * r.v[j];
132  }
133 
134  return x;
135 }
136 
137 // Draw randomly from a zero-mean Gaussian distribution, with standard
138 // deviation sigma.
139 // We use the polar form of the Box-Muller transformation, explained here:
140 // http://www.taygeta.com/random/gaussian.html
141 double pf_ran_gaussian(double sigma)
142 {
143  double x1, x2, w, r;
144 
145  do
146  {
147  do { r = drand48(); } while (r==0.0);
148  x1 = 2.0 * r - 1.0;
149  do { r = drand48(); } while (r==0.0);
150  x2 = 2.0 * r - 1.0;
151  w = x1*x1 + x2*x2;
152  } while(w > 1.0 || w==0.0);
153 
154  return(sigma * x2 * sqrt(-2.0*log(w)/w));
155 }
pf_pdf_gaussian_free
void pf_pdf_gaussian_free(pf_pdf_gaussian_t *pdf)
Definition: pf_pdf.c:83
pf_ran_gaussian
double pf_ran_gaussian(double sigma)
Definition: pf_pdf.c:141
pf_vector_t
Definition: pf_vector.h:46
pf_pdf_gaussian_sample
pf_vector_t pf_pdf_gaussian_sample(pf_pdf_gaussian_t *pdf)
Definition: pf_pdf.c:114
pf_pdf_gaussian_t::cx
pf_matrix_t cx
Definition: pf_pdf.h:64
pf_pdf_gaussian_t::x
pf_vector_t x
Definition: pf_pdf.h:63
pf_pdf_gaussian_alloc
pf_pdf_gaussian_t * pf_pdf_gaussian_alloc(pf_vector_t x, pf_matrix_t cx)
Definition: pf_pdf.c:55
drand48
static double drand48(void)
Definition: portable_utils.hpp:13
pf_pdf_gaussian_t
Definition: pf_pdf.h:53
srand48
static void srand48(long int seedval)
Definition: portable_utils.hpp:18
pf_pdf_seed
static unsigned int pf_pdf_seed
Definition: pf_pdf.c:47
pf_matrix_t::m
double m[3][3]
Definition: pf_vector.h:55
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_pdf.h
portable_utils.hpp
pf_matrix_t
Definition: pf_vector.h:53
pf_pdf_gaussian_t::cr
pf_matrix_t cr
Definition: pf_pdf.h:69
assert.h
pf_vector_t::v
double v[3]
Definition: pf_vector.h:53
pf_pdf_gaussian_t::cd
pf_vector_t cd
Definition: pf_pdf.h:70


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