contrib/rpl/rgrl/rgrl_trans_translation.cxx
Go to the documentation of this file.
00001 #include "rgrl_trans_translation.h"
00002 //:
00003 // \file
00004 // \author Charlene Tsai
00005 // \date   Dec 2003
00006 
00007 #include <vcl_cassert.h>
00008 #include <vcl_iostream.h>
00009 #include <rgrl/rgrl_util.h>
00010 
00011 rgrl_trans_translation::
00012 rgrl_trans_translation( unsigned int dimension )
00013   : trans_( vnl_vector<double>( dimension, 0.0 ) ),
00014     from_centre_( dimension, 0.0 )
00015 {
00016 }
00017 
00018 rgrl_trans_translation::
00019 rgrl_trans_translation( vnl_vector<double> const& in_trans,
00020                         vnl_matrix<double> const& in_covar )
00021   : rgrl_transformation( in_covar ),
00022     trans_( in_trans ),
00023     from_centre_( in_trans.size(), 0.0 )
00024 {
00025   if ( is_covar_set() ) {
00026     assert ( covar_.rows() == covar_.cols() );
00027     assert ( covar_.rows() == trans_.size() );
00028   }
00029 }
00030 
00031 rgrl_trans_translation::
00032 rgrl_trans_translation( vnl_vector<double> const& in_trans )
00033   : trans_( in_trans ),
00034     from_centre_( in_trans.size(), 0.0 )
00035 {
00036 }
00037 
00038 rgrl_trans_translation::
00039 rgrl_trans_translation( vnl_vector<double> const& in_trans,
00040                         vnl_matrix<double> const& in_covar,
00041                         vnl_vector<double> const& in_from_centre,
00042                         vnl_vector<double> const& in_to_centre )
00043   : rgrl_transformation( in_covar ),
00044     trans_( in_trans + in_to_centre ),
00045     from_centre_( in_from_centre )
00046 {
00047   if ( is_covar_set() ) {
00048     assert ( covar_.rows() == covar_.cols() );
00049     assert ( covar_.rows() == trans_.size() );
00050   }
00051   assert ( from_centre_.size() == trans_.size() );
00052 }
00053 
00054 void
00055 rgrl_trans_translation::
00056 map_loc( vnl_vector<double> const& from,
00057          vnl_vector<double>      & to   ) const
00058 {
00059   assert ( from.size() == trans_.size() );
00060 
00061   to = (from-from_centre_) + trans_;
00062 }
00063 
00064 void
00065 rgrl_trans_translation::
00066 map_dir( vnl_vector<double> const& from_loc,
00067          vnl_vector<double> const& from_dir,
00068          vnl_vector<double>      & to_dir    ) const
00069 {
00070   assert ( from_loc.size() == trans_.size() );
00071   assert ( from_dir.size() == trans_.size() );
00072 
00073   to_dir = from_dir;
00074 }
00075 
00076 vnl_matrix<double>
00077 rgrl_trans_translation::
00078 transfer_error_covar( vnl_vector<double> const& p  ) const
00079 {
00080   assert ( is_covar_set() );
00081   assert ( p.size() == trans_.size() );
00082 
00083   return covar_;
00084 }
00085 
00086 vnl_vector<double>
00087 rgrl_trans_translation::
00088 t() const
00089 {
00090   return trans_ -  from_centre_;
00091 }
00092 
00093 void
00094 rgrl_trans_translation::
00095 inv_map( const vnl_vector<double>& to,
00096          bool initialize_next,
00097          const vnl_vector<double>& to_delta,
00098          vnl_vector<double>& from,
00099          vnl_vector<double>& from_next_est) const
00100 {
00101   const double epsilon = 0.01;
00102   vnl_vector<double> to_est = this->map_location(from);
00103 
00104   // update from to become true inv_map of to, based on (to - to_est)
00105   if (vnl_vector_ssd(to, to_est) > epsilon*epsilon) {
00106     from += to - to_est;
00107   }
00108 
00109   if ( initialize_next ) {
00110     from_next_est = from + to_delta;
00111   }
00112 }
00113 
00114 void
00115 rgrl_trans_translation::
00116 inv_map( const vnl_vector<double>& to,
00117          vnl_vector<double>& from ) const
00118 {
00119   from = to - trans_ + from_centre_;
00120 }
00121 
00122 rgrl_transformation_sptr
00123 rgrl_trans_translation::
00124 inverse_transform( ) const
00125 {
00126   rgrl_transformation_sptr result = new rgrl_trans_translation( -t() );
00127 
00128   const unsigned m = scaling_factors_.size();
00129   if ( m > 0 ) {
00130     vnl_vector<double> scaling( m );
00131     for ( unsigned int i=0; i<m; ++i )
00132       scaling[i] = 1.0 / scaling_factors_[i];
00133     result->set_scaling_factors( scaling );
00134   }
00135 
00136   return result;
00137 }
00138 
00139 
00140 void
00141 rgrl_trans_translation::
00142 jacobian_wrt_loc( vnl_matrix<double>& jac, vnl_vector<double> const& /*from_loc*/ ) const
00143 {
00144   unsigned int m = trans_.size();
00145   jac.set_size( m, m );
00146   jac.set_identity();
00147 }
00148 
00149 rgrl_transformation_sptr
00150 rgrl_trans_translation::
00151 scale_by( double scale ) const
00152 {
00153   rgrl_transformation_sptr xform
00154     = new rgrl_trans_translation( trans_ * scale,
00155                                    covar_, from_centre_ * scale,
00156                                    vnl_vector<double>(from_centre_.size(), 0.0) );
00157   xform->set_scaling_factors( this->scaling_factors() );
00158   return xform;
00159 }
00160 
00161 void
00162 rgrl_trans_translation::
00163 write( vcl_ostream& os ) const
00164 {
00165   // tag
00166   os << "TRANSLATION\n"
00167   // parameters
00168      << trans_.size() << vcl_endl
00169      << trans_ << ' ' << from_centre_ << vcl_endl;
00170 
00171   // parent
00172   rgrl_transformation::write( os );
00173 }
00174 
00175 bool
00176 rgrl_trans_translation::
00177 read( vcl_istream& is )
00178 {
00179   int dim;
00180 
00181   // skip empty lines
00182   rgrl_util_skip_empty_lines( is );
00183 
00184   vcl_string str;
00185   vcl_getline( is, str );
00186 
00187   // The token should appear at the beginning of line
00188   if ( str.find( "TRANSLATION" ) != 0 ) {
00189     WarningMacro( "The tag is not TRANSLATION. reading is aborted.\n" );
00190     return false;
00191   }
00192 
00193   // input global xform
00194   dim=-1;
00195   is >> dim;
00196   if ( dim > 0 ) {
00197     trans_.set_size( dim );
00198     from_centre_.set_size( dim );
00199     is >> trans_ >> from_centre_;
00200   }
00201 
00202   // parent
00203   return is.good() && rgrl_transformation::read( is );
00204 }
00205 
00206 //: make a clone copy
00207 rgrl_transformation_sptr
00208 rgrl_trans_translation::
00209 clone() const
00210 {
00211   return new rgrl_trans_translation( *this );
00212 }