contrib/mul/msm/msm_points.h
Go to the documentation of this file.
00001 #ifndef msm_points_h_
00002 #define msm_points_h_
00003 //:
00004 // \file
00005 // \brief Set of 2D points, stored in a vnl_vector (x0,y0,x1,y1...)
00006 // \author Tim Cootes
00007 
00008 #include <vcl_cassert.h>
00009 #include <vcl_iosfwd.h>
00010 #include <vcl_string.h>
00011 #include <vsl/vsl_fwd.h>
00012 #include <vnl/vnl_vector.h>
00013 #include <vgl/vgl_point_2d.h>
00014 #include <vgl/vgl_box_2d.h>
00015 #include <vimt/vimt_transform_2d.h>
00016 
00017 //: Set of 2D points, stored in a vnl_vector (x0,y0,x1,y1...)
00018 //  Get at raw vector with vector()
00019 class msm_points
00020 {
00021  private:
00022   vnl_vector<double> v_;
00023  public:
00024 
00025   // Dflt ctor
00026   msm_points();
00027 
00028   // Set to hold n points (initially all zero)
00029   msm_points(unsigned n);
00030 
00031   // Destructor
00032   ~msm_points();
00033 
00034   //: Vector storing point ordinates as (x0,y0,x1,y1...)
00035   const vnl_vector<double>& vector() const { return v_; }
00036 
00037   //: Vector storing point ordinates as (x0,y0,x1,y1...)
00038   // Non-const access - use with care
00039   vnl_vector<double>& vector() { return v_; }
00040 
00041   //: Number of points
00042   unsigned size() const { return v_.size()/2; }
00043 
00044   //: Set to hold n points (initially all (x,y))
00045   //  If n==size() then does not change anything.
00046   void set_size(unsigned n, double x=0, double y=0);
00047 
00048   //: Set point i
00049   void set_point(unsigned i, double x, double y)
00050   {
00051     assert(i<size()); v_[2*i]=x; v_[2*i+1]=y;
00052   }
00053 
00054   //: Return i-th point
00055   vgl_point_2d<double> operator[](unsigned i) const
00056   {
00057     assert(i<size());
00058     return vgl_point_2d<double>(v_[2*i],v_[2*i+1]);
00059   }
00060 
00061   //: Set this to be equal to supplied points
00062   void set_points(const vcl_vector<vgl_point_2d<double> >& pts);
00063 
00064   //: Copy points into pts
00065   void get_points(vcl_vector<vgl_point_2d<double> >& pts) const;
00066 
00067   //: Return centre of gravity of points
00068   vgl_point_2d<double> cog() const;
00069 
00070   //: Return RMS of distance of points to CoG.
00071   double scale() const;
00072 
00073   //: Compute centre of gravity and RMS distance to CoG
00074   void get_cog_and_scale(vgl_point_2d<double>& cog, double& scale) const;
00075 
00076   //: Scale current points by s about the origin
00077   void scale_by(double s);
00078 
00079   //: Translate current points by (tx,ty)
00080   void translate_by(double tx, double ty);
00081 
00082   //: Transform current points with similarity transform
00083   //  (x,y) -> (ax-by+tx, bx+ay+ty);
00084   void transform_by(double a, double b, double tx, double ty);
00085 
00086   //: Transform current points with t
00087   void transform_by(const vimt_transform_2d& t);
00088 
00089   //: Bounding box of points
00090   void get_bounds(vgl_point_2d<double>& b_lo,
00091                   vgl_point_2d<double>& b_hi) const;
00092 
00093   //: Return bounding box of points
00094   vgl_box_2d<double> bounds() const;
00095 
00096   //: Write out points to named text file
00097   //  Returns true if successful.
00098   bool write_text_file(const vcl_string& path) const;
00099 
00100   //: Read in points from named text file
00101   //  Returns true if successful.
00102   bool read_text_file(const vcl_string& path);
00103 
00104   //: Version number for I/O
00105   short version_no() const;
00106 
00107   //: Name of the class
00108   vcl_string is_a() const;
00109 
00110   //: Print class to os
00111   void print_summary(vcl_ostream& os) const;
00112 
00113   //: Save class to binary file stream
00114   void b_write(vsl_b_ostream& bfs) const;
00115 
00116   //: Load class from binary file stream
00117   void b_read(vsl_b_istream& bfs);
00118 
00119   //: Equality test
00120   bool operator==(const msm_points& points) const;
00121 };
00122 
00123 
00124 //: Binary file stream output operator for class reference
00125 void vsl_b_write(vsl_b_ostream& bfs, const msm_points& pts);
00126 
00127 
00128 //: Binary file stream input operator for class reference
00129 void vsl_b_read(vsl_b_istream& bfs, msm_points& pts);
00130 
00131 //: Stream output operator for class reference
00132 vcl_ostream& operator<<(vcl_ostream& os,const msm_points& pts);
00133 
00134 //: Stream output operator for class reference
00135 void vsl_print_summary(vcl_ostream& os,const msm_points& pts);
00136 
00137 #endif // msm_points_h_