contrib/gel/vsol/vsol_box_3d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_box_3d.h
00002 #ifndef vsol_box_3d_h_
00003 #define vsol_box_3d_h_
00004 //:
00005 // \file
00006 // \brief A bounding box
00007 //
00008 //  Note that the definition of width, depth and
00009 //  height are such that the X-Y plane is considered
00010 //  as a Rectangle with a "width" and "height"
00011 //  according to the usual definition. The figure
00012 //  shows a right-handed coordinate system, but there
00013 //  is no commitment to that in the definitions
00014 // \verbatim
00015 //                       |<--width-->|
00016 //                 Z     O-----------O  ---
00017 //                    | /           /|   ^
00018 //                    |/           / |   |
00019 //                    O-----------O  | depth
00020 //                    |           |  |   |
00021 //                    |  centroid |  |   v
00022 //                    |  Y  o     |  O  ---
00023 //                    | /         | /   /_____height
00024 //                    |/          |/   /
00025 //                    O-----------O  --- X
00026 // \endverbatim
00027 //
00028 // \verbatim
00029 //  Modifications
00030 //   2003/01/09 Peter Vanroose deprecated set_min_x() etc. and replaced with
00031 //                             more safe add_point()
00032 //   2004/09/25 Peter Vanroose added binary I/O
00033 //   2004/09/27 Peter Vanroose grow_minmax_bounds() now takes smart pointer arg
00034 //   2004/11/15 H.Can Aras     added inheritance from vsol_box
00035 // \endverbatim
00036 
00037 #include <vsol/vsol_box.h>
00038 #include <vsl/vsl_binary_io.h>
00039 #include <vul/vul_timestamp.h>
00040 #include <vbl/vbl_ref_count.h>
00041 #include <vbl/vbl_bounding_box.h>
00042 #include "vsol_box_3d_sptr.h"
00043 #include <vcl_iostream.h>
00044 
00045 //: A bounding box for 3d spatial objects
00046 
00047 class vsol_box_3d : public vsol_box, public vbl_ref_count , public vul_timestamp
00048 {
00049  protected:
00050   vbl_bounding_box<double,3> box_;
00051 
00052  public:
00053   //: create an empty box
00054   vsol_box_3d() {}
00055 
00056   vsol_box_3d(vsol_box_3d const& b)
00057     : vsol_box(), vbl_ref_count(), vul_timestamp(), box_(b.box_) {}
00058 
00059   vsol_box_3d(vbl_bounding_box<double,3> const &b) : box_(b) {}
00060 
00061   ~vsol_box_3d() {}
00062 
00063   // accessors
00064 
00065   double get_min_x() const;
00066   double get_max_x() const;
00067 
00068   double get_min_y() const;
00069   double get_max_y() const;
00070 
00071   double get_min_z() const;
00072   double get_max_z() const;
00073 
00074   double width() const { return get_max_x() - get_min_x(); }
00075   double height() const { return get_max_y() - get_min_y(); }
00076   double depth() const { return get_max_z() - get_min_z(); }
00077   double volume() const { return width() * height() * depth(); }
00078 
00079   //: enlarge the bounding box by adding the point (x,y,z) & taking convex hull
00080   void add_point(double x, double y, double z);
00081 
00082   //: Compare this' bounds to comp_box and grow to the maximum bounding box.
00083   //  I.e., take the convex union of this and comp_box
00084   void grow_minmax_bounds(vsol_box_3d_sptr const& comp_box);
00085 
00086   //: a<b means a is inside b
00087   bool operator< (vsol_box_3d& box) const;
00088 
00089   //: is box about the same as this?
00090   bool near_equal(vsol_box_3d const& box, float tolerance) const;
00091 
00092   //: reset the bounds of the box, i.e., make the box empty
00093   void reset_bounds();
00094 
00095   // ==== Binary IO methods ======
00096 
00097   //: Binary save self to stream.
00098   void b_write(vsl_b_ostream &os) const;
00099 
00100   //: Binary load self from stream.
00101   void b_read(vsl_b_istream &is);
00102 
00103   //: Return IO version number;
00104   short version() const;
00105 
00106   //: Print an ascii summary to the stream
00107   void print_summary(vcl_ostream &os) const;
00108 
00109   //: Return a platform independent string identifying the class
00110   virtual vcl_string is_a() const { return vcl_string("vsol_box_3d"); }
00111 
00112   //: Return true if the argument matches the string identifying the class or any parent class
00113   virtual bool is_class(const vcl_string& cls) const { return cls==is_a(); }
00114 };
00115 
00116 //: Stream operator
00117 vcl_ostream&  operator<<(vcl_ostream& s, vsol_box_3d const& p);
00118 
00119 //: Binary save vsol_box_3d* to stream.
00120 void vsl_b_write(vsl_b_ostream &os, vsol_box_3d_sptr const& p);
00121 
00122 //: Binary load vsol_box_3d* from stream.
00123 void vsl_b_read(vsl_b_istream &is, vsol_box_3d_sptr &p);
00124 
00125 //: Print human readable summary of box to a stream
00126 //  (This is needed for the instantiation of vsl_vector_io<vsol_box_3d_sptr>)
00127 inline void vsl_print_summary(vcl_ostream& os, vsol_box_3d_sptr const& b)
00128 {
00129   os << *b;
00130 }
00131 
00132 #endif // vsol_box_3d_h_