gaussian_vector.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 * 
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 * 
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 * 
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 * 
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Wim Meeussen */
00036 /* modified for SRS by Alex Noyvirt */
00037 
00038 #include "srs_people_tracking_filter/gaussian_vector.h"
00039 #include <wrappers/rng/rng.h>
00040 #include <cmath> 
00041 #include <cassert>
00042 
00043 using namespace tf;
00044 
00045 namespace BFL
00046 {
00047   GaussianVector::GaussianVector(const Vector3& mu, const Vector3& sigma)
00048     : Pdf<Vector3> ( 1 ),
00049       mu_(mu),
00050       sigma_(sigma),
00051       sigma_changed_(true)
00052   {
00053     for (unsigned int i=0; i<3; i++)
00054       assert(sigma[i] > 0);
00055   }
00056 
00057 
00058   GaussianVector::~GaussianVector(){}
00059 
00060 
00061   std::ostream& operator<< (std::ostream& os, const GaussianVector& g)
00062   {
00063     os << "Mu   :\n" << g.ExpectedValueGet() << endl
00064        << "Sigma:\n" << g.CovarianceGet() << endl;
00065     return os;
00066   }
00067 
00068   void GaussianVector::sigmaSet( const Vector3& sigma )
00069   {
00070     sigma_ = sigma;
00071     sigma_changed_ = true;
00072   }
00073 
00074   Probability GaussianVector::ProbabilityGet(const Vector3& input) const
00075   {
00076     if (sigma_changed_){
00077       sigma_changed_ = false;
00078       // 2 * sigma^2
00079       for (unsigned int i=0; i<3; i++)
00080         sigma_sq_[i] = 2 * sigma_[i] * sigma_[i];
00081       // sqrt
00082       sqrt_ = 1/ sqrt(M_PI*M_PI*M_PI* sigma_sq_[0] * sigma_sq_[1] * sigma_sq_[2]);
00083     }
00084 
00085     Vector3 diff = input - mu_;
00086     return sqrt_ * exp( - (diff[0]*diff[0]/sigma_sq_[0])
00087                         - (diff[1]*diff[1]/sigma_sq_[1])
00088                         - (diff[2]*diff[2]/sigma_sq_[2]) );
00089   }
00090 
00091 
00092   bool
00093   GaussianVector::SampleFrom (vector<Sample<Vector3> >& list_samples, const int num_samples, int method, void * args) const
00094   {
00095     list_samples.resize(num_samples);
00096     vector<Sample<Vector3> >::iterator sample_it = list_samples.begin();
00097     for (sample_it=list_samples.begin(); sample_it!=list_samples.end(); sample_it++)
00098       SampleFrom( *sample_it, method, args);
00099 
00100     return true;
00101   }
00102 
00103 
00104   bool
00105   GaussianVector::SampleFrom (Sample<Vector3>& one_sample, int method, void * args) const
00106   {
00107     one_sample.ValueSet( Vector3(rnorm(mu_[0], sigma_[0]), 
00108                                  rnorm(mu_[1], sigma_[1]),
00109                                  rnorm(mu_[2], sigma_[2])));
00110     return true;
00111   }
00112 
00113 
00114   Vector3
00115   GaussianVector::ExpectedValueGet (  ) const 
00116   { 
00117     return mu_;
00118   }
00119 
00120   SymmetricMatrix
00121   GaussianVector::CovarianceGet () const
00122   {
00123     SymmetricMatrix sigma(3); sigma = 0;
00124     for (unsigned int i=0; i<3; i++)
00125       sigma(i+1,i+1) = pow(sigma_[i],2);
00126     return sigma;
00127   }
00128 
00129   GaussianVector* 
00130   GaussianVector::Clone() const
00131   {
00132     return new GaussianVector(mu_, sigma_);
00133   }
00134 
00135 } // End namespace BFL


srs_people_tracking_filter
Author(s): Alex Noyvirt
autogenerated on Sun Jan 5 2014 12:18:09