contrib/mul/pdf1d/pdf1d_gaussian_sampler.cxx
Go to the documentation of this file.
00001 // This is mul/pdf1d/pdf1d_gaussian_sampler.cxx
00002 
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Sampler class for univariate Gaussian classes.
00007 
00008 #include "pdf1d_gaussian_sampler.h"
00009 
00010 #include <vcl_cassert.h>
00011 #include <pdf1d/pdf1d_gaussian.h>
00012 
00013 //=======================================================================
00014 // Dflt ctor
00015 //=======================================================================
00016 
00017 pdf1d_gaussian_sampler::pdf1d_gaussian_sampler():
00018  rng_(9667566ul)
00019 {
00020 }
00021 
00022 //=======================================================================
00023 // Destructor
00024 //=======================================================================
00025 
00026 pdf1d_gaussian_sampler::~pdf1d_gaussian_sampler()
00027 {
00028 }
00029 
00030 
00031 //=======================================================================
00032 // Method: is_a
00033 //=======================================================================
00034 
00035 vcl_string pdf1d_gaussian_sampler::is_a() const
00036 {
00037   return vcl_string("pdf1d_gaussian_sampler");
00038 }
00039 
00040 //=======================================================================
00041 // Method: is_class
00042 //=======================================================================
00043 
00044 bool pdf1d_gaussian_sampler::is_class(vcl_string const& s) const
00045 {
00046   return pdf1d_sampler::is_class(s) || s==pdf1d_gaussian_sampler::is_a();
00047 }
00048 
00049 //=======================================================================
00050 // Method: clone
00051 //=======================================================================
00052 
00053 pdf1d_sampler* pdf1d_gaussian_sampler::clone() const
00054 {
00055   return new pdf1d_gaussian_sampler(*this);
00056 }
00057 //=======================================================================
00058 
00059 void pdf1d_gaussian_sampler::reseed(unsigned long seed)
00060 {
00061   rng_.reseed(seed);
00062 }
00063 //=======================================================================
00064 
00065 //: Set model for which this is an instance
00066 // Error check that it is an axis gaussian.
00067 void pdf1d_gaussian_sampler::set_model(const pdf1d_pdf& model)
00068 {
00069   assert(model.is_class("pdf1d_gaussian"));
00070   // cannot use dynamic_cast<> without rtti - PVr
00071   // rtti currently turned off
00072   pdf1d_sampler::set_model(model);
00073 }
00074 
00075 //=======================================================================
00076 
00077 double pdf1d_gaussian_sampler::sample()
00078 {
00079   const pdf1d_gaussian & gauss = static_cast<const pdf1d_gaussian &>(model());
00080   return  gauss.mean() + gauss.sd()*rng_.normal();
00081 }
00082 
00083 //: Fill x with samples possibly chosen so as to represent the distribution
00084 //  5 or fewer samples requested, they are spaced out equally.
00085 void pdf1d_gaussian_sampler::regular_samples(vnl_vector<double>& x)
00086 {
00087   int n = x.size();
00088   if (n>5)
00089   {
00090     pdf1d_sampler::regular_samples(x);
00091     return;
00092   }
00093 
00094   // Strictly should select samples so that CDF(x) is equally spread in [0,1]
00095 
00096   const pdf1d_gaussian & gauss = static_cast<const pdf1d_gaussian &>( model());
00097   double lim = gauss.sd()*(n-1)/2.0;
00098   double mean = gauss.mean();
00099 
00100   for (int i=0;i<n;++i)
00101     x[i] = mean+lim * (2*(double(i)/(n-1))-1);
00102 }
00103 
00104 
00105 //=======================================================================
00106 
00107 
00108 //: Return a reference to the pdf model
00109 // This is properly cast.
00110 const pdf1d_gaussian& pdf1d_gaussian_sampler::gaussian() const
00111 {
00112   return static_cast<const pdf1d_gaussian&>( model());
00113 }
00114