vbl_io_bounding_box.hxx
Go to the documentation of this file.
1 // This is core/vbl/io/vbl_io_bounding_box.hxx
2 #ifndef vbl_io_bounding_box_hxx_
3 #define vbl_io_bounding_box_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include "vbl_io_bounding_box.h"
9 #include <vsl/vsl_binary_io.h>
10 
11 //==========================================================================
12 //: Binary save self to stream.
13 template<class T, class DIM_>
14 void vsl_b_write(vsl_b_ostream &os, const vbl_bounding_box_base<T, DIM_> & p)
15 {
16  constexpr short io_version_no = 1;
17  vsl_b_write(os, io_version_no);
18  vsl_b_write(os, !p.empty());
19  for (int i = 0; i< p.dimension(); i++)
20  {
21  vsl_b_write(os, p.min()[i]);
22  vsl_b_write(os, p.max()[i]);
23  }
24 }
25 
26 //=========================================================================
27 //: Binary load self from stream.
28 template<class T, class DIM_>
29 void vsl_b_read(vsl_b_istream &is, vbl_bounding_box_base<T, DIM_> & p)
30 {
31  if (!is) return;
32 
33  short v;
34  vsl_b_read(is, v);
35  switch (v)
36  {
37  case 1:
38  p.reset(); // empty the bounding box
39  bool b; vsl_b_read(is, b);
40  if (b) {
41  T min_point[DIM_::value], max_point[DIM_::value];
42  for (int i = 0; i< p.dimension(); i++)
43  {
44  vsl_b_read(is, min_point[i]);
45  vsl_b_read(is, max_point[i]);
46  }
47  p.update(min_point); p.update(max_point);
48  }
49  break;
50 
51  default:
52  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_bounding_box_base<T, DIM_>&)\n"
53  << " Unknown version number "<< v << '\n';
54  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
55  return;
56  }
57 }
58 
59 
60 //===========================================================================
61 //: Output a human readable summary to the stream
62 template<class T, class DIM_>
63 void vsl_print_summary(std::ostream& os,const vbl_bounding_box_base<T, DIM_> & p)
64 {
65  os << '\n';
66  if (p.empty())
67  os << "empty\n";
68  else {
69  for (int i=0;i<p.dimension();i++)
70  {
71  os << "min[" << i << "] = " << p.min()[i] << '\n';
72  }
73  os << '\n';
74  for (int i=0;i<p.dimension();i++)
75  {
76  os << "max[" << i << "] = " << p.max()[i] << '\n';
77  }
78  }
79  os << '\n';
80 }
81 
82 #define VBL_IO_BOUNDING_BOX_INSTANTIATE(T, DIM) \
83 template void vsl_print_summary(std::ostream&,const vbl_bounding_box_base<T, vbl_bounding_box_DIM<DIM > >&);\
84 template void vsl_b_read(vsl_b_istream &, vbl_bounding_box_base<T, vbl_bounding_box_DIM<DIM > > &); \
85 template void vsl_b_write(vsl_b_ostream &, const vbl_bounding_box_base<T, vbl_bounding_box_DIM<DIM > > &)
86 
87 #endif // vbl_io_bounding_box_hxx_
T const * min() const
void vsl_b_write(vsl_b_ostream &os, const vbl_bounding_box_base< T, DIM_ > &p)
Binary save self to stream.
void reset()
Reset to empty.
void vsl_print_summary(std::ostream &os, const vbl_bounding_box_base< T, DIM_ > &p)
Output a human readable summary to the stream.
T const * max() const
int dimension() const
return dimension.
void vsl_b_read(vsl_b_istream &is, vbl_bounding_box_base< T, DIM_ > &p)
Binary load self from stream.
bool empty() const
Return initialisation status.
void update(T const &x)
Incorporate 1d point x.