contrib/brl/bbas/bsta/io/bsta_io_histogram.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/io/bsta_io_histogram.h
00002 #ifndef bsta_io_histogram_h_
00003 #define bsta_io_histogram_h_
00004 //:
00005 // \file
00006 // \brief Binary I/O for bsta_histogram
00007 // \author J.L. Mundy
00008 // \date February 29, 2008
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   <none yet>
00013 // \endverbatim
00014 
00015 #include <bsta/bsta_histogram.h>
00016 #include <bsta/bsta_joint_histogram.h>
00017 #include <bsta/bsta_joint_histogram_3d.h>
00018 #include <vsl/vsl_binary_io.h>
00019 #include <vsl/vsl_vector_io.h>
00020 #include <vbl/vbl_array_2d.h>
00021 #include <vbl/io/vbl_io_array_2d.h>
00022 #include <vbl/vbl_array_3d.h>
00023 #include <vbl/io/vbl_io_array_3d.h>
00024 #include <vcl_iostream.h>
00025 
00026 //: Binary save bsta_histogram to stream.
00027 template <class T>
00028 void
00029 vsl_b_write(vsl_b_ostream &os, const bsta_histogram<T>& h)
00030 {
00031   const short io_version_no = 1;
00032   vsl_b_write(os, io_version_no);
00033   bsta_histogram_base::bsta_hist_type type = h.type_;
00034   int itype = static_cast<int>(type);
00035   int nbins = h.nbins();
00036   T min = h.min();
00037   T max = h.max();
00038   T min_prob = h.min_prob();
00039   vcl_vector<T> values = h.value_array();
00040   vcl_vector<T> counts = h.count_array();
00041   vsl_b_write(os, itype);
00042   vsl_b_write(os, nbins);
00043   vsl_b_write(os, min);
00044   vsl_b_write(os, max);
00045   vsl_b_write(os, min_prob);
00046   vsl_b_write(os, values);
00047   vsl_b_write(os, counts);
00048 }
00049 
00050 //: Binary load bsta_histogram from stream.
00051 template <class T>
00052 void
00053 vsl_b_read(vsl_b_istream &is, bsta_histogram<T>& h, bool skip = false)
00054 {
00055   short ver;
00056   int itype = 0;
00057   if (!skip){
00058     vsl_b_read(is, ver);
00059     if (ver != 1)
00060       return;
00061     vsl_b_read(is, itype);
00062   }
00063   int nbins;
00064   T min, max, min_prob;
00065   vcl_vector<T> values, counts;
00066   vsl_b_read(is, nbins);
00067   vsl_b_read(is, min);
00068   vsl_b_read(is, max);
00069   vsl_b_read(is, min_prob);
00070   vsl_b_read(is, values);
00071   vsl_b_read(is, counts);
00072   bsta_histogram<T> temp(min, max, nbins, min_prob);
00073   for (unsigned i = 0; i<static_cast<unsigned>(nbins); ++i)
00074     temp.upcount(values[i], counts[i]);
00075   h = temp;
00076 }
00077 
00078 //: Print summary
00079 template <class T>
00080 void
00081 vsl_print_summary(vcl_ostream &os, const bsta_histogram<T>& h)
00082 {
00083   os << "bsta_histogram\n";
00084   h.print(os);
00085 }
00086 
00087 
00088 void vsl_b_write(vsl_b_ostream& os, const bsta_histogram_base* hptr);
00089 
00090 void vsl_b_read(vsl_b_istream &is, bsta_histogram_base*& hptr);
00091 
00092 void vsl_b_write(vsl_b_ostream& os, const bsta_histogram_base_sptr& hptr);
00093 
00094 void vsl_b_read(vsl_b_istream &is, bsta_histogram_base_sptr& hptr);
00095 
00096 
00097 //: Binary save bsta_joint_histogram to stream.
00098 template <class T>
00099 void
00100 vsl_b_write(vsl_b_ostream &os, const bsta_joint_histogram<T>& h)
00101 {
00102   const short io_version_no = 1;
00103   vsl_b_write(os, io_version_no);
00104   bsta_joint_histogram_base::bsta_joint_hist_type type = h.type_;
00105   int itype = static_cast<int>(type);
00106   vsl_b_write(os, itype);
00107   int nbins_a = h.nbins_a();
00108   int nbins_b = h.nbins_b();
00109   T min_a = h.min_a();
00110   T max_a = h.max_a();
00111   T min_b = h.min_b();
00112   T max_b = h.max_b();
00113   T min_prob = h.min_prob();
00114   vbl_array_2d<T> counts = h.counts();
00115   vsl_b_write(os, nbins_a);
00116   vsl_b_write(os, min_a);
00117   vsl_b_write(os, max_a);
00118   vsl_b_write(os, nbins_b);
00119   vsl_b_write(os, min_b);
00120   vsl_b_write(os, max_b);
00121   vsl_b_write(os, min_prob);
00122   vsl_b_write(os, counts);
00123 }
00124 
00125 //: Binary load bsta_joint_histogram from stream.
00126 template <class T>
00127 void
00128 vsl_b_read(vsl_b_istream &is, bsta_joint_histogram<T>& h,
00129            bool skip = false)
00130 {
00131   short ver;
00132   int itype = 0;
00133   if (!skip){
00134     vsl_b_read(is, ver);
00135     if (ver != 1)
00136       return;
00137     vsl_b_read(is, itype);
00138   }
00139   int nbins_a, nbins_b;
00140   T min_a, max_a, min_b, max_b, min_prob;
00141   vbl_array_2d<T> counts;
00142   vsl_b_read(is, nbins_a);
00143   vsl_b_read(is, min_a);
00144   vsl_b_read(is, max_a);
00145   vsl_b_read(is, nbins_b);
00146   vsl_b_read(is, min_b);
00147   vsl_b_read(is, max_b);
00148   vsl_b_read(is, min_prob);
00149   vsl_b_read(is, counts);
00150   bsta_joint_histogram<T> temp(min_a, max_a, nbins_a, min_b, max_b, nbins_b, min_prob);
00151   unsigned nr = counts.rows(), nc = counts.cols();
00152   for (unsigned r = 0; r<nr; ++r)
00153     for (unsigned c = 0; c<nc; ++c)
00154       temp.set_count(r, c, counts[r][c]);
00155   h = temp;
00156 }
00157 
00158 
00159 //: Print summary
00160 template <class T>
00161 void
00162 vsl_print_summary(vcl_ostream &os, const bsta_joint_histogram<T>& h)
00163 {
00164   os << "bsta_joint_histogram\n";
00165   h.print(os);
00166 }
00167 
00168 void vsl_b_write(vsl_b_ostream &os, const bsta_joint_histogram_base* hptr);
00169 
00170 void vsl_b_read(vsl_b_istream &is, bsta_joint_histogram_base*& hptr);
00171 
00172 void vsl_b_write(vsl_b_ostream& os, const bsta_joint_histogram_base_sptr& hptr);
00173 void vsl_b_read(vsl_b_istream &is, bsta_joint_histogram_base_sptr& hptr);
00174 
00175 
00176 //: Binary save bsta_joint_histogram_3d to stream.
00177 template <class T>
00178 void
00179 vsl_b_write(vsl_b_ostream &os, const bsta_joint_histogram_3d<T>& h)
00180 {
00181   const short io_version_no = 1;
00182   vsl_b_write(os, io_version_no);
00183   bsta_joint_histogram_3d_base::bsta_joint_hist_3d_type type = h.type_;
00184   int itype = static_cast<int>(type);
00185   vsl_b_write(os, itype);
00186   int nbins_a = h.nbins_a();
00187   int nbins_b = h.nbins_b();
00188   int nbins_c = h.nbins_c();
00189   T min_a = h.min_a();
00190   T max_a = h.max_a();
00191   T min_b = h.min_b();
00192   T max_b = h.max_b();
00193   T min_c = h.min_c();
00194   T max_c = h.max_c();
00195   T min_prob = h.min_prob();
00196   vbl_array_3d<T> counts = h.counts();
00197   vsl_b_write(os, nbins_a);
00198   vsl_b_write(os, min_a);
00199   vsl_b_write(os, max_a);
00200   vsl_b_write(os, nbins_b);
00201   vsl_b_write(os, min_b);
00202   vsl_b_write(os, max_b);
00203   vsl_b_write(os, nbins_c);
00204   vsl_b_write(os, min_c);
00205   vsl_b_write(os, max_c);
00206   vsl_b_write(os, min_prob);
00207   vsl_b_write(os, counts);
00208 }
00209 
00210 //: Binary load bsta_joint_histogram_3d from stream.
00211 template <class T>
00212 void
00213 vsl_b_read(vsl_b_istream &is,  bsta_joint_histogram_3d<T>& h,
00214            bool skip = false)
00215 {
00216   short ver;
00217   int itype = 0;
00218   if (!skip){
00219     vsl_b_read(is, ver);
00220     if (ver != 1)
00221       return;
00222     vsl_b_read(is, itype);
00223   }
00224   int nbins_a, nbins_b, nbins_c;
00225   T min_a, max_a, min_b, max_b, min_c, max_c, min_prob;
00226   vbl_array_3d<T> counts;
00227   vsl_b_read(is, nbins_a);
00228   vsl_b_read(is, min_a);
00229   vsl_b_read(is, max_a);
00230   vsl_b_read(is, nbins_b);
00231   vsl_b_read(is, min_b);
00232   vsl_b_read(is, max_b);
00233   vsl_b_read(is, nbins_c);
00234   vsl_b_read(is, min_c);
00235   vsl_b_read(is, max_c);
00236   vsl_b_read(is, min_prob);
00237   vsl_b_read(is, counts);
00238   bsta_joint_histogram_3d<T> temp(min_a, max_a, nbins_a, min_b, max_b, nbins_b,
00239                                   min_c, max_c, nbins_c,min_prob);
00240   unsigned na = counts.get_row1_count(), nb = counts.get_row2_count(),
00241     nc = counts.get_row3_count();
00242   for (unsigned a = 0; a<na; ++a)
00243     for (unsigned b = 0; b<nb; ++b)
00244       for (unsigned c = 0; c<nc; ++c)
00245         temp.set_count(a, b, c, counts[a][b][c]);
00246   h = temp;
00247 }
00248 
00249 //: Print summary
00250 template <class T>
00251 void
00252 vsl_print_summary(vcl_ostream &os, const bsta_joint_histogram_3d<T>& h)
00253 {
00254   os << "bsta_joint_histogram_3d\n";
00255   h.print(os);
00256 }
00257 
00258 void vsl_b_write(vsl_b_ostream &os, const bsta_joint_histogram_3d_base* hptr);
00259 
00260 void vsl_b_read(vsl_b_istream &is, bsta_joint_histogram_3d_base*& hptr);
00261 
00262 void vsl_b_write(vsl_b_ostream& os, const bsta_joint_histogram_3d_base_sptr& hptr);
00263 
00264 
00265 void vsl_b_read(vsl_b_istream &is, bsta_joint_histogram_3d_base_sptr& hptr);
00266 
00267 
00268 #endif // bsta_io_histogram_h_