contrib/mul/mmn/mmn_solver.cxx
Go to the documentation of this file.
00001 #include "mmn_solver.h"
00002 //:
00003 // \file
00004 // \brief Base for classes which solve Markov Random Field problems
00005 // \author Tim Cootes
00006 
00007 #include <vsl/vsl_indent.h>
00008 #include <vsl/vsl_binary_loader.h>
00009 #include <vcl_sstream.h>
00010 #include <vcl_cassert.h>
00011 
00012 #include <mbl/mbl_parse_block.h>
00013 #include <mbl/mbl_read_props.h>
00014 #include <mbl/mbl_cloneables_factory.h>
00015 
00016 //=======================================================================
00017 // Dflt ctor
00018 //=======================================================================
00019 
00020 mmn_solver::mmn_solver()
00021 {
00022 }
00023 
00024 //=======================================================================
00025 // Destructor
00026 //=======================================================================
00027 
00028 mmn_solver::~mmn_solver()
00029 {
00030 }
00031 
00032 
00033 //: Initialise from a string stream
00034 bool mmn_solver::set_from_stream(vcl_istream &is)
00035 {
00036   // Cycle through string and produce a map of properties
00037   vcl_string s = mbl_parse_block(is);
00038   vcl_istringstream ss(s);
00039   mbl_read_props_type props = mbl_read_props_ws(ss);
00040 
00041   if (props.size()!=0)
00042   {
00043     vcl_cerr<<is_a()<<" does not expect any extra arguments.\n";
00044     mbl_read_props_look_for_unused_props(
00045       "mmn_solver::set_from_stream", props, mbl_read_props_type());
00046   }
00047   return true;
00048 }
00049 
00050 //=======================================================================
00051 // Method: version_no
00052 //=======================================================================
00053 
00054 short mmn_solver::version_no() const
00055 {
00056   return 1;
00057 }
00058 
00059 //=======================================================================
00060 // Method: is_a
00061 //=======================================================================
00062 
00063 vcl_string mmn_solver::is_a() const
00064 {
00065   return vcl_string("mmn_solver");
00066 }
00067 
00068 //: Allows derived class to be loaded by base-class pointer
00069 void vsl_add_to_binary_loader(const mmn_solver& b)
00070 {
00071   vsl_binary_loader<mmn_solver>::instance().add(b);
00072 }
00073 
00074 //: Create a concrete region_model-derived object, from a text specification.
00075 vcl_auto_ptr<mmn_solver> mmn_solver::
00076   create_from_stream(vcl_istream &is)
00077 {
00078   vcl_string name;
00079   is >> name;
00080   vcl_auto_ptr<mmn_solver> pair_cost;
00081   try {
00082     pair_cost = mbl_cloneables_factory<mmn_solver>::get_clone(name);
00083   }
00084   catch (const mbl_exception_no_name_in_factory & e)
00085   {
00086     throw (mbl_exception_parse_error( e.what() ));
00087   }
00088   pair_cost->set_from_stream(is);
00089   return pair_cost;
00090 }
00091 
00092 //=======================================================================
00093 // Associated function: operator<<
00094 //=======================================================================
00095 
00096 void vsl_b_write(vsl_b_ostream& bfs, const mmn_solver& b)
00097 {
00098   b.b_write(bfs);
00099 }
00100 
00101 //=======================================================================
00102 // Associated function: operator>>
00103 //=======================================================================
00104 
00105 void vsl_b_read(vsl_b_istream& bfs, mmn_solver& b)
00106 {
00107   b.b_read(bfs);
00108 }
00109 
00110 //=======================================================================
00111 // Associated function: operator<<
00112 //=======================================================================
00113 
00114 vcl_ostream& operator<<(vcl_ostream& os,const mmn_solver& b)
00115 {
00116   os << b.is_a() << ": ";
00117   vsl_indent_inc(os);
00118   b.print_summary(os);
00119   vsl_indent_dec(os);
00120   return os;
00121 }
00122 
00123 //=======================================================================
00124 // Associated function: operator<<
00125 //=======================================================================
00126 
00127 vcl_ostream& operator<<(vcl_ostream& os,const mmn_solver* b)
00128 {
00129   if (b)
00130     return os << *b;
00131   else
00132     return os << "No mmn_solver defined.";
00133 }