contrib/mul/msm/msm_ref_shape_instance.h
Go to the documentation of this file.
00001 #ifndef msm_ref_shape_instance_h_
00002 #define msm_ref_shape_instance_h_
00003 //:
00004 // \file
00005 // \brief Representation of an instance of a shape model in ref frame.
00006 // \author Tim Cootes
00007 
00008 #include <msm/msm_points.h>
00009 #include <msm/msm_wt_mat_2d.h>
00010 #include <msm/msm_param_limiter.h>
00011 #include <mbl/mbl_cloneable_ptr.h>
00012 #include <vcl_iosfwd.h>
00013 
00014 class msm_ref_shape_model;
00015 
00016 //: Representation of an instance of a shape model in ref frame.
00017 //  Includes functions to fit instances to sets of points
00018 //  and to generate sets of points.
00019 //
00020 //  By default, all shape parameters are used and params() returns 
00021 //  a vector of length equal to the full number of shape modes.
00022 //  To use fewer modes, create a parameter vector with the desired
00023 //  number, and call set_params(b).
00024 //
00025 //  This uses no global pose - use msm_shape_instance for those.
00026 class msm_ref_shape_instance
00027 {
00028  private:
00029   //: Shape model of which this is an instance
00030   const msm_ref_shape_model *model_;
00031 
00032   //: Model parameters
00033   vnl_vector<double> b_;
00034 
00035   //: Current model points
00036   msm_points points_;
00037 
00038   //: When true, use Gaussian prior on params in fit_to_points*
00039   bool use_prior_;
00040 
00041   //: True if model points up to date with b_
00042   bool points_valid_;
00043 
00044   //: Object which applies limits to parameters in fit_to_points*
00045   mbl_cloneable_ptr<msm_param_limiter> param_limiter_;
00046 
00047   //: Workspace for points in fit_to_points
00048   msm_points tmp_points_;
00049 
00050  public:
00051 
00052   // Dflt ctor
00053   msm_ref_shape_instance();
00054 
00055   // Destructor
00056   ~msm_ref_shape_instance();
00057 
00058   //: Set up model (retains pointer to model)
00059   void set_shape_model(const msm_ref_shape_model& model);
00060 
00061   //: Define limits on parameters (clone taken)
00062   void set_param_limiter(const msm_param_limiter& limiter);
00063 
00064   //: Current object which limits parameters
00065   const msm_param_limiter& param_limiter() const 
00066   { return param_limiter_; }
00067 
00068   //: Current object which limits parameters (non-const)
00069   msm_param_limiter& param_limiter()
00070   { return param_limiter_; }
00071 
00072   //: When true, use Gaussian prior on params in fit_to_points*
00073   bool use_prior() const { return use_prior_; }
00074 
00075   //: When true, use Gaussian prior on params in fit_to_points*
00076   void set_use_prior(bool);
00077 
00078   //: Define parameters
00079   void set_params(const vnl_vector<double>& b);
00080 
00081   //: Set all shape parameters to zero
00082   void set_to_mean();
00083 
00084   //: Pointer to current model
00085   const msm_ref_shape_model* model_ptr() const { return model_; }
00086 
00087   //: Reference to current model
00088   const msm_ref_shape_model& model() const 
00089   { assert(model_!=0); return *model_; }
00090 
00091   //: Current shape parameters
00092   const vnl_vector<double>& params() const { return b_; }
00093 
00094   //: Current shape in model frame (uses lazy evaluation)
00095   const msm_points& points();
00096 
00097   //: Finds parameters to best match to points
00098   //  All points equally weighted.
00099   //  If pt_var>0, and use_prior(), then effect of
00100   //  Gaussian prior is to scale parameters by
00101   //  mode_var/(mode_var+pt_var).
00102   void fit_to_points(const msm_points& points, double pt_var=0);
00103 
00104   //: Finds parameters to best match to points
00105   //  Errors on point i are weighted by wts[i]
00106   //  If use_prior(), then include Gaussian prior on
00107   //  the shape parameters, and assume that wts are
00108   //  inverse variances.
00109   void fit_to_points_wt(const msm_points& points,
00110                         const vnl_vector<double>& wts);
00111 
00112   //: Finds parameters to best match to points
00113   //  Errors on point i are weighted by wt_mat[i] in target frame
00114   //  If use_prior(), then include Gaussian prior on
00115   //  the shape parameters, and assume that wt_mat are
00116   //  inverse covariances.
00117   void fit_to_points_wt_mat(const msm_points& pts,
00118                         const vcl_vector<msm_wt_mat_2d>& wt_mat);
00119 
00120   //: Version number for I/O
00121   short version_no() const;
00122 
00123   //: Name of the class
00124   vcl_string is_a() const;
00125 
00126   //: Print class to os
00127   void print_summary(vcl_ostream& os) const;
00128 
00129   //: Save class to binary file stream
00130   void b_write(vsl_b_ostream& bfs) const;
00131 
00132   //: Load class from binary file stream
00133   void b_read(vsl_b_istream& bfs);
00134 };
00135 
00136 
00137 //: Binary file stream output operator for class reference
00138 void vsl_b_write(vsl_b_ostream& bfs, const msm_ref_shape_instance& pts);
00139 
00140 
00141 //: Binary file stream input operator for class reference
00142 void vsl_b_read(vsl_b_istream& bfs, msm_ref_shape_instance& pts);
00143 
00144 //: Stream output operator for class reference
00145 vcl_ostream& operator<<(vcl_ostream& os,const msm_ref_shape_instance& pts);
00146 
00147 //: Stream output operator for class reference
00148 void vsl_print_summary(vcl_ostream& os,const msm_ref_shape_instance& pts);
00149 
00150 #endif // msm_ref_shape_instance_h_