contrib/rpl/rgrl/rgrl_scale.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \author Amitha Perera
00004 // \date   Feb 2003
00005 // \verbatim
00006 // Modifications
00007 //      Nov 2008 J Becker: Modified read so that one can easily read in a vector of scales.
00008 // \endverbatim
00009 
00010 #include "rgrl_scale.h"
00011 
00012 
00013 rgrl_scale::
00014 rgrl_scale()
00015   : has_geometric_scale_( false ),
00016     has_signature_inv_covar_( false )
00017 {
00018 }
00019 
00020 
00021 bool
00022 rgrl_scale::
00023 has_geometric_scale() const
00024 {
00025   return has_geometric_scale_;
00026 }
00027 
00028 
00029 double
00030 rgrl_scale::
00031 geometric_scale( ) const
00032 {
00033   return geometric_scale_;
00034 }
00035 
00036 
00037 bool
00038 rgrl_scale::
00039 has_signature_inv_covar() const
00040 {
00041   return has_signature_inv_covar_;
00042 }
00043 
00044 
00045 vnl_matrix<double> const&
00046 rgrl_scale::
00047 signature_inv_covar() const
00048 {
00049   return signature_inv_covar_;
00050 }
00051 
00052 
00053 void
00054 rgrl_scale::
00055 set_scales( double                     geometric_scale,
00056             vnl_matrix<double>  const& signature_inv_covar,
00057             type new_type )
00058 {
00059   set_geometric_scale( geometric_scale );
00060   set_signature_inv_covar( signature_inv_covar );
00061   set_geo_scale_type( new_type );
00062 }
00063 
00064 
00065 void
00066 rgrl_scale::
00067 set_geometric_scale( double scale, type new_type )
00068 {
00069   geometric_scale_ = scale;
00070   has_geometric_scale_ = true;
00071   set_geo_scale_type( new_type );
00072 }
00073 
00074 
00075 void
00076 rgrl_scale::
00077 set_signature_inv_covar( vnl_matrix<double> const& covar )
00078 {
00079   signature_inv_covar_ = covar;
00080   has_signature_inv_covar_ = true;
00081 }
00082 
00083 void
00084 rgrl_scale::
00085 write( vcl_ostream& os ) const
00086 {
00087   // write out geometric scale
00088   if( has_geometric_scale_ ) {
00089 
00090     os << "GEOMETRIC_SCALE" << vcl_endl;
00091     os << ((geo_scale_type_==prior)?"PRIOR":"ESTIMATE") << vcl_endl;
00092     os << geometric_scale_ << vcl_endl;
00093   }
00094   
00095   // write out signature covariance
00096   if( has_signature_inv_covar_ ) {
00097     
00098     os << "SIGNATURE_INV_COVARIANCE" << vcl_endl;
00099     os << signature_inv_covar_.rows() << " " 
00100       << signature_inv_covar_.cols() << vcl_endl;
00101     os << signature_inv_covar_ << vcl_endl;
00102   }
00103 }
00104 
00105 bool
00106 rgrl_scale::
00107 read( vcl_istream& is )
00108 {
00109   vcl_streampos pos;
00110   static const vcl_string white_chars = " \t\r";
00111   vcl_string tag;
00112   has_geometric_scale_ = false;
00113   has_signature_inv_covar_ = false;
00114 
00115   // continue when stream does not reach the end 
00116   // and when the scales are not all read
00117   while( is && !is.eof() && (!has_geometric_scale_ || !has_signature_inv_covar_))  {
00118     pos = is.tellg();
00119     vcl_getline( is, tag );
00120     
00121     if( tag.empty() || tag.find_first_not_of( white_chars ) == vcl_string::npos )
00122       continue;
00123     
00124     // geometric scale
00125     if( tag.find("GEOMETRIC_SCALE") != vcl_string::npos ) {
00126       if ( !has_geometric_scale_ && !has_signature_inv_covar_ ) {
00127         // get scale type
00128         vcl_string type_str;
00129         vcl_getline( is, type_str );
00130         if( type_str.find("PRIOR") != vcl_string::npos ) 
00131           geo_scale_type_ = prior;
00132         else if( type_str.find("ESTIMATE") != vcl_string::npos ) 
00133           geo_scale_type_ = estimate;
00134         else {  // cannot handle
00135           WarningMacro( "Cannot parse this line for geometric scale type: " << type_str << vcl_endl );
00136           return false;
00137         }
00138         
00139         // get scale
00140         is >> geometric_scale_;
00141         if( !is ) {
00142           WarningMacro( "Cannot parse geometric scale." << vcl_endl );
00143           return false;
00144         }
00145         
00146         // set flag
00147         has_geometric_scale_ = true;
00148       } else {
00149         is.seekg( pos );
00150         return true;
00151       }
00152     } else if ( tag.find("SIGNATURE_INV_COVARIANCE") != vcl_string::npos ) {
00153       if( !has_signature_inv_covar_ ) {
00154         // signature covariance
00155         int nrow = -1;
00156         int ncol = -1;
00157         
00158         // get number of rows and cols
00159         is >> nrow >> ncol;
00160         if( !is || nrow<=0 || ncol<=0 ) {
00161           WarningMacro( "Cannot parse the number of rows and columns." << vcl_endl );
00162           return false;
00163         }
00164         
00165         signature_inv_covar_.set_size( nrow, ncol );
00166         is >> signature_inv_covar_;
00167 
00168         if( !is ) {
00169           WarningMacro( "Cannot parse signature covariance" << vcl_endl );
00170           return false;
00171         }
00172         
00173         // set the flag
00174         has_signature_inv_covar_ = true;
00175       } else {
00176         is.seekg( pos );
00177         return true;
00178       }
00179     } else {// cannot handle
00180       WarningMacro( "Cannot parse this line for tags: " << tag << vcl_endl );
00181       return false;
00182     }
00183   }
00184   
00185   // succeeded
00186   return true;
00187 }
00188 
00189 //: output operator
00190 vcl_ostream&
00191 operator<<( vcl_ostream& ofs, rgrl_scale const& scale )
00192 {
00193   scale.write(ofs);
00194   return ofs;
00195 }
00196 
00197 //: input operator
00198 vcl_istream&
00199 operator>>( vcl_istream& ifs, rgrl_scale& scale )
00200 {
00201   scale.read(ifs);
00202   return ifs;
00203 }