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