core/vpgl/io/vpgl_io_camera.txx
Go to the documentation of this file.
00001 #ifndef vpgl_io_camera_txx_
00002 #define vpgl_io_camera_txx_
00003 
00004 #include "vpgl_io_camera.h"
00005 //:
00006 // \file
00007 #include <vpgl/vpgl_proj_camera.h>
00008 #include <vpgl/vpgl_perspective_camera.h>
00009 #include <vpgl/vpgl_affine_camera.h>
00010 #include <vpgl/vpgl_rational_camera.h>
00011 #include <vpgl/vpgl_local_rational_camera.h>
00012 #include <vpgl/io/vpgl_io_proj_camera.h>
00013 #include <vpgl/io/vpgl_io_perspective_camera.h>
00014 #include <vpgl/io/vpgl_io_affine_camera.h>
00015 #include <vpgl/io/vpgl_io_rational_camera.h>
00016 #include <vpgl/io/vpgl_io_local_rational_camera.h>
00017 
00018 #include <vsl/vsl_binary_io.h>
00019 #include <vcl_string.h>
00020 #include <vcl_iostream.h>
00021 
00022 
00023 //: Binary save camera to stream
00024 template <class T>
00025 void vsl_b_write(vsl_b_ostream & os, vpgl_camera<T>* const& camera)
00026 {
00027   if ( camera->type_name() == "vpgl_proj_camera" ){
00028     // projective camera
00029     vpgl_proj_camera<T>* procam = static_cast<vpgl_proj_camera<T>*>(camera);
00030     vsl_b_write(os,procam->type_name());
00031     vsl_b_write(os,*procam);
00032   }
00033   else if ( camera->type_name() == "vpgl_perspective_camera" ) {
00034     // perspective camera
00035     vpgl_perspective_camera<T>* percam =
00036       static_cast<vpgl_perspective_camera<T>*>(camera);
00037     vsl_b_write(os,percam->type_name());
00038     vsl_b_write(os,*percam);
00039   }
00040   else if ( camera->type_name() == "vpgl_affine_camera" ) {
00041     // affine camera
00042     vpgl_affine_camera<T>* affcam =
00043       static_cast<vpgl_affine_camera<T>*>(camera);
00044     vsl_b_write(os,affcam->type_name());
00045     vsl_b_write(os,*affcam);
00046   }
00047   else if ( camera->type_name() == "vpgl_rational_camera" ) {
00048     // rational camera
00049     vpgl_rational_camera<T>* ratcam =
00050       static_cast<vpgl_rational_camera<T>*>(camera);
00051     vsl_b_write(os,ratcam->type_name());
00052     vsl_b_write(os,*ratcam);
00053   }
00054   else if ( camera->type_name() == "vpgl_local_rational_camera" ) {
00055     // local rational camera
00056     vpgl_local_rational_camera<T>* lratcam =
00057       static_cast<vpgl_local_rational_camera<T>*>(camera);
00058     vsl_b_write(os,lratcam->type_name());
00059     vsl_b_write(os,*lratcam);
00060   }
00061   else {
00062     vcl_cerr << "tried to write unknown camera type!\n";
00063     vcl_string cam_type("unknown");
00064     vsl_b_write(os,cam_type);
00065   }
00066   return;
00067 }
00068 
00069 
00070 //: Binary load camera from stream.
00071 template <class T>
00072 void vsl_b_read(vsl_b_istream & is, vpgl_camera<T>* &camera)
00073 {
00074   vcl_string cam_type;
00075   vsl_b_read(is,cam_type);
00076 
00077   if (cam_type == "vpgl_proj_camera") {
00078     // projective camera
00079     vpgl_proj_camera<T>* procam = new vpgl_proj_camera<T>();
00080     vsl_b_read(is,*procam);
00081     camera = procam;
00082   }
00083   else if (cam_type == "vpgl_perspective_camera") {
00084     // perspective camera
00085     vpgl_perspective_camera<T>* percam = new vpgl_perspective_camera<T>();
00086     vsl_b_read(is,*percam);
00087     camera = percam;
00088   }
00089   else if (cam_type == "vpgl_affine_camera") {
00090     // rational camera
00091     vpgl_affine_camera<T>* affcam = new vpgl_affine_camera<T>();
00092     vsl_b_read(is,*affcam);
00093     camera = affcam;
00094   }
00095   else if (cam_type == "vpgl_rational_camera") {
00096     // rational camera
00097     vpgl_rational_camera<T>* ratcam = new vpgl_rational_camera<T>();
00098     vsl_b_read(is,*ratcam);
00099     camera = ratcam;
00100   }
00101   else if (cam_type == "vpgl_local_rational_camera") {
00102     // rational camera
00103     vpgl_local_rational_camera<T>* lratcam=new vpgl_local_rational_camera<T>();
00104     vsl_b_read(is,*lratcam);
00105     camera = lratcam;
00106   }
00107   else if (cam_type == "unknown") {
00108     vcl_cerr << "cannot read camera of unknown type!\n";
00109   }
00110   else {
00111     vcl_cerr << "error reading vpgl_camera!\n";
00112   }
00113   return;
00114 }
00115 
00116 #define VPGL_IO_CAMERA_INSTANTIATE(T) \
00117 template void vsl_b_read(vsl_b_istream &, vpgl_camera<T >* &); \
00118 template void vsl_b_write(vsl_b_ostream &, vpgl_camera<T >* const&)
00119 
00120 #endif // vpgl_io_camera_txx_