contrib/gel/vsol/vsol_box_2d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_box_2d.h
00002 #ifndef vsol_box_2d_h_
00003 #define vsol_box_2d_h_
00004 //:
00005 // \file
00006 // \brief A bounding box
00007 //
00008 // This is a time stamped and refcounted interface to vbl_box<double,2>
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   2003/01/09 Peter Vanroose deprecated set_min_x() etc. and replaced with
00013 //                             more safe add_point()
00014 //   2004/05/10 Joseph Mundy   added binary I/O methods
00015 //   2004/05/10 Joseph Mundy   changed bounds access methods to const
00016 //   2004/09/27 Peter Vanroose grow_minmax_bounds() now takes smart pointer arg
00017 //   2004/11/15 H.Can Aras     added inheritance from vsol_box
00018 //   2006/05/31 O.C. Ozcanli   added inside method
00019 // \endverbatim
00020 
00021 #include <vsol/vsol_box.h>
00022 #include <vsl/vsl_binary_io.h>
00023 #include <vul/vul_timestamp.h>
00024 #include <vbl/vbl_ref_count.h>
00025 #include <vbl/vbl_bounding_box.h>
00026 #include "vsol_box_2d_sptr.h"
00027 #include <vcl_iostream.h>
00028 
00029 //: A bounding box for 2d spatial objects
00030 
00031 class vsol_box_2d : public vsol_box, public vbl_ref_count, public vul_timestamp
00032 {
00033  protected:
00034   vbl_bounding_box<double,2> box_;
00035 
00036  public:
00037   //: create an empty box
00038   vsol_box_2d() {}
00039 
00040   vsol_box_2d(vsol_box_2d const &b)
00041     :  vsol_box(), vbl_ref_count(), vul_timestamp(), box_(b.box_) {}
00042 
00043   vsol_box_2d(vbl_bounding_box<double,2> const &b) : box_(b) {}
00044 
00045   ~vsol_box_2d() {}
00046 
00047   // accessors
00048   bool empty() const { return box_.empty(); }
00049   double get_min_x() const;
00050   double get_max_x() const;
00051 
00052   double get_min_y() const;
00053   double get_max_y() const;
00054 
00055   double width() const { return get_max_x() - get_min_x(); }
00056   double height()const { return get_max_y() - get_min_y(); }
00057   double area() const { return width() * height(); }
00058 
00059   //: enlarge the bounding box by adding the point (x,y) and taking convex hull
00060   void add_point(double x, double y);
00061 
00062   //: Compare this' bounds to comp_box and grow to the maximum bounding box.
00063   //  I.e., take the convex union of this and comp_box
00064   void grow_minmax_bounds(vsol_box_2d_sptr const& comp_box);
00065 
00066   //: a<b means a is inside b
00067   bool operator< (vsol_box_2d& box) const;
00068 
00069   //: is box about the same as this?
00070   bool near_equal(vsol_box_2d const& box, float tolerance) const;
00071 
00072   //: reset the bounds of the box, i.e., make the box empty
00073   void reset_bounds();
00074 
00075   //: is a 2D point inside the bounding box
00076   bool inside(double x, double y) const { return box_.inside(x, y); }
00077 
00078   // ==== Binary IO methods ======
00079 
00080   //: Binary save self to stream.
00081   void b_write(vsl_b_ostream &os) const;
00082 
00083   //: Binary load self from stream.
00084   void b_read(vsl_b_istream &is);
00085 
00086   //: Return IO version number;
00087   short version() const;
00088 
00089   //: Print an ascii summary to the stream
00090   void print_summary(vcl_ostream &os) const;
00091 
00092   //: Return a platform independent string identifying the class
00093   virtual vcl_string is_a() const { return vcl_string("vsol_box_2d"); }
00094 
00095   //: Return true if the argument matches the string identifying the class or any parent class
00096   virtual bool is_class(const vcl_string& cls) const { return cls==is_a(); }
00097 };
00098 
00099 //: Stream operator
00100 vcl_ostream&  operator<<(vcl_ostream& s, vsol_box_2d const& p);
00101 
00102 //: Binary save vsol_box_2d* to stream.
00103 void vsl_b_write(vsl_b_ostream &os, vsol_box_2d_sptr const& p);
00104 
00105 //: Binary load vsol_box_2d* from stream.
00106 void vsl_b_read(vsl_b_istream &is, vsol_box_2d_sptr &p);
00107 
00108 //: Print human readable summary of box to a stream
00109 //  (This is needed for the instantiation of vsl_vector_io<vsol_box_2d_sptr>)
00110 inline void vsl_print_summary(vcl_ostream& os, vsol_box_2d_sptr const& b)
00111 {
00112   os << *b;
00113 }
00114 
00115 #endif // vsol_box_2d_h_