1 // This is gel/vsol/vsol_box_2d.h
2 #ifndef vsol_box_2d_h_
3 #define vsol_box_2d_h_
4 //:
5 // \file
6 // \brief A bounding box
7 //
8 // This is a time stamped and refcounted interface to vbl_box<double,2>
9 //
10 // \verbatim
11 // Modifications
12 // 2003/01/09 Peter Vanroose deprecated set_min_x() etc. and replaced with
13 // more safe add_point()
14 // 2004/05/10 Joseph Mundy added binary I/O methods
15 // 2004/05/10 Joseph Mundy changed bounds access methods to const
16 // 2004/09/27 Peter Vanroose grow_minmax_bounds() now takes smart pointer arg
17 // 2004/11/15 H.Can Aras added inheritance from vsol_box
18 // 2006/05/31 O.C. Ozcanli added inside method
19 // \endverbatim
20
21 #include <iostream>
22 #include <vsol/vsol_box.h>
23 #include <vsl/vsl_binary_io.h>
24 #include <vul/vul_timestamp.h>
25 #include <vbl/vbl_ref_count.h>
26 #include <vbl/vbl_bounding_box.h>
27 #include "vsol_box_2d_sptr.h"
28 #ifdef _MSC_VER
29 # include <vcl_msvc_warnings.h>
30 #endif
31
32 //: A bounding box for 2d spatial objects
33
34 class vsol_box_2d : public vsol_box, public vbl_ref_count, public vul_timestamp
35 {
36 protected:
37 vbl_bounding_box<double,2> box_;
38
39 public:
40 //: create an empty box
41 vsol_box_2d() = default;
42
vsol_box_2d(vsol_box_2d const & b)43 vsol_box_2d(vsol_box_2d const &b)
44 : vsol_box(), vbl_ref_count(), vul_timestamp(), box_(b.box_) {}
45
vsol_box_2d(vbl_bounding_box<double,2> const & b)46 vsol_box_2d(vbl_bounding_box<double,2> const &b) : box_(b) {}
47
48 ~vsol_box_2d() override = default;
49
50 // accessors
empty()51 bool empty() const { return box_.empty(); }
52 double get_min_x() const;
53 double get_max_x() const;
54
55 double get_min_y() const;
56 double get_max_y() const;
57
width()58 double width() const { return get_max_x() - get_min_x(); }
height()59 double height()const { return get_max_y() - get_min_y(); }
area()60 double area() const { return width() * height(); }
61
62 //: enlarge the bounding box by adding the point (x,y) and taking convex hull
63 void add_point(double x, double y);
64
65 //: Compare this' bounds to comp_box and grow to the maximum bounding box.
66 // I.e., take the convex union of this and comp_box
67 void grow_minmax_bounds(vsol_box_2d_sptr const& comp_box);
68
69 //: a<b means a is inside b
70 bool operator< (vsol_box_2d& box) const;
71
72 //: is box about the same as this?
73 bool near_equal(vsol_box_2d const& box, float tolerance) const;
74
75 //: reset the bounds of the box, i.e., make the box empty
76 void reset_bounds();
77
78 //: is a 2D point inside the bounding box
inside(double x,double y)79 bool inside(double x, double y) const { return box_.inside(x, y); }
80
81 // ==== Binary IO methods ======
82
83 //: Binary save self to stream.
84 void b_write(vsl_b_ostream &os) const;
85
86 //: Binary load self from stream.
87 void b_read(vsl_b_istream &is);
88
89 //: Return IO version number;
90 short version() const;
91
92 //: Print an ascii summary to the stream
93 void print_summary(std::ostream &os) const;
94
95 //: Return a platform independent string identifying the class
is_a()96 virtual std::string is_a() const { return std::string("vsol_box_2d"); }
97
98 //: Return true if the argument matches the string identifying the class or any parent class
is_class(const std::string & cls)99 virtual bool is_class(const std::string& cls) const { return cls==is_a(); }
100 };
101
102 //: Stream operator
103 std::ostream& operator<<(std::ostream& s, vsol_box_2d const& p);
104
105 //: Binary save vsol_box_2d* to stream.
106 void vsl_b_write(vsl_b_ostream &os, vsol_box_2d_sptr const& p);
107
108 //: Binary load vsol_box_2d* from stream.
109 void vsl_b_read(vsl_b_istream &is, vsol_box_2d_sptr &p);
110
111 //: Print human readable summary of box to a stream
112 // (This is needed for the instantiation of vsl_vector_io<vsol_box_2d_sptr>)
vsl_print_summary(std::ostream & os,vsol_box_2d_sptr const & b)113 inline void vsl_print_summary(std::ostream& os, vsol_box_2d_sptr const& b)
114 {
115 os << *b;
116 }
117
118 #endif // vsol_box_2d_h_
119