contrib/brl/bbas/bsta/bsta_parzen.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/bsta_parzen.h
00002 #ifndef bsta_parzen_h_
00003 #define bsta_parzen_h_
00004 //:
00005 // \file
00006 // \brief A distribution based on a number of samples (Parzen windows).
00007 // \author Joseph L. Mundy
00008 // \date October 12, 2008
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   (none yet)
00013 // \endverbatim
00014 //
00015 // Do not remove the following statement
00016 // Approved for Public Release, Distribution Unlimited (DISTAR Case 12529)
00017 //
00018 
00019 #include "bsta_distribution.h"
00020 #include <vcl_vector.h>
00021 #include <vcl_iostream.h>
00022 
00023 //: forward declare vnl_vector_fixed
00024 template<class T, unsigned n> class vnl_vector_fixed;
00025 
00026 //: forward declare parzen
00027 template<class T, unsigned n> class bsta_parzen;
00028 
00029 //: functor for sorting samples in descending order of likelihood
00030 template<class T, unsigned n>
00031 class vless
00032 {
00033  public:
00034   vless():parz_ptr_(0){}
00035   vless(bsta_parzen<T, n>* parz_ptr): parz_ptr_(parz_ptr){}
00036   bool operator()(vnl_vector_fixed<T,n> const& va,
00037                   vnl_vector_fixed<T,n> const& vb) const
00038   {
00039     if (!parz_ptr_) return false;
00040       if (!parz_ptr_->size()) return false;
00041     T pda = parz_ptr_->prob_density(va);
00042     T pdb = parz_ptr_->prob_density(vb);
00043     return pdb < pda; // descending order
00044   }
00045   bool operator()(T const& va, T const& vb) const
00046   {
00047     if (!parz_ptr_) return false;
00048     bool comp = parz_ptr_->prob_density(vb)<parz_ptr_->prob_density(va);
00049     return comp;
00050   }
00051  private:
00052   //: pointer to the parzen distribution for computing probability density
00053   bsta_parzen<T, n>* parz_ptr_;
00054 };
00055 
00056 //: A Parzen distribution
00057 // used as a component of the mixture
00058 template <class T, unsigned n>
00059 class bsta_parzen : public bsta_distribution<T,n>
00060 {
00061  public:
00062   typedef typename bsta_distribution<T,n>::vector_type vect_t;
00063   typedef vcl_vector<vect_t > sample_vector;
00064   typedef typename sample_vector::const_iterator sv_const_it;
00065 
00066  protected:
00067   //: The samples
00068   sample_vector samples_;
00069 
00070  public:
00071 
00072   bsta_parzen() {}
00073   virtual ~bsta_parzen() {}
00074 
00075   bsta_parzen(sample_vector const& samples)
00076   : samples_(samples) {}
00077 
00078   //: Insert a new sample into the distribution
00079   void insert_sample(const vect_t& sample)
00080   { samples_.push_back(sample); }
00081 
00082   //: Insert a vector of samples
00083   void insert_samples(const sample_vector & samples)
00084   { for (sv_const_it sit = samples.begin();
00085          sit != samples.end(); ++sit)
00086       samples_.push_back(*sit);
00087   }
00088 
00089   sample_vector samples() const
00090   { return samples_; }
00091 
00092   vect_t sample(unsigned index) const
00093   { if (index>=samples_.size())
00094     { vect_t v(T(0)); return v; }
00095     return samples_[index];
00096   }
00097 
00098   bool remove_sample(unsigned index)
00099   { if (index>=samples_.size()) return false;
00100     samples_.erase(samples_.begin()+index);
00101     return true;
00102   }
00103 
00104   //: clear the sample set
00105   void clear() { samples_.clear(); }
00106 
00107   //: number of samples
00108   unsigned size() const { return samples_.size(); }
00109 };
00110 
00111 #endif // bsta_parzen_h_