contrib/mul/mfpf/mfpf_region_about_pt.cxx
Go to the documentation of this file.
00001 #include "mfpf_region_about_pt.h"
00002 //:
00003 // \file
00004 // \brief Region centred on a single point, pose defined by other pts
00005 // \author Tim Cootes
00006 
00007 #include <vsl/vsl_binary_loader.h>
00008 #include <vcl_cassert.h>
00009 #include <vcl_sstream.h>
00010 
00011 #include <mbl/mbl_parse_block.h>
00012 #include <mbl/mbl_read_props.h>
00013 
00014 #include <vul/vul_string.h>
00015 
00016 //=======================================================================
00017 // Dflt ctor
00018 //=======================================================================
00019 
00020 mfpf_region_about_pt::mfpf_region_about_pt()
00021   : i0_(0),i1_(0),i2_(1),rel_wi_(1.0),rel_wj_(1.0),form_("box")
00022 {
00023 }
00024 
00025 //=======================================================================
00026 // Destructor
00027 //=======================================================================
00028 
00029 mfpf_region_about_pt::~mfpf_region_about_pt()
00030 {
00031 }
00032 
00033 //: Returns true as the region is centred on an input point
00034 bool mfpf_region_about_pt::is_centred_on_pt() const
00035 {
00036   return true;
00037 }
00038 
00039 //: Returns index of reference point on which the region is centred
00040 unsigned mfpf_region_about_pt::ref_point_index() const
00041 {
00042   return i0_;
00043 }
00044 
00045 //: Returns original index of reference point on which the region is centred
00046 unsigned mfpf_region_about_pt::orig_ref_point_index() const
00047 {
00048   return i0_orig_;
00049 }
00050 
00051 //: Replace each point index i with new_index[i]
00052 //  Allows for re-numbering of the points used.
00053 //  Returns true if successful.
00054 bool mfpf_region_about_pt::replace_index(
00055                         const vcl_vector<unsigned>& new_index)
00056 {
00057   if (i0_>=new_index.size()) return false;
00058   if (new_index[i0_]==mfpf_invalid_index) return false;
00059   i0_=new_index[i0_];
00060   if (i1_>=new_index.size()) return false;
00061   if (new_index[i1_]==mfpf_invalid_index) return false;
00062   i1_=new_index[i1_];
00063   if (i2_>=new_index.size()) return false;
00064   if (new_index[i2_]==mfpf_invalid_index) return false;
00065   i2_=new_index[i2_];
00066   return true;
00067 }
00068 
00069 //: Returns reference point for region, pts[i0()]
00070 vgl_point_2d<double> mfpf_region_about_pt::get_ref_point(
00071             const vcl_vector<vgl_point_2d<double> >& pts) const
00072 {
00073   assert(i0_<pts.size());
00074   return pts[i0_];
00075 }
00076 
00077 //: Defines a region centred on a point
00078 mfpf_region_form mfpf_region_about_pt::set_up(
00079             const vcl_vector<vgl_point_2d<double> >& pts)
00080 {
00081   assert(i0_<pts.size());
00082   assert(i1_<pts.size());
00083   assert(i2_<pts.size());
00084 
00085   vgl_vector_2d<double> u=pts[i2_]-pts[i1_];
00086   double L=u.length();
00087   wi_=rel_wi_*L;
00088   wj_=rel_wj_*L;
00089 
00090   mfpf_pose pose(pts[i0_],u/L);
00091   return mfpf_region_form(pose,form_,wi_,wj_);
00092 }
00093 
00094 //: Defines a region centred on a point
00095 //  The aspect ratio of the region will be the same as that
00096 //  from the last call to set_up.
00097 mfpf_region_form mfpf_region_about_pt::get_region(
00098               const vcl_vector<vgl_point_2d<double> >& pts) const
00099 {
00100   assert(i0_<pts.size());
00101   assert(i1_<pts.size());
00102   assert(i2_<pts.size());
00103 
00104   vgl_vector_2d<double> u=pts[i2_]-pts[i1_];
00105   double L=u.length();
00106 
00107   // Compute scale of this box relative to the last one called by set_up
00108   double rel_scale = (rel_wi_*L)/wi_;
00109 
00110   mfpf_pose pose(pts[i0_],(rel_scale/L)*u);
00111   return mfpf_region_form(pose,form_,wi_,wj_);
00112 }
00113 
00114 //=======================================================================
00115 // Method: set_from_stream
00116 //=======================================================================
00117 //: Initialise from a string stream
00118 bool mfpf_region_about_pt::set_from_stream(vcl_istream &is)
00119 {
00120   // Cycle through stream and produce a map of properties
00121   vcl_string s = mbl_parse_block(is);
00122   vcl_istringstream ss(s);
00123   mbl_read_props_type props = mbl_read_props_ws(ss);
00124 
00125   // Extract the properties
00126   i0_=vul_string_atoi(props.get_required_property("i0"));
00127   i1_=vul_string_atoi(props.get_required_property("i1"));
00128   i2_=vul_string_atoi(props.get_required_property("i2"));
00129   rel_wi_=vul_string_atof(props.get_required_property("rel_wi"));
00130   rel_wj_=vul_string_atof(props.get_required_property("rel_wj"));
00131   form_=props.get_optional_property("form","box");
00132 
00133   // Check for unused props
00134   mbl_read_props_look_for_unused_props(
00135       "mfpf_region_about_pt::set_from_stream", props, mbl_read_props_type());
00136 
00137   // Store original index of i0 since this may be renumbered later
00138   i0_orig_=i0_;
00139 
00140   return true;
00141 }
00142 
00143 //=======================================================================
00144 // Method: version_no
00145 //=======================================================================
00146 
00147 short mfpf_region_about_pt::version_no() const
00148 {
00149   return 1;
00150 }
00151 
00152 //=======================================================================
00153 // Method: is_a
00154 //=======================================================================
00155 
00156 vcl_string mfpf_region_about_pt::is_a() const
00157 {
00158   return vcl_string("mfpf_region_about_pt");
00159 }
00160 
00161 //: Create a copy on the heap and return base class pointer
00162 mfpf_region_definer* mfpf_region_about_pt::clone() const
00163 {
00164   return new mfpf_region_about_pt(*this);
00165 }
00166 
00167 //=======================================================================
00168 // Method: print
00169 //=======================================================================
00170 
00171 void mfpf_region_about_pt::print_summary(vcl_ostream& os) const
00172 {
00173   os<<"{ i0: "<<i0_<<" i1: "<<i1_<<" i2: "<<i2_
00174     <<" rel_wi: "<<rel_wi_<<" rel_wj: "<<rel_wj_
00175     <<" form: "<<form_<<" } ";
00176 }
00177 
00178 //=======================================================================
00179 // Method: save
00180 //=======================================================================
00181 
00182 void mfpf_region_about_pt::b_write(vsl_b_ostream& bfs) const
00183 {
00184   vsl_b_write(bfs,version_no());
00185   vsl_b_write(bfs,i0_);
00186   vsl_b_write(bfs,i1_);
00187   vsl_b_write(bfs,i2_);
00188   vsl_b_write(bfs,rel_wi_);
00189   vsl_b_write(bfs,rel_wj_);
00190   vsl_b_write(bfs,wi_);
00191   vsl_b_write(bfs,wj_);
00192   vsl_b_write(bfs,form_);
00193 }
00194 
00195 //=======================================================================
00196 // Method: load
00197 //=======================================================================
00198 
00199 void mfpf_region_about_pt::b_read(vsl_b_istream& bfs)
00200 {
00201   if (!bfs) return;
00202   short version;
00203   vsl_b_read(bfs,version);
00204   switch (version)
00205   {
00206     case (1):
00207       vsl_b_read(bfs,i0_);
00208       vsl_b_read(bfs,i1_);
00209       vsl_b_read(bfs,i2_);
00210       vsl_b_read(bfs,rel_wi_);
00211       vsl_b_read(bfs,rel_wj_);
00212       vsl_b_read(bfs,wi_);
00213       vsl_b_read(bfs,wj_);
00214       vsl_b_read(bfs,form_);
00215       break;
00216     default:
00217       vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&)\n"
00218                << "           Unknown version number "<< version << vcl_endl;
00219       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00220       return;
00221   }
00222 }