1 #ifndef msm_ref_shape_instance_h_ 2 #define msm_ref_shape_instance_h_ 3 //: 4 // \file 5 // \brief Representation of an instance of a shape model in ref frame. 6 // \author Tim Cootes 7 8 #include <iostream> 9 #include <iosfwd> 10 #include <msm/msm_points.h> 11 #include <msm/msm_wt_mat_2d.h> 12 #include <msm/msm_param_limiter.h> 13 #include <mbl/mbl_cloneable_ptr.h> 14 #ifdef _MSC_VER 15 # include <vcl_msvc_warnings.h> 16 #endif 17 18 class msm_ref_shape_model; 19 20 //: Representation of an instance of a shape model in ref frame. 21 // Includes functions to fit instances to sets of points 22 // and to generate sets of points. 23 // 24 // By default, all shape parameters are used and params() returns 25 // a vector of length equal to the full number of shape modes. 26 // To use fewer modes, create a parameter vector with the desired 27 // number, and call set_params(b). 28 // 29 // This uses no global pose - use msm_shape_instance for those. 30 class msm_ref_shape_instance 31 { 32 private: 33 //: Shape model of which this is an instance 34 const msm_ref_shape_model *model_{nullptr}; 35 36 //: Model parameters 37 vnl_vector<double> b_; 38 39 //: Current model points 40 msm_points points_; 41 42 //: When true, use Gaussian prior on params in fit_to_points* 43 bool use_prior_{false}; 44 45 //: True if model points up to date with b_ 46 bool points_valid_; 47 48 //: Object which applies limits to parameters in fit_to_points* 49 mbl_cloneable_ptr<msm_param_limiter> param_limiter_; 50 51 //: Workspace for points in fit_to_points 52 msm_points tmp_points_; 53 54 public: 55 56 // Dflt ctor 57 msm_ref_shape_instance(); 58 59 // Destructor 60 ~msm_ref_shape_instance(); 61 62 //: Set up model (retains pointer to model) 63 void set_shape_model(const msm_ref_shape_model& model); 64 65 //: Define limits on parameters (clone taken) 66 void set_param_limiter(const msm_param_limiter& limiter); 67 68 //: Current object which limits parameters param_limiter()69 const msm_param_limiter& param_limiter() const 70 { return param_limiter_; } 71 72 //: Current object which limits parameters (non-const) param_limiter()73 msm_param_limiter& param_limiter() 74 { return param_limiter_; } 75 76 //: When true, use Gaussian prior on params in fit_to_points* use_prior()77 bool use_prior() const { return use_prior_; } 78 79 //: When true, use Gaussian prior on params in fit_to_points* 80 void set_use_prior(bool); 81 82 //: Define parameters 83 void set_params(const vnl_vector<double>& b); 84 85 //: Set all shape parameters to zero 86 void set_to_mean(); 87 88 //: Pointer to current model model_ptr()89 const msm_ref_shape_model* model_ptr() const { return model_; } 90 91 //: Reference to current model model()92 const msm_ref_shape_model& model() const 93 { assert(model_!=nullptr); return *model_; } 94 95 //: Current shape parameters params()96 const vnl_vector<double>& params() const { return b_; } 97 98 //: Current shape in model frame (uses lazy evaluation) 99 const msm_points& points(); 100 101 //: Finds parameters to best match to points 102 // All points equally weighted. 103 // If pt_var>0, and use_prior(), then effect of 104 // Gaussian prior is to scale parameters by 105 // mode_var/(mode_var+pt_var). 106 void fit_to_points(const msm_points& points, double pt_var=0); 107 108 //: Finds parameters to best match to points 109 // Errors on point i are weighted by wts[i] 110 // If use_prior(), then include Gaussian prior on 111 // the shape parameters, and assume that wts are 112 // inverse variances. 113 void fit_to_points_wt(const msm_points& points, 114 const vnl_vector<double>& wts); 115 116 //: Finds parameters to best match to points 117 // Errors on point i are weighted by wt_mat[i] in target frame 118 // If use_prior(), then include Gaussian prior on 119 // the shape parameters, and assume that wt_mat are 120 // inverse covariances. 121 void fit_to_points_wt_mat(const msm_points& pts, 122 const std::vector<msm_wt_mat_2d>& wt_mat); 123 124 //: Version number for I/O 125 short version_no() const; 126 127 //: Name of the class 128 std::string is_a() const; 129 130 //: Print class to os 131 void print_summary(std::ostream& os) const; 132 133 //: Save class to binary file stream 134 void b_write(vsl_b_ostream& bfs) const; 135 136 //: Load class from binary file stream 137 void b_read(vsl_b_istream& bfs); 138 }; 139 140 141 //: Binary file stream output operator for class reference 142 void vsl_b_write(vsl_b_ostream& bfs, const msm_ref_shape_instance& pts); 143 144 145 //: Binary file stream input operator for class reference 146 void vsl_b_read(vsl_b_istream& bfs, msm_ref_shape_instance& pts); 147 148 //: Stream output operator for class reference 149 std::ostream& operator<<(std::ostream& os,const msm_ref_shape_instance& pts); 150 151 //: Stream output operator for class reference 152 void vsl_print_summary(std::ostream& os,const msm_ref_shape_instance& pts); 153 154 #endif // msm_ref_shape_instance_h_ 155