multivariate_gaussian.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
37 #ifndef STOMP_MOVEIT_MULTIVARIATE_GAUSSIAN_H_
38 #define STOMP_MOVEIT_MULTIVARIATE_GAUSSIAN_H_
39 
40 #include <Eigen/Core>
41 #include <Eigen/Cholesky>
42 #include <boost/random/variate_generator.hpp>
43 #include <boost/random/normal_distribution.hpp>
44 #include <boost/random/mersenne_twister.hpp>
45 #include <boost/shared_ptr.hpp>
46 #include <cstdlib>
47 
48 namespace stomp_moveit
49 {
50 
51 namespace utils
52 {
53 
54 class MultivariateGaussian;
55 typedef std::shared_ptr<MultivariateGaussian> MultivariateGaussianPtr;
56 
61 {
62 public:
63  template <typename Derived1, typename Derived2>
64  MultivariateGaussian(const Eigen::MatrixBase<Derived1>& mean, const Eigen::MatrixBase<Derived2>& covariance);
65 
71  template <typename Derived>
72  void sample(Eigen::MatrixBase<Derived>& output,bool use_covariance = true);
73 
74 private:
75  Eigen::VectorXd mean_;
76  Eigen::MatrixXd covariance_;
77  Eigen::MatrixXd covariance_cholesky_;
79  int size_;
80  boost::mt19937 rng_;
81  boost::normal_distribution<> normal_dist_;
82  std::shared_ptr<boost::variate_generator<boost::mt19937, boost::normal_distribution<> > > gaussian_;
83 };
84 
86 
87 template <typename Derived1, typename Derived2>
88 MultivariateGaussian::MultivariateGaussian(const Eigen::MatrixBase<Derived1>& mean, const Eigen::MatrixBase<Derived2>& covariance):
89  mean_(mean),
90  covariance_(covariance),
91  covariance_cholesky_(covariance_.llt().matrixL()),
92  normal_dist_(0.0,1.0)
93 {
94 
95  rng_.seed(rand());
96  size_ = mean.rows();
97  gaussian_.reset(new boost::variate_generator<boost::mt19937, boost::normal_distribution<> >(rng_, normal_dist_));
98 }
99 
100 template <typename Derived>
101 void MultivariateGaussian::sample(Eigen::MatrixBase<Derived>& output,bool use_covariance)
102 {
103  for (int i=0; i<size_; ++i)
104  output(i) = (*gaussian_)();
105 
106  if(use_covariance)
107  {
108  output = mean_ + covariance_cholesky_*output;
109  }
110  else
111  {
112  output = mean_ + output;
113  }
114 }
115 
116 }
117 
118 }
119 
120 #endif /* STOMP_MOVEIT_MULTIVARIATE_GAUSSIAN_H_ */
void sample(Eigen::MatrixBase< Derived > &output, bool use_covariance=true)
generates random values using a normal distribution.
Generates samples from a multivariate gaussian distribution.


stomp_moveit
Author(s): Jorge Nicho
autogenerated on Fri May 8 2020 03:35:47