contrib/mul/mfpf/mfpf_patch_data.cxx
Go to the documentation of this file.
00001 #include "mfpf_patch_data.h"
00002 //:
00003 // \file
00004 // \author Tim Cootes
00005 // \brief Defines region size, shape, and form of model to use
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_exception.h>
00013 #include <vul/vul_string.h>
00014 #include <vcl_sstream.h>
00015 
00016 //=======================================================================
00017 // Dflt ctor
00018 //=======================================================================
00019 
00020 mfpf_patch_data::mfpf_patch_data()
00021 {
00022 }
00023 
00024 //=======================================================================
00025 // Destructor
00026 //=======================================================================
00027 
00028 mfpf_patch_data::~mfpf_patch_data()
00029 {
00030 }
00031 
00032 //: Initialise from a string stream
00033 bool mfpf_patch_data::set_from_stream(vcl_istream &is)
00034 {
00035   // Cycle through string and produce a map of properties
00036   vcl_string s = mbl_parse_block(is);
00037   vcl_istringstream ss(s);
00038   mbl_read_props_type props = mbl_read_props_ws(ss);
00039 
00040   name_=props.get_required_property("name");
00041   min_width_=vul_string_atoi(props.get_optional_property("min_width","7"));
00042   max_width_=vul_string_atoi(props.get_optional_property("max_width","25"));
00043 
00044   vcl_string region_str = props.get_required_property("region");
00045   {
00046     vcl_istringstream iss(region_str);
00047     definer_ = *mfpf_region_definer::create_from_stream(iss);
00048   }
00049 
00050   vcl_string builder_str = props.get_required_property("builder");
00051   {
00052     vcl_istringstream iss(builder_str);
00053     builder_ = *mfpf_point_finder_builder::create_from_stream(iss);
00054   }
00055 
00056   // Check for unused props
00057   mbl_read_props_look_for_unused_props(
00058       "mfpf_patch_data::set_from_stream", props, mbl_read_props_type());
00059   return true;
00060 }
00061 
00062 
00063 //=======================================================================
00064 // Method: version_no
00065 //=======================================================================
00066 
00067 short mfpf_patch_data::version_no() const
00068 {
00069   return 1;
00070 }
00071 
00072 
00073 //=======================================================================
00074 // Method: is_a
00075 //=======================================================================
00076 
00077 vcl_string mfpf_patch_data::is_a() const
00078 {
00079   return vcl_string("mfpf_patch_data");
00080 }
00081 
00082 //: Print class to os
00083 void mfpf_patch_data::print_summary(vcl_ostream& os) const
00084 {
00085   os<<" {\n";
00086   vsl_indent_inc(os);
00087   os<<vsl_indent()<<"name: "<<name_<<'\n'
00088     <<vsl_indent()<<"min_width: "<<min_width_<<'\n'
00089     <<vsl_indent()<<"max_width: "<<max_width_<<'\n'
00090     <<vsl_indent()<<"region: "<<definer_<<'\n'
00091     <<vsl_indent()<<"builder: "<<builder_<<'\n';
00092   vsl_indent_dec(os);
00093   os<<"} ";
00094 }
00095 
00096 //=======================================================================
00097 // Method: save
00098 //=======================================================================
00099 
00100 void mfpf_patch_data::b_write(vsl_b_ostream& bfs) const
00101 {
00102   vsl_b_write(bfs,version_no());
00103   vsl_b_write(bfs,name_);
00104   vsl_b_write(bfs,min_width_);
00105   vsl_b_write(bfs,max_width_);
00106   vsl_b_write(bfs,definer_);
00107   vsl_b_write(bfs,builder_);
00108 }
00109 
00110 //=======================================================================
00111 // Method: load
00112 //=======================================================================
00113 
00114 void mfpf_patch_data::b_read(vsl_b_istream& bfs)
00115 {
00116   if (!bfs) return;
00117   short version;
00118   vsl_b_read(bfs,version);
00119   switch (version)
00120   {
00121     case (1):
00122       vsl_b_read(bfs,name_);
00123       vsl_b_read(bfs,min_width_);
00124       vsl_b_read(bfs,max_width_);
00125       vsl_b_read(bfs,definer_);
00126       vsl_b_read(bfs,builder_);
00127       break;
00128     default:
00129       vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&)\n"
00130                << "           Unknown version number "<< version << '\n';
00131       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00132       return;
00133   }
00134 }
00135 
00136 //=======================================================================
00137 // Associated function: operator<<
00138 //=======================================================================
00139 
00140 vcl_ostream& operator<<(vcl_ostream& os,const mfpf_patch_data& b)
00141 {
00142   os << b.is_a() << ": ";
00143   vsl_indent_inc(os);
00144   b.print_summary(os);
00145   vsl_indent_dec(os);
00146   return os;
00147 }
00148 
00149   //: Binary file stream output operator for class reference
00150 void vsl_b_write(vsl_b_ostream& bfs, const mfpf_patch_data& b)
00151 {
00152   b.b_write(bfs);
00153 }
00154 
00155   //: Binary file stream input operator for class reference
00156 void vsl_b_read(vsl_b_istream& bfs, mfpf_patch_data& b)
00157 {
00158   b.b_read(bfs);
00159 }
00160 
00161 // =====================================================
00162 // ======= Functions for sets of mfpf_patch_data =======
00163 // =====================================================
00164 
00165 //: Reads in a list of mfpf_patch_data objects from a text stream
00166 //  Format: "{ region: { ... } region: { ... } .... }"
00167 void mfpf_read_from_stream(vcl_istream &is,
00168                            vcl_vector<mfpf_patch_data>& data)
00169 {
00170   vcl_string s = mbl_parse_block(is);
00171   vcl_istringstream ss(s);
00172   char c;
00173   ss>>c;  // Remove opening brace
00174   if (c!='{')
00175   {
00176     throw mbl_exception_parse_error("Expected '{' in ggr_parse_list");
00177   }
00178   data.resize(0);
00179 
00180   vcl_string label;
00181   while (!ss.eof())
00182   {
00183     ss >> label;         // Next follows the parameters
00184     if (label == "}") continue;
00185     if (label!="region:")
00186     {
00187       vcl_string error_msg = "Expected keyword: region:";
00188       error_msg+=" Got '"+label+"'";
00189       throw mbl_exception_parse_error(error_msg);
00190     }
00191 
00192     mfpf_patch_data patch;
00193     vcl_istringstream ss2(mbl_parse_block(ss));
00194     patch.set_from_stream(ss2);
00195     data.push_back(patch);
00196   }
00197 }
00198