contrib/mul/msm/msm_curve.h
Go to the documentation of this file.
00001 #ifndef msm_curve_h_
00002 #define msm_curve_h_
00003 //:
00004 // \file
00005 // \brief List of points making a curve - for defining boundaries
00006 // \author Tim Cootes
00007 
00008 #include <vcl_cassert.h>
00009 #include <vcl_cstddef.h>
00010 #include <vcl_iosfwd.h>
00011 #include <vcl_string.h>
00012 #include <vsl/vsl_fwd.h>
00013 #include <vcl_vector.h>
00014 
00015 //: List of points making a curve - for defining boundaries
00016 //  A curve is a sequence of point indices indicating how
00017 //  to joint the dots to form a boundary, for instance for
00018 //  display or in an Active Shape Model.
00019 //
00020 //  If closed, then last point assumed to be connected to the
00021 //  first.
00022 class msm_curve
00023 {
00024  private:
00025   //: Name of this curve or boundary
00026   vcl_string name_;
00027 
00028   //: True if this is an open curve (else closed curve)
00029   bool open_;
00030 
00031   //: List of point indices representing curve
00032   vcl_vector<unsigned> index_;
00033  public:
00034   // Default Constructor
00035   msm_curve();
00036 
00037   //: Define as range of indices [lo,hi]
00038   msm_curve(unsigned lo, unsigned hi,
00039             bool open=true, vcl_string name="");
00040 
00041   // Destructor
00042   ~msm_curve() {}
00043 
00044   //: Number of points defining the curve
00045   vcl_size_t size() const { return index_.size(); }
00046 
00047   void set(const vcl_vector<unsigned>& index,
00048            bool open=true, vcl_string name="");
00049 
00050   //: Define as range of indices [lo,hi]
00051   void set(unsigned lo, unsigned hi,
00052            bool open=true, vcl_string name="");
00053 
00054   //: Name of this curve or boundary
00055   const vcl_string& name() const { return name_; }
00056 
00057   //: True if this is an open curve (else closed curve)
00058   bool open() const { return open_; }
00059 
00060   //: Indicate if this is an open curve
00061   void set_open(bool b) { open_=b; }
00062 
00063   //: List of point indices representing curve
00064   vcl_vector<unsigned>& index() { return index_; }
00065 
00066   //: List of point indices representing curve
00067   const vcl_vector<unsigned>& index() const { return index_; }
00068 
00069   unsigned operator[](unsigned i) const
00070   { assert(i<index_.size()); return index_[i]; }
00071 
00072   //: Return the largest index value
00073   unsigned max_index() const;
00074 
00075   //: Adds offset to index of every point
00076   //  Useful when concatenating models
00077   void add_index_offset(int offset);
00078 
00079   //: Parse parameters in stream
00080   //  Expects
00081   // \verbatim
00082   // { name: Chin open: true indices: { 0 1 2 3 4 5 6 } }
00083   // or equivalently
00084   // { name: Chin open: true indices: { 0 : 6 } }
00085   // \endverbatim
00086   void config_from_stream(vcl_istream&);
00087 
00088   //: Print class to os
00089   void print_summary(vcl_ostream& os) const;
00090 
00091   //: Save class to binary file stream
00092   void b_write(vsl_b_ostream& bfs) const;
00093 
00094   //: Load class from binary file stream
00095   void b_read(vsl_b_istream& bfs);
00096 
00097   //: Equality test
00098   bool operator==(const msm_curve& curve) const;
00099 };
00100 
00101 
00102 //: Binary file stream output operator for class reference
00103 void vsl_b_write(vsl_b_ostream& bfs, const msm_curve& c);
00104 
00105 
00106 //: Binary file stream input operator for class reference
00107 void vsl_b_read(vsl_b_istream& bfs, msm_curve& c);
00108 
00109 //: Stream output operator for class reference
00110 vcl_ostream& operator<<(vcl_ostream& os,const msm_curve& c);
00111 
00112 //: Stream output operator for class reference
00113 void vsl_print_summary(vcl_ostream& os,const msm_curve& c);
00114 
00115 //: Container for a set of curves
00116 class msm_curves : public vcl_vector<msm_curve>
00117 {
00118  public:
00119   //: Default constructor
00120   msm_curves();
00121 
00122   //: Construct as a single curve
00123   msm_curves(unsigned lo, unsigned hi,
00124              bool open=true, vcl_string name="");
00125 
00126   //: Return index of first curve with given name, or -1
00127   int which_curve(const vcl_string& name) const;
00128 
00129   //: Return the largest index value in any curve
00130   unsigned max_index() const;
00131 
00132   //: Parse parameters in stream
00133   //  Expects
00134   // \verbatim
00135   // {
00136   //   curve: { name: Chin open: true indices: { 0 1 2 3 4 5 6 } }
00137   //   curve: { name: Nose open: false indices: { 11 : 15 } }
00138   // }
00139   // \endverbatim
00140   void config_from_stream(vcl_istream&);
00141 
00142   //: Save to text file
00143   // Writes in format:
00144   // \verbatim
00145   // curves: {
00146   //   curve: { name: Chin open: true indices: { 0 1 2 3 4 5 6 } }
00147   //   curve: { name: Nose open: false indices: { 11 : 15 } }
00148   // }
00149   // \endverbatim
00150   bool write_text_file(const vcl_string& path);
00151 
00152   //: Read from text file
00153   bool read_text_file(const vcl_string& path);
00154 };
00155 
00156 //: Stream output operator
00157 vcl_ostream& operator<<(vcl_ostream& os,const msm_curves& c);
00158 
00159 //: Binary file stream output operator for class reference
00160 void vsl_b_write(vsl_b_ostream& bfs, const msm_curves& c);
00161 
00162 
00163 //: Binary file stream input operator for class reference
00164 void vsl_b_read(vsl_b_istream& bfs, msm_curves& c);
00165 
00166 
00167 #endif // msm_curve_h_