The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

histogram3d.h

Go to the documentation of this file.
00001 #ifndef __HISTOGRAM_3d__
00002 #define __HISTOGRAM_3d__
00003 
00004 #include "preprocessor.h"
00005 #include "common.h"
00006 
00007 #include <fstream>
00008 
00009 using std::ofstream;
00010 using std::ifstream;
00011 
00013 
00014 typedef struct tagHISTBIN { 
00015   int b1; 
00016   int b2; 
00017   int b3; 
00018 
00019         tagHISTBIN() {
00020                 b1 = 0;
00021                 b2 = 0; 
00022                 b3 = 0;
00023         }
00024 
00025         inline tagHISTBIN(int _b1, int _b2, int _b3) {
00026                 set_bin(_b1, _b2, _b3);
00027         }
00028 
00029         inline void set_bin (int _b1, int _b2, int _b3) {
00030                 b1 = _b1;
00031                 b2 = _b2; 
00032                 b3 = _b3;
00033         }
00034 } HISTBIN; 
00035 
00037 
00038 template<int BINSIZE> class histogram3d {
00039 private:
00040         double m_hist[BINSIZE][BINSIZE][BINSIZE];       
00041         int m_totalMemSize;
00042         int m_numBins;
00043         int m_numColors;
00044 
00045         inline double & get_prob(HISTBIN & bin) {
00046                 return((*this)[bin]);
00047         }
00048 
00049         inline void rgb2bin(const RGBTRIPLE & rgb, HISTBIN & bin) {
00050                 const double alpha = 0.0;       // -0.5 left edge of distribution
00051                 const double binwidth = static_cast<double>(m_numColors)/static_cast<double>(BINSIZE);
00052                 int r = floor( static_cast<double>(rgb.rgbtRed-alpha)/binwidth );
00053                 int g = floor( static_cast<double>(rgb.rgbtGreen-alpha)/binwidth );
00054                 int b = floor( static_cast<double>(rgb.rgbtBlue-alpha)/binwidth );
00055                 bin.set_bin(r, g, b);
00056         }
00057 
00058         inline void bin2rgb(const HISTBIN & bin, RGBTRIPLE & rgb) {
00059                 const double alpha = 0.0;       // -0.5 left edge of distribution
00060                 const double binwidth = static_cast<double>(m_numColors)/static_cast<double>(BINSIZE);
00061                 rgb.rgbtRed = (bin.b1 - alpha)*binwidth + alpha;
00062                 rgb.rgbtGreen = (bin.b2 - alpha)*binwidth + alpha;
00063                 rgb.rgbtBlue = (bin.b2 - alpha)*binwidth + alpha;
00064         }
00065 
00066   inline void addtobin(HISTBIN & bin, double value = 1.0) {
00067                 (*this)[bin] += value;
00068         }
00069 
00070 public:
00071         histogram3d() {
00072                 m_numBins = BINSIZE*BINSIZE*BINSIZE;
00073                 m_totalMemSize = m_numBins * sizeof(double);
00074                 m_numColors = 256;
00075                 clear();
00076         }
00077         
00078         inline double *begin () {
00079                 return (&(m_hist[0][0][0]));
00080         }
00081 
00082         inline const double *end () {
00083                 return(&(m_hist[BINSIZE-1][BINSIZE-1][BINSIZE-1])+1);
00084         }
00085 
00086         inline double & operator[] (HISTBIN & bin) {
00087                 return(m_hist[bin.b1][bin.b2][bin.b3]);
00088         }
00089 
00090   inline void addtobin(const RGBTRIPLE & rgb, const double value = 1.0) {
00091                 HISTBIN bin;
00092                 rgb2bin(rgb, bin);
00093                 addtobin(bin, value);
00094         }
00095 
00096         inline double & get_prob(const RGBTRIPLE & rgb) {
00097                 HISTBIN bin;
00098                 rgb2bin(rgb, bin);
00099                 return(get_prob(bin));
00100         }
00101 
00102         void clear () {
00103                 memset(m_hist, 0, m_totalMemSize);
00104         }
00105 
00106         histogram3d & copy(histogram3d & hist) {
00107                 memcpy(hist.m_hist, m_hist, m_totalMemSize);
00108                 return(hist);
00109         }
00110 
00111         void normalize() {
00112                 double s = sum();
00113                 for (double *it = begin(); it != end(); it++) {
00114                         if (*it)
00115                                 *it /= s;
00116                 }
00117         }
00118 
00119         double sum() {
00120                 double s = 0.0;
00121                 for (double *it = begin(); it != end(); it++)
00122                         s += *it;
00123                 return (s);
00124         }
00125 
00126         void add_uniform (double weight = 1.0) {
00127                 const double uniform = weight*(1.0/static_cast<double>(m_numBins));
00128                 for (double *it = begin(); it != end(); it++)
00129                         *it = uniform + *it*(1-weight);
00130         }
00131 
00132         void weighted_add (histogram3d & hist, const double & weight) {
00133                 for (double *toptr = begin(), *fromptr = hist.begin(); toptr != end(); toptr++, fromptr++)
00134                         *toptr = *toptr*weight + *fromptr*(1-weight);
00135         }
00136 
00137         void print_values (ofstream &os) {
00138                 for (double *it = begin(); it != end(); it++)
00139                         os << *it << endl;
00140         }
00141 
00142         void load_from_file (ifstream &is) {
00143                 clear();
00144                 for (double *it = begin(); it != end(); it++)
00145                         is >> *it;
00146         }
00147 
00148         void save_to_file (ofstream &os) {
00149                 print_values (os);
00150         }
00151 };
00152 
00154 
00155 #endif __HISTOGRAM_3d__

Generated on Mon Nov 8 17:07:36 2004 for MPT by  doxygen 1.3.9.1