1 #include <iostream>
2 #include <sstream>
3 #include "mfpf_patch_data.h"
4 //:
5 // \file
6 // \author Tim Cootes
7 // \brief Defines region size, shape, and form of model to use
8 
9 #include "vsl/vsl_indent.h"
10 #include "vsl/vsl_binary_loader.h"
11 
12 #include <mbl/mbl_parse_block.h>
13 #include <mbl/mbl_read_props.h>
14 #include <mbl/mbl_exception.h>
15 #include "vul/vul_string.h"
16 #ifdef _MSC_VER
17 #  include "vcl_msvc_warnings.h"
18 #endif
19 
20 //=======================================================================
21 // Dflt ctor
22 //=======================================================================
23 
24 mfpf_patch_data::mfpf_patch_data() = default;
25 
26 //=======================================================================
27 // Destructor
28 //=======================================================================
29 
30 mfpf_patch_data::~mfpf_patch_data() = default;
31 
32 //: Initialise from a string stream
set_from_stream(std::istream & is)33 bool mfpf_patch_data::set_from_stream(std::istream &is)
34 {
35   // Cycle through string and produce a map of properties
36   std::string s = mbl_parse_block(is);
37   std::istringstream ss(s);
38   mbl_read_props_type props = mbl_read_props_ws(ss);
39 
40   name_=props.get_required_property("name");
41   min_width_=vul_string_atoi(props.get_optional_property("min_width","7"));
42   max_width_=vul_string_atoi(props.get_optional_property("max_width","25"));
43 
44   std::string region_str = props.get_required_property("region");
45   {
46     std::istringstream iss(region_str);
47     definer_ = *mfpf_region_definer::create_from_stream(iss);
48   }
49 
50   std::string builder_str = props.get_required_property("builder");
51   {
52     std::istringstream iss(builder_str);
53     builder_ = *mfpf_point_finder_builder::create_from_stream(iss);
54   }
55 
56   // Check for unused props
57   mbl_read_props_look_for_unused_props(
58       "mfpf_patch_data::set_from_stream", props, mbl_read_props_type());
59   return true;
60 }
61 
62 
63 //=======================================================================
64 // Method: version_no
65 //=======================================================================
66 
version_no() const67 short mfpf_patch_data::version_no() const
68 {
69   return 1;
70 }
71 
72 
73 //=======================================================================
74 // Method: is_a
75 //=======================================================================
76 
is_a() const77 std::string mfpf_patch_data::is_a() const
78 {
79   return std::string("mfpf_patch_data");
80 }
81 
82 //: Print class to os
print_summary(std::ostream & os) const83 void mfpf_patch_data::print_summary(std::ostream& os) const
84 {
85   os<<" {\n";
86   vsl_indent_inc(os);
87   os<<vsl_indent()<<"name: "<<name_<<'\n'
88     <<vsl_indent()<<"min_width: "<<min_width_<<'\n'
89     <<vsl_indent()<<"max_width: "<<max_width_<<'\n'
90     <<vsl_indent()<<"region: "<<definer_<<'\n'
91     <<vsl_indent()<<"builder: "<<builder_<<'\n';
92   vsl_indent_dec(os);
93   os<<"} ";
94 }
95 
96 //=======================================================================
97 // Method: save
98 //=======================================================================
99 
b_write(vsl_b_ostream & bfs) const100 void mfpf_patch_data::b_write(vsl_b_ostream& bfs) const
101 {
102   vsl_b_write(bfs,version_no());
103   vsl_b_write(bfs,name_);
104   vsl_b_write(bfs,min_width_);
105   vsl_b_write(bfs,max_width_);
106   vsl_b_write(bfs,definer_);
107   vsl_b_write(bfs,builder_);
108 }
109 
110 //=======================================================================
111 // Method: load
112 //=======================================================================
113 
b_read(vsl_b_istream & bfs)114 void mfpf_patch_data::b_read(vsl_b_istream& bfs)
115 {
116   if (!bfs) return;
117   short version;
118   vsl_b_read(bfs,version);
119   switch (version)
120   {
121     case (1):
122       vsl_b_read(bfs,name_);
123       vsl_b_read(bfs,min_width_);
124       vsl_b_read(bfs,max_width_);
125       vsl_b_read(bfs,definer_);
126       vsl_b_read(bfs,builder_);
127       break;
128     default:
129       std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&)\n"
130                << "           Unknown version number "<< version << '\n';
131       bfs.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
132       return;
133   }
134 }
135 
136 //=======================================================================
137 // Associated function: operator<<
138 //=======================================================================
139 
operator <<(std::ostream & os,const mfpf_patch_data & b)140 std::ostream& operator<<(std::ostream& os,const mfpf_patch_data& b)
141 {
142   os << b.is_a() << ": ";
143   vsl_indent_inc(os);
144   b.print_summary(os);
145   vsl_indent_dec(os);
146   return os;
147 }
148 
149   //: Binary file stream output operator for class reference
vsl_b_write(vsl_b_ostream & bfs,const mfpf_patch_data & b)150 void vsl_b_write(vsl_b_ostream& bfs, const mfpf_patch_data& b)
151 {
152   b.b_write(bfs);
153 }
154 
155   //: Binary file stream input operator for class reference
vsl_b_read(vsl_b_istream & bfs,mfpf_patch_data & b)156 void vsl_b_read(vsl_b_istream& bfs, mfpf_patch_data& b)
157 {
158   b.b_read(bfs);
159 }
160 
161 // =====================================================
162 // ======= Functions for sets of mfpf_patch_data =======
163 // =====================================================
164 
165 //: Reads in a list of mfpf_patch_data objects from a text stream
166 //  Format: "{ region: { ... } region: { ... } .... }"
mfpf_read_from_stream(std::istream & is,std::vector<mfpf_patch_data> & data)167 void mfpf_read_from_stream(std::istream &is,
168                            std::vector<mfpf_patch_data>& data)
169 {
170   std::string s = mbl_parse_block(is);
171   std::istringstream ss(s);
172   char c;
173   ss>>c;  // Remove opening brace
174   if (c!='{')
175   {
176     throw mbl_exception_parse_error("Expected '{' in ggr_parse_list");
177   }
178   data.resize(0);
179 
180   std::string label;
181   while (!ss.eof())
182   {
183     ss >> label;         // Next follows the parameters
184     if (label == "}") continue;
185     if (label!="region:")
186     {
187       std::string error_msg = "Expected keyword: region:";
188       error_msg+=" Got '"+label+"'";
189       throw mbl_exception_parse_error(error_msg);
190     }
191 
192     mfpf_patch_data patch;
193     std::istringstream ss2(mbl_parse_block(ss));
194     patch.set_from_stream(ss2);
195     data.push_back(patch);
196   }
197 }
198