BoundingBox.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_BOUNDING_BOX_HPP
12 #define CUBBYFLOW_BOUNDING_BOX_HPP
13 
14 #include <Core/Geometry/Ray.hpp>
15 #include <Core/Matrix/Matrix.hpp>
16 
17 #include <limits>
18 
19 namespace CubbyFlow
20 {
26 template <typename T>
28 {
30  bool isIntersecting = false;
31 
33  T near = std::numeric_limits<T>::max();
34 
36  T far = std::numeric_limits<T>::max();
37 };
38 
45 template <typename T, size_t N>
47 {
48  public:
49  static_assert(N > 0, "Dimension should be greater than 0");
50  static_assert(
51  std::is_floating_point<T>::value,
52  "BoundingBox only can be instantiated with floating point types");
53 
55  using RayType = Ray<T, N>;
56 
58  BoundingBox();
59 
61  BoundingBox(const VectorType& point1, const VectorType& point2);
62 
64  ~BoundingBox() = default;
65 
67  BoundingBox(const BoundingBox& other);
68 
70  BoundingBox(BoundingBox&& other) noexcept;
71 
73  BoundingBox& operator=(const BoundingBox& other);
74 
76  BoundingBox& operator=(BoundingBox&& other) noexcept;
77 
79  [[nodiscard]] T Width() const;
80 
82  template <typename U = T>
83  [[nodiscard]] std::enable_if_t<(N > 1), U> Height() const;
84 
86  template <typename U = T>
87  [[nodiscard]] std::enable_if_t<(N > 2), U> Depth() const;
88 
90  [[nodiscard]] T Length(size_t axis);
91 
93  [[nodiscard]] bool Overlaps(const BoundingBox& other) const;
94 
96  [[nodiscard]] bool Contains(const VectorType& point) const;
97 
99  [[nodiscard]] bool Intersects(const RayType& ray) const;
100 
105  [[nodiscard]] BoundingBoxRayIntersection<T> ClosestIntersection(
106  const RayType& ray) const;
107 
109  [[nodiscard]] VectorType MidPoint() const;
110 
112  [[nodiscard]] T DiagonalLength() const;
113 
115  [[nodiscard]] T DiagonalLengthSquared() const;
116 
118  void Reset();
119 
121  void Merge(const VectorType& point);
122 
124  void Merge(const BoundingBox& other);
125 
129  void Expand(T delta);
130 
132  [[nodiscard]] VectorType Corner(size_t idx) const;
133 
135  [[nodiscard]] VectorType Clamp(const VectorType& point) const;
136 
138  [[nodiscard]] bool IsEmpty() const;
139 
141  template <typename U>
142  [[nodiscard]] BoundingBox<U, N> CastTo() const;
143 
146 
149 };
150 
151 template <typename T>
153 
154 template <typename T>
156 
158 
160 
162 
164 
166 
168 } // namespace CubbyFlow
169 
171 
172 #endif
VectorType upperCorner
Upper corner of the bounding box.
Definition: BoundingBox.hpp:148
Class for N-D ray.
Definition: Ray.hpp:25
std::enable_if_t< std::is_arithmetic< T >::value, T > Clamp(T val, T low, T high)
Returns the clamped value.
Definition: MathUtils-Impl.hpp:166
T near
Distance to the first intersection point.
Definition: BoundingBox.hpp:33
N-D axis-aligned bounding box class.
Definition: BoundingBox.hpp:46
bool isIntersecting
True if the box and ray intersects.
Definition: BoundingBox.hpp:30
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
void Merge(RandomIterator a, size_t size, RandomIterator2 temp, CompareFunction compareFunction)
Definition: Parallel-Impl.hpp:107
T far
Distance to the second (and the last) intersection point.
Definition: BoundingBox.hpp:36
VectorType lowerCorner
Lower corner of the bounding box.
Definition: BoundingBox.hpp:145
Box-ray intersection result.
Definition: BoundingBox.hpp:27