contrib/brl/bbas/bsta/algo/bsta_mixture_functors.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/algo/bsta_mixture_functors.h
00002 #ifndef bsta_mixture_functors_h_
00003 #define bsta_mixture_functors_h_
00004 //:
00005 // \file
00006 // \brief Functors that apply to mixtures of distributions
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date March 29, 2006
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   (none yet)
00013 // \endverbatim
00014 
00015 #include <bsta/bsta_mixture.h>
00016 
00017 
00018 //: Compute the weighted sum of functor value while the total weight is below a threshold.
00019 template <class mixture_, class functor_>
00020 class bsta_top_weight_functor
00021 {
00022   public:
00023     typedef typename mixture_::math_type T;
00024     typedef typename functor_::return_T return_T;
00025     typedef return_T return_type; // for compatibility with vpdl/vdpt
00026     typedef vnl_vector_fixed<T,mixture_::dimension> vector_;
00027     enum { return_dim = functor_::return_dim };
00028 
00029     //: Constructor
00030     bsta_top_weight_functor(const T& w=T(0.5)) : functor(), weight_thresh(w) {}
00031     //: Constructor
00032     bsta_top_weight_functor(const functor_& f, const T& w=T(0.5))
00033       : functor(f), weight_thresh(w) {}
00034 
00035     //: The main function
00036     bool operator() (const mixture_& mix, const vector_& sample, return_T& retval) const
00037     {
00038       const unsigned int nc = mix.num_components();
00039       if (nc == 0)
00040         return false;
00041 
00042       return_T temp;
00043       if ( !functor(mix.distribution(0),sample,temp) )
00044         return false;
00045       T w = mix.weight(0);
00046       T total_weight = w;
00047       retval = w * temp;
00048 
00049       for (unsigned int i=1; i<nc; ++i){
00050         if (total_weight > weight_thresh)
00051           break;
00052         if ( !functor(mix.distribution(i),sample,temp) )
00053           return false;
00054         w = mix.weight(i);
00055         total_weight += w;
00056         retval += w * temp;
00057       }
00058       retval /= total_weight;
00059       return true;
00060     }
00061 
00062     //: The functor to apply to components
00063     functor_ functor;
00064     //: The index to detect
00065     T weight_thresh;
00066 };
00067 
00068 
00069 #endif // bsta_mixture_functors_h_