UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
box.h
Go to the documentation of this file.
1 #ifndef UGDK_UTIL_BOX_H_
2 #define UGDK_UTIL_BOX_H_
3 
4 #include <array>
5 #include <ugdk/structure.h>
6 
7 namespace ugdk {
8 namespace structure {
9 
10 template <int DIMENSIONS>
11 class Box {
12  public:
13  typedef std::array<Coordinate, DIMENSIONS> Point;
14 
15  Box();
16  Box(const Point& min_coordinates, const Point& max_coordinates);
17  virtual ~Box();
18 
19  void UpdateBox(const Box&);
20  bool IsBelow(int depth, Coordinate boundary) const;
21  bool IsAbove(int depth, Coordinate boundary) const;
22  bool Contains(const Box& box) const;
23  bool Intersects(const Box *box) const;
24 
25  const Point& min_coordinates() const;
26  const Point& max_coordinates() const;
27 
28  protected:
31 };
32 
33 //************* Implementation **************
34 
35 template <int DIMENSIONS>
37 
38 template <int DIMENSIONS>
39 Box<DIMENSIONS>::Box(const Point& min_coordinates, const Point& max_coordinates)
40  : min_coordinates_(min_coordinates), max_coordinates_(max_coordinates) {}
41 
42 template <int DIMENSIONS>
44 
45 template <int DIMENSIONS>
46 void Box<DIMENSIONS>::UpdateBox(const Box& box) {
47  *this = box;
48 }
49 
50 template <int DIMENSIONS>
51 bool Box<DIMENSIONS>::IsBelow (int depth, Coordinate boundary) const {
52  return max_coordinates_[depth % DIMENSIONS] <= boundary;
53 }
54 
55 template <int DIMENSIONS>
56 bool Box<DIMENSIONS>::IsAbove (int depth, Coordinate boundary) const {
57  return boundary < min_coordinates_[depth % DIMENSIONS];
58 }
59 
60 template <int DIMENSIONS>
61 bool Box<DIMENSIONS>::Contains (const Box& box) const {
62  for (int k = 0; k < DIMENSIONS; ++k) {
63  // first comparison is a strict inequality
64  // because of the way IsBelow and IsAbove are
65  // defined.
66  if (!( min_coordinates_[k] < box.min_coordinates_[k]
67  && box.max_coordinates_[k] <= max_coordinates_[k]))
68  return false;
69  }
70  return true;
71 }
72 
73 template <int DIMENSIONS>
74 bool Box<DIMENSIONS>::Intersects (const Box *box) const {
75  for (int k = 0; k < DIMENSIONS; ++k) {
76  if (!( min_coordinates_[k] <= box->max_coordinates_[k]
77  && box->min_coordinates_[k] <= max_coordinates_[k]))
78  // found separating axis
79  return false;
80  }
81  return true;
82 }
83 
84 template <int DIMENSIONS>
86  return min_coordinates_;
87 }
88 
89 template <int DIMENSIONS>
91  return max_coordinates_;
92 
93 }
94 
95 } // namespace structure
96 } // namespace ugdk
97 
98 #endif
const Point & min_coordinates() const
Definition: box.h:85
Box()
Definition: box.h:36
bool IsAbove(int depth, Coordinate boundary) const
Definition: box.h:56
Point min_coordinates_
Definition: box.h:29
std::array< Coordinate, DIMENSIONS > Point
Definition: box.h:13
bool Intersects(const Box *box) const
Definition: box.h:74
Point max_coordinates_
Definition: box.h:30
Definition: animation.h:11
void UpdateBox(const Box &)
Definition: box.h:46
const Point & max_coordinates() const
Definition: box.h:90
bool IsBelow(int depth, Coordinate boundary) const
Definition: box.h:51
virtual ~Box()
Definition: box.h:43
Definition: box.h:11
bool Contains(const Box &box) const
Definition: box.h:61
double Coordinate
Definition: structure.h:7