1 // This is core/vgl/io/vgl_io_box_2d.hxx
2 #ifndef vgl_io_box_2d_hxx_
3 #define vgl_io_box_2d_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include "vgl_io_box_2d.h"
9 #include <vsl/vsl_binary_io.h>
10 
11 //============================================================================
12 //: Binary save self to stream.
13 template<class T>
vsl_b_write(vsl_b_ostream & os,const vgl_box_2d<T> & p)14 void vsl_b_write(vsl_b_ostream &os, const vgl_box_2d<T> & p)
15 {
16   constexpr short io_version_no = 1;
17   vsl_b_write(os, io_version_no);
18   vsl_b_write(os, p.min_x());
19   vsl_b_write(os, p.min_y());
20   vsl_b_write(os, p.max_x());
21   vsl_b_write(os, p.max_y());
22 }
23 
24 //============================================================================
25 //: Binary load self from stream.
26 template<class T>
vsl_b_read(vsl_b_istream & is,vgl_box_2d<T> & p)27 void vsl_b_read(vsl_b_istream &is, vgl_box_2d<T> & p)
28 {
29   if (!is) return;
30 
31   short v;
32   T min_pos[2];
33   T max_pos[2];
34   vsl_b_read(is, v);
35   switch (v)
36   {
37    case 1:
38     vsl_b_read(is, min_pos[0]);
39     vsl_b_read(is, min_pos[1]);
40     vsl_b_read(is, max_pos[0]);
41     vsl_b_read(is, max_pos[1]);
42 
43     p.set_min_x(min_pos[0]);
44     p.set_min_y(min_pos[1]);
45     p.set_max_x(max_pos[0]);
46     p.set_max_y(max_pos[1]);
47     break;
48 
49    default:
50     std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vgl_box_2d<T>&)\n"
51              << "           Unknown version number "<< v << '\n';
52     is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
53     return;
54   }
55 }
56 
57 //============================================================================
58 //: Output a human readable summary to the stream
59 template<class T>
vsl_print_summary(std::ostream & os,const vgl_box_2d<T> & p)60 void vsl_print_summary(std::ostream& os,const vgl_box_2d<T> & p)
61 {
62   if (p.is_empty())
63     os<<"Empty 2d box\n";
64   else
65     os<<"2d box with opposite corners at (" <<p.min_x() << ',' << p.min_y()
66       <<") and (" << p.max_x() << ',' << p.max_y() << ")\n";
67 }
68 
69 #define VGL_IO_BOX_2D_INSTANTIATE(T) \
70 template void vsl_print_summary(std::ostream &, const vgl_box_2d<T > &); \
71 template void vsl_b_read(vsl_b_istream &, vgl_box_2d<T > &); \
72 template void vsl_b_write(vsl_b_ostream &, const vgl_box_2d<T > &)
73 
74 #endif // vgl_io_box_2d_hxx_
75