core/vpgl/io/vpgl_io_rational_camera.txx
Go to the documentation of this file.
00001 #ifndef vpgl_io_rational_camera_txx_
00002 #define vpgl_io_rational_camera_txx_
00003 
00004 #include "vpgl_io_rational_camera.h"
00005 //:
00006 // \file
00007 #include <vpgl/vpgl_rational_camera.h>
00008 #include <vnl/io/vnl_io_matrix_fixed.h>
00009 
00010 template <class T>
00011 void vsl_b_write(vsl_b_ostream & os, vpgl_rational_camera<T> const& camera)
00012 {
00013   if (!os) return;
00014   unsigned version = 1;
00015   vsl_b_write(os, version);
00016   // write rational coefficients
00017   vsl_b_write(os, camera.coefficient_matrix());
00018   // write scale offsets
00019   vcl_vector<vpgl_scale_offset<T> > scale_off = camera.scale_offsets();
00020   vsl_b_write(os,scale_off.size());
00021   for (unsigned i=0; i<scale_off.size(); ++i) {
00022     vsl_b_write(os,scale_off[i].scale());
00023     vsl_b_write(os,scale_off[i].offset());
00024   }
00025 }
00026 
00027 //: Binary load camera from stream.
00028 template <class T>
00029 void vsl_b_read(vsl_b_istream & is, vpgl_rational_camera<T> &camera)
00030 {
00031   if (!is) return;
00032   short ver;
00033   vsl_b_read(is, ver);
00034   switch (ver)
00035   {
00036     case 1:
00037     {
00038       // read rational coefficients
00039       vnl_matrix_fixed<T, 4, 20> coefficient_matrix;
00040       vsl_b_read(is,coefficient_matrix);
00041       // read scale+offsets
00042       unsigned n_scale_offsets;
00043       vsl_b_read(is,n_scale_offsets);
00044       vcl_vector<vpgl_scale_offset<T> > scale_off(n_scale_offsets);
00045       for (unsigned i=0; i<n_scale_offsets; ++i) {
00046         T scale, off;
00047         vsl_b_read(is,scale);
00048         vsl_b_read(is,off);
00049         scale_off[i] = vpgl_scale_offset<T>(scale,off);
00050       }
00051       camera.set_coefficients(coefficient_matrix);
00052       camera.set_scale_offsets(scale_off);
00053       break;
00054     }
00055     default:
00056       vcl_cerr << "I/O ERROR: vpgl_rational_camera::b_read(vsl_b_istream&)\n"
00057                << "           Unknown version number "<< ver << '\n';
00058       is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00059       return;
00060   }
00061 }
00062 
00063 //: Print human readable summary of object to a stream
00064 template <class T>
00065 void vsl_print_summary(vcl_ostream& os,const vpgl_rational_camera<T> & c)
00066 {
00067   os << c << '\n';
00068 }
00069 
00070 
00071 #define VPGL_IO_RATIONAL_CAMERA_INSTANTIATE(T) \
00072 template void vsl_b_write(vsl_b_ostream & os, vpgl_rational_camera<T > const& camera); \
00073 template void vsl_b_read(vsl_b_istream & is, vpgl_rational_camera<T > &camera); \
00074 template void vsl_print_summary(vcl_ostream& os,const vpgl_rational_camera<T > & b)
00075 
00076 #endif // vpgl_io_rational_camera_txx_