contrib/brl/bbas/bsta/bsta_joint_histogram_3d.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/bsta_joint_histogram_3d.h
00002 #ifndef bsta_joint_histogram_3d_h_
00003 #define bsta_joint_histogram_3d_h_
00004 //:
00005 // \file
00006 // \brief A simple joint_histogram_3d class
00007 // \author Joseph L. Mundy
00008 // \date   May 19, 2004
00009 //
00010 // A templated joint_histogram_3d class.  Supports entropy calculations
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //  none
00015 // \endverbatim
00016 
00017 #include <vbl/vbl_array_3d.h>
00018 #include <vcl_vector.h>
00019 #include <vcl_iostream.h>
00020 #include <vcl_cassert.h>
00021 #include <bsta/bsta_joint_histogram_3d_base.h>
00022 template <class T> class bsta_joint_histogram_3d : 
00023 public bsta_joint_histogram_3d_base
00024 {
00025  public:
00026   bsta_joint_histogram_3d();
00027 
00028   bsta_joint_histogram_3d(const T range, const unsigned nbins,
00029                     const T min_prob = 0.0);
00030 
00031   bsta_joint_histogram_3d(const T min, const T max, const unsigned nbins,
00032                     const T min_prob = 0.0);
00033 
00034   bsta_joint_histogram_3d(const T range_a, const unsigned nbins_a,
00035                     const T range_b, const unsigned nbins_b, 
00036                     const T range_c, const unsigned nbins_c, 
00037                     const T min_prob = 0.0);
00038 
00039   //:More general constructor defining a signed value range
00040   bsta_joint_histogram_3d(const T min_a, const T max_a,
00041                     const unsigned nbins_a,
00042                     const T min_b, const T max_b,
00043                     const unsigned nbins_b,
00044                     const T min_c, const T max_c,
00045                     const unsigned nbins_c,
00046                     const T min_prob = 0.0);
00047 
00048  ~bsta_joint_histogram_3d() {}
00049 
00050  //: legacy use where a, b and c, have the same bin granularity
00051  unsigned nbins() const {
00052    assert(nbins_a_==nbins_b_&&nbins_a_==nbins_c_);
00053    return nbins_a_;}
00054 
00055  //: number of bins for variable a
00056   unsigned nbins_a() const {return nbins_a_;}
00057 
00058  //: number of bins for variable b
00059   unsigned nbins_b() const {return nbins_b_;}
00060 
00061  //: number of bins for variable b
00062   unsigned nbins_c() const {return nbins_c_;}
00063 
00064  //: legacy use where a and b have the same range
00065   T range() const {
00066     assert(range_a_==range_b_&&range_a_==range_c_);
00067     return range_a_;}
00068 
00069   //: range for variable a
00070   T range_a() const {return range_a_;}  
00071  //: range for variable b
00072   T range_b() const {return range_b_;}
00073  //: range for variable c
00074   T range_c() const {return range_c_;}
00075 
00076  //: min value for variable a
00077   T min_a() const {return min_a_;}
00078  //: max value for variable a
00079   T max_a() const {return max_a_;}
00080 
00081  //: min value for variable b
00082   T min_b() const {return min_b_;}
00083  //: max value for variable b
00084   T max_b() const {return max_b_;}
00085 
00086  //: min value for variable b
00087   T min_c() const {return min_c_;}
00088  //: max value for variable b
00089   T max_c() const {return max_c_;}
00090 
00091   //: delta value for variable a
00092   T delta_a() const { return delta_a_; }
00093   //: delta value for variable b
00094   T delta_b() const { return delta_b_; }
00095   //: delta value for variable c
00096   T delta_c() const { return delta_c_; }
00097 
00098   T min_prob() const {return min_prob_;}
00099 
00100   vbl_array_3d<T> counts() const {return counts_;}
00101 
00102   //: update the count of the cell corresponding to the input values
00103   void upcount(T a, T mag_a,
00104                T b, T mag_b,
00105                T c, T mag_c);
00106 
00107   //: smooth histogram with a spherical Gaussian kernel
00108   void parzen(const T sigma);
00109 
00110   //: access by bin index
00111   T p(unsigned ia, unsigned ib, unsigned ic) const;
00112 
00113   //: access by value
00114   T p(T a, T b, T c) const;
00115 
00116   T volume() const;
00117   T entropy() const;
00118 
00119 #if 0 // not implemented
00120   T mutual_information() const;
00121   T renyi_entropy() const;
00122   T entropy_marginal_a() const;
00123   //: The average and variance bin value for row a using counts to compute probs
00124   bool avg_and_variance_bin_for_row_a(const unsigned a, T & avg, T & var) const;
00125 #endif
00126   void set_count(unsigned ia, unsigned ib, unsigned ic, T cnt)
00127   {if(ia<static_cast<unsigned>(counts_.get_row1_count())&&
00128       ib<static_cast<unsigned>(counts_.get_row2_count())&&
00129       ic<static_cast<unsigned>(counts_.get_row3_count()))
00130     counts_[ia][ib][ic]=cnt;
00131   }
00132 
00133   //:access by index
00134   T get_count(unsigned ia, unsigned ib, unsigned ic) const
00135   { if (ia<static_cast<unsigned>(counts_.get_row1_count())&&
00136         ib<static_cast<unsigned>(counts_.get_row2_count())&&
00137         ic<static_cast<unsigned>(counts_.get_row3_count()))
00138       return counts_[ia][ib][ic];
00139   else
00140     return T(0);
00141   }
00142   //:access by value
00143   T get_count(T a, T b, T c) const;
00144 
00145   // prints only cells with non-zero probability
00146   void print(vcl_ostream& os = vcl_cout) const;
00147 
00148   //: If relative_probability_scale = true, then the largest probability sphere will occupy one histogram cell. Otherwise only p=1 will occupy a full cell.
00149   // bin axis lines: a = red, b = green, c = blue
00150   // color arguments define color of bin probability spheres
00151   void print_to_vrml(vcl_ostream& os, bool relative_prob_scale = true,
00152                      T red = T(1), T green = T(0), T blue = T(0)) const;
00153   void print_to_text(vcl_ostream& os) const;
00154 
00155   //:restore to default constructor state
00156   void clear();
00157 
00158  private:
00159   void compute_volume() const; // mutable const
00160   mutable bool volume_valid_;
00161   mutable T volume_;
00162   unsigned nbins_a_, nbins_b_, nbins_c_;
00163   T range_a_, range_b_, range_c_;
00164   T delta_a_, delta_b_, delta_c_;
00165   T min_a_, max_a_;
00166   T min_b_, max_b_;
00167   T min_c_, max_c_;
00168   T min_prob_;
00169   vbl_array_3d<T> counts_;
00170 };
00171 #include <bsta/bsta_joint_histogram_3d_sptr.h>
00172 #define BSTA_JOINT_HISTOGRAM_3D_INSTANTIATE(T) extern "Please #include <bsta/bsta_joint_histogram_3d.txx>"
00173 
00174 #endif // bsta_joint_histogram_3d_h_