contrib/mul/mfpf/mfpf_point_finder_builder.cxx
Go to the documentation of this file.
00001 #include "mfpf_point_finder_builder.h"
00002 //:
00003 // \file
00004 // \brief Base for classes which build mfpf_point_finder objects.
00005 // \author Tim Cootes
00006 
00007 #include <vsl/vsl_indent.h>
00008 #include <vsl/vsl_binary_loader.h>
00009 
00010 #include <mbl/mbl_parse_block.h>
00011 #include <mbl/mbl_read_props.h>
00012 #include <mbl/mbl_cloneables_factory.h>
00013 #include <vcl_cmath.h>
00014 #include <vcl_sstream.h>
00015 
00016 #include <mfpf/mfpf_point_finder.h>
00017 #include <vul/vul_string.h>
00018 
00019 //=======================================================================
00020 // Dflt ctor
00021 //=======================================================================
00022 
00023 mfpf_point_finder_builder::mfpf_point_finder_builder()
00024   : step_size_(1.0),
00025     search_ni_(5),search_nj_(0),
00026     search_nA_(0),search_dA_(0.0),search_ns_(0),search_ds_(1.0)
00027 {
00028 }
00029 
00030 //=======================================================================
00031 // Destructor
00032 //=======================================================================
00033 
00034 mfpf_point_finder_builder::~mfpf_point_finder_builder()
00035 {
00036 }
00037 
00038 //: Size of step between sample points
00039 void mfpf_point_finder_builder::set_step_size(double s)
00040 {
00041   step_size_=s;
00042 }
00043 
00044 //: Define search region size
00045 //  During search, samples at points on grid [-ni,ni]x[-nj,nj],
00046 //  with axes defined by u.
00047 void mfpf_point_finder_builder::set_search_area(unsigned ni, unsigned nj)
00048 {
00049   search_ni_=ni;
00050   search_nj_=nj;
00051 }
00052 
00053 //: Define angle search parameters
00054 void mfpf_point_finder_builder::set_search_angle_range(unsigned nA, double dA)
00055 {
00056   search_nA_=nA;
00057   search_dA_=dA;
00058 }
00059 
00060 //: Define scale search parameters
00061 void mfpf_point_finder_builder::set_search_scale_range(unsigned ns, double ds)
00062 {
00063   search_ns_=ns;
00064   search_ds_=ds;
00065 }
00066 
00067 //: Number of dimensions in the model
00068 unsigned mfpf_point_finder_builder::model_dim()
00069 {
00070   return 0;
00071 }
00072 
00073 //: Get sample of region around specified point in image
00074 void mfpf_point_finder_builder::get_sample_vector(const vimt_image_2d_of<float>& image,
00075                                                   const vgl_point_2d<double>& p,
00076                                                   const vgl_vector_2d<double>& u,
00077                                                   vcl_vector<double>& v)
00078 {
00079   // Return empty vector
00080   v = vcl_vector<double>();
00081 }
00082 
00083 
00084 //: Parse relevant parameters from props list
00085 void mfpf_point_finder_builder::parse_base_props(mbl_read_props_type& props)
00086 {
00087   if (props.find("step_size")!=props.end())
00088   {
00089     step_size_=vul_string_atof(props["step_size"]);
00090     props.erase("step_size");
00091   }
00092   if (props.find("search_ni")!=props.end())
00093   {
00094     search_ni_=vul_string_atoi(props["search_ni"]);
00095     props.erase("search_ni");
00096   }
00097   if (props.find("search_nj")!=props.end())
00098   {
00099     search_nj_=vul_string_atoi(props["search_nj"]);
00100     props.erase("search_nj");
00101   }
00102   if (props.find("search_nA")!=props.end())
00103   {
00104     search_nA_=vul_string_atoi(props["search_nA"]);
00105     props.erase("search_nA");
00106   }
00107   if (props.find("search_ns")!=props.end())
00108   {
00109     search_ns_=vul_string_atoi(props["search_ns"]);
00110     props.erase("search_ns");
00111   }
00112   if (props.find("search_dA")!=props.end())
00113   {
00114     search_dA_=vul_string_atof(props["search_dA"]);
00115     props.erase("search_dA");
00116   }
00117   if (props.find("search_ds")!=props.end())
00118   {
00119     search_ds_=vul_string_atof(props["search_ds"]);
00120     props.erase("search_ds");
00121   }
00122 }
00123 
00124 
00125 //: Set base-class parameters of point finder
00126 void mfpf_point_finder_builder::set_base_parameters(mfpf_point_finder& pf)
00127 {
00128   pf.set_step_size(step_size_);
00129   pf.set_search_area(search_ni_,search_nj_);
00130   pf.set_angle_range(search_nA_,search_dA_);
00131   pf.set_scale_range(search_ns_,search_ds_);
00132 }
00133 
00134 //: Initialise from a string stream
00135 bool mfpf_point_finder_builder::set_from_stream(vcl_istream &is)
00136 {
00137   // Cycle through string and produce a map of properties
00138   vcl_string s = mbl_parse_block(is);
00139   vcl_istringstream ss(s);
00140   mbl_read_props_type props = mbl_read_props_ws(ss);
00141 
00142   if (props.size()!=0)
00143   {
00144     vcl_cerr<<is_a()<<" does not expect any extra arguments.\n";
00145     mbl_read_props_look_for_unused_props(
00146       "mfpf_point_finder_builder::set_from_stream", props, mbl_read_props_type());
00147   }
00148   return true;
00149 }
00150 
00151 //: Create a concrete object, from a text specification.
00152 vcl_auto_ptr<mfpf_point_finder_builder> mfpf_point_finder_builder::
00153   create_from_stream(vcl_istream &is)
00154 {
00155   vcl_string name;
00156   is >> name;
00157   vcl_auto_ptr<mfpf_point_finder_builder> opt;
00158   try {
00159     opt = mbl_cloneables_factory<mfpf_point_finder_builder>::get_clone(name);
00160   }
00161   catch (const mbl_exception_no_name_in_factory & e)
00162   {
00163     throw (mbl_exception_parse_error( e.what() ));
00164   }
00165   opt->set_from_stream(is);
00166   return opt;
00167 }
00168 
00169 
00170 //=======================================================================
00171 // Method: version_no
00172 //=======================================================================
00173 
00174 short mfpf_point_finder_builder::version_no() const
00175 {
00176   return 1;
00177 }
00178 
00179 //=======================================================================
00180 // Method: is_a
00181 //=======================================================================
00182 
00183 vcl_string mfpf_point_finder_builder::is_a() const
00184 {
00185   return vcl_string("mfpf_point_finder_builder");
00186 }
00187 
00188 //: Return true if base class parameters are the same in pf
00189 bool mfpf_point_finder_builder::base_equality(const mfpf_point_finder_builder& pf) const
00190 {
00191   if (search_ni_!=pf.search_ni_) return false;
00192   if (search_nj_!=pf.search_nj_) return false;
00193   if (search_nA_!=pf.search_nA_) return false;
00194   if (search_ns_!=pf.search_ns_) return false;
00195   if (vcl_fabs(search_dA_-pf.search_dA_)>1e-6) return false;
00196   if (vcl_fabs(search_ds_-pf.search_ds_)>1e-6) return false;
00197   if (vcl_fabs(step_size_-pf.step_size_)>1e-6) return false;
00198   return true;
00199 }
00200 
00201 //=======================================================================
00202 // Method: print
00203 //=======================================================================
00204 
00205 void mfpf_point_finder_builder::print_summary(vcl_ostream& os) const
00206 {
00207   os<<" step_size: "<<step_size_
00208     <<" search: { ni: "<<search_ni_
00209     <<" nj: "<<search_nj_
00210     <<" nA: "<<search_nA_<<" dA: "<<search_dA_
00211     <<" ns: "<<search_ns_<<" ds: "<<search_ds_<<" } ";
00212 }
00213 
00214 //=======================================================================
00215 // Binary I/O
00216 //=======================================================================
00217 
00218 void mfpf_point_finder_builder::b_write(vsl_b_ostream& bfs) const
00219 {
00220   vsl_b_write(bfs,version_no());
00221   vsl_b_write(bfs,step_size_);
00222   vsl_b_write(bfs,search_ni_);
00223   vsl_b_write(bfs,search_nj_);
00224   vsl_b_write(bfs,search_nA_);
00225   vsl_b_write(bfs,search_dA_);
00226   vsl_b_write(bfs,search_ns_);
00227   vsl_b_write(bfs,search_ds_);
00228 }
00229 
00230 //=======================================================================
00231 // Method: load
00232 //=======================================================================
00233 
00234 void mfpf_point_finder_builder::b_read(vsl_b_istream& bfs)
00235 {
00236   if (!bfs) return;
00237   short version;
00238   vsl_b_read(bfs,version);
00239   switch (version)
00240   {
00241     case 1:
00242       vsl_b_read(bfs,step_size_);
00243       vsl_b_read(bfs,search_ni_);
00244       vsl_b_read(bfs,search_nj_);
00245       vsl_b_read(bfs,search_nA_);
00246       vsl_b_read(bfs,search_dA_);
00247       vsl_b_read(bfs,search_ns_);
00248       vsl_b_read(bfs,search_ds_);
00249       break;
00250     default:
00251       vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&)\n"
00252                << "           Unknown version number "<< version << '\n';
00253       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00254       return;
00255   }
00256 }
00257 
00258 //: Allows derived class to be loaded by base-class pointer
00259 void vsl_add_to_binary_loader(const mfpf_point_finder_builder& b)
00260 {
00261   vsl_binary_loader<mfpf_point_finder_builder>::instance().add(b);
00262 }
00263 
00264 //=======================================================================
00265 // Associated function: operator<<
00266 //=======================================================================
00267 
00268 void vsl_b_write(vsl_b_ostream& bfs, const mfpf_point_finder_builder& b)
00269 {
00270   b.b_write(bfs);
00271 }
00272 
00273 //=======================================================================
00274 // Associated function: operator>>
00275 //=======================================================================
00276 
00277 void vsl_b_read(vsl_b_istream& bfs, mfpf_point_finder_builder& b)
00278 {
00279   b.b_read(bfs);
00280 }
00281 
00282 //=======================================================================
00283 // Associated function: operator<<
00284 //=======================================================================
00285 
00286 vcl_ostream& operator<<(vcl_ostream& os,const mfpf_point_finder_builder& b)
00287 {
00288   os << b.is_a() << ": ";
00289   vsl_indent_inc(os);
00290   b.print_summary(os);
00291   vsl_indent_dec(os);
00292   return os;
00293 }
00294 
00295 //=======================================================================
00296 // Associated function: operator<<
00297 //=======================================================================
00298 
00299 vcl_ostream& operator<<(vcl_ostream& os,const mfpf_point_finder_builder* b)
00300 {
00301   if (b)
00302     return os << *b;
00303   else
00304     return os << "No mfpf_point_finder_builder defined.";
00305 }