core/vpdl/vpdt/vpdt_mixture_detector.h
Go to the documentation of this file.
00001 // This is core/vpdl/vpdt/vpdt_mixture_detector.h
00002 #ifndef vpdt_mixture_detector_h_
00003 #define vpdt_mixture_detector_h_
00004 //:
00005 // \file
00006 // \brief Detectors applying to mixtures
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date March 11, 2009
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   <None yet>
00013 // \endverbatim
00014 
00015 #include <vpdl/vpdt/vpdt_field_traits.h>
00016 
00017 //: Apply a detector to each component to see if ANY match
00018 //  Return true if any component matches
00019 template <class mixture_type, class detector_type>
00020 class vpdt_mixture_any_detector
00021 {
00022  public:
00023   //: the functor return type
00024   typedef bool return_type;
00025   //: the functor return type
00026   static const unsigned int return_dim = 1;
00027   //: the distribution operated on by the detector
00028   typedef mixture_type distribution_type;
00029 
00030   //: the data type to represent a point in the field
00031   typedef typename mixture_type::field_type F;
00032   //: define the scalar type (normally specified by template parameter T)
00033   typedef typename vpdt_field_traits<F>::scalar_type T;
00034 
00035   //: Constructor
00036   vpdt_mixture_any_detector(const detector_type& d) : detect(d) {}
00037 
00038   //: The main function
00039   bool operator() (const mixture_type& mix, const F& sample, bool& result) const
00040   {
00041     result = false;
00042     for (unsigned int i=0; i<mix.num_components(); ++i){
00043       if ( !detect(mix.distribution(i),sample,result) )
00044         return false;
00045       if (result)
00046         return true;
00047     }
00048     return true;
00049   }
00050 
00051   //: The detector to apply to components
00052   detector_type detect;
00053 };
00054 
00055 
00056 //: Apply a detector to each component to see if ALL match
00057 //  Return true if all components match
00058 template <class mixture_type, class detector_type>
00059 class vpdt_mixture_all_detector
00060 {
00061  public:
00062   //: the functor return type
00063   typedef bool return_type;
00064   //: the functor return type
00065   static const unsigned int return_dim = 1;
00066   //: the distribution operated on by the detector
00067   typedef mixture_type distribution_type;
00068 
00069   //: the data type to represent a point in the field
00070   typedef typename mixture_type::field_type F;
00071   //: define the scalar type (normally specified by template parameter T)
00072   typedef typename vpdt_field_traits<F>::scalar_type T;
00073 
00074   //: Constructor
00075   vpdt_mixture_all_detector(const detector_type& d) : detect(d) {}
00076 
00077   //: The main function
00078   bool operator() (const mixture_type& mix, const F& sample, bool& result) const
00079   {
00080     result = true;
00081     for (unsigned int i=0; i<mix.num_components(); ++i){
00082       if ( !detect(mix.distribution(i),sample,result) )
00083         return false;
00084       if (!result)
00085         return true;
00086     }
00087     return true;
00088   }
00089 
00090   //: The detector to apply to components
00091   detector_type detect;
00092 };
00093 
00094 
00095 //: Apply a detector to each component in order while the total weight is below a threshold.
00096 //  Return true if any tested component matches
00097 template <class mixture_type, class detector_type>
00098 class vpdt_mixture_top_weight_detector
00099 {
00100  public:
00101   //: the functor return type
00102   typedef bool return_type;
00103   //: the functor return type
00104   static const unsigned int return_dim = 1;
00105   //: the distribution operated on by the detector
00106   typedef mixture_type distribution_type;
00107 
00108   //: the data type to represent a point in the field
00109   typedef typename mixture_type::field_type F;
00110   //: define the scalar type (normally specified by template parameter T)
00111   typedef typename vpdt_field_traits<F>::scalar_type T;
00112 
00113   //: Constructor
00114   vpdt_mixture_top_weight_detector(const detector_type& d, const T& w=T(0.5))
00115     : detect(d), weight_thresh(w) {}
00116 
00117   //: The main function
00118   bool operator() (const mixture_type& mix, const F& sample, bool& result) const
00119   {
00120     T total_weight = T(0);
00121     result = false;
00122     for (unsigned int i=0; i<mix.num_components(); ++i){
00123       if (total_weight > weight_thresh)
00124         return true;
00125       if ( !detect(mix.distribution(i),sample,result) )
00126         return false;
00127       if (result)
00128         return true;
00129       total_weight += mix.weight(i);
00130     }
00131     return true;
00132   }
00133 
00134   //: The detector to apply to components
00135   detector_type detect;
00136   //: The total weight threshold
00137   T weight_thresh;
00138 };
00139 
00140 
00141 #endif // vpdt_mixture_detector_h_