BoundingBox-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: BoundingBox-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Dongmin Kim
5 > Purpose: Generic N-D axis-aligned bounding box class.
6 > Created Time: 2017/03/30
7 > Copyright (c) 2018, Dongmin Kim
8 *************************************************************************/
9 #ifndef CUBBYFLOW_BOUNDING_BOX_IMPL_H
10 #define CUBBYFLOW_BOUNDING_BOX_IMPL_H
11 
12 #include <Core/Math/MathUtils.h>
13 
14 namespace CubbyFlow
15 {
16  template <typename T, size_t N>
18  {
19  Reset();
20  }
21 
22  template <typename T, size_t N>
24  {
25  for (size_t i = 0; i < N; ++i)
26  {
27  lowerCorner[i] = std::min(point1[i], point2[i]);
28  upperCorner[i] = std::max(point1[i], point2[i]);
29  }
30  }
31 
32  template <typename T, size_t N>
34  lowerCorner(other.lowerCorner), upperCorner(other.upperCorner)
35  {
36  // Do nothing
37  }
38 
39  template <typename T, size_t N>
40  bool BoundingBox<T, N>::Overlaps(const BoundingBox& other) const
41  {
42  for (size_t i = 0; i < N; ++i)
43  {
44  if (upperCorner[i] < other.lowerCorner[i] || lowerCorner[i] > other.upperCorner[i])
45  {
46  return false;
47  }
48  }
49 
50  return true;
51  }
52 
53  template <typename T, size_t N>
54  bool BoundingBox<T, N>::Contains(const VectorType& point) const
55  {
56  for (size_t i = 0; i < N; ++i)
57  {
58  if (upperCorner[i] < point[i] || lowerCorner[i] > point[i])
59  {
60  return false;
61  }
62  }
63 
64  return true;
65  }
66 
67  template <typename T, size_t N>
69  {
70  Vector<T, N> result;
71 
72  for (size_t i = 0; i < N; ++i)
73  {
74  result[i] = (upperCorner[i] + lowerCorner[i]) / 2;
75  }
76 
77  return result;
78  }
79 
80  template <typename T, size_t N>
82  {
83  T result = 0;
84 
85  for (size_t i = 0; i < N; ++i)
86  {
87  result += Square(upperCorner[i] - lowerCorner[i]);
88  }
89 
90  return std::sqrt(result);
91  }
92 
93  template <typename T, size_t N>
95  {
96  T result = 0;
97 
98  for (size_t i = 0; i < N; ++i)
99  {
100  result += Square(upperCorner[i] - lowerCorner[i]);
101  }
102 
103  return result;
104  }
105 
106  template <typename T, size_t N>
108  {
109  for (size_t i = 0; i < N; ++i)
110  {
111  lowerCorner[i] = std::numeric_limits<T>::max();
112  upperCorner[i] = -std::numeric_limits<T>::max();
113  }
114  }
115 
116  template <typename T, size_t N>
118  {
119  for (size_t i = 0; i < N; ++i)
120  {
121  lowerCorner[i] = std::min(lowerCorner[i], point[i]);
122  upperCorner[i] = std::max(upperCorner[i], point[i]);
123  }
124  }
125 
126  template <typename T, size_t N>
128  {
129  for (size_t i = 0; i < N; ++i)
130  {
131  lowerCorner[i] = std::min(lowerCorner[i], other.lowerCorner[i]);
132  upperCorner[i] = std::max(upperCorner[i], other.upperCorner[i]);
133  }
134  }
135 
136  template <typename T, size_t N>
138  {
139  for (size_t i = 0; i < N; ++i)
140  {
141  lowerCorner[i] -= delta;
142  upperCorner[i] += delta;
143  }
144  }
145 }
146 
147 #endif
VectorType upperCorner
Upper corner of the bounding box.
Definition: BoundingBox.h:34
bool Contains(const VectorType &point) const
Returns true if the input point is inside of this box.
Definition: BoundingBox-Impl.h:54
VectorType MidPoint() const
Returns the mid-point of this box.
Definition: BoundingBox-Impl.h:68
BoundingBox()
Default constructor.
Definition: BoundingBox-Impl.h:17
Generic N-D axis-aligned bounding box class.
Definition: BoundingBox.h:23
T Square(T x)
Returns the square of x.
Definition: MathUtils-Impl.h:111
void Reset()
Resets this box to initial state (min=infinite, max=-infinite).
Definition: BoundingBox-Impl.h:107
Generic statically-sized N-D vector class.
Definition: Vector.h:33
Definition: pybind11Utils.h:24
VectorType lowerCorner
Lower corner of the bounding box.
Definition: BoundingBox.h:31
T DiagonalLength() const
Returns diagonal length of this box.
Definition: BoundingBox-Impl.h:81
void Merge(const VectorType &point)
Merges this and other point.
Definition: BoundingBox-Impl.h:117
bool Overlaps(const BoundingBox &other) const
Returns true of this box and other box overlaps.
Definition: BoundingBox-Impl.h:40
void Expand(T delta)
Definition: BoundingBox-Impl.h:137
T DiagonalLengthSquared() const
Returns squared diagonal length of this box.
Definition: BoundingBox-Impl.h:94