contrib/rpl/rgrl/rgrl_trans_reader.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Smart reader function for reading in ASCII transformation file.
00004 // \author Gehua yang, Chia-ling Tsai
00005 // \date Aug 04 2004
00006 
00007 #include "rgrl_trans_reader.h"
00008 #include <rgrl/rgrl_trans_translation.h>
00009 #include <rgrl/rgrl_trans_similarity.h>
00010 #include <rgrl/rgrl_trans_affine.h>
00011 #include <rgrl/rgrl_trans_reduced_quad.h>
00012 #include <rgrl/rgrl_trans_quadratic.h>
00013 #include <rgrl/rgrl_trans_homography2d.h>
00014 #include <rgrl/rgrl_trans_rad_dis_homo2d.h>
00015 #include <rgrl/rgrl_trans_rigid.h>
00016 #include <rgrl/rgrl_trans_couple.h>
00017 #include <rgrl/rgrl_trans_spline.h>
00018 #include <rgrl/rgrl_util.h>
00019 #include <vcl_iostream.h>
00020 #include <vcl_fstream.h>
00021 #include <vcl_string.h>
00022 #include <vcl_compiler.h>
00023 
00024 // initialize the static variables
00025 vcl_vector< rgrl_transformation_sptr >  rgrl_trans_reader::xform_candidates_;
00026 
00027 rgrl_transformation_sptr
00028 rgrl_trans_reader::
00029 read( char const* fn )
00030 {
00031   vcl_ifstream ifs( fn, vcl_ios_in|vcl_ios_binary );
00032   if( ifs.good() )
00033     return read( ifs );
00034   else
00035     return 0;
00036 }
00037 
00038 #undef READ_THIS_TRANSFORMATION
00039 #define READ_THIS_TRANSFORMATION(tag, trans) \
00040   if ( tag_str.find(tag) == 0 ){    \
00041     trans* trans_ptr = new trans(); \
00042     if( trans_ptr->read(is) )       \
00043       return trans_ptr;             \
00044     else                            \
00045       return 0;                     \
00046   }
00047 
00048 //: Read a transformation from input stream
00049 //  The type of transformation depends on the content of the input stream.
00050 //  NULL smart ptr is returned if reading fails.
00051 //  Please check the validity of the return smart ptr
00052 rgrl_transformation_sptr
00053 rgrl_trans_reader::
00054 read( vcl_istream& is )
00055 {
00056   vcl_string tag_str;
00057   vcl_streampos pos;
00058 
00059   // 1. get to the tag line and save the position
00060   //
00061   // skip any empty lines
00062   rgrl_util_skip_empty_lines( is );
00063   // store current reading position
00064   pos = is.tellg();
00065   vcl_getline( is, tag_str );
00066 
00067   // 2. try classes stored in the vector
00068   //
00069   // back to the beginning of the tag line
00070   is.seekg( pos );
00071 
00072   typedef vcl_vector< rgrl_transformation_sptr >::const_iterator iter;
00073   for( iter i=xform_candidates_.begin(); i!=xform_candidates_.end(); ++i ) {
00074     
00075     // make a copy of the transformation
00076     rgrl_transformation_sptr candidate = (*i)->clone();
00077     if( candidate->read( is) )
00078       return candidate;
00079 
00080     // else reset the pos
00081     is.seekg( pos );
00082   }
00083 
00084   // 3. built-in classes are handled in a different way
00085   // use the following macro to read in each specific transformation.
00086   // The first argument is a string to identify the transformation.
00087   // The second is the corresponding transformation class
00088   //
00089   // NOTE: due to the use of find function, the order of the following is important
00090   //       If one tag is a subset of the other tag, then it must be after the other one.
00091   //       For instance, AFFINE_NEW must be in front of AFFINE. Otherwise, 
00092   //       it is read in as AFFINE
00093   //       
00094   READ_THIS_TRANSFORMATION("TRANSLATION", rgrl_trans_translation)
00095   READ_THIS_TRANSFORMATION("SIMILARITY", rgrl_trans_similarity)
00096   READ_THIS_TRANSFORMATION("AFFINE", rgrl_trans_affine)
00097   READ_THIS_TRANSFORMATION("REDUCED_QUADRATIC", rgrl_trans_reduced_quad)
00098   READ_THIS_TRANSFORMATION("RIGID", rgrl_trans_rigid)
00099   READ_THIS_TRANSFORMATION("QUADRATIC", rgrl_trans_quadratic)
00100   READ_THIS_TRANSFORMATION("BSPLINE", rgrl_trans_spline)
00101   READ_THIS_TRANSFORMATION("HOMOGRAPHY2D_WITH_RADIAL_DISTORTION", rgrl_trans_rad_dis_homo2d)
00102   READ_THIS_TRANSFORMATION("HOMOGRAPHY2D", rgrl_trans_homography2d)
00103   READ_THIS_TRANSFORMATION("COUPLE_TRANS", rgrl_trans_couple)
00104 
00105   // default, should never reach here
00106   vcl_cout<< "WARNING: " << RGRL_HERE << " ( line "
00107           << __LINE__ << " )\n"
00108           << "       " << "Tag [" << tag_str
00109           << "] cannot match with any existing transformations.\n"
00110           << "         Try to open istream in BINARY mode!" << vcl_endl;
00111   return 0;
00112 }
00113 
00114 void
00115 rgrl_trans_reader::
00116 add_xform( rgrl_transformation_sptr xform )
00117 {
00118   xform_candidates_.push_back( xform );
00119 }
00120 
00121 //: stream operator for reading transformation
00122 vcl_istream&
00123 operator>> (vcl_istream& is, rgrl_transformation_sptr& trans_sptr)
00124 {
00125   trans_sptr = rgrl_trans_reader::read( is );
00126   return is;
00127 }
00128