contrib/rpl/rgrl/rgrl_initializer_reader.cxx
Go to the documentation of this file.
00001 #include "rgrl_initializer_reader.h"
00002 //:
00003 // \file
00004 // \author Gehua Yang
00005 
00006 #include <rgrl/rgrl_view.h>
00007 #include <rgrl/rgrl_transformation.h>
00008 #include <rgrl/rgrl_trans_reader.h>
00009 #include <rgrl/rgrl_util.h>
00010 #include <rgrl/rgrl_scale.h>
00011 
00012 
00013 rgrl_initializer_reader::
00014 rgrl_initializer_reader(vcl_istream& istr,
00015                         rgrl_mask_sptr             const& from_image_roi,
00016                         rgrl_mask_sptr             const& to_image_roi,
00017                         rgrl_scale_sptr            const& prior_scale,
00018                         rgrl_estimator_sptr        const& estimator,
00019                         unsigned int                      resolution )
00020   : xform_index_(0),
00021     from_image_roi_( from_image_roi ),
00022     to_image_roi_( to_image_roi ),
00023     prior_scale_( prior_scale ),
00024     estimator_( estimator ),
00025     res_( resolution )
00026 {
00027   // ASSUMING 2D
00028   //
00029   // here read in the number of initializations.
00030   int num=-1;
00031 
00032   istr>> num;
00033   if ( !istr || num < 0 ) {
00034     WarningMacro( "Cannot read in the number of initializations." );
00035     return;
00036   }
00037 
00038   // go through inits
00039   for ( int i=0; i<num; ++i ) {
00040 
00041     // read in transformation
00042     init_record one;
00043     rgrl_mask_box region(2), global_region(2);
00044     bool is_global_region_set = false;
00045 
00046     rgrl_transformation_sptr xform = rgrl_trans_reader::read( istr );
00047     if ( !istr || !xform ) {
00048       WarningMacro( "Cannot parse transformation" );
00049       return;
00050     }
00051     one.xform_ = xform;
00052 
00053     while (true)
00054     {
00055       vcl_streampos pos;
00056       vcl_string tag_str;
00057 
00058       // skip any empty lines
00059       rgrl_util_skip_empty_lines( istr );
00060 
00061       // store current reading position
00062       pos = istr.tellg();
00063       vcl_getline( istr, tag_str );
00064 
00065       if ( tag_str.find( "REGION" ) == 0 ) {
00066 
00067         vnl_vector<double> x0(2), x1(2);
00068         istr >> x0 >> x1;
00069         if ( !istr ) {
00070           WarningMacro( "Cannot parse region" );
00071           return;
00072         }
00073 
00074         region.set_x0( x0 );
00075         region.set_x1( x1 );
00076       }
00077       else if ( tag_str.find( "GLOBAL_REGION" ) == 0 )
00078       {
00079         vnl_vector<double> x0(2), x1(2);
00080         istr >> x0 >> x1;
00081         if ( !istr ) {
00082           WarningMacro( "Cannot parse region" );
00083           return;
00084         }
00085 
00086         global_region.set_x0( x0 );
00087         global_region.set_x1( x1 );
00088         is_global_region_set = true;
00089       }
00090       else if ( tag_str.find( "GEOMETRIC_SCALE" ) == 0 )
00091       {
00092         double scale=-1.0;
00093         istr >> scale;
00094         if ( !istr ) {
00095           WarningMacro( "Cannot parse scale" );
00096           return;
00097         }
00098         one.scale_ = new rgrl_scale;
00099         one.scale_->set_geometric_scale( scale, rgrl_scale::prior );
00100       }
00101       else
00102       {
00103         // Maybe it is the beginning of next transformation
00104         // back to the beginning of the tag line
00105         istr.seekg( pos );
00106 
00107         // break the loop
00108         break;
00109       }
00110     }
00111 
00112     // estimate global region
00113     if ( !is_global_region_set ) {
00114       global_region =
00115         rgrl_util_estimate_global_region(from_image_roi_,
00116                                          to_image_roi_,
00117                                          from_image_roi_->bounding_box(),
00118                                          *one.xform_);
00119     }
00120 
00121     // should use general prior_scale?
00122     if ( !one.scale_ )
00123       one.scale_ = prior_scale_;
00124 
00125     //inverse transformation
00126     rgrl_transformation_sptr inverse_xform;
00127     if ( xform->is_invertible() )
00128       inverse_xform = xform->inverse_transform();
00129 
00130     // setup view
00131     one.view_ = new rgrl_view( from_image_roi_,
00132                                to_image_roi_,
00133                                region,
00134                                global_region,
00135                                estimator_,
00136                                one.xform_,
00137                                res_,
00138                                inverse_xform );
00139 
00140     // store
00141     init_records_.push_back( one );
00142   }
00143 }
00144 
00145 bool
00146 rgrl_initializer_reader::
00147 next_initial( rgrl_view_sptr           & view,
00148               rgrl_scale_sptr          & prior_scale )
00149 {
00150   if ( xform_index_ >= init_records_.size())
00151     return false;
00152 
00153   init_record const& one = init_records_[xform_index_];
00154   //view = new rgrl_view( from_image_roi_,
00155   //                      to_image_roi_,
00156   //                      init_view_->region(),
00157   //                      init_view_->global_region(),
00158   //                      init_view_->xform_estimator(),
00159   //                      xforms_[xform_index_],
00160   //                      init_view_->resolution() );
00161   view = one.view_;
00162   prior_scale = rgrl_initializer::enforce_prior_scale( one.scale_ );
00163   xform_index_++;
00164 
00165   return true;
00166 }
00167 
00168 int
00169 rgrl_initializer_reader::
00170 size() const
00171 {
00172   return int( init_records_.size() );
00173 }