core/vgl/io/vgl_io_polygon.cxx
Go to the documentation of this file.
00001 // This is core/vgl/io/vgl_io_polygon.cxx
00002 #include "vgl_io_polygon.h"
00003 //:
00004 // \file
00005 
00006 //====================================================================================
00007 //: Binary save self to stream.
00008 template <class T>
00009 void vsl_b_write(vsl_b_ostream &os, vgl_polygon<T> const& p)
00010 {
00011     const short io_version_no = 1;
00012     vsl_b_write(os, io_version_no);
00013     vsl_b_write(os, p.num_sheets());
00014     for (unsigned int i=0;i<p.num_sheets();i++)
00015     {
00016         vsl_b_write(os,p[i].size());
00017         for (unsigned int j=0;j<p[i].size();j++)
00018             vsl_b_write(os, p[i][j]);
00019     }
00020 }
00021 
00022 template void vsl_b_write(vsl_b_ostream&, vgl_polygon<float> const&);
00023 template void vsl_b_write(vsl_b_ostream&, vgl_polygon<double> const&);
00024 
00025 //====================================================================================
00026 //: Binary load self from stream.
00027 template <class T>
00028 void vsl_b_read(vsl_b_istream &is, vgl_polygon<T> & p)
00029 {
00030   if (!is) return;
00031 
00032   short v;
00033   vsl_b_read(is, v);
00034   switch (v)
00035   {
00036    case 1:
00037     unsigned int num_sheets;
00038     vsl_b_read(is, num_sheets);
00039     p.clear();
00040     for (unsigned int i=0;i<num_sheets;i++)
00041     {
00042       p.new_sheet();
00043       typedef typename vgl_polygon<T>::sheet_t::size_type size_type;
00044       size_type npoints;
00045       vsl_b_read(is, npoints);
00046       vgl_point_2d<T> point;
00047       for (size_type j=0;j<npoints;j++)
00048       {
00049         vsl_b_read(is, point);
00050         p.push_back(point);
00051       }
00052     }
00053     break;
00054 
00055    default:
00056     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vgl_polygon<T>&)\n"
00057              << "           Unknown version number "<< v << '\n';
00058     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00059     return;
00060   }
00061 }
00062 
00063 template void vsl_b_read(vsl_b_istream&, vgl_polygon<float>&);
00064 template void vsl_b_read(vsl_b_istream&, vgl_polygon<double>&);
00065 
00066 //====================================================================================
00067 //: Output a human readable summary to the stream
00068 template <class T>
00069 void vsl_print_summary(vcl_ostream& os, vgl_polygon<T>const& p)
00070 {
00071   os<<"Polygon with points defined by sheets :\n";
00072   for (unsigned int i=0;i<p.num_sheets();i++)
00073   {
00074       for (unsigned int j=0;j<p[i].size();j++)
00075           os << p[i][j].x()<<','<<p[i][j].y()<<vcl_endl;
00076       os<<vcl_endl;
00077   }
00078 }
00079 
00080 template void vsl_print_summary(vcl_ostream&, vgl_polygon<float>const&);
00081 template void vsl_print_summary(vcl_ostream&, vgl_polygon<double>const&);