contrib/mul/msm/msm_shape_model.cxx
Go to the documentation of this file.
00001 #include "msm_shape_model.h"
00002 //:
00003 // \file
00004 // \brief Contains mean/modes etc of a shape model
00005 // \author Tim Cootes
00006 
00007 #include <vcl_iostream.h>
00008 #include <vsl/vsl_indent.h>
00009 #include <vsl/vsl_binary_io.h>
00010 #include <vnl/io/vnl_io_vector.h>
00011 #include <vnl/io/vnl_io_matrix.h>
00012 #include <vcl_cstdlib.h>  // for vcl_atoi() & vcl_abort()
00013 #include <vcl_cassert.h>
00014 
00015 //=======================================================================
00016 // Dflt ctor
00017 //=======================================================================
00018 
00019 msm_shape_model::msm_shape_model()
00020 {
00021 }
00022 
00023 //=======================================================================
00024 // Destructor
00025 //=======================================================================
00026 
00027 msm_shape_model::~msm_shape_model()
00028 {
00029 }
00030 
00031 //: Set up model
00032 void msm_shape_model::set(const msm_points& mean,
00033                           const vnl_matrix<double>& modes,
00034                           const vnl_vector<double>& mode_var,
00035                           const vnl_vector<double>& default_pose,
00036                           const msm_aligner& aligner,
00037                           const msm_param_limiter& param_limiter)
00038 {
00039   msm_ref_shape_model::set(mean,modes,mode_var,param_limiter);
00040 
00041   assert(default_pose.size()==aligner.size());
00042   default_pose_ = default_pose;
00043   aligner_ = aligner;
00044 }
00045 
00046 //: Equality test
00047 bool msm_shape_model::operator==(const msm_shape_model& model)
00048 {
00049   if (model.mean_.size()!=mean_.size()) return false;
00050   if (model.mode_var_.size()!=mode_var_.size()) return false;
00051   if (mean_.size()==0) return true;  // Empty models
00052 
00053   const double epsilon = 1e-3;
00054 
00055   double ssd1=vnl_vector_ssd(model.mean_.vector(),mean_.vector());
00056   if (vcl_fabs(ssd1/mean_.size())>epsilon) return false;
00057 
00058   double ssd2=vnl_vector_ssd(model.mode_var_,mode_var_);
00059   if (vcl_fabs(ssd2/mode_var_.size())>epsilon) return false;
00060 
00061   double max_d = (modes_-model.modes_).absolute_value_max();
00062   if (max_d>epsilon) return false;
00063 
00064   return true;
00065 }
00066 
00067 
00068 //=======================================================================
00069 // Method: version_no
00070 //=======================================================================
00071 
00072 short msm_shape_model::version_no() const
00073 {
00074   return 1;
00075 }
00076 
00077 //=======================================================================
00078 // Method: is_a
00079 //=======================================================================
00080 
00081 vcl_string msm_shape_model::is_a() const
00082 {
00083   return vcl_string("msm_shape_model");
00084 }
00085 
00086 //=======================================================================
00087 // Method: print
00088 //=======================================================================
00089 
00090   // required if data is present in this class
00091 void msm_shape_model::print_summary(vcl_ostream& os) const
00092 {
00093   os << "n_pts: "<<size()<<" n_modes: "<<n_modes()<<vcl_endl;
00094   vsl_indent_inc(os);
00095   os << vsl_indent() << " aligner: ";
00096   if (aligner_.isDefined()) os<<aligner_; else os<<"-";
00097   os << vcl_endl << vsl_indent() << " param_limiter: ";
00098   if (param_limiter_.isDefined())
00099     os<<param_limiter_; else os<<"-";
00100   os<<vcl_endl;
00101   vsl_indent_dec(os);
00102 }
00103 
00104 //=======================================================================
00105 // Method: save
00106 //=======================================================================
00107 
00108   // required if data is present in this class
00109 void msm_shape_model::b_write(vsl_b_ostream& bfs) const
00110 {
00111   vsl_b_write(bfs,version_no());
00112   msm_ref_shape_model::b_write(bfs);  // Save base
00113   vsl_b_write(bfs,default_pose_);
00114   vsl_b_write(bfs,aligner_);
00115 }
00116 
00117 //=======================================================================
00118 // Method: load
00119 //=======================================================================
00120 
00121   // required if data is present in this class
00122 void msm_shape_model::b_read(vsl_b_istream& bfs)
00123 {
00124   short version;
00125   vsl_b_read(bfs,version);
00126   switch (version)
00127   {
00128     case (1):
00129       msm_ref_shape_model::b_read(bfs); // Load base
00130       vsl_b_read(bfs,default_pose_);
00131       vsl_b_read(bfs,aligner_);
00132       break;
00133     default:
00134       vcl_cerr << "msm_shape_model::b_read() :\n"
00135                << "Unexpected version number " << version << vcl_endl;
00136       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00137       return;
00138   }
00139 }
00140 
00141 
00142 //=======================================================================
00143 // Associated function: operator<<
00144 //=======================================================================
00145 
00146 void vsl_b_write(vsl_b_ostream& bfs, const msm_shape_model& b)
00147 {
00148   b.b_write(bfs);
00149 }
00150 
00151 //=======================================================================
00152 // Associated function: operator>>
00153 //=======================================================================
00154 
00155 void vsl_b_read(vsl_b_istream& bfs, msm_shape_model& b)
00156 {
00157   b.b_read(bfs);
00158 }
00159 
00160 //=======================================================================
00161 // Associated function: operator<<
00162 //=======================================================================
00163 
00164 vcl_ostream& operator<<(vcl_ostream& os,const msm_shape_model& b)
00165 {
00166   vsl_indent_inc(os);
00167   b.print_summary(os);
00168   vsl_indent_dec(os);
00169   return os;
00170 }
00171 
00172 //: Stream output operator for class reference
00173 void vsl_print_summary(vcl_ostream& os,const msm_shape_model& b)
00174 {
00175  os << b;
00176 }