contrib/rpl/rgrl/rgrl_feature_reader.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Smart reader for reading in features.
00004 // \author Gehua yang
00005 // \date Aug 04 2004
00006 
00007 #include "rgrl_feature_reader.h"
00008 #include <rgrl/rgrl_feature_point.h>
00009 #include <rgrl/rgrl_feature_landmark.h>
00010 #include <rgrl/rgrl_feature_face_pt.h>
00011 #include <rgrl/rgrl_feature_trace_pt.h>
00012 
00013 #include <rgrl/rgrl_util.h>
00014 #include <vcl_iostream.h>
00015 #include <vcl_string.h>
00016 #include <vcl_compiler.h>
00017 
00018 // initialize the static variables
00019 vcl_vector< rgrl_feature_sptr >  rgrl_feature_reader::feature_candidates_;
00020 
00021 
00022 #undef READ_THIS_FEATURE
00023 #define READ_THIS_FEATURE(tag, fea) \
00024   if ( tag_str.find(tag) == 0 ){ \
00025     fea* fea_ptr = new fea(); \
00026     fea_ptr->read(is); \
00027     return fea_ptr; \
00028   }
00029 
00030 void rgrl_feature_reader::add_feature( rgrl_feature_sptr feat )
00031 {
00032   feature_candidates_.push_back(feat);
00033 }
00034 
00035 rgrl_feature_sptr
00036 rgrl_feature_reader::
00037 read( vcl_istream& is )
00038 {
00039   vcl_string tag_str;
00040   vcl_streampos pos;
00041 
00042   // 1. get to the tag line and save the position
00043   //
00044   // skip any empty lines
00045   rgrl_util_skip_empty_lines( is );
00046   // store current reading position
00047   pos = is.tellg();
00048   vcl_getline( is, tag_str );
00049 
00050   // 2. try classes stored in the vector
00051   //
00052   // back to the beginning of the tag line
00053   is.seekg( pos );
00054 
00055   typedef vcl_vector< rgrl_feature_sptr >::const_iterator iter;
00056   for( iter i=feature_candidates_.begin(); i!=feature_candidates_.end(); ++i ) {
00057 
00058     // make a copy of the transformation
00059     rgrl_feature_sptr candidate = (*i)->clone();
00060     if( candidate->read( is ) )
00061       return candidate;
00062 
00063     // else reset the pos
00064     is.seekg( pos );
00065   }
00066 
00067   // 3. built-in classes are handled in a different way
00068   // use the following macro to read in each specific feature.
00069   // The first argument is a string to identify the feature.
00070   // The second is the corresponding feature class
00071   //
00072   READ_THIS_FEATURE("POINT",    rgrl_feature_point)
00073   READ_THIS_FEATURE("LANDMARK", rgrl_feature_landmark)
00074   READ_THIS_FEATURE("FACE",     rgrl_feature_face_pt)
00075   READ_THIS_FEATURE("TRACE",    rgrl_feature_trace_pt)
00076   // default, should never reach here
00077   vcl_cout<< "WARNING: " << RGRL_HERE << " ( line "
00078       << __LINE__ << " )\n"
00079       << "       " << "Tag " << tag_str
00080       << " cannot match with any existing features.\n"
00081       << "         Try to open istream in BINARY mode!" << vcl_endl;
00082   return 0;
00083 }
00084 
00085 //: Read a feature from input stream
00086 //  The type of feature depends on the content of the input stream.
00087 //  NULL smart ptr is returned if reading fails.
00088 //  Please check the validity of the return smart ptr
00089 rgrl_feature_sptr
00090 rgrl_feature_reader( vcl_istream& is )
00091 {
00092   return rgrl_feature_reader::read(is);
00093 }
00094 
00095 //: stream operator for reading feature
00096 vcl_istream&
00097 operator>> (vcl_istream& is, rgrl_feature_sptr& fea_sptr)
00098 {
00099   fea_sptr = rgrl_feature_reader( is );
00100   return is;
00101 }
00102