Loading...
Searching...
No Matches
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>
16
17#include <limits>
18
19namespace CubbyFlow
20{
26template <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
45template <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
56
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
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
151template <typename T>
153
154template <typename T>
156
158
160
162
164
166
168} // namespace CubbyFlow
169
171
172#endif
N-D axis-aligned bounding box class.
Definition BoundingBox.hpp:47
T Width() const
Returns width of the box.
Definition BoundingBox-Impl.hpp:64
bool Contains(const VectorType &point) const
Returns true if the input vector is inside of this box.
Definition BoundingBox-Impl.hpp:105
bool Intersects(const RayType &ray) const
Returns true if the input ray is intersecting with this box.
Definition BoundingBox-Impl.hpp:119
BoundingBoxRayIntersection< T > ClosestIntersection(const RayType &ray) const
Definition BoundingBox-Impl.hpp:148
std::enable_if_t<(N > 2), U > Depth() const
Returns depth of the box.
Definition BoundingBox-Impl.hpp:78
T Length(size_t axis)
Returns length of the box in given axis.
Definition BoundingBox-Impl.hpp:84
~BoundingBox()=default
Default destructor.
VectorType MidPoint() const
Returns the mid-point of this box.
Definition BoundingBox-Impl.hpp:194
VectorType Corner(size_t idx) const
Returns corner position. Index starts from x-first order.
Definition BoundingBox-Impl.hpp:240
T DiagonalLength() const
Returns diagonal length of this box.
Definition BoundingBox-Impl.hpp:200
void Reset()
Resets this box to initial state (min=infinite, max=-infinite).
Definition BoundingBox-Impl.hpp:212
bool IsEmpty() const
Returns true if the box is empty.
Definition BoundingBox-Impl.hpp:260
void Merge(const VectorType &point)
Merges this and other point.
Definition BoundingBox-Impl.hpp:219
BoundingBox & operator=(const BoundingBox &other)
Copy assignment operator.
Definition BoundingBox-Impl.hpp:46
void Expand(T delta)
Definition BoundingBox-Impl.hpp:233
T DiagonalLengthSquared() const
Returns squared diagonal length of this box.
Definition BoundingBox-Impl.hpp:206
BoundingBox()
Default constructor.
Definition BoundingBox-Impl.hpp:17
bool Overlaps(const BoundingBox &other) const
Returns true of this box and other box overlaps.
Definition BoundingBox-Impl.hpp:90
VectorType lowerCorner
Lower corner of the bounding box.
Definition BoundingBox.hpp:145
VectorType upperCorner
Upper corner of the bounding box.
Definition BoundingBox.hpp:148
std::enable_if_t<(N > 1), U > Height() const
Returns height of the box.
Definition BoundingBox-Impl.hpp:71
BoundingBox< U, N > CastTo() const
Returns box with different value type.
Definition BoundingBox-Impl.hpp:275
VectorType Clamp(const VectorType &point) const
Returns the clamped point.
Definition BoundingBox-Impl.hpp:253
Definition Matrix.hpp:30
Class for N-D ray.
Definition Ray.hpp:26
Definition pybind11Utils.hpp:21
Box-ray intersection result.
Definition BoundingBox.hpp:28
T near
Distance to the first intersection point.
Definition BoundingBox.hpp:33
bool isIntersecting
True if the box and ray intersects.
Definition BoundingBox.hpp:30
T far
Distance to the second (and the last) intersection point.
Definition BoundingBox.hpp:36