core/vbl/io/vbl_io_bounding_box.txx
Go to the documentation of this file.
00001 // This is core/vbl/io/vbl_io_bounding_box.txx
00002 #ifndef vbl_io_bounding_box_txx_
00003 #define vbl_io_bounding_box_txx_
00004 //:
00005 // \file
00006 
00007 #include "vbl_io_bounding_box.h"
00008 #include <vsl/vsl_binary_io.h>
00009 
00010 //==========================================================================
00011 //: Binary save self to stream.
00012 template<class T, class DIM_>
00013 void vsl_b_write(vsl_b_ostream &os, const vbl_bounding_box_base<T, DIM_> & p)
00014 {
00015   const short io_version_no = 1;
00016   vsl_b_write(os, io_version_no);
00017   vsl_b_write(os, !p.empty());
00018   for (int i = 0; i< p.dimension(); i++)
00019   {
00020     vsl_b_write(os, p.min()[i]);
00021     vsl_b_write(os, p.max()[i]);
00022   }
00023 }
00024 
00025 //=========================================================================
00026 //: Binary load self from stream.
00027 template<class T, class DIM_>
00028 void vsl_b_read(vsl_b_istream &is, vbl_bounding_box_base<T, DIM_> & p)
00029 {
00030   if (!is) return;
00031 
00032   short v;
00033   vsl_b_read(is, v);
00034   switch (v)
00035   {
00036    case 1:
00037     p.reset(); // empty the bounding box
00038     bool b; vsl_b_read(is, b);
00039     if (b) {
00040       T min_point[DIM_::value], max_point[DIM_::value];
00041       for (int i = 0; i< p.dimension(); i++)
00042       {
00043         vsl_b_read(is, min_point[i]);
00044         vsl_b_read(is, max_point[i]);
00045       }
00046       p.update(min_point); p.update(max_point);
00047     }
00048     break;
00049 
00050    default:
00051     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_bounding_box_base<T, DIM_>&)\n"
00052              << "           Unknown version number "<< v << '\n';
00053     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00054     return;
00055   }
00056 }
00057 
00058 
00059 //===========================================================================
00060 //: Output a human readable summary to the stream
00061 template<class T, class DIM_>
00062 void vsl_print_summary(vcl_ostream& os,const vbl_bounding_box_base<T, DIM_> & p)
00063 {
00064     os << '\n';
00065     if (p.empty())
00066       os << "empty\n";
00067     else {
00068       for (int i=0;i<p.dimension();i++)
00069       {
00070         os << "min[" << i << "] = " << p.min()[i] << '\n';
00071       }
00072       os << '\n';
00073       for (int i=0;i<p.dimension();i++)
00074       {
00075         os << "max[" << i << "] = " << p.max()[i] << '\n';
00076       }
00077     }
00078     os << '\n';
00079 }
00080 
00081 #define VBL_IO_BOUNDING_BOX_INSTANTIATE(T, DIM) \
00082 template void vsl_print_summary(vcl_ostream&,const vbl_bounding_box_base<T, vbl_bounding_box_DIM<DIM > >&);\
00083 template void vsl_b_read(vsl_b_istream &, vbl_bounding_box_base<T, vbl_bounding_box_DIM<DIM > > &); \
00084 template void vsl_b_write(vsl_b_ostream &, const vbl_bounding_box_base<T, vbl_bounding_box_DIM<DIM >  > &)
00085 
00086 #endif // vbl_io_bounding_box_txx_