|
1 // -*- c++ -*- |
|
2 /** |
|
3 Implementation of a bounding box of plainvectors. |
|
4 |
|
5 */ |
|
6 #ifndef HUGO_BOUNDINGBOX_H |
|
7 #define HUGO_BOUNDINGBOX_H |
|
8 |
|
9 |
|
10 #include <xy.h> |
|
11 |
|
12 namespace hugo { |
|
13 |
|
14 template<typename T> |
|
15 class BoundingBox { |
|
16 xy<T> bottomleft, topright; |
|
17 bool _empty; |
|
18 public: |
|
19 |
|
20 ///Default constructor: an empty bounding box |
|
21 BoundingBox() { _empty = true; } |
|
22 |
|
23 ///Constructing the instance from one point |
|
24 BoundingBox(xy<T> a) { bottomleft=topright=a; _empty = false; } |
|
25 |
|
26 ///Is there any point added |
|
27 bool empty() const { |
|
28 return _empty; |
|
29 } |
|
30 |
|
31 ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) |
|
32 xy<T> bottomLeft() const { |
|
33 return bottomleft; |
|
34 }; |
|
35 |
|
36 ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) |
|
37 xy<T> topRight() const { |
|
38 return topright; |
|
39 }; |
|
40 |
|
41 ///Checks whether a point is inside a bounding box |
|
42 bool inside(const xy<T>& u){ |
|
43 if (_empty) |
|
44 return false; |
|
45 else{ |
|
46 return ((u.x-bottomleft.x)*(topright.x-u.x) >= 0 && |
|
47 (u.y-bottomleft.y)*(topright.y-u.y) >= 0 ); |
|
48 } |
|
49 } |
|
50 |
|
51 ///Increments a bounding box with a point |
|
52 BoundingBox& operator +=(const xy<T>& u){ |
|
53 if (_empty){ |
|
54 bottomleft=topright=u; |
|
55 _empty = false; |
|
56 } |
|
57 else{ |
|
58 if (bottomleft.x > u.x) bottomleft.x = u.x; |
|
59 if (bottomleft.y > u.y) bottomleft.y = u.y; |
|
60 if (topright.x < u.x) topright.x = u.x; |
|
61 if (topright.y < u.y) topright.y = u.y; |
|
62 } |
|
63 return *this; |
|
64 }; |
|
65 |
|
66 ///Sums a bounding box and a point |
|
67 BoundingBox operator +(const xy<T>& u){ |
|
68 BoundingBox b = *this; |
|
69 return b += u; |
|
70 }; |
|
71 |
|
72 ///Increments a bounding box with an other bounding box |
|
73 BoundingBox& operator +=(const BoundingBox &u){ |
|
74 if ( !u.empty() ){ |
|
75 *this += u.bottomLeft(); |
|
76 *this += u.topRight(); |
|
77 } |
|
78 return *this; |
|
79 }; |
|
80 |
|
81 ///Sums two bounding boxes |
|
82 BoundingBox operator +(const BoundingBox& u){ |
|
83 BoundingBox b = *this; |
|
84 return b += u; |
|
85 }; |
|
86 |
|
87 };//class BoundingBox |
|
88 |
|
89 |
|
90 } //namespace hugo |
|
91 |
|
92 #endif //HUGO_BOUNDINGBOX_H |