contrib/mul/msm/msm_aligner.cxx
Go to the documentation of this file.
00001 #include "msm_aligner.h"
00002 //:
00003 // \file
00004 // \author Tim Cootes
00005 // \brief Base for functions which calculate and apply 2D transformations
00006 
00007 #include <vsl/vsl_indent.h>
00008 #include <vsl/vsl_binary_loader.h>
00009 #include <mbl/mbl_cloneables_factory.h>
00010 #include <mbl/mbl_parse_block.h>
00011 #include <mbl/mbl_exception.h>
00012 
00013 //: Compute mean of points after transforming with pose
00014 void msm_aligner::mean_of_transformed(
00015                          const vcl_vector<msm_points>& points,
00016                          const vcl_vector<vnl_vector<double> >& pose,
00017                          msm_points& mean) const
00018 {
00019   mean.vector().set_size(points[0].vector().size());
00020   mean.vector().fill(0.0);
00021   msm_points posed_points;
00022   for (unsigned i=0;i<points.size();++i)
00023   {
00024     apply_transform(points[i],pose[i],posed_points);
00025     mean.vector()+=posed_points.vector();
00026   }
00027   mean.vector()/=points.size();
00028 }
00029 
00030 //: Print class to os
00031 void msm_aligner::print_summary(vcl_ostream& os) const
00032 {
00033   os<<" { } ";
00034 }
00035 
00036 const static short version_no = 1;
00037 
00038 //: Save class to binary file stream
00039 void msm_aligner::b_write(vsl_b_ostream& bfs) const
00040 {
00041   vsl_b_write(bfs,version_no);
00042 }
00043 
00044 
00045 //: Load class from binary file stream
00046 void msm_aligner::b_read(vsl_b_istream& bfs)
00047 {
00048   short version;
00049   vsl_b_read(bfs,version);
00050 }
00051 
00052 //=======================================================================
00053 
00054 void vsl_add_to_binary_loader(const msm_aligner& b)
00055 {
00056   vsl_binary_loader<msm_aligner>::instance().add(b);
00057 }
00058 
00059 //=======================================================================
00060 
00061 void vsl_b_write(vsl_b_ostream& bfs, const msm_aligner& b)
00062 {
00063   b.b_write(bfs);
00064 }
00065 
00066 //=======================================================================
00067 //: Initialise from a text stream.
00068 // The default implementation is for attribute-less normalisers,
00069 // and throws if it finds any data in the stream.
00070 void msm_aligner::config_from_stream(vcl_istream &is)
00071 {
00072   vcl_string s = mbl_parse_block(is);
00073   if (s.empty() || s=="{}") return;
00074 
00075   mbl_exception_parse_error x(
00076     this->is_a() + " expects no properties in initialisation,\n"
00077     "But the following properties were given:\n" + s);
00078   mbl_exception_error(x);
00079 }
00080 
00081 
00082 //=======================================================================
00083 //: Create a concrete msm_aligner-derived object, from a text specification.
00084 vcl_auto_ptr<msm_aligner> msm_aligner::create_from_stream(vcl_istream &is)
00085 {
00086   vcl_string name;
00087   is >> name;
00088 
00089   vcl_auto_ptr<msm_aligner> ps =
00090     mbl_cloneables_factory<msm_aligner>::get_clone(name);
00091 
00092   ps -> config_from_stream(is);
00093   return ps;
00094 }
00095 
00096 //=======================================================================
00097 
00098 void vsl_b_read(vsl_b_istream& bfs, msm_aligner& b)
00099 {
00100   b.b_read(bfs);
00101 }
00102 
00103 //=======================================================================
00104 
00105 vcl_ostream& operator<<(vcl_ostream& os,const msm_aligner& b)
00106 {
00107   os << b.is_a() << ": ";
00108   vsl_indent_inc(os);
00109   b.print_summary(os);
00110   vsl_indent_dec(os);
00111   return os;
00112 }
00113 
00114 //=======================================================================
00115 
00116 vcl_ostream& operator<<(vcl_ostream& os,const msm_aligner* b)
00117 {
00118   if (b)
00119     return os << *b;
00120   else
00121     return os << "No msm_aligner defined.";
00122 }
00123 
00124 //=======================================================================
00125 //: Stream output operator for class reference
00126 void vsl_print_summary(vcl_ostream& os,const msm_aligner& b)
00127 {
00128   os << b;
00129 }
00130 
00131 //=======================================================================
00132 //: Stream output operator for class reference
00133 void vsl_print_summary(vcl_ostream& os,const msm_aligner* b)
00134 {
00135   if (b)
00136     os << *b;
00137   else
00138     os << vsl_indent() << "No msm_aligner defined.";
00139 }