00001 #ifndef rgrl_est_proj_func_h_
00002 #define rgrl_est_proj_func_h_
00003
00004
00005
00006
00007
00008
00009
00010 #include <vnl/vnl_least_squares_function.h>
00011 #include <vnl/vnl_vector_fixed.h>
00012 #include <rgrl/rgrl_fwd.h>
00013 #include <rgrl/rgrl_match_set_sptr.h>
00014
00015
00016 template <unsigned int Tdim, unsigned int Fdim>
00017 inline
00018 void
00019 rgrl_est_proj_map_homo_point( vnl_vector_fixed<double, Tdim+1>& mapped,
00020 vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00021 vnl_vector_fixed<double, Fdim> const& loc )
00022 {
00023 for ( unsigned i=0; i<Tdim+1; ++i ) {
00024
00025 mapped[i] = proj(i, Fdim);
00026
00027 for ( unsigned j=0; j<Fdim; ++j )
00028 mapped[i] += loc[j] * proj(i,j);
00029 }
00030 }
00031
00032
00033 template <unsigned int Tdim, unsigned int Fdim>
00034 inline
00035 void
00036 rgrl_est_proj_map_inhomo_point( vnl_vector_fixed<double, Tdim>& mapped,
00037 vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00038 vnl_vector_fixed<double, Fdim> const& loc )
00039 {
00040 vnl_vector_fixed<double, Tdim+1> tmp;
00041
00042
00043 for ( unsigned i=0; i<Tdim+1; ++i ) {
00044
00045 tmp[i] = proj(i, Fdim);
00046
00047 for ( unsigned j=0; j<Fdim; ++j )
00048 tmp[i] += loc[j] * proj(i,j);
00049 }
00050
00051
00052 for ( unsigned i=0; i<Tdim; ++i )
00053 mapped[i] = tmp[i]/tmp[Tdim];
00054 }
00055
00056
00057 template <unsigned int Tdim, unsigned int Fdim>
00058 class rgrl_est_proj_func
00059 : public vnl_least_squares_function
00060 {
00061 public:
00062
00063 rgrl_est_proj_func( rgrl_set_of<rgrl_match_set_sptr> const& matches,
00064 bool with_grad = true );
00065
00066
00067 rgrl_est_proj_func( bool with_grad = true );
00068
00069 void set_centres( vnl_vector_fixed<double, Fdim> const& fc,
00070 vnl_vector_fixed<double, Tdim> const& tc )
00071 {
00072 from_centre_ = fc;
00073 to_centre_ = tc;
00074 }
00075
00076
00077 void set_max_num_iter( int max )
00078 { max_num_iterations_ = max; }
00079
00080
00081 int max_num_iter() const
00082 { return max_num_iterations_; }
00083
00084
00085 void set_rel_thres( double thres )
00086 { relative_threshold_ = thres; }
00087
00088
00089 double rel_thres() const
00090 { return relative_threshold_; }
00091
00092
00093 void set_zero_svd_thres( double thres )
00094 { zero_svd_thres_ = thres; }
00095
00096
00097 double zero_svd_thres() const
00098 { return zero_svd_thres_; }
00099
00100
00101 bool
00102 projective_estimate( vnl_matrix_fixed<double, Tdim+1, Fdim+1>& proj,
00103 vnl_matrix<double>& full_covar,
00104 vnl_vector_fixed<double, Fdim>& from_centre,
00105 vnl_vector_fixed<double, Tdim>& to_centre );
00106
00107
00108 void f(vnl_vector<double> const& x, vnl_vector<double>& fx);
00109
00110
00111 void gradf(vnl_vector<double> const& x, vnl_matrix<double>& jacobian);
00112
00113
00114 vnl_matrix_fixed<double, Tdim+1, Fdim+1>
00115 uncentre_proj( vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj ) const;
00116
00117
00118 inline
00119 void
00120 map_loc( vnl_vector_fixed<double, Tdim>& mapped,
00121 vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00122 vnl_vector_fixed<double, Fdim> const& from ) const
00123 {
00124 rgrl_est_proj_map_inhomo_point<Tdim, Fdim>( mapped, proj, from-from_centre_ );
00125 mapped += to_centre_;
00126 }
00127
00128 protected:
00129
00130 void
00131 reduced_proj_jacobian( vnl_matrix_fixed<double, Tdim, (Fdim+1)*(Tdim+1)-1>& base_jac,
00132 vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00133 vnl_vector_fixed<double, Fdim> const& from ) const;
00134
00135
00136 void
00137 full_proj_jacobian( vnl_matrix_fixed<double, Tdim, (Fdim+1)*(Tdim+1)>& base_jac,
00138 vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00139 vnl_vector_fixed<double, Fdim> const& from ) const;
00140
00141
00142 void
00143 proj_jac_wrt_loc( vnl_matrix_fixed<double, Tdim, Fdim> & jac_loc,
00144 vnl_matrix_fixed<double, Tdim+1, Fdim+1> const& proj,
00145 vnl_vector_fixed<double, Fdim> const& from ) const;
00146
00147
00148 void convert_parameters( vnl_vector<double>& params,
00149 vnl_matrix_fixed<double, Tdim+1, Fdim+1> proj_matrix,
00150 vnl_vector_fixed<double, Fdim> const& fc,
00151 vnl_vector_fixed<double, Tdim> const& tc );
00152
00153 void restored_centered_proj( vnl_matrix_fixed<double, Tdim+1, Fdim+1>& proj_matrix,
00154 vnl_vector<double> const& params ) const;
00155
00156 protected:
00157 static const unsigned int proj_size_ = (Tdim+1)*(Fdim+1);
00158
00159 rgrl_set_of<rgrl_match_set_sptr> const* matches_ptr_;
00160 vnl_vector_fixed<double, Fdim> from_centre_;
00161 vnl_vector_fixed<double, Tdim> to_centre_;
00162
00163 unsigned int index_row_;
00164 unsigned int index_col_;
00165
00166 int max_num_iterations_;
00167
00168
00169 double relative_threshold_;
00170
00171
00172 double zero_svd_thres_;
00173 };
00174
00175 #if 0
00176
00177
00178
00179 template <unsigned int Tdim, unsigned int Fdim>
00180 class rgrl_est_proj_lm
00181 : public rgrl_nonlinear_estimator
00182 {
00183 public:
00184
00185 rgrl_est_proj_lm( bool with_grad = true );
00186
00187
00188
00189 rgrl_type_macro( rgrl_est_proj_lm, rgrl_nonlinear_estimator );
00190
00191 protected:
00192
00193 bool with_grad_;
00194
00195
00196 };
00197 #endif // 0
00198
00199 #endif //rgrl_est_proj_func_h_